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