/[qspeakers]/plot.cpp
ViewVC logotype

Contents of /plot.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 190 - (show annotations)
Thu Jun 1 23:05:02 2017 UTC (3 weeks, 6 days ago) by ben
File size: 4362 byte(s)
make previously non-translatable strings as translatable and update translations
1 #include "plot.h"
2 #include <qwt_scale_engine.h>
3 #include <qwt_plot_grid.h>
4 #include <qwt_symbol.h>
5
6 Plot::Plot(QWidget *parent) :
7 QwtPlot(parent),
8 xmin(10.0),
9 xmax(1000.0)
10 {
11 setAutoReplot(false);
12 initializeScales();
13 initializeCanvas();
14 initializeCurve();
15 initializeMarkers();
16 initializePicker();
17 setAutoReplot(true);
18 }
19
20 Plot::Plot(QwtText title, QWidget *parent) :
21 QwtPlot(title, parent),
22 xmin(10.0),
23 xmax(1000.0)
24 {
25 setAutoReplot(false);
26 initializeScales();
27 initializeCanvas();
28 initializeCurve();
29 initializeMarkers();
30 initializePicker();
31 setAutoReplot(true);
32 }
33
34 Plot::~Plot()
35 {
36 curve->setSamples(QVector<QPointF>());
37 replot();
38 delete picker;
39 delete marker3;
40 delete markerP;
41 delete curve;
42 }
43
44 void Plot::plot(const QList<double> x, QList<double> y)
45 {
46 QVector<QPointF> samples;
47 double xpeak = xmax, ypeak = -120;
48 double x3 = xmax, y3 = -120;
49
50 /* roll in inverse order to find lower fpeak/f3 */
51 for (int i = x.size() - 1; i > -1; i--) {
52 /* search for fpeak */
53 if (y[i] >= ypeak) {
54 ypeak = y[i];
55 xpeak = x[i];
56 }
57
58 /* search for f3 */
59 if (round(y[i]) == round(ypeak - 3.0)) {
60 x3 = x[i];
61 y3 = y[i];
62 }
63
64
65 QPointF p;
66 p.setX(x[i]);
67 if (i < y.size())
68 p.setY(y[i]);
69 samples.append(p);
70 }
71
72 curve->setSamples(samples);
73 //ypeak = round(ypeak);
74 //y3 = round(y3);
75 showPeak(xpeak, ypeak);
76 show3dB(x3, y3);
77 replot();
78 }
79
80 void Plot::initializeCurve() {
81 curve = new QwtPlotCurve("Amplitude");
82 curve->setRenderHint(QwtPlotItem::RenderAntialiased);
83 curve->setPen(QPen(Qt::darkBlue, 1, Qt::SolidLine));
84 curve->setLegendAttribute(QwtPlotCurve::LegendShowLine);
85 curve->setYAxis(QwtPlot::yLeft);
86 curve->attach(this);
87 }
88
89 void Plot::initializeMarkers()
90 {
91 marker3 = new QwtPlotMarker();
92 marker3->setLineStyle(QwtPlotMarker::VLine);
93 marker3->setLabelAlignment(Qt::AlignRight | Qt::AlignBottom);
94 marker3->setLinePen(QPen(Qt::blue, 1, Qt::DashDotLine));
95 marker3->attach(this);
96
97 markerP = new QwtPlotMarker();
98 markerP->setLineStyle(QwtPlotMarker::HLine);
99 markerP->setLabelAlignment(Qt::AlignRight | Qt::AlignBottom);
100 markerP->setLinePen(QPen(Qt::red, 1, Qt::DashDotLine));
101 markerP->attach(this);
102
103 }
104
105 void Plot::initializePicker()
106 {
107 picker = new QwtPlotPicker(this->canvas());
108 picker->setRubberBand(QwtPlotPicker::CrossRubberBand);
109 picker->setTrackerMode(QwtPlotPicker::AlwaysOn);
110 }
111
112 double Plot::getXmax() const
113 {
114 return xmax;
115 }
116
117 double Plot::getXmin() const
118 {
119 return xmin;
120 }
121
122 void Plot::initializeCanvas() {
123
124 setCanvasBackground(QColor(Qt::white));
125
126 QwtPlotGrid *grid = new QwtPlotGrid;
127 grid->enableXMin(true);
128 #if (QWT_VERSION >= QT_VERSION_CHECK(6,1,0))
129 grid->setMajorPen(QPen(Qt::black, 1, Qt::DotLine));
130 grid->setMinorPen(QPen(Qt::gray, 1, Qt::DotLine));
131 #else
132 grid->setMajPen(QPen(Qt::black, 1, Qt::DotLine));
133 grid->setMinPen(QPen(Qt::gray, 1, Qt::DotLine));
134 #endif
135 grid->attach(this);
136 }
137
138 void Plot::initializeScales() {
139 QwtText XAxisLabel;
140 XAxisLabel.setText(tr("Frequency [Hz]"));
141 setAxisTitle(QwtPlot::xBottom, XAxisLabel);
142 setAxisMaxMajor(QwtPlot::xBottom, 3);
143 setAxisMaxMinor(QwtPlot::xBottom, 10);
144 #if (QWT_VERSION >= QT_VERSION_CHECK(6,1,0))
145 setAxisScaleEngine(QwtPlot::xBottom, new QwtLogScaleEngine());
146 #else
147 setAxisScaleEngine(QwtPlot::xBottom, new QwtLog10ScaleEngine());
148 #endif
149 setAxisScale(QwtPlot::xBottom, xmin, xmax);
150
151 QwtText YAxisLabel;
152 YAxisLabel.setText(tr("Sound pressure [dB]"));
153 setAxisTitle(QwtPlot::yLeft, YAxisLabel);
154 setAxisMaxMajor(QwtPlot::yLeft, 5);
155 setAxisMaxMinor(QwtPlot::yLeft, 10);
156 setAxisScale(QwtPlot::yLeft, -40.0, 10.0);
157 }
158
159 void Plot::showPeak(double x, double y)
160 {
161 QString label = QString(tr("Peak: %1 dB")).arg((int)round(y));
162
163 QwtText text(label);
164 text.setColor(Qt::red);
165
166 markerP->setValue(x, y);
167 markerP->setLabel(text);
168 }
169
170 void Plot::show3dB(double x, double y)
171 {
172 QString label = QString(tr("%1 dB at %2 Hz")).arg((int)round(y)).arg((int)x);
173
174 QwtText text(label);
175 text.setColor(Qt::blue);
176
177 marker3->setValue(x, -3.0);
178 marker3->setLabel(text);
179 }

  ViewVC Help
Powered by ViewVC 1.1.26