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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
|
/* -*- c++ -*- */
/*
* Copyright 2014 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 <gnuradio/qtgui/numberdisplayform.h>
#include <qwt_color_map.h>
#include <QFileDialog>
#include <QMessageBox>
#include <cmath>
#include <iostream>
NumberDisplayForm::NumberDisplayForm(int nplots, gr::qtgui::graph_t type, QWidget* parent)
: QWidget(parent)
{
d_nplots = nplots;
d_graph_type = type;
d_title = new QLabel(QString(""));
d_layout = new QGridLayout(this);
for (unsigned int i = 0; i < d_nplots; ++i) {
d_min.push_back(+1e32);
d_max.push_back(-1e32);
d_label.push_back(new QLabel(QString("Data %1").arg(i)));
d_unit.emplace_back("");
d_factor.push_back(1);
d_text_box.push_back(new QLabel(QString("0")));
d_indicator.push_back(new QwtThermo());
d_indicator[i]->setScale(-1, 1);
#if QWT_VERSION < 0x060100
#else
d_indicator[i]->setOriginMode(QwtThermo::OriginCustom);
d_indicator[i]->setOrigin(0.0);
#endif /* if QWT_VERSION < 0x060100 */
switch (type) {
case (gr::qtgui::NUM_GRAPH_HORIZ):
#if QWT_VERSION < 0x060100
d_indicator[i]->setOrientation(Qt::Horizontal, QwtThermo::BottomScale);
#else
d_indicator[i]->setOrientation(Qt::Horizontal);
#endif /* if QWT_VERSION < 0x060100 */
d_layout->addWidget(d_label[i], 2 * i, 0);
d_layout->addWidget(d_text_box[i], 2 * i, 1);
d_layout->addWidget(d_indicator[i], 2 * i + 1, 1);
break;
case (gr::qtgui::NUM_GRAPH_VERT):
#if QWT_VERSION < 0x060100
d_indicator[i]->setOrientation(Qt::Vertical, QwtThermo::LeftScale);
#else
d_indicator[i]->setOrientation(Qt::Vertical);
#endif /* if QWT_VERSION < 0x060100 */
d_layout->addWidget(d_label[i], 0, i);
d_layout->addWidget(d_text_box[i], 1, i);
d_layout->addWidget(d_indicator[i], 2, i);
break;
case (gr::qtgui::NUM_GRAPH_NONE):
default:
d_layout->addWidget(d_label[i], 0, i);
d_layout->addWidget(d_text_box[i], 1, i);
}
setColor(i, QColor("black"), QColor("black"));
}
d_avg = 0.0;
d_update_time = 0.1;
d_menu_on = true;
d_menu = new QMenu(this);
// Create a set of actions for the menu
d_stop_act = new QAction("Stop", this);
d_stop_act->setStatusTip(tr("Start/Stop"));
connect(d_stop_act, SIGNAL(triggered()), this, SLOT(setStop()));
d_stop_state = false;
d_menu->addAction(d_stop_act);
// Menu items for each number line
for (unsigned int i = 0; i < d_nplots; ++i) {
d_label_act.push_back(new LineTitleAction(i, this));
connect(d_label_act[i],
SIGNAL(whichTrigger(unsigned int, const QString&)),
this,
SLOT(setLabel(unsigned int, const QString&)));
d_label_menu.push_back(new QMenu(tr(""), this));
d_label_menu[i]->addAction(d_label_act[i]);
d_color_menu.push_back(new NumberColorMapMenu(i, this));
connect(d_color_menu[i],
SIGNAL(whichTrigger(unsigned int, const QColor&, const QColor&)),
this,
SLOT(setColor(unsigned int, const QColor&, const QColor&)));
d_label_menu[i]->addMenu(d_color_menu[i]);
d_factor_act.push_back(new ItemFloatAct(i, "Factor", this));
connect(d_factor_act[i],
SIGNAL(whichTrigger(unsigned int, float)),
this,
SLOT(setFactor(unsigned int, float)));
d_label_menu[i]->addAction(d_factor_act[i]);
d_menu->addMenu(d_label_menu[i]);
}
d_avg_menu = new FFTAverageMenu(this);
d_avg_menu->setTitle("Average");
d_avg_menu->setHigh(0.001);
d_avg_menu->setMedium(0.01);
d_avg_menu->setLow(0.1);
connect(d_avg_menu, SIGNAL(whichTrigger(float)), this, SLOT(setAverage(const float)));
d_menu->addMenu(d_avg_menu);
d_layout_menu = new NumberLayoutMenu(this);
connect(d_layout_menu,
SIGNAL(whichTrigger(gr::qtgui::graph_t)),
this,
SLOT(setGraphType(const gr::qtgui::graph_t)));
d_menu->addMenu(d_layout_menu);
d_autoscale_act = new QAction("Auto Scale", this);
d_autoscale_act->setCheckable(true);
connect(d_autoscale_act, SIGNAL(triggered(bool)), this, SLOT(autoScale(bool)));
d_autoscale_state = false;
d_menu->addAction(d_autoscale_act);
d_update_time_menu = new PopupMenu("Update Time", this);
connect(d_update_time_menu,
SIGNAL(whichTrigger(QString)),
this,
SLOT(setUpdateTime(QString)));
d_menu->addAction(d_update_time_menu);
d_save_act = new QAction("Save", this);
d_save_act->setStatusTip(tr("Save Figure"));
connect(d_save_act, SIGNAL(triggered()), this, SLOT(saveFigure()));
d_menu->addAction(d_save_act);
setLayout(d_layout);
}
NumberDisplayForm::~NumberDisplayForm()
{
// Qt deletes children when parent is deleted
}
void NumberDisplayForm::mousePressEvent(QMouseEvent* e)
{
bool ctrloff = Qt::ControlModifier != QApplication::keyboardModifiers();
if ((e->button() == Qt::MidButton) && ctrloff && (d_menu_on)) {
if (d_stop_state == false)
d_stop_act->setText(tr("Stop"));
else
d_stop_act->setText(tr("Start"));
// Update the line titles if changed externally
for (unsigned int i = 0; i < d_nplots; ++i) {
d_label_menu[i]->setTitle(label(i).c_str());
}
d_menu->exec(e->globalPos());
}
}
void NumberDisplayForm::setStop(bool on)
{
if (!on) {
d_stop_state = false;
} else {
d_stop_state = true;
}
}
void NumberDisplayForm::setStop()
{
if (d_stop_state == false)
setStop(true);
else
setStop(false);
}
void NumberDisplayForm::saveFigure()
{
QPixmap qpix = QPixmap::grabWidget(this);
QString types = QString(tr("JPEG file (*.jpg);;Portable Network Graphics file "
"(*.png);;Bitmap file (*.bmp);;TIFF file (*.tiff)"));
QString filename, filetype;
QFileDialog* filebox = new QFileDialog(0, "Save Image", "./", types);
filebox->setViewMode(QFileDialog::Detail);
if (filebox->exec()) {
filename = filebox->selectedFiles()[0];
filetype = filebox->selectedNameFilter();
} else {
return;
}
if (filetype.contains(".jpg")) {
qpix.save(filename, "JPEG");
} else if (filetype.contains(".png")) {
qpix.save(filename, "PNG");
} else if (filetype.contains(".bmp")) {
qpix.save(filename, "BMP");
} else if (filetype.contains(".tiff")) {
qpix.save(filename, "TIFF");
} else {
qpix.save(filename, "JPEG");
}
delete filebox;
}
void NumberDisplayForm::newData(const QEvent* updateEvent)
{
if (!d_stop_state) {
NumberUpdateEvent* tevent = (NumberUpdateEvent*)updateEvent;
const std::vector<float> samples = tevent->getSamples();
for (unsigned int i = 0; i < d_nplots; ++i) {
float f = d_factor[i] * samples[i];
d_text_box[i]->setText(
QString("%1 %2").arg(f, 4, ' ').arg(QString(d_unit[i].c_str())));
d_indicator[i]->setValue(f);
d_min[i] = std::min(d_min[i], f);
d_max[i] = std::max(d_max[i], f);
if (d_autoscale_state) {
d_indicator[i]->setScale(d_min[i], d_max[i]);
}
}
}
}
void NumberDisplayForm::customEvent(QEvent* e)
{
if (e->type() == NumberUpdateEvent::Type()) {
newData(e);
}
}
void NumberDisplayForm::setGraphType(const gr::qtgui::graph_t type)
{
int off = 0;
// Remove all widgets from the layout
QLayoutItem* item;
while ((item = d_layout->takeAt(0)) != NULL) {
d_layout->removeItem(item);
}
// If we have a title, add it at the 0,0 grid point (top left)
// set off = 1 to offset the rest of the widgets
if (d_title->text().length() > 0) {
d_layout->addWidget(d_title, 0, 0);
off = 1;
}
d_graph_type = type;
for (unsigned int i = 0; i < d_nplots; ++i) {
switch (d_graph_type) {
case (gr::qtgui::NUM_GRAPH_HORIZ):
#if QWT_VERSION < 0x060100
d_indicator[i]->setOrientation(Qt::Horizontal, QwtThermo::BottomScale);
#else
d_indicator[i]->setOrientation(Qt::Horizontal);
#endif /* if QWT_VERSION < 0x060100 */
d_indicator[i]->setVisible(true);
d_layout->addWidget(d_label[i], 2 * i + off, 0);
d_layout->addWidget(d_text_box[i], 2 * i + off, 1);
d_layout->addWidget(d_indicator[i], 2 * i + 1 + off, 1);
break;
case (gr::qtgui::NUM_GRAPH_VERT):
#if QWT_VERSION < 0x060100
d_indicator[i]->setOrientation(Qt::Vertical, QwtThermo::LeftScale);
#else
d_indicator[i]->setOrientation(Qt::Vertical);
#endif /* if QWT_VERSION < 0x060100 */
d_indicator[i]->setVisible(true);
d_layout->addWidget(d_label[i], 0 + off, i);
d_layout->addWidget(d_text_box[i], 1 + off, i);
d_layout->addWidget(d_indicator[i], 2 + off, i);
break;
case (gr::qtgui::NUM_GRAPH_NONE):
default:
d_indicator[i]->setVisible(false);
d_layout->addWidget(d_label[i], 0 + off, i);
d_layout->addWidget(d_text_box[i], 1 + off, i);
break;
}
}
}
void NumberDisplayForm::setColor(unsigned int which, const QColor& min, const QColor& max)
{
QwtLinearColorMap* map = new QwtLinearColorMap();
map->setColorInterval(min, max);
#if QWT_VERSION < 0x060000
d_indicator[which]->setFillColor(max);
#else
d_indicator[which]->setColorMap(map);
#endif /* QWT_VERSION < 0x060000 */
}
void NumberDisplayForm::setColorMin(unsigned int which, QString min)
{
setColor(which, QColor(min), colorMax(which));
}
void NumberDisplayForm::setColorMax(unsigned int which, QString max)
{
setColor(which, colorMin(which), QColor(max));
}
void NumberDisplayForm::setLabel(unsigned int which, const std::string& label)
{
d_label[which]->setText(label.c_str());
}
void NumberDisplayForm::setLabel(unsigned int which, QString label)
{
d_label[which]->setText(label);
}
void NumberDisplayForm::setAverage(const float avg) { d_avg = avg; }
void NumberDisplayForm::setUpdateTime(const float time) { d_update_time = time; }
void NumberDisplayForm::setUpdateTime(QString time) { setUpdateTime(time.toFloat()); }
void NumberDisplayForm::setScale(unsigned int which, int min, int max)
{
d_min[which] = min;
d_max[which] = max;
d_indicator[which]->setScale(min, max);
#if QWT_VERSION < 0x060100
d_indicator[which]->setRange(min, max);
#endif
}
void NumberDisplayForm::setScaleMin(unsigned int which, int min)
{
setScale(which, min, d_max[which]);
}
void NumberDisplayForm::setScaleMax(unsigned int which, int max)
{
setScale(which, d_min[which], max);
}
gr::qtgui::graph_t NumberDisplayForm::graphType() const { return d_graph_type; }
QColor NumberDisplayForm::colorMin(unsigned int which) const
{
#if QWT_VERSION < 0x060000
return d_indicator[which]->fillColor();
#else
QwtLinearColorMap* map =
static_cast<QwtLinearColorMap*>(d_indicator[which]->colorMap());
return map->color1();
#endif /* QWT_VERSION < 0x060000 */
}
QColor NumberDisplayForm::colorMax(unsigned int which) const
{
#if QWT_VERSION < 0x060000
return d_indicator[which]->fillColor();
#else
QwtLinearColorMap* map =
static_cast<QwtLinearColorMap*>(d_indicator[which]->colorMap());
return map->color2();
#endif /* QWT_VERSION < 0x060000 */
}
std::string NumberDisplayForm::label(unsigned int which) const
{
return d_label[which]->text().toStdString();
}
float NumberDisplayForm::average() const { return d_avg; }
float NumberDisplayForm::updateTime() const { return d_update_time; }
int NumberDisplayForm::scaleMin(unsigned int which) { return d_min[which]; }
int NumberDisplayForm::scaleMax(unsigned int which) { return d_max[which]; }
void NumberDisplayForm::autoScale(bool on)
{
d_autoscale_state = on;
d_autoscale_act->setChecked(on);
// Reset the autoscale limits
for (unsigned int i = 0; i < d_nplots; ++i) {
d_min[i] = +1e32;
d_max[i] = -1e32;
}
}
std::string NumberDisplayForm::title() const { return d_title->text().toStdString(); }
void NumberDisplayForm::setTitle(const std::string& title)
{
std::string t = title;
if (t.length() > 0)
t = "<b><FONT SIZE=4>" + title + "</b>";
d_title->setText(QString(t.c_str()));
setGraphType(d_graph_type);
}
std::string NumberDisplayForm::unit(unsigned int which) const
{
if (static_cast<size_t>(which) >= d_unit.size())
throw std::runtime_error("NumberDisplayForm::units: invalid 'which'.");
return d_unit[which];
}
void NumberDisplayForm::setUnit(unsigned int which, const std::string& unit)
{
if (static_cast<size_t>(which) >= d_unit.size())
throw std::runtime_error("NumberDisplayForm::setUnits: invalid 'which'.");
d_unit[which] = unit;
}
float NumberDisplayForm::factor(unsigned int which) const
{
if (static_cast<size_t>(which) >= d_factor.size())
throw std::runtime_error("NumberDisplayForm::factor: invalid 'which'.");
return d_factor[which];
}
void NumberDisplayForm::setFactor(unsigned int which, float factor)
{
if (static_cast<size_t>(which) >= d_factor.size())
throw std::runtime_error("NumberDisplayForm::setFactor: invalid 'which'.");
d_factor[which] = factor;
}
|