]>
Commit | Line | Data |
---|---|---|
1 | //----------------------------------------------------------------------------- | |
2 | // Copyright (C) 2009 Michael Gernoth <michael at gernoth.net> | |
3 | // | |
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 | ||
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> | |
21 | #include <stdio.h> | |
22 | #include "proxguiqt.h" | |
23 | #include "proxgui.h" | |
24 | ||
25 | int GridOffset= 0; | |
26 | bool GridLocked= 0; | |
27 | int startMax; | |
28 | int PageWidth; | |
29 | ||
30 | void ProxGuiQT::ShowGraphWindow(void) | |
31 | { | |
32 | emit ShowGraphWindowSignal(); | |
33 | } | |
34 | ||
35 | void ProxGuiQT::RepaintGraphWindow(void) | |
36 | { | |
37 | emit RepaintGraphWindowSignal(); | |
38 | } | |
39 | ||
40 | void ProxGuiQT::HideGraphWindow(void) | |
41 | { | |
42 | emit HideGraphWindowSignal(); | |
43 | } | |
44 | ||
45 | void ProxGuiQT::_ShowGraphWindow(void) | |
46 | { | |
47 | if(!plotapp) | |
48 | return; | |
49 | ||
50 | if (!plotwidget) | |
51 | plotwidget = new ProxWidget(); | |
52 | ||
53 | plotwidget->show(); | |
54 | } | |
55 | ||
56 | void ProxGuiQT::_RepaintGraphWindow(void) | |
57 | { | |
58 | if (!plotapp || !plotwidget) | |
59 | return; | |
60 | ||
61 | plotwidget->update(); | |
62 | } | |
63 | ||
64 | void ProxGuiQT::_HideGraphWindow(void) | |
65 | { | |
66 | if (!plotapp || !plotwidget) | |
67 | return; | |
68 | ||
69 | plotwidget->hide(); | |
70 | } | |
71 | ||
72 | void ProxGuiQT::MainLoop() | |
73 | { | |
74 | plotapp = new QApplication(argc, argv); | |
75 | ||
76 | connect(this, SIGNAL(ShowGraphWindowSignal()), this, SLOT(_ShowGraphWindow())); | |
77 | connect(this, SIGNAL(RepaintGraphWindowSignal()), this, SLOT(_RepaintGraphWindow())); | |
78 | connect(this, SIGNAL(HideGraphWindowSignal()), this, SLOT(_HideGraphWindow())); | |
79 | ||
80 | plotapp->exec(); | |
81 | } | |
82 | ||
83 | ProxGuiQT::ProxGuiQT(int argc, char **argv) : plotapp(NULL), plotwidget(NULL), argc(argc), argv(argv) {} | |
84 | ||
85 | ProxGuiQT::~ProxGuiQT(void) | |
86 | { | |
87 | if (plotwidget) { | |
88 | delete plotwidget; | |
89 | plotwidget = NULL; | |
90 | } | |
91 | ||
92 | if (plotapp) { | |
93 | plotapp->quit(); | |
94 | delete plotapp; | |
95 | plotapp = NULL; | |
96 | } | |
97 | } | |
98 | ||
99 | // solid colors | |
100 | #define QT_ORANGE QColor(255, 153, 0) | |
101 | #define QT_WHITE QColor(255, 255, 255) | |
102 | #define QT_YELLOW QColor(255, 255, 0) | |
103 | #define QT_MAGENTA QColor(255, 0, 255) | |
104 | #define QT_LIGHTBLUE QColor(0, 0, 205) | |
105 | #define QT_LIGHTGREEN QColor(100, 255, 100) | |
106 | #define QT_GRAY QColor(100,100,100) | |
107 | #define QT_BLACK QColor(0,0,0) | |
108 | // transparent colors | |
109 | #define QT_ORANGE_TS QColor(255, 153, 0, 96) | |
110 | #define QT_RED_TS QColor(255, 0, 0, 64) | |
111 | #define QT_BLACK_TS QColor(0,0,0,0) | |
112 | ||
113 | void ProxWidget::paintEvent(QPaintEvent *event) | |
114 | { | |
115 | QPainter painter(this); | |
116 | QPainterPath penPath, whitePath, greyPath, lightgreyPath, cursorAPath, cursorBPath, cursorCPath, cursorDPath; | |
117 | QRect r; | |
118 | QBrush brush(QT_LIGHTGREEN); | |
119 | QPen pen(QT_LIGHTGREEN); | |
120 | ||
121 | painter.setFont(QFont("Arial", 10)); | |
122 | ||
123 | if(GraphStart < 0) | |
124 | GraphStart = 0; | |
125 | ||
126 | if (CursorAPos > GraphTraceLen) CursorAPos = 0; | |
127 | if (CursorBPos > GraphTraceLen) CursorBPos = 0; | |
128 | if (CursorCPos > GraphTraceLen) CursorCPos = 0; | |
129 | if (CursorDPos > GraphTraceLen) CursorDPos = 0; | |
130 | ||
131 | r = rect(); | |
132 | painter.fillRect(r, QT_BLACK); | |
133 | ||
134 | whitePath.moveTo(r.left() + 40, r.top()); | |
135 | whitePath.lineTo(r.left() + 40, r.bottom()); | |
136 | ||
137 | int zeroHeight = r.top() + (r.bottom() - r.top()) / 2; | |
138 | ||
139 | greyPath.moveTo(r.left(), zeroHeight); | |
140 | greyPath.lineTo(r.right(), zeroHeight); | |
141 | painter.setPen(QT_GRAY); | |
142 | painter.drawPath(greyPath); | |
143 | ||
144 | PageWidth = (int)((r.right() - r.left() - 40) / GraphPixelsPerPoint); | |
145 | ||
146 | // plot X and Y grid lines | |
147 | int i; | |
148 | if ((PlotGridX > 0) && ((PlotGridX * GraphPixelsPerPoint) > 1)) { | |
149 | for(i = 40 + (GridOffset * GraphPixelsPerPoint); i < r.right(); i += (int)(PlotGridX * GraphPixelsPerPoint)) { | |
150 | //SelectObject(hdc, GreyPenLite); | |
151 | //MoveToEx(hdc, r.left + i, r.top, NULL); | |
152 | //LineTo(hdc, r.left + i, r.bottom); | |
153 | lightgreyPath.moveTo(r.left()+i,r.top()); | |
154 | lightgreyPath.lineTo(r.left()+i,r.bottom()); | |
155 | painter.drawPath(lightgreyPath); | |
156 | } | |
157 | } | |
158 | if ((PlotGridY > 0) && ((PlotGridY * GraphPixelsPerPoint) > 1)){ | |
159 | for(i = 0; i < ((r.top() + r.bottom())>>1); i += (int)(PlotGridY * GraphPixelsPerPoint)) { | |
160 | lightgreyPath.moveTo(r.left() + 40,zeroHeight + i); | |
161 | lightgreyPath.lineTo(r.right(),zeroHeight + i); | |
162 | painter.drawPath(lightgreyPath); | |
163 | lightgreyPath.moveTo(r.left() + 40,zeroHeight - i); | |
164 | lightgreyPath.lineTo(r.right(),zeroHeight - i); | |
165 | painter.drawPath(lightgreyPath); | |
166 | } | |
167 | } | |
168 | ||
169 | startMax = (GraphTraceLen - (int)((r.right() - r.left() - 40) / GraphPixelsPerPoint)); | |
170 | ||
171 | if(startMax < 0) | |
172 | startMax = 0; | |
173 | ||
174 | if(GraphStart > startMax) | |
175 | GraphStart = startMax; | |
176 | ||
177 | int absYMax = 1; | |
178 | ||
179 | for(i = GraphStart; ;i++) { | |
180 | ||
181 | if(i >= GraphTraceLen) break; | |
182 | ||
183 | if (fabs((double)GraphBuffer[i]) > absYMax) | |
184 | absYMax = (int)fabs((double)GraphBuffer[i]); | |
185 | ||
186 | int x = 40 + (int)((i - GraphStart)*GraphPixelsPerPoint); | |
187 | ||
188 | if(x > r.right()) break; | |
189 | } | |
190 | ||
191 | absYMax = (int)(absYMax*1.2 + 1); | |
192 | ||
193 | // number of points that will be plotted | |
194 | int span = (int)((r.right() - r.left()) / GraphPixelsPerPoint); | |
195 | ||
196 | // one label every 100 pixels, let us say | |
197 | int labels = (r.right() - r.left() - 40) / 100; | |
198 | if(labels <= 0) labels = 1; | |
199 | ||
200 | int pointsPerLabel = span / labels; | |
201 | if(pointsPerLabel <= 0) pointsPerLabel = 1; | |
202 | ||
203 | int yMin = INT_MAX; | |
204 | int yMax = INT_MIN; | |
205 | int yMean = 0; | |
206 | int n = 0; | |
207 | //int stt_x1 = 0, stt_x2 = 0; | |
208 | ||
209 | for(i = GraphStart; ; i++) { | |
210 | if(i >= GraphTraceLen) break; | |
211 | ||
212 | // x == pixel pos. | |
213 | int x = 40 + (int)((i - GraphStart) * GraphPixelsPerPoint); | |
214 | ||
215 | // if x reaches end of box, stop loop | |
216 | if(x > r.right() + GraphPixelsPerPoint) break; | |
217 | ||
218 | int y = GraphBuffer[i]; | |
219 | if(y < yMin) | |
220 | yMin = y; | |
221 | ||
222 | if(y > yMax) | |
223 | yMax = y; | |
224 | ||
225 | yMean += y; | |
226 | n++; | |
227 | ||
228 | y = (y * (r.top() - r.bottom()) / (2*absYMax)) + zeroHeight; | |
229 | ||
230 | if(i == GraphStart) | |
231 | penPath.moveTo(x, y); | |
232 | else | |
233 | penPath.lineTo(x, y); | |
234 | ||
235 | ||
236 | // small white boxes (the dots on the signal) | |
237 | if(GraphPixelsPerPoint > 10) { | |
238 | QRect f(QPoint(x - 3, y - 3),QPoint(x + 3, y + 3)); | |
239 | painter.fillRect(f, brush); | |
240 | } | |
241 | ||
242 | if(((i - GraphStart) % pointsPerLabel == 0) && i != GraphStart) { | |
243 | whitePath.moveTo(x, zeroHeight - 3); | |
244 | whitePath.lineTo(x, zeroHeight + 3); | |
245 | ||
246 | char str[100]; | |
247 | sprintf(str, "+%d", (i - GraphStart)); | |
248 | ||
249 | painter.setPen( QT_WHITE ); | |
250 | QRect size; | |
251 | QFontMetrics metrics(painter.font()); | |
252 | size = metrics.boundingRect(str); | |
253 | painter.drawText(x - (size.right() - size.left()), zeroHeight + 9, str); | |
254 | ||
255 | penPath.moveTo(x,y); | |
256 | } | |
257 | ||
258 | if(i == CursorAPos || i == CursorBPos || i == CursorCPos || i == CursorDPos) { | |
259 | QPainterPath *cursorPath; | |
260 | ||
261 | if ( i == CursorAPos ) | |
262 | cursorPath = &cursorAPath; | |
263 | else if ( i == CursorBPos ) | |
264 | cursorPath = &cursorBPath; | |
265 | else if ( i == CursorCPos ) | |
266 | cursorPath = &cursorCPath; | |
267 | else | |
268 | cursorPath = &cursorDPath; | |
269 | ||
270 | cursorPath->moveTo(x, r.top()); | |
271 | cursorPath->lineTo(x, r.bottom()); | |
272 | penPath.moveTo(x, y); | |
273 | } | |
274 | } | |
275 | ||
276 | // Mark STT block in signal | |
277 | if ( CursorCPos > 0 ){ | |
278 | int foo = 40 + (int)((CursorCPos - GraphStart) * GraphPixelsPerPoint); | |
279 | int bar = 40 + ((CursorDPos - GraphStart) * GraphPixelsPerPoint); | |
280 | QRect r_stt(foo, r.top(), bar-foo, r.bottom() ); | |
281 | painter.fillRect(r_stt, QBrush( QT_ORANGE_TS )); | |
282 | painter.drawRect(r_stt); | |
283 | } | |
284 | ||
285 | // Mark Clock pulse | |
286 | //extern int PlotClock, PlockClockStartIndex; | |
287 | if ( PlotClock > 0){ | |
288 | for(int i = PlockClockStartIndex; ; i += PlotClock * 2) { | |
289 | ||
290 | if(i >= GraphTraceLen ) break; | |
291 | if ((CursorCPos > 0) && (i >= CursorCPos)) break; | |
292 | ||
293 | int foo = 40 + (int)((i - GraphStart) * GraphPixelsPerPoint); | |
294 | int bar = 40 + ((i + PlotClock - GraphStart) * GraphPixelsPerPoint); | |
295 | QRect r_clock(foo, r.top(), bar-foo, r.bottom() ); | |
296 | painter.fillRect(r_clock, QBrush( QT_RED_TS )); | |
297 | painter.drawRect(r_clock); | |
298 | } | |
299 | } | |
300 | ||
301 | if(n != 0) | |
302 | yMean /= n; | |
303 | ||
304 | painter.setPen( QT_WHITE ); painter.drawPath(whitePath); | |
305 | painter.setPen(pen); painter.drawPath(penPath); | |
306 | painter.setPen( QT_YELLOW ); painter.drawPath(cursorAPath); | |
307 | painter.setPen( QT_MAGENTA ); painter.drawPath(cursorBPath); | |
308 | //painter.setPen( QT_ORANGE ); painter.drawPath(cursorCPath); | |
309 | //painter.setPen( QT_LIGHTBLUE ); painter.drawPath(cursorDPath); | |
310 | ||
311 | char str[200]; | |
312 | sprintf(str, "@%d max=%d min=%d mean=%d n=%d/%d dt=%d [%.3f] zoom=%.3f CursorA=%d [%d] CursorB=%d [%d] GridX=%d GridY=%d (%s)", | |
313 | GraphStart, yMax, yMin, yMean, n, GraphTraceLen, | |
314 | CursorBPos - CursorAPos, | |
315 | (CursorBPos - CursorAPos)/CursorScaleFactor, | |
316 | GraphPixelsPerPoint, | |
317 | CursorAPos, | |
318 | GraphBuffer[CursorAPos], | |
319 | CursorBPos, | |
320 | GraphBuffer[CursorBPos], | |
321 | PlotGridXdefault, | |
322 | PlotGridYdefault, | |
323 | GridLocked ? "Locked" : "Unlocked" | |
324 | ); | |
325 | ||
326 | painter.setPen( QT_WHITE ); | |
327 | painter.drawText(50, r.bottom() - 20, str); | |
328 | } | |
329 | ||
330 | ProxWidget::ProxWidget(QWidget *parent) : QWidget(parent), GraphStart(0), GraphPixelsPerPoint(1) | |
331 | { | |
332 | resize(600, 300); | |
333 | ||
334 | QPalette palette( QT_BLACK_TS ); | |
335 | palette.setColor(QPalette::WindowText, QT_WHITE ); | |
336 | palette.setColor(QPalette::Text, QT_WHITE ); | |
337 | palette.setColor(QPalette::Button, QT_GRAY ); | |
338 | setPalette(palette); | |
339 | setAutoFillBackground(true); | |
340 | CursorAPos = 0; | |
341 | CursorBPos = 0; | |
342 | } | |
343 | ||
344 | void ProxWidget::closeEvent(QCloseEvent *event) | |
345 | { | |
346 | event->ignore(); | |
347 | this->hide(); | |
348 | } | |
349 | ||
350 | void ProxWidget::mouseMoveEvent(QMouseEvent *event) | |
351 | { | |
352 | int x = event->x(); | |
353 | x -= 40; | |
354 | x = (int)(x / GraphPixelsPerPoint); | |
355 | x += GraphStart; | |
356 | if((event->buttons() & Qt::LeftButton)) { | |
357 | CursorAPos = x; | |
358 | } else if (event->buttons() & Qt::RightButton) { | |
359 | CursorBPos = x; | |
360 | } | |
361 | ||
362 | this->update(); | |
363 | } | |
364 | ||
365 | void ProxWidget::keyPressEvent(QKeyEvent *event) | |
366 | { | |
367 | int offset; | |
368 | int gridchanged; | |
369 | ||
370 | gridchanged= 0; | |
371 | ||
372 | if(event->modifiers() & Qt::ShiftModifier) { | |
373 | if (PlotGridX) | |
374 | offset= PageWidth - (PageWidth % PlotGridX); | |
375 | else | |
376 | offset= PageWidth; | |
377 | } else | |
378 | if(event->modifiers() & Qt::ControlModifier) | |
379 | offset= 1; | |
380 | else | |
381 | offset= (int)(20 / GraphPixelsPerPoint); | |
382 | ||
383 | switch(event->key()) { | |
384 | case Qt::Key_Down: | |
385 | if(GraphPixelsPerPoint <= 50) { | |
386 | GraphPixelsPerPoint *= 2; | |
387 | } | |
388 | break; | |
389 | ||
390 | case Qt::Key_Up: | |
391 | if(GraphPixelsPerPoint >= 0.02) { | |
392 | GraphPixelsPerPoint /= 2; | |
393 | } | |
394 | break; | |
395 | ||
396 | case Qt::Key_Right: | |
397 | if(GraphPixelsPerPoint < 20) { | |
398 | if (PlotGridX && GridLocked && GraphStart < startMax){ | |
399 | GridOffset -= offset; | |
400 | GridOffset %= PlotGridX; | |
401 | gridchanged= 1; | |
402 | } | |
403 | GraphStart += offset; | |
404 | } else { | |
405 | if (PlotGridX && GridLocked && GraphStart < startMax){ | |
406 | GridOffset--; | |
407 | GridOffset %= PlotGridX; | |
408 | gridchanged= 1; | |
409 | } | |
410 | GraphStart++; | |
411 | } | |
412 | if(GridOffset < 0) { | |
413 | GridOffset += PlotGridX; | |
414 | } | |
415 | if (gridchanged) | |
416 | if (GraphStart > startMax) { | |
417 | GridOffset += (GraphStart - startMax); | |
418 | GridOffset %= PlotGridX; | |
419 | } | |
420 | break; | |
421 | ||
422 | case Qt::Key_Left: | |
423 | if(GraphPixelsPerPoint < 20) { | |
424 | if (PlotGridX && GridLocked && GraphStart > 0){ | |
425 | GridOffset += offset; | |
426 | GridOffset %= PlotGridX; | |
427 | gridchanged= 1; | |
428 | } | |
429 | GraphStart -= offset; | |
430 | } else { | |
431 | if (PlotGridX && GridLocked && GraphStart > 0){ | |
432 | GridOffset++; | |
433 | GridOffset %= PlotGridX; | |
434 | gridchanged= 1; | |
435 | } | |
436 | GraphStart--; | |
437 | } | |
438 | if (gridchanged){ | |
439 | if (GraphStart < 0) | |
440 | GridOffset += GraphStart; | |
441 | if(GridOffset < 0) | |
442 | GridOffset += PlotGridX; | |
443 | GridOffset %= PlotGridX; | |
444 | } | |
445 | break; | |
446 | ||
447 | case Qt::Key_G: | |
448 | if(PlotGridX || PlotGridY) { | |
449 | PlotGridX= 0; | |
450 | PlotGridY= 0; | |
451 | } else { | |
452 | PlotGridX= PlotGridXdefault; | |
453 | PlotGridY= PlotGridYdefault; | |
454 | } | |
455 | break; | |
456 | ||
457 | case Qt::Key_H: | |
458 | puts("Plot Window Keystrokes:\n"); | |
459 | puts(" Key Action\n"); | |
460 | puts(" DOWN Zoom in"); | |
461 | puts(" G Toggle grid display"); | |
462 | puts(" H Show help"); | |
463 | puts(" L Toggle lock grid relative to samples"); | |
464 | puts(" LEFT Move left"); | |
465 | puts(" <CTL>LEFT Move left 1 sample"); | |
466 | puts(" <SHIFT>LEFT Page left"); | |
467 | puts(" LEFT-MOUSE-CLICK Set yellow cursor"); | |
468 | puts(" Q Hide window"); | |
469 | puts(" RIGHT Move right"); | |
470 | puts(" <CTL>RIGHT Move right 1 sample"); | |
471 | puts(" <SHIFT>RIGHT Page right"); | |
472 | puts(" RIGHT-MOUSE-CLICK Set purple cursor"); | |
473 | puts(" UP Zoom out"); | |
474 | puts(""); | |
475 | puts("Use client window 'data help' for more plot commands\n"); | |
476 | break; | |
477 | ||
478 | case Qt::Key_L: | |
479 | GridLocked= !GridLocked; | |
480 | break; | |
481 | ||
482 | case Qt::Key_Q: | |
483 | this->hide(); | |
484 | break; | |
485 | ||
486 | default: | |
487 | QWidget::keyPressEvent(event); | |
488 | return; | |
489 | break; | |
490 | } | |
491 | ||
492 | this->update(); | |
493 | } |