summaryrefslogtreecommitdiff
path: root/gr-qtgui/lib/displayform.cc
blob: b8a4b34efb4ddea620e817b4e8ade472e8ad7f8d (plain)
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
/* -*- c++ -*- */
/*
 * Copyright 2012 Free Software Foundation, Inc.
 *
 * This file is part of GNU Radio
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 */

#include <gnuradio/qtgui/displayform.h>

#include <QFileDialog>
#include <QPixmap>
#include <iostream>

DisplayForm::DisplayForm(int nplots, QWidget* parent)
    : QWidget(parent), d_nplots(nplots), d_system_specified_flag(false)
{
    d_isclosed = false;
    d_axislabels = true;

    // Set the initial plot size
    resize(QSize(800, 600));

    // Set up a grid that can be turned on/off
    d_grid = new QwtPlotGrid();
    QPen* gridpen = new QPen(Qt::DashLine);
    gridpen->setWidthF(0.25);
    gridpen->setColor(Qt::gray);
    d_grid->setPen(*gridpen);

    // 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_grid_act = new QAction("Grid", this);
    d_grid_act->setCheckable(true);
    d_grid_act->setStatusTip(tr("Toggle Grid on/off"));
    connect(d_grid_act, SIGNAL(triggered(bool)), this, SLOT(setGrid(bool)));
    d_grid_state = false;

    d_axislabelsmenu = new QAction("Axis Labels", this);
    d_axislabelsmenu->setCheckable(true);
    d_axislabelsmenu->setStatusTip(tr("Toggle Axis Labels on/off"));
    connect(d_axislabelsmenu, SIGNAL(triggered(bool)), this, SLOT(setAxisLabels(bool)));

    // Create a pop-up menu for manipulating the figure
    d_menu_on = true;
    d_menu = new QMenu(this);
    d_menu->addAction(d_stop_act);
    d_menu->addAction(d_grid_act);
    d_menu->addAction(d_axislabelsmenu);

    for (unsigned int i = 0; i < d_nplots; ++i) {
        d_line_title_act.push_back(new LineTitleAction(i, this));
        d_line_color_menu.push_back(new LineColorMenu(i, this));
        d_line_width_menu.push_back(new LineWidthMenu(i, this));
        d_line_style_menu.push_back(new LineStyleMenu(i, this));
        d_line_marker_menu.push_back(new LineMarkerMenu(i, this));
        d_marker_alpha_menu.push_back(new MarkerAlphaMenu(i, this));

        connect(d_line_title_act[i],
                SIGNAL(whichTrigger(unsigned int, const QString&)),
                this,
                SLOT(setLineLabel(unsigned int, const QString&)));

        for (int j = 0; j < d_line_color_menu[i]->getNumActions(); j++) {
            connect(d_line_color_menu[i],
                    SIGNAL(whichTrigger(unsigned int, const QString&)),
                    this,
                    SLOT(setLineColor(unsigned int, const QString&)));
        }

        for (int j = 0; j < d_line_width_menu[i]->getNumActions(); j++) {
            connect(d_line_width_menu[i],
                    SIGNAL(whichTrigger(unsigned int, unsigned int)),
                    this,
                    SLOT(setLineWidth(unsigned int, unsigned int)));
        }

        for (int j = 0; j < d_line_style_menu[i]->getNumActions(); j++) {
            connect(d_line_style_menu[i],
                    SIGNAL(whichTrigger(unsigned int, Qt::PenStyle)),
                    this,
                    SLOT(setLineStyle(unsigned int, Qt::PenStyle)));
        }

        for (int j = 0; j < d_line_marker_menu[i]->getNumActions(); j++) {
            connect(d_line_marker_menu[i],
                    SIGNAL(whichTrigger(unsigned int, QwtSymbol::Style)),
                    this,
                    SLOT(setLineMarker(unsigned int, QwtSymbol::Style)));
        }

        for (int j = 0; j < d_marker_alpha_menu[i]->getNumActions(); j++) {
            connect(d_marker_alpha_menu[i],
                    SIGNAL(whichTrigger(unsigned int, unsigned int)),
                    this,
                    SLOT(setMarkerAlpha(unsigned int, unsigned int)));
        }

        d_lines_menu.push_back(new QMenu(tr(""), this));
        d_lines_menu[i]->addAction(d_line_title_act[i]);
        d_lines_menu[i]->addMenu(d_line_color_menu[i]);
        d_lines_menu[i]->addMenu(d_line_width_menu[i]);
        d_lines_menu[i]->addMenu(d_line_style_menu[i]);
        d_lines_menu[i]->addMenu(d_line_marker_menu[i]);
        d_lines_menu[i]->addMenu(d_marker_alpha_menu[i]);
        d_menu->addMenu(d_lines_menu[i]);
    }

    d_samp_rate_act = new PopupMenu("Sample Rate", this);
    d_samp_rate_act->setStatusTip(tr("Set Sample Rate"));
    connect(d_samp_rate_act,
            SIGNAL(whichTrigger(QString)),
            this,
            SLOT(setSampleRate(QString)));
    d_menu->addAction(d_samp_rate_act);

    d_autoscale_act = new QAction("Auto Scale", this);
    d_autoscale_act->setStatusTip(tr("Autoscale Plot"));
    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_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);

    Reset();
}

DisplayForm::~DisplayForm()
{
    d_isclosed = true;

    // Qt deletes children when parent is deleted
    // Don't worry about deleting Display Plots - they are deleted when parents are
    // deleted
}

void DisplayForm::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_lines_menu[i]->setTitle(d_display_plot->getLineLabel(i));
        }
        d_menu->exec(e->globalPos());
    }
}

void DisplayForm::updateGuiTimer() { d_display_plot->canvas()->update(); }

void DisplayForm::onPlotPointSelected(const QPointF p) { emit plotPointSelected(p, 3); }

void DisplayForm::Reset() {}

bool DisplayForm::isClosed() const { return d_isclosed; }

void DisplayForm::enableMenu(bool en) { d_menu_on = en; }

void DisplayForm::closeEvent(QCloseEvent* e)
{
    d_isclosed = true;
    qApp->processEvents();
    QWidget::closeEvent(e);
}

void DisplayForm::setUpdateTime(double t) { d_update_time = t; }

void DisplayForm::setTitle(const QString& title) { d_display_plot->setTitle(title); }

void DisplayForm::setLineLabel(unsigned int which, const QString& label)
{
    d_display_plot->setLineLabel(which, label);
}

void DisplayForm::setLineColor(unsigned int which, const QString& color)
{
    QColor c = QColor(color);
    d_display_plot->setLineColor(which, c);
    d_display_plot->replot();
}

void DisplayForm::setLineWidth(unsigned int which, unsigned int width)
{
    d_display_plot->setLineWidth(which, width);
    d_display_plot->replot();
}

void DisplayForm::setLineStyle(unsigned int which, Qt::PenStyle style)
{
    d_display_plot->setLineStyle(which, style);
    d_display_plot->replot();
}

void DisplayForm::setLineMarker(unsigned int which, QwtSymbol::Style marker)
{
    d_display_plot->setLineMarker(which, marker);
    d_display_plot->replot();
}

void DisplayForm::setMarkerAlpha(unsigned int which, unsigned int alpha)
{
    d_display_plot->setMarkerAlpha(which, alpha);
    d_display_plot->replot();
}

QString DisplayForm::title() { return d_display_plot->title().text(); }

QString DisplayForm::lineLabel(unsigned int which)
{
    return d_display_plot->getLineLabel(which);
}

QString DisplayForm::lineColor(unsigned int which)
{
    return d_display_plot->getLineColor(which).name();
}

int DisplayForm::lineWidth(unsigned int which)
{
    return d_display_plot->getLineWidth(which);
}

Qt::PenStyle DisplayForm::lineStyle(unsigned int which)
{
    return d_display_plot->getLineStyle(which);
}

QwtSymbol::Style DisplayForm::lineMarker(unsigned int which)
{
    return d_display_plot->getLineMarker(which);
}

int DisplayForm::markerAlpha(unsigned int which)
{
    return d_display_plot->getMarkerAlpha(which);
}

void DisplayForm::setSampleRate(const QString& rate) {}

void DisplayForm::setStop(bool on)
{
    if (!on) {
        // will auto-detach if already attached.
        d_display_plot->setStop(false);
        d_stop_state = false;
    } else {
        d_display_plot->setStop(true);
        d_stop_state = true;
    }
    d_display_plot->replot();
}

void DisplayForm::setStop()
{
    if (d_stop_state == false)
        setStop(true);
    else
        setStop(false);
}

void DisplayForm::setGrid(bool on)
{
    if (on) {
        // will auto-detach if already attached.
        d_grid->attach(d_display_plot);
        d_grid_state = true;
    } else {
        d_grid->detach();
        d_grid_state = false;
    }
    d_grid_act->setChecked(on);
    d_display_plot->replot();
}

void DisplayForm::setAxisLabels(bool en)
{
    d_axislabels = en;
    d_axislabelsmenu->setChecked(en);
    getPlot()->setAxisLabels(d_axislabels);
}

void DisplayForm::saveFigure()
{
    QPixmap qpix = this->grab();

    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 DisplayForm::disableLegend() { d_display_plot->disableLegend(); }