1
/***************************************************************************
4
--------------------------------------------------------------------
5
Copyright : (C) 2006 by Ion Vasilief, Tilman Benkert
6
Email (use @ for *) : ion_vasilief*yahoo.fr, thzs*gmx.net
7
Description : A wizard type dialog to create new plots
9
***************************************************************************/
11
/***************************************************************************
13
* This program is free software; you can redistribute it and/or modify *
14
* it under the terms of the GNU General Public License as published by *
15
* the Free Software Foundation; either version 2 of the License, or *
16
* (at your option) any later version. *
18
* This program is distributed in the hope that it will be useful, *
19
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
20
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
21
* GNU General Public License for more details. *
23
* You should have received a copy of the GNU General Public License *
24
* along with this program; if not, write to the Free Software *
25
* Foundation, Inc., 51 Franklin Street, Fifth Floor, *
26
* Boston, MA 02110-1301 USA *
28
***************************************************************************/
29
#include "PlotWizard.h"
30
#include "ApplicationWindow.h"
34
#include <QApplication>
35
#include <QHBoxLayout>
36
#include <QVBoxLayout>
37
#include <QGridLayout>
38
#include <QSizePolicy>
40
#include <QMessageBox>
42
#include <QPushButton>
43
#include <QListWidget>
46
PlotWizard::PlotWizard( QWidget* parent, Qt::WFlags fl )
47
: QDialog( parent, fl )
49
setWindowTitle( tr("Select Columns to Plot") );
51
setSizeGripEnabled( true );
53
// top part starts here
54
groupBox1 = new QGroupBox();
56
QGridLayout *gl1 = new QGridLayout();
57
buttonX = new QPushButton("<->" + tr("&X"));
58
buttonX->setAutoDefault( false );
59
gl1->addWidget( buttonX, 0, 0);
61
buttonXErr = new QPushButton("<->" + tr("x&Err"));
62
buttonXErr->setAutoDefault( false );
63
gl1->addWidget( buttonXErr, 0, 1);
65
buttonY = new QPushButton("<->" + tr("&Y"));
66
buttonY->setAutoDefault( false );
67
gl1->addWidget( buttonY, 1, 0);
69
buttonYErr = new QPushButton("<->" + tr("yE&rr"));
70
buttonYErr->setAutoDefault( false );
71
gl1->addWidget( buttonYErr, 1, 1);
73
buttonZ = new QPushButton("<->" + tr("&Z"));
74
buttonZ->setAutoDefault( false );
75
gl1->addWidget( buttonZ, 2, 0);
76
gl1->setRowStretch(3,1);
78
QHBoxLayout *hl2 = new QHBoxLayout();
79
buttonNew = new QPushButton(tr("&New curve"));
80
buttonNew->setDefault( true );
81
buttonNew->setAutoDefault( true );
82
hl2->addWidget(buttonNew);
84
buttonDelete = new QPushButton(tr("&Delete curve"));
85
buttonDelete->setAutoDefault( false );
86
hl2->addWidget(buttonDelete);
88
QVBoxLayout *vl = new QVBoxLayout();
93
QGridLayout *gl2 = new QGridLayout(groupBox1);
94
gl2->addWidget(new QLabel(tr( "Worksheet" )), 0, 0);
95
boxTables = new QComboBox();
96
gl2->addWidget(boxTables, 0, 1);
97
columnsList = new QListWidget();
98
gl2->addWidget(columnsList, 1, 0);
99
gl2->addLayout(vl, 1, 1);
101
// middle part is only one widget
102
plotAssociations = new QListWidget();
104
// bottom part starts here
105
QHBoxLayout * bottomLayout = new QHBoxLayout();
106
bottomLayout->addStretch();
108
buttonOk = new QPushButton(tr("&Plot"));
109
buttonOk->setAutoDefault( false );
110
bottomLayout->addWidget( buttonOk );
112
buttonCancel = new QPushButton(tr("&Close"));
113
buttonCancel->setAutoDefault( false );
114
bottomLayout->addWidget( buttonCancel );
116
QVBoxLayout* vlayout = new QVBoxLayout( this );
117
vlayout->addWidget( groupBox1 );
118
vlayout->addWidget( plotAssociations );
119
vlayout->addLayout( bottomLayout );
121
// signals and slots connections
122
connect( boxTables, SIGNAL(activated(const QString &)),this, SLOT(changeColumnsList(const QString &)));
123
connect( buttonOk, SIGNAL( clicked() ), this, SLOT( accept() ) );
124
connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( reject() ) );
125
connect( buttonNew, SIGNAL( clicked() ), this, SLOT( addCurve() ) );
126
connect( buttonDelete, SIGNAL( clicked() ), this, SLOT( removeCurve() ) );
127
connect( buttonX, SIGNAL( clicked() ), this, SLOT(addXCol()));
128
connect( buttonY, SIGNAL( clicked() ), this, SLOT(addYCol()));
129
connect( buttonXErr, SIGNAL( clicked() ), this, SLOT(addXErrCol()));
130
connect( buttonYErr, SIGNAL( clicked() ), this, SLOT(addYErrCol()));
131
connect( buttonZ, SIGNAL( clicked() ), this, SLOT(addZCol()));
134
QSize PlotWizard::sizeHint() const
136
return QSize(350, 400);
139
void PlotWizard::accept()
141
QStringList curves, curves3D, ribbons;
142
for (int i=0; i < plotAssociations->count(); i++)
144
QString text = plotAssociations->item(i)->text();
145
if (text.endsWith("(X)"))
147
QMessageBox::critical(this, tr("Error"),
148
tr("Please define a Y column for the following curve") + ":\n\n" + text);
152
if ( text.contains("(Z)") )
154
if ( text.contains("(Y)") && !curves3D.contains(text) )
156
else if ( !text.contains("(Y)") && !ribbons.contains(text) )
159
else if ( text.contains("(xErr)") || text.contains("(yErr)"))
161
QStringList lst = text.split(",", QString::SkipEmptyParts);
163
QString master_curve = lst.join(",");
165
if (!curves.contains(master_curve))
166
curves.prepend(master_curve);
168
if (!curves.contains(text))
169
curves << text; //add error bars at the end of the list.
171
else if (!curves.contains(text))
172
curves.prepend(text);
175
if (curves.count()>0)
178
if (curves3D.count()>0)
181
if (ribbons.count()>0)
182
plot3DRibbon(ribbons);
188
void PlotWizard::changeColumnsList(const QString &table)
192
for( int i=0 ; i<columns.count() ; i++)
194
QString s = columns[i];
195
if ( s.contains(table) )
196
newList << s.remove(table+"_");
198
setColumnsListBoxContents(newList);
201
void PlotWizard::addXCol()
206
QString text = plotAssociations->currentItem()->text();
207
if ( text.contains("(X)") )
208
QMessageBox::warning(this, tr("Error"), tr("You have already defined a X column!"));
211
plotAssociations->currentItem()->setText(text+columnsList->currentItem()->text()+"(X)");
215
void PlotWizard::addYCol()
220
QString text = plotAssociations->currentItem()->text();
221
if ( !text.contains("(X)") )
222
QMessageBox::warning(this, tr("Error"),tr("You must define a X column first!"));
223
else if ( text.contains("(Y)") )
224
QMessageBox::warning(this, tr("Error"), tr("You have already defined a Y column!"));
227
plotAssociations->currentItem()->setText(text+", "+columnsList->currentItem()->text()+"(Y)");
231
void PlotWizard::addZCol()
236
QString text = plotAssociations->currentItem()->text();
237
if ( text.contains("(xErr)") || text.contains("(yErr)") )
238
QMessageBox::warning(this, tr("Error"), tr("This kind of curve is not handled by SciDAVis!"));
239
else if ( !text.contains("(X)") )
240
QMessageBox::warning(this, tr("Error"), tr("You must define a X column first!"));
241
else if ( text.contains("(Z)") )
242
QMessageBox::warning(this, tr("Error"), tr("You have already defined a Z column!"));
245
plotAssociations->currentItem()->setText(text+", "+columnsList->currentItem()->text()+"(Z)");
249
void PlotWizard::addXErrCol()
254
QString text = plotAssociations->currentItem()->text();
255
if ( text.contains("(Z)") )
256
QMessageBox::warning(this, tr("Error"), tr("This kind of curve is not handled by SciDAVis!"));
257
else if ( !text.contains("(X)") )
258
QMessageBox::warning(this, tr("Error"), tr("You must define a X column first!"));
259
else if ( !text.contains("(Y)") )
260
QMessageBox::warning(this, tr("Error"), tr("You must define a Y column first!"));
261
else if ( text.contains("(xErr)") || text.contains("(yErr)") )
262
QMessageBox::warning(this, tr("Error"), tr("You have already defined an error-bars column!"));
265
plotAssociations->currentItem()->setText(text+", "+columnsList->currentItem()->text()+"(xErr)");
269
void PlotWizard::addYErrCol()
274
QString text = plotAssociations->currentItem()->text();
275
if ( text.contains("(Z)") )
276
QMessageBox::warning(this, tr("Error"), tr("This kind of curve is not handled by SciDAVis!"));
277
else if ( !text.contains("(X)") )
278
QMessageBox::warning(this, tr("Error"), tr("You must define a X column first!"));
279
else if ( !text.contains("(Y)") )
280
QMessageBox::warning(this, tr("Error"), tr("You must define a Y column first!"));
281
else if ( text.contains("(xErr)") || text.contains("(yErr)") )
282
QMessageBox::warning(this, tr("Error"), tr("You have already defined an error-bars column!"));
285
plotAssociations->currentItem()->setText(text+", "+columnsList->currentItem()->text()+"(yErr)");
289
void PlotWizard::addCurve()
291
plotAssociations->addItem( boxTables->currentText() + ": " );
292
plotAssociations->setCurrentRow( plotAssociations->count()-1 );
295
void PlotWizard::removeCurve()
297
plotAssociations->takeItem( plotAssociations->currentRow() );
300
void PlotWizard::insertTablesList(const QStringList& tables)
302
boxTables->addItems(tables);
305
void PlotWizard::setColumnsListBoxContents(const QStringList& cols)
307
columnsList->clear();
308
columnsList->insertItems(0, cols);
309
columnsList->setCurrentRow(0);
312
void PlotWizard::setColumnsList(const QStringList& cols)
317
bool PlotWizard::noCurves()
319
if ( plotAssociations->count() == 0 )
321
QMessageBox::warning(0, tr("Error"), tr("You must add a new curve first!"));
328
void PlotWizard::plot3DRibbon(const QStringList& lst)
330
ApplicationWindow *app = (ApplicationWindow *)this->parent();
334
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
336
for (int i=0; i< lst.count(); i++)
339
int pos = s.find(":", 0);
340
QString table_name = s.left(pos) + "_";
341
Table *t = app->table(table_name);
344
int posX = s.find("(", pos);
345
QString xColName = table_name + s.mid(pos+2, posX-pos-2);
347
posX = s.find(",", posX);
348
int posY = s.find("(", posX);
349
QString yColName = table_name + s.mid(posX+2, posY-posX-2);
351
Graph3D *g = app->newPlot3D();
354
g->addData(t, xColName, yColName);
359
QApplication::restoreOverrideCursor();
362
void PlotWizard::plot3D(const QStringList& lst)
364
ApplicationWindow *app = (ApplicationWindow *)this->parent();
368
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
370
for (int i=0; i< lst.count(); i++)
373
int pos = s.find(":", 0);
374
QString table_name = s.left(pos) + "_";
375
Table *t = app->table(table_name);
378
int posX = s.find("(", pos);
379
QString xColName = table_name + s.mid(pos+2, posX-pos-2);
381
posX = s.find(",", posX);
382
int posY = s.find("(", posX);
383
QString yColName = table_name + s.mid(posX+2, posY-posX-2);
385
posY = s.find(",", posY);
386
int posZ = s.find("(", posY);
387
QString zColName = table_name + s.mid(posY+2, posZ-posY-2);
389
int xCol = t->colIndex(xColName);
390
int yCol = t->colIndex(yColName);
391
int zCol = t->colIndex(zColName);
392
if (xCol >= 0 && yCol >= 0 && zCol >= 0)
394
Graph3D *g = app->newPlot3D();
397
g->addData(t, xCol, yCol, zCol, 1);
403
QApplication::restoreOverrideCursor();
406
PlotWizard::~PlotWizard()