~valavanisalex/ubuntu/maverick/scidavis/fix-604811

« back to all changes in this revision

Viewing changes to scidavis/src/PlotWizard.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Ruben Molina
  • Date: 2009-09-06 11:34:04 UTC
  • Revision ID: james.westby@ubuntu.com-20090906113404-4awaey82l3686w4q
Tags: upstream-0.2.3
ImportĀ upstreamĀ versionĀ 0.2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
    File                 : PlotWizard.cpp
 
3
    Project              : SciDAVis
 
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
 
8
 
 
9
 ***************************************************************************/
 
10
 
 
11
/***************************************************************************
 
12
 *                                                                         *
 
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.                                    *
 
17
 *                                                                         *
 
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.                           *
 
22
 *                                                                         *
 
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                                           *
 
27
 *                                                                         *
 
28
 ***************************************************************************/
 
29
#include "PlotWizard.h"
 
30
#include "ApplicationWindow.h"
 
31
#include "Table.h"
 
32
#include "Graph3D.h"
 
33
 
 
34
#include <QApplication>
 
35
#include <QHBoxLayout>
 
36
#include <QVBoxLayout>
 
37
#include <QGridLayout>
 
38
#include <QSizePolicy>
 
39
#include <QLabel>
 
40
#include <QMessageBox>
 
41
#include <QGroupBox>
 
42
#include <QPushButton>
 
43
#include <QListWidget>
 
44
#include <QComboBox>
 
45
 
 
46
PlotWizard::PlotWizard( QWidget* parent, Qt::WFlags fl )
 
47
: QDialog( parent, fl )
 
48
{
 
49
        setWindowTitle( tr("Select Columns to Plot") );
 
50
 
 
51
        setSizeGripEnabled( true );
 
52
 
 
53
        // top part starts here
 
54
        groupBox1 = new QGroupBox();
 
55
 
 
56
    QGridLayout *gl1 = new QGridLayout();
 
57
        buttonX = new QPushButton("<->" + tr("&X"));
 
58
        buttonX->setAutoDefault( false );
 
59
        gl1->addWidget( buttonX, 0, 0);
 
60
 
 
61
        buttonXErr = new QPushButton("<->" + tr("x&Err"));
 
62
        buttonXErr->setAutoDefault( false );
 
63
        gl1->addWidget( buttonXErr, 0, 1);
 
64
 
 
65
        buttonY = new QPushButton("<->" + tr("&Y"));
 
66
        buttonY->setAutoDefault( false );
 
67
        gl1->addWidget( buttonY, 1, 0);
 
68
 
 
69
        buttonYErr = new QPushButton("<->" + tr("yE&rr"));
 
70
        buttonYErr->setAutoDefault( false );
 
71
        gl1->addWidget( buttonYErr, 1, 1);
 
72
 
 
73
        buttonZ = new QPushButton("<->" + tr("&Z"));
 
74
        buttonZ->setAutoDefault( false );
 
75
        gl1->addWidget( buttonZ, 2, 0);
 
76
    gl1->setRowStretch(3,1);
 
77
 
 
78
    QHBoxLayout *hl2 = new QHBoxLayout();
 
79
    buttonNew = new QPushButton(tr("&New curve"));
 
80
    buttonNew->setDefault( true );
 
81
    buttonNew->setAutoDefault( true );
 
82
        hl2->addWidget(buttonNew);
 
83
 
 
84
        buttonDelete = new QPushButton(tr("&Delete curve"));
 
85
    buttonDelete->setAutoDefault( false );
 
86
        hl2->addWidget(buttonDelete);
 
87
 
 
88
    QVBoxLayout *vl = new QVBoxLayout();
 
89
    vl->addLayout(gl1);
 
90
    vl->addStretch();
 
91
    vl->addLayout(hl2);
 
92
 
 
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);
 
100
 
 
101
        // middle part is only one widget
 
102
        plotAssociations = new QListWidget();
 
103
 
 
104
        // bottom part starts here
 
105
        QHBoxLayout * bottomLayout = new QHBoxLayout();
 
106
    bottomLayout->addStretch();
 
107
 
 
108
        buttonOk = new QPushButton(tr("&Plot"));
 
109
    buttonOk->setAutoDefault( false );
 
110
        bottomLayout->addWidget( buttonOk );
 
111
 
 
112
        buttonCancel = new QPushButton(tr("&Close"));
 
113
    buttonCancel->setAutoDefault( false );
 
114
        bottomLayout->addWidget( buttonCancel );
 
115
 
 
116
        QVBoxLayout* vlayout = new QVBoxLayout( this );
 
117
        vlayout->addWidget( groupBox1 );
 
118
        vlayout->addWidget( plotAssociations );
 
119
        vlayout->addLayout( bottomLayout );
 
120
 
 
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()));
 
132
}
 
133
 
 
134
QSize PlotWizard::sizeHint() const
 
135
{
 
136
        return QSize(350, 400);
 
137
}
 
138
 
 
139
void PlotWizard::accept()
 
140
{
 
141
        QStringList curves, curves3D, ribbons;
 
142
        for (int i=0; i < plotAssociations->count(); i++)
 
143
        {
 
144
                QString text = plotAssociations->item(i)->text();
 
145
        if (text.endsWith("(X)"))
 
146
        {
 
147
            QMessageBox::critical(this, tr("Error"),
 
148
            tr("Please define a Y column for the following curve") + ":\n\n" + text);
 
149
            return;
 
150
        }
 
151
 
 
152
                if ( text.contains("(Z)") )
 
153
                {
 
154
                        if ( text.contains("(Y)") && !curves3D.contains(text) )
 
155
                                curves3D << text;
 
156
                        else if ( !text.contains("(Y)") && !ribbons.contains(text) )
 
157
                                ribbons << text;
 
158
                }
 
159
                else if ( text.contains("(xErr)") || text.contains("(yErr)"))
 
160
                {
 
161
                        QStringList lst = text.split(",", QString::SkipEmptyParts);
 
162
                        lst.pop_back();
 
163
                        QString master_curve = lst.join(",");
 
164
 
 
165
                        if (!curves.contains(master_curve))
 
166
                                curves.prepend(master_curve);
 
167
 
 
168
                        if (!curves.contains(text))
 
169
                                curves << text; //add error bars at the end of the list.
 
170
                }
 
171
                else if (!curves.contains(text))
 
172
                        curves.prepend(text);
 
173
        }
 
174
 
 
175
        if (curves.count()>0)
 
176
        emit plot(curves);
 
177
 
 
178
         if (curves3D.count()>0)
 
179
                plot3D(curves3D);
 
180
 
 
181
    if (ribbons.count()>0)
 
182
        plot3DRibbon(ribbons);
 
183
 
 
184
        if(!noCurves())
 
185
                close();
 
186
}
 
187
 
 
188
void PlotWizard::changeColumnsList(const QString &table)
 
189
{
 
190
        QStringList newList;
 
191
 
 
192
        for( int i=0 ; i<columns.count() ; i++)
 
193
        {
 
194
                QString s = columns[i];
 
195
                if ( s.contains(table) )
 
196
                        newList << s.remove(table+"_");
 
197
        }
 
198
        setColumnsListBoxContents(newList);
 
199
}
 
200
 
 
201
void PlotWizard::addXCol()
 
202
{
 
203
        if (noCurves())
 
204
                return;
 
205
 
 
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!"));
 
209
        else
 
210
        {
 
211
                plotAssociations->currentItem()->setText(text+columnsList->currentItem()->text()+"(X)");
 
212
        }
 
213
}
 
214
 
 
215
void PlotWizard::addYCol()
 
216
{
 
217
        if (noCurves())
 
218
                return;
 
219
 
 
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!"));
 
225
        else
 
226
        {
 
227
                plotAssociations->currentItem()->setText(text+", "+columnsList->currentItem()->text()+"(Y)");
 
228
        }
 
229
}
 
230
 
 
231
void PlotWizard::addZCol()
 
232
{
 
233
        if (noCurves())
 
234
                return;
 
235
 
 
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!"));
 
243
        else
 
244
        {
 
245
                plotAssociations->currentItem()->setText(text+", "+columnsList->currentItem()->text()+"(Z)");
 
246
        }
 
247
}
 
248
 
 
249
void PlotWizard::addXErrCol()
 
250
{
 
251
        if (noCurves())
 
252
                return;
 
253
 
 
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!"));
 
263
        else
 
264
        {
 
265
                plotAssociations->currentItem()->setText(text+", "+columnsList->currentItem()->text()+"(xErr)");
 
266
        }
 
267
}
 
268
 
 
269
void PlotWizard::addYErrCol()
 
270
{
 
271
        if (noCurves())
 
272
                return;
 
273
 
 
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!"));
 
283
        else
 
284
        {
 
285
                plotAssociations->currentItem()->setText(text+", "+columnsList->currentItem()->text()+"(yErr)");
 
286
        }
 
287
}
 
288
 
 
289
void PlotWizard::addCurve()
 
290
{
 
291
        plotAssociations->addItem( boxTables->currentText() + ": " );
 
292
        plotAssociations->setCurrentRow( plotAssociations->count()-1 );
 
293
}
 
294
 
 
295
void PlotWizard::removeCurve()
 
296
{
 
297
        plotAssociations->takeItem( plotAssociations->currentRow() );
 
298
}
 
299
 
 
300
void PlotWizard::insertTablesList(const QStringList& tables)
 
301
{
 
302
        boxTables->addItems(tables);
 
303
}
 
304
 
 
305
void PlotWizard::setColumnsListBoxContents(const QStringList& cols)
 
306
{
 
307
        columnsList->clear();
 
308
        columnsList->insertItems(0, cols);
 
309
        columnsList->setCurrentRow(0);
 
310
}
 
311
 
 
312
void PlotWizard::setColumnsList(const QStringList& cols)
 
313
{
 
314
        columns = cols;
 
315
}
 
316
 
 
317
bool PlotWizard::noCurves()
 
318
{
 
319
        if ( plotAssociations->count() == 0 )
 
320
        {
 
321
                QMessageBox::warning(0, tr("Error"), tr("You must add a new curve first!"));
 
322
                return true;
 
323
        }
 
324
        else
 
325
                return false;
 
326
}
 
327
 
 
328
void PlotWizard::plot3DRibbon(const QStringList& lst)
 
329
{
 
330
    ApplicationWindow *app = (ApplicationWindow *)this->parent();
 
331
    if (!app)
 
332
        return;
 
333
 
 
334
    QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
 
335
 
 
336
    for (int i=0; i< lst.count(); i++)
 
337
    {
 
338
        QString s = lst[i];
 
339
        int pos = s.find(":", 0);
 
340
        QString table_name = s.left(pos) + "_";
 
341
        Table *t = app->table(table_name);
 
342
        if (t)
 
343
        {
 
344
            int posX = s.find("(", pos);
 
345
            QString xColName = table_name + s.mid(pos+2, posX-pos-2);
 
346
 
 
347
            posX = s.find(",", posX);
 
348
            int posY = s.find("(", posX);
 
349
            QString yColName = table_name + s.mid(posX+2, posY-posX-2);
 
350
 
 
351
            Graph3D *g = app->newPlot3D();
 
352
            if (g)
 
353
            {
 
354
                g->addData(t, xColName, yColName);
 
355
                g->update();
 
356
            }
 
357
        }
 
358
    }
 
359
    QApplication::restoreOverrideCursor();
 
360
}
 
361
 
 
362
void PlotWizard::plot3D(const QStringList& lst)
 
363
{
 
364
    ApplicationWindow *app = (ApplicationWindow *)this->parent();
 
365
    if (!app)
 
366
        return;
 
367
 
 
368
        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
 
369
 
 
370
    for (int i=0; i< lst.count(); i++)
 
371
    {
 
372
        QString s = lst[i];
 
373
        int pos = s.find(":", 0);
 
374
        QString table_name = s.left(pos) + "_";
 
375
        Table *t = app->table(table_name);
 
376
        if (t)
 
377
        {
 
378
            int posX = s.find("(", pos);
 
379
            QString xColName = table_name + s.mid(pos+2, posX-pos-2);
 
380
 
 
381
            posX = s.find(",", posX);
 
382
            int posY = s.find("(", posX);
 
383
            QString yColName = table_name + s.mid(posX+2, posY-posX-2);
 
384
 
 
385
            posY = s.find(",", posY);
 
386
            int posZ = s.find("(", posY);
 
387
            QString zColName = table_name + s.mid(posY+2, posZ-posY-2);
 
388
 
 
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)
 
393
            {
 
394
                Graph3D *g = app->newPlot3D();
 
395
                if (g)
 
396
                {
 
397
                    g->addData(t, xCol, yCol, zCol, 1);
 
398
                    g->update();
 
399
                }
 
400
            }
 
401
        }
 
402
    }
 
403
        QApplication::restoreOverrideCursor();
 
404
}
 
405
 
 
406
PlotWizard::~PlotWizard()
 
407
{
 
408
}