1 /***************************************************************************
2  *                                                                         *
3  *   copyright : (C) 2007 The University of Toronto                        *
4  *                   netterfield@astro.utoronto.ca                         *
5  *                                                                         *
6  *   This program is free software; you can redistribute it and/or modify  *
7  *   it under the terms of the GNU General Public License as published by  *
8  *   the Free Software Foundation; either version 2 of the License, or     *
9  *   (at your option) any later version.                                   *
10  *                                                                         *
11  ***************************************************************************/
12 
13 #include "choosecolordialog.h"
14 
15 #include "datavector.h"
16 #include "datacollection.h"
17 #include "objectstore.h"
18 #include "mainwindow.h"
19 #include "document.h"
20 #include "application.h"
21 #include "updatemanager.h"
22 
23 #include "colorsequence.h"
24 #include <QPushButton>
25 
26 namespace Kst {
27 
ChooseColorDialog(QWidget * parent)28 ChooseColorDialog::ChooseColorDialog(QWidget *parent)
29   : QDialog(parent) {
30 
31   setupUi(this);
32 
33   MainWindow::setWidgetFlags(this);
34 
35   _grid = 0;
36 
37   if (MainWindow *mw = qobject_cast<MainWindow*>(parent)) {
38     _store = mw->document()->objectStore();
39   } else {
40     // FIXME: we need the object store
41     qFatal("ERROR: can't construct a ChooseColorDialog without the object store");
42   }
43   QMap<DataSourcePtr, QColor> _dataSourceColors;
44   connect(_buttonBox->button(QDialogButtonBox::Cancel), SIGNAL(clicked()), this, SLOT(reject()));
45   connect(_buttonBox->button(QDialogButtonBox::Ok), SIGNAL(clicked()), this, SLOT(OKClicked()));
46   connect(_buttonBox->button(QDialogButtonBox::Apply), SIGNAL(clicked()), this, SLOT(apply()));
47 }
48 
49 
~ChooseColorDialog()50 ChooseColorDialog::~ChooseColorDialog() {
51   delete _grid;
52 }
53 
54 
show()55 void ChooseColorDialog::show() {
56   updateColorGroup();
57   QDialog::show();
58 }
59 
60 
updateColorGroup()61 void ChooseColorDialog::updateColorGroup() {
62 
63   // cannot use dataSourceList.fileNames() as it contains datasources that
64   // are not used by any curves or vectors
65   DataVectorList vcList = _store->getObjects<DataVector>();
66 
67   _dataSourceColors.clear();
68   for (DataVectorList::Iterator vc_iter = vcList.begin();
69         vc_iter != vcList.end();
70         ++vc_iter)
71   {
72     if (! (_dataSourceColors.contains((*vc_iter)->dataSource())))
73       _dataSourceColors[(*vc_iter)->dataSource()] = (*vc_iter)->dataSource()->color();
74   }
75 
76   cleanColorGroup();
77 
78   _grid = new QGridLayout(colorFrame);
79   _grid->setSpacing(8);
80   _grid->setColumnStretch(0,1);
81   _grid->setColumnMinimumWidth(0,450);
82 
83   int i=0;
84   QMapIterator<DataSourcePtr, QColor> it(_dataSourceColors);
85   while (it.hasNext()) {
86     it.next();
87     QLineEdit* dataSourceName = new QLineEdit(colorFrame);
88     dataSourceName->setReadOnly(true);
89     dataSourceName->setText(it.key()->fileName());
90     _grid->addWidget(dataSourceName,i,0);
91     _lineEdits.push_back(dataSourceName);
92     dataSourceName->show();
93 
94     ColorButton* dataSourceColor = new ColorButton(colorFrame);
95     dataSourceColor->setColor(it.value());
96     _grid->addWidget(dataSourceColor,i,1);
97     _colorButtons.push_back(dataSourceColor);
98     dataSourceColor->show();
99     i++;
100   }
101 
102   adjustSize();
103 }
104 
105 
cleanColorGroup()106 void ChooseColorDialog::cleanColorGroup() {
107   while (!_lineEdits.isEmpty())
108   {
109     QLineEdit* tempLineEdit = _lineEdits.back();
110     _lineEdits.pop_back();
111     delete tempLineEdit;
112   }
113 
114   while (!_colorButtons.isEmpty())
115   {
116     ColorButton* tempColorButton = _colorButtons.back();
117     _colorButtons.pop_back();
118     delete tempColorButton;
119   }
120   delete _grid;
121 }
122 
123 
OKClicked()124 void ChooseColorDialog::OKClicked() {
125   if (_buttonBox->button(QDialogButtonBox::Apply)->isEnabled()) {
126     apply();
127   }
128   accept();
129 }
130 
131 
apply()132 void ChooseColorDialog::apply() {
133   CurveList curveList = _store->getObjects<Curve>();
134   for (CurveList::iterator curve_iter = curveList.begin(); curve_iter != curveList.end(); ++curve_iter)
135   {
136     VectorPtr vector;
137     CurvePtr curve = kst_cast<Curve>(*curve_iter);
138     if (_xVector->isChecked()) {
139       vector = curve->xVector();
140     } else {
141       vector = curve->yVector();
142     }
143     if (DataVectorPtr dataVector = kst_cast<DataVector>(vector))
144     {
145       curve->writeLock();
146       curve->setColor(getColorForFile(dataVector->filename()));
147       curve->registerChange();
148       curve->unlock();
149     }
150   }
151   // Store the selected colors in the corresponding datasource objects
152   QMutableMapIterator<DataSourcePtr, QColor> itDatasource(_dataSourceColors);
153   QListIterator<ColorButton*> itColorButton(_colorButtons);
154   DataSourcePtr ds;
155   while (itDatasource.hasNext()) {
156     ds = itDatasource.next().key();
157     ds->setColor(itColorButton.next()->color()); // Per construction there should always be as many color buttons as datasources
158   }
159 
160   updateColorGroup(); // This will update the _dataSourceColors map
161 
162   UpdateManager::self()->doUpdates(true);
163   kstApp->mainWindow()->document()->setChanged(true);
164 }
165 
166 
getColorForFile(const QString & fileName)167 QColor ChooseColorDialog::getColorForFile(const QString &fileName) {
168   QList<ColorButton*>::Iterator kc_iter = _colorButtons.begin();
169   for (QList<QLineEdit*>::Iterator fn_iter = _lineEdits.begin(); fn_iter != _lineEdits.end(); ++fn_iter) {
170     if (fileName == (*fn_iter)->text()) {
171       return (*kc_iter)->color();
172     }
173     ++kc_iter;
174   }
175   return QColor();
176 }
177 
178 }
179 
180 // vim: ts=2 sw=2 et
181