forked from analogdevicesinc/scopy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbgraph.cpp
799 lines (648 loc) · 18.7 KB
/
dbgraph.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
/*
* 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/>.
*/
#include "dbgraph.hpp"
#include "DisplayPlot.h"
#include "osc_scale_engine.h"
#include "x_axis_scale_zoomer.h"
#include "plotpickerwrapper.h"
#include <qwt_plot_layout.h>
#include <QIcon>
using namespace adiscope;
void dBgraph::setupVerticalBars()
{
d_plotBar = new VertBar(this, true);
d_frequencyBar = new VertBar(this, true);
d_symbolCtrl->attachSymbol(d_plotBar);
d_symbolCtrl->attachSymbol(d_frequencyBar);
QPen plotLinePen = QPen(QColor(211, 211, 211, 50), 5, Qt::SolidLine);
QPen frequencyLinePen = QPen(QColor(74, 100, 255, 150), 2, Qt::DashLine);
d_plotBar->setVisible(false);
d_frequencyBar->setPen(frequencyLinePen);
d_frequencyBar->setVisible(true);
d_frequencyBar->setMobileAxis(QwtAxis::XTop);
d_frequencyBar->setPixelPosition(0);
d_plotBar->setPen(plotLinePen);
d_plotBar->setMobileAxis(QwtAxis::XTop);
connect(d_frequencyBar, &VertBar::pixelPositionChanged,
this, &dBgraph::frequencyBarPositionChanged);
d_vBar1->setMobileAxis(QwtAxis::XTop);
d_vBar2->setMobileAxis(QwtAxis::XTop);
}
void dBgraph::setupReadouts()
{
d_cursorReadouts->setTimeReadoutVisible(false);
d_cursorReadouts->setVoltageReadoutVisible(false);
d_cursorReadouts->setTimeCursor1LabelText("F1 = ");
d_cursorReadouts->setTimeCursor2LabelText("F2 = ");
d_cursorReadouts->setVoltageCursor1LabelText("Mag1 = ");
d_cursorReadouts->setVoltageCursor2LabelText("Mag2 = ");
d_cursorReadouts->setDeltaVoltageLabelText("ΔMag = ");
d_cursorReadouts->setFrequencyDeltaVisible(false);
d_cursorReadouts->setTimeDeltaVisible(false);
d_cursorReadouts->setTransparency(0);
}
dBgraph::dBgraph(QWidget *parent, bool isdBgraph)
: DisplayPlot(0, parent, isdBgraph),
curve("data"),
reference("reference"),
xmin(10),
xmax(10),
ymin(10),
ymax(10),
d_plotPosition(0),
numSamples(0),
delta_label(false),
d_plotBarEnabled(true)
{
setAxisVisible(QwtAxis::XBottom, false);
setAxisVisible(QwtAxis::XTop, true);
setAxisAutoScale(QwtAxis::YLeft, false);
setAxisAutoScale(QwtAxis::XTop, false);
QColor plotColor;
if (QIcon::themeName() == "scopy-default") {
plotColor = QColor("#6E6E6F");
} else {
plotColor = QColor("#D3D3D3");
}
EdgelessPlotGrid *grid = new EdgelessPlotGrid;
grid->setMajorPen(plotColor, 1.0, Qt::DashLine);
grid->setXAxis(QwtAxis::XTop);
grid->attach(this);
plotLayout()->setAlignCanvasToScales(true);
curve.attach(this);
curve.setRenderHint(QwtPlotItem::RenderAntialiased);
curve.setXAxis(QwtAxis::XTop);
curve.setYAxis(QwtAxis::YLeft);
reference.setRenderHint(QwtPlotItem::RenderAntialiased);
reference.setXAxis(QwtAxis::XTop);
reference.setYAxis(QwtAxis::YLeft);
reference.setPen(Qt::red, 1.5);
thickness = 1;
d_formatter = static_cast<PrefixFormatter *>(new MetricPrefixFormatter);
OscScaleEngine *scaleLeft = new OscScaleEngine;
setYaxisNumDiv(7);
scaleLeft->setMajorTicksCount(7);
this->setAxisScaleEngine(QwtAxis::YLeft,
static_cast<QwtScaleEngine *>(scaleLeft));
/* draw_x / draw_y: Outmost X / Y scales. Only draw the labels */
draw_x = new OscScaleDraw(d_formatter, "Hz");
draw_x->setFloatPrecision(2);
draw_x->enableComponent(QwtAbstractScaleDraw::Ticks, false);
draw_x->enableComponent(QwtAbstractScaleDraw::Backbone, false);
setAxisScaleDraw(QwtAxis::XTop, draw_x);
draw_y = new OscScaleDraw("dB");
draw_y->setFloatPrecision(2);
draw_y->enableComponent(QwtAbstractScaleDraw::Ticks, false);
draw_y->enableComponent(QwtAbstractScaleDraw::Backbone, false);
draw_y->setMinimumExtent(50);
setAxisScaleDraw(QwtAxis::YLeft, draw_y);
d_leftHandlesArea->setMinimumWidth(60);
d_leftHandlesArea->setTopPadding(10);
d_leftHandlesArea->setBottomPadding(0);
d_leftHandlesArea->setMinimumHeight(this->minimumHeight());
d_rightHandlesArea->setMinimumWidth(40);
d_rightHandlesArea->setMinimumHeight(this->minimumHeight());
d_topHandlesArea->setMinimumHeight(20);
d_topHandlesArea->setLargestChildWidth(60);
useLogFreq(false);
/* Create 4 scales within the plot itself. Only draw the ticks */
for (unsigned int i = 0; i < 4; i++) {
EdgelessPlotScaleItem *scaleItem = new EdgelessPlotScaleItem(
static_cast<QwtScaleDraw::Alignment>(i));
/* Top/bottom scales must be sync'd to xTop; left/right scales
* must be sync'd to yLeft */
if (i < 2) {
scaleItem->setXAxis(QwtAxis::XTop);
} else {
scaleItem->setYAxis(QwtAxis::YLeft);
}
scaleItem->scaleDraw()->enableComponent(
QwtAbstractScaleDraw::Backbone, false);
scaleItem->scaleDraw()->enableComponent(
QwtAbstractScaleDraw::Labels, false);
QPalette palette = scaleItem->palette();
palette.setBrush(QPalette::Foreground, QColor(plotColor));
palette.setBrush(QPalette::Text, QColor(plotColor));
scaleItem->setPalette(palette);
scaleItem->setBorderDistance(0);
scaleItem->attach(this);
}
zoomer = new XAxisScaleZoomer(canvas());
zoomer->setMousePattern(QwtEventPattern::MouseSelect3,
Qt::RightButton);
zoomer->setMousePattern(QwtEventPattern::MouseSelect2,
Qt::RightButton, Qt::ControlModifier);
installEventFilter(this);
static_cast<QFrame *>(canvas())->setLineWidth(0);
setContentsMargins(10, 10, 24, 20);
QMargins margins = contentsMargins();
margins.setBottom(0);
setContentsMargins(margins);
setAxisVisible(QwtAxis::YLeft, false);
setAxisVisible(QwtAxis::XTop, false);
QwtScaleWidget *scaleWidget = axisWidget(QwtAxis::XTop);
const int fmw = QFontMetrics(scaleWidget->font()).width("-XXXX.XX XX");
scaleWidget->setMinBorderDist(fmw / 2, fmw / 2);
setupVerticalBars();
setupReadouts();
markerIntersection1->setAxes(QwtAxis::XTop, QwtAxis::YLeft);
markerIntersection2->setAxes(QwtAxis::XTop, QwtAxis::YLeft);
}
dBgraph::~dBgraph()
{
canvas()->removeEventFilter(d_cursorReadouts);
canvas()->removeEventFilter(d_symbolCtrl);
delete d_formatter;
}
void dBgraph::replot()
{
if (!d_leftHandlesArea || !d_topHandlesArea) {
return;
}
d_leftHandlesArea->repaint();
d_topHandlesArea->repaint();
BasicPlot::replot();
}
void dBgraph::enableXaxisLabels()
{
d_topHandlesArea->installExtension(std::unique_ptr<HandlesAreaExtension>(new XTopRuller(this)));
}
void dBgraph::enableYaxisLabels()
{
d_leftHandlesArea->installExtension(std::unique_ptr<HandlesAreaExtension>(new YLeftRuller(this)));
}
void dBgraph::parametersOverrange(bool enable)
{
if (enable) {
d_plotBar->setPen(QPen(QColor(250, 0, 0, 50), 5, Qt::SolidLine));
} else {
d_plotBar->setPen(QPen(QColor(211, 211, 211, 50), 5, Qt::SolidLine));
}
}
void dBgraph::setAxesScales(double xmin, double xmax, double ymin, double ymax)
{
setAxisScale(QwtAxis::XTop, xmin, xmax);
setAxisScale(QwtAxis::YLeft, ymin, ymax);
}
void dBgraph::setAxesTitles(const QString& x, const QString& y)
{
QwtText xTitle(x);
QwtText yTitle(y);
QFont font = axisTitle(QwtAxis::XTop).font();
font.setWeight(QFont::Normal);
xTitle.setFont(font);
yTitle.setFont(font);
setAxisTitle(QwtAxis::XTop, xTitle);
setAxisTitle(QwtAxis::YLeft, yTitle);
}
void dBgraph::plot(double x, double y)
{
if (!d_plotBar->isVisible() && !xdata.size()) {
if (d_plotBarEnabled) {
d_plotBar->setVisible(true);
}
}
if (xdata.size() == numSamples) {
xdata[d_plotPosition] = x;
ydata[d_plotPosition] = y;
// Advance to next point
d_plotPosition++;
// Return to first point when reaching the end
if (d_plotPosition == numSamples) {
d_plotPosition = 0;
}
} else {
xdata.push_back(x);
ydata.push_back(y);
}
d_plotBar->setPlotCoord(QPointF(x, d_plotBar->plotCoord().y()));
curve.setRawSamples(xdata.data(), ydata.data(), xdata.size());
if (d_cursorsEnabled) {
onVCursor1Moved(d_vBar1->plotCoord().x());
onVCursor2Moved(d_vBar2->plotCoord().x());
}
replot();
}
bool dBgraph::eventFilter(QObject *object, QEvent *event)
{
if (object == canvas() && event->type() == QEvent::Resize)
{
d_leftHandlesArea->repaint();
d_bottomHandlesArea->setLeftPadding(d_leftHandlesArea->width() + 10);
d_bottomHandlesArea->setRightPadding(d_rightHandlesArea->width() + 24);
}
return QObject::eventFilter(object, event);
}
int dBgraph::getNumSamples() const
{
return numSamples;
}
QString dBgraph::getScaleValueFormat(double value, QwtAxisId scale) const
{
auto *scaleDraw = static_cast<const OscScaleDraw *>(
axisScaleDraw(scale));
return d_formatter->format(value,
scaleDraw->getUnitType(),
scaleDraw->getFloatPrecison());
}
QString dBgraph::getScaleValueFormat(double value, QwtAxisId scale,
int precision) const
{
auto *scaleDraw = static_cast<const OscScaleDraw *>(
axisScaleDraw(scale));
return d_formatter->format(value,
scaleDraw->getUnitType(),
precision);
}
void dBgraph::setShowZero(bool en)
{
OscScaleEngine *scaleLeft = new OscScaleEngine();
scaleLeft->setMajorTicksCount(7);
scaleLeft->showZero(en);
this->setAxisScaleEngine(QwtAxis::YLeft,
static_cast<QwtScaleEngine *>(scaleLeft));
replot();
}
const QwtScaleWidget *dBgraph::getAxisWidget(QwtAxisId id)
{
return axisWidget(id);
}
void dBgraph::setNumSamples(int num)
{
if (numSamples == num) {
return;
}
numSamples = (unsigned int) num;
reset();
replot();
}
void dBgraph::reset()
{
xdata.clear();
ydata.clear();
d_plotPosition = 0;
}
void dBgraph::setColor(const QColor& color)
{
this->color = color;
curve.setPen(QPen(color,thickness));
}
const QColor& dBgraph::getColor() const
{
return this->color;
}
void dBgraph::setThickness(int index)
{
double thickness = 0.5 * (index + 1);
this->thickness = thickness;
curve.setPen(QPen(color,thickness));
replot();
}
double dBgraph::getThickness()
{
return this->thickness;
}
QString dBgraph::xTitle() const
{
return axisTitle(QwtAxis::XTop).text();
}
QString dBgraph::yTitle() const
{
return axisTitle(QwtAxis::YLeft).text();
}
void dBgraph::setXTitle(const QString& title)
{
QwtText xTitle(title);
QFont font = axisTitle(QwtAxis::XTop).font();
font.setWeight(QFont::Normal);
xTitle.setFont(font);
setAxisTitle(QwtAxis::XTop, xTitle);
}
void dBgraph::setYTitle(const QString& title)
{
QwtText yTitle(title);
QFont font = axisTitle(QwtAxis::XTop).font();
font.setWeight(QFont::Normal);
yTitle.setFont(font);
setAxisTitle(QwtAxis::YLeft, yTitle);
d_cursorReadouts->setVoltageCursor1LabelText(title.mid(0,3)+"1 = ");
d_cursorReadouts->setVoltageCursor2LabelText(title.mid(0,3)+"2 = ");
d_cursorReadouts->setDeltaVoltageLabelText("Δ"+title.mid(0,3)+" = ");
}
void dBgraph::setXMin(double val)
{
zoomer->resetZoom();
setAxisScale(QwtAxis::XTop, val, xmax);
xmin = val;
draw_x->invalidateCache();
zoomer->setZoomBase();
replot();
auto div = axisScaleDiv(QwtAxis::XTop);
setXaxisNumDiv((div.ticks(2)).size() - 1);
setXaxisMajorTicksPos(div.ticks(2));
}
void dBgraph::setXMax(double val)
{
zoomer->resetZoom();
setAxisScale(QwtAxis::XTop, xmin, val);
xmax = val;
draw_x->invalidateCache();
zoomer->setZoomBase();
replot();
auto div = axisScaleDiv(QwtAxis::XTop);
setXaxisNumDiv((div.ticks(2)).size() - 1);
setXaxisMajorTicksPos(div.ticks(2));
}
void dBgraph::setYMin(double val)
{
setAxisScale(QwtAxis::YLeft, val, ymax);
ymin = val;
replot();
d_leftHandlesArea->repaint();
d_topHandlesArea->repaint();
double width = xmax - xmin;
double height = ymax - ymin;
zoomer->setZoomBase(QRectF(xmin, ymin, width, height));
}
void dBgraph::setYMax(double val)
{
setAxisScale(QwtAxis::YLeft, ymin, val);
ymax = val;
replot();
double width = xmax - xmin;
double height = ymax - ymin;
zoomer->setZoomBase(QRectF(xmin, ymin, width, height));
}
QString dBgraph::xUnit() const
{
return draw_x->getUnitType();;
}
QString dBgraph::yUnit() const
{
return draw_y->getUnitType();;
}
void dBgraph::setXUnit(const QString& unit)
{
draw_x->setUnitType(unit);
}
void dBgraph::setYUnit(const QString& unit)
{
draw_y->setUnitType(unit);
}
void dBgraph::useLogFreq(bool use_log_freq)
{
if (use_log_freq) {
setPlotLogaritmic(true);
this->setAxisScaleEngine(QwtAxis::XTop, new QwtLogScaleEngine);
replot();
auto div = axisScaleDiv(QwtAxis::XTop);
setXaxisNumDiv((div.ticks(2)).size() - 1);
setXaxisMajorTicksPos(div.ticks(2));
} else {
setPlotLogaritmic(false);
auto scaleTop = new OscScaleEngine;
scaleTop->setMajorTicksCount(9);
setXaxisNumDiv(8);
this->setAxisScaleEngine(QwtAxis::XTop, scaleTop);
}
this->log_freq = use_log_freq;
if (d_cursorsEnabled && isVisible()) {
onVCursor1Moved(d_vBar1->plotCoord().x());
onVCursor2Moved(d_vBar2->plotCoord().x());
}
// Use delta only when log scale is disabled and delta
// label mode is enabled
auto sw = axisWidget(QwtAxis::XTop);
auto *sd = dynamic_cast<OscScaleDraw *>(sw->scaleDraw());
sd->enableDeltaLabel(delta_label & (!use_log_freq));
sw->repaint();
replot();
}
void dBgraph::useDeltaLabel(bool use_delta)
{
if (delta_label != use_delta) {
delta_label = use_delta;
if (!log_freq) {
auto sw = axisWidget(QwtAxis::XTop);
auto *sd = dynamic_cast<OscScaleDraw *>(sw->scaleDraw());
sd->enableDeltaLabel(use_delta);
sw->repaint();
}
}
}
void dBgraph::sweepDone()
{
if (d_plotBarEnabled) {
d_plotBar->setVisible(false);
}
}
void dBgraph::onFrequencyCursorPositionChanged(int pos)
{
d_frequencyBar->setPixelPosition(pos);
Q_EMIT frequencySelected(d_frequencyBar->plotCoord().x());
}
void dBgraph::onFrequencyBarMoved(double frequency)
{
auto oldY = d_frequencyBar->plotCoord().y();
d_frequencyBar->setPlotCoord(QPointF(frequency, oldY));
}
void dBgraph::addReferenceWaveform(QVector<double> xData, QVector<double> yData)
{
reference.setSamples(xData, yData);
reference.attach(this);
replot();
}
void dBgraph::removeReferenceWaveform()
{
reference.detach();
replot();
}
bool dBgraph::addReferenceWaveformFromPlot()
{
if (xdata.size() == 0 || ydata.size() == 0) {
return false;
}
addReferenceWaveform(xdata, ydata);
return true;
}
QString dBgraph::formatXValue(double value, int precision) const
{
return d_formatter->format(value, "Hz", precision);
}
QString dBgraph::formatYValue(double value, int precision) const
{
return d_formatter->format(value, draw_y->getUnitType(), precision);
}
void dBgraph::onVCursor1Moved(double value)
{
QString text;
text = d_formatter->format(value, "Hz", 2);
d_cursorReadouts->setTimeCursor1Text(text);
text = cursorIntersection(value);
d_cursorReadouts->setVoltageCursor1Text(text);
double d1 = d_cursorReadouts->voltageCursor1Text().split(" ")[0].toDouble();
double d2 = d_cursorReadouts->voltageCursor2Text().split(" ")[0].toDouble();
if (text == "-") {
markerIntersection1->detach();
} else {
if (d_cursorsEnabled) {
markerIntersection1->attach(this);
markerIntersection1->setValue(value, d1);
}
}
replot();
d_cursorReadouts->setVoltageDeltaText(QString::number(d2-d1)+" "+
draw_y->getUnitType());
}
void dBgraph::onVCursor2Moved(double value)
{
QString text;
text = d_formatter->format(value, "Hz", 2);
d_cursorReadouts->setTimeCursor2Text(text);
text = cursorIntersection(value);
d_cursorReadouts->setVoltageCursor2Text(text);
double d1 = d_cursorReadouts->voltageCursor1Text().split(" ")[0].toDouble();
double d2 = d_cursorReadouts->voltageCursor2Text().split(" ")[0].toDouble();
if (text == "-") {
markerIntersection2->detach();
} else {
if (d_cursorsEnabled) {
markerIntersection2->attach(this);
markerIntersection2->setValue(value, d2);
}
}
replot();
d_cursorReadouts->setVoltageDeltaText(QString::number(d2-d1)+" "+
draw_y->getUnitType());
}
QString dBgraph::cursorIntersection(qreal freq)
{
if (xdata.size() == 0 || xdata.data()[xdata.size()-1] < freq) {
return QString("-");//for the case when there is no plot
} else {
double leftFreq,rightFreq,leftCustom,rightCustom;
int rightIndex = -1;
int leftIndex = -1;
for (int i=1; i<xdata.size(); i++) {
if (xdata.data()[i-1] <= freq && freq <= xdata.data()[i]) {
leftIndex=i-1;
rightIndex=i;
}
}
if (leftIndex == -1 || rightIndex == -1) {
return QString("-");
}
if (!log_freq) {
leftFreq = xdata.data()[leftIndex];
rightFreq = xdata.data()[rightIndex];
} else {
freq = log10(freq);
leftFreq = log10(xdata.data()[leftIndex]);
rightFreq = log10(xdata.data()[rightIndex]);
}
leftCustom = ydata.data()[leftIndex];
rightCustom = ydata.data()[rightIndex];
double val = (rightCustom - leftCustom)/(rightFreq - leftFreq)*
(freq-leftFreq)+leftCustom;
return QString::number(val,'f',2) +" "+ draw_y->getUnitType();
}
}
QVector<double> dBgraph::getXAxisData()
{
QVector<double> data;
for (size_t i = 0; i < curve.data()->size(); ++i) {
data.push_back(curve.data()->sample(i).x());
}
return data;
}
QVector<double> dBgraph::getYAxisData()
{
QVector<double> data;
for (size_t i = 0; i < curve.data()->size(); ++i) {
data.push_back(curve.data()->sample(i).y());
}
return data;
}
void dBgraph::enableFrequencyBar(bool enable)
{
d_frequencyBar->setVisible(enable);
d_frequencyBar->setPixelPosition(0);
}
void dBgraph::setYAxisInterval(double min, double max, double correction)
{
for (size_t i = 0; i < ydata.size(); ++i) {
double value = ydata[i];
bool modified = false;
if (value > max) {
value = value - correction;
modified = true;
} else if (value < min) {
value = value + correction;
modified = true;
}
if (modified) {
ydata[i] = value;
curve.setRawSamples(xdata.data(), ydata.data(), xdata.size());
replot();
}
}
}
void dBgraph::scaleDivChanged()
{
QwtPlot *plt = static_cast<QwtPlot *>((sender())->parent());
QwtInterval intv = plt->axisInterval(QwtAxis::XTop);
this->setAxisScale(QwtAxis::XTop, intv.minValue(), intv.maxValue());
this->replot();
onVCursor1Moved(d_vBar1->plotCoord().x());
onVCursor2Moved(d_vBar2->plotCoord().x());
}
void dBgraph::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::RightButton) {
Q_EMIT resetZoom();
#ifdef __ANDROID__
} else if (event->type() == QEvent::MouseButtonDblClick) {
zoomer->popZoom();
Q_EMIT zoomOut();
#endif
}
}
void dBgraph::onResetZoom()
{
zoomer->resetZoom();
}
#ifdef __ANDROID__
void dBgraph::onZoomOut()
{
zoomer->popZoom();
}
#endif
void dBgraph::showEvent(QShowEvent *event)
{
d_hCursorHandle1->updatePosition();
d_hCursorHandle2->updatePosition();
auto sw = axisWidget(QwtAxis::XTop);
sw->scaleDraw()->invalidateCache();
sw->repaint();
}
void dBgraph::setPlotBarEnabled(bool enabled)
{
d_plotBarEnabled = enabled;
}