1 /***************************************************************************
2 * *
3 * copyright : (C) 2010 C. Barth Netterfield *
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
14 #include "exportvectorsdialog.h"
15 #include "dialogdefaults.h"
16
17 #include "datavector.h"
18 #include "objectstore.h"
19 #include "mainwindow.h"
20 #include "document.h"
21
22 #include <QLineEdit>
23
24 namespace Kst {
25
ExportVectorsDialog(QWidget * parent)26 ExportVectorsDialog::ExportVectorsDialog(QWidget *parent) :
27 QDialog(parent)
28 {
29 setupUi(this);
30
31 MainWindow::setWidgetFlags(this);
32
33 _saveLocationLabel->setBuddy(_saveLocation->_fileEdit);
34 _saveLocation->setFile(dialogDefaults().value("vectorexport/filename",QDir::currentPath()).toString());
35
36 if (MainWindow *mw = qobject_cast<MainWindow*>(parent)) {
37 _store = mw->document()->objectStore();
38 } else {
39 // FIXME: we need the object store
40 qFatal("ERROR: can't construct a ExportVectorsDialog without the object store");
41 }
42
43 connect(_add, SIGNAL(clicked()), this, SLOT(addButtonClicked()));
44 connect(_remove, SIGNAL(clicked()), this, SLOT(removeButtonClicked()));
45 connect(_removeAll, SIGNAL(clicked()), this, SLOT(removeAll()));
46 connect(_addAll, SIGNAL(clicked()), this, SLOT(addAll()));
47
48 connect(_changeVectorList, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(availableDoubleClicked(QListWidgetItem*)));
49 connect(_selectedVectorList, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(selectedDoubleClicked(QListWidgetItem*)));
50
51 connect(_changeVectorList, SIGNAL(itemSelectionChanged()), this, SLOT(updateButtons()));
52 connect(_selectedVectorList, SIGNAL(itemSelectionChanged()), this, SLOT(updateButtons()));
53
54 connect(_buttonBox->button(QDialogButtonBox::Ok), SIGNAL(clicked()), this, SLOT(OKClicked()));
55 connect(_buttonBox->button(QDialogButtonBox::Apply), SIGNAL(clicked()), this, SLOT(apply()));
56
57 }
58
~ExportVectorsDialog()59 ExportVectorsDialog::~ExportVectorsDialog()
60 {
61 }
62
show()63 void ExportVectorsDialog::show() {
64 updateVectorList();
65 QDialog::show();
66 }
67
updateVectorList()68 void ExportVectorsDialog::updateVectorList() {
69 VectorList allVectors = _store->getObjects<Vector>();
70
71 QStringList vectorNameList;
72
73 ObjectList<Vector> vectors;
74
75 foreach (VectorPtr P, allVectors) {
76 vectors.append(P);
77 vectorNameList.append(P->Name());
78 }
79
80 // make sure all items in _changeVectorList exist in the store; remove if they don't.
81 for (int i_item = 0; i_item < _changeVectorList->count(); i_item++) {
82 bool exists=false;
83 for (int i_vector = 0; i_vector<vectors.count(); i_vector++) {
84 if (vectors.at(i_vector)->Name() == _changeVectorList->item(i_item)->text()) {
85 exists = true;
86 break;
87 }
88 }
89 if (!exists) {
90 QListWidgetItem *item = _changeVectorList->takeItem(i_item);
91 delete item;
92 }
93 }
94
95 // make sure all items in _selectedVectorList exist in the store; remove if they don't.
96 for (int i_item = 0; i_item < _selectedVectorList->count(); i_item++) {
97 bool exists=false;
98 for (int i_vector = 0; i_vector<vectors.count(); i_vector++) {
99 if (vectors.at(i_vector)->Name() == _selectedVectorList->item(i_item)->text()) {
100 exists = true;
101 break;
102 }
103 }
104 if (!exists) {
105 QListWidgetItem *item = _selectedVectorList->takeItem(i_item);
106 delete item;
107 }
108 }
109
110 // insert into _changeVectorList all items in store not in one of the lists.
111 for (int i_vector = 0; i_vector<vectors.count(); i_vector++) {
112 bool listed = false;
113 for (int i_item = 0; i_item<_changeVectorList->count(); i_item++) {
114 if (vectors.at(i_vector)->Name() == _changeVectorList->item(i_item)->text()) {
115 _changeVectorList->item(i_item)->setToolTip(vectors.at(i_vector)->descriptionTip());
116 listed = true;
117 break;
118 }
119 }
120 for (int i_item = 0; i_item<_selectedVectorList->count(); i_item++) {
121 if (vectors.at(i_vector)->Name() == _selectedVectorList->item(i_item)->text()) {
122 _selectedVectorList->item(i_item)->setToolTip(vectors.at(i_vector)->descriptionTip());
123 listed = true;
124 break;
125 }
126 }
127 if (!listed) {
128 QListWidgetItem *wi = new QListWidgetItem(vectors.at(i_vector)->Name());
129 _changeVectorList->addItem(wi);
130 wi->setToolTip(vectors.at(i_vector)->descriptionTip());
131 }
132 }
133
134 updateButtons();
135 }
136
137
removeButtonClicked()138 void ExportVectorsDialog::removeButtonClicked() {
139 foreach (QListWidgetItem* item, _selectedVectorList->selectedItems()) {
140 _changeVectorList->addItem(_selectedVectorList->takeItem(_selectedVectorList->row(item)));
141 }
142
143 _changeVectorList->clearSelection();
144 updateButtons();
145 }
146
147
selectedDoubleClicked(QListWidgetItem * item)148 void ExportVectorsDialog::selectedDoubleClicked(QListWidgetItem * item) {
149 if (item) {
150 _changeVectorList->addItem(_selectedVectorList->takeItem(_selectedVectorList->row(item)));
151 _changeVectorList->clearSelection();
152 updateButtons();
153 }
154 }
155
156
addButtonClicked()157 void ExportVectorsDialog::addButtonClicked() {
158 foreach (QListWidgetItem* item, _changeVectorList->selectedItems()) {
159 _selectedVectorList->addItem(_changeVectorList->takeItem(_changeVectorList->row(item)));
160 }
161 _selectedVectorList->clearSelection();
162 updateButtons();
163 }
164
165
availableDoubleClicked(QListWidgetItem * item)166 void ExportVectorsDialog::availableDoubleClicked(QListWidgetItem * item) {
167 if (item) {
168 _selectedVectorList->addItem(_changeVectorList->takeItem(_changeVectorList->row(item)));
169 _selectedVectorList->clearSelection();
170 updateButtons();
171 }
172 }
173
addAll()174 void ExportVectorsDialog::addAll() {
175 _changeVectorList->selectAll();
176 addButtonClicked();
177 }
178
179
removeAll()180 void ExportVectorsDialog::removeAll() {
181 _selectedVectorList->selectAll();
182 removeButtonClicked();
183 }
184
updateButtons()185 void ExportVectorsDialog::updateButtons() {
186 bool valid = _selectedVectorList->count();
187
188 QFileInfo qfi(_saveLocation->file());
189
190 if (qfi.isFile()) {
191 valid &= qfi.isWritable();
192 }
193
194 _buttonBox->button(QDialogButtonBox::Ok)->setEnabled(valid);
195 _buttonBox->button(QDialogButtonBox::Apply)->setEnabled(valid);
196
197 _add->setEnabled(_changeVectorList->selectedItems().count() > 0);
198 _addAll->setEnabled(_changeVectorList->count() > 0);
199 _remove->setEnabled(_selectedVectorList->selectedItems().count() > 0);
200 _removeAll->setEnabled(_selectedVectorList->count() > 0);
201
202 }
203
OKClicked()204 void ExportVectorsDialog::OKClicked() {
205 if (apply()) {
206 accept();
207 }
208 }
209
210
apply()211 bool ExportVectorsDialog::apply() {
212 QFile file(_saveLocation->file());
213 VectorList vectors;
214 QList<int> lengths;
215
216 if (!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate))
217 return false;
218
219 QTextStream out(&file);
220
221 out << "#";
222 int count = _selectedVectorList->count();
223 for (int i = 0; i<count; i++) {
224 VectorPtr V = kst_cast<Vector>(_store->retrieveObject(_selectedVectorList->item(i)->text()));
225 if (V) {
226 vectors.append(V);
227 out << " " << V->descriptiveName();
228 lengths << V->length();
229 }
230 }
231
232 out << "\n";
233
234 int maxLength = 0;
235 for (int i=0; i<lengths.size(); i++) {
236 if (lengths.at(i)>maxLength) {
237 maxLength = lengths.at(i);
238 }
239 }
240
241 out.setRealNumberPrecision(14);
242
243 int ncols = vectors.size();
244 for (int row = 0; row < maxLength; row++) {
245 for (int col = 0; col < ncols; col++) {
246 out << " " << vectors.at(col)->interpolate(row, maxLength);
247 }
248 out << "\n";
249 }
250
251 out.flush();
252
253 file.close();
254 dialogDefaults().setValue("vectorexport/filename", _saveLocation->file());
255
256 return(true);
257 }
258
259 }
260