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