7 #include "CmdMediator.h" 9 #include "CurveConnectAs.h" 10 #include "CurveStyle.h" 11 #include "EngaugeAssert.h" 12 #include "FittingCurveCoefficients.h" 13 #include "FittingModel.h" 14 #include "FittingStatistics.h" 15 #include "FittingWindow.h" 16 #include "GeometryModel.h" 18 #include "MainWindow.h" 19 #include "MainWindowModel.h" 20 #include <QApplication> 23 #include <QGridLayout> 24 #include <QItemSelectionModel> 28 #include "Transformation.h" 29 #include "WindowTable.h" 31 const int COLUMN_COEFFICIENTS = 0;
32 const int COLUMN_POLYNOMIAL_TERMS = 1;
36 m_isLogXTheta (false),
37 m_isLogYRadius (false)
40 setAllowedAreas (Qt::AllDockWidgetAreas);
41 setWindowTitle (tr (
"Curve Fitting Window"));
42 setStatusTip (tr (
"Curve Fitting Window"));
43 setWhatsThis (tr (
"Curve Fitting Window\n\n" 44 "This window applies a curve fit to the currently selected curve.\n\n" 45 "If drag-and-drop is disabled, a rectangular set of cells may be selected by clicking and dragging. Otherwise, if " 46 "drag-and-drop is enabled, a rectangular set of cells may be selected using Click then Shift+Click, since click and drag " 47 "starts the dragging operation. Drag-and-drop mode is set in the Main Window settings"));
49 m_coefficients.resize (MAX_POLYNOMIAL_ORDER + 1);
51 createWidgets (mainWindow);
56 FittingWindow::~FittingWindow()
60 void FittingWindow::calculateCurveFitAndStatistics ()
64 double mse = 0, rms = 0, rSquared = 0;
72 m_lblMeanSquareError->setText (QString::number (mse));
73 m_lblRootMeanSquare->setText (QString::number (rms));
74 m_lblRSquared->setText (QString::number (rSquared));
77 if (m_pointsConvenient.size () > 0) {
78 int last = m_pointsConvenient.size () - 1;
80 m_pointsConvenient [0].x(),
81 m_pointsConvenient [last].x (),
93 for (
int row = 0, order = m_model->rowCount () - 1; row < m_model->rowCount (); row++, order--) {
95 QStandardItem *item =
new QStandardItem (QString::number (m_coefficients [order]));
96 m_model->setItem (row, COLUMN_COEFFICIENTS, item);
102 m_labelY->setText (
"");
103 m_model->setRowCount (0);
104 m_lblMeanSquareError->setText (
"");
105 m_lblRootMeanSquare->setText (
"");
106 m_lblRSquared->setText (
"");
111 LOG4CPP_INFO_S ((*mainCat)) <<
"FittingWindow::closeEvent";
116 void FittingWindow::createWidgets (
MainWindow *mainWindow)
118 QWidget *widget =
new QWidget;
121 QGridLayout *layout =
new QGridLayout;
122 widget->setLayout (layout);
126 QLabel *labelOrder =
new QLabel (tr (
"Order:"));
127 layout->addWidget (labelOrder, row, 0, 1, 1);
129 m_cmbOrder =
new QComboBox;
130 for (
int order = 0; order <= MAX_POLYNOMIAL_ORDER; order++) {
131 m_cmbOrder->addItem (QString::number (order), QVariant (order));
133 connect (m_cmbOrder, SIGNAL (currentIndexChanged (
int)),
this, SLOT (slotCmbOrder (
int)));
134 layout->addWidget (m_cmbOrder, row++, 1, 1, 1);
137 m_labelY =
new QLabel;
138 layout->addWidget (m_labelY, row++, 0, 1, 1);
142 m_model->setColumnCount (2);
145 connect (m_view, SIGNAL (signalTableStatusChange ()),
146 mainWindow, SLOT (slotTableStatusChange ()));
148 layout->addWidget (m_view, row++, 0, 1, 2);
151 QLabel *lblMeanSquareError =
new QLabel (tr (
"Mean square error:"));
152 layout->addWidget (lblMeanSquareError, row, 0, 1, 1);
154 m_lblMeanSquareError =
new QLineEdit;
155 m_lblMeanSquareError->setReadOnly (
true);
156 m_lblMeanSquareError->setWhatsThis (tr (
"Calculated mean square error statistic"));
157 layout->addWidget (m_lblMeanSquareError, row++, 1, 1, 1);
159 QLabel *lblRootMeanSquare =
new QLabel (tr (
"Root mean square:"));
160 layout->addWidget (lblRootMeanSquare, row, 0, 1, 1);
162 m_lblRootMeanSquare =
new QLineEdit;
163 m_lblRootMeanSquare->setReadOnly (
true);
164 m_lblRootMeanSquare->setWhatsThis (tr (
"Calculated root mean square statistic. This is calculated as the square root of the mean square error"));
165 layout->addWidget (m_lblRootMeanSquare, row++, 1, 1, 1);
167 QLabel *lblRSquared =
new QLabel (tr (
"R squared:"));
168 layout->addWidget (lblRSquared, row, 0, 1, 1);
170 m_lblRSquared =
new QLineEdit;
171 m_lblRSquared->setReadOnly (
true);
172 m_lblRSquared->setWhatsThis (tr (
"Calculated R squared statistic"));
173 layout->addWidget (m_lblRSquared, row++, 1, 1, 1);
178 LOG4CPP_INFO_S ((*mainCat)) <<
"FittingWindow::doCopy";
182 if (!text.isEmpty ()) {
185 QApplication::clipboard ()->setText (text);
190 void FittingWindow::initializeOrder ()
192 const int SECOND_ORDER = 2;
194 int index = m_cmbOrder->findData (QVariant (SECOND_ORDER));
195 m_cmbOrder->setCurrentIndex (index);
198 int FittingWindow::maxOrder ()
const 200 return m_cmbOrder->currentData().toInt();
203 void FittingWindow::refreshTable ()
205 int order = m_cmbOrder->currentData().toInt();
210 calculateCurveFitAndStatistics ();
213 void FittingWindow::resizeTable (
int order)
215 LOG4CPP_INFO_S ((*mainCat)) <<
"FittingWindow::resizeTable";
217 m_model->setRowCount (order + 1);
220 QString yTerm = QString (
"%1%2%3")
221 .arg (m_curveSelected)
222 .arg (m_curveSelected.isEmpty () ?
225 .arg (m_isLogYRadius ?
228 m_labelY->setText (yTerm);
231 QString xString = (m_isLogXTheta ?
234 for (
int row = 0, term = order; term >= 0; row++, term--) {
237 QString termString = QString (
"%1%2%3%4")
238 .arg ((term > 0) ? xString :
"")
239 .arg ((term > 1) ?
"^" :
"")
240 .arg ((term > 1) ? QString::number (term) :
"")
241 .arg ((term > 0) ?
"+" :
"");
243 QStandardItem *item =
new QStandardItem (termString);
244 m_model->setItem (row, COLUMN_POLYNOMIAL_TERMS, item);
248 void FittingWindow::slotCmbOrder(
int )
255 const QString &curveSelected,
258 LOG4CPP_INFO_S ((*mainCat)) <<
"FittingWindow::update";
261 m_curveSelected = curveSelected;
268 m_pointsConvenient.clear ();
275 ENGAUGE_CHECK_PTR (curve);
280 const Points points = curve->
points();
281 Points::const_iterator itr;
282 for (itr = points.begin (); itr != points.end (); itr++) {
284 const Point &point = *itr;
292 double x = qLn (posGraph.x()) / qLn (10.0);
295 if (m_isLogYRadius) {
296 double y = qLn (posGraph.y()) / qLn (10.0);
300 m_pointsConvenient.append (posGraph);
310 return dynamic_cast<QTableView*
> (m_view);
Dockable widget abstract base class.
const Curve * curveForCurveName(const QString &curveName) const
See CurvesGraphs::curveForCurveNames, although this also works for AXIS_CURVE_NAME.
virtual void update(const CmdMediator &cmdMediator, const MainWindowModel &modelMainWindow, const QString &curveSelected, const Transformation &transformation)
Populate the table with the specified Curve.
Class that represents one digitized point. The screen-to-graph coordinate transformation is always ex...
QPointF posScreen() const
Accessor for screen position.
const Points points() const
Return a shallow copy of the Points.
int numPoints() const
Number of points.
FittingWindow(MainWindow *mainWindow)
Single constructor. Parent is needed or else this widget cannot be redocked after being undocked...
QString selectionAsText(ExportDelimiter delimiter) const
Convert the selection into exportable text which is good for text editors.
void setDelimiter(ExportDelimiter delimiter)
Save output delimiter.
Model for DlgSettingsMainWindow.
CoordScale coordScaleXTheta() const
Get method for linear/log scale on x/theta.
void signalFittingWindowClosed()
Signal that this QDockWidget was just closed.
void signalCurveFit(FittingCurveCoefficients, double, double, bool, bool)
Signal containing coefficients from curve fit.
Table view class with support for both drag-and-drop and copy-and-paste.
virtual QTableView * view() const
QTableView-based class used by child class.
Container for one set of digitized Points.
CoordScale coordScaleYRadius() const
Get method for linear/log scale on y/radius.
bool dragDropExport() const
Get method for drag and drop export.
DocumentModelExportFormat modelExport() const
Get method for DocumentModelExportFormat.
This class does the math to compute statistics for FittingWindow.
virtual void closeEvent(QCloseEvent *event)
Catch close event so corresponding menu item in MainWindow can be updated accordingly.
DocumentModelCoords modelCoords() const
Get method for DocumentModelCoords.
Main window consisting of menu, graphics scene, status bar and optional toolbars as a Single Document...
virtual void clear()
Clear stale information.
void calculateCurveFitAndStatistics(unsigned int order, const FittingPointsConvenient &pointsConvenient, FittingCurveCoefficients &coefficients, double &mse, double &rms, double &rSquared)
Compute the curve fit and the statistics for that curve fit.
virtual void doCopy()
Copy the current selection to the clipboard.