scidavis  2.3.0
About: SciDAVis is a free application for Scientific Data Analysis and Visualization (a fork off of QtiPlot).
  Fossies Dox: scidavis-2.3.0.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

FunctionDialog.cpp
Go to the documentation of this file.
1 
11 
29 #include "FunctionDialog.h"
30 #include "MyParser.h"
31 #include "ApplicationWindow.h"
32 #include "FunctionCurve.h"
33 
34 #include <QTextEdit>
35 #include <QLineEdit>
36 #include <QLayout>
37 #include <QSpinBox>
38 #include <QCheckBox>
39 #include <QPushButton>
40 #include <QLabel>
41 #include <QComboBox>
42 #include <QStackedWidget>
43 #include <QWidget>
44 #include <QMessageBox>
45 
46 FunctionDialog::FunctionDialog( QWidget* parent, Qt::WindowFlags fl )
47 : QDialog( parent, fl )
48 {
49  setWindowTitle( tr( "Add function curve" ) );
50  setSizeGripEnabled(true);
51 
52  QHBoxLayout *hbox1 = new QHBoxLayout();
53  hbox1->addWidget(new QLabel(tr( "Curve type " )));
54  boxType = new QComboBox();
55  boxType->addItem( tr( "Function" ) );
56  boxType->addItem( tr( "Parametric plot" ) );
57  boxType->addItem( tr( "Polar plot" ) );
58  hbox1->addWidget(boxType);
59 
60  optionStack = new QStackedWidget();
61  optionStack->setFrameShape( QFrame::StyledPanel );
62  optionStack->setFrameShadow( QStackedWidget::Plain );
63 
64  QGridLayout *gl1 = new QGridLayout();
65  gl1->addWidget(new QLabel(tr( "f(x)= " )), 0, 0);
66  boxFunction = new QTextEdit();
67  boxFunction->setMinimumWidth(350);
68  gl1->addWidget(boxFunction, 0, 1);
69  gl1->addWidget(new QLabel(tr( "From x= " )), 1, 0);
70  boxFrom = new QLineEdit();
71  boxFrom->setText("0");
72  gl1->addWidget(boxFrom, 1, 1);
73  gl1->addWidget(new QLabel(tr( "To x= " )), 2, 0);
74  boxTo = new QLineEdit();
75  boxTo->setText("1");
76  gl1->addWidget(boxTo, 2, 1);
77  gl1->addWidget(new QLabel(tr( "Points" )), 3, 0);
78  boxPoints = new QSpinBox();
79  boxPoints->setRange(2, 1000000);
80  boxPoints->setSingleStep(100);
81  boxPoints->setValue(100);
82  gl1->addWidget(boxPoints, 3, 1);
83 
84  functionPage = new QWidget();
85  functionPage->setLayout(gl1);
86  optionStack->addWidget( functionPage );
87 
88  QGridLayout *gl2 = new QGridLayout();
89  gl2->addWidget(new QLabel(tr( "Parameter" )), 0, 0);
90  boxParameter = new QLineEdit();
91  boxParameter->setText("m");
92  gl2->addWidget(boxParameter, 0, 1);
93  gl2->addWidget(new QLabel(tr( "From" )), 1, 0);
94  boxParFrom = new QLineEdit();
95  boxParFrom->setText("0");
96  gl2->addWidget(boxParFrom, 1, 1);
97  gl2->addWidget(new QLabel(tr( "To" )), 2, 0);
98  boxParTo = new QLineEdit();
99  boxParTo->setText("1");
100  gl2->addWidget(boxParTo, 2, 1);
101  gl2->addWidget(new QLabel(tr( "x = " )), 3, 0);
102  boxXFunction = new QComboBox( );
103  boxXFunction->setEditable ( true );
104  gl2->addWidget(boxXFunction, 3, 1);
105  gl2->addWidget(new QLabel(tr( "y = " )), 4, 0);
106  boxYFunction = new QComboBox( );
107  boxYFunction->setEditable ( true );
108  gl2->addWidget(boxYFunction, 4, 1);
109  gl2->addWidget(new QLabel(tr( "Points" )), 5, 0);
110  boxParPoints = new QSpinBox();
111  boxParPoints->setRange(2, 1000000);
112  boxParPoints->setSingleStep(100);
113  boxParPoints->setValue(100);
114  gl2->addWidget(boxParPoints, 5, 1);
115 
116  parametricPage = new QWidget();
117  parametricPage->setLayout(gl2);
118  optionStack->addWidget( parametricPage );
119 
120  QGridLayout *gl3 = new QGridLayout();
121  gl3->addWidget(new QLabel(tr( "Parameter" )), 0, 0);
122  boxPolarParameter = new QLineEdit();
123  boxPolarParameter->setText ("t");
124  gl3->addWidget(boxPolarParameter, 0, 1);
125  gl3->addWidget(new QLabel(tr( "From" )), 2, 0);
126  boxPolarFrom = new QLineEdit();
127  boxPolarFrom->setText("0");
128  gl3->addWidget(boxPolarFrom, 2, 1);
129  gl3->addWidget(new QLabel(tr( "To" )), 3, 0);
130  boxPolarTo = new QLineEdit();
131  boxPolarTo->setText("pi");
132  gl3->addWidget(boxPolarTo, 3, 1);
133  gl3->addWidget(new QLabel(tr( "R =" )), 4, 0);
134  boxPolarRadius = new QComboBox();
135  boxPolarRadius->setEditable ( true );
136  gl3->addWidget(boxPolarRadius, 4, 1);
137  gl3->addWidget(new QLabel(tr( "Theta =" )), 5, 0);
138  boxPolarTheta = new QComboBox();
139  boxPolarTheta->setEditable ( true );
140  gl3->addWidget(boxPolarTheta, 5, 1);
141  gl3->addWidget(new QLabel(tr( "Points" )), 6, 0);
142  boxPolarPoints = new QSpinBox();
143  boxPolarPoints->setRange(2, 1000000);
144  boxPolarPoints->setSingleStep(100);
145  boxPolarPoints->setValue(100);
146  gl3->addWidget(boxPolarPoints, 6, 1);
147 
148  polarPage = new QWidget();
149  polarPage->setLayout(gl3);
150  optionStack->addWidget( polarPage );
151 
152  buttonApply = new QPushButton(tr("&Apply"));
153  buttonClear = new QPushButton(tr( "Clear Function" ));
154  buttonOk = new QPushButton(tr( "Ok" ));
155  buttonOk->setDefault(true);
156  buttonCancel = new QPushButton(tr( "Close" ));
157 
158  QHBoxLayout *hbox2 = new QHBoxLayout();
159  hbox2->addWidget(buttonApply);
160  hbox2->addStretch();
161  hbox2->addWidget(buttonClear);
162  hbox2->addWidget(buttonOk);
163  hbox2->addWidget(buttonCancel);
164 
165  QVBoxLayout *vbox1 = new QVBoxLayout();
166  vbox1->addLayout(hbox1);
167  vbox1->addWidget(optionStack);
168  vbox1->addLayout(hbox2);
169 
170  setLayout(vbox1);
171  setFocusProxy (boxFunction);
172 
173  connect( boxType, SIGNAL( activated(int) ), this, SLOT( raiseWidget(int) ) );
174  connect(buttonApply, SIGNAL(clicked()), this, SLOT(apply()));
175  connect( buttonOk, SIGNAL( clicked() ), this, SLOT( accept() ) );
176  connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( reject() ) );
177  connect( buttonClear, SIGNAL( clicked() ), this, SLOT(clearList() ) );
178 
179  curveID = -1;
180  graph = 0;
181 }
182 
184 {
185  if (index)
186  buttonClear->setText( tr( "Clear List" ) );
187  else
188  buttonClear->setText( tr( "Clear Function" ) );
189 
190  optionStack->setCurrentIndex(index);
191 }
192 
194 {
195  if (!g)
196  return;
197 
198  graph = g;
199 
200  FunctionCurve *c = (FunctionCurve *)graph->curve(curve);
201  if (!c)
202  return;
203 
204  curveID = curve;
205 
206  QStringList formulas = c->formulas();
208  {
209  boxFunction->setText(formulas[0]);
210  boxFrom->setText(QString::number(c->startRange(), 'g', 15));
211  boxTo->setText(QString::number(c->endRange(), 'g', 15));
212  boxPoints->setValue(c->dataSize());
213  }
214  else if (c->functionType() == FunctionCurve::Polar)
215  {
216  optionStack->setCurrentIndex(2);
217  boxType->setCurrentIndex(2);
218 
219  boxPolarRadius->setItemText(boxPolarRadius->currentIndex(), formulas[0]);
220  boxPolarTheta->setItemText(boxPolarTheta->currentIndex(), formulas[1]);
221  boxPolarParameter->setText(c->variable());
222  boxPolarFrom->setText(QString::number(c->startRange(), 'g', 15));
223  boxPolarTo->setText(QString::number(c->endRange(), 'g', 15));
224  boxPolarPoints->setValue(c->dataSize());
225  }
226  else if (c->functionType() == FunctionCurve::Parametric)
227  {
228  boxType->setCurrentIndex(1);
229  optionStack->setCurrentIndex(1);
230 
231  boxXFunction->setItemText(boxXFunction->currentIndex(), formulas[0]);
232  boxYFunction->setItemText(boxYFunction->currentIndex(), formulas[1]);
233  boxParameter->setText(c->variable());
234  boxParFrom->setText(QString::number(c->startRange(), 'g', 15));
235  boxParTo->setText(QString::number(c->endRange(), 'g', 15));
236  boxParPoints->setValue(c->dataSize());
237  }
238 }
239 
241 {
242  int type=boxType->currentIndex();
243  switch (type)
244  {
245  case 0:
246  boxFunction->clear();
247  break;
248 
249  case 1:
250  boxXFunction->clear();
251  boxYFunction->clear();
253  break;
254 
255  case 2:
256  boxPolarTheta->clear();
257  boxPolarRadius->clear();
259  break;
260  }
261 }
262 
264 {
265  QString from=boxFrom->text().toLower();
266  QString to=boxTo->text().toLower();
267  QString points=boxPoints->text().toLower();
268 
269  double start, end;
270  try
271  {
272  MyParser parser;
273  parser.SetExpr(from);
274  start=parser.Eval();
275  }
276  catch(mu::ParserError &e)
277  {
278  QMessageBox::critical(0, tr("Start limit error"), QStringFromString(e.GetMsg()));
279  boxFrom->setFocus();
280  return false;
281  }
282  try
283  {
284  MyParser parser;
285  parser.SetExpr(to);
286  end=parser.Eval();
287  }
288  catch(mu::ParserError &e)
289  {
290  QMessageBox::critical(0, tr("End limit error"), QStringFromString(e.GetMsg()));
291  boxTo->setFocus();
292  return false;
293  }
294 
295  if (start>=end)
296  {
297  QMessageBox::critical(0, tr("Input error"),
298  tr("Please enter x limits that satisfy: from < end!"));
299  boxTo->setFocus();
300  return false;
301  }
302 
303  QString formula = boxFunction->toPlainText().replace(QChar::ParagraphSeparator,"\n");
304 
305  // Collecting all the information
306  int type = boxType->currentIndex();
307  QStringList formulas;
308  QList<double> ranges;
309  formulas+=formula;
310  ranges+=start;
311  ranges+=end;
312 
313  bool result;
314  ApplicationWindow *app = (ApplicationWindow *)this->parent();
315  app->updateFunctionLists(type,formulas);
316  if (!graph)
317  result = app->newFunctionPlot(type, formulas, "x", ranges, boxPoints->value());
318  else {
319  if (curveID >= 0)
320  result = graph->modifyFunctionCurve(app, curveID, type, formulas, "x", ranges, boxPoints->value());
321  else
322  result = graph->addFunctionCurve(app, type,formulas, "x", ranges, boxPoints->value());
323  }
324  if (!result) boxFunction->setFocus();
325  return result;
326 }
327 
329 {
330  QString from=boxParFrom->text().toLower();
331  QString to=boxParTo->text().toLower();
332  QString points=boxParPoints->text().toLower();
333 
334  double start, end;
335  try
336  {
337  MyParser parser;
338  parser.SetExpr(from);
339  start=parser.Eval();
340  }
341  catch(mu::ParserError &e)
342  {
343  QMessageBox::critical(0, tr("Start limit error"), QStringFromString(e.GetMsg()));
344  boxParFrom->setFocus();
345  return false;
346  }
347 
348  try
349  {
350  MyParser parser;
351  parser.SetExpr(to);
352  end=parser.Eval();
353  }
354  catch(mu::ParserError &e)
355  {
356  QMessageBox::critical(0, tr("End limit error"), QStringFromString(e.GetMsg()));
357  boxParTo->setFocus();
358  return false;
359  }
360 
361  if (start>=end)
362  {
363  QMessageBox::critical(0, tr("Input error"),
364  tr("Please enter parameter limits that satisfy: from < end!"));
365  boxParTo->setFocus();
366  return false;
367  }
368 
369  QString xformula=boxXFunction->currentText().replace(QChar::ParagraphSeparator,"\n");
370  QString yformula=boxYFunction->currentText().replace(QChar::ParagraphSeparator,"\n");
371 
372  // Collecting all the information
373  int type = boxType->currentIndex();
374  QStringList formulas;
375  QList<double> ranges;
376  formulas+=xformula;
377  formulas+=yformula;
378  ranges+=start;
379  ranges+=end;
380 
381  bool result;
382  ApplicationWindow *app = (ApplicationWindow *)this->parent();
383  app->updateFunctionLists(type,formulas);
384  if (!graph)
385  result = app->newFunctionPlot(type, formulas, boxParameter->text(),ranges, boxParPoints->value());
386  else {
387  if (curveID >= 0)
388  result = graph->modifyFunctionCurve(app, curveID, type, formulas, boxParameter->text(),ranges, boxParPoints->value());
389  else
390  result = graph->addFunctionCurve(app, type,formulas, boxParameter->text(),ranges, boxParPoints->value());
391  }
392  if (!result) boxXFunction->setFocus();
393  return result;
394 }
395 
397 {
398  QString from=boxPolarFrom->text().toLower();
399  QString to=boxPolarTo->text().toLower();
400  QString points=boxPolarPoints->text().toLower();
401 
402  double start, end;
403  try
404  {
405  MyParser parser;
406  parser.SetExpr(from);
407  start=parser.Eval();
408  }
409  catch(mu::ParserError &e)
410  {
411  QMessageBox::critical(0, tr("Start limit error"), QStringFromString(e.GetMsg()));
412  boxPolarFrom->setFocus();
413  return false;
414  }
415 
416  try
417  {
418  MyParser parser;
419  parser.SetExpr(to);
420  end=parser.Eval();
421  }
422  catch(mu::ParserError &e)
423  {
424  QMessageBox::critical(0, tr("End limit error"), QStringFromString(e.GetMsg()));
425  boxPolarTo->setFocus();
426  return false;
427  }
428 
429  if (start>=end)
430  {
431  QMessageBox::critical(0, tr("Input error"),
432  tr("Please enter parameter limits that satisfy: from < end!"));
433  boxPolarTo->setFocus();
434  return false;
435  }
436 
437  QString rformula=boxPolarRadius->currentText().replace(QChar::ParagraphSeparator,"\n");
438  QString tformula=boxPolarTheta->currentText().replace(QChar::ParagraphSeparator,"\n");
439 
440  // Collecting all the information
441  int type = boxType->currentIndex();
442  QStringList formulas;
443  QList<double> ranges;
444  formulas+=rformula;
445  formulas+=tformula;
446  ranges+=start;
447  ranges+=end;
448 
449  bool result;
450  ApplicationWindow *app = (ApplicationWindow *)this->parent();
451  app->updateFunctionLists(type,formulas);
452 
453  if (!graph)
454  result = app->newFunctionPlot(type, formulas, boxPolarParameter->text(),ranges, boxPolarPoints->value());
455  else {
456  if (curveID >= 0)
457  result = graph->modifyFunctionCurve(app, curveID, type, formulas, boxPolarParameter->text(),ranges, boxPolarPoints->value());
458  else
459  result = graph->addFunctionCurve(app, type, formulas, boxPolarParameter->text(),ranges, boxPolarPoints->value());
460  }
461  if (!result)
462  boxPolarRadius->setFocus();
463  return result;
464 }
465 
467 {
468  if (apply())
469  close();
470 }
471 
473  bool result = true;
474  switch (boxType->currentIndex())
475  {
476  case 0:
477  result = acceptFunction();
478  break;
479 
480  case 1:
481  result = acceptParametric();
482  break;
483 
484  case 2:
485  result = acceptPolar();
486  break;
487  }
488  return result;
489 }
490 
491 void FunctionDialog::insertParamFunctionsList(const QStringList& xList, const QStringList& yList)
492 {
493  boxXFunction->insertItems (0, xList);
494  boxYFunction->insertItems (0, yList);
495 }
496 
497 void FunctionDialog::insertPolarFunctionsList(const QStringList& rList, const QStringList& thetaList)
498 {
499  boxPolarRadius->insertItems (0, rList);
500  boxPolarTheta->insertItems (0, thetaList);
501 }
502 
FunctionDialog::boxPolarRadius
QComboBox * boxPolarRadius
Definition: FunctionDialog.h:54
FunctionDialog::raiseWidget
void raiseWidget(int index)
Definition: FunctionDialog.cpp:183
MyParser.h
FunctionCurve::Parametric
@ Parametric
Definition: FunctionCurve.h:41
FunctionDialog::boxFrom
QLineEdit * boxFrom
Definition: FunctionDialog.h:58
FunctionCurve.h
FunctionDialog::boxParPoints
QSpinBox * boxParPoints
Definition: FunctionDialog.h:71
ApplicationWindow::newFunctionPlot
bool newFunctionPlot(int type, QStringList &formulas, const QString &var, QList< double > &ranges, int points)
Definition: ApplicationWindow.cpp:8423
FunctionDialog::boxXFunction
QComboBox * boxXFunction
Definition: FunctionDialog.h:52
FunctionDialog::boxTo
QLineEdit * boxTo
Definition: FunctionDialog.h:59
FunctionDialog::clearParamFunctionsList
void clearParamFunctionsList()
QStringFromString
QString QStringFromString(const std::string &x)
Definition: QStringStdString.h:15
FunctionDialog::boxParTo
QLineEdit * boxParTo
Definition: FunctionDialog.h:62
FunctionCurve::formulas
QStringList formulas()
Definition: FunctionCurve.h:50
FunctionDialog::acceptPolar
bool acceptPolar()
Definition: FunctionDialog.cpp:396
FunctionDialog::setCurveToModify
void setCurveToModify(Graph *g, int curve)
Definition: FunctionDialog.cpp:193
FunctionDialog::functionPage
QWidget * functionPage
Definition: FunctionDialog.h:75
FunctionDialog::buttonOk
QPushButton * buttonOk
Definition: FunctionDialog.h:69
FunctionDialog::boxType
QComboBox * boxType
Definition: FunctionDialog.h:56
FunctionDialog::polarPage
QWidget * polarPage
Definition: FunctionDialog.h:76
FunctionCurve::variable
QString variable()
Definition: FunctionCurve.h:56
MyParser
Mathematical parser class based on muParser.
Definition: MyParser.h:52
FunctionDialog::FunctionDialog
FunctionDialog(QWidget *parent=0, Qt::WindowFlags fl=0)
Definition: FunctionDialog.cpp:46
FunctionCurve
Definition: FunctionCurve.h:38
FunctionDialog::boxParFrom
QLineEdit * boxParFrom
Definition: FunctionDialog.h:61
FunctionDialog.h
MyParser::SetExpr
void SetExpr(const QString &x)
Definition: MyParser.h:56
FunctionDialog::boxParameter
QLineEdit * boxParameter
Definition: FunctionDialog.h:60
Graph::addFunctionCurve
bool addFunctionCurve(ApplicationWindow *parent, int type, const QStringList &formulas, const QString &var, QList< double > &ranges, int points, const QString &title={})
Definition: Graph.cpp:3922
FunctionDialog::boxPolarPoints
QSpinBox * boxPolarPoints
Definition: FunctionDialog.h:72
FunctionCurve::Polar
@ Polar
Definition: FunctionCurve.h:41
FunctionDialog::buttonApply
QPushButton * buttonApply
Definition: FunctionDialog.h:66
FunctionDialog::boxPolarTo
QLineEdit * boxPolarTo
Definition: FunctionDialog.h:65
FunctionDialog::boxPolarFrom
QLineEdit * boxPolarFrom
Definition: FunctionDialog.h:64
FunctionCurve::endRange
double endRange()
Definition: FunctionCurve.h:47
FunctionDialog::boxPolarParameter
QLineEdit * boxPolarParameter
Definition: FunctionDialog.h:63
FunctionDialog::buttonCancel
QPushButton * buttonCancel
Definition: FunctionDialog.h:68
FunctionDialog::optionStack
QStackedWidget * optionStack
Definition: FunctionDialog.h:73
ApplicationWindow::updateFunctionLists
void updateFunctionLists(int type, QStringList &formulas)
Definition: ApplicationWindow.cpp:8392
FunctionDialog::accept
void accept()
Definition: FunctionDialog.cpp:466
FunctionCurve::Normal
@ Normal
Definition: FunctionCurve.h:41
FunctionDialog::curveID
int curveID
Definition: FunctionDialog.h:100
ApplicationWindow
SciDAVis's main window.
Definition: ApplicationWindow.h:122
FunctionDialog::acceptFunction
bool acceptFunction()
Definition: FunctionDialog.cpp:263
Graph::modifyFunctionCurve
bool modifyFunctionCurve(ApplicationWindow *parent, int curve, int type, const QStringList &formulas, const QString &var, QList< double > &ranges, int points)
Definition: Graph.cpp:3859
FunctionCurve::functionType
FunctionType functionType()
Definition: FunctionCurve.h:59
FunctionDialog::clearPolarFunctionsList
void clearPolarFunctionsList()
FunctionDialog::apply
bool apply()
Definition: FunctionDialog.cpp:472
FunctionDialog::acceptParametric
bool acceptParametric()
Definition: FunctionDialog.cpp:328
FunctionDialog::insertParamFunctionsList
void insertParamFunctionsList(const QStringList &xList, const QStringList &yList)
Definition: FunctionDialog.cpp:491
FunctionDialog::buttonClear
QPushButton * buttonClear
Definition: FunctionDialog.h:67
FunctionDialog::boxFunction
QTextEdit * boxFunction
Definition: FunctionDialog.h:74
FunctionDialog::boxPoints
QSpinBox * boxPoints
Definition: FunctionDialog.h:70
Graph
A 2D-plotting widget.
Definition: Graph.h:119
FunctionCurve::startRange
double startRange()
Definition: FunctionCurve.h:46
ApplicationWindow.h
FunctionDialog::clearList
void clearList()
Definition: FunctionDialog.cpp:240
FunctionDialog::insertPolarFunctionsList
void insertPolarFunctionsList(const QStringList &rList, const QStringList &thetaList)
Definition: FunctionDialog.cpp:497
Graph::curve
QwtPlotCurve * curve(int index) const
get curve by index
Definition: Graph.cpp:2821
FunctionDialog::parametricPage
QWidget * parametricPage
Definition: FunctionDialog.h:77
FunctionDialog::graph
Graph * graph
Definition: FunctionDialog.h:99
FunctionDialog::boxPolarTheta
QComboBox * boxPolarTheta
Definition: FunctionDialog.h:55
FunctionDialog::boxYFunction
QComboBox * boxYFunction
Definition: FunctionDialog.h:53