-
Notifications
You must be signed in to change notification settings - Fork 181
Expand file tree
/
Copy pathtimeplotcomponentsettings.cpp
More file actions
336 lines (279 loc) · 10.6 KB
/
timeplotcomponentsettings.cpp
File metadata and controls
336 lines (279 loc) · 10.6 KB
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
* Copyright (c) 2024 Analog Devices Inc.
*
* This file is part of Scopy
* (see https://www.github.com/analogdevicesinc/scopy).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include <timeplotcomponentsettings.h>
#include <gui/widgets/menusectionwidget.h>
#include <gui/widgets/menucollapsesection.h>
#include <QWidget>
#include <QLineEdit>
#include <timeplotcomponentchannel.h>
#include <style.h>
#include <pluginbase/preferences.h>
using namespace scopy;
using namespace scopy::adc;
TimePlotComponentSettings::TimePlotComponentSettings(TimePlotComponent *plt, QWidget *parent)
: QWidget(parent)
, ToolComponent()
, m_plotComponent(plt)
, m_autoscaleEnabled(false)
, m_running(false)
{
// This could be refactored in it's own class
QVBoxLayout *v = new QVBoxLayout(this);
v->setSpacing(0);
v->setMargin(0);
setLayout(v);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
MenuSectionCollapseWidget *plotMenu = new MenuSectionCollapseWidget(
"SETTINGS", MenuCollapseSection::MHCW_NONE, MenuCollapseSection::MHW_BASEWIDGET, parent);
QLabel *plotTitleLabel = new QLabel("Plot title");
Style::setStyle(plotTitleLabel, style::properties::label::menuSmall);
QLineEdit *plotTitle = new QLineEdit(m_plotComponent->name());
Style::setStyle(plotTitle, style::properties::lineedit::menuLineEdit);
connect(plotTitle, &QLineEdit::textChanged, this, [=](QString s) {
m_plotComponent->setName(s);
// plotMenu->setTitle("PLOT - " + s);
});
MenuOnOffSwitch *labelsSwitch = new MenuOnOffSwitch("Show plot labels", plotMenu, false);
connect(labelsSwitch->onOffswitch(), &QAbstractButton::toggled, m_plotComponent,
&PlotComponent::showPlotLabels);
m_yCtrl = new MenuPlotAxisRangeControl(m_plotComponent->timePlot()->yAxis(), this);
m_autoscaleBtn = new MenuOnOffSwitch(tr("AUTOSCALE"), plotMenu, false);
m_autoscaler = new PlotAutoscaler(this);
connect(m_autoscaler, &PlotAutoscaler::newMin, m_yCtrl, &MenuPlotAxisRangeControl::setMin);
connect(m_autoscaler, &PlotAutoscaler::newMax, m_yCtrl, &MenuPlotAxisRangeControl::setMax);
connect(m_yCtrl, &MenuPlotAxisRangeControl::intervalChanged, this, [=](double min, double max) {
bool singleYMode = false;
if(m_plotComponent->XYXChannel()) {
singleYMode = dynamic_cast<TimePlotComponentChannel *>(
m_plotComponent->XYXChannel()->plotChannelCmpt())
->m_singleYMode;
}
if(singleYMode) {
m_plotComponent->xyPlot()->xAxis()->setInterval(m_yCtrl->min(), m_yCtrl->max());
}
m_plotComponent->xyPlot()->yAxis()->setInterval(m_yCtrl->min(), m_yCtrl->max());
});
connect(m_autoscaleBtn->onOffswitch(), &QAbstractButton::toggled, this, [=](bool b) {
m_yCtrl->setEnabled(!b);
m_autoscaleEnabled = b;
toggleAutoScale();
});
MenuSectionCollapseWidget *yaxis = new MenuSectionCollapseWidget("Y-AXIS", MenuCollapseSection::MHCW_NONE,
MenuCollapseSection::MHW_BASEWIDGET, parent);
MenuSectionCollapseWidget *xySection = new MenuSectionCollapseWidget(
"XY PLOT", MenuCollapseSection::MHCW_ONOFF, MenuCollapseSection::MHW_BASEWIDGET, parent);
QAbstractButton *xySwitch = xySection->collapseSection()->header();
m_xAxisSrc = new MenuCombo("XY - X Axis source");
connect(m_xAxisSrc->combo(), qOverload<int>(&QComboBox::currentIndexChanged), this, [=](int idx) {
QComboBox *cb = m_xAxisSrc->combo();
ChannelComponent *c = static_cast<ChannelComponent *>(cb->itemData(idx).value<void *>());
m_plotComponent->setXYXChannel(c);
});
m_xAxisShow = new MenuOnOffSwitch("XY - Plot X source", plotMenu, false);
connect(xySwitch, &QAbstractButton::toggled, this, [=](bool b) {
m_plotComponent->xyPlot()->setVisible(b);
m_xAxisSrc->setVisible(b);
m_xAxisShow->setVisible(b);
});
connect(m_xAxisShow->onOffswitch(), &QAbstractButton::toggled, this,
[=](bool b) { m_plotComponent->showXSourceOnXy(b); });
m_yModeCb = new MenuCombo("YMODE", plotMenu);
auto ycb = m_yModeCb->combo();
ycb->addItem("ADC Counts", YMODE_COUNT);
ycb->addItem("% Full Scale", YMODE_FS);
connect(ycb, qOverload<int>(&QComboBox::currentIndexChanged), this, [=](int idx) {
m_ymode = static_cast<YMode>(ycb->itemData(idx).toInt());
for(auto c : qAsConst(m_scaleProviders)) {
c->setYMode(m_ymode);
}
updateYAxis();
});
m_curve = new MenuPlotChannelCurveStyleControl(plotMenu);
m_deletePlot = new QPushButton("DELETE PLOT");
StyleHelper::BasicButton(m_deletePlot);
connect(m_deletePlot, &QAbstractButton::clicked, this, [=]() { Q_EMIT requestDeletePlot(); });
yaxis->contentLayout()->setSpacing(2);
yaxis->contentLayout()->addWidget(m_autoscaleBtn);
yaxis->contentLayout()->addWidget(m_yCtrl);
yaxis->contentLayout()->addWidget(m_yModeCb);
plotMenu->contentLayout()->addWidget(plotTitleLabel);
plotMenu->contentLayout()->addWidget(plotTitle);
plotMenu->contentLayout()->addWidget(labelsSwitch);
plotMenu->contentLayout()->addWidget(m_curve);
plotMenu->contentLayout()->setSpacing(10);
xySection->add(m_xAxisSrc);
xySection->add(m_xAxisShow);
v->setSpacing(10);
v->addWidget(yaxis);
v->addWidget(xySection);
v->addWidget(plotMenu);
v->addWidget(m_deletePlot);
v->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding));
m_autoscaleBtn->setVisible(true);
m_yCtrl->setVisible(true);
m_xAxisSrc->setVisible(false);
m_xAxisShow->setVisible(false);
// init
xySwitch->setChecked(false);
m_yCtrl->setMin(-2048);
m_yCtrl->setMax(2048);
labelsSwitch->onOffswitch()->setChecked(true);
labelsSwitch->onOffswitch()->setChecked(false);
m_deletePlotHover = new QPushButton("", nullptr);
m_deletePlotHover->setMaximumSize(16, 16);
m_deletePlotHover->setIcon(QIcon(":/gui/icons/orange_close.svg"));
connect(m_deletePlotHover, &QAbstractButton::clicked, this, [=]() { Q_EMIT requestDeletePlot(); });
m_settingsPlotHover = new QPushButton("", nullptr);
m_settingsPlotHover->setMaximumSize(16, 16);
m_settingsPlotHover->setIcon(
QIcon(":/gui/icons/" + Style::getAttribute(json::theme::icon_theme_folder) + "/icons/preferences.svg"));
connect(m_settingsPlotHover, &QAbstractButton::clicked, this, [=]() { Q_EMIT requestSettings(); });
m_plotComponent->timePlot()->plotButtonManager()->add(m_deletePlotHover);
m_plotComponent->timePlot()->plotButtonManager()->add(m_settingsPlotHover);
m_autoscaleBtn->onOffswitch()->setChecked(true);
m_ymode = static_cast<YMode>(-1);
auto y = Preferences::get("adc_default_y_mode").toInt();
m_yModeCb->combo()->setCurrentIndex(y);
}
void TimePlotComponentSettings::showDeleteButtons(bool b)
{
m_deletePlotHover->setVisible(b);
m_deletePlot->setVisible(b);
}
void TimePlotComponentSettings::showPlotButtons(bool b)
{
m_plotComponent->timePlot()->plotButtonManager()->setVisible(b);
m_deletePlot->setVisible(b);
m_settingsPlotHover->setVisible(b);
m_deletePlotHover->setVisible(b);
}
TimePlotComponentSettings::~TimePlotComponentSettings() {}
void TimePlotComponentSettings::addChannel(ChannelComponent *c)
{
// https://stackoverflow.com/questions/44501171/qvariant-with-custom-class-pointer-does-not-return-same-address
auto timePlotComponentChannel = dynamic_cast<TimePlotComponentChannel *>(c->plotChannelCmpt());
m_xAxisSrc->combo()->addItem(c->name(), QVariant::fromValue(static_cast<void *>(c)));
m_autoscaler->addChannels(timePlotComponentChannel->m_timePlotCh);
ScaleProvider *sp = dynamic_cast<ScaleProvider *>(c);
if(sp) {
m_scaleProviders.append(sp);
updateYModeCombo();
}
m_curve->addChannels(timePlotComponentChannel->m_timePlotCh);
m_curve->addChannels(timePlotComponentChannel->m_xyPlotCh);
m_channels.append(c);
}
void TimePlotComponentSettings::removeChannel(ChannelComponent *c)
{
m_channels.removeAll(c);
int comboId = m_xAxisSrc->combo()->findData(QVariant::fromValue(static_cast<void *>(c)));
m_xAxisSrc->combo()->removeItem(comboId);
TimePlotComponentChannel *chcmpt = dynamic_cast<TimePlotComponentChannel *>(c->plotChannelCmpt());
m_autoscaler->removeChannels(chcmpt->m_timePlotCh);
ScaleProvider *sp = dynamic_cast<ScaleProvider *>(c);
if(sp) {
m_scaleProviders.removeAll(sp);
updateYModeCombo();
}
m_curve->removeChannels(chcmpt->m_timePlotCh);
m_curve->removeChannels(chcmpt->m_xyPlotCh);
}
void TimePlotComponentSettings::onInit() {}
void TimePlotComponentSettings::onDeinit() {}
void TimePlotComponentSettings::onStart()
{
m_running = true;
toggleAutoScale();
}
void TimePlotComponentSettings::onStop()
{
m_running = false;
toggleAutoScale();
}
void TimePlotComponentSettings::toggleAutoScale()
{
if(m_running && m_autoscaleEnabled) {
m_autoscaler->start();
} else {
m_autoscaler->stop();
}
}
void TimePlotComponentSettings::updateYModeCombo()
{
bool scaleItemCbtmp = true;
for(ScaleProvider *s : qAsConst(m_scaleProviders)) {
if(s->scaleAvailable() == false) {
scaleItemCbtmp = false;
break;
}
}
if(scaleItemCbtmp) {
// need scale item
int idx = m_yModeCb->combo()->findData(YMODE_SCALE);
if(idx == -1) {
m_yModeCb->combo()->addItem("Scale", YMODE_SCALE);
}
} else {
// no need
int idx = m_yModeCb->combo()->findData(YMODE_SCALE);
if(idx) {
m_yModeCb->combo()->removeItem(idx);
}
}
}
void TimePlotComponentSettings::updateYAxis()
{
double max = -1000000.0;
double min = 1000000.0;
for(ScaleProvider *s : qAsConst(m_scaleProviders)) {
if(s->yMax() > max) {
max = s->yMax();
}
if(s->yMin() < min) {
min = s->yMin();
}
}
m_yCtrl->setMin(min);
m_yCtrl->setMax(max);
auto timePlotYAxis = m_plotComponent->timePlot()->yAxis();
switch(m_ymode) {
case YMODE_COUNT:
// Move this to iio_units function
timePlotYAxis->setUnits("");
timePlotYAxis->scaleDraw()->setFloatPrecision(3);
timePlotYAxis->getFormatter()->setTwoDecimalMode(false);
break;
case YMODE_FS:
timePlotYAxis->setUnits("");
timePlotYAxis->scaleDraw()->setFloatPrecision(3);
timePlotYAxis->getFormatter()->setTwoDecimalMode(false);
break;
case YMODE_SCALE:
timePlotYAxis->setUnits(m_scaleProviders[0]->unit().symbol);
timePlotYAxis->scaleDraw()->setFloatPrecision(3);
timePlotYAxis->getFormatter()->setTwoDecimalMode(true);
break;
default:
break;
}
timePlotYAxis->scaleDraw()->invalidateCache();
timePlotYAxis->updateAxisScale();
}