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)  

FitDialog.cpp
Go to the documentation of this file.
1 
11 
29 #include "FitDialog.h"
30 #include "MyParser.h"
31 #include "ApplicationWindow.h"
32 #include "ColorButton.h"
33 #include "Fit.h"
34 #include "MultiPeakFit.h"
35 #include "ExponentialFit.h"
36 #include "PolynomialFit.h"
37 #include "PluginFit.h"
38 #include "NonLinearFit.h"
39 #include "SigmoidalFit.h"
40 #include "Matrix.h"
41 #include <muParserError.h>
42 
43 #include <QListWidget>
44 #include <QTableWidget>
45 #include <QHeaderView>
46 #include <QLineEdit>
47 #include <QLayout>
48 #include <QSpinBox>
49 #include <QCheckBox>
50 #include <QPushButton>
51 #include <QLabel>
52 #include <QStackedWidget>
53 #include <QWidget>
54 #include <QMessageBox>
55 #include <QComboBox>
56 #include <QWidgetList>
57 #include <QRadioButton>
58 #include <QFileDialog>
59 #include <QGroupBox>
60 #include <QLibrary>
61 #include <QLocale>
62 #include <stdio.h>
63 
64 #define CONFS(string) QString::number(QLocale().toDouble(string),'g',boxPrecision->value())
65 
66 FitDialog::FitDialog( QWidget* parent, Qt::WindowFlags fl )
67 : QDialog( parent, fl )
68 {
69  setWindowTitle(tr("Fit Wizard"));
70  setSizeGripEnabled( true );
71 
72  d_user_function_names = QStringList();
73  d_user_functions = QStringList();
74  d_user_function_params = QStringList();
75 
76  d_fitter = 0;
77 
78  tw = new QStackedWidget();
79 
80  initEditPage();
81  initFitPage();
83 
84  QVBoxLayout* vl = new QVBoxLayout();
85  vl->addWidget(tw);
86  setLayout(vl);
87 
90 
91  categoryBox->setCurrentRow (2);
92  funcBox->setCurrentRow (0);
93 
94  loadPlugins();
95 }
96 
98 {
99  QGridLayout *gl1 = new QGridLayout();
100  gl1->addWidget(new QLabel(tr("Curve")), 0, 0);
101  boxCurve = new QComboBox();
102  gl1->addWidget(boxCurve, 0, 1);
103  gl1->addWidget(new QLabel(tr("Function")), 1, 0);
104  lblFunction = new QLabel();
105  gl1->addWidget(lblFunction, 1, 1);
106  boxFunction = new QTextEdit();
107  boxFunction->setReadOnly(true);
108  QPalette palette = boxFunction->palette();
109  palette.setColor(QPalette::Active, QPalette::Base, Qt::lightGray);
110  boxFunction->setPalette(palette);
111  boxFunction->setMaximumHeight(50);
112  gl1->addWidget(boxFunction, 2, 1);
113  gl1->addWidget(new QLabel( tr("Initial guesses")), 3, 0 );
114 
115  boxParams = new QTableWidget();
116  boxParams->setColumnCount(3);
117 #if QT_VERSION >= 0x050000
118  boxParams->horizontalHeader()->setSectionsClickable(false);
119  boxParams->horizontalHeader()->setSectionResizeMode (0, QHeaderView::ResizeToContents);
120  boxParams->horizontalHeader()->setSectionResizeMode (1, QHeaderView::Stretch);
121  boxParams->horizontalHeader()->setSectionResizeMode (2, QHeaderView::ResizeToContents);
122  boxParams->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
123 #else
124  boxParams->horizontalHeader()->setClickable(false);
125  boxParams->horizontalHeader()->setResizeMode (0, QHeaderView::ResizeToContents);
126  boxParams->horizontalHeader()->setResizeMode (1, QHeaderView::Stretch);
127  boxParams->horizontalHeader()->setResizeMode (2, QHeaderView::ResizeToContents);
128  boxParams->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
129 #endif
130  QStringList header = QStringList() << tr("Parameter") << tr("Value") << tr("Constant");
131  boxParams->setHorizontalHeaderLabels(header);
132  boxParams->verticalHeader()->hide();
133  gl1->addWidget(boxParams, 3, 1);
134 
135  gl1->addWidget(new QLabel( tr("Algorithm")), 4, 0 );
136  boxAlgorithm = new QComboBox();
137  boxAlgorithm->addItem(tr("Scaled Levenberg-Marquardt"));
138  boxAlgorithm->addItem(tr("Unscaled Levenberg-Marquardt"));
139  boxAlgorithm->addItem(tr("Nelder-Mead Simplex"));
140  gl1->addWidget(boxAlgorithm, 4, 1);
141 
142  gl1->addWidget(new QLabel( tr("Color")), 5, 0);
143  btnColor = new ColorButton();
144  btnColor->setColor(QColor(Qt::red));
145  gl1->addWidget(btnColor, 5, 1);
146 
147  QGroupBox *gb1 = new QGroupBox();
148  gb1->setLayout(gl1);
149 
150  QGridLayout *gl2 = new QGridLayout();
151  gl2->addWidget(new QLabel(tr("From x=")), 0, 0);
152  boxFrom = new QLineEdit();
153  gl2->addWidget(boxFrom, 0, 1);
154  gl2->addWidget(new QLabel( tr("To x=")), 1, 0);
155  boxTo = new QLineEdit();
156  gl2->addWidget(boxTo, 1, 1);
157  QGroupBox *gb2 = new QGroupBox();
158  gb2->setLayout(gl2);
159 
160  QGridLayout *gl3 = new QGridLayout();
161  gl3->addWidget(new QLabel(tr("Iterations")), 0, 0);
162  boxPoints = new QSpinBox();
163  boxPoints->setRange(10, 10000);
164  boxPoints->setSingleStep(50);
165  boxPoints->setValue(1000);
166  gl3->addWidget(boxPoints, 0, 1);
167  gl3->addWidget(new QLabel( tr("Tolerance")), 1, 0);
168  boxTolerance = new QLineEdit("1e-4");
169  gl3->addWidget(boxTolerance, 1, 1);
170  QGroupBox *gb3 = new QGroupBox();
171  gb3->setLayout(gl3);
172 
173  QHBoxLayout *hbox1 = new QHBoxLayout();
174  hbox1->addWidget(gb2);
175  hbox1->addWidget(gb3);
176 
177  QHBoxLayout *hbox2 = new QHBoxLayout();
178  hbox2->addWidget(new QLabel(tr( "Y Error Source" )));
179  boxYErrorSource = new QComboBox();
180  boxYErrorSource->addItem(tr("Errors Unknown"));
181  boxYErrorSource->addItem(tr("Associated"));
182  boxYErrorSource->addItem(tr("Statistical (Poisson)"));
183  boxYErrorSource->addItem(tr("Arbitrary Dataset"));
184  hbox2->addWidget(boxYErrorSource);
185  QGroupBox *gb4 = new QGroupBox();
186  gb4->setLayout(hbox2);
187 
188  tableNamesBox = new QComboBox();
189  tableNamesBox->setEnabled(false);
190  hbox2->addWidget(tableNamesBox);
191  colNamesBox = new QComboBox();
192  colNamesBox->setEnabled(false);
193  hbox2->addWidget(colNamesBox);
194 
195  QHBoxLayout *hbox3 = new QHBoxLayout();
196  buttonEdit = new QPushButton(tr( "<< &Edit function" ) );
197  hbox3->addWidget(buttonEdit);
198  btnDeleteFitCurves = new QPushButton(tr( "&Delete Fit Curves" ));
199  hbox3->addWidget(btnDeleteFitCurves);
200  buttonOk = new QPushButton(tr( "&Fit" ) );
201  buttonOk->setDefault( true );
202  hbox3->addWidget(buttonOk);
203  buttonCancel1 = new QPushButton(tr( "&Close" ));
204  hbox3->addWidget(buttonCancel1);
205  buttonAdvanced = new QPushButton(tr( "Custom &Output >>" ));
206  hbox3->addWidget(buttonAdvanced);
207  hbox3->addStretch();
208 
209  QVBoxLayout *vbox1 = new QVBoxLayout();
210  vbox1->addWidget(gb1);
211  vbox1->addLayout(hbox1);
212  vbox1->addWidget(gb4);
213  vbox1->addLayout(hbox3);
214 
215  fitPage = new QWidget();
216  fitPage->setLayout(vbox1);
217  tw->addWidget(fitPage);
218 
219  connect( boxCurve, SIGNAL( activated(const QString&) ), this, SLOT( activateCurve(const QString&) ) );
220  connect( buttonOk, SIGNAL( clicked() ), this, SLOT(accept()));
221  connect( buttonCancel1, SIGNAL( clicked() ), this, SLOT(close()));
222  connect( buttonEdit, SIGNAL( clicked() ), this, SLOT(showEditPage()));
223  connect( btnDeleteFitCurves, SIGNAL( clicked() ), this, SLOT(deleteFitCurves()));
224  connect( boxYErrorSource, SIGNAL( activated(int) ), this, SLOT( yErrorSourceChanged(int) ) );
225  connect( buttonAdvanced, SIGNAL(clicked()), this, SLOT(showAdvancedPage() ) );
226  connect( tableNamesBox, SIGNAL( activated(int) ), this, SLOT( selectSrcTable(int) ) );
227 
228  setFocusProxy(boxFunction);
229 }
230 
232 {
233  QGridLayout *gl1 = new QGridLayout();
234  gl1->addWidget(new QLabel(tr("Category")), 0, 0);
235  gl1->addWidget(new QLabel(tr("Function")), 0, 1);
236  gl1->addWidget(new QLabel(tr("Expression")), 0, 2);
237 
238  categoryBox = new QListWidget();
239  categoryBox->addItem(tr("User defined"));
240  categoryBox->addItem(tr("Built-in"));
241  categoryBox->addItem(tr("Basic"));
242  categoryBox->addItem(tr("Plugins"));
243 
244  gl1->addWidget(categoryBox, 1, 0);
245  funcBox = new QListWidget();
246  gl1->addWidget(funcBox, 1, 1);
247  explainBox = new QTextEdit();
248  explainBox->setReadOnly(true);
249  gl1->addWidget(explainBox, 1, 2);
250 
251  boxUseBuiltIn = new QCheckBox();
252  boxUseBuiltIn->setText(tr("Fit with &built-in function"));
253  boxUseBuiltIn->hide();
254 
255  QHBoxLayout *hbox1 = new QHBoxLayout();
256  hbox1->addWidget(boxUseBuiltIn);
257  hbox1->addStretch();
258 
259  polynomOrderLabel = new QLabel( tr("Polynomial Order"));
260  polynomOrderLabel->hide();
261  hbox1->addWidget(polynomOrderLabel);
262 
263  polynomOrderBox = new QSpinBox();
264  polynomOrderBox->setMinimum(1);
265  polynomOrderBox->setValue(2);
266  polynomOrderBox->hide();
267  connect(polynomOrderBox, SIGNAL(valueChanged(int)), this, SLOT(showExpression(int)));
268  hbox1->addWidget(polynomOrderBox);
269 
270  buttonPlugins = new QPushButton(tr( "&Choose plugins folder..." ) );
271  hbox1->addWidget(buttonPlugins);
272  buttonPlugins->hide();
273 
274  buttonClearUsrList = new QPushButton(tr( "Clear user &list" ) );
275  hbox1->addWidget(buttonClearUsrList);
276  buttonClearUsrList->hide();
277 
278  QGridLayout *gl2 = new QGridLayout();
279  gl2->addWidget(new QLabel(tr("Name")), 0, 0);
280  boxName = new QLineEdit(tr("user1"));
281  gl2->addWidget(boxName, 0, 1);
282  btnAddFunc = new QPushButton(tr( "&Save" ));
283  gl2->addWidget(btnAddFunc, 0, 2);
284  gl2->addWidget(new QLabel(tr("Parameters")), 1, 0);
285  boxParam = new QLineEdit("a, b");
286  gl2->addWidget(boxParam, 1, 1);
287  btnDelFunc = new QPushButton( tr( "&Remove" ));
288  gl2->addWidget(btnDelFunc, 1, 2);
289 
290  QGroupBox *gb = new QGroupBox();
291  gb->setLayout(gl2);
292 
293  editBox = new QTextEdit();
294  editBox->setAcceptRichText(false);
295  editBox->setFocus();
296 
297  QVBoxLayout *vbox1 = new QVBoxLayout();
298  btnAddTxt = new QPushButton(tr( "Add &expression" ) );
299  vbox1->addWidget(btnAddTxt);
300  btnAddName = new QPushButton(tr( "Add &name" ));
301  vbox1->addWidget(btnAddName);
302  buttonClear = new QPushButton(tr( "Rese&t" ));
303  vbox1->addWidget(buttonClear);
304  buttonCancel2 = new QPushButton(tr( "&Close" ));
305  vbox1->addWidget(buttonCancel2);
306  btnContinue = new QPushButton(tr( "&Fit >>" ));
307  vbox1->addWidget(btnContinue);
308  vbox1->addStretch();
309 
310  QHBoxLayout *hbox2 = new QHBoxLayout();
311  hbox2->addWidget(editBox);
312  hbox2->addLayout(vbox1);
313 
314  QVBoxLayout *vbox2 = new QVBoxLayout();
315  vbox2->addLayout(gl1);
316  vbox2->addLayout(hbox1);
317  vbox2->addWidget(gb);
318  vbox2->addLayout(hbox2);
319 
320  editPage = new QWidget();
321  editPage->setLayout(vbox2);
322  tw->addWidget(editPage);
323 
324  connect( buttonPlugins, SIGNAL( clicked() ), this, SLOT(choosePluginsFolder()));
325  connect( buttonClear, SIGNAL( clicked() ), this, SLOT(resetFunction()));
326  connect( buttonClearUsrList, SIGNAL( clicked() ), this, SLOT(clearUserFunctions()));
327  connect( categoryBox, SIGNAL(currentRowChanged (int)), this, SLOT(showFunctionsList(int) ) );
328  connect( funcBox, SIGNAL(currentRowChanged(int)), this, SLOT(showExpression(int)));
329  connect( boxUseBuiltIn, SIGNAL(toggled(bool)), this, SLOT(setFunction(bool) ) );
330  connect( btnAddName, SIGNAL(clicked()), this, SLOT(pasteFunctionName() ) );
331  connect( btnAddTxt, SIGNAL(clicked()), this, SLOT(pasteExpression() ) );
332  connect( btnContinue, SIGNAL(clicked()), this, SLOT(showFitPage() ) );
333  connect( btnAddFunc, SIGNAL(clicked()), this, SLOT(saveUserFunction()));
334  connect( btnDelFunc, SIGNAL(clicked()), this, SLOT(removeUserFunction()));
335  connect( buttonCancel2, SIGNAL(clicked()), this, SLOT(close()) );
336 }
337 
338 
340 {
341  ApplicationWindow *app = (ApplicationWindow *)this->parent();
342 
343  generatePointsBtn = new QRadioButton (tr("&Uniform X Function"));
345  connect( generatePointsBtn, SIGNAL(clicked()), this, SLOT(enableApplyChanges()));
346 
347 
348  QGridLayout *gl1 = new QGridLayout();
349  gl1->addWidget(generatePointsBtn, 0, 0);
350 
351  lblPoints = new QLabel( tr("Points"));
352 
353  generatePointsBox = new QSpinBox ();
354  generatePointsBox->setRange(0, 1000000);
355  generatePointsBox->setSingleStep(10);
356  generatePointsBox->setValue(app->fitPoints);
357  connect( generatePointsBox, SIGNAL(valueChanged(int)), this, SLOT(enableApplyChanges(int)));
359 
360  QHBoxLayout *hb = new QHBoxLayout();
361  hb->addStretch();
362  hb->addWidget(lblPoints);
363  hb->addWidget(generatePointsBox);
364  gl1->addLayout(hb, 0, 1);
365 
366  samePointsBtn = new QRadioButton(tr( "Same X as Fitting &Data" ));
367  gl1->addWidget(samePointsBtn, 1, 0);
368  samePointsBtn->setChecked(!app->generateUniformFitPoints);
369  connect( samePointsBtn, SIGNAL(clicked()), this, SLOT(enableApplyChanges()));
370 
371  QGroupBox *gb1 = new QGroupBox(tr("Generated Fit Curve"));
372  gb1->setLayout(gl1);
373 
374  QGridLayout *gl2 = new QGridLayout();
375  gl2->addWidget(new QLabel( tr("Significant Digits")), 0, 1);
376  boxPrecision = new QSpinBox ();
377  boxPrecision->setRange(0, 15);
378  boxPrecision->setValue (app->fit_output_precision);
379  connect( boxPrecision, SIGNAL(valueChanged (int)), this, SLOT(enableApplyChanges(int)));
380  gl2->addWidget(boxPrecision, 0, 2);
381  btnParamTable = new QPushButton(tr( "Parameters &Table" ));
382  gl2->addWidget(btnParamTable, 1, 0);
383  gl2->addWidget(new QLabel( tr("Name: ")), 1, 1);
384  paramTableName = new QLineEdit(tr( "Parameters" ));
385  gl2->addWidget(paramTableName, 1, 2);
386  btnCovMatrix = new QPushButton(tr( "Covariance &Matrix" ));
387  gl2->addWidget(btnCovMatrix, 2, 0);
388  gl2->addWidget(new QLabel( tr("Name: ")), 2, 1);
389  covMatrixName = new QLineEdit( tr( "CovMatrix" ) );
390  gl2->addWidget(covMatrixName, 2, 2);
391 
392  scaleErrorsBox = new QCheckBox(tr("Scale Errors with sqrt(Chi^2/doF)"));
393  scaleErrorsBox->setChecked(app->fit_scale_errors);
394  connect( scaleErrorsBox, SIGNAL(stateChanged (int)), this, SLOT(enableApplyChanges(int)));
395 
396  QGroupBox *gb2 = new QGroupBox(tr("Parameters Output"));
397  gb2->setLayout(gl2);
398 
399  logBox = new QCheckBox (tr("&Write Parameters to Result Log"));
400  logBox->setChecked(app->writeFitResultsToLog);
401  connect( logBox, SIGNAL(stateChanged(int)), this, SLOT(enableApplyChanges(int)));
402 
403  plotLabelBox = new QCheckBox (tr("&Paste Parameters to Plot"));
404  plotLabelBox->setChecked(app->pasteFitResultsToPlot);
405  connect( plotLabelBox, SIGNAL(stateChanged (int)), this, SLOT(enableApplyChanges(int)));
406 
407  QHBoxLayout *hbox1 = new QHBoxLayout();
408 
409  btnBack = new QPushButton(tr( "<< &Fit" ));
410  connect( btnBack, SIGNAL(clicked()), this, SLOT(showFitPage()));
411  connect( btnBack, SIGNAL(clicked()), this, SLOT(applyChanges()));
412  hbox1->addWidget(btnBack);
413 
414  btnApply = new QPushButton(tr( "&Apply" ));
415  btnApply->setEnabled(false);
416  connect( btnApply, SIGNAL(clicked()), this, SLOT(applyChanges()));
417  hbox1->addWidget(btnApply);
418 
419  buttonCancel3 = new QPushButton(tr( "&Close" ));
420  hbox1->addWidget(buttonCancel3);
421  hbox1->addStretch();
422 
423  QVBoxLayout *vbox1 = new QVBoxLayout();
424  vbox1->addWidget(gb1);
425  vbox1->addWidget(gb2);
426  vbox1->addWidget(scaleErrorsBox);
427  vbox1->addWidget(logBox);
428  vbox1->addWidget(plotLabelBox);
429  vbox1->addStretch();
430  vbox1->addLayout(hbox1);
431 
432  advancedPage = new QWidget();
433  advancedPage->setLayout(vbox1);
434  tw->addWidget(advancedPage);
435 
436  connect(btnParamTable, SIGNAL(clicked()), this, SLOT(showParametersTable()));
437  connect(btnCovMatrix, SIGNAL(clicked()), this, SLOT(showCovarianceMatrix()));
438  connect(samePointsBtn, SIGNAL(toggled(bool)), this, SLOT(showPointsBox(bool)));
439  connect(generatePointsBtn, SIGNAL(toggled(bool)), this, SLOT(showPointsBox(bool)));
440  connect(buttonCancel3, SIGNAL(clicked()), this, SLOT(close()));
441 }
442 
444 {
445  ApplicationWindow *app = (ApplicationWindow *)this->parent();
446  app->fit_output_precision = boxPrecision->value();
447  app->pasteFitResultsToPlot = plotLabelBox->isChecked();
448  app->writeFitResultsToLog = logBox->isChecked();
449  app->fitPoints = generatePointsBox->value();
450  app->generateUniformFitPoints = generatePointsBtn->isChecked();
451  app->fit_scale_errors = scaleErrorsBox->isChecked();
452  app->saveSettings();
453  btnApply->setEnabled(false);
454 }
455 
457 {
458  QString tableName = paramTableName->text();
459  if (tableName.isEmpty())
460  {
461  QMessageBox::critical(this, tr("Error"),
462  tr("Please enter a valid name for the parameters table."));
463  return;
464  }
465 
466  if (!d_fitter)
467  {
468  QMessageBox::critical(this, tr("Error"),
469  tr("Please perform a fit first and try again."));
470  return;
471  }
472 
473  ApplicationWindow *app = (ApplicationWindow *)this->parent();
474  tableName = app->generateUniqueName(tableName, false);
475  d_fitter->parametersTable(tableName);
476 }
477 
479 {
480  QString matrixName = covMatrixName->text();
481  if (matrixName.isEmpty())
482  {
483  QMessageBox::critical(this, tr("Error"),
484  tr("Please enter a valid name for the covariance matrix."));
485  return;
486  }
487 
488  if (!d_fitter)
489  {
490  QMessageBox::critical(this, tr("Error"),
491  tr("Please perform a fit first and try again."));
492  return;
493  }
494 
495  ApplicationWindow *app = (ApplicationWindow *)this->parent();
496  matrixName = app->generateUniqueName(matrixName, false);
497  d_fitter->covarianceMatrix(matrixName);
498 }
499 
501 {
502  if (generatePointsBtn->isChecked())
503  {
504  lblPoints->show();
505  generatePointsBox->show();
506  }
507  else
508  {
509  lblPoints->hide();
510  generatePointsBox->hide();
511  }
512 }
513 
515 {
516  if (!g)
517  return;
518 
519  d_graph = g;
520  boxCurve->clear();
521  boxCurve->addItems(d_graph->analysableCurvesList());
522 
523  QString selectedCurve = g->selectedCurveTitle();
524  if (!selectedCurve.isEmpty())
525  {
526  int index = boxCurve->findText (selectedCurve);
527  boxCurve->setCurrentIndex(index);
528  }
529  activateCurve(boxCurve->currentText());
530 
531  connect (d_graph, SIGNAL(closedGraph()), this, SLOT(close()));
532  connect (d_graph, SIGNAL(dataRangeChanged()), this, SLOT(changeDataRange()));
533 };
534 
535 void FitDialog::activateCurve(const QString& curveName)
536 {
537  QwtPlotCurve *c = d_graph->curve(curveName);
538  if (!c)
539  return;
540 
541  double start, end;
542  d_graph->range(d_graph->curveIndex(curveName), &start, &end);
543  boxFrom->setText(QLocale().toString(qMin(start, end), 'g', 15));
544  boxTo->setText(QLocale().toString(qMax(start, end), 'g', 15));
545 };
546 
548 {
549  if (editBox->toPlainText().isEmpty())
550  {
551  QMessageBox::critical(this, tr("Input function error"), tr("Please enter a valid function!"));
552  editBox->setFocus();
553  return;
554  }
555  else if (boxName->text().isEmpty())
556  {
557  QMessageBox::critical(this, tr("Input function error"),
558  tr("Please enter a function name!"));
559  boxName->setFocus();
560  return;
561  }
562  else if (boxParam->text().remove(QRegExp("[,;\\s]")).isEmpty())
563  {
564  QMessageBox::critical(this, tr("Input function error"),
565  tr("Please enter at least one parameter name!"));
566  boxParam->setFocus();
567  return;
568  }
569 
570  if (d_built_in_function_names.contains(boxName->text()))
571  {
572  QMessageBox::critical(this, tr("Error: function name"),
573  "<p><b>" + boxName->text() + "</b>" + tr(" is a built-in function name"
574  "<p>You must choose another name for your function!"));
575  editBox->setFocus();
576  return;
577  }
578  if (editBox->toPlainText().contains(boxName->text()))
579  {
580  QMessageBox::critical(this, tr("Input function error"),
581  tr("You can't define functions recursevely!"));
582  editBox->setFocus();
583  return;
584  }
585 
586  QString name = boxName->text();
587  QString f = name + "(x, " + boxParam->text() + ")=" + editBox->toPlainText().remove("\n");
588 
589  if (d_user_function_names.contains(name))
590  {
591  int index = d_user_function_names.indexOf(name);
592  d_user_functions[index] = f;
593  d_user_function_params[index] = boxParam->text();
594 
595  if (funcBox->currentItem()->text() == name)
596  showExpression(index);
597  }
598  else
599  {
601  d_user_functions << f;
602  d_user_function_params << boxParam->text();
603 
604  if (categoryBox->currentRow() == 0)
605  {
606  funcBox->addItem(name);
607  funcBox->setCurrentRow (funcBox->count()-1);
608  }
609 
610  if (d_user_function_names.count()>0 && !boxUseBuiltIn->isEnabled() && categoryBox->currentRow() == 0)
611  boxUseBuiltIn->setEnabled(true);
612  }
613  buttonClearUsrList->setEnabled(true);
615 }
616 
618 {
619  if (!funcBox->currentItem())
620  return;
621  QString name = funcBox->currentItem()->text();
622  if (d_user_function_names.contains(name))
623  {
624  explainBox->setText(QString());
625 
626  int index = d_user_function_names.indexOf(name);
627  d_user_function_names.removeAll(name);
628 
629  QString f = d_user_functions[index];
630  d_user_functions.removeAll(f);
631 
632  f = d_user_function_params[index];
633  d_user_function_params.removeAll(f);
634 
635  funcBox->clear();
636  funcBox->addItems (d_user_function_names);
637  funcBox->setCurrentRow (0);
638 
639  if (!d_user_function_names.count())
640  {
641  boxUseBuiltIn->setEnabled(false);
642  buttonClearUsrList->setEnabled(false);
643  }
644 
646  }
647 }
648 
650 {
651  int param_table_rows = boxParams->rowCount();
652 
653  QString par = boxParam->text().simplified();
654  QStringList paramList = par.split(QRegExp("[,;]+[\\s]*"), QString::SkipEmptyParts);
655  int parameters = paramList.count();
656  boxParams->setRowCount(parameters);
657  boxParams->hideColumn(2);
658 
659  if (parameters > 7)
660  parameters = 7;
661  boxParams->setMinimumHeight(4+(parameters+1)*boxParams->horizontalHeader()->height());
662 
663  for (int i = param_table_rows; i<paramList.count(); i++)
664  {
665  QTableWidgetItem *it = new QTableWidgetItem(paramList[i]);
666  it->setFlags(Qt::ItemFlags(Qt::NoItemFlags));
667  it->setBackground(QBrush(Qt::lightGray));
668  it->setForeground(QBrush(Qt::darkRed));
669  QFont font = it->font();
670  font.setBold(true);
671  it->setFont(font);
672  boxParams->setItem(i, 0, it);
673 
674  it = new QTableWidgetItem(QLocale().toString(1.0, 'f', boxPrecision->value()));
675  it->setTextAlignment(Qt::AlignRight);
676  boxParams->setItem(i, 1, it);
677  }
678  for (int i = 0; i<paramList.count(); i++)
679  boxParams->item (i, 0)->setText(paramList[i]);
680 
681  // FIXME: this check is pretty ugly, should be changed to a more elegant way some time
682  if (!boxUseBuiltIn->isChecked() ||
683  (boxUseBuiltIn->isChecked()&& categoryBox->currentRow()!=3 && categoryBox->currentRow()!=1))
684  {
685  boxParams->showColumn(2);
686 
687  for (int i = 0; i<boxParams->rowCount(); i++ )
688  {
689  QTableWidgetItem *it = new QTableWidgetItem();
690  it->setFlags(Qt::ItemFlags(Qt::NoItemFlags));
691  it->setBackground(QBrush(Qt::lightGray));
692  boxParams->setItem(i, 2, it);
693 
694  QCheckBox *cb = new QCheckBox();
695  boxParams->setCellWidget(i, 2, cb);
696  }
697  }
698 
699  boxFunction->setText(editBox->toPlainText().simplified());
700  lblFunction->setText(boxName->text() +" (x, " + par + ")");
701 
702  tw->setCurrentWidget (fitPage);
703 }
704 
706 {
707  tw->setCurrentWidget (editPage);
708 }
709 
711 {
712  tw->setCurrentWidget (advancedPage);
713 }
714 
716 {
717  editBox->setEnabled(!ok);
718  boxParam->setEnabled(!ok);
719  boxName->setEnabled(!ok);
720  btnAddFunc->setEnabled(!ok);
721  btnAddName->setEnabled(!ok);
722  btnAddTxt->setEnabled(!ok);
723  buttonClear->setEnabled(!ok);
724 
725  if (ok)
726  {
727  boxName->setText(funcBox->currentItem()->text());
728  editBox->setText(explainBox->toPlainText());
729 
730  if (categoryBox->currentRow() == 0 && d_user_function_params.size() > 0)
731  boxParam->setText(d_user_function_params[funcBox->currentRow ()]);
732  else if (categoryBox->currentRow() == 1)
733  {
734  QStringList lst;
735  switch(funcBox->currentRow ())
736  {
737  case 0:
738  lst << "A1" << "A2" << "x0" << "dx";
739  break;
740  case 1:
741  lst << "A" << "t" << "y0";
742  break;
743  case 2:
744  lst << "A" << "t" << "y0";
745  break;
746  case 3:
747  lst << "A1" << "t1" << "A2" << "t2" << "y0";
748  break;
749  case 4:
750  lst << "A1" << "t1" << "A2" << "t2" << "A3" << "t3" << "y0";
751  break;
752  case 5:
753  lst << "y0" << "A" << "xc" << "w";
754  break;
755  case 6:
757  break;
758  case 7:
760  break;
761  case 8:
763  break;
764  }
765  boxParam->setText(lst.join(", "));
766  }
767  else if (categoryBox->currentRow() == 3 && d_plugin_params.size() > 0 )
768  boxParam->setText(d_plugin_params[funcBox->currentRow()]);
769  }
770 }
771 
773 {
774  d_user_functions.clear();
775  d_user_function_names.clear();
776  if (categoryBox->currentRow() == 0)
777  {
778  funcBox->clear();
779  explainBox->clear();
780  boxUseBuiltIn->setEnabled(false);
781  buttonClearUsrList->setEnabled(false);
782  }
783  emit clearFunctionsList();
784 }
785 
786 void FitDialog::addUserFunctions(const QStringList& list)
787 {
788  if (!list.count())
789  {
790  boxUseBuiltIn->setEnabled(false);
791  return;
792  }
793 
794  for (int i = 0; i<list.count(); i++)
795  {
796  QString s = list.at(i).simplified();
797  if (!s.isEmpty())
798  {
799  d_user_functions << s;
800 
801  int pos1 = s.indexOf("(", 0);
802  d_user_function_names << s.left(pos1);
803 
804  int pos2 = s.indexOf(")", pos1);
805  d_user_function_params << s.mid(pos1+4, pos2-pos1-4);
806  }
807  }
808 }
809 
811 {
812  boxUseBuiltIn->setChecked(false);
813  boxUseBuiltIn->setEnabled(false);
814  boxUseBuiltIn->hide();
815  buttonPlugins->hide();
816  buttonClearUsrList->hide();
817  buttonClearUsrList->setEnabled(false);
818  btnDelFunc->setEnabled(false);
819  funcBox->blockSignals(true);
820  funcBox->clear();
821  explainBox->clear();
822  polynomOrderLabel->hide();
823  polynomOrderBox->hide();
824 
825  switch (category)
826  {
827  case 0:
828  if (d_user_function_names.count() > 0)
829  {
831  buttonClearUsrList->show();
832  boxUseBuiltIn->setEnabled(true);
833  buttonClearUsrList->setEnabled(true);
834  }
835 
836  boxUseBuiltIn->setText(tr("Fit with selected &user function"));
837  boxUseBuiltIn->show();
838  buttonClearUsrList->show();
839  btnDelFunc->setEnabled(true);
840  break;
841 
842  case 1:
843  boxUseBuiltIn->setText(tr("Fit using &built-in function"));
844  boxUseBuiltIn->show();
845  boxUseBuiltIn->setEnabled(true);
847  break;
848 
849  case 2:
851  break;
852 
853  case 3:
854  buttonPlugins->show();
855  boxUseBuiltIn->setText(tr("Fit using &plugin function"));
856  boxUseBuiltIn->show();
857  if (d_plugin_function_names.size() > 0)
858  {
859  funcBox->addItems(d_plugin_function_names);
860  boxUseBuiltIn->setEnabled(true);
861  }
862  break;
863  }
864  funcBox->blockSignals(false);
865  funcBox->setCurrentRow (0);
866 }
867 
869 {
870  ApplicationWindow *app = (ApplicationWindow *)this->parent();
871  QString dir = QFileDialog::getExistingDirectory(this, tr("Choose the plugins folder"),
872  QDir::currentPath(), QFileDialog::ShowDirsOnly);
873  if (!dir.isEmpty())
874  {
875  d_plugin_files_list.clear();
876  d_plugin_function_names.clear();
877  d_plugin_functions.clear();
878  d_plugin_params.clear();
879  funcBox->clear();
880  explainBox->clear();
881 
882  app->fitPluginsPath = dir;
883  loadPlugins();
884  if (d_plugin_function_names.size() > 0)
885  {
886  funcBox->addItems(d_plugin_function_names);
887  if (!boxUseBuiltIn->isEnabled())
888  boxUseBuiltIn->setEnabled(true);
889 
890  funcBox->setCurrentRow(0);
891  }
892  else
893  boxUseBuiltIn->setEnabled(false);
894  }
895 }
896 
898 {
899  typedef char* (*fitFunc)();
900 
901  ApplicationWindow *app = (ApplicationWindow *)this->parent();
902  QString path = app->fitPluginsPath + "/";
903  QDir dir(path);
904  QStringList lst = dir.entryList(QDir::Files|QDir::NoSymLinks);
905 
906  for (int i=0; i<lst.count(); i++)
907  {
908  QLibrary lib(path + lst[i]);
909 
910  fitFunc name = (fitFunc) lib.resolve( "name" );
911  fitFunc function = (fitFunc) lib.resolve("function");
912  fitFunc params = (fitFunc) lib.resolve("parameters");
913 
914  if ( name && function && params )
915  {
916  d_plugin_function_names << QString(name());
917  d_plugin_functions << QString(function());
918  d_plugin_params << QString(params());
919  d_plugin_files_list << lib.fileName();
920  }
921  }
922 }
923 
925 {
926  funcBox->addItems(d_user_function_names);
927 }
928 
930 {
931  d_built_in_function_names << "Boltzmann" << "ExpGrowth" << "ExpDecay1" << "ExpDecay2" << "ExpDecay3"
932  << "GaussAmp" << "Gauss" << "Lorentz" << "Polynomial";
933 }
934 
936 {
937  d_built_in_functions << "(A1-A2)/(1+exp((x-x0)/dx))+A2";
938  d_built_in_functions << "y0+A*exp(x/t)";
939  d_built_in_functions << "y0+A*exp(-x/t)";
940  d_built_in_functions << "y0+A1*exp(-x/t1)+A2*exp(-x/t2)";
941  d_built_in_functions << "y0+A1*exp(-x/t1)+A2*exp(-x/t2)+A3*exp(-x/t3)";
942  d_built_in_functions << "y0+A*exp(-(x-xc)*(x-xc)/(2*w*w))";
943 }
944 
946 {
947  funcBox->addItems(MyParser::functionsList());
948 }
949 
950 void FitDialog::showExpression(int function)
951 {
952  if (function < 0)
953  return;
954 
955  if (categoryBox->currentRow() == 2)
956  {
957  explainBox->setText(MyParser::explainFunction(function));
958  }
959  else if (categoryBox->currentRow() == 1)
960  {
961  polynomOrderLabel->show();
962  polynomOrderBox->show();
963 
964  switch(funcBox->currentRow ()) {
965  case 6: // Gauss
966  polynomOrderLabel->setText(tr("Peaks"));
968  break;
969  case 7: // Lorentz
970  polynomOrderLabel->setText(tr("Peaks"));
972  break;
973  case 8: // Polynomial
974  polynomOrderLabel->setText(tr("Polynomial Order"));
976  break;
977  default:
978  polynomOrderLabel->hide();
979  polynomOrderBox->hide();
980  polynomOrderBox->setValue(1);
981  explainBox->setText(d_built_in_functions[function]);
982  }
983  setFunction(boxUseBuiltIn->isChecked());
984  }
985  else if (categoryBox->currentRow() == 0)
986  {
987  if (d_user_functions.size() > function) {
988  QStringList l = d_user_functions[function].split("=");
989  explainBox->setText(l[1]);
990  } else
991  explainBox->clear();
992  setFunction(boxUseBuiltIn->isChecked());
993  }
994  else if (categoryBox->currentRow() == 3)
995  {
996  if (d_plugin_functions.size() > 0)
997  {
998  explainBox->setText(d_plugin_functions[function]);
999  setFunction(boxUseBuiltIn->isChecked());
1000  }
1001  else
1002  explainBox->clear();
1003  }
1004 }
1005 
1007 {
1008  QString f = explainBox->toPlainText();
1009  if (categoryBox->currentRow() == 2)
1010  {//basic parser function
1011  f = f.left(f.indexOf("(", 0)+1);
1012  if (editBox->textCursor().hasSelection())
1013  {
1014  QString markedText=editBox->textCursor().selectedText();
1015  editBox->insertPlainText(f+markedText+")");
1016  }
1017  else
1018  editBox->insertPlainText(f+")");
1019  }
1020  else
1021  editBox->insertPlainText(f);
1022 
1023  editBox->setFocus();
1024 }
1025 
1027 {
1028  if (!funcBox->currentItem())
1029  return;
1030  editBox->insertPlainText(funcBox->currentItem()->text());
1031  editBox->setFocus();
1032 }
1033 
1035 {
1036  QString curve = boxCurve->currentText();
1037  QStringList curvesList = d_graph->curvesList();
1038  if (curvesList.contains(curve) <= 0)
1039  {
1040  QMessageBox::critical(this,tr("Warning"),
1041  tr("The curve <b> %1 </b> doesn't exist anymore! Operation aborted!").arg(curve));
1042  boxCurve->clear();
1043  boxCurve->addItems(curvesList);
1044  return;
1045  }
1046 
1047  if (!validInitialValues())
1048  return;
1049 
1050  QString from=boxFrom->text().toLower();
1051  QString to=boxTo->text().toLower();
1052  QString tolerance=boxTolerance->text().toLower();
1053  double start, end, eps;
1054  try
1055  {
1056  MyParser parser;
1057  parser.SetExpr(CONFS(from));
1058  start=parser.Eval();
1059  }
1060  catch(mu::ParserError &e)
1061  {
1062  QMessageBox::critical(this, tr("Start limit error"),QStringFromString(e.GetMsg()));
1063  boxFrom->setFocus();
1064  return;
1065  }
1066 
1067  try
1068  {
1069  MyParser parser;
1070  parser.SetExpr(CONFS(to));
1071  end=parser.Eval();
1072  }
1073  catch(mu::ParserError &e)
1074  {
1075  QMessageBox::critical(this, tr("End limit error"),QStringFromString(e.GetMsg()));
1076  boxTo->setFocus();
1077  return;
1078  }
1079 
1080  if (start>=end)
1081  {
1082  QMessageBox::critical(0, tr("Input error"),
1083  tr("Please enter x limits that satisfy: from < end!"));
1084  boxTo->setFocus();
1085  return;
1086  }
1087 
1088  try
1089  {
1090  MyParser parser;
1091  parser.SetExpr(CONFS(tolerance));
1092  eps=parser.Eval();
1093  }
1094  catch(mu::ParserError &e)
1095  {
1096  QMessageBox::critical(0, tr("Tolerance input error"),QStringFromString(e.GetMsg()));
1097  boxTolerance->setFocus();
1098  return;
1099  }
1100 
1101  if (eps<0 || eps>=1)
1102  {
1103  QMessageBox::critical(0, tr("Tolerance input error"),
1104  tr("The tolerance value must be positive and less than 1!"));
1105  boxTolerance->setFocus();
1106  return;
1107  }
1108 
1109  int i, n=0, rows=boxParams->rowCount();
1110  if (!boxParams->isColumnHidden(2))
1111  {
1112  for (i=0;i<rows;i++)
1113  {//count the non-constant parameters
1114  QCheckBox *cb = (QCheckBox*)boxParams->cellWidget(i, 2);
1115  if (!cb->isChecked())
1116  n++;
1117  }
1118  }
1119  else
1120  n=rows;
1121 
1122  QStringList parameters;
1123  double *paramsInit = new double[n];
1124  QString formula;
1125 
1126  // recursively define variables for user functions used in formula
1127  bool found_uf;
1128  do {
1129  found_uf = false;
1130  for (i=0; i<d_user_function_names.count(); i++)
1131  if (boxFunction->toPlainText().contains(d_user_function_names[i])) {
1132  QStringList l = d_user_functions[i].split("=");
1133  formula += QString("%1=%2\n")
1134  .arg(d_user_function_names[i])
1135  .arg(l[1]);
1136  found_uf = true;
1137  }
1138  } while (found_uf);
1139  formula += boxFunction->toPlainText();
1140 
1141  // define variables for builtin functions used in formula
1142  for (i=0; i<d_built_in_function_names.count(); i++)
1143  if (formula.contains(d_built_in_function_names[i]))
1144  formula.prepend(QString("%1=%2\n")
1145  .arg(d_built_in_function_names[i])
1146  .arg(d_built_in_functions[i]));
1147 
1148 
1149  if (!boxParams->isColumnHidden(2))
1150  {
1151  int j = 0;
1152  for (i=0;i<rows;i++)
1153  {
1154  QCheckBox *cb = (QCheckBox*)boxParams->cellWidget(i, 2);
1155  if (!cb->isChecked())
1156  {
1157  paramsInit[j] = QLocale().toDouble(boxParams->item(i,1)->text());
1158  parameters << boxParams->item(i,0)->text();
1159  j++;
1160  }
1161  else
1162  formula.prepend(QString("%1=%2\n")
1163  .arg(boxParams->item(i,0)->text())
1164  .arg(CONFS(boxParams->item(i,1)->text())));
1165  }
1166  }
1167  else
1168  {
1169  for (i=0;i<n;i++)
1170  {
1171  paramsInit[i] = QLocale().toDouble(boxParams->item(i,1)->text());
1172  parameters << boxParams->item(i,0)->text();
1173  }
1174  }
1175 
1176  ApplicationWindow *app = (ApplicationWindow *)this->parent();
1177 
1178  if (d_fitter)
1179  {
1180  delete d_fitter;
1181  d_fitter = 0;
1182  }
1183 
1184  if (boxUseBuiltIn->isChecked() && categoryBox->currentRow() == 1)
1185  fitBuiltInFunction(funcBox->currentItem()->text(), paramsInit);
1186  else if (boxUseBuiltIn->isChecked() && categoryBox->currentRow() == 3)
1187  {
1188  d_fitter = new PluginFit(app, d_graph);
1189  if (!((PluginFit*)d_fitter)->load(d_plugin_files_list[funcBox->currentRow()])){
1190  d_fitter = 0;
1191  return;}
1192  d_fitter->setInitialGuesses(paramsInit);
1193  }
1194  else
1195  {
1196  d_fitter = new NonLinearFit(app, d_graph);
1197  ((NonLinearFit*)d_fitter)->setParametersList(parameters);
1198  ((NonLinearFit*)d_fitter)->setFormula(formula);
1199  d_fitter->setInitialGuesses(paramsInit);
1200  }
1201  delete[] paramsInit;
1202 
1203  if (!d_fitter->setDataFromCurve(curve, start, end) ||
1205  tableNamesBox->currentText()+"_"+colNamesBox->currentText()))
1206  {
1207  delete d_fitter;
1208  d_fitter = 0;
1209  return;
1210  }
1211 
1212  d_fitter->setTolerance (eps);
1213  d_fitter->setAlgorithm((Fit::Algorithm)boxAlgorithm->currentIndex());
1217  d_fitter->scaleErrors(scaleErrorsBox->isChecked());
1218 
1219  if (d_fitter->objectName() == tr("MultiPeak") && ((MultiPeakFit *)d_fitter)->peaks() > 1)
1220  {
1221  ((MultiPeakFit *)d_fitter)->enablePeakCurves(app->generatePeakCurves);
1222  ((MultiPeakFit *)d_fitter)->setPeakCurvesColor(app->peakCurvesColor);
1223  }
1224 
1225  d_fitter->fit();
1226  auto res = d_fitter->results();
1227  if (!boxParams->isColumnHidden(2))
1228  {
1229  int j = 0;
1230  for (i=0;i<rows;i++)
1231  {
1232  QCheckBox *cb = (QCheckBox*)boxParams->cellWidget(i, 2);
1233  if (!cb->isChecked())
1234  boxParams->item(i, 1)->setText(QLocale().toString(res[j++], 'g', boxPrecision->value()));
1235  }
1236  }
1237  else
1238  {
1239  for (i=0;i<rows;i++)
1240  boxParams->item(i, 1)->setText(QLocale().toString(res[i], 'g', boxPrecision->value()));
1241  }
1242 }
1243 
1244 void FitDialog::fitBuiltInFunction(const QString& function, double* initVal)
1245 {
1246  ApplicationWindow *app = (ApplicationWindow *)this->parent();
1247  if (function == "ExpDecay1")
1248  {
1249  initVal[1] = 1/initVal[1];
1250  d_fitter = new ExponentialFit(app, d_graph);
1251  }
1252  else if (function == "ExpGrowth")
1253  {
1254  initVal[1] = -1/initVal[1];
1255  d_fitter = new ExponentialFit(app, d_graph, true);
1256  }
1257  else if (function == "ExpDecay2")
1258  {
1259  initVal[1] = 1/initVal[1];
1260  initVal[3] = 1/initVal[3];
1261  d_fitter = new TwoExpFit(app, d_graph);
1262  }
1263  else if (function == "ExpDecay3")
1264  {
1265  initVal[1] = 1/initVal[1];
1266  initVal[3] = 1/initVal[3];
1267  initVal[5] = 1/initVal[5];
1268  d_fitter = new ThreeExpFit(app, d_graph);
1269  }
1270  else if (function == "Boltzmann")
1271  d_fitter = new SigmoidalFit(app, d_graph);
1272  else if (function == "GaussAmp")
1273  d_fitter = new GaussAmpFit(app, d_graph);
1274  else if (function == "Gauss")
1276  else if (function == "Lorentz")
1278  else if (function == "Polynomial")
1279  d_fitter = new PolynomialFit(app, d_graph, polynomOrderBox->value());
1280 
1281  if (function != "Polynomial")
1282  d_fitter->setInitialGuesses(initVal);
1283 }
1284 
1285 bool FitDialog::containsUserFunctionName(const QString& function)
1286 {
1287  foreach(QString fn, d_user_function_names)
1288  if (!fn.isEmpty() && function.contains(fn))
1289  return true;
1290 
1291  return false;
1292 }
1293 
1295 {
1296  for (int i=0; i<boxParams->rowCount(); i++)
1297  {
1298  if(boxParams->item(i,1)->text().isEmpty())
1299  {
1300  QMessageBox::critical(0, tr("Input error"),
1301  tr("Please enter initial guesses for your parameters!"));
1302  boxParams->setCurrentCell (i,1);
1303  return false;
1304  }
1305 
1306  try
1307  {
1308  MyParser parser;
1309  parser.SetExpr(CONFS(boxParams->item(i,1)->text()));
1310  parser.Eval();
1311  }
1312  catch (mu::ParserError &e)
1313  {
1314  QMessageBox::critical(0, tr("Start limit error"), QStringFromString(e.GetMsg()));
1315  boxParams->setCurrentCell (i,1);
1316  return false;
1317  }
1318  }
1319  return true;
1320 }
1321 
1323 {
1324  double start = d_graph->selectedXStartValue();
1325  double end = d_graph->selectedXEndValue();
1326  boxFrom->setText(QString::number(qMin(start, end), 'g', 15));
1327  boxTo->setText(QString::number(qMax(start, end), 'g', 15));
1328 }
1329 
1330 void FitDialog::setSrcTables(QList<MyWidget*>* tables)
1331 {
1332  if (tables->isEmpty())
1333  {
1334  tableNamesBox->addItem(tr("No data tables"));
1335  colNamesBox->addItem(tr("No data tables"));
1336  return;
1337  }
1338 
1339  d_src_table = tables;
1340  tableNamesBox->clear();
1341  foreach(QWidget *i, *d_src_table)
1342  tableNamesBox->addItem(i->objectName());
1343 
1344  tableNamesBox->setCurrentIndex(tableNamesBox->findText(boxCurve->currentText().split("_", QString::SkipEmptyParts)[0]));
1345  selectSrcTable(tableNamesBox->currentIndex());
1346 }
1347 
1349 {
1350  colNamesBox->clear();
1351 
1352  if (tabnr >= 0 && tabnr < d_src_table->count())
1353  {
1354  Table *t = (Table*)d_src_table->at(tabnr);
1355  if (t)
1356  colNamesBox->addItems(t->colNames());
1357  }
1358 }
1359 
1361 {
1362  if (index == Fit::CustomErrors)
1363  {
1364  tableNamesBox->setEnabled(true);
1365  colNamesBox->setEnabled(true);
1366  }
1367  else
1368  {
1369  tableNamesBox->setEnabled(false);
1370  colNamesBox->setEnabled(false);
1371  }
1372 }
1373 
1374 void FitDialog::closeEvent (QCloseEvent * e )
1375 {
1376  if(d_fitter && plotLabelBox->isChecked())
1377  d_fitter->showLegend();
1378 
1379  e->accept();
1380 }
1381 
1383 {
1384  btnApply->setEnabled(true);
1385 }
1386 
1388 {
1390  boxCurve->clear();
1391  boxCurve->addItems(d_graph->curvesList());
1392 }
1393 
1395 {
1396  boxName->clear();
1397  boxParam->clear();
1398  editBox->clear();
1399 }
FitDialog::boxTolerance
QLineEdit * boxTolerance
Definition: FitDialog.h:173
FitDialog::validInitialValues
bool validInitialValues()
Check the validity of the initial values.
Definition: FitDialog.cpp:1294
Fit.h
FitDialog::showFitPage
void showFitPage()
Show the fit page.
Definition: FitDialog.cpp:649
MultiPeakFit::generateFormula
static QString generateFormula(int order, PeakProfile profile)
Definition: MultiPeakFit.cpp:131
FitDialog::buttonOk
QPushButton * buttonOk
Definition: FitDialog.h:159
FitDialog::editBox
QTextEdit * editBox
Definition: FitDialog.h:176
FitDialog::buttonCancel2
QPushButton * buttonCancel2
Definition: FitDialog.h:161
MyParser.h
FitDialog::boxPoints
QSpinBox * boxPoints
Definition: FitDialog.h:174
FitDialog::boxFrom
QLineEdit * boxFrom
Definition: FitDialog.h:171
FitDialog::showCovarianceMatrix
void showCovarianceMatrix()
Display the covariance matrix.
Definition: FitDialog.cpp:478
FitDialog::generatePointsBtn
QRadioButton * generatePointsBtn
Definition: FitDialog.h:184
FitDialog::plotLabelBox
QCheckBox * plotLabelBox
Definition: FitDialog.h:187
PolynomialFit.h
Filter::setColor
void setColor(QColor colorId)
Sets the color of the output fit curve.
Definition: Filter.h:64
ApplicationWindow::fitPluginsPath
QString fitPluginsPath
Definition: ApplicationWindow.h:992
FitDialog::advancedPage
QWidget * advancedPage
Definition: FitDialog.h:175
FitDialog::buttonClear
QPushButton * buttonClear
Definition: FitDialog.h:164
MultiPeakFit::Lorentz
@ Lorentz
Definition: MultiPeakFit.h:40
NonLinearFit
Definition: NonLinearFit.h:35
Graph::deleteFitCurves
void deleteFitCurves()
Definition: Graph.cpp:5050
TwoExpFit
Definition: ExponentialFit.h:53
ColorButton
A button used for color selection.
Definition: ColorButton.h:54
FitDialog::pasteExpression
void pasteExpression()
Paste the expression of the current function into the editing text box.
Definition: FitDialog.cpp:1006
FitDialog::btnColor
ColorButton * btnColor
Definition: FitDialog.h:182
Filter::setMaximumIterations
void setMaximumIterations(int iter)
Sets the maximum number of iterations to be performed during an iterative session.
Definition: Filter.h:76
ApplicationWindow::pasteFitResultsToPlot
bool pasteFitResultsToPlot
Definition: ApplicationWindow.h:947
Fit::setInitialGuesses
void setInitialGuesses(double *x_init)
Definition: Fit.cpp:216
FitDialog::fitPage
QWidget * fitPage
Definition: FitDialog.h:175
FitDialog::btnBack
QPushButton * btnBack
Definition: FitDialog.h:167
FitDialog::showEditPage
void showEditPage()
Show the edit function page.
Definition: FitDialog.cpp:705
QStringFromString
QString QStringFromString(const std::string &x)
Definition: QStringStdString.h:15
FitDialog::polynomOrderLabel
QLabel * polynomOrderLabel
Definition: FitDialog.h:179
PluginFit.h
FitDialog::initFitPage
void initFitPage()
Initialized the widget for the first dialog page.
Definition: FitDialog.cpp:97
FitDialog::resetFunction
void resetFunction()
Clears the function editor, the parameter names and the function name.
Definition: FitDialog.cpp:1394
FitDialog::setBuiltInFunctions
void setBuiltInFunctions()
Populate the list of build-in function.
Definition: FitDialog.cpp:935
FitDialog::btnAddFunc
QPushButton * btnAddFunc
Definition: FitDialog.h:180
Fit::generateFunction
void generateFunction(bool yes, int points=100)
Specifies weather the result of the fit is a function curve.
Definition: Fit.cpp:222
FitDialog::btnApply
QPushButton * btnApply
Definition: FitDialog.h:180
CONFS
#define CONFS(string)
Definition: FitDialog.cpp:64
FitDialog::changeDataRange
void changeDataRange()
Read the selected data range from the graph.
Definition: FitDialog.cpp:1322
FitDialog::btnDeleteFitCurves
QPushButton * btnDeleteFitCurves
Definition: FitDialog.h:181
Matrix.h
FitDialog::boxYErrorSource
QComboBox * boxYErrorSource
Definition: FitDialog.h:183
Graph::analysableCurvesList
QStringList analysableCurvesList()
Returns the names of all the curves suitable for data analysis, as a string list. The list excludes e...
Definition: Graph.cpp:1311
FitDialog::boxCurve
QComboBox * boxCurve
Definition: FitDialog.h:168
FitDialog::tableNamesBox
QComboBox * tableNamesBox
Definition: FitDialog.h:183
FitDialog::d_user_function_params
QStringList d_user_function_params
Definition: FitDialog.h:152
MultiPeakFit::generateParameterList
static QStringList generateParameterList(int order)
Definition: MultiPeakFit.cpp:97
ApplicationWindow::writeFitResultsToLog
bool writeFitResultsToLog
Write fit output information to Result Log.
Definition: ApplicationWindow.h:950
PolynomialFit
Definition: PolynomialFit.h:35
FitDialog::d_user_function_names
QStringList d_user_function_names
Definition: FitDialog.h:152
FitDialog::loadPlugins
void loadPlugins()
Load the fit plugins.
Definition: FitDialog.cpp:897
Fit::results
const std::vector< double > & results() const
Returns a vector with the fit results.
Definition: Fit.h:90
PolynomialFit::generateParameterList
static QStringList generateParameterList(int order)
Definition: PolynomialFit.cpp:93
ColorButton.h
ApplicationWindow::saveSettings
void saveSettings()
Definition: ApplicationWindow.cpp:4451
MyParser
Mathematical parser class based on muParser.
Definition: MyParser.h:52
MultiPeakFit.h
toString
S toString(const QString &x)
Deal with conversion between QString and std::string/std::wstring in a generic way.
FitDialog::boxUseBuiltIn
QCheckBox * boxUseBuiltIn
Definition: FitDialog.h:157
MyParser::functionsList
static QStringList functionsList()
Definition: MyParser.cpp:54
FitDialog::initEditPage
void initEditPage()
Initialized the widget for the second dialog page.
Definition: FitDialog.cpp:231
Table
MDI window providing a spreadsheet table with column logic.
Definition: Table.h:51
MyParser::SetExpr
void SetExpr(const QString &x)
Definition: MyParser.h:56
FitDialog::boxPrecision
QSpinBox * boxPrecision
Definition: FitDialog.h:174
FitDialog::boxName
QLineEdit * boxName
Definition: FitDialog.h:178
ExponentialFit.h
FitDialog::samePointsBtn
QRadioButton * samePointsBtn
Definition: FitDialog.h:184
FitDialog::editPage
QWidget * editPage
Definition: FitDialog.h:175
ApplicationWindow::fitPoints
int fitPoints
Number of points in a generated fit curve.
Definition: ApplicationWindow.h:942
ApplicationWindow::fit_output_precision
int fit_output_precision
precision used for the output of the fit operations
Definition: ApplicationWindow.h:953
Graph::selectedXStartValue
double selectedXStartValue()
Definition: Graph.cpp:2787
ColorButton::color
QColor color() const
Get the color of the display part.
Definition: ColorButton.cpp:140
FitDialog::setSrcTables
void setSrcTables(QList< MyWidget * > *tables)
Populate the list of tables containing data displayed in the corresponding graph.
Definition: FitDialog.cpp:1330
FitDialog::boxAlgorithm
QComboBox * boxAlgorithm
Definition: FitDialog.h:169
FitDialog::accept
void accept()
Start the actual fitting.
Definition: FitDialog.cpp:1034
FitDialog::d_plugin_params
QStringList d_plugin_params
Definition: FitDialog.h:154
FitDialog::showPointsBox
void showPointsBox(bool)
Enable the X points spin box.
Definition: FitDialog.cpp:500
Graph::selectedXEndValue
double selectedXEndValue()
Definition: Graph.cpp:2795
Graph::curvesList
QStringList curvesList()
Returns the names of all the QwtPlotCurve items on the plot, as a string list.
Definition: Graph.cpp:1324
SigmoidalFit
Definition: SigmoidalFit.h:35
FitDialog::clearUserFunctions
void clearUserFunctions()
Clear the list of user defined functions.
Definition: FitDialog.cpp:772
FitDialog::lblFunction
QLabel * lblFunction
Definition: FitDialog.h:179
FitDialog::tw
QStackedWidget * tw
Definition: FitDialog.h:158
FitDialog::d_plugin_files_list
QStringList d_plugin_files_list
Definition: FitDialog.h:154
FitDialog::containsUserFunctionName
bool containsUserFunctionName(const QString &function)
Check whether the given function contains a user-defined function.
Definition: FitDialog.cpp:1285
FitDialog::btnAddTxt
QPushButton * btnAddTxt
Definition: FitDialog.h:181
FitDialog::addUserFunctions
void addUserFunctions(const QStringList &list)
Add a list of user defined functions.
Definition: FitDialog.cpp:786
PolynomialFit::generateFormula
static QString generateFormula(int order)
Definition: PolynomialFit.cpp:76
FitDialog::selectSrcTable
void selectSrcTable(int tabnr)
Select a table from the source tables list.
Definition: FitDialog.cpp:1348
FitDialog::categoryBox
QListWidget * categoryBox
Definition: FitDialog.h:177
FitDialog::showFunctionsList
void showFunctionsList(int category)
Populate the functions list with functions from the given 'category'.
Definition: FitDialog.cpp:810
Fit::ErrorSource
ErrorSource
Definition: Fit.h:60
FitDialog::polynomOrderBox
QSpinBox * polynomOrderBox
Definition: FitDialog.h:174
FitDialog::btnAddName
QPushButton * btnAddName
Definition: FitDialog.h:181
FitDialog::saveUserFunction
void saveUserFunction()
Save the user-defined function.
Definition: FitDialog.cpp:547
FitDialog::saveFunctionsList
void saveFunctionsList(const QStringList &)
FitDialog::FitDialog
FitDialog(QWidget *parent=0, Qt::WindowFlags fl=0)
Definition: FitDialog.cpp:66
MultiPeakFit
Definition: MultiPeakFit.h:36
Graph::selectedCurveTitle
QString selectedCurveTitle()
Definition: Graph.cpp:1535
FitDialog::closeEvent
void closeEvent(QCloseEvent *e)
On closing show the fit function in the plot window if that option is selected.
Definition: FitDialog.cpp:1374
FitDialog::d_user_functions
QStringList d_user_functions
Definition: FitDialog.h:152
FitDialog::d_built_in_function_names
QStringList d_built_in_function_names
Definition: FitDialog.h:153
FitDialog::btnDelFunc
QPushButton * btnDelFunc
Definition: FitDialog.h:180
FitDialog::buttonPlugins
QPushButton * buttonPlugins
Definition: FitDialog.h:166
FitDialog::showParametersTable
void showParametersTable()
Display the parameters table.
Definition: FitDialog.cpp:456
Fit::CustomErrors
@ CustomErrors
Definition: Fit.h:60
FitDialog::d_graph
Graph * d_graph
Definition: FitDialog.h:151
FitDialog::boxParams
QTableWidget * boxParams
Definition: FitDialog.h:170
MyParser::explainFunction
static QString explainFunction(int index)
Definition: MyParser.cpp:67
parameters
char * parameters()
Definition: exp_saturation.c:49
FitDialog::d_plugin_function_names
QStringList d_plugin_function_names
Definition: FitDialog.h:154
FitDialog::logBox
QCheckBox * logBox
Definition: FitDialog.h:187
FitDialog::setBuiltInFunctionNames
void setBuiltInFunctionNames()
Populate the list of build-in function names.
Definition: FitDialog.cpp:929
ExponentialFit
Definition: ExponentialFit.h:35
PluginFit
Definition: PluginFit.h:35
Fit::setYErrorSource
bool setYErrorSource(ErrorSource err, const QString &colName={}, bool fail_silently=false)
Sets the data set to be used as source of Y errors.
Definition: Fit.cpp:335
Fit::parametersTable
Table * parametersTable(const QString &tableName)
Definition: Fit.cpp:417
FitDialog::showExpression
void showExpression(int function)
Populate the expression text box according to the given function.
Definition: FitDialog.cpp:950
FitDialog::showUserFunctions
void showUserFunctions()
Populate the functions list with user-defined functions.
Definition: FitDialog.cpp:924
FitDialog::buttonAdvanced
QPushButton * buttonAdvanced
Definition: FitDialog.h:163
FitDialog::deleteFitCurves
void deleteFitCurves()
Deletes the result fit curves from the plot.
Definition: FitDialog.cpp:1387
ApplicationWindow::generateUniformFitPoints
bool generateUniformFitPoints
Definition: ApplicationWindow.h:932
ApplicationWindow
SciDAVis's main window.
Definition: ApplicationWindow.h:122
FitDialog::lblPoints
QLabel * lblPoints
Definition: FitDialog.h:179
FitDialog::boxParam
QLineEdit * boxParam
Definition: FitDialog.h:178
FitDialog::clearFunctionsList
void clearFunctionsList()
FitDialog::removeUserFunction
void removeUserFunction()
Remove a user-defined function.
Definition: FitDialog.cpp:617
FitDialog::yErrorSourceChanged
void yErrorSourceChanged(int index)
Enable/disable widgets for selection of custom error source.
Definition: FitDialog.cpp:1360
FitDialog::funcBox
QListWidget * funcBox
Definition: FitDialog.h:177
FitDialog::buttonEdit
QPushButton * buttonEdit
Definition: FitDialog.h:181
MultiPeakFit::Gauss
@ Gauss
Definition: MultiPeakFit.h:40
FitDialog::covMatrixName
QLineEdit * covMatrixName
Definition: FitDialog.h:186
FitDialog::boxTo
QLineEdit * boxTo
Definition: FitDialog.h:172
FitDialog::btnCovMatrix
QPushButton * btnCovMatrix
Definition: FitDialog.h:185
Fit::setAlgorithm
void setAlgorithm(Algorithm s)
Definition: Fit.h:81
name
char * name()
Definition: exp_saturation.c:45
ThreeExpFit
Definition: ExponentialFit.h:68
FitDialog::fitBuiltInFunction
void fitBuiltInFunction(const QString &function, double *initVal)
Fit using a built-in function.
Definition: FitDialog.cpp:1244
FitDialog::showAdvancedPage
void showAdvancedPage()
Show the advanced options page.
Definition: FitDialog.cpp:710
Filter::showLegend
virtual void showLegend()
Adds a new legend to the plot. Calls virtual legendInfo()
Definition: Filter.cpp:196
FitDialog::scaleErrorsBox
QCheckBox * scaleErrorsBox
Definition: FitDialog.h:187
ApplicationWindow::fit_scale_errors
bool fit_scale_errors
Scale the errors output in fit operations with reduced chi^2.
Definition: ApplicationWindow.h:939
FitDialog::buttonClearUsrList
QPushButton * buttonClearUsrList
Definition: FitDialog.h:165
FitDialog::applyChanges
void applyChanges()
Applies the user changes to the numerical format of the output results.
Definition: FitDialog.cpp:443
Fit::covarianceMatrix
Matrix * covarianceMatrix(const QString &matrixName)
Definition: Fit.cpp:442
SigmoidalFit.h
NonLinearFit.h
ApplicationWindow::generatePeakCurves
bool generatePeakCurves
Definition: ApplicationWindow.h:933
FitDialog::showParseFunctions
void showParseFunctions()
Populate the functions list with functions from MyParser.
Definition: FitDialog.cpp:945
FitDialog::d_plugin_functions
QStringList d_plugin_functions
Definition: FitDialog.h:154
FitDialog::generatePointsBox
QSpinBox * generatePointsBox
Definition: FitDialog.h:174
Table::colNames
QStringList colNames()
Definition: Table.cpp:1263
FitDialog::d_fitter
Fit * d_fitter
Definition: FitDialog.h:150
FitDialog::btnContinue
QPushButton * btnContinue
Definition: FitDialog.h:180
Graph
A 2D-plotting widget.
Definition: Graph.h:119
FitDialog::colNamesBox
QComboBox * colNamesBox
Definition: FitDialog.h:183
FitDialog::initAdvancedPage
void initAdvancedPage()
Initialized the widget for the third dialog page.
Definition: FitDialog.cpp:339
Filter::setTolerance
void setTolerance(double eps)
Sets the tolerance used by the GSL routines.
Definition: Filter.h:61
Fit::Algorithm
Algorithm
Definition: Fit.h:59
GaussAmpFit
Definition: MultiPeakFit.h:105
FitDialog::activateCurve
void activateCurve(const QString &curveName)
Read the range of curve 'curveName' and set the from/to fields.
Definition: FitDialog.cpp:535
FitDialog::paramTableName
QLineEdit * paramTableName
Definition: FitDialog.h:186
Filter::setDataFromCurve
bool setDataFromCurve(const QString &curveTitle, Graph *g=0)
Definition: Filter.cpp:151
FitDialog::d_src_table
QList< MyWidget * > * d_src_table
Definition: FitDialog.h:155
ApplicationWindow::peakCurvesColor
QColor peakCurvesColor
Definition: ApplicationWindow.h:934
FitDialog::setGraph
void setGraph(Graph *g)
Set the graph that is to be fitted.
Definition: FitDialog.cpp:514
FitDialog::choosePluginsFolder
void choosePluginsFolder()
Let the user select the Plugin folder.
Definition: FitDialog.cpp:868
Fit::fit
virtual void fit()
Actually does the fit. Should be reimplemented in derived classes.
Definition: Fit.cpp:471
FitDialog::boxFunction
QTextEdit * boxFunction
Definition: FitDialog.h:176
Fit::scaleErrors
void scaleErrors(bool yes=true)
Specifies wheather the errors must be scaled with sqrt(chi_2/dof)
Definition: Fit.h:102
ApplicationWindow.h
FitDialog::buttonCancel3
QPushButton * buttonCancel3
Definition: FitDialog.h:162
FitDialog::btnParamTable
QPushButton * btnParamTable
Definition: FitDialog.h:185
FitDialog::setFunction
void setFunction(bool ok)
Toggle between predefined function and function editing.
Definition: FitDialog.cpp:715
FitDialog::d_built_in_functions
QStringList d_built_in_functions
Definition: FitDialog.h:153
FitDialog.h
Graph::curve
QwtPlotCurve * curve(int index) const
get curve by index
Definition: Graph.cpp:2821
ApplicationWindow::generateUniqueName
QString generateUniqueName(const QString &name, bool increment=true)
Definition: ApplicationWindow.cpp:13503
Graph::curveIndex
int curveIndex(long key) const
Definition: Graph.h:199
FitDialog::pasteFunctionName
void pasteFunctionName()
Paste the name of the current function into the editing text box.
Definition: FitDialog.cpp:1026
FitDialog::enableApplyChanges
void enableApplyChanges(int=0)
Enable the "Apply" button.
Definition: FitDialog.cpp:1382
FitDialog::explainBox
QTextEdit * explainBox
Definition: FitDialog.h:176
FitDialog::buttonCancel1
QPushButton * buttonCancel1
Definition: FitDialog.h:160
ColorButton::setColor
void setColor(const QColor &c)
Set the color of the display part.
Definition: ColorButton.cpp:132
Graph::range
int range(int index, double *start, double *end)
Definition: Graph.cpp:2834