-
Notifications
You must be signed in to change notification settings - Fork 181
Expand file tree
/
Copy pathplotwidget.cpp
More file actions
430 lines (356 loc) · 11.6 KB
/
plotwidget.cpp
File metadata and controls
430 lines (356 loc) · 11.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
* 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 "plotwidget.h"
#include "plotaxis.h"
#include "style.h"
#include <QDebug>
#include <QLabel>
#include <QMouseEvent>
#include <QObject>
#include <QwtPlot>
#include <QwtPlotCurve>
#include <QwtPlotLayout>
#include <QwtPlotOpenGLCanvas>
#include <QwtPlotSeriesItem>
#include <plotinfowidgets.h>
#include <plotnavigator.hpp>
#include <plotscales.h>
#include <plottracker.hpp>
#include <QwtPlotCanvas>
#include <qwt_scale_widget.h>
#include <osc_scale_engine.h>
#include <pluginbase/preferences.h>
#include <QPainter>
#include <QwtLegend>
#include <QwtPlotRenderer>
#include <stylehelper.h>
#include <plotbuttonmanager.h>
using namespace scopy;
PlotWidget::PlotWidget(QWidget *parent)
: QWidget(parent)
{
m_selectedChannel = nullptr;
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
m_plot = new QwtPlot(this);
m_plot->canvas()->setContentsMargins(0, 0, 0, 0);
m_layout = new QGridLayout(this);
m_layout->addWidget(m_plot, 1, 0);
m_layout->setSpacing(0);
m_layout->setMargin(0);
setLayout(m_layout);
m_plot->plotLayout()->setAlignCanvasToScales(true);
m_plot->plotLayout()->setCanvasMargin(0);
m_plot->plotLayout()->setSpacing(0);
setupAxes();
setupOpenGLCanvas();
setupNavigator();
setupPlotInfo();
setupPlotScales();
setupPlotButtonManager();
m_plot->canvas()->installEventFilter(this);
Style::setBackgroundColor(m_plot->canvas(), json::theme::background_plot, true);
}
void PlotWidget::setupNavigator()
{
m_navigator = new PlotNavigator(this);
connect(m_navigator, &PlotNavigator::rectChanged, this, &PlotWidget::plotScaleChanged);
m_tracker = new PlotTracker(this);
}
PlotInfo *PlotWidget::getPlotInfo() { return m_plotInfo; }
PlotWidget::~PlotWidget() {}
PlotAxis *PlotWidget::plotAxisFromId(QwtAxisId axisId)
{
for(QList<PlotAxis *> axes : m_plotAxis) {
for(PlotAxis *axis : axes) {
if(axis->axisId() == axisId) {
return axis;
}
}
}
return nullptr;
}
PlotNavigator *PlotWidget::navigator() const { return m_navigator; }
PlotTracker *PlotWidget::tracker() const { return m_tracker; }
PlotScales *PlotWidget::scales() const { return m_plotScales; }
void PlotWidget::setupOpenGLCanvas()
{
bool useOpenGLCanvas = Preferences::GetInstance()->get("general_use_opengl").toBool();
if(useOpenGLCanvas) {
QwtPlotOpenGLCanvas *plotCanvas = qobject_cast<QwtPlotOpenGLCanvas *>(m_plot->canvas());
if(plotCanvas == NULL) {
plotCanvas = new QwtPlotOpenGLCanvas(m_plot);
plotCanvas->setPaintAttribute(QwtPlotAbstractGLCanvas::BackingStore);
m_plot->setCanvas(plotCanvas);
} else {
;
}
} else {
QwtPlotCanvas *plotCanvas = qobject_cast<QwtPlotCanvas *>(m_plot->canvas());
plotCanvas->setPaintAttribute(QwtPlotCanvas::BackingStore, true);
}
}
void PlotWidget::plotChannelChangeXAxis(PlotChannel *c, PlotAxis *x)
{
m_navigator->removeChannel(c);
m_tracker->removeChannel(c);
c->xAxis()->setVisible(false);
c->setXAxis(x);
m_navigator->addChannel(c);
m_tracker->addChannel(c);
showAxisLabels();
}
void PlotWidget::plotChannelChangeYAxis(PlotChannel *c, PlotAxis *y)
{
m_navigator->removeChannel(c);
m_tracker->removeChannel(c);
c->yAxis()->setVisible(false);
c->setYAxis(y);
m_navigator->addChannel(c);
m_tracker->addChannel(c);
showAxisLabels();
Q_EMIT channelSelected(c);
}
void PlotWidget::addPlotChannel(PlotChannel *ch)
{
ch->init();
m_plotChannels.append(ch);
if(m_selectedChannel == nullptr) {
selectChannel(ch);
}
connect(ch, &PlotChannel::doReplot, this, &PlotWidget::replot);
connect(ch, &PlotChannel::attachCurve, this, [=](QwtPlotCurve *curve) { curve->attach(m_plot); });
m_navigator->addChannel(ch);
m_tracker->addChannel(ch);
Q_EMIT addedChannel(ch);
}
void PlotWidget::removePlotChannel(PlotChannel *ch)
{
m_plotChannels.removeAll(ch);
m_navigator->removeChannel(ch);
m_tracker->removeChannel(ch);
// QwtAxisId cannot be removed from QwtPlot
ch->yAxis()->setVisible(false);
if(m_selectedChannel == ch) {
if(m_plotChannels.size() > 0) {
selectChannel(m_plotChannels[0]);
} else {
selectChannel(nullptr);
}
}
ch->deinit();
Q_EMIT removedChannel(ch);
}
QList<PlotChannel *> PlotWidget::getChannels() { return m_plotChannels; }
void PlotWidget::addPlotAxisHandle(PlotAxisHandle *ax) { m_plotAxisHandles[ax->axis()->position()].append(ax); }
void PlotWidget::removePlotAxisHandle(PlotAxisHandle *ax) { m_plotAxisHandles[ax->axis()->position()].removeAll(ax); }
void PlotWidget::addPlotAxis(PlotAxis *ax) { m_plotAxis[ax->position()].append(ax); }
bool PlotWidget::eventFilter(QObject *object, QEvent *event)
{
if(object == m_plot->canvas()) {
switch(event->type()) {
case QEvent::MouseMove: {
Q_EMIT mouseMove(static_cast<QMouseEvent *>(event));
break;
}
case QEvent::MouseButtonPress: {
Q_EMIT mouseButtonPress(static_cast<QMouseEvent *>(event));
break;
}
case QEvent::MouseButtonRelease: {
Q_EMIT mouseButtonRelease(static_cast<QMouseEvent *>(event));
break;
}
case QEvent::Resize: {
Q_EMIT canvasSizeChanged();
break;
}
default:
break;
}
}
return QObject::eventFilter(object, event);
}
QList<PlotAxis *> &PlotWidget::plotAxis(int position) { return m_plotAxis[position]; }
PlotAxis *PlotWidget::xAxis() { return m_xAxis; }
PlotAxis *PlotWidget::yAxis() { return m_yAxis; }
QwtPlot *PlotWidget::plot() const { return m_plot; }
QGridLayout *PlotWidget::layout() { return m_layout; }
void PlotWidget::replot() { m_plot->replot(); }
void PlotWidget::hideAxisLabels()
{
m_yAxis->setVisible(false);
m_xAxis->setVisible(false);
if(m_selectedChannel != nullptr) {
m_selectedChannel->yAxis()->setVisible(false);
m_selectedChannel->xAxis()->setVisible(false);
}
}
void PlotWidget::setAlignCanvasToScales(bool alignCanvasToScales)
{
m_plot->plotLayout()->setAlignCanvasToScales(alignCanvasToScales);
}
PlotButtonManager *PlotWidget::plotButtonManager() const { return m_plotButtonManager; }
void PlotWidget::setupPlotInfo()
{
m_plotInfo = new PlotInfo(m_plot->canvas());
m_plotInfo->addCustomInfo(new FPSInfo(this, this), InfoPosition::IP_LEFT);
HDivInfo *hDivInfo = new HDivInfo(this);
m_plotInfo->addCustomInfo(hDivInfo, InfoPosition::IP_LEFT);
if(!Preferences::GetInstance()->get("show_grid").toBool()) {
hDivInfo->hide();
}
connect(Preferences::GetInstance(), &Preferences::preferenceChanged, this,
[=](QString preference, QVariant value) {
if(preference == "show_grid") {
hDivInfo->setVisible(value.toBool());
}
});
}
void PlotWidget::setupPlotScales() { m_plotScales = new PlotScales(this); }
void PlotWidget::setupAxes()
{
// this is needed to hide default QwtPlot axes
m_plot->setAxisVisible(QwtAxis::YLeft, false);
m_plot->setAxisVisible(QwtAxis::XBottom, false);
m_xPosition = Preferences::get("adc_plot_xaxis_label_position").toInt();
m_yPosition = Preferences::get("adc_plot_yaxis_label_position").toInt();
QPen pen(QColor(Style::getAttribute(json::theme::content_subtle)));
m_xAxis = new PlotAxis(m_xPosition, this, pen, this);
m_yAxis = new PlotAxis(m_yPosition, this, pen, this);
}
void PlotWidget::setupPlotButtonManager()
{
m_plotButtonManager = new PlotButtonManager(this);
m_plotButtonManager->setCollapseOrientation(PlotButtonManager::PBM_RIGHT);
HoverWidget *hoverPlotButtonManager = new HoverWidget(m_plotButtonManager, m_plot->canvas(), m_plot->canvas());
hoverPlotButtonManager->setAnchorPos(HoverPosition::HP_BOTTOMRIGHT);
hoverPlotButtonManager->setContentPos(HoverPosition::HP_TOPLEFT);
hoverPlotButtonManager->setAnchorOffset(QPoint(0, -20));
hoverPlotButtonManager->setRelative(true);
hoverPlotButtonManager->show();
}
QwtSymbol::Style PlotWidget::getCurveStyle(int i)
{
if(i == 0)
return QwtSymbol::Ellipse;
if(i == 1)
return QwtSymbol::Rect;
if(i == 2)
return QwtSymbol::Diamond;
if(i == 3)
return QwtSymbol::Triangle;
if(i == 4)
return QwtSymbol::Cross;
if(i == 5)
return QwtSymbol::XCross;
if(i == 6)
return QwtSymbol::Star1;
if(i == 7)
return QwtSymbol::Star2;
if(i == 8)
return QwtSymbol::Hexagon;
if(i == 9)
return QwtSymbol::DTriangle;
if(i == 10)
return QwtSymbol::UTriangle;
if(i == 11)
return QwtSymbol::LTriangle;
return QwtSymbol::RTriangle;
}
bool PlotWidget::showYAxisLabels() const { return m_showYAxisLabels; }
void PlotWidget::setShowYAxisLabels(bool newShowYAxisLabels) { m_showYAxisLabels = newShowYAxisLabels; }
bool PlotWidget::showXAxisLabels() const { return m_showXAxisLabels; }
void PlotWidget::setShowXAxisLabels(bool newShowXAxisLabels) { m_showXAxisLabels = newShowXAxisLabels; }
void PlotWidget::showAxisLabels()
{
if(m_selectedChannel != nullptr) {
m_selectedChannel->xAxis()->setVisible(m_showXAxisLabels);
m_selectedChannel->yAxis()->setVisible(m_showYAxisLabels);
} else {
xAxis()->setVisible(m_showXAxisLabels);
yAxis()->setVisible(m_showXAxisLabels);
}
}
void PlotWidget::selectChannel(PlotChannel *ch)
{
hideAxisLabels();
m_selectedChannel = ch;
showAxisLabels();
if(m_selectedChannel != nullptr) {
if(m_selectedChannel->isEnabled()) {
m_selectedChannel->raise();
}
}
// return;
Q_EMIT channelSelected(m_selectedChannel);
}
void PlotWidget::setUnitsVisible(bool visible)
{
for(PlotChannel *ch : getChannels()) {
ch->xAxis()->setUnitsVisible(visible);
ch->yAxis()->setUnitsVisible(visible);
}
}
void PlotWidget::printPlot(QPainter *painter, bool useSymbols)
{
QwtPlotRenderer renderer;
renderer.setDiscardFlag(QwtPlotRenderer::DiscardBackground, true);
renderer.setDiscardFlag(QwtPlotRenderer::DiscardCanvasBackground, true);
// Change axis and legend color
auto currentStyle = m_plot->styleSheet();
m_plot->setStyleSheet("color: #9E9E9F");
// Apply printable plot changes
// list of current curve colors
QList<QPen> plotChColors;
for(int i = 0; i < getChannels().length(); i++) {
QPen printPen = getChannels().at(i)->curve()->pen();
// save current curve color
plotChColors.push_back(getChannels().at(i)->curve()->pen());
// get channel colors from StyleHelper
printPen.setColor(StyleHelper::getChannelColor(i));
printPen.setWidth(2);
getChannels().at(i)->curve()->setPen(printPen);
if(useSymbols) {
QwtSymbol *symbol = new QwtSymbol(getCurveStyle(i), Qt::white, printPen, QSize(10, 10));
getChannels().at(i)->curve()->setSymbol(symbol);
}
}
QwtLegend *legendDisplay = new QwtLegend(m_plot);
legendDisplay->setDefaultItemMode(QwtLegendData::ReadOnly);
m_plot->insertLegend(legendDisplay, QwtPlot::TopLegend);
m_plot->updateLegend();
m_plot->replot();
// Print plot to Painter
renderer.render(m_plot, painter, QRectF(0, 0, 400, 300));
// revert changes made for printable plot
m_plot->insertLegend(nullptr);
m_plot->setStyleSheet(currentStyle);
// revert curves to original settings
for(int i = 0; i < getChannels().length(); i++) {
getChannels().at(i)->curve()->setPen(plotChColors.at(i));
if(useSymbols) {
getChannels().at(i)->curve()->setSymbol(new QwtSymbol(QwtSymbol::NoSymbol));
}
}
m_plot->replot();
}
PlotChannel *PlotWidget::selectedChannel() const { return m_selectedChannel; }
#include "moc_plotwidget.cpp"