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
|
/* -*- c++ -*- */
/*
* Copyright 2012 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
* GNU Radio 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, or (at your option)
* any later version.
*
* GNU Radio 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 GNU Radio; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#include <DisplayPlot.h>
#include <qwt_scale_draw.h>
#include <qwt_legend.h>
#include <QColor>
#include <cmath>
#include <iostream>
#include <QDebug>
DisplayPlot::DisplayPlot(int nplots, QWidget* parent)
: QwtPlot(parent), _nplots(nplots), _stop(false)
{
qRegisterMetaType<QColorList>("QColorList");
resize(parent->width(), parent->height());
// Disable polygon clipping
#if QWT_VERSION < 0x060000
QwtPainter::setDeviceClipping(false);
#else
QwtPainter::setPolylineSplitting(false);
#endif
#if QWT_VERSION < 0x060000
// We don't need the cache here
canvas()->setPaintAttribute(QwtPlotCanvas::PaintCached, false);
canvas()->setPaintAttribute(QwtPlotCanvas::PaintPacked, false);
#endif
QColor default_palette_color = QColor("white");
setPaletteColor(default_palette_color);
_panner = new QwtPlotPanner(canvas());
_panner->setAxisEnabled(QwtPlot::yRight, false);
_panner->setMouseButton(Qt::MidButton);
// emit the position of clicks on widget
_picker = new QwtDblClickPlotPicker(canvas());
#if QWT_VERSION < 0x060000
connect(_picker, SIGNAL(selected(const QwtDoublePoint &)),
this, SLOT(OnPickerPointSelected(const QwtDoublePoint &)));
#else
_picker->setStateMachine(new QwtPickerDblClickPointMachine());
connect(_picker, SIGNAL(selected(const QPointF &)),
this, SLOT(OnPickerPointSelected6(const QPointF &)));
#endif
// Configure magnify on mouse wheel
_magnifier = new QwtPlotMagnifier(canvas());
_magnifier->setAxisEnabled(QwtPlot::xBottom, false);
// Avoid jumping when labels with more/less digits
// appear/disappear when scrolling vertically
const QFontMetrics fm(axisWidget(QwtPlot::yLeft)->font());
QwtScaleDraw *sd = axisScaleDraw(QwtPlot::yLeft);
sd->setMinimumExtent( fm.width("100.00") );
QwtLegend* legendDisplay = new QwtLegend(this);
legendDisplay->setItemMode(QwtLegend::CheckableItem);
insertLegend(legendDisplay);
connect(this, SIGNAL(legendChecked(QwtPlotItem *, bool)),
this, SLOT(LegendEntryChecked(QwtPlotItem *, bool)));
}
DisplayPlot::~DisplayPlot()
{
// _zoomer and _panner deleted when parent deleted
}
void
DisplayPlot::setYaxis(double min, double max)
{
setAxisScale(QwtPlot::yLeft, min, max);
_zoomer->setZoomBase();
}
void
DisplayPlot::setXaxis(double min, double max)
{
setAxisScale(QwtPlot::xBottom, min, max);
_zoomer->setZoomBase();
}
void
DisplayPlot::setLineLabel(int which, QString label)
{
_plot_curve[which]->setTitle(label);
}
QString
DisplayPlot::lineLabel(int which)
{
return _plot_curve[which]->title().text();
}
void
DisplayPlot::setLineColor(int which, QString color)
{
if (which < _nplots) {
// Set the color of the pen
QPen pen(_plot_curve[which]->pen());
pen.setColor(color);
_plot_curve[which]->setPen(pen);
// And set the color of the markers
#if QWT_VERSION < 0x060000
//_plot_curve[which]->setBrush(QBrush(QColor(color)));
_plot_curve[which]->setPen(pen);
QwtSymbol sym = (QwtSymbol)_plot_curve[which]->symbol();
setLineMarker(which, sym.style());
#else
QwtSymbol *sym = (QwtSymbol*)_plot_curve[which]->symbol();
if(sym) {
sym->setColor(color);
sym->setPen(pen);
_plot_curve[which]->setSymbol(sym);
}
#endif
}
}
QColor
DisplayPlot::getLineColor(int which) const {
// If that plot doesn't exist then return black.
if (which < _nplots)
return _plot_curve[which]->pen().color();
return QColor("black");
}
// Use a preprocessor macro to create a bunch of hooks for Q_PROPERTY and hence the stylesheet.
#define SETUPLINE(i, im1) \
void DisplayPlot::setLineColor ## i (QColor c) {setColor(im1, c);} \
const QColor DisplayPlot::getLineColor ## i () const {return getLineColor(im1);} \
void DisplayPlot::setLineWidth ## i (int width) {setLineWidth(im1, width);} \
int DisplayPlot::getLineWidth ## i () const {return getLineWidth(im1);} \
void DisplayPlot::setLineStyle ## i (Qt::PenStyle ps) {setLineStyle(im1, ps);} \
const Qt::PenStyle DisplayPlot::getLineStyle ## i () const {return getLineStyle(im1);} \
void DisplayPlot::setLineMarker ## i (QwtSymbol::Style ms) {setLineMarker(im1, ms);} \
const QwtSymbol::Style DisplayPlot::getLineMarker ## i () const {return getLineMarker(im1);} \
void DisplayPlot::setMarkerAlpha ## i (int alpha) {setMarkerAlpha(im1, alpha);} \
int DisplayPlot::getMarkerAlpha ## i () const {return getMarkerAlpha(im1);}
SETUPLINE(1, 0)
SETUPLINE(2, 1)
SETUPLINE(3, 2)
SETUPLINE(4, 3)
SETUPLINE(5, 4)
SETUPLINE(6, 5)
SETUPLINE(7, 6)
SETUPLINE(8, 7)
SETUPLINE(9, 8)
void
DisplayPlot::setZoomerColor(QColor c) {
_zoomer->setRubberBandPen(c);
_zoomer->setTrackerPen(c);
}
QColor
DisplayPlot::getZoomerColor() const {
return _zoomer->rubberBandPen().color();
}
void
DisplayPlot::setPaletteColor(QColor c) {
QPalette palette;
palette.setColor(canvas()->backgroundRole(), c);
canvas()->setPalette(palette);
}
QColor
DisplayPlot::getPaletteColor() const {
return canvas()->palette().color(canvas()->backgroundRole());
}
void
DisplayPlot::setAxisLabelFontSize(int axisId, int fs) {
QwtText axis_title = QwtText(axisWidget(axisId)->title());
QFont font = QFont(axis_title.font());
font.setPointSize(fs);
axis_title.setFont(font);
axisWidget(axisId)->setTitle(axis_title);
}
int
DisplayPlot::getAxisLabelFontSize(int axisId) const {
return axisWidget(axisId)->title().font().pointSize();
}
void
DisplayPlot::setYaxisLabelFontSize(int fs) {
setAxisLabelFontSize(QwtPlot::yLeft, fs);
}
int
DisplayPlot::getYaxisLabelFontSize() const {
int fs = getAxisLabelFontSize(QwtPlot::yLeft);
return fs;
}
void
DisplayPlot::setXaxisLabelFontSize(int fs) {
setAxisLabelFontSize(QwtPlot::xBottom, fs);
}
int
DisplayPlot::getXaxisLabelFontSize() const {
int fs = getAxisLabelFontSize(QwtPlot::xBottom);
return fs;
}
void
DisplayPlot::setAxesLabelFontSize(int fs) {
setAxisLabelFontSize(QwtPlot::yLeft, fs);
setAxisLabelFontSize(QwtPlot::xBottom, fs);
}
int
DisplayPlot::getAxesLabelFontSize() const {
// Returns 0 if all axes do not have the same font size.
int fs = getAxisLabelFontSize(QwtPlot::yLeft);
if (getAxisLabelFontSize(QwtPlot::xBottom) != fs)
return 0;
return fs;
}
void
DisplayPlot::setLineWidth(int which, int width)
{
if (which < _nplots) {
// Set the new line width
QPen pen(_plot_curve[which]->pen());
pen.setWidth(width);
_plot_curve[which]->setPen(pen);
// Scale the marker size proportionally
#if QWT_VERSION < 0x060000
QwtSymbol sym = (QwtSymbol)_plot_curve[which]->symbol();
sym.setSize(7+10*log10(1.0*width), 7+10*log10(1.0*width));
_plot_curve[which]->setSymbol(sym);
#else
QwtSymbol *sym = (QwtSymbol*)_plot_curve[which]->symbol();
if(sym) {
sym->setSize(7+10*log10(1.0*width), 7+10*log10(1.0*width));
_plot_curve[which]->setSymbol(sym);
}
#endif
}
}
int
DisplayPlot::getLineWidth(int which) const {
if (which < _nplots) {
return _plot_curve[which]->pen().width();
} else {
return 0;
}
}
void
DisplayPlot::setLineStyle(int which, Qt::PenStyle style)
{
if (which < _nplots) {
QPen pen(_plot_curve[which]->pen());
pen.setStyle(style);
_plot_curve[which]->setPen(pen);
}
}
const Qt::PenStyle
DisplayPlot::getLineStyle(int which) const
{
if (which < _nplots) {
return _plot_curve[which]->pen().style();
} else {
return Qt::SolidLine;
}
}
void
DisplayPlot::setLineMarker(int which, QwtSymbol::Style marker)
{
if (which < _nplots) {
#if QWT_VERSION < 0x060000
QwtSymbol sym = (QwtSymbol)_plot_curve[which]->symbol();
QPen pen(_plot_curve[which]->pen());
QBrush brush(pen.color());
sym.setStyle(marker);
sym.setPen(pen);
sym.setBrush(brush);
_plot_curve[which]->setSymbol(sym);
#else
QwtSymbol *sym = (QwtSymbol*)_plot_curve[which]->symbol();
if(sym) {
sym->setStyle(marker);
_plot_curve[which]->setSymbol(sym);
}
#endif
}
}
const QwtSymbol::Style
DisplayPlot::getLineMarker(int which) const
{
if(which < _nplots) {
#if QWT_VERSION < 0x060000
QwtSymbol sym = (QwtSymbol)_plot_curve[which]->symbol();
return sym.style();
#else
QwtSymbol *sym = (QwtSymbol*)_plot_curve[which]->symbol();
return sym->style();
#endif
}
else {
return QwtSymbol::NoSymbol;
}
}
void
DisplayPlot::setMarkerAlpha(int which, int alpha)
{
if (which < _nplots) {
// Get the pen color
QPen pen(_plot_curve[which]->pen());
QColor color = pen.color();
// Set new alpha and update pen
color.setAlpha(alpha);
pen.setColor(color);
_plot_curve[which]->setPen(pen);
// And set the new color for the markers
#if QWT_VERSION < 0x060000
QwtSymbol sym = (QwtSymbol)_plot_curve[which]->symbol();
setLineMarker(which, sym.style());
#else
QwtSymbol *sym = (QwtSymbol*)_plot_curve[which]->symbol();
if(sym) {
sym->setColor(color);
sym->setPen(pen);
_plot_curve[which]->setSymbol(sym);
}
#endif
}
}
int
DisplayPlot::getMarkerAlpha(int which) const
{
if (which < _nplots) {
return _plot_curve[which]->pen().color().alpha();
} else {
return 0;
}
}
void
DisplayPlot::setStop(bool on)
{
_stop = on;
}
void
DisplayPlot::resizeSlot( QSize *s )
{
// -10 is to spare some room for the legend and x-axis label
resize(s->width()-10, s->height()-10);
}
void DisplayPlot::LegendEntryChecked(QwtPlotItem* plotItem, bool on)
{
plotItem->setVisible(!on);
replot();
}
void
DisplayPlot::OnPickerPointSelected(const QwtDoublePoint & p)
{
QPointF point = p;
//fprintf(stderr,"OnPickerPointSelected %f %f\n", point.x(), point.y());
emit plotPointSelected(point);
}
void
DisplayPlot::OnPickerPointSelected6(const QPointF & p)
{
QPointF point = p;
//fprintf(stderr,"OnPickerPointSelected %f %f\n", point.x(), point.y());
emit plotPointSelected(point);
}
|