-
Notifications
You must be signed in to change notification settings - Fork 12
/
AbstractWaterfall.cpp
2433 lines (2092 loc) · 63.9 KB
/
AbstractWaterfall.cpp
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- c++ -*- */
/* + + + This Software is released under the "Simplified BSD License" + + +
* Copyright 2010 Moe Wheatley. All rights reserved.
* Copyright 2011-2013 Alexandru Csete OZ9AEC
* Copyright 2018 Gonzalo José Carracedo Carballal - Minimal modifications for integration
* Copyright 2023-2024 Sultan Qasim Khan - Abstract class for OpenGL and QPainter waterfalls
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY Moe Wheatley ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Moe Wheatley OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of Moe Wheatley.
*/
#include <cmath>
#include <QColor>
#include <QDateTime>
#include <QDebug>
#include <QFont>
#include <QPainter>
#include <QtGlobal>
#include <QToolTip>
#include <QDebug>
#include <cstring>
#include "AbstractWaterfall.h"
#include "SuWidgetsHelpers.h"
// Comment out to enable plotter debug messages
//#define PLOTTER_DEBUG
#define STATUS_TIP \
"Click, drag or scroll on spectrum to tune. " \
"Drag and scroll X and Y axes for pan and zoom. " \
"Drag filter edges to adjust filter."
///////////////////////////// AbstractWaterfall ////////////////////////////////////////
AbstractWaterfall::AbstractWaterfall(QWidget *parent) : QOpenGLWidget(parent)
{
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
setFocusPolicy(Qt::StrongFocus);
setAttribute(Qt::WA_PaintOnScreen,false);
setAutoFillBackground(false);
setAttribute(Qt::WA_OpaquePaintEvent, false);
setAttribute(Qt::WA_NoSystemBackground, true);
setMouseTracking(true);
setTooltipsEnabled(false);
setStatusTip(tr(STATUS_TIP));
m_PeakHoldActive = false;
m_PeakHoldValid = false;
m_FftCenter = 0;
m_CenterFreq = 144500000;
m_DemodCenterFreq = 144500000;
m_DemodHiCutFreq = 5000;
m_DemodLowCutFreq = -5000;
m_FLowCmin = -25000;
m_FLowCmax = -1000;
m_FHiCmin = 1000;
m_FHiCmax = 25000;
m_symetric = true;
m_ClickResolution = 100;
m_FilterClickResolution = 100;
m_CursorCaptureDelta = CUR_CUT_DELTA;
m_FilterBoxEnabled = true;
m_CenterLineEnabled = true;
m_BookmarksEnabled = true;
m_Locked = false;
m_freqDragLocked = false;
m_Span = 96000;
m_SampleFreq = 96000;
m_HorDivs = 12;
m_VerDivs = 6;
m_PandMaxdB = m_WfMaxdB = 0.f;
m_PandMindB = m_WfMindB = -150.f;
m_CumWheelDelta = 0;
m_FreqUnits = 1000000;
m_CursorCaptured = NOCAP;
m_Running = false;
m_DrawOverlay = true;
m_2DPixmap = QPixmap(0,0);
m_OverlayPixmap = QPixmap(0,0);
m_Size = QSize(0,0);
m_SpectrumPlotHeight = 0;
m_WaterfallHeight = 0;
m_GrabPosition = 0;
m_Percent2DScreen = 30; //percent of screen used for 2D display
m_VdivDelta = 30;
m_HdivDelta = 70;
m_ZeroPoint = 0;
m_dBPerUnit = 1;
m_FreqDigits = 3;
m_Peaks = QMap<int,int>();
setPeakDetection(false, 2);
m_PeakHoldValid = false;
setFftPlotColor(QColor(0xFF,0xFF,0xFF,0xFF));
setFftBgColor(QColor(PLOTTER_BGD_COLOR));
setFftAxesColor(QColor(PLOTTER_GRID_COLOR));
setFilterBoxColor(QColor(PLOTTER_FILTER_BOX_COLOR));
setTimeStampColor(QColor(0xFF,0xFF,0xFF,0xFF));
setFftFill(false);
// always update waterfall
tlast_wf_ms = 0;
msec_per_wfline = 0;
wf_span = 0;
fft_rate = 15;
m_fftData = nullptr;
m_fftDataSize = 0;
m_infoTextColor = m_FftTextColor;
}
AbstractWaterfall::~AbstractWaterfall()
{
}
QSize AbstractWaterfall::minimumSizeHint() const
{
return QSize(50, 50);
}
QSize AbstractWaterfall::sizeHint() const
{
return QSize(180, 180);
}
void AbstractWaterfall::mouseMoveEvent(QMouseEvent* event)
{
QPoint pt = event->pos();
/* mouse enter / mouse leave events */
if (m_OverlayPixmap.rect().contains(pt))
{
//is in Overlay bitmap region
if (event->buttons() == Qt::NoButton)
{
bool onTag = false;
if(pt.y() < 15 * 10) // FIXME
{
for(int i = 0; i < m_BookmarkTags.size() && !onTag; i++)
{
if (m_BookmarkTags[i].first.contains(event->pos()))
onTag = true;
}
}
// if no mouse button monitor grab regions and change cursor icon
if (onTag)
{
setCursor(QCursor(Qt::PointingHandCursor));
m_CursorCaptured = BOOKMARK;
}
else if (isPointCloseTo(pt.x(), m_DemodFreqX, m_CursorCaptureDelta))
{
// in move demod box center frequency region
if (CENTER != m_CursorCaptured)
setCursor(QCursor(Qt::SizeHorCursor));
m_CursorCaptured = CENTER;
if (m_TooltipsEnabled)
QToolTip::showText(
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
event->globalPosition().toPoint(),
#else
event->globalPos(),
#endif
QString("Demod: %1 kHz")
.arg(m_DemodCenterFreq/1.e3f, 0, 'f', 3),
this);
}
else if (isPointCloseTo(pt.x(), m_DemodHiCutFreqX, m_CursorCaptureDelta))
{
// in move demod hicut region
if (RIGHT != m_CursorCaptured)
setCursor(QCursor(Qt::SizeFDiagCursor));
m_CursorCaptured = RIGHT;
if (m_TooltipsEnabled)
QToolTip::showText(
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
event->globalPosition().toPoint(),
#else
event->globalPos(),
#endif
QString("High cut: %1 Hz")
.arg(m_DemodHiCutFreq),
this);
}
else if (isPointCloseTo(pt.x(), m_DemodLowCutFreqX, m_CursorCaptureDelta))
{
// in move demod lowcut region
if (LEFT != m_CursorCaptured)
setCursor(QCursor(Qt::SizeBDiagCursor));
m_CursorCaptured = LEFT;
if (m_TooltipsEnabled)
QToolTip::showText(
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
event->globalPosition().toPoint(),
#else
event->globalPos(),
#endif
QString("Low cut: %1 Hz")
.arg(m_DemodLowCutFreq),
this);
}
else if (isPointCloseTo(pt.x(), m_YAxisWidth/2, m_YAxisWidth/2))
{
if (YAXIS != m_CursorCaptured)
setCursor(QCursor(Qt::OpenHandCursor));
m_CursorCaptured = YAXIS;
if (m_TooltipsEnabled)
QToolTip::hideText();
}
else if (isPointCloseTo(pt.y(), m_XAxisYCenter, m_CursorCaptureDelta+5))
{
if (XAXIS != m_CursorCaptured)
setCursor(QCursor(Qt::OpenHandCursor));
m_CursorCaptured = XAXIS;
if (m_TooltipsEnabled)
QToolTip::hideText();
}
else
{ //if not near any grab boundaries
if (NOCAP != m_CursorCaptured)
{
setCursor(QCursor(Qt::ArrowCursor));
m_CursorCaptured = NOCAP;
}
if (m_TooltipsEnabled)
QToolTip::showText(
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
event->globalPosition().toPoint(),
#else
event->globalPos(),
#endif
QString("F: %1 kHz")
.arg(freqFromX(pt.x())/1.e3f, 0, 'f', 3),
this);
}
m_GrabPosition = 0;
}
}
else
{
// not in Overlay region
if (event->buttons() == Qt::NoButton)
{
if (NOCAP != m_CursorCaptured)
setCursor(QCursor(Qt::ArrowCursor));
m_CursorCaptured = NOCAP;
m_GrabPosition = 0;
}
if (m_TooltipsEnabled)
{
QDateTime tt;
tt.setMSecsSinceEpoch(msecFromY(pt.y()));
QToolTip::showText(
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
event->globalPosition().toPoint(),
#else
event->globalPos(),
#endif
QString("%1\n%2 kHz")
.arg(tt.toString("yyyy.MM.dd hh:mm:ss.zzz"))
.arg(freqFromX(pt.x())/1.e3f, 0, 'f', 3),
this);
}
}
// process mouse moves while in cursor capture modes
if (YAXIS == m_CursorCaptured)
{
if (event->buttons() & Qt::LeftButton)
{
setCursor(QCursor(Qt::ClosedHandCursor));
// move Y scale up/down
float delta_px = m_Yzero - pt.y();
float delta_db = delta_px * fabs(m_PandMindB - m_PandMaxdB) /
(float)m_SpectrumPlotHeight;
m_PandMindB -= delta_db;
m_PandMaxdB -= delta_db;
if (out_of_range(m_PandMindB, m_PandMaxdB))
{
m_PandMindB += delta_db;
m_PandMaxdB += delta_db;
}
else
{
emit pandapterRangeChanged(m_PandMindB, m_PandMaxdB);
updateOverlay();
m_PeakHoldValid = false;
m_Yzero = pt.y();
}
}
}
else if (XAXIS == m_CursorCaptured)
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
if (event->buttons() & (Qt::LeftButton | Qt::MiddleButton))
#else
if (event->buttons() & (Qt::LeftButton | Qt::MidButton))
#endif // QT_VERSION
{
setCursor(QCursor(Qt::ClosedHandCursor));
// pan viewable range or move center frequency
int delta_px = m_Xzero - pt.x();
qint64 delta_hz = delta_px * m_Span / m_Size.width();
if ((event->buttons() & m_freqDragBtn) ||
(event->modifiers() & Qt::ShiftModifier))
{
if (!m_Locked && !m_freqDragLocked) {
qint64 centerFreq = boundCenterFreq(roundFreq(
m_CenterFreq + delta_hz, m_ClickResolution));
delta_hz = centerFreq - m_CenterFreq;
m_CenterFreq += delta_hz;
m_DemodCenterFreq += delta_hz;
// Scroll the spectrum only if data flow is stopped.
// When running, the hardware may be delayed in retuning, so some new
// PSDs at the old centre frequency may still arrive, causing visual
// jumping/shaking of the spectrum.
if (!m_Running)
m_tentativeCenterFreq += delta_hz;
if (delta_hz != 0)
emit newCenterFreq(m_CenterFreq);
}
} else {
setFftCenterFreq(m_FftCenter + delta_hz);
}
if (delta_hz != 0) {
updateOverlay();
m_PeakHoldValid = false;
m_Xzero = pt.x();
}
}
}
else if (LEFT == m_CursorCaptured)
{
// moving in demod lowcut region
if (event->buttons() & (Qt::LeftButton | Qt::RightButton))
{
// moving in demod lowcut region with left button held
if (m_GrabPosition != 0)
{
m_DemodLowCutFreq = freqFromX(pt.x() - m_GrabPosition ) - m_DemodCenterFreq;
m_DemodLowCutFreq = roundFreq(m_DemodLowCutFreq, m_FilterClickResolution);
if (m_symetric && (event->buttons() & Qt::LeftButton)) // symetric adjustment
{
m_DemodHiCutFreq = -m_DemodLowCutFreq;
}
clampDemodParameters();
emit newFilterFreq(m_DemodLowCutFreq, m_DemodHiCutFreq);
updateOverlay();
}
else
{
// save initial grab postion from m_DemodFreqX
m_GrabPosition = pt.x()-m_DemodLowCutFreqX;
}
}
else if (event->buttons() & ~Qt::NoButton)
{
setCursor(QCursor(Qt::ArrowCursor));
m_CursorCaptured = NOCAP;
}
}
else if (RIGHT == m_CursorCaptured)
{
// moving in demod highcut region
if (event->buttons() & (Qt::LeftButton | Qt::RightButton))
{
// moving in demod highcut region with right button held
if (m_GrabPosition != 0)
{
m_DemodHiCutFreq = freqFromX( pt.x()-m_GrabPosition ) - m_DemodCenterFreq;
m_DemodHiCutFreq = roundFreq(m_DemodHiCutFreq, m_FilterClickResolution);
if (m_symetric && (event->buttons() & Qt::LeftButton)) // symetric adjustment
{
m_DemodLowCutFreq = -m_DemodHiCutFreq;
}
clampDemodParameters();
emit newFilterFreq(m_DemodLowCutFreq, m_DemodHiCutFreq);
updateOverlay();
}
else
{
// save initial grab postion from m_DemodFreqX
m_GrabPosition = pt.x() - m_DemodHiCutFreqX;
}
}
else if (event->buttons() & ~Qt::NoButton)
{
setCursor(QCursor(Qt::ArrowCursor));
m_CursorCaptured = NOCAP;
}
}
else if (CENTER == m_CursorCaptured)
{
// moving inbetween demod lowcut and highcut region
if (event->buttons() & Qt::LeftButton)
{ // moving inbetween demod lowcut and highcut region with left button held
if (m_GrabPosition != 0)
{
if (!m_Locked) {
m_DemodCenterFreq = roundFreq(freqFromX(pt.x() - m_GrabPosition),
m_ClickResolution );
emit newDemodFreq(m_DemodCenterFreq,
m_DemodCenterFreq - m_CenterFreq);
updateOverlay();
m_PeakHoldValid = false;
}
}
else
{
// save initial grab postion from m_DemodFreqX
m_GrabPosition = pt.x() - m_DemodFreqX;
}
}
else if (event->buttons() & ~Qt::NoButton)
{
setCursor(QCursor(Qt::ArrowCursor));
m_CursorCaptured = NOCAP;
}
}
else
{
// cursor not captured
m_GrabPosition = 0;
}
if (!this->rect().contains(pt))
{
if (NOCAP != m_CursorCaptured)
setCursor(QCursor(Qt::ArrowCursor));
m_CursorCaptured = NOCAP;
}
}
// Called by QT when screen needs to be redrawn
void AbstractWaterfall::paintEvent(QPaintEvent *ev)
{
QOpenGLWidget::paintEvent(ev);
QPainter painter(this);
qint64 StartFreq = m_CenterFreq + m_FftCenter - m_Span / 2;
qint64 EndFreq = StartFreq + m_Span;
painter.setRenderHint(QPainter::Antialiasing);
painter.drawPixmap(0, 0, m_2DPixmap);
this->drawWaterfall(painter);
// Draw named channel cutoffs
if (m_channelsEnabled) {
for (auto i = m_channelSet.find(StartFreq); i != m_channelSet.cend(); ++i) {
auto p = i.value();
int x_fCenter = xFromFreq(p->frequency);
int x_fMin = xFromFreq(p->frequency + p->lowFreqCut);
int x_fMax = xFromFreq(p->frequency + p->highFreqCut);
if (EndFreq < p->frequency + p->lowFreqCut)
break;
WFHelpers::drawChannelCutoff(
painter,
m_SpectrumPlotHeight,
x_fMin,
x_fMax,
x_fCenter,
p->markerColor,
p->cutOffColor,
!p->bandLike);
}
}
// Draw demod filter box
if (m_FilterBoxEnabled) {
this->drawFilterBox(painter, m_SpectrumPlotHeight);
this->drawFilterCutoff(painter, m_SpectrumPlotHeight);
}
if (m_TimeStampsEnabled)
paintTimeStamps(
painter,
QRect(2, m_SpectrumPlotHeight, this->width(), this->height()));
}
int AbstractWaterfall::getNearestPeak(QPoint pt)
{
QMap<int, int>::const_iterator i = m_Peaks.lowerBound(pt.x() - PEAK_CLICK_MAX_H_DISTANCE);
QMap<int, int>::const_iterator upperBound = m_Peaks.upperBound(pt.x() + PEAK_CLICK_MAX_H_DISTANCE);
float dist = 1.0e10;
int best = -1;
for ( ; i != upperBound; i++)
{
int x = i.key();
int y = i.value();
if (abs(y - pt.y()) > PEAK_CLICK_MAX_V_DISTANCE)
continue;
float d = powf(y - pt.y(), 2) + powf(x - pt.x(), 2);
if (d < dist)
{
dist = d;
best = x;
}
}
return best;
}
/** Set waterfall span in milliseconds */
void AbstractWaterfall::setWaterfallSpan(quint64 span_ms)
{
qreal dpi_factor = isHdpiAware() ? screen()->devicePixelRatio() : 1;
wf_span = span_ms;
if (m_WaterfallHeight > 0)
msec_per_wfline = wf_span / (m_WaterfallHeight * dpi_factor);
clearWaterfall();
}
/** Get waterfall time resolution in milleconds / line. */
double AbstractWaterfall::getWfTimeRes()
{
if (msec_per_wfline)
return msec_per_wfline;
else
return 1000.0 / fft_rate; // Auto mode
}
void AbstractWaterfall::setFftRate(int rate_hz)
{
fft_rate = rate_hz;
clearWaterfall();
}
// Called when a mouse button is pressed
void AbstractWaterfall::mousePressEvent(QMouseEvent * event)
{
QPoint pt = event->pos();
if (NOCAP == m_CursorCaptured)
{
if (isPointCloseTo(pt.x(), m_DemodFreqX, m_CursorCaptureDelta))
{
// move demod box center frequency region
m_CursorCaptured = CENTER;
m_GrabPosition = pt.x() - m_DemodFreqX;
}
else if (isPointCloseTo(pt.x(), m_DemodLowCutFreqX, m_CursorCaptureDelta))
{
// filter low cut
m_CursorCaptured = LEFT;
m_GrabPosition = pt.x() - m_DemodLowCutFreqX;
}
else if (isPointCloseTo(pt.x(), m_DemodHiCutFreqX, m_CursorCaptureDelta))
{
// filter high cut
m_CursorCaptured = RIGHT;
m_GrabPosition = pt.x() - m_DemodHiCutFreqX;
}
else
{
if (event->buttons() == Qt::LeftButton)
{
if (!m_Locked) {
int best = -1;
if (m_PeakDetection > 0)
best = getNearestPeak(pt);
if (best != -1)
m_DemodCenterFreq = freqFromX(best);
else
m_DemodCenterFreq = roundFreq(freqFromX(pt.x()), m_ClickResolution);
// if cursor not captured set demod frequency and start demod box capture
emit newDemodFreq(m_DemodCenterFreq, m_DemodCenterFreq - m_CenterFreq);
// save initial grab postion from m_DemodFreqX
// setCursor(QCursor(Qt::CrossCursor));
m_CursorCaptured = CENTER;
m_GrabPosition = 1;
updateOverlay();
}
}
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
else if (event->buttons() == Qt::MiddleButton)
#else
else if (event->buttons() == Qt::MidButton)
#endif // QT_VERSION
{
if (!m_Locked && !m_freqDragLocked) {
// set center freq
m_CenterFreq
= boundCenterFreq(roundFreq(freqFromX(pt.x()), m_ClickResolution));
m_DemodCenterFreq = m_CenterFreq;
emit newCenterFreq(m_CenterFreq);
emit newDemodFreq(m_DemodCenterFreq, m_DemodCenterFreq - m_CenterFreq);
updateOverlay();
}
}
else if (event->buttons() == Qt::RightButton)
{
// reset frequency zoom
resetHorizontalZoom();
updateOverlay();
}
}
}
else
{
if (m_CursorCaptured == YAXIS)
// get ready for moving Y axis
m_Yzero = pt.y();
else if (m_CursorCaptured == XAXIS)
{
m_Xzero = pt.x();
if (event->buttons() == Qt::RightButton)
{
// reset frequency zoom
resetHorizontalZoom();
updateOverlay();
}
}
else if (m_CursorCaptured == BOOKMARK)
{
if (!m_Locked) {
for (int i = 0; i < m_BookmarkTags.size(); i++)
{
if (m_BookmarkTags[i].first.contains(event->pos()))
{
BookmarkInfo info = m_BookmarkTags[i].second;
if (!info.modulation.isEmpty()) {
emit newModulation(info.modulation);
}
m_DemodCenterFreq = info.frequency;
emit newDemodFreq(m_DemodCenterFreq, m_DemodCenterFreq - m_CenterFreq);
if (info.bandwidth() != 0) {
emit newFilterFreq(info.lowFreqCut, info.highFreqCut);
}
break;
}
}
}
}
}
}
void AbstractWaterfall::mouseReleaseEvent(QMouseEvent * event)
{
QPoint pt = event->pos();
if (!m_OverlayPixmap.rect().contains(pt))
{
// not in Overlay region
if (NOCAP != m_CursorCaptured)
setCursor(QCursor(Qt::ArrowCursor));
m_CursorCaptured = NOCAP;
m_GrabPosition = 0;
}
else
{
if (YAXIS == m_CursorCaptured)
{
setCursor(QCursor(Qt::OpenHandCursor));
m_Yzero = -1;
}
else if (XAXIS == m_CursorCaptured)
{
setCursor(QCursor(Qt::OpenHandCursor));
m_Xzero = -1;
}
}
}
// Make a single zoom step on the X axis.
void AbstractWaterfall::zoomStepX(float step, int x)
{
// calculate new range shown on FFT, at least 5 bins
qint64 new_range = qBound(m_fftDataSize > 0 ? 5.0 * m_SampleFreq / m_fftDataSize : 10.0,
(double)(m_Span) * step,
(double)(m_SampleFreq));
// Frequency where event occured is kept fixed under mouse
double ratio = (double)x / (double)m_Size.width();
qint64 fixed_hz = freqFromX(x);
qint64 f_min = (fixed_hz - ratio * new_range) + 0.5; // +0.5 for rounding
qint64 f_max = f_min + new_range;
// Keep edges of plot in valid frequency range
qint64 min_limit = m_CenterFreq - m_SampleFreq/2;
qint64 max_limit = m_CenterFreq + m_SampleFreq/2;
if (f_min < min_limit) {
f_min = min_limit;
f_max = f_min + new_range;
} else if (f_max > max_limit) {
f_max = max_limit;
f_min = f_max - new_range;
}
qint64 fc = (f_min + f_max) / 2;
updateOverlay();
// Explicitly set m_Span instead of calling setSpanFreq(), which also calls
// setFftCenterFreq() and updateOverlay() internally. Span needs to be set
// before frequency limits can be checked in setFftCenterFreq().
m_Span = new_range;
setFftCenterFreq(fc - m_CenterFreq);
emit newZoomLevel(getZoomLevel());
m_PeakHoldValid = false;
}
// Zoom on X axis (absolute level)
void AbstractWaterfall::zoomOnXAxis(float level)
{
float current_level = (float)m_SampleFreq / (float)m_Span;
zoomStepX(current_level / level, xFromFreq(m_DemodCenterFreq));
}
// Called when a mouse wheel is turned
void AbstractWaterfall::wheelEvent(QWheelEvent * event)
{
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
QPointF pt = event->position();
#else
QPointF pt = event->pos();
#endif // QT_VERSION
// delta is in eigths of a degree, 15 degrees is one step
double numSteps = event->angleDelta().y() / (8.0 * 15.0);
if (m_CursorCaptured == YAXIS)
{
// Vertical zoom. Wheel down: zoom out, wheel up: zoom in
// During zoom we try to keep the point (dB or kHz) under the cursor fixed
qreal zoom_fac = pow(0.9, numSteps);
qreal ratio = pt.y() / m_SpectrumPlotHeight;
qreal db_range = m_PandMaxdB - m_PandMindB;
qreal y_range = m_SpectrumPlotHeight;
qreal db_per_pix = db_range / y_range;
qreal fixed_db = m_PandMaxdB - pt.y() * db_per_pix;
db_range = qBound(
10.,
db_range * zoom_fac,
SCAST(qreal, FFT_MAX_DB - FFT_MIN_DB));
m_PandMaxdB = fixed_db + ratio * db_range;
if (m_PandMaxdB > FFT_MAX_DB)
m_PandMaxdB = FFT_MAX_DB;
m_PandMindB = m_PandMaxdB - db_range;
if (m_PandMindB < FFT_MIN_DB)
m_PandMindB = FFT_MIN_DB;
m_PeakHoldValid = false;
emit pandapterRangeChanged(m_PandMindB, m_PandMaxdB);
}
else if (m_CursorCaptured == XAXIS)
{
zoomStepX(pow(0.9, numSteps), pt.x());
}
else if (event->modifiers() & Qt::ControlModifier)
{
// filter width
m_DemodLowCutFreq -= numSteps * m_ClickResolution;
m_DemodHiCutFreq += numSteps * m_ClickResolution;
clampDemodParameters();
emit newFilterFreq(m_DemodLowCutFreq, m_DemodHiCutFreq);
}
else if (event->modifiers() & Qt::ShiftModifier)
{
if (!m_Locked) {
// filter shift
m_DemodLowCutFreq += numSteps * m_ClickResolution;
m_DemodHiCutFreq += numSteps * m_ClickResolution;
clampDemodParameters();
emit newFilterFreq(m_DemodLowCutFreq, m_DemodHiCutFreq);
}
}
else
{
if (!m_Locked) {
// small steps will be lost by roundFreq, let them accumulate
m_CumWheelDelta += event->angleDelta().y();
if (abs(m_CumWheelDelta) < 8*15)
return;
numSteps = m_CumWheelDelta / (8.0 * 15.0);
// inc/dec demod frequency
m_DemodCenterFreq += (numSteps * m_ClickResolution);
m_DemodCenterFreq = roundFreq(m_DemodCenterFreq, m_ClickResolution );
emit newDemodFreq(m_DemodCenterFreq, m_DemodCenterFreq-m_CenterFreq);
}
}
updateOverlay();
m_CumWheelDelta = 0;
}
// Called when screen size changes so must recalculate bitmaps
void AbstractWaterfall::resizeEvent(QResizeEvent* event)
{
qreal dpi_factor = screen()->devicePixelRatio();
// mandatory to call for QOpenGLWidget to resize framebuffer
if (event != nullptr)
QOpenGLWidget::resizeEvent(event);
if (!size().isValid())
return;
if (m_Size != size())
{
// if changed, resize pixmaps to new screensize
m_Size = size();
m_SpectrumPlotHeight = m_Percent2DScreen * m_Size.height() / 100;
m_WaterfallHeight = m_Size.height() - m_SpectrumPlotHeight;
m_OverlayPixmap = QPixmap(m_Size.width() * dpi_factor,
m_SpectrumPlotHeight * dpi_factor);
m_OverlayPixmap.setDevicePixelRatio(dpi_factor);
m_OverlayPixmap.fill(Qt::black);
m_PeakHoldValid = false;
if (wf_span > 0)
msec_per_wfline = wf_span / (m_WaterfallHeight * (isHdpiAware() ? dpi_factor : 1));
}
updateOverlay();
}
void AbstractWaterfall::paintTimeStamps(
QPainter &painter,
QRect const &where)
{
QFontMetrics metrics(m_Font);
int y = where.y();
int textWidth;
int textHeight = metrics.height();
int items = 0;
int leftSpacing = 0;
qreal dpi_factor = isHdpiAware() ? screen()->devicePixelRatio() : 1;
auto it = m_TimeStamps.begin();
painter.setFont(m_Font);
y += m_TimeStampCounter / dpi_factor;
if (m_TimeStampMaxHeight < where.height())
m_TimeStampMaxHeight = where.height();
painter.setPen(m_TimeStampColor);
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
leftSpacing = metrics.horizontalAdvance("00:00:00.000");
#else
leftSpacing = metrics.width("00:00:00.000");
#endif // QT_VERSION_CHECK
while (y < m_TimeStampMaxHeight + textHeight && it != m_TimeStamps.end()) {
QString const &timeStampText =
m_TimeStampsUTC ? it->utcTimeStampText : it->timeStampText;
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
textWidth = metrics.horizontalAdvance(timeStampText);
#else
textWidth = metrics.width(it->timeStampText);
#endif // QT_VERSION_CHECK
if (it->marker) {
painter.drawText(
where.x() + where.width() - textWidth - 2,
y - 2,
timeStampText);
painter.drawLine(where.x() + leftSpacing, y, where.width() - 1, y);
} else {
painter.drawText(where.x(), y - 2, timeStampText);
painter.drawLine(where.x(), y, textWidth + where.x(), y);
}
y += it->counter / dpi_factor;
++it;
++items;
}
// TimeStamps from here could not be painted due to geometry restrictions.
// we silently discard them.
while (items < m_TimeStamps.size())
m_TimeStamps.removeLast();
}
void
AbstractWaterfall::drawChannelBoxAndCutoff(
QPainter &painter,
int h,
qint64 fMin,
qint64 fMax,
qint64 fCenter,
QColor boxColor,
QColor markerColor,
QColor cutOffColor,
QString text,
QColor textColor)
{
int x_fCenter = xFromFreq(fCenter);
int x_fMin = xFromFreq(fMin);
int x_fMax = xFromFreq(fMax);
WFHelpers::drawChannelBox(
painter,
h,
x_fMin,
x_fMax,
x_fCenter,
boxColor,
markerColor,
text,
textColor);
WFHelpers::drawChannelCutoff(
painter,
h,
x_fMin,
x_fMax,
x_fCenter,
markerColor,
cutOffColor);
}
void
AbstractWaterfall::drawFilterBox(QPainter &painter, int h)
{
m_DemodFreqX = xFromFreq(m_DemodCenterFreq);
m_DemodLowCutFreqX = xFromFreq(m_DemodCenterFreq + m_DemodLowCutFreq);
m_DemodHiCutFreqX = xFromFreq(m_DemodCenterFreq + m_DemodHiCutFreq);
WFHelpers::drawChannelBox(
painter,
h,
m_DemodLowCutFreqX,
m_DemodHiCutFreqX,
m_DemodFreqX,
m_FilterBoxColor,
QColor(PLOTTER_FILTER_LINE_COLOR),
"",
QColor());
}