1 /***************************************************************************
2     File                 : SmoothCurveDialog.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          : Smoothing options dialog
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 "SmoothCurveDialog.h"
30 #include "Graph.h"
31 #include "MyParser.h"
32 #include "ColorButton.h"
33 #include "SmoothFilter.h"
34 
35 #include <QGroupBox>
36 #include <QSpinBox>
37 #include <QMessageBox>
38 #include <QPushButton>
39 #include <QLabel>
40 #include <QLineEdit>
41 #include <QComboBox>
42 #include <QLayout>
43 
SmoothCurveDialog(int method,QWidget * parent,Qt::WindowFlags fl)44 SmoothCurveDialog::SmoothCurveDialog(int method, QWidget *parent, Qt::WindowFlags fl)
45     : QDialog(parent, fl)
46 {
47     smooth_method = method;
48 
49     setWindowTitle(tr("Smoothing Options"));
50 
51     QGroupBox *gb1 = new QGroupBox();
52     QGridLayout *gl1 = new QGridLayout(gb1);
53     gl1->addWidget(new QLabel(tr("Curve")), 0, 0);
54 
55     boxName = new QComboBox();
56     gl1->addWidget(boxName, 0, 1);
57 
58     btnColor = new ColorButton();
59     btnColor->setColor(QColor(Qt::red));
60 
61     if (method == SmoothFilter::SavitzkyGolay) {
62         gl1->addWidget(new QLabel(tr("Polynomial Order")), 1, 0);
63         boxOrder = new QSpinBox();
64         boxOrder->setRange(0, 9);
65         boxOrder->setValue(2);
66         gl1->addWidget(boxOrder, 1, 1);
67 
68         gl1->addWidget(new QLabel(tr("Points to the Left")), 2, 0);
69         boxPointsLeft = new QSpinBox();
70         boxPointsLeft->setRange(1, 25);
71         boxPointsLeft->setValue(2);
72         gl1->addWidget(boxPointsLeft, 2, 1);
73 
74         gl1->addWidget(new QLabel(tr("Points to the Right")), 3, 0);
75         boxPointsRight = new QSpinBox();
76         boxPointsRight->setRange(1, 25);
77         boxPointsRight->setValue(2);
78         gl1->addWidget(boxPointsRight, 3, 1);
79 
80         gl1->addWidget(new QLabel(tr("Color")), 4, 0);
81         gl1->addWidget(btnColor, 4, 1);
82         gl1->setRowStretch(5, 1);
83     } else {
84         gl1->addWidget(new QLabel(tr("Points")), 1, 0);
85         boxPointsLeft = new QSpinBox();
86         boxPointsLeft->setRange(1, 1000000);
87         boxPointsLeft->setSingleStep(10);
88         boxPointsLeft->setValue(5);
89         gl1->addWidget(boxPointsLeft, 1, 1);
90 
91         gl1->addWidget(new QLabel(tr("Color")), 2, 0);
92         gl1->addWidget(btnColor, 2, 1);
93         gl1->setRowStretch(3, 1);
94     }
95     gl1->setColumnStretch(2, 1);
96 
97     btnSmooth = new QPushButton(tr("&Smooth"));
98     btnSmooth->setDefault(true);
99     buttonCancel = new QPushButton(tr("&Close"));
100 
101     QVBoxLayout *vl = new QVBoxLayout();
102     vl->addWidget(btnSmooth);
103     vl->addWidget(buttonCancel);
104     vl->addStretch();
105 
106     QHBoxLayout *hb = new QHBoxLayout(this);
107     hb->addWidget(gb1);
108     hb->addLayout(vl);
109 
110     connect(btnSmooth, SIGNAL(clicked()), this, SLOT(smooth()));
111     connect(buttonCancel, SIGNAL(clicked()), this, SLOT(reject()));
112     connect(boxName, SIGNAL(activated(const QString &)), this,
113             SLOT(activateCurve(const QString &)));
114 }
115 
smooth()116 void SmoothCurveDialog::smooth()
117 {
118     SmoothFilter *sf = new SmoothFilter((ApplicationWindow *)this->parent(), graph,
119                                         boxName->currentText(), smooth_method);
120     if (smooth_method == SmoothFilter::SavitzkyGolay) {
121         sf->setSmoothPoints(boxPointsLeft->value(), boxPointsRight->value());
122         sf->setPolynomOrder(boxOrder->value());
123     } else
124         sf->setSmoothPoints(boxPointsLeft->value());
125 
126     sf->setColor(btnColor->color());
127     sf->run();
128     delete sf;
129 }
130 
setGraph(Graph * g)131 void SmoothCurveDialog::setGraph(Graph *g)
132 {
133     graph = g;
134     boxName->addItems(g->analysableCurvesList());
135     activateCurve(boxName->currentText());
136 }
137 
activateCurve(const QString & curveName)138 void SmoothCurveDialog::activateCurve(const QString &curveName)
139 {
140     if (smooth_method == SmoothFilter::Average) {
141         QwtPlotCurve *c = graph->curve(curveName);
142         if (!c || c->rtti() != QwtPlotItem::Rtti_PlotCurve)
143             return;
144 
145         boxPointsLeft->setMaximum(c->dataSize() / 2);
146     }
147 }
148