forked from analogdevicesinc/scopy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeDomainDisplayPlot.cc
1366 lines (1122 loc) · 36.1 KB
/
TimeDomainDisplayPlot.cc
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++ -*- */
/*
* Copyright 2008-2012 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 LICENSE. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
/*
* Copyright (c) 2019 Analog Devices Inc.
*
* This file is part of Scopy
* (see http://www.github.com/analogdevicesinc/scopy).
*
* 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/>.
*/
#ifndef TIME_DOMAIN_DISPLAY_PLOT_C
#define TIME_DOMAIN_DISPLAY_PLOT_C
#include <qwt_scale_draw.h>
#include <qwt_legend.h>
#include <QColor>
#include <cmath>
#include <iostream>
#include <volk/volk.h>
#include <QFont>
#include "TimeDomainDisplayPlot.h"
#include "osc_scale_engine.h"
#include "smoothcurvefitter.h"
using namespace adiscope;
class TimeDomainDisplayZoomer: public OscPlotZoomer
{
public:
#if QWT_VERSION < 0x060100
TimeDomainDisplayZoomer(QwtPlotCanvas* canvas)
#else /* QWT_VERSION < 0x060100 */
TimeDomainDisplayZoomer(QWidget* canvas)
#endif /* QWT_VERSION < 0x060100 */
: OscPlotZoomer(canvas)
{
setTrackerMode(QwtPicker::AlwaysOn);
}
virtual ~TimeDomainDisplayZoomer()
{
}
virtual void updateTrackerText()
{
updateDisplay();
}
protected:
using QwtPlotZoomer::trackerText;
virtual QwtText trackerText( const QPoint& p ) const
{
QwtText t;
QwtDoublePoint dp = QwtPlotZoomer::invTransform(p);
const TimeDomainDisplayPlot* plt = (const TimeDomainDisplayPlot *)plot();
t.setText(plt->timeScaleValueFormat(dp.x(), 3) + "," +
plt->yAxisScaleValueFormat(dp.y(), 3));
return t;
}
};
SinkManager::SinkManager()
{
}
SinkManager::~SinkManager()
{
}
bool SinkManager::addSink(const std::string& name, unsigned int numChannels, unsigned long long channelsDataLength)
{
bool can_add = true;
for (auto it = d_sinkList.begin(); it != d_sinkList.end(); ++it)
if ((*it).name() == name) {
can_add = false;
break;
}
if (can_add)
d_sinkList.push_back(Sink(name, numChannels, channelsDataLength));
return can_add;
}
bool SinkManager::removeSink(const std::string& name)
{
bool found = false;
for (auto it = d_sinkList.begin(); it != d_sinkList.end(); ++it)
if ((*it).name() == name) {
found = true;
it = d_sinkList.erase(it);
break;
}
return found;
}
unsigned int SinkManager::sinkListLength()
{
return d_sinkList.size();
}
Sink* SinkManager::sink(unsigned int index)
{
if (index < d_sinkList.size())
return &d_sinkList[index];
return NULL;
}
int SinkManager::indexOfSink(const std::string& name)
{
int pos = find_if(d_sinkList.begin(), d_sinkList.end(), [&name](Sink& s) {return s.name() == name;}) - d_sinkList.begin();
if (pos >= d_sinkList.size())
pos = -1;
return pos;
}
int SinkManager::sinkFirstChannelPos(const std::string& name)
{
int i, index = 0;
for (i = 0; i < d_sinkList.size(); ++i) {
if (d_sinkList[i].name() == name)
break;
index += d_sinkList[i].numChannels();
}
if (i == d_sinkList.size())
index = -1;
return index;
}
/***********************************************************************
* Main Time domain plotter widget
**********************************************************************/
TimeDomainDisplayPlot::TimeDomainDisplayPlot(QWidget* parent, unsigned int xNumDivs, unsigned int yNumDivs)
: DisplayPlot(0, parent, xNumDivs, yNumDivs)
{
d_tag_text_color = Qt::black;
d_tag_background_color = Qt::white;
d_tag_background_style = Qt::NoBrush;
d_sample_rate = 1;
d_data_starting_point = 0.0;
d_curves_hidden = false;
d_nbPtsXAxis = 0;
d_nb_ref_curves = 0;
// Reconfigure the bottom horizontal axis that was created by the base class
configureAxis(QwtPlot::xBottom, 0);
configureAxis(QwtPlot::yLeft, 0);
d_yAxisUnit = "";
//d_zoomer = new TimeDomainDisplayZoomer(canvas());
QFont font;
font.setPointSize(10);
font.setWeight(75);
//d_zoomer->setTrackerFont(font);
#if QWT_VERSION < 0x060000
//d_zoomer->setSelectionFlags(QwtPicker::RectSelection | QwtPicker::DragSelection);
#endif
//d_zoomer->setMousePattern(QwtEventPattern::MouseSelect2,
// Qt::RightButton, Qt::ControlModifier);
//d_zoomer->setMousePattern(QwtEventPattern::MouseSelect3,
// Qt::RightButton);
const QColor c("#999999");
//d_zoomer->setRubberBandPen(c);
//d_zoomer->setTrackerPen(c);
d_semilogx = false;
d_semilogy = false;
d_autoscale_shot = false;
d_metricFormatter.setTwoDecimalMode(true);
d_timeFormatter.setTwoDecimalMode(true);
d_tag_markers.resize(d_nplots);
d_tag_markers_en = std::vector<bool>(d_nplots, true);
d_trigger_lines[0] = new QwtPlotMarker();
d_trigger_lines[0]->setLineStyle(QwtPlotMarker::HLine);
d_trigger_lines[0]->setLinePen(QPen(Qt::red, 0.6, Qt::DashLine));
d_trigger_lines[0]->setRenderHint(QwtPlotItem::RenderAntialiased);
d_trigger_lines[0]->setXValue(0.0);
d_trigger_lines[0]->setYValue(0.0);
d_trigger_lines[1] = new QwtPlotMarker();
d_trigger_lines[1]->setLineStyle(QwtPlotMarker::VLine);
d_trigger_lines[1]->setLinePen(QPen(Qt::red, 0.6, Qt::DashLine));
d_trigger_lines[1]->setRenderHint(QwtPlotItem::RenderAntialiased);
d_trigger_lines[1]->setXValue(0.0);
d_trigger_lines[1]->setYValue(0.0);
for (int i = 0; i < 6; i++) {
d_zoomer.push_back(new TimeDomainDisplayZoomer(this->canvas()));
d_zoomer[i]->setEnabled(false);
}
}
TimeDomainDisplayPlot::~TimeDomainDisplayPlot()
{
while (d_sinkManager.sinkListLength()) {
Sink *sink = d_sinkManager.sink(0);
unregisterSink(sink->name());
}
//TODO: prob remove d_trigger_lines -> not used.
delete d_trigger_lines[0];
delete d_trigger_lines[1];
for (int i = 0; i < d_ref_ydata.size(); ++i) {
delete[] d_ref_ydata[i];
}
// d_zoomer and _panner deleted when parent deleted
}
void
TimeDomainDisplayPlot::replot()
{
QwtPlot::replot();
}
void
TimeDomainDisplayPlot::plotNewData(const std::string &sender,
const std::vector<double*> &dataPoints,
const int64_t numDataPoints,
const double timeInterval,
const std::vector< std::vector<gr::tag_t> > &tags)
{
int sinkIndex = d_sinkManager.indexOfSink(sender);
if(!d_stop) {
if((numDataPoints > 0) && sinkIndex >= 0) {
Sink *sink = d_sinkManager.sink((unsigned int)sinkIndex);
int start = d_sinkManager.sinkFirstChannelPos(sender);
unsigned int sinkNumChannels = sink->numChannels();
unsigned long long sinkNumPoints = sink->channelsDataLength();
bool reset_x_axis_points = d_sink_reset_x_axis_pts[sinkIndex];
if(numDataPoints != sinkNumPoints){
sinkNumPoints = numDataPoints;
sink->setChannelsDataLength(numDataPoints);
delete[] d_xdata[sinkIndex];
d_xdata[sinkIndex] = new double[numDataPoints];
int ref_offset = countReferenceWaveform(start);
for(int i = start; i < start + sinkNumChannels; i++) {
delete[] d_ydata[i];
d_ydata[i] = new double[numDataPoints];
d_plot_curve[i + ref_offset]->setRawSamples(d_xdata[sinkIndex], d_ydata[i], numDataPoints);
}
_resetXAxisPoints(d_xdata[sinkIndex], numDataPoints, d_sample_rate);
} else if (reset_x_axis_points) {
_resetXAxisPoints(d_xdata[sinkIndex], numDataPoints, d_sample_rate);
reset_x_axis_points = false;
}
for(int i = 0; i < sinkNumChannels; i++) {
if(d_semilogy) {
for(int n = 0; n < numDataPoints; n++)
d_ydata[start + i][n] = fabs(dataPoints[i][n]);
}
else {
memcpy(d_ydata[start + i], dataPoints[i], numDataPoints*sizeof(double));
}
}
for (int i = 0; i < d_plot_curve.size(); i++)
d_plot_curve.at(i)->show();
d_curves_hidden = false;
// // Detach and delete any tags that were plotted last time
// for(int n = 0; n < d_nplots; n++) {
// for(size_t i = 0; i < d_tag_markers[n].size(); i++) {
// d_tag_markers[n][i]->detach();
// delete d_tag_markers[n][i];
// }
// d_tag_markers[n].clear();
// }
// Plot and attach any new tags found.
// First test if this was a complex input where real/imag get
// split here into two stream.
// if(tags.size() > 0) {
// bool cmplx = false;
// int mult = (int)d_nplots / (int)tags.size();
// if(mult == 2)
// cmplx = true;
// std::vector< std::vector<gr::tag_t> >::const_iterator tag = tags.begin();
// for(int i = 0; i < d_nplots; i+=mult) {
// std::vector<gr::tag_t>::const_iterator t;
// for(t = tag->begin(); t != tag->end(); t++) {
// uint64_t offset = (*t).offset;
// // Ignore tag if its offset is outside our plottable vector.
// if(offset >= (uint64_t)d_numPoints) {
// continue;
// }
// double sample_offset = double(offset)/d_sample_rate;
// std::stringstream s;
// s << (*t).key << ": " << (*t).value;
// // Select the right input stream to put the tag on. If real,
// // just use i; if it's a complex stream, find the max of the
// // real and imaginary parts and put the tag on that one.
// int which = i;
// if(cmplx) {
// bool show0 = d_plot_curve[i]->isVisible();
// bool show1 = d_plot_curve[i+1]->isVisible();
// // If we are showing both streams, select the inptu stream
// // with the larger value
// if(show0 && show1) {
// if(fabs(d_ydata[i][offset]) < fabs(d_ydata[i+1][offset]))
// which = i+1;
// }
// else {
// // If show0, we keep which = i; otherwise, use i+1.
// if(show1)
// which = i+1;
// }
// }
// double yval = d_ydata[which][offset];
// // Find if we already have a marker at this location
// std::vector<QwtPlotMarker*>::iterator mitr;
// for(mitr = d_tag_markers[which].begin(); mitr != d_tag_markers[which].end(); mitr++) {
// if((*mitr)->xValue() == sample_offset) {
// break;
// }
// }
// // If no matching marker, create a new one
// if(mitr == d_tag_markers[which].end()) {
// bool show = d_plot_curve[which]->isVisible();
// QwtPlotMarker *m = new QwtPlotMarker();
// m->setXValue(sample_offset);
// m->setYValue(yval);
// QBrush brush;
// brush.setColor(QColor(0xC8, 0x2F, 0x1F));
// brush.setStyle(Qt::SolidPattern);
// QPen pen;
// pen.setColor(Qt::black);
// pen.setWidth(1);
// QwtSymbol *sym = new QwtSymbol(QwtSymbol::NoSymbol, brush, pen, QSize(12,12));
// if(yval >= 0) {
// sym->setStyle(QwtSymbol::DTriangle);
// m->setLabelAlignment(Qt::AlignTop);
// }
// else {
// sym->setStyle(QwtSymbol::UTriangle);
// m->setLabelAlignment(Qt::AlignBottom);
// }
//#if QWT_VERSION < 0x060000
// m->setSymbol(*sym);
//#else
// m->setSymbol(sym);
//#endif
// m->setLabel(QwtText(s.str().c_str()));
// m->attach(this);
// if(!(show && d_tag_markers_en[which])) {
// m->hide();
// }
// d_tag_markers[which].push_back(m);
// }
// else {
// // Prepend the new tag to the existing marker
// // And set it at the max value
// if(fabs(yval) < fabs((*mitr)->yValue()))
// (*mitr)->setYValue(yval);
// QString orig = (*mitr)->label().text();
// s << std::endl;
// orig.prepend(s.str().c_str());
// QwtText newtext(orig);
// newtext.setColor(getTagTextColor());
// QBrush brush(getTagBackgroundColor(), getTagBackgroundStyle());
// newtext.setBackgroundBrush(brush);
// (*mitr)->setLabel(newtext);
// }
// }
// tag++;
// }
// }
// if(d_autoscale_state) {
// double bottom=1e20, top=-1e20;
// for(int n = 0; n < d_nplots; n++) {
// for(int64_t point = 0; point < numDataPoints; point++) {
// if(d_ydata[n][point] < bottom) {
// bottom = d_ydata[n][point];
// }
// if(d_ydata[n][point] > top) {
// top = d_ydata[n][point];
// }
// }
// }
// _autoScale(bottom, top);
// if(d_autoscale_shot) {
// d_autoscale_state = false;
// d_autoscale_shot = false;
// }
// }
replot();
Q_EMIT newData();
}
}
}
void TimeDomainDisplayPlot::newData(const QEvent* updateEvent)
{
IdentifiableTimeUpdateEvent *tevent = (IdentifiableTimeUpdateEvent*)updateEvent;
const std::vector<double*> dataPoints = tevent->getTimeDomainPoints();
const uint64_t numDataPoints = tevent->getNumTimeDomainDataPoints();
const std::vector< std::vector<gr::tag_t> > tags = tevent->getTags();
const std::string sender = tevent->senderName();
if ((d_nbPtsXAxis != 0) && (d_nbPtsXAxis <= numDataPoints)
&& sender == "Osc Time") {
Q_EMIT filledScreen(true, numDataPoints);
}
this->plotNewData(sender,
dataPoints,
numDataPoints,
0,
tags);
}
void TimeDomainDisplayPlot::customEvent(QEvent * e)
{
if(e->type() == TimeUpdateEvent::Type()) {
newData(e);
}
}
void
TimeDomainDisplayPlot::legendEntryChecked(QwtPlotItem* plotItem, bool on)
{
// When line is turned on/off, immediately show/hide tag markers
for(int n = 0; n < d_nplots; n++) {
if(plotItem == d_plot_curve[n]) {
for(size_t i = 0; i < d_tag_markers[n].size(); i++) {
if(!(!on && d_tag_markers_en[n]))
d_tag_markers[n][i]->hide();
else
d_tag_markers[n][i]->show();
}
}
}
DisplayPlot::legendEntryChecked(plotItem, on);
}
void
TimeDomainDisplayPlot::legendEntryChecked(const QVariant &plotItem, bool on, int index)
{
#if QWT_VERSION < 0x060100
std::runtime_error("TimeDomainDisplayPlot::legendEntryChecked with QVariant not enabled in this version of QWT.\n");
#else
QwtPlotItem *p = infoToItem(plotItem);
legendEntryChecked(p, on);
#endif /* QWT_VERSION < 0x060100 */
}
void
TimeDomainDisplayPlot::_resetXAxisPoints(double*& xAxis, unsigned long long numPoints, double sampleRate)
{
double delt = 1.0 / sampleRate;
for (long loc = 0; loc < numPoints; loc++)
xAxis[loc] = (d_data_starting_point + loc) * delt;
// Set up zoomer base for maximum unzoom x-axis
// and reset to maximum unzoom level
// QwtDoubleRect zbase = d_zoomer->zoomBase();
// if(d_semilogx) {
// setAxisScale(QwtPlot::xBottom, 1e-1, d_numPoints*delt);
// zbase.setLeft(1e-1);
// }
// else {
// setAxisScale(QwtPlot::xBottom, 0, d_numPoints*delt);
// zbase.setLeft(0);
// }
// zbase.setRight(d_numPoints*delt);
// d_zoomer->zoom(zbase);
// d_zoomer->setZoomBase(zbase);
// d_zoomer->zoom(0);
}
void
TimeDomainDisplayPlot::_autoScale(double bottom, double top)
{
// Auto scale the y-axis with a margin of 20% (10 dB for log scale)
double _bot = bottom - fabs(bottom)*0.20;
double _top = top + fabs(top)*0.20;
if(d_semilogy) {
if(bottom > 0) {
setYaxis(_bot-10, _top+10);
}
else {
setYaxis(1e-3, _top+10);
}
}
else {
setYaxis(_bot, _top);
}
}
void
TimeDomainDisplayPlot::addZoomer(unsigned int zoomerIdx)
{
QFont font;
font.setPointSize(10);
font.setWeight(75);
d_zoomer[zoomerIdx]->setTrackerFont(font);
#if QWT_VERSION < 0x060000
d_zoomer[zoomerIdx]->setSelectionFlags(QwtPicker::RectSelection | QwtPicker::DragSelection);
#endif
d_zoomer[zoomerIdx]->setMousePattern(QwtEventPattern::MouseSelect2,
Qt::RightButton, Qt::ControlModifier);
d_zoomer[zoomerIdx]->setMousePattern(QwtEventPattern::MouseSelect3,
Qt::RightButton);
d_zoomer[zoomerIdx]->setTrackerMode(QwtPicker::AlwaysOff);
const QColor c("#999999");
d_zoomer[zoomerIdx]->setRubberBandPen(c);
d_zoomer[zoomerIdx]->setTrackerPen(getLineColor(zoomerIdx));
d_zoomer[zoomerIdx]->setEnabled(true);
d_zoomer[zoomerIdx]->setAxes(QwtAxisId(QwtPlot::xBottom, 0), QwtAxisId(QwtPlot::yLeft, zoomerIdx));
}
void
TimeDomainDisplayPlot::removeZoomer(unsigned int zoomerIdx)
{
if (zoomerIdx == 0 || zoomerIdx == 1) {
d_zoomer[zoomerIdx]->setEnabled(false);
return;
}
int toDisable = zoomerIdx;
while (d_zoomer[toDisable]->isEnabled()
&& toDisable < d_zoomer.size() - 1) {
toDisable++;
}
if (toDisable == d_zoomer.size() - 1 && d_zoomer[toDisable]->isEnabled()) {
d_zoomer[toDisable]->setEnabled(false);
} else {
d_zoomer[toDisable - 1]->setEnabled(false);
}
for (int i = 0; i < d_zoomer.size(); ++i) {
if (d_zoomer[i]->isEnabled()) {
d_zoomer[i]->setAxes(QwtAxisId(QwtPlot::xBottom, 0), QwtAxisId(QwtPlot::yLeft, i));
d_zoomer[i]->setTrackerPen(getLineColor(i));
}
}
}
void TimeDomainDisplayPlot::setXAxisNumPoints(unsigned int pts)
{
d_nbPtsXAxis = pts;
}
void
TimeDomainDisplayPlot::setAutoScale(bool state)
{
d_autoscale_state = state;
}
void
TimeDomainDisplayPlot::setAutoScaleShot()
{
d_autoscale_state = true;
d_autoscale_shot = true;
}
void
TimeDomainDisplayPlot::setSampleRate(double sr, double units,
const std::string &strunits)
{
double newsr = sr/units;
if(newsr != d_sample_rate) {
d_sample_rate = sr/units;
for (unsigned int i = 0; i < d_sinkManager.sinkListLength(); i++)
_resetXAxisPoints(d_xdata[i], d_sinkManager.sink(i)->channelsDataLength(), d_sample_rate);
}
}
double
TimeDomainDisplayPlot::sampleRate() const
{
return d_sample_rate;
}
void TimeDomainDisplayPlot::setYaxisUnit(QString unitType)
{
if (d_yAxisUnit != unitType) {
d_yAxisUnit = unitType;
OscScaleDraw *scaleDraw = static_cast<OscScaleDraw *>(this->axisScaleDraw(QwtPlot::yLeft));
if (scaleDraw)
scaleDraw->setUnitType(d_yAxisUnit);
}
}
QString TimeDomainDisplayPlot::yAxisUnit(void)
{
return d_yAxisUnit;
}
void
TimeDomainDisplayPlot::stemPlot(bool en)
{
if(en) {
for(int i = 0; i < d_nplots; i++) {
d_plot_curve[i]->setStyle(QwtPlotCurve::Sticks);
setLineMarker(i, QwtSymbol::Ellipse);
}
}
else {
for(int i = 0; i < d_nplots; i++) {
d_plot_curve[i]->setStyle(QwtPlotCurve::Lines);
setLineMarker(i, QwtSymbol::NoSymbol);
}
}
}
void TimeDomainDisplayPlot::setPlotLineStyle(unsigned int chIdx, unsigned int style)
{
if (chIdx < 0 || chIdx >= d_plot_curve.size()) {
return;
}
auto curve = d_plot_curve[chIdx];
curve->setPaintAttribute(QwtPlotCurve::ClipPolygons, true);
curve->setCurveAttribute(QwtPlotCurve::Fitted, false);
switch (style) {
case 0:
curve->setStyle(QwtPlotCurve::CurveStyle::Lines);
replot();
break;
case 1:
curve->setStyle(QwtPlotCurve::CurveStyle::Dots);
replot();
break;
case 2:
curve->setStyle(QwtPlotCurve::CurveStyle::Steps);
replot();
break;
case 3:
curve->setStyle(QwtPlotCurve::CurveStyle::Sticks);
replot();
break;
case 4:
curve->setPaintAttribute(QwtPlotCurve::ClipPolygons, false);
curve->setCurveAttribute(QwtPlotCurve::Fitted, true);
curve->setStyle(QwtPlotCurve::CurveStyle::Lines);
replot();
break;
}
}
int TimeDomainDisplayPlot::getPlotLineStyle(unsigned int chIdx) const
{
if (chIdx < 0 || chIdx >= d_plot_curve.size()) {
return -1;
}
auto style = d_plot_curve[chIdx]->style();
switch (style) {
case QwtPlotCurve::CurveStyle::Lines:
if (d_plot_curve[chIdx]->testCurveAttribute(QwtPlotCurve::Fitted)) {
return 4;
} else {
return 0;
}
case QwtPlotCurve::CurveStyle::Dots:
return 1;
case QwtPlotCurve::CurveStyle::Steps:
return 2;
case QwtPlotCurve::CurveStyle::Sticks:
return 3;
default:
return -1;
}
}
void
TimeDomainDisplayPlot::setSemilogx(bool en)
{
d_semilogx = en;
if(!d_semilogx) {
setAxisScaleEngine(QwtPlot::xBottom, new QwtLinearScaleEngine);
}
else {
#if QWT_VERSION < 0x060100
setAxisScaleEngine(QwtPlot::xBottom, new QwtLog10ScaleEngine);
#else /* QWT_VERSION < 0x060100 */
setAxisScaleEngine(QwtPlot::xBottom, new QwtLogScaleEngine);
#endif /*QWT_VERSION < 0x060100 */
}
for (unsigned int i = 0; i < d_sinkManager.sinkListLength(); i++)
_resetXAxisPoints(d_xdata[i], d_sinkManager.sink(i)->channelsDataLength(), d_sample_rate);
}
void
TimeDomainDisplayPlot::setSemilogy(bool en)
{
if(d_semilogy != en) {
d_semilogy = en;
#if QWT_VERSION < 0x060100
double max = axisScaleDiv(QwtPlot::yLeft)->upperBound();
#else /* QWT_VERSION < 0x060100 */
double max = axisScaleDiv(QwtPlot::yLeft).upperBound();
#endif /* QWT_VERSION < 0x060100 */
if(!d_semilogy) {
setAxisScaleEngine(QwtPlot::yLeft, new QwtLinearScaleEngine);
setYaxis(-pow(10.0, max/10.0), pow(10.0, max/10.0));
}
else {
#if QWT_VERSION < 0x060100
setAxisScaleEngine(QwtPlot::yLeft, new QwtLog10ScaleEngine);
#else /* QWT_VERSION < 0x060100 */
setAxisScaleEngine(QwtPlot::yLeft, new QwtLogScaleEngine);
#endif /*QWT_VERSION < 0x060100 */
setYaxis(1e-10, 10.0*log10(max));
}
}
}
void
TimeDomainDisplayPlot::enableTagMarker(int which, bool en)
{
if((size_t)which < d_tag_markers_en.size())
d_tag_markers_en[which] = en;
else
throw std::runtime_error("TimeDomainDisplayPlot: enabled tag marker does not exist.\n");
}
const QColor
TimeDomainDisplayPlot::getTagTextColor()
{
return d_tag_text_color;
}
const QColor
TimeDomainDisplayPlot::getTagBackgroundColor()
{
return d_tag_background_color;
}
const Qt::BrushStyle
TimeDomainDisplayPlot::getTagBackgroundStyle()
{
return d_tag_background_style;
}
void
TimeDomainDisplayPlot::setTagTextColor(QColor c)
{
d_tag_text_color = c;
}
void
TimeDomainDisplayPlot::setTagBackgroundColor(QColor c)
{
d_tag_background_color = c;
}
void
TimeDomainDisplayPlot::setTagBackgroundStyle(Qt::BrushStyle b)
{
d_tag_background_style = b;
}
void TimeDomainDisplayPlot::setZoomerEnabled(bool en)
{
for (unsigned int i = 0; i < d_zoomer.size(); ++i)
d_zoomer[i]->setEnabled(en);
}
bool TimeDomainDisplayPlot::isZoomerEnabled()
{
if (d_zoomer.isEmpty())
return false;
return d_zoomer[0]->isEnabled();
}
void TimeDomainDisplayPlot::setZoomerVertAxis(int index)
{
if (index < -1 || index >= axesCount(QwtPlot::yLeft))
return;
for (unsigned int i = 0; i < d_zoomer.size(); ++i)
if (d_zoomer[i]->isEnabled())
d_zoomer[i]->setTrackerMode(
(i == index) ? QwtPicker::AlwaysOn : QwtPicker::AlwaysOff);
}
QString TimeDomainDisplayPlot::timeScaleValueFormat(double value, int precision) const
{
return d_timeFormatter.format(value, "", precision);
}
QString TimeDomainDisplayPlot::timeScaleValueFormat(double value)
{
OscScaleDraw *scale = static_cast<OscScaleDraw *>(this->axisScaleDraw(QwtPlot::xBottom));
return d_timeFormatter.format(value, "", scale->getFloatPrecison());
}
QString TimeDomainDisplayPlot::yAxisScaleValueFormat(double value, int precision) const
{
value *= d_displayScale;
return d_metricFormatter.format(value, d_yAxisUnit, precision);
}
QString TimeDomainDisplayPlot::yAxisScaleValueFormat(double value)
{
OscScaleDraw *scale = static_cast<OscScaleDraw *>(this->axisScaleDraw(QwtPlot::yLeft));
return d_metricFormatter.format(value, d_yAxisUnit, scale->getFloatPrecison());
}
void
TimeDomainDisplayPlot::setYLabel(const std::string &label,
const std::string &unit)
{
std::string l = label;
if(unit.length() > 0)
l += " (" + unit + ")";
setAxisTitle(QwtPlot::yLeft, QString(l.c_str()));
}
void
TimeDomainDisplayPlot::attachTriggerLines(bool en)
{
if(en) {
d_trigger_lines[0]->attach(this);
d_trigger_lines[1]->attach(this);
}
else {
d_trigger_lines[0]->detach();
d_trigger_lines[1]->detach();
}
}
void
TimeDomainDisplayPlot::setTriggerLines(double x, double y)
{
d_trigger_lines[0]->setXValue(x);
d_trigger_lines[0]->setYValue(y);
d_trigger_lines[1]->setXValue(x);
d_trigger_lines[1]->setYValue(y);
}
QColor TimeDomainDisplayPlot::getChannelColor()
{
for (QList<QColor>::const_iterator it = d_CurveColors.cbegin();
it != d_CurveColors.cend(); ++it) {
bool used = false;
for (std::vector<QwtPlotCurve *>::const_iterator it2 = d_plot_curve.cbegin();
!used && it2 != d_plot_curve.cend(); ++it2)
used = (*it2)->pen().color() == (*it);
if (!used)
return *it;
}
return Qt::black;
}
bool TimeDomainDisplayPlot::isReferenceWaveform(QwtPlotCurve *curve)
{
return d_ref_curves.values().contains(curve);
}
bool TimeDomainDisplayPlot::isMathWaveform(QwtPlotCurve *curve) const
{
return d_math_curves.values().contains(curve);
}
int TimeDomainDisplayPlot::getCurveNextTo(int pos)
{
while (isReferenceWaveform(Curve(pos))) pos++;
return pos;
}
int TimeDomainDisplayPlot::countReferenceWaveform(int position)
{
/* returns the number of curves that are of type reference that were added before "curve" */
int curveIdx = getCurveNextTo(position);
QwtPlotCurve *curve = Curve(curveIdx);
int count = 0;
for (int i = 0; i < d_plot_curve.size(); ++i)
if (d_plot_curve[i] == curve) {
return count;
} else if (isReferenceWaveform(d_plot_curve[i])) {
count++;
}
return count;
}
void TimeDomainDisplayPlot::registerReferenceWaveform(QString name, QVector<double> xData, QVector<double> yData)
{
QColor color = getChannelColor();
QwtPlotCurve *curve = new QwtPlotCurve();
curve->setSamples(xData, yData);
curve->setPen(QPen(color));