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

#include <gnuradio/qtgui/eyedisplaysform.h>

#include <QFileDialog>
#include <QPixmap>

EyeDisplaysForm::EyeDisplaysForm(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
    QPen* gridpen = new QPen(Qt::DashLine);
    gridpen->setWidthF(1.0);
    gridpen->setColor(Qt::gray);

    // Each eye must have one grid attached
    for (unsigned int i = 0; i < d_nplots; ++i) {
        d_grids.push_back(new QwtPlotGrid());
        d_grids[i]->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();
}

EyeDisplaysForm::~EyeDisplaysForm()
{
    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 EyeDisplaysForm::resizeEvent(QResizeEvent* e)
{
    // not used by eye_sink
}

void EyeDisplaysForm::mousePressEvent(QMouseEvent* e)
{
    bool ctrloff = Qt::ControlModifier != QApplication::keyboardModifiers();
    if ((e->button() == Qt::MiddleButton) && 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_displays_plot[i]->title().text());
        }
        d_menu->exec(e->globalPos());
    }
}

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

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

void EyeDisplaysForm::Reset() {}

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

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

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

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

void EyeDisplaysForm::setSamplesPerSymbol(int64_t samples_per_symbol)
{
    d_sps = (int)samples_per_symbol;
}

void EyeDisplaysForm::setTitle(const QString& title)
{
    /* Used by parent class, do not remove */
}

void EyeDisplaysForm::setLineLabel(unsigned int which, const QString& label)
{
    // Line label used as eye pattern title
    d_displays_plot[which]->setTitle(label);
}

void EyeDisplaysForm::setLineColor(unsigned int which, const QString& color)
{
    QColor c = QColor(color);
    d_displays_plot[which]->setLineColor(0, c);
    d_displays_plot[which]->replot();
}

void EyeDisplaysForm::setLineWidth(unsigned int which, unsigned int width)
{
    d_displays_plot[which]->setLineWidth(0, width);
    d_displays_plot[which]->replot();
}

void EyeDisplaysForm::setLineStyle(unsigned int which, Qt::PenStyle style)
{
    d_displays_plot[which]->setLineStyle(0, style);
    d_displays_plot[which]->replot();
}

void EyeDisplaysForm::setLineMarker(unsigned int which, QwtSymbol::Style marker)
{
    d_displays_plot[which]->setLineMarker(0, marker);
    d_displays_plot[which]->replot();
}

void EyeDisplaysForm::setMarkerAlpha(unsigned int which, unsigned int alpha)
{
    d_displays_plot[which]->setMarkerAlpha(0, alpha);
    d_displays_plot[which]->replot();
}

QString EyeDisplaysForm::title()
{
    /* Title unused by eye_sink */
    return "";
}

QString EyeDisplaysForm::lineLabel(unsigned int which)
{
    return d_displays_plot[which]->title().text();
}

QString EyeDisplaysForm::lineColor(unsigned int which)
{
    return d_displays_plot[which]->getLineColor(0).name();
}

int EyeDisplaysForm::lineWidth(unsigned int which)
{
    return d_displays_plot[which]->getLineWidth(0);
}

Qt::PenStyle EyeDisplaysForm::lineStyle(unsigned int which)
{
    return d_displays_plot[which]->getLineStyle(0);
}

QwtSymbol::Style EyeDisplaysForm::lineMarker(unsigned int which)
{
    return d_displays_plot[which]->getLineMarker(0);
}

int EyeDisplaysForm::markerAlpha(unsigned int which)
{
    return d_displays_plot[which]->getMarkerAlpha(0);
}

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

void EyeDisplaysForm::setStop(bool on)
{
    for (unsigned int i = 0; i < d_nplots; ++i) {
        if (!on) {
            // will auto-detach if already attached.
            d_displays_plot[i]->setStop(false);
            d_stop_state = false;
        } else {
            d_displays_plot[i]->setStop(true);
            d_stop_state = true;
        }
        d_displays_plot[i]->replot();
    }
}

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

void EyeDisplaysForm::setGrid(bool on)
{
    if (on) {
        d_grid_state = true;
    } else {
        d_grid_state = false;
    }

    // create one grid per eye pattern
    for (unsigned int i = 0; i < d_nplots; ++i) {
        if (on) {
            // will auto-detach if already attached.
            d_grids[i]->attach(d_displays_plot[i]);
        } else {
            d_grids[i]->detach();
        }
        d_grid_act->setChecked(on);
        d_displays_plot[i]->replot();
    }
}

void EyeDisplaysForm::setAxisLabels(bool en)
{
    d_axislabels = en;
    d_axislabelsmenu->setChecked(en);

    for (unsigned int i = 0; i < d_nplots; ++i) {
        d_displays_plot[i]->setAxisLabels(d_axislabels);
    }
}

void EyeDisplaysForm::saveFigure()
{
    QPixmap qpix = 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 EyeDisplaysForm::disableLegend()
{
    for (unsigned int i = 0; i < d_nplots; ++i) {
        d_displays_plot[i]->disableLegend();
    }
}