File indexing completed on 2024-12-22 04:17:44

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 
0013 #include "scalardialog.h"
0014 #include "application.h"
0015 
0016 #include "datasourceconfiguredialog.h"
0017 
0018 #include "enodes.h"
0019 #include "document.h"
0020 #include "objectstore.h"
0021 #include "datascalar.h"
0022 #include "vscalar.h"
0023 #include "dialogdefaults.h"
0024 #include "datasourcepluginmanager.h"
0025 
0026 #include <QPushButton>
0027 #include <QThreadPool>
0028 
0029 namespace Kst {
0030 
0031 ScalarTab::ScalarTab(ObjectStore *store, QWidget *parent)
0032   : DataTab(parent),  _mode(DataScalar), _store(store), _requestID(0) {
0033 
0034   setupUi(this);
0035   setTabTitle(tr("Scalar"));
0036 
0037   setDataOptions();
0038 
0039   connect(_scalarValue, SIGNAL(textChanged(QString)), this, SLOT(entryChanged()));
0040   connect(_start, SIGNAL(valueChanged(int)), this, SLOT(entryChanged()));
0041   connect(_countFromEnd, SIGNAL(clicked()), this, SLOT(entryChanged()));
0042   connect(_field, SIGNAL(editTextChanged(QString)), this, SLOT(entryChanged()));
0043   connect(_fieldRV, SIGNAL(editTextChanged(QString)), this, SLOT(entryChanged()));
0044   connect(_readFromSource, SIGNAL(toggled(bool)), this, SLOT(readFromSourceChanged()));
0045   connect(_readFromRVector, SIGNAL(toggled(bool)), this, SLOT(readFromSourceChanged()));
0046   connect(_fileName, SIGNAL(changed(QString)), this, SLOT(fileNameChanged(QString)));
0047   connect(_configure, SIGNAL(clicked()), this, SLOT(showConfigWidget()));
0048 }
0049 
0050 
0051 ScalarTab::~ScalarTab() {
0052 }
0053 
0054 
0055 QString ScalarTab::value() const {
0056   return _scalarValue->text();
0057 }
0058 
0059 
0060 void ScalarTab::setValue(const QString &value) {
0061   _scalarValue->setText(value);
0062 }
0063 
0064 
0065 void ScalarTab::entryChanged() {
0066   _start->setEnabled(_readFromRVector->isChecked() && !_countFromEnd->isChecked());
0067   emit valueChanged();
0068 }
0069 
0070 
0071 void ScalarTab::readFromSourceChanged() {
0072 
0073   if (_readFromSource->isChecked()) {
0074     _mode = DataScalar;
0075     setDataOptions();
0076   } else if (_readFromRVector->isChecked()) {
0077     _mode = RVectorScalar;
0078     setRVOptions();
0079   } else {
0080     _mode = GeneratedScalar;
0081   }
0082   _dataScalarGroup->setEnabled((_readFromSource->isChecked())||(_readFromRVector->isChecked()));
0083 
0084   bool isRV = _readFromRVector->isChecked();
0085 
0086   label_6->setEnabled(isRV);
0087   label_7->setEnabled(isRV);
0088   _start->setEnabled(isRV && !_countFromEnd->isChecked());
0089   _countFromEnd->setEnabled(isRV);
0090 
0091  _generatedScalarGroup->setEnabled(_generateX->isChecked());
0092 
0093   emit sourceChanged();
0094 }
0095 
0096 
0097 
0098 void ScalarTab::hideGeneratedOptions() {
0099   _readFromSource->setVisible(false);
0100   _generateX->setVisible(false);
0101   _readFromRVector->setVisible(false);
0102   _generatedScalarGroup->setVisible(false);
0103 }
0104 
0105 
0106 void ScalarTab::hideDataOptions() {
0107   _readFromSource->setVisible(false);
0108   _generateX->setVisible(false);
0109   _readFromRVector->setVisible(false);
0110   _dataScalarGroup->setVisible(false);
0111   _generatedScalarGroup->setEnabled(true);
0112 }
0113 
0114 void ScalarTab::setRVOptions() {
0115  // replace in right order to avoid flicker
0116  _field->setVisible(false);
0117  label_5->setVisible(false);
0118  _fieldRV->setVisible(true);
0119  label_8->setVisible(true);
0120  _readFromRVector->setChecked(true);
0121 }
0122 
0123 
0124 void ScalarTab::setDataOptions() {
0125   _fieldRV->setVisible(false);
0126   label_8->setVisible(false);
0127   _field->setVisible(true);
0128   label_5->setVisible(true);
0129   _readFromSource->setChecked(true);
0130 }
0131 
0132 
0133 DataSourcePtr ScalarTab::dataSource() const {
0134   return _dataSource;
0135 }
0136 
0137 
0138 void ScalarTab::setDataSource(DataSourcePtr dataSource) {
0139   _dataSource = dataSource;
0140 }
0141 
0142 
0143 QString ScalarTab::file() const {
0144   return _fileName->file();
0145 }
0146 
0147 
0148 void ScalarTab::setFile(const QString &file) {
0149   _fileName->setFile(file);
0150 }
0151 
0152 
0153 QString ScalarTab::field() const {
0154   return _field->currentText();
0155 }
0156 
0157 
0158 void ScalarTab::setField(const QString &field) {
0159   _current_field = field;
0160   _field->setCurrentIndex(_field->findText(field));
0161 }
0162 
0163 
0164 QString ScalarTab::fieldRV() const {
0165   return _fieldRV->currentText();
0166 }
0167 
0168 
0169 void ScalarTab::setFieldRV(const QString &field) {
0170   _current_rvfield = field;
0171   _fieldRV->setCurrentIndex(_fieldRV->findText(field));
0172 }
0173 
0174 void ScalarTab::setF0(int f0) {
0175   _start->setValue(f0);
0176 }
0177 
0178 int ScalarTab::F0() const {
0179   if (_countFromEnd->isChecked() == true) {
0180     return -1;
0181   } else {
0182     return _start->text().toInt();
0183   }
0184 }
0185 
0186 void ScalarTab::updateDataSource() {
0187   fileNameChanged(_fileName->file());
0188 }
0189 
0190 
0191 void ScalarTab::sourceValid(QString filename, int requestID) {
0192   if (_requestID != requestID) {
0193     return;
0194   }
0195   _dataSource = DataSourcePluginManager::findOrLoadSource(_store, filename);
0196 
0197   _field->setEnabled(true);
0198   _fieldRV->setEnabled(true);
0199 
0200   _dataSource->readLock();
0201 
0202   _field->addItems(_dataSource->scalar().list());
0203   setField(_current_field);
0204   _field->setEditable(!_dataSource->scalar().isListComplete());
0205   _fieldRV->addItems(_dataSource->vector().list());
0206   setFieldRV(_current_rvfield);
0207   _fieldRV->setEditable(!_dataSource->vector().isListComplete());
0208   _configure->setEnabled(_dataSource->hasConfigWidget());
0209 
0210   _dataSource->unlock();
0211   modified();
0212 
0213   _store->cleanUpDataSourceList();
0214   emit sourceChanged();
0215 }
0216 
0217 
0218 
0219 void ScalarTab::fileNameChanged(const QString &file) {
0220   _field->clear();
0221   _fieldRV->clear();
0222   _field->setEnabled(false);
0223   _fieldRV->setEnabled(false);
0224   _configure->setEnabled(false);
0225   emit sourceChanged();
0226 
0227   _requestID += 1;
0228   ValidateDataSourceThread *validateDSThread = new ValidateDataSourceThread(file, _requestID);
0229   connect(validateDSThread, SIGNAL(dataSourceValid(QString,int)), this, SLOT(sourceValid(QString,int)));
0230   QThreadPool::globalInstance()->start(validateDSThread);
0231 }
0232 
0233 
0234 void ScalarTab::showConfigWidget() {
0235   QPointer<DataSourceConfigureDialog> dialog = new DataSourceConfigureDialog(dataDialog()->editMode(), _dataSource, this);
0236   if ( dialog->exec() == QDialog::Accepted ) {
0237     fileNameChanged(_dataSource->fileName());
0238   }
0239   delete dialog;
0240 }
0241 
0242 
0243 ScalarDialog::ScalarDialog(ObjectPtr dataObject, QWidget *parent)
0244   : DataDialog(dataObject, parent) {
0245 
0246   if (editMode() == Edit)
0247     setWindowTitle(tr("Edit Scalar"));
0248   else
0249     setWindowTitle(tr("New Scalar"));
0250 
0251   _scalarTab = new ScalarTab(_document->objectStore(), this);
0252   addDataTab(_scalarTab);
0253 
0254   if (editMode() == Edit) {
0255     configureTab(dataObject);
0256   } else {
0257     configureTab(0);
0258   }
0259 
0260   connect(_scalarTab, SIGNAL(valueChanged()), this, SLOT(updateButtons()));
0261   connect(_scalarTab, SIGNAL(valueChanged()), this, SLOT(modified()));
0262   connect(_scalarTab, SIGNAL(destroyed()), kstApp->mainWindow(), SLOT(cleanUpDataSourceList()));
0263   updateButtons();
0264 }
0265 
0266 
0267 ScalarDialog::~ScalarDialog() {
0268 }
0269 
0270 void ScalarDialog::configureTab(ObjectPtr object) {
0271   if (DataScalarPtr dataScalar = kst_cast<DataScalar>(object)) {
0272     _scalarTab->setFile(dataScalar->dataSource()->fileName());
0273     _scalarTab->setDataSource(dataScalar->dataSource());
0274     _scalarTab->setField(dataScalar->field());
0275     _scalarTab->hideGeneratedOptions();
0276     _scalarTab->setDataOptions();
0277   } else if (VScalarPtr vScalar = kst_cast<VScalar>(object)) {
0278     _scalarTab->setFile(vScalar->dataSource()->fileName());
0279     _scalarTab->setDataSource(vScalar->dataSource());
0280     _scalarTab->setFieldRV(vScalar->field());
0281     _scalarTab->setF0(vScalar->F0());
0282     _scalarTab->hideGeneratedOptions();
0283     _scalarTab->setRVOptions();
0284   } else if (ScalarPtr scalar = kst_cast<Scalar>(object)) { // edit value scalar
0285     _scalarTab->hideDataOptions();
0286     _scalarTab->setValue(QString::number(scalar->value()));
0287   } else { // new scalar
0288     _scalarTab->setFile(dialogDefaults().value("vector/datasource",_scalarTab->file()).toString());
0289   }
0290 }
0291 
0292 void ScalarDialog::updateButtons() {
0293   bool valid=false;
0294 
0295   switch(_scalarTab->scalarMode()) {
0296   case ScalarTab::DataScalar:
0297     valid = !_scalarTab->field().isEmpty();
0298     break;
0299   case ScalarTab::RVectorScalar:
0300     valid = !_scalarTab->fieldRV().isEmpty();
0301     break;
0302   case ScalarTab::GeneratedScalar:
0303     valid = !_scalarTab->value().isEmpty();
0304     break;
0305   default:
0306     valid = false;
0307   }
0308 
0309   _buttonBox->button(QDialogButtonBox::Ok)->setEnabled(valid);
0310   //_buttonBox->button(QDialogButtonBox::Apply)->setEnabled(valid);
0311 }
0312 
0313 
0314 ObjectPtr ScalarDialog::createNewDataObject() {
0315   switch(_scalarTab->scalarMode()) {
0316   case ScalarTab::DataScalar:
0317     return createNewDataScalar();
0318   case ScalarTab::RVectorScalar:
0319     return createNewVScalar();
0320   case ScalarTab::GeneratedScalar:
0321     return createNewGeneratedScalar();
0322   default:
0323     return 0;
0324   }
0325 }
0326 
0327 ObjectPtr ScalarDialog::createNewGeneratedScalar(){
0328   bool ok = false;
0329 
0330   Q_ASSERT(_document && _document->objectStore());
0331 
0332   double value = _scalarTab->value().toDouble(&ok);
0333   if (!ok) {
0334     value = Equations::interpret(_document->objectStore(), _scalarTab->value().toLatin1(), &ok);
0335   }
0336 
0337   if (!ok) {
0338     return 0; //invalid
0339   }
0340 
0341   ScalarPtr scalar = _document->objectStore()->createObject<Scalar>();
0342   scalar->setValue(value);
0343   scalar->setOrphan(true);
0344   scalar->setEditable(true);
0345   if (DataDialog::tagStringAuto()) {
0346      scalar->setDescriptiveName(QString());
0347   } else {
0348      scalar->setDescriptiveName(DataDialog::tagString());
0349   }
0350 
0351   scalar->writeLock();
0352   scalar->registerChange();
0353   scalar->unlock();
0354 
0355   _dataObjectName = scalar->Name();
0356 
0357   //UpdateServer::self()->requestUpdateSignal();
0358   _scalarTab->setDataSource(0L);
0359   _document->objectStore()->cleanUpDataSourceList();
0360 
0361   return static_cast<ObjectPtr>(scalar);
0362 }
0363 
0364 
0365 ObjectPtr ScalarDialog::createNewDataScalar() {
0366   const DataSourcePtr dataSource = _scalarTab->dataSource();
0367 
0368   if (!dataSource)
0369     return 0;
0370 
0371   const QString field = _scalarTab->field();
0372 
0373   Q_ASSERT(_document && _document->objectStore());
0374 
0375   DataScalarPtr scalar = _document->objectStore()->createObject<DataScalar>();
0376 
0377   scalar->writeLock();
0378   scalar->change(dataSource, field);
0379 
0380   if (DataDialog::tagStringAuto()) {
0381      scalar->setDescriptiveName(QString());
0382   } else {
0383      scalar->setDescriptiveName(DataDialog::tagString());
0384   }
0385 
0386   scalar->registerChange();
0387   scalar->unlock();
0388 
0389   _dataObjectName = scalar->Name();
0390 
0391   //UpdateServer::self()->requestUpdateSignal();
0392 
0393   return scalar;
0394 }
0395 
0396 
0397 ObjectPtr ScalarDialog::createNewVScalar() {
0398   const DataSourcePtr dataSource = _scalarTab->dataSource();
0399 
0400   if (!dataSource)
0401     return 0;
0402 
0403   const QString field = _scalarTab->fieldRV();
0404   const int f0 = _scalarTab->F0();
0405 
0406   Q_ASSERT(_document && _document->objectStore());
0407 
0408   VScalarPtr scalar = _document->objectStore()->createObject<VScalar>();
0409 
0410   scalar->writeLock();
0411   scalar->change(dataSource, field, f0);
0412 
0413   if (DataDialog::tagStringAuto()) {
0414      scalar->setDescriptiveName(QString());
0415   } else {
0416      scalar->setDescriptiveName(DataDialog::tagString());
0417   }
0418 
0419   scalar->registerChange();
0420   scalar->unlock();
0421 
0422   _dataObjectName = scalar->Name();
0423 
0424   kstApp->mainWindow()->updateRecentDataFiles(dataSource->fileName());
0425   //dialogDefaults().setValue("vector/datasource", dataSource->fileName());
0426 
0427   return scalar;
0428 }
0429 
0430 
0431 ObjectPtr ScalarDialog::editExistingDataObject() const {
0432   if (DataScalarPtr scalar = kst_cast<DataScalar>(dataObject())) {
0433     const DataSourcePtr dataSource = _scalarTab->dataSource();
0434 
0435     if (dataSource) {
0436       const QString field = _scalarTab->field();
0437       scalar->writeLock();
0438       scalar->change(dataSource, field);
0439 
0440       if (DataDialog::tagStringAuto()) {
0441         scalar->setDescriptiveName(QString());
0442       } else {
0443         scalar->setDescriptiveName(DataDialog::tagString());
0444       }
0445 
0446       scalar->registerChange();
0447       scalar->unlock();
0448     }
0449   } else if (VScalarPtr scalar = kst_cast<VScalar>(dataObject())) {
0450     const DataSourcePtr dataSource = _scalarTab->dataSource();
0451 
0452     if (dataSource) {
0453       const QString field = _scalarTab->fieldRV();
0454       int f0 = _scalarTab->F0();
0455       scalar->writeLock();
0456       scalar->change(dataSource, field, f0);
0457 
0458       if (DataDialog::tagStringAuto()) {
0459         scalar->setDescriptiveName(QString());
0460       } else {
0461         scalar->setDescriptiveName(DataDialog::tagString());
0462       }
0463 
0464       scalar->registerChange();
0465       scalar->unlock();
0466     }
0467   } else   if (ScalarPtr scalar = kst_cast<Scalar>(dataObject())) {
0468     bool ok;
0469     double value = _scalarTab->value().toDouble(&ok);
0470     if (!ok) {
0471       value = Equations::interpret(_document->objectStore(), _scalarTab->value().toLatin1(), &ok);
0472     }
0473     if (DataDialog::tagStringAuto()) {
0474       scalar->setDescriptiveName(QString());
0475     } else {
0476       scalar->setDescriptiveName(DataDialog::tagString());
0477     }
0478     scalar->writeLock();
0479     scalar->setValue(value);
0480     scalar->registerChange();
0481     scalar->unlock();
0482   }
0483 
0484   return dataObject();
0485 }
0486 
0487 }
0488 
0489 // vim: ts=2 sw=2 et