-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
PlotWizard.cpp
380 lines (319 loc) · 12.8 KB
/
PlotWizard.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/***************************************************************************
File : PlotWizard.cpp
Project : AlphaPlot
--------------------------------------------------------------------
Copyright : (C) 2006 by Ion Vasilief, Tilman Benkert
Email (use @ for *) : ion_vasilief*yahoo.fr, thzs*gmx.net
Description : A wizard type dialog to create new plots
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, *
* Boston, MA 02110-1301 USA *
* *
***************************************************************************/
#include "PlotWizard.h"
#include <QApplication>
#include <QComboBox>
#include <QGridLayout>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QLabel>
#include <QListWidget>
#include <QMessageBox>
#include <QPushButton>
#include <QSizePolicy>
#include <QVBoxLayout>
#include "3Dplot/Layout3D.h"
#include "ApplicationWindow.h"
#include "Table.h"
PlotWizard::PlotWizard(QWidget *parent, Qt::WindowFlags fl)
: QDialog(parent, fl) {
setWindowTitle(tr("Select Columns to Plot"));
setSizeGripEnabled(true);
// top part starts here
groupBox1 = new QGroupBox();
QGridLayout *gl1 = new QGridLayout();
buttonX = new QPushButton("<->" + tr("&X"));
buttonX->setAutoDefault(false);
gl1->addWidget(buttonX, 0, 0);
buttonXErr = new QPushButton("<->" + tr("x&Err"));
buttonXErr->setAutoDefault(false);
gl1->addWidget(buttonXErr, 0, 1);
buttonY = new QPushButton("<->" + tr("&Y"));
buttonY->setAutoDefault(false);
gl1->addWidget(buttonY, 1, 0);
buttonYErr = new QPushButton("<->" + tr("yE&rr"));
buttonYErr->setAutoDefault(false);
gl1->addWidget(buttonYErr, 1, 1);
buttonZ = new QPushButton("<->" + tr("&Z"));
buttonZ->setAutoDefault(false);
gl1->addWidget(buttonZ, 2, 0);
gl1->setRowStretch(3, 1);
QHBoxLayout *hl2 = new QHBoxLayout();
buttonNew = new QPushButton(tr("&New curve"));
buttonNew->setDefault(true);
buttonNew->setAutoDefault(true);
hl2->addWidget(buttonNew);
buttonDelete = new QPushButton(tr("&Delete curve"));
buttonDelete->setAutoDefault(false);
hl2->addWidget(buttonDelete);
QVBoxLayout *vl = new QVBoxLayout();
vl->addLayout(gl1);
vl->addStretch();
vl->addLayout(hl2);
QGridLayout *gl2 = new QGridLayout(groupBox1);
gl2->addWidget(new QLabel(tr("Worksheet")), 0, 0);
boxTables = new QComboBox();
gl2->addWidget(boxTables, 0, 1);
columnsList = new QListWidget();
gl2->addWidget(columnsList, 1, 0);
gl2->addLayout(vl, 1, 1);
// middle part is only one widget
plotAssociations = new QListWidget();
// bottom part starts here
QHBoxLayout *bottomLayout = new QHBoxLayout();
bottomLayout->addStretch();
buttonOk = new QPushButton(tr("&Plot"));
buttonOk->setAutoDefault(false);
bottomLayout->addWidget(buttonOk);
buttonCancel = new QPushButton(tr("&Close"));
buttonCancel->setAutoDefault(false);
bottomLayout->addWidget(buttonCancel);
QVBoxLayout *vlayout = new QVBoxLayout(this);
vlayout->addWidget(groupBox1);
vlayout->addWidget(plotAssociations);
vlayout->addLayout(bottomLayout);
// signals and slots connections
connect(boxTables, &QComboBox::textActivated, this,
&PlotWizard::changeColumnsList);
connect(buttonOk, &QPushButton::clicked, this, &PlotWizard::accept);
connect(buttonCancel, &QPushButton::clicked, this, &PlotWizard::reject);
connect(buttonNew, &QPushButton::clicked, this, &PlotWizard::addCurve);
connect(buttonDelete, &QPushButton::clicked, this, &PlotWizard::removeCurve);
connect(buttonX, &QPushButton::clicked, this, &PlotWizard::addXCol);
connect(buttonY, &QPushButton::clicked, this, &PlotWizard::addYCol);
connect(buttonXErr, &QPushButton::clicked, this, &PlotWizard::addXErrCol);
connect(buttonYErr, &QPushButton::clicked, this, &PlotWizard::addYErrCol);
connect(buttonZ, &QPushButton::clicked, this, &PlotWizard::addZCol);
}
QSize PlotWizard::sizeHint() const { return QSize(350, 400); }
void PlotWizard::accept() {
QStringList curves, curves3D, ribbons;
for (int i = 0; i < plotAssociations->count(); i++) {
QString text = plotAssociations->item(i)->text();
if (text.endsWith("(X)")) {
QMessageBox::critical(
this, tr("Error"),
tr("Please define a Y column for the following curve") + ":\n\n" +
text);
return;
}
if (text.contains("(Z)")) {
if (text.contains("(Y)") && !curves3D.contains(text))
curves3D << text;
else if (!text.contains("(Y)") && !ribbons.contains(text))
ribbons << text;
} else if (text.contains("(xErr)") || text.contains("(yErr)")) {
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
QStringList lst = text.split(",", Qt::SkipEmptyParts);
#else
QStringList lst = text.split(",", QString::SkipEmptyParts);
#endif
lst.pop_back();
QString master_curve = lst.join(",");
if (!curves.contains(master_curve)) curves.prepend(master_curve);
if (!curves.contains(text))
curves << text; // add error bars at the end of the list.
} else if (!curves.contains(text))
curves.prepend(text);
}
if (curves.count() > 0) emit plot(curves);
if (curves3D.count() > 0) plot3D(curves3D);
if (ribbons.count() > 0) plot3DRibbon(ribbons);
if (!noCurves()) close();
}
void PlotWizard::changeColumnsList(const QString &table) {
QStringList newList;
for (int i = 0; i < columns.count(); i++) {
QString s = columns[i];
if (s.contains(table)) newList << s.remove(table + "_");
}
setColumnsListBoxContents(newList);
}
void PlotWizard::addXCol() {
if (noCurves()) return;
QString text = plotAssociations->currentItem()->text();
if (text.contains("(X)"))
QMessageBox::warning(this, tr("Error"),
tr("You have already defined a X column!"));
else {
plotAssociations->currentItem()->setText(
text + columnsList->currentItem()->text() + "(X)");
}
}
void PlotWizard::addYCol() {
if (noCurves()) return;
QString text = plotAssociations->currentItem()->text();
if (!text.contains("(X)"))
QMessageBox::warning(this, tr("Error"),
tr("You must define a X column first!"));
else if (text.contains("(Y)"))
QMessageBox::warning(this, tr("Error"),
tr("You have already defined a Y column!"));
else {
plotAssociations->currentItem()->setText(
text + ", " + columnsList->currentItem()->text() + "(Y)");
}
}
void PlotWizard::addZCol() {
if (noCurves()) return;
QString text = plotAssociations->currentItem()->text();
if (text.contains("(xErr)") || text.contains("(yErr)"))
QMessageBox::warning(this, tr("Error"),
tr("This kind of curve is not handled by AlphaPlot!"));
else if (!text.contains("(X)"))
QMessageBox::warning(this, tr("Error"),
tr("You must define a X column first!"));
else if (text.contains("(Z)"))
QMessageBox::warning(this, tr("Error"),
tr("You have already defined a Z column!"));
else {
plotAssociations->currentItem()->setText(
text + ", " + columnsList->currentItem()->text() + "(Z)");
}
}
void PlotWizard::addXErrCol() {
if (noCurves()) return;
QString text = plotAssociations->currentItem()->text();
if (text.contains("(Z)"))
QMessageBox::warning(this, tr("Error"),
tr("This kind of curve is not handled by AlphaPlot!"));
else if (!text.contains("(X)"))
QMessageBox::warning(this, tr("Error"),
tr("You must define a X column first!"));
else if (!text.contains("(Y)"))
QMessageBox::warning(this, tr("Error"),
tr("You must define a Y column first!"));
else if (text.contains("(xErr)") || text.contains("(yErr)"))
QMessageBox::warning(this, tr("Error"),
tr("You have already defined an error-bars column!"));
else {
plotAssociations->currentItem()->setText(
text + ", " + columnsList->currentItem()->text() + "(xErr)");
}
}
void PlotWizard::addYErrCol() {
if (noCurves()) return;
QString text = plotAssociations->currentItem()->text();
if (text.contains("(Z)"))
QMessageBox::warning(this, tr("Error"),
tr("This kind of curve is not handled by AlphaPlot!"));
else if (!text.contains("(X)"))
QMessageBox::warning(this, tr("Error"),
tr("You must define a X column first!"));
else if (!text.contains("(Y)"))
QMessageBox::warning(this, tr("Error"),
tr("You must define a Y column first!"));
else if (text.contains("(xErr)") || text.contains("(yErr)"))
QMessageBox::warning(this, tr("Error"),
tr("You have already defined an error-bars column!"));
else {
plotAssociations->currentItem()->setText(
text + ", " + columnsList->currentItem()->text() + "(yErr)");
}
}
void PlotWizard::addCurve() {
plotAssociations->addItem(boxTables->currentText() + ": ");
plotAssociations->setCurrentRow(plotAssociations->count() - 1);
}
void PlotWizard::removeCurve() {
plotAssociations->takeItem(plotAssociations->currentRow());
}
void PlotWizard::insertTablesList(const QStringList &tables) {
boxTables->addItems(tables);
}
void PlotWizard::setColumnsListBoxContents(const QStringList &cols) {
columnsList->clear();
columnsList->insertItems(0, cols);
columnsList->setCurrentRow(0);
}
void PlotWizard::setColumnsList(const QStringList &cols) { columns = cols; }
bool PlotWizard::noCurves() {
if (plotAssociations->count() == 0) {
QMessageBox::warning(0, tr("Error"), tr("You must add a new curve first!"));
return true;
} else
return false;
}
void PlotWizard::plot3DRibbon(const QStringList &lst) {
Q_UNUSED(lst)
/*ApplicationWindow *app = (ApplicationWindow *)this->parent();
if (!app) return;
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
for (int i = 0; i < lst.count(); i++) {
QString s = lst[i];
int pos = s.indexOf(":", 0);
QString table_name = s.left(pos) + "_";
Table *t = app->table(table_name);
if (t) {
int posX = s.indexOf("(", pos);
QString xColName = table_name + s.mid(pos + 2, posX - pos - 2);
posX = s.indexOf(",", posX);
int posY = s.indexOf("(", posX);
QString yColName = table_name + s.mid(posX + 2, posY - posX - 2);
Graph3D *g = app->newPlot3D();
if (g) {
g->addData(t, xColName, yColName);
g->update();
}
}
}
QApplication::restoreOverrideCursor();*/
}
void PlotWizard::plot3D(const QStringList &lst) {
Q_UNUSED(lst)
/*ApplicationWindow *app = (ApplicationWindow *)this->parent();
if (!app) return;
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
for (int i = 0; i < lst.count(); i++) {
QString s = lst[i];
int pos = s.indexOf(":", 0);
QString table_name = s.left(pos) + "_";
Table *t = app->table(table_name);
if (t) {
int posX = s.indexOf("(", pos);
QString xColName = table_name + s.mid(pos + 2, posX - pos - 2);
posX = s.indexOf(",", posX);
int posY = s.indexOf("(", posX);
QString yColName = table_name + s.mid(posX + 2, posY - posX - 2);
posY = s.indexOf(",", posY);
int posZ = s.indexOf("(", posY);
QString zColName = table_name + s.mid(posY + 2, posZ - posY - 2);
int xCol = t->colIndex(xColName);
int yCol = t->colIndex(yColName);
int zCol = t->colIndex(zColName);
if (xCol >= 0 && yCol >= 0 && zCol >= 0) {
Graph3D *g = app->newPlot3D();
if (g) {
g->addData(t, xCol, yCol, zCol, 1);
g->update();
}
}
}
}
QApplication::restoreOverrideCursor();*/
}
PlotWizard::~PlotWizard() {}