Repository navigation
Expand file tree
/
Copy pathplotview.cpp
More file actions
259 lines (221 loc) · 8.1 KB
/
Copy pathplotview.cpp
File metadata and controls
259 lines (221 loc) · 8.1 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
/*
* Copyright (C) 2015-2016, Mike Walters <mike@flomp.net>
*
* This file is part of inspectrum.
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "plotview.h"
#include <QApplication>
#include <QDebug>
#include <QPainter>
#include <QScrollBar>
#include <gnuradio/top_block.h>
#include <gnuradio/analog/quadrature_demod_cf.h>
#include <gnuradio/blocks/multiply_const_cc.h>
#include <gnuradio/filter/firdes.h>
#include <gnuradio/filter/freq_xlating_fir_filter_ccf.h>
#include "grsamplebuffer.h"
#include "memory_sink.h"
#include "memory_source.h"
PlotView::PlotView(InputSource *input) : cursors(this), viewRange({0, 0})
{
mainSampleSource = input;
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
enableCursors(false);
viewport()->installEventFilter(&cursors);
connect(&cursors, SIGNAL(cursorsMoved()), this, SLOT(cursorsMoved()));
spectrogramPlot = new SpectrogramPlot(mainSampleSource);
plots.emplace_back(spectrogramPlot);
mainSampleSource->subscribe(this);
}
TracePlot* PlotView::createIQPlot(SampleSource<std::complex<float>> *src)
{
gr::top_block_sptr iq_tb = gr::make_top_block("multiply");
auto iq_mem_source = gr::blocks::memory_source::make(8);
auto iq_mem_sink = gr::blocks::memory_sink::make(8);
auto multiply = gr::blocks::multiply_const_cc::make(20);
if (selection || true) {
float centre = -0.05; //(selectionFreq.first + selectionFreq.second) / 2;
float cutoff = 0.02; //std::abs(selectionFreq.first - centre);
auto lp_taps = gr::filter::firdes::low_pass(1.0, 1.0, cutoff, cutoff / 2);
auto filter = gr::filter::freq_xlating_fir_filter_ccf::make(1, lp_taps, centre, 1.0);
iq_tb->connect(iq_mem_source, 0, filter, 0);
iq_tb->connect(filter, 0, multiply, 0);
iq_tb->connect(multiply, 0, iq_mem_sink, 0);
} else {
iq_tb->connect(iq_mem_source, 0, multiply, 0);
iq_tb->connect(multiply, 0, iq_mem_sink, 0);
}
auto iq_src = std::make_shared<GRSampleBuffer<std::complex<float>, std::complex<float>>>(mainSampleSource, iq_tb, iq_mem_source, iq_mem_sink);
return new TracePlot(iq_src);
}
TracePlot* PlotView::createQuadratureDemodPlot(SampleSource<std::complex<float>> *src)
{
gr::top_block_sptr quad_demod_tb = gr::make_top_block("quad_demod");
auto quad_demod_mem_source = gr::blocks::memory_source::make(8);
auto quad_demod_mem_sink = gr::blocks::memory_sink::make(4);
auto quad_demod = gr::analog::quadrature_demod_cf::make(5);
quad_demod_tb->connect(quad_demod_mem_source, 0, quad_demod, 0);
quad_demod_tb->connect(quad_demod, 0, quad_demod_mem_sink, 0);
return new TracePlot(
std::make_shared<GRSampleBuffer<std::complex<float>, float>>(
dynamic_cast<SampleSource<std::complex<float>>*>(src), quad_demod_tb, quad_demod_mem_source, quad_demod_mem_sink
)
);
}
void PlotView::cursorsMoved()
{
selectedSamples = {
horizontalScrollBar()->value() + cursors.selection().minimum * samplesPerLine(),
horizontalScrollBar()->value() + cursors.selection().maximum * samplesPerLine()
};
off_t sampleCount = selectedSamples.length();
float selectionTime = sampleCount / (float)mainSampleSource->rate();
emit timeSelectionChanged(selectionTime);
viewport()->update();
}
void PlotView::enableCursors(bool enabled)
{
cursorsEnabled = enabled;
if (enabled) {
int margin = viewport()->rect().width() / 3;
cursors.setSelection({viewport()->rect().left() + margin, viewport()->rect().right() - margin});
}
viewport()->update();
}
bool PlotView::eventFilter(QObject * obj, QEvent *event)
{
if (event->type() == QEvent::Wheel) {
QWheelEvent *wheelEvent = (QWheelEvent*)event;
if (QApplication::keyboardModifiers() & Qt::ControlModifier) {
if (wheelEvent->angleDelta().y() > 0) {
emit zoomIn();
} else if (wheelEvent->angleDelta().y() < 0) {
emit zoomOut();
}
return true;
}
}
return false;
}
void PlotView::invalidateEvent()
{
horizontalScrollBar()->setMinimum(0);
horizontalScrollBar()->setMaximum(mainSampleSource->count());
}
void PlotView::setCursorBits(int bits)
{
cursors.setBits(bits);
cursorsMoved();
viewport()->update();
}
void PlotView::setFFTAndZoom(int size, int zoom)
{
// Set new FFT size
fftSize = size;
if (spectrogramPlot != nullptr)
spectrogramPlot->setFFTSize(size);
// Set new zoom level
zoomLevel = zoom;
if (spectrogramPlot != nullptr)
spectrogramPlot->setZoomLevel(zoom);
// Update horizontal (time) scrollbar
horizontalScrollBar()->setSingleStep(size * 10 / zoomLevel);
horizontalScrollBar()->setPageStep(size * 100 / zoomLevel);
updateView(true);
}
void PlotView::setPowerMin(int power)
{
powerMin = power;
if (spectrogramPlot != nullptr)
spectrogramPlot->setPowerMin(power);
updateView();
}
void PlotView::setPowerMax(int power)
{
powerMax = power;
if (spectrogramPlot != nullptr)
spectrogramPlot->setPowerMax(power);
updateView();
}
void PlotView::paintEvent(QPaintEvent *event)
{
if (mainSampleSource == nullptr) return;
QRect rect = QRect(0, 0, width(), height());
QPainter painter(viewport());
painter.fillRect(rect, Qt::black);
#define PLOT_LAYER(paintFunc) \
{ \
int y = -verticalScrollBar()->value(); \
for (auto&& plot : plots) { \
QRect rect = QRect(0, y, width(), plot->height()); \
plot->paintFunc(painter, rect, viewRange); \
y += plot->height(); \
} \
}
PLOT_LAYER(paintBack);
PLOT_LAYER(paintMid);
PLOT_LAYER(paintFront);
if (cursorsEnabled)
cursors.paintFront(painter, rect, viewRange);
#undef PLOT_LAYER
}
int PlotView::plotsHeight()
{
int height = 0;
for (auto&& plot : plots) {
height += plot->height();
}
return height;
}
void PlotView::resizeEvent(QResizeEvent * event)
{
updateView();
}
off_t PlotView::samplesPerLine()
{
return fftSize / zoomLevel;
}
void PlotView::scrollContentsBy(int dx, int dy)
{
updateView();
}
void PlotView::updateView(bool reCenter)
{
// Store old view for recentering
auto oldViewRange = viewRange;
// Update current view
viewRange = {
horizontalScrollBar()->value(),
horizontalScrollBar()->value() + width() * samplesPerLine()
};
// Adjust time offset to zoom around central sample
if (reCenter) {
horizontalScrollBar()->setValue(
horizontalScrollBar()->value() + (oldViewRange.length() - viewRange.length()) / 2
);
}
horizontalScrollBar()->setMaximum(mainSampleSource->count() - ((width() - 1) * samplesPerLine()));
verticalScrollBar()->setMaximum(std::max(0, plotsHeight() - viewport()->height()));
// Update cursors
QRect rect = viewport()->rect();
range_t<int> newSelection = {
(int)((selectedSamples.minimum - horizontalScrollBar()->value()) / samplesPerLine()),
(int)((selectedSamples.maximum - horizontalScrollBar()->value()) / samplesPerLine())
};
cursors.setSelection(newSelection);
// Re-paint
viewport()->update();
}