File indexing completed on 2024-12-22 04:17:46
0001 /*************************************************************************** 0002 * * 0003 * copyright : (C) 2007 The University of Toronto * 0004 * netterfield@astro.utoronto.ca * 0005 * * 0006 * This program is free software; you can redistribute it and/or modify * 0007 * it under the terms of the GNU General Public License as published by * 0008 * the Free Software Foundation; either version 2 of the License, or * 0009 * (at your option) any later version. * 0010 * * 0011 ***************************************************************************/ 0012 #include "stringdialog.h" 0013 0014 #include "application.h" 0015 #include "datasourceconfiguredialog.h" 0016 #include "enodes.h" 0017 #include "document.h" 0018 #include "objectstore.h" 0019 #include "datastring.h" 0020 #include "dialogdefaults.h" 0021 #include "datasourcepluginmanager.h" 0022 0023 #include <QPushButton> 0024 #include <QThreadPool> 0025 0026 namespace Kst { 0027 0028 StringTab::StringTab(ObjectStore *store, QWidget *parent) 0029 : DataTab(parent), _mode(DataString), _store(store), _requestID(0) { 0030 0031 setupUi(this); 0032 setTabTitle(tr("String")); 0033 0034 _fileNameLabel->setBuddy(_fileName->_fileEdit); 0035 0036 connect(_generatedStringGroup, SIGNAL(toggled(bool)), this, SLOT(generateClicked())); 0037 connect(_dataStringGroup, SIGNAL(toggled(bool)), this, SLOT(readFromSourceClicked())); 0038 connect(_stringValue, SIGNAL(textChanged(QString)), this, SLOT(textChanged())); 0039 connect(_fileName, SIGNAL(changed(QString)), this, SLOT(fileNameChanged(QString))); 0040 connect(_field, SIGNAL(currentTextChanged(QString)), this, SLOT(fieldChanged(QString))); 0041 connect(_configure, SIGNAL(clicked()), this, SLOT(showConfigWidget())); 0042 connect(_field, SIGNAL(currentIndexChanged(int)), this, SLOT(textChanged())); 0043 connect(_frame, SIGNAL(valueChanged(int)), this, SLOT(textChanged())); 0044 connect(_lastFrame, SIGNAL(clicked(bool)), this, SLOT(enableFrames())); 0045 } 0046 0047 0048 StringTab::~StringTab() { 0049 } 0050 0051 0052 void StringTab::generateClicked() { 0053 if (_generatedStringGroup->isChecked()) 0054 setStringMode(GeneratedString); 0055 else 0056 setStringMode(DataString); 0057 0058 emit sourceChanged(); 0059 } 0060 0061 void StringTab::readFromSourceClicked() { 0062 if (_dataStringGroup->isChecked()) 0063 setStringMode(DataString); 0064 else 0065 setStringMode(GeneratedString); 0066 0067 emit sourceChanged(); 0068 } 0069 0070 0071 QString StringTab::value() const { 0072 return _stringValue->text(); 0073 } 0074 0075 0076 void StringTab::setValue(const QString &value) { 0077 _stringValue->setText(value); 0078 } 0079 0080 0081 void StringTab::textChanged() { 0082 emit valueChanged(); 0083 } 0084 0085 0086 int StringTab::frame() const { 0087 if (_lastFrame->isChecked()) { 0088 return (-1); 0089 } else { 0090 return _frame->value(); 0091 } 0092 } 0093 0094 0095 void StringTab::setFrame(int frame) { 0096 if (frame<0) { 0097 _lastFrame->setChecked(true); 0098 _frame->setEnabled(false); 0099 } else { 0100 _lastFrame->setChecked(false); 0101 _frame ->setValue(frame); 0102 _frame->setEnabled(true); 0103 } 0104 } 0105 0106 0107 void StringTab::hideGeneratedOptions() { 0108 _generatedStringGroup->setVisible(false); 0109 _dataStringGroup->setCheckable(false); 0110 _dataStringGroup->setTitle(""); 0111 _dataStringGroup->setFlat(true); 0112 int top_margin; 0113 _dataStringGroup->layout()->getContentsMargins(NULL,&top_margin,NULL,NULL); 0114 _dataStringGroup->layout()->setContentsMargins(0,top_margin,0,0); 0115 0116 } 0117 0118 0119 void StringTab::hideDataOptions() { 0120 _dataStringGroup->setVisible(false); 0121 _generatedStringGroup->setCheckable(false); 0122 _generatedStringGroup->setTitle(""); 0123 _generatedStringGroup->setFlat(true); 0124 int top_margin; 0125 _generatedStringGroup->layout()->getContentsMargins(NULL,&top_margin,NULL,NULL); 0126 _generatedStringGroup->layout()->setContentsMargins(0,top_margin,0,0); 0127 } 0128 0129 0130 DataSourcePtr StringTab::dataSource() const { 0131 return _dataSource; 0132 } 0133 0134 0135 void StringTab::setDataSource(DataSourcePtr dataSource) { 0136 _dataSource = dataSource; 0137 } 0138 0139 0140 void StringTab::setStringMode(StringMode mode) { 0141 _mode = mode; 0142 _dataStringGroup->setChecked(mode == DataString); 0143 _generatedStringGroup->setChecked(mode == GeneratedString); 0144 } 0145 0146 0147 QString StringTab::file() const { 0148 return _fileName->file(); 0149 } 0150 0151 0152 void StringTab::setFile(const QString &file) { 0153 _fileName->setFile(file); 0154 } 0155 0156 0157 QString StringTab::field() const { 0158 return _field->currentText(); 0159 } 0160 0161 0162 void StringTab::setField(const QString &field) { 0163 _current_field = field; 0164 _field->setCurrentIndex(_field->findText(field)); 0165 } 0166 0167 0168 void StringTab::setFieldList(const QStringList &fieldList) { 0169 _field->clear(); 0170 _field->addItems(fieldList); 0171 } 0172 0173 0174 void StringTab::updateDataSource() { 0175 fileNameChanged(_fileName->file()); 0176 } 0177 0178 0179 void StringTab::sourceValid(QString filename, int requestID) { 0180 if (_requestID != requestID) { 0181 return; 0182 } 0183 _dataSource = DataSourcePluginManager::findOrLoadSource(_store, filename); 0184 0185 _field->setEnabled(true); 0186 0187 _dataSource->readLock(); 0188 0189 _field->addItems(_dataSource->string().list()); 0190 setField(_current_field); 0191 _field->setEditable(!_dataSource->string().isListComplete()); 0192 _configure->setEnabled(_dataSource->hasConfigWidget()); 0193 0194 bool isStringStream = _dataSource->isStringStream(_field->currentText()); 0195 _frame->setVisible(isStringStream); 0196 _frameLabel->setVisible(isStringStream); 0197 _lastFrame->setVisible(isStringStream); 0198 0199 _dataSource->unlock(); 0200 0201 _store->cleanUpDataSourceList(); 0202 0203 emit sourceChanged(); 0204 } 0205 0206 void StringTab::enableFrames() { 0207 _frame->setEnabled(!_lastFrame->isChecked()); 0208 } 0209 0210 0211 void StringTab::fileNameChanged(const QString &file) { 0212 _field->clear(); 0213 _field->setEnabled(false); 0214 _configure->setEnabled(false); 0215 emit sourceChanged(); 0216 0217 _requestID += 1; 0218 ValidateDataSourceThread *validateDSThread = new ValidateDataSourceThread(file, _requestID); 0219 connect(validateDSThread, SIGNAL(dataSourceValid(QString,int)), this, SLOT(sourceValid(QString,int))); 0220 QThreadPool::globalInstance()->start(validateDSThread); 0221 } 0222 0223 void StringTab::fieldChanged(const QString &field) { 0224 bool isImageStream = _dataSource->isStringStream(field); 0225 _frame->setVisible(isImageStream); 0226 _frameLabel->setVisible(isImageStream); 0227 _lastFrame->setVisible(isImageStream); 0228 } 0229 0230 0231 void StringTab::showConfigWidget() { 0232 QPointer<DataSourceConfigureDialog> dialog = new DataSourceConfigureDialog(dataDialog()->editMode(), _dataSource, this); 0233 if ( dialog->exec() == QDialog::Accepted ) { 0234 fileNameChanged(_dataSource->fileName()); 0235 } 0236 delete dialog; 0237 } 0238 0239 0240 StringDialog::StringDialog(ObjectPtr dataObject, QWidget *parent) 0241 : DataDialog(dataObject, parent) { 0242 0243 if (editMode() == Edit) 0244 setWindowTitle(tr("Edit String")); 0245 else 0246 setWindowTitle(tr("New String")); 0247 0248 _stringTab = new StringTab(_document->objectStore(), this); 0249 addDataTab(_stringTab); 0250 0251 if (editMode() == Edit) { 0252 configureTab(dataObject); 0253 } else { 0254 configureTab(0); 0255 } 0256 0257 connect(_stringTab, SIGNAL(valueChanged()), this, SLOT(updateButtons())); 0258 connect(_stringTab, SIGNAL(sourceChanged()), this, SLOT(updateButtons())); 0259 connect(_stringTab, SIGNAL(destroyed()), kstApp->mainWindow(), SLOT(cleanUpDataSourceList())); 0260 updateButtons(); 0261 } 0262 0263 0264 StringDialog::~StringDialog() { 0265 } 0266 0267 0268 // QString StringDialog::tagString() const { 0269 // return DataDialog::tagString(); 0270 // } 0271 0272 0273 void StringDialog::configureTab(ObjectPtr object) { 0274 if (DataStringPtr dataString = kst_cast<DataString>(object)) { 0275 _stringTab->setFile(dataString->dataSource()->fileName()); 0276 _stringTab->setDataSource(dataString->dataSource()); 0277 _stringTab->setField(dataString->field()); 0278 _stringTab->hideGeneratedOptions(); 0279 _stringTab->setFrame(dataString->frame()); 0280 } else if (StringPtr string = kst_cast<String>(object)) { // edit value string 0281 _stringTab->hideDataOptions(); 0282 _stringTab->setValue(string->value()); 0283 } else { // new string 0284 _stringTab->setFile(dialogDefaults().value("String/datasource",_stringTab->file()).toString()); 0285 } 0286 } 0287 0288 void StringDialog::updateButtons() { 0289 bool enable = !_stringTab->value().isEmpty() 0290 || !_stringTab->field().isEmpty(); 0291 0292 _buttonBox->button(QDialogButtonBox::Ok)->setEnabled(enable); 0293 _buttonBox->button(QDialogButtonBox::Apply)->setEnabled(enable); 0294 } 0295 0296 0297 ObjectPtr StringDialog::createNewDataObject() { 0298 switch(_stringTab->stringMode()) { 0299 case StringTab::DataString: 0300 return createNewDataString(); 0301 case StringTab::GeneratedString: 0302 return createNewGeneratedString(); 0303 default: 0304 return 0; 0305 } 0306 } 0307 0308 ObjectPtr StringDialog::createNewGeneratedString(){ 0309 0310 Q_ASSERT(_document && _document->objectStore()); 0311 0312 QString value = _stringTab->value(); 0313 0314 // qDebug() << "Creating new string ===>" 0315 // << "\n\ttag:" << DataDialog::tagString() 0316 // << "\n\tvalue:" << value 0317 // << endl; 0318 0319 StringPtr string = _document->objectStore()->createObject<String>(); 0320 string->setValue(value); 0321 string->setOrphan(true); 0322 string->setEditable(true); 0323 if (DataDialog::tagStringAuto()) { 0324 string->setDescriptiveName(QString()); 0325 } else { 0326 string->setDescriptiveName(DataDialog::tagString()); 0327 } 0328 0329 string->writeLock(); 0330 string->registerChange(); 0331 string->unlock(); 0332 0333 _dataObjectName = string->Name(); 0334 0335 _stringTab->setDataSource(0L); 0336 _document->objectStore()->cleanUpDataSourceList(); 0337 0338 return static_cast<ObjectPtr>(string); 0339 } 0340 0341 0342 ObjectPtr StringDialog::createNewDataString() { 0343 const DataSourcePtr dataSource = _stringTab->dataSource(); 0344 0345 if (!dataSource) 0346 return 0; 0347 0348 const QString field = _stringTab->field(); 0349 0350 Q_ASSERT(_document && _document->objectStore()); 0351 0352 DataStringPtr string = _document->objectStore()->createObject<DataString>(); 0353 0354 string->writeLock(); 0355 string->change(dataSource, field, _stringTab->frame()); 0356 0357 if (DataDialog::tagStringAuto()) { 0358 string->setDescriptiveName(QString()); 0359 } else { 0360 string->setDescriptiveName(DataDialog::tagString()); 0361 } 0362 0363 string->registerChange(); 0364 string->unlock(); 0365 0366 _dataObjectName = string->Name(); 0367 0368 //UpdateServer::self()->requestUpdateSignal(); 0369 0370 dialogDefaults().setValue("String/datasource", _stringTab->file()); 0371 0372 return string; 0373 } 0374 0375 0376 ObjectPtr StringDialog::editExistingDataObject() const { 0377 if (_stringTab->stringMode()==StringTab::GeneratedString) { 0378 if (StringPtr string = kst_cast<String>(dataObject())) { 0379 QString value = _stringTab->value(); 0380 if (DataDialog::tagStringAuto()) { 0381 string->setDescriptiveName(QString()); 0382 } else { 0383 string->setDescriptiveName(DataDialog::tagString()); 0384 } 0385 string->writeLock(); 0386 string->setValue(value); 0387 string->registerChange(); 0388 string->unlock(); 0389 } 0390 } else if (_stringTab->stringMode()==StringTab::DataString) { 0391 if (DataStringPtr string = kst_cast<DataString>(dataObject())) { 0392 const DataSourcePtr dataSource = _stringTab->dataSource(); 0393 0394 if (!dataSource) 0395 return 0; 0396 0397 const QString field = _stringTab->field(); 0398 0399 string->writeLock(); 0400 if (DataDialog::tagStringAuto()) { 0401 string->setDescriptiveName(QString()); 0402 } else { 0403 string->setDescriptiveName(DataDialog::tagString()); 0404 } 0405 string->change(dataSource, field, _stringTab->frame()); 0406 string->registerChange(); 0407 string->unlock(); 0408 dialogDefaults().setValue("String/datasource", _stringTab->file()); 0409 } 0410 } 0411 return dataObject(); 0412 } 0413 0414 } 0415 0416 // vim: ts=2 sw=2 et