/[qspeakers]/branches/qtcharts/plot.cpp
ViewVC logotype

Contents of /branches/qtcharts/plot.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 287 - (show annotations)
Sun Nov 22 19:11:12 2020 UTC (6 months, 3 weeks ago) by ben
File size: 5008 byte(s)
Implement gnuplot export and OpenSCAD export (not finished)
1 #include "plot.h"
2 #include <QtCharts/QtCharts>
3
4 using namespace QtCharts;
5
6 Plot::Plot(QWidget *parent) :
7 QChartView(parent),
8 xmin(10.0),
9 xmax(1000.0),
10 pointerLabel(nullptr),
11 vLine(nullptr),
12 vLabel(nullptr)
13 {
14 initializeChart();
15 initializeCurve();
16 initializeScales();
17 }
18
19 Plot::Plot(QString title, QWidget *parent) :
20 QChartView(parent),
21 xmin(10.0),
22 xmax(1000.0),
23 pointerLabel(nullptr),
24 vLine(nullptr),
25 vLabel(nullptr)
26 {
27 initializeChart(title);
28 initializeCurve();
29 initializeScales();
30 }
31
32 Plot::~Plot()
33 {
34 curve->clear();
35 delete curve;
36 delete chart;
37 delete vLine;
38 delete vLabel;
39 delete pointerLabel;
40 }
41
42 void Plot::appendPointF(const QPointF &pointf)
43 {
44 curve->append(pointf);
45 }
46
47 void Plot::plot(const QList<double> x, QList<double> y)
48 {
49 QList<QPointF> samples;
50 for (int i = 0; i < x.size(); i++) {
51 QPointF p(x[i], y[i]);
52 curve->append(p);
53 }
54 }
55
56 void Plot::clear()
57 {
58 curve->clear();
59 }
60
61 void Plot::setUseOpenGL(bool enable)
62 {
63 curve->setUseOpenGL(enable);
64 }
65
66 void Plot::initializeCurve() {
67 curve = new QLineSeries(chart);
68 //curve->setUseOpenGL(true);
69 chart->addSeries(curve);
70 }
71
72
73 void Plot::draw3dbVLine()
74 {
75 double x = curveXfromY(-3.0);
76 double y1 = 10.0, y2 = -40.0;
77
78 QPointF value1(x, y1);
79 QPointF point1 = chart->mapToPosition(value1);
80 QPointF value2 = QPoint(x, y2);
81 QPointF point2 = chart->mapToPosition(value2);
82 QLineF line (point1, point2);
83 if (vLine) {
84 this->scene()->removeItem(vLine);
85 delete vLine;
86 vLine = nullptr;
87 }
88 vLine = this->scene()->addLine(line, QPen(Qt::DashLine));
89
90 if (vLabel) {
91 this->scene()->removeItem(vLabel);
92 delete vLabel;
93 vLabel = nullptr;
94 }
95 QString label = QString(tr("%1 dB at %2 Hz")).arg(-3.0).arg(round(x));
96 vLabel = this->scene()->addSimpleText(label);
97 QPointF pos = chart->mapToPosition(value1);
98 vLabel->setPos(pos);
99 }
100
101 void Plot::resizeEvent(QResizeEvent *event)
102 {
103 QChartView::resizeEvent(event);
104 draw3dbVLine();
105 }
106
107 void Plot::mousePressEvent(QMouseEvent *event)
108 {
109 if (!pointerLabel) {
110 QPoint pos = event->pos();
111 QPointF val = chart->mapToValue(pos);
112 double y = curveYfromX(val.x());
113 QString label = QString(tr("%1 dB at %2 Hz")).arg(round(y)).arg(round(val.x()));
114 pointerLabel = this->scene()->addSimpleText(label);
115 pointerLabel->setPos(pos.x() - pointerLabel->boundingRect().width(), pos.y() - pointerLabel->boundingRect().height());
116 event->accept();
117 }
118 }
119
120 void Plot::mouseMoveEvent(QMouseEvent *event)
121 {
122 if (pointerLabel) {
123 QPoint pos = event->pos();
124 QPointF val = chart->mapToValue(pos);
125 double y = curveYfromX(val.x());
126 QString label = QString(tr("%1 dB at %2 Hz")).arg(round(y)).arg(round(val.x()));
127 pointerLabel->setText(label);
128 pointerLabel->setPos(pos.x() - pointerLabel->boundingRect().width(), pos.y() - pointerLabel->boundingRect().height());
129 event->accept();
130 }
131 }
132
133 void Plot::mouseReleaseEvent(QMouseEvent *event)
134 {
135 if (pointerLabel) {
136 this->scene()->removeItem(pointerLabel);
137 delete pointerLabel;
138 pointerLabel = nullptr;
139 event->accept();
140 }
141 }
142 double Plot::curveXfromY(double y)
143 {
144 double x = 1.0;
145 QList<QPointF> list = curve->points();
146 for (int i = 0; i < list.size() - 1; i++) {
147 QPointF p1 = list.at(i);
148 QPointF p2 = list.at(i+1);
149 if (p1.y() <= y && p2.y() > y) {
150 x = p1.x();
151 break;
152 }
153 }
154
155 return x;
156 }
157
158 double Plot::curveYfromX(double x)
159 {
160
161 double y = 0.0;
162 QList<QPointF> list = curve->points();
163 for (int i = 0; i < list.size() - 1; i++) {
164 QPointF p1 = list.at(i);
165 QPointF p2 = list.at(i+1);
166 if (p1.x() <= x && p2.x() > x) {
167 y = p1.y();
168 break;
169 }
170 }
171
172 return y;
173 }
174
175 double Plot::getXmax() const
176 {
177 return xmax;
178 }
179
180 QLineSeries* Plot::series() const
181 {
182 return curve;
183 }
184
185 double Plot::getXmin() const
186 {
187 return xmin;
188 }
189
190 void Plot::initializeChart(const QString& title)
191 {
192 chart = new QChart();
193 chart->setTitle(title);
194 chart->legend()->hide();
195 this->setChart(chart);
196 this->setRenderHint(QPainter::Antialiasing);
197 }
198
199 void Plot::initializeScales()
200 {
201 QLogValueAxis *XAxis = new QLogValueAxis();
202 XAxis->setBase(10.0);
203 XAxis->setLabelFormat("%g");
204 XAxis->setTitleText(tr("Frequency [Hz]"));
205 XAxis->setMax(xmax);
206 XAxis->setMin(xmin);
207 XAxis->setMinorTickCount(8);
208 chart->addAxis(XAxis, Qt::AlignBottom);
209 curve->attachAxis(XAxis);
210
211 QValueAxis *YAxis = new QValueAxis();
212 YAxis->setTitleText(tr("Sound pressure [dB]"));
213 YAxis->setLabelFormat("%g");
214 YAxis->setMax(10);
215 YAxis->setMin(-40);
216 YAxis->setTickCount(6);
217 YAxis->setMinorTickCount(1);
218 chart->addAxis(YAxis, Qt::AlignLeft);
219 curve->attachAxis(YAxis);
220 }

  ViewVC Help
Powered by ViewVC 1.1.26