-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDPlotter.cpp
300 lines (250 loc) · 10.4 KB
/
DPlotter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include "DPlotter.h"
#include <QString>
#include <QColorDialog>
#include <QPixmap>
#include <QIcon>
#include <QDebug>
#include <qwt_scale_div.h>
DPlotter::DPlotter(QGroupBox *parent) : QGroupBox(parent) {
setupUi(this);
guiTimer.start(50);
qwtPlot->setCanvasBackground(Qt::white);
qwtPlot->setAxisTitle(QwtPlot::xBottom, QwtText("t in Sekunden",QwtText::PlainText) );
qwtPlot->setAxisScale(QwtPlot::xBottom, -doubleSpinBox_keepSeconds->value()*1000,0,0);
buttonGroup_scaleYSelect = new QButtonGroup();
buttonGroup_scaleYSelect->addButton(radioButton_autoPlotYScale);
buttonGroup_scaleYSelect->setId(radioButton_autoPlotYScale,1);
buttonGroup_scaleYSelect->addButton(radioButton_manualPlotYScale);
buttonGroup_scaleYSelect->setId(radioButton_manualPlotYScale,2);
radioButton_manualPlotYScale->setChecked(true);
handle_plotYScale(buttonGroup_scaleYSelect->checkedId());
buttonGroup_scaleXSelect = new QButtonGroup();
buttonGroup_scaleXSelect->addButton(radioButton_realPlotXScale);
buttonGroup_scaleXSelect->setId(radioButton_realPlotXScale,1);
buttonGroup_scaleXSelect->addButton(radioButton_dynPlotXScale);
buttonGroup_scaleXSelect->setId(radioButton_dynPlotXScale,2);
radioButton_dynPlotXScale->setChecked(true);
handle_plotXScale(buttonGroup_scaleXSelect->checkedId());
connect(&guiTimer, SIGNAL(timeout()), this, SLOT(updateGui()));
connect(doubleSpinBox_keepSeconds, SIGNAL(valueChanged(double)), this, SLOT(change_keepTime(double)));
connect(checkBox_showCurve, SIGNAL(clicked()), this, SLOT(handle_curveVisibility()));
connect(comboBox_curveNames, SIGNAL(currentIndexChanged(QString)), this, SLOT(selectCurve(QString)));
connect(toolButton_selectColor, SIGNAL(clicked()), this, SLOT(selectColor()));
connect(buttonGroup_scaleYSelect, SIGNAL(buttonClicked(int)), this, SLOT(handle_plotYScale(int)));
connect(buttonGroup_scaleXSelect, SIGNAL(buttonClicked(int)), this, SLOT(handle_plotXScale(int)));
connect(doubleSpinBox_minPlotScale, SIGNAL(valueChanged(double)), this, SLOT(handle_manualPlotScale(double)));
connect(doubleSpinBox_maxPlotScale, SIGNAL(valueChanged(double)), this, SLOT(handle_manualPlotScale(double)));
connect(doubleSpinBox_curveOffset, SIGNAL(valueChanged(double)), this, SLOT(handle_curveOffset(double)));
connect(doubleSpinBox_curveScale, SIGNAL(valueChanged(double)), this, SLOT(handle_curveScale(double)));
connect(pushButton_clearPlot, SIGNAL(clicked()), this, SLOT(clearCurves()));
connect(pushButton_mark, SIGNAL(clicked()), this, SLOT(setMarker()));
connect(toolButton_runStop, SIGNAL(clicked()), this, SLOT(handle_runStop()));
m_isRunning = true;
}
DPlotter::~DPlotter() {
delete buttonGroup_scaleYSelect;
delete buttonGroup_scaleXSelect;
QHashIterator<QString, DPlotter::curve_t*> i(allCurves);
while (i.hasNext()) {
i.next();
removeCurve(i.key());
}
}
/**
* Adding and removing curves:
*/
void DPlotter::addCurves( QHash<QString, QColor> *curves){
QHashIterator<QString, QColor> i(*curves);
while (i.hasNext()) {
i.next();
addCurve(i.key(), i.value());
}
}
void DPlotter::addCurve( const QString name, const QColor color, bool vis){
// add curve to Hash:
if (!allCurves.contains(name)){
qDebug() << "DPlotter: Adding curve"<<name;
DPlotter::curve_t* newCurve = new DPlotter::curve_t;
newCurve->dataCurve = new QwtPlotCurve(name);
newCurve->dataCurve->setStyle(QwtPlotCurve::Lines);
//newCurve->dataCurve->setRenderHint(QwtPlotItem::RenderAntialiased);// nice looking! but slow...
newCurve->dataCurve->setPen(QPen(color));
newCurve->dataVault = new DVault;
allCurves.insert(name,newCurve);
// update gui-elements
comboBox_curveNames->insertItem(comboBox_curveNames->maxCount(),name);// insert at end of the dropbox
setComboBoxColor(name,color);
if (vis)
setCurveVisible(name,vis);
}
}
void DPlotter::removeCurve( const QString name ){
// remove curve from QHash:
if (allCurves.contains(name)){
qDebug() << "DPlotter: removing curve"<<name;
allCurves.value(name)->dataCurve->detach();
delete allCurves.value(name)->dataCurve;
delete allCurves.value(name)->dataVault;
delete allCurves.value(name);
allCurves.remove(name);
// update gui-elements
comboBox_curveNames->removeItem(comboBox_curveNames->findText(name));
}
}
void DPlotter::addPlotValue(const QString name, double val){
if (m_isRunning)
allCurves.value(name)->dataVault->slurp(val);
}
// will also delete the marker curves...
void DPlotter::clearCurves(){
QHashIterator<QString, DPlotter::curve_t*> i(allCurves);
while (i.hasNext()) {
i.next();
i.value()->dataVault->clear();
if (i.key() == "marker")
removeCurve("marker");
}
}
void DPlotter::updateGui(){
DVault::dataPtr_t* ptr;// intermediate structure to save pointers and length information
QHashIterator<QString, DPlotter::curve_t*> i(allCurves);
while (i.hasNext()) {
i.next();
// first, get the informations assembled in the Vault, see description of this class
ptr = i.value()->dataVault->getDataPtr();
// then feed this data into the Curve
i.value()->dataCurve->setRawData(ptr->dataT, ptr->dataY, ptr->len);
}
// at the end, update the plot
qwtPlot->replot();
DVault::stats_t *stats = allCurves.value(comboBox_curveNames->currentText())->dataVault->getStats();
label_fsample->setText(QString::number((int)stats->f_sample)+"Hz");
label_mean->setText(QString::number(stats->mean,'f',2));
label_max->setText(QString::number(stats->max,'f',2));
label_min->setText(QString::number(stats->min,'f',2));
label_points->setText(QString::number(stats->points));
label_head->setText(QString::number(stats->head,'f',2));
label_CF->setText(QString::number(stats->CF,'f',2));
label_rms->setText(QString::number(stats->rms,'f',2));
// dirty hack...
if (ptr->len >= 1024){
ptr = allCurves.value(comboBox_curveNames->currentText())->dataVault->getDataPtr(1024);
stats = allCurves.value(comboBox_curveNames->currentText())->dataVault->getStats();
} else {
ptr = allCurves.value(comboBox_curveNames->currentText())->dataVault->getDataPtr(ptr->len);
stats = allCurves.value(comboBox_curveNames->currentText())->dataVault->getStats();
}
}
void DPlotter::setMarker()
{
// add new temporary curve and slurp two points in it. it will be deleted automatically, of its run empty
QString name = QString("marker");
addCurve( name, Qt::red, true);
allCurves.value(name)->dataCurve->setStyle(QwtPlotCurve::Sticks);
addPlotValue(name, (qwtPlot->axisScaleDiv(QwtPlot::yLeft))->upperBound());
addPlotValue(name, (qwtPlot->axisScaleDiv(QwtPlot::yLeft))->lowerBound());
allCurves.value("marker")->dataVault->setDynamixXAxis(false);
}
/**
* Gui-Functionality:
*/
void DPlotter::selectCurve(QString name){
if (!name.isEmpty()){// the last item, before we exit, when all others are deleted, is empty...
checkBox_showCurve->setChecked(allCurves.value(name)->dataCurve->plot());//plot returns true is attached, false if not
doubleSpinBox_curveOffset->setValue(allCurves.value(name)->dataVault->getOffset());
doubleSpinBox_curveScale->setValue(allCurves.value(name)->dataVault->getScale());
}
}
void DPlotter::setComboBoxColor(QString name, QColor color){
// create the nice color-block in the dropbown menu
QIcon myIcon;
QPixmap myPix(10,10);
myPix.fill(color);
myIcon.addPixmap(myPix);
int id = comboBox_curveNames->findText(name);
comboBox_curveNames->setItemIcon(id, myIcon);
}
void DPlotter::selectColor(){
QString name = comboBox_curveNames->currentText();
// create an extra window to ask the user, therefore we need to know the current color of the actual curve
QColor color = QColorDialog::getColor( allCurves.value(name)->dataCurve->pen().color() );
setComboBoxColor(name,color);
// set curve to color:
allCurves.value(name)->dataCurve->setPen(QPen(color));
}
void DPlotter::handle_curveVisibility(){
QString name = comboBox_curveNames->currentText();
if (checkBox_showCurve->isChecked()) {
allCurves.value(name)->dataCurve->attach(qwtPlot);
} else {
allCurves.value(name)->dataCurve->detach();
}
}
//wrapperfunction to be able to trigger the above function from outside
void DPlotter::setCurveVisible(const QString name, const bool vis){
if (vis)
allCurves.value(name)->dataCurve->attach(qwtPlot);
else
allCurves.value(name)->dataCurve->detach();
if (name == comboBox_curveNames->currentText())//???
checkBox_showCurve->setChecked(true);
}
void DPlotter::handle_runStop()
{
if (m_isRunning)
{
qDebug() << "DPlotter: halting plot";
toolButton_runStop->setText("run");
guiTimer.stop();
m_isRunning = false;
} else
{
qDebug() << "DPlotter: restarting plot";
toolButton_runStop->setText("stop");
guiTimer.start();
m_isRunning = true;
}
}
void DPlotter::change_keepTime(const double keepTime_s){
doubleSpinBox_keepSeconds->setValue(keepTime_s);
qwtPlot->setAxisScale(QwtPlot::xBottom, -keepTime_s*1000,0,0);
// Set new keeptime in all curves:
QHashIterator<QString, DPlotter::curve_t*> i(allCurves);
while (i.hasNext()) {
i.next();
i.value()->dataVault->setKeepTime(keepTime_s*1000.0);
}
}
void DPlotter::handle_curveOffset(double offset){
QString name = comboBox_curveNames->currentText();
allCurves.value(name)->dataVault->setOffset(offset);
}
void DPlotter::handle_curveScale(double scale){
QString name = comboBox_curveNames->currentText();
allCurves.value(name)->dataVault->setScale(scale);
}
void DPlotter::handle_manualPlotScale(double value){
value++;//ignore the given value, read both needed freshly from the widget
if (buttonGroup_scaleYSelect->checkedId() == 2)
qwtPlot->setAxisScale(QwtPlot::yLeft, doubleSpinBox_minPlotScale->value(), doubleSpinBox_maxPlotScale->value(),0);
}
void DPlotter::handle_plotYScale(int id){
if (id == 1) {//eg "auto"
qwtPlot->setAxisAutoScale(QwtPlot::yLeft);
} else {
qwtPlot->setAxisScale(QwtPlot::yLeft, doubleSpinBox_minPlotScale->value(), doubleSpinBox_maxPlotScale->value(),0);
}
}
void DPlotter::handle_plotXScale(int id){
if (comboBox_curveNames->currentText() != QString("")) {//this is the case during contruction of widget, where this handler is triggered
if (id == 1) {//eg "real"
qDebug() << "DPlotter: disabling dynamic x-axis for curve"<<comboBox_curveNames->currentText();
allCurves.value(comboBox_curveNames->currentText())->dataVault->setDynamixXAxis(false);
} else if (id == 2) {
qDebug() << "DPlotter: enabling dynamic x-axis for curve"<<comboBox_curveNames->currentText();
allCurves.value(comboBox_curveNames->currentText())->dataVault->setDynamixXAxis(true);
} else {
qDebug() << "DPlotter: can't find id"<<id<<"in buttonGroup plotXscale";
}
}
}