File indexing completed on 2024-12-22 04:17:32
0001 /*************************************************************************** 0002 * * 0003 * copyright : (C) 2016 C. Barth Netterfield * 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 "filtermultipledialog.h" 0014 #include "ui_filtermultipledialog.h" 0015 0016 #include "objectstore.h" 0017 #include "mainwindow.h" 0018 #include "application.h" 0019 #include "document.h" 0020 #include "basicplugin.h" 0021 #include "updatemanager.h" 0022 #include "updateserver.h" 0023 #include "curve.h" 0024 #include "plotiteminterface.h" 0025 #include "plotitem.h" 0026 #include "datacollection.h" 0027 #include "geticon.h" 0028 #include "psd.h" 0029 #include "histogram.h" 0030 0031 #include <QMessageBox> 0032 0033 namespace Kst { 0034 0035 FilterMultipleDialog::FilterMultipleDialog(QWidget *parent) : 0036 QDialog(parent), ui(new Ui::FilterMultipleDialog), 0037 _configWidget(0), _layout(0), _store(0) 0038 { 0039 if (MainWindow *mw = qobject_cast<MainWindow*>(parent)) { 0040 _store = mw->document()->objectStore(); 0041 } else { 0042 // FIXME: we need the object store 0043 qFatal("ERROR: can't construct a FilterMultipleDialog without the object store"); 0044 } 0045 0046 ui->setupUi(this); 0047 0048 ui->_add->setIcon(KstGetIcon("kst_rightarrow")); 0049 ui->_addAll->setIcon(KstGetIcon("kst_rightarrow")); 0050 ui->_timestreams->setIcon(KstGetIcon("kst_rightarrow")); 0051 ui->_remove->setIcon(KstGetIcon("kst_leftarrow")); 0052 ui->_removeAll->setIcon(KstGetIcon("kst_leftarrow")); 0053 0054 ui->_pluginCombo->addItems(DataObject::filterPluginList()); 0055 0056 connect(ui->_add, SIGNAL(clicked()), this, SLOT(addButtonClicked())); 0057 connect(ui->_timestreams, SIGNAL(clicked()), this, SLOT(smartButtonClicked())); 0058 connect(ui->_remove, SIGNAL(clicked()), this, SLOT(removeButtonClicked())); 0059 connect(ui->_addAll, SIGNAL(clicked()), this, SLOT(addAll())); 0060 connect(ui->_removeAll, SIGNAL(clicked()), this, SLOT(removeAll())); 0061 0062 connect(ui->_selectedVectors, SIGNAL(itemSelectionChanged()), this, SLOT(updateButtons())); 0063 connect(ui->_availableVectors, SIGNAL(itemSelectionChanged()), this, SLOT(updateButtons())); 0064 0065 connect(ui->_availableVectors, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(availableDoubleClicked(QListWidgetItem*))); 0066 connect(ui->_selectedVectors, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(selectedDoubleClicked(QListWidgetItem*))); 0067 0068 connect(ui->_pluginCombo, SIGNAL(currentIndexChanged(QString)), this, SLOT(pluginChanged(QString))); 0069 0070 connect(ui->buttonBox->button(QDialogButtonBox::Apply), SIGNAL(clicked()), this, SLOT(apply())); 0071 connect(ui->buttonBox->button(QDialogButtonBox::Ok), SIGNAL(clicked()), this, SLOT(OKClicked())); 0072 0073 pluginChanged(); 0074 } 0075 0076 FilterMultipleDialog::~FilterMultipleDialog() 0077 { 0078 delete ui; 0079 } 0080 0081 void FilterMultipleDialog::show() { 0082 updateVectorLists(); 0083 QDialog::show(); 0084 } 0085 0086 void FilterMultipleDialog::updateVectorLists() { 0087 VectorList vectors = _store->getObjects<Vector>(); 0088 0089 // make sure all items in ui->_availableVectors exist in the store; remove if they don't. 0090 for (int i_item = 0; i_item < ui->_availableVectors->count(); i_item++) { 0091 bool exists=false; 0092 for (int i_vector = 0; i_vector<vectors.count(); i_vector++) { 0093 if (vectors.at(i_vector)->Name() == ui->_availableVectors->item(i_item)->text()) { 0094 exists = true; 0095 break; 0096 } 0097 } 0098 if (!exists) { 0099 QListWidgetItem *item = ui->_availableVectors->takeItem(i_item); 0100 delete item; 0101 i_item--; 0102 } 0103 } 0104 0105 // make sure all items in ui->ui->_selectedVectors exist in the store; remove if they don't. 0106 for (int i_item = 0; i_item < ui->_selectedVectors->count(); i_item++) { 0107 bool exists=false; 0108 for (int i_primitive = 0; i_primitive<vectors.count(); i_primitive++) { 0109 if (vectors.at(i_primitive)->Name() == ui->_selectedVectors->item(i_item)->text()) { 0110 exists = true; 0111 break; 0112 } 0113 } 0114 if (!exists) { 0115 QListWidgetItem *item = ui->_selectedVectors->takeItem(i_item); 0116 delete item; 0117 i_item--; 0118 } 0119 } 0120 0121 // insert into ui->_availableVectors all items in store not in one of the lists. 0122 for (int i_primitive = 0; i_primitive<vectors.count(); i_primitive++) { 0123 bool listed = false; 0124 for (int i_item = 0; i_item<ui->_availableVectors->count(); i_item++) { 0125 if (vectors.at(i_primitive)->Name() == ui->_availableVectors->item(i_item)->text()) { 0126 ui->_availableVectors->item(i_item)->setToolTip(vectors.at(i_primitive)->descriptionTip()); 0127 listed = true; 0128 break; 0129 } 0130 } 0131 for (int i_item = 0; i_item<ui->_selectedVectors->count(); i_item++) { 0132 if (vectors.at(i_primitive)->Name() == ui->_selectedVectors->item(i_item)->text()) { 0133 ui->_selectedVectors->item(i_item)->setToolTip(vectors.at(i_primitive)->descriptionTip()); 0134 listed = true; 0135 break; 0136 } 0137 } 0138 if (!listed) { 0139 QListWidgetItem *wi = new QListWidgetItem(vectors.at(i_primitive)->Name()); 0140 ui->_availableVectors->addItem(wi); 0141 wi->setToolTip(vectors.at(i_primitive)->descriptionTip()); 0142 } 0143 } 0144 0145 updateButtons(); 0146 } 0147 0148 0149 void FilterMultipleDialog::updateButtons() { 0150 bool valid = ui->_selectedVectors->count() > 0; // fixme: check valid plugin 0151 ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(valid); 0152 ui->buttonBox->button(QDialogButtonBox::Apply)->setEnabled(valid); 0153 ui->_add->setEnabled(ui->_availableVectors->selectedItems().count() > 0); 0154 ui->_addAll->setEnabled(ui->_availableVectors->count() > 0); 0155 ui->_remove->setEnabled(ui->_selectedVectors->selectedItems().count() > 0); 0156 ui->_removeAll->setEnabled(ui->_selectedVectors->count() > 0); 0157 } 0158 0159 0160 void FilterMultipleDialog::addButtonClicked() { 0161 foreach (QListWidgetItem* item, ui->_availableVectors->selectedItems()) { 0162 ui->_selectedVectors->addItem(ui->_availableVectors->takeItem(ui->_availableVectors->row(item))); 0163 } 0164 ui->_availableVectors->clearSelection(); 0165 updateButtons(); 0166 } 0167 0168 0169 void FilterMultipleDialog::smartButtonClicked() { 0170 QList<CurvePtr> curves = _store->getObjects<Curve>(); 0171 foreach (CurvePtr curve, curves) { 0172 bool is_timestream = true; 0173 if (kst_cast<PSD>(curve->yVector()->provider())) { 0174 is_timestream = false; 0175 } 0176 if (kst_cast<Histogram>(curve->yVector()->provider())) { 0177 is_timestream = false; 0178 } 0179 if (is_timestream) { 0180 QList<QListWidgetItem *> vec_items = ui->_availableVectors->findItems(curve->yVector()->Name(), Qt::MatchExactly); 0181 if (vec_items.size()>0) { 0182 ui->_selectedVectors->addItem(ui->_availableVectors->takeItem(ui->_availableVectors->row(vec_items[0]))); 0183 } 0184 } 0185 } 0186 updateButtons(); 0187 } 0188 0189 0190 void FilterMultipleDialog::removeButtonClicked() { 0191 foreach (QListWidgetItem* item, ui->_selectedVectors->selectedItems()) { 0192 ui->_availableVectors->addItem(ui->_selectedVectors->takeItem(ui->_selectedVectors->row(item))); 0193 } 0194 0195 ui->_availableVectors->clearSelection(); 0196 updateButtons(); 0197 } 0198 0199 0200 void FilterMultipleDialog::addAll() { 0201 ui->_availableVectors->selectAll(); 0202 addButtonClicked(); 0203 } 0204 0205 0206 void FilterMultipleDialog::removeAll() { 0207 ui->_selectedVectors->selectAll(); 0208 removeButtonClicked(); 0209 } 0210 0211 0212 void FilterMultipleDialog::availableDoubleClicked(QListWidgetItem * item) { 0213 if (item) { 0214 ui->_selectedVectors->addItem(ui->_availableVectors->takeItem(ui->_availableVectors->row(item))); 0215 ui->_selectedVectors->clearSelection(); 0216 updateButtons(); 0217 } 0218 } 0219 0220 void FilterMultipleDialog::selectedDoubleClicked(QListWidgetItem * item) { 0221 if (item) { 0222 ui->_availableVectors->addItem(ui->_selectedVectors->takeItem(ui->_selectedVectors->row(item))); 0223 ui->_availableVectors->clearSelection(); 0224 updateButtons(); 0225 } 0226 } 0227 0228 void FilterMultipleDialog::OKClicked() { 0229 apply(); 0230 accept(); 0231 } 0232 0233 0234 void FilterMultipleDialog::apply() { 0235 ui->_selectedVectors->selectAll(); 0236 QList<QListWidgetItem*> selectedItems = ui->_selectedVectors->selectedItems(); 0237 0238 QList<CurvePtr> curves = _store->getObjects<Curve>(); 0239 0240 foreach (QListWidgetItem *vec_item, selectedItems) { 0241 VectorPtr vector = kst_cast<Vector>(_store->retrieveObject(vec_item->text())); 0242 if (vector) { 0243 _configWidget->setVectorY(vector); 0244 BasicPluginPtr dataObject = kst_cast<BasicPlugin>( 0245 DataObject::createPlugin(ui->_pluginCombo->currentText(), 0246 _store, 0247 _configWidget)); 0248 Q_ASSERT(dataObject); 0249 0250 if (!dataObject->isValid()) { 0251 _store->removeObject(dataObject); 0252 QString msg(tr("Unable to create Plugin Object using provided parameters.\n\n")); 0253 msg += dataObject->errorMessage(); 0254 QMessageBox::warning(this, tr("Kst"), msg); 0255 0256 return; 0257 } 0258 VectorPtr yVector = dataObject->outputVectors().value(dataObject->outputVectorList().first()); 0259 if (ui->_replaceCurves->isChecked()) { 0260 foreach (CurvePtr curve, curves) { 0261 if (curve->xVector()->shortName() == vector->shortName()) { 0262 curve->setXVector(yVector); 0263 } 0264 if (curve->yVector()->shortName() == vector->shortName()) { 0265 curve->setYVector(yVector); 0266 } 0267 } 0268 } else if (ui->_copyCurves->isChecked()) { 0269 foreach (CurvePtr curve, curves) { 0270 if (curve->yVector()->shortName() == vector->shortName()) { 0271 CurvePtr new_curve = _store->createObject<Curve>(); 0272 new_curve->setXVector(curve->xVector()); 0273 new_curve->setYVector(yVector); 0274 0275 // plot it 0276 QList<PlotItem *> plots = ViewItem::getItems<PlotItem>(); 0277 foreach (PlotItemInterface *plot, plots) { 0278 PlotItem* plotItem = static_cast<PlotItem*>(plot); 0279 foreach (PlotRenderItem* renderItem, plotItem->renderItems()) { 0280 if (renderItem->relationList().contains(curve)) { 0281 renderItem->addRelation(new_curve); 0282 } 0283 } 0284 } 0285 } 0286 } 0287 } 0288 } 0289 } 0290 UpdateManager::self()->doUpdates(); 0291 UpdateServer::self()->requestUpdateSignal(); 0292 0293 kstApp->mainWindow()->document()->setChanged(true); 0294 _configWidget->save(); 0295 0296 } 0297 0298 0299 void FilterMultipleDialog::pluginChanged(QString plugin) { 0300 if (plugin.isEmpty()) { 0301 int index = ui->_pluginCombo->currentIndex(); 0302 if (index < 0) { 0303 ui->_pluginCombo->setCurrentIndex(0); 0304 } 0305 plugin = ui->_pluginCombo->currentText(); 0306 } 0307 0308 if (plugin != ui->_pluginCombo->currentText()) { 0309 ui->_pluginCombo->setCurrentIndex(ui->_pluginCombo->findText(plugin)); 0310 } 0311 0312 ui->_descriptionLabel->setText(DataObject::pluginDescription(plugin)); 0313 0314 if (_layout) { 0315 delete _layout; 0316 } 0317 0318 if (_configWidget) { 0319 delete _configWidget; 0320 } 0321 _layout = new QGridLayout(ui->_inputOutputBox); 0322 _configWidget = DataObject::pluginWidget(plugin); 0323 0324 _configWidget->setupSlots(this); 0325 if (_store) { 0326 _configWidget->setObjectStore(_store); 0327 } 0328 _configWidget->load(); 0329 _configWidget->setVectorsLocked(); 0330 _layout->addWidget(_configWidget, 0, 0); 0331 _layout->activate(); 0332 0333 } 0334 0335 0336 0337 0338 }