File indexing completed on 2024-12-22 04:17:26
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 "choosecolordialog.h" 0014 0015 #include "datavector.h" 0016 #include "datacollection.h" 0017 #include "objectstore.h" 0018 #include "mainwindow.h" 0019 #include "document.h" 0020 #include "application.h" 0021 #include "updatemanager.h" 0022 0023 #include "colorsequence.h" 0024 #include <QPushButton> 0025 0026 namespace Kst { 0027 0028 ChooseColorDialog::ChooseColorDialog(QWidget *parent) 0029 : QDialog(parent) { 0030 0031 setupUi(this); 0032 0033 MainWindow::setWidgetFlags(this); 0034 0035 _grid = 0; 0036 0037 if (MainWindow *mw = qobject_cast<MainWindow*>(parent)) { 0038 _store = mw->document()->objectStore(); 0039 } else { 0040 // FIXME: we need the object store 0041 qFatal("ERROR: can't construct a ChooseColorDialog without the object store"); 0042 } 0043 QMap<DataSourcePtr, QColor> _dataSourceColors; 0044 connect(_buttonBox->button(QDialogButtonBox::Cancel), SIGNAL(clicked()), this, SLOT(reject())); 0045 connect(_buttonBox->button(QDialogButtonBox::Ok), SIGNAL(clicked()), this, SLOT(OKClicked())); 0046 connect(_buttonBox->button(QDialogButtonBox::Apply), SIGNAL(clicked()), this, SLOT(apply())); 0047 } 0048 0049 0050 ChooseColorDialog::~ChooseColorDialog() { 0051 delete _grid; 0052 } 0053 0054 0055 void ChooseColorDialog::show() { 0056 updateColorGroup(); 0057 QDialog::show(); 0058 } 0059 0060 0061 void ChooseColorDialog::updateColorGroup() { 0062 0063 // cannot use dataSourceList.fileNames() as it contains datasources that 0064 // are not used by any curves or vectors 0065 DataVectorList vcList = _store->getObjects<DataVector>(); 0066 0067 _dataSourceColors.clear(); 0068 for (DataVectorList::Iterator vc_iter = vcList.begin(); 0069 vc_iter != vcList.end(); 0070 ++vc_iter) 0071 { 0072 if (! (_dataSourceColors.contains((*vc_iter)->dataSource()))) 0073 _dataSourceColors[(*vc_iter)->dataSource()] = (*vc_iter)->dataSource()->color(); 0074 } 0075 0076 cleanColorGroup(); 0077 0078 _grid = new QGridLayout(colorFrame); 0079 _grid->setSpacing(8); 0080 _grid->setColumnStretch(0,1); 0081 _grid->setColumnMinimumWidth(0,450); 0082 0083 int i=0; 0084 QMapIterator<DataSourcePtr, QColor> it(_dataSourceColors); 0085 while (it.hasNext()) { 0086 it.next(); 0087 QLineEdit* dataSourceName = new QLineEdit(colorFrame); 0088 dataSourceName->setReadOnly(true); 0089 dataSourceName->setText(it.key()->fileName()); 0090 _grid->addWidget(dataSourceName,i,0); 0091 _lineEdits.push_back(dataSourceName); 0092 dataSourceName->show(); 0093 0094 ColorButton* dataSourceColor = new ColorButton(colorFrame); 0095 dataSourceColor->setColor(it.value()); 0096 _grid->addWidget(dataSourceColor,i,1); 0097 _colorButtons.push_back(dataSourceColor); 0098 dataSourceColor->show(); 0099 i++; 0100 } 0101 0102 adjustSize(); 0103 } 0104 0105 0106 void ChooseColorDialog::cleanColorGroup() { 0107 while (!_lineEdits.isEmpty()) 0108 { 0109 QLineEdit* tempLineEdit = _lineEdits.back(); 0110 _lineEdits.pop_back(); 0111 delete tempLineEdit; 0112 } 0113 0114 while (!_colorButtons.isEmpty()) 0115 { 0116 ColorButton* tempColorButton = _colorButtons.back(); 0117 _colorButtons.pop_back(); 0118 delete tempColorButton; 0119 } 0120 delete _grid; 0121 } 0122 0123 0124 void ChooseColorDialog::OKClicked() { 0125 if (_buttonBox->button(QDialogButtonBox::Apply)->isEnabled()) { 0126 apply(); 0127 } 0128 accept(); 0129 } 0130 0131 0132 void ChooseColorDialog::apply() { 0133 CurveList curveList = _store->getObjects<Curve>(); 0134 for (CurveList::iterator curve_iter = curveList.begin(); curve_iter != curveList.end(); ++curve_iter) 0135 { 0136 VectorPtr vector; 0137 CurvePtr curve = kst_cast<Curve>(*curve_iter); 0138 if (_xVector->isChecked()) { 0139 vector = curve->xVector(); 0140 } else { 0141 vector = curve->yVector(); 0142 } 0143 if (DataVectorPtr dataVector = kst_cast<DataVector>(vector)) 0144 { 0145 curve->writeLock(); 0146 curve->setColor(getColorForFile(dataVector->filename())); 0147 curve->registerChange(); 0148 curve->unlock(); 0149 } 0150 } 0151 // Store the selected colors in the corresponding datasource objects 0152 QMutableMapIterator<DataSourcePtr, QColor> itDatasource(_dataSourceColors); 0153 QListIterator<ColorButton*> itColorButton(_colorButtons); 0154 DataSourcePtr ds; 0155 while (itDatasource.hasNext()) { 0156 ds = itDatasource.next().key(); 0157 ds->setColor(itColorButton.next()->color()); // Per construction there should always be as many color buttons as datasources 0158 } 0159 0160 updateColorGroup(); // This will update the _dataSourceColors map 0161 0162 UpdateManager::self()->doUpdates(true); 0163 kstApp->mainWindow()->document()->setChanged(true); 0164 } 0165 0166 0167 QColor ChooseColorDialog::getColorForFile(const QString &fileName) { 0168 QList<ColorButton*>::Iterator kc_iter = _colorButtons.begin(); 0169 for (QList<QLineEdit*>::Iterator fn_iter = _lineEdits.begin(); fn_iter != _lineEdits.end(); ++fn_iter) { 0170 if (fileName == (*fn_iter)->text()) { 0171 return (*kc_iter)->color(); 0172 } 0173 ++kc_iter; 0174 } 0175 return QColor(); 0176 } 0177 0178 } 0179 0180 // vim: ts=2 sw=2 et