]>
Commit | Line | Data |
---|---|---|
1 | //----------------------------------------------------------------------------- | |
2 | // This code is licensed to you under the terms of the GNU GPL, version 2 or, | |
3 | // at your option, any later version. See the LICENSE.txt file for the text of | |
4 | // the license. | |
5 | //----------------------------------------------------------------------------- | |
6 | // GUI (QT) | |
7 | //----------------------------------------------------------------------------- | |
8 | ||
9 | #include <iostream> | |
10 | #include <QPainterPath> | |
11 | #include <QBrush> | |
12 | #include <QPen> | |
13 | #include <QTimer> | |
14 | #include <QCloseEvent> | |
15 | #include <QMouseEvent> | |
16 | #include <QKeyEvent> | |
17 | #include <math.h> | |
18 | #include <limits.h> | |
19 | #include <stdio.h> | |
20 | #include "proxguiqt.h" | |
21 | #include "proxgui.h" | |
22 | ||
23 | void ProxGuiQT::ShowGraphWindow(void) | |
24 | { | |
25 | emit ShowGraphWindowSignal(); | |
26 | } | |
27 | ||
28 | void ProxGuiQT::RepaintGraphWindow(void) | |
29 | { | |
30 | emit RepaintGraphWindowSignal(); | |
31 | } | |
32 | ||
33 | void ProxGuiQT::HideGraphWindow(void) | |
34 | { | |
35 | emit HideGraphWindowSignal(); | |
36 | } | |
37 | ||
38 | void ProxGuiQT::_ShowGraphWindow(void) | |
39 | { | |
40 | if(!plotapp) | |
41 | return; | |
42 | ||
43 | if (!plotwidget) | |
44 | plotwidget = new ProxWidget(); | |
45 | ||
46 | plotwidget->show(); | |
47 | } | |
48 | ||
49 | void ProxGuiQT::_RepaintGraphWindow(void) | |
50 | { | |
51 | if (!plotapp || !plotwidget) | |
52 | return; | |
53 | ||
54 | plotwidget->update(); | |
55 | } | |
56 | ||
57 | void ProxGuiQT::_HideGraphWindow(void) | |
58 | { | |
59 | if (!plotapp || !plotwidget) | |
60 | return; | |
61 | ||
62 | plotwidget->hide(); | |
63 | } | |
64 | ||
65 | void ProxGuiQT::MainLoop() | |
66 | { | |
67 | plotapp = new QApplication(argc, argv); | |
68 | ||
69 | connect(this, SIGNAL(ShowGraphWindowSignal()), this, SLOT(_ShowGraphWindow())); | |
70 | connect(this, SIGNAL(RepaintGraphWindowSignal()), this, SLOT(_RepaintGraphWindow())); | |
71 | connect(this, SIGNAL(HideGraphWindowSignal()), this, SLOT(_HideGraphWindow())); | |
72 | ||
73 | plotapp->exec(); | |
74 | } | |
75 | ||
76 | ProxGuiQT::ProxGuiQT(int argc, char **argv) : plotapp(NULL), plotwidget(NULL), | |
77 | argc(argc), argv(argv) | |
78 | { | |
79 | } | |
80 | ||
81 | ProxGuiQT::~ProxGuiQT(void) | |
82 | { | |
83 | if (plotwidget) { | |
84 | delete plotwidget; | |
85 | plotwidget = NULL; | |
86 | } | |
87 | ||
88 | if (plotapp) { | |
89 | plotapp->quit(); | |
90 | delete plotapp; | |
91 | plotapp = NULL; | |
92 | } | |
93 | } | |
94 | ||
95 | void ProxWidget::paintEvent(QPaintEvent *event) | |
96 | { | |
97 | QPainter painter(this); | |
98 | QPainterPath penPath, whitePath, greyPath, lightgreyPath, cursorAPath, cursorBPath; | |
99 | QRect r; | |
100 | QBrush brush(QColor(100, 255, 100)); | |
101 | QPen pen(QColor(100, 255, 100)); | |
102 | ||
103 | painter.setFont(QFont("Arial", 10)); | |
104 | ||
105 | if(GraphStart < 0) { | |
106 | GraphStart = 0; | |
107 | } | |
108 | ||
109 | if (CursorAPos > GraphTraceLen) | |
110 | CursorAPos= 0; | |
111 | if(CursorBPos > GraphTraceLen) | |
112 | CursorBPos= 0; | |
113 | ||
114 | r = rect(); | |
115 | ||
116 | painter.fillRect(r, QColor(0, 0, 0)); | |
117 | ||
118 | whitePath.moveTo(r.left() + 40, r.top()); | |
119 | whitePath.lineTo(r.left() + 40, r.bottom()); | |
120 | ||
121 | int zeroHeight = r.top() + (r.bottom() - r.top()) / 2; | |
122 | ||
123 | greyPath.moveTo(r.left(), zeroHeight); | |
124 | greyPath.lineTo(r.right(), zeroHeight); | |
125 | painter.setPen(QColor(100, 100, 100)); | |
126 | painter.drawPath(greyPath); | |
127 | ||
128 | // plot X and Y grid lines | |
129 | int i; | |
130 | if ((PlotGridX > 0) && ((PlotGridX * GraphPixelsPerPoint) > 1)) { | |
131 | for(i = 40; i < r.right(); i += (int)(PlotGridX * GraphPixelsPerPoint)) { | |
132 | //SelectObject(hdc, GreyPenLite); | |
133 | //MoveToEx(hdc, r.left + i, r.top, NULL); | |
134 | //LineTo(hdc, r.left + i, r.bottom); | |
135 | lightgreyPath.moveTo(r.left()+i,r.top()); | |
136 | lightgreyPath.lineTo(r.left()+i,r.bottom()); | |
137 | painter.drawPath(lightgreyPath); | |
138 | } | |
139 | } | |
140 | if ((PlotGridY > 0) && ((PlotGridY * GraphPixelsPerPoint) > 1)){ | |
141 | for(i = 0; i < ((r.top() + r.bottom())>>1); i += (int)(PlotGridY * GraphPixelsPerPoint)) { | |
142 | lightgreyPath.moveTo(r.left() + 40,zeroHeight + i); | |
143 | lightgreyPath.lineTo(r.right(),zeroHeight + i); | |
144 | painter.drawPath(lightgreyPath); | |
145 | lightgreyPath.moveTo(r.left() + 40,zeroHeight - i); | |
146 | lightgreyPath.lineTo(r.right(),zeroHeight - i); | |
147 | painter.drawPath(lightgreyPath); | |
148 | } | |
149 | } | |
150 | ||
151 | int startMax = | |
152 | (GraphTraceLen - (int)((r.right() - r.left() - 40) / GraphPixelsPerPoint)); | |
153 | if(startMax < 0) { | |
154 | startMax = 0; | |
155 | } | |
156 | if(GraphStart > startMax) { | |
157 | GraphStart = startMax; | |
158 | } | |
159 | ||
160 | int absYMax = 1; | |
161 | ||
162 | for(i = GraphStart; ; i++) { | |
163 | if(i >= GraphTraceLen) { | |
164 | break; | |
165 | } | |
166 | if(fabs((double)GraphBuffer[i]) > absYMax) { | |
167 | absYMax = (int)fabs((double)GraphBuffer[i]); | |
168 | } | |
169 | int x = 40 + (int)((i - GraphStart)*GraphPixelsPerPoint); | |
170 | if(x > r.right()) { | |
171 | break; | |
172 | } | |
173 | } | |
174 | ||
175 | absYMax = (int)(absYMax*1.2 + 1); | |
176 | ||
177 | // number of points that will be plotted | |
178 | int span = (int)((r.right() - r.left()) / GraphPixelsPerPoint); | |
179 | // one label every 100 pixels, let us say | |
180 | int labels = (r.right() - r.left() - 40) / 100; | |
181 | if(labels <= 0) labels = 1; | |
182 | int pointsPerLabel = span / labels; | |
183 | if(pointsPerLabel <= 0) pointsPerLabel = 1; | |
184 | ||
185 | int yMin = INT_MAX; | |
186 | int yMax = INT_MIN; | |
187 | int yMean = 0; | |
188 | int n = 0; | |
189 | ||
190 | for(i = GraphStart; ; i++) { | |
191 | if(i >= GraphTraceLen) { | |
192 | break; | |
193 | } | |
194 | int x = 40 + (int)((i - GraphStart)*GraphPixelsPerPoint); | |
195 | if(x > r.right() + GraphPixelsPerPoint) { | |
196 | break; | |
197 | } | |
198 | ||
199 | int y = GraphBuffer[i]; | |
200 | if(y < yMin) { | |
201 | yMin = y; | |
202 | } | |
203 | if(y > yMax) { | |
204 | yMax = y; | |
205 | } | |
206 | yMean += y; | |
207 | n++; | |
208 | ||
209 | y = (y * (r.top() - r.bottom()) / (2*absYMax)) + zeroHeight; | |
210 | if(i == GraphStart) { | |
211 | penPath.moveTo(x, y); | |
212 | } else { | |
213 | penPath.lineTo(x, y); | |
214 | } | |
215 | ||
216 | if(GraphPixelsPerPoint > 10) { | |
217 | QRect f(QPoint(x - 3, y - 3),QPoint(x + 3, y + 3)); | |
218 | painter.fillRect(f, brush); | |
219 | } | |
220 | ||
221 | if(((i - GraphStart) % pointsPerLabel == 0) && i != GraphStart) { | |
222 | whitePath.moveTo(x, zeroHeight - 3); | |
223 | whitePath.lineTo(x, zeroHeight + 3); | |
224 | ||
225 | char str[100]; | |
226 | sprintf(str, "+%d", (i - GraphStart)); | |
227 | ||
228 | painter.setPen(QColor(255, 255, 255)); | |
229 | QRect size; | |
230 | QFontMetrics metrics(painter.font()); | |
231 | size = metrics.boundingRect(str); | |
232 | painter.drawText(x - (size.right() - size.left()), zeroHeight + 9, str); | |
233 | ||
234 | penPath.moveTo(x,y); | |
235 | } | |
236 | ||
237 | if(i == CursorAPos || i == CursorBPos) { | |
238 | QPainterPath *cursorPath; | |
239 | ||
240 | if(i == CursorAPos) { | |
241 | cursorPath = &cursorAPath; | |
242 | } else { | |
243 | cursorPath = &cursorBPath; | |
244 | } | |
245 | cursorPath->moveTo(x, r.top()); | |
246 | cursorPath->lineTo(x, r.bottom()); | |
247 | penPath.moveTo(x, y); | |
248 | } | |
249 | } | |
250 | ||
251 | if(n != 0) { | |
252 | yMean /= n; | |
253 | } | |
254 | ||
255 | painter.setPen(QColor(255, 255, 255)); | |
256 | painter.drawPath(whitePath); | |
257 | painter.setPen(pen); | |
258 | painter.drawPath(penPath); | |
259 | painter.setPen(QColor(255, 255, 0)); | |
260 | painter.drawPath(cursorAPath); | |
261 | painter.setPen(QColor(255, 0, 255)); | |
262 | painter.drawPath(cursorBPath); | |
263 | ||
264 | char str[100]; | |
265 | sprintf(str, "@%d max=%d min=%d mean=%d n=%d/%d dt=%d [%.3f] zoom=%.3f CursorA=%d [%d] CursorB=%d [%d]", | |
266 | GraphStart, yMax, yMin, yMean, n, GraphTraceLen, | |
267 | CursorBPos - CursorAPos, (CursorBPos - CursorAPos)/CursorScaleFactor,GraphPixelsPerPoint,CursorAPos,GraphBuffer[CursorAPos],CursorBPos,GraphBuffer[CursorBPos]); | |
268 | ||
269 | painter.setPen(QColor(255, 255, 255)); | |
270 | painter.drawText(50, r.bottom() - 20, str); | |
271 | } | |
272 | ||
273 | ProxWidget::ProxWidget(QWidget *parent) : QWidget(parent), GraphStart(0), GraphPixelsPerPoint(1) | |
274 | { | |
275 | resize(600, 500); | |
276 | ||
277 | QPalette palette(QColor(0,0,0,0)); | |
278 | palette.setColor(QPalette::WindowText, QColor(255,255,255)); | |
279 | palette.setColor(QPalette::Text, QColor(255,255,255)); | |
280 | palette.setColor(QPalette::Button, QColor(100, 100, 100)); | |
281 | setPalette(palette); | |
282 | setAutoFillBackground(true); | |
283 | } | |
284 | ||
285 | void ProxWidget::closeEvent(QCloseEvent *event) | |
286 | { | |
287 | event->ignore(); | |
288 | this->hide(); | |
289 | } | |
290 | ||
291 | void ProxWidget::mouseMoveEvent(QMouseEvent *event) | |
292 | { | |
293 | int x = event->x(); | |
294 | x -= 40; | |
295 | x = (int)(x / GraphPixelsPerPoint); | |
296 | x += GraphStart; | |
297 | if((event->buttons() & Qt::LeftButton)) { | |
298 | CursorAPos = x; | |
299 | } else if (event->buttons() & Qt::RightButton) { | |
300 | CursorBPos = x; | |
301 | } | |
302 | ||
303 | ||
304 | this->update(); | |
305 | } | |
306 | ||
307 | void ProxWidget::keyPressEvent(QKeyEvent *event) | |
308 | { | |
309 | switch(event->key()) { | |
310 | case Qt::Key_Down: | |
311 | if(GraphPixelsPerPoint <= 50) { | |
312 | GraphPixelsPerPoint *= 2; | |
313 | } | |
314 | break; | |
315 | ||
316 | case Qt::Key_Up: | |
317 | if(GraphPixelsPerPoint >= 0.02) { | |
318 | GraphPixelsPerPoint /= 2; | |
319 | } | |
320 | break; | |
321 | ||
322 | case Qt::Key_Right: | |
323 | if(GraphPixelsPerPoint < 20) { | |
324 | GraphStart += (int)(20 / GraphPixelsPerPoint); | |
325 | } else { | |
326 | GraphStart++; | |
327 | } | |
328 | break; | |
329 | ||
330 | case Qt::Key_Left: | |
331 | if(GraphPixelsPerPoint < 20) { | |
332 | GraphStart -= (int)(20 / GraphPixelsPerPoint); | |
333 | } else { | |
334 | GraphStart--; | |
335 | } | |
336 | break; | |
337 | ||
338 | default: | |
339 | QWidget::keyPressEvent(event); | |
340 | return; | |
341 | break; | |
342 | } | |
343 | ||
344 | this->update(); | |
345 | } |