]> git.zerfleddert.de Git - proxmark3-svn/blob - client/proxguiqt.cpp
CHG: `data plot`- the marking of clock, looks better without borders. It only connec...
[proxmark3-svn] / client / proxguiqt.cpp
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)",
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 );
331
332 painter.setPen( QT_WHITE );
333 painter.drawText(50, r.bottom() - 20, str);
334 }
335
336 ProxWidget::ProxWidget(QWidget *parent) : QWidget(parent), GraphStart(0), GraphPixelsPerPoint(1)
337 {
338 resize(600, 300);
339
340 QPalette palette( QT_BLACK_TS );
341 palette.setColor(QPalette::WindowText, QT_WHITE );
342 palette.setColor(QPalette::Text, QT_WHITE );
343 palette.setColor(QPalette::Button, QT_GRAY );
344 setPalette(palette);
345 setAutoFillBackground(true);
346 CursorAPos = 0;
347 CursorBPos = 0;
348 }
349
350 void ProxWidget::closeEvent(QCloseEvent *event)
351 {
352 event->ignore();
353 this->hide();
354 }
355
356 void ProxWidget::mouseMoveEvent(QMouseEvent *event)
357 {
358 int x = event->x();
359 x -= 40;
360 x = (int)(x / GraphPixelsPerPoint);
361 x += GraphStart;
362 if((event->buttons() & Qt::LeftButton)) {
363 CursorAPos = x;
364 } else if (event->buttons() & Qt::RightButton) {
365 CursorBPos = x;
366 }
367
368 this->update();
369 }
370
371 void ProxWidget::keyPressEvent(QKeyEvent *event)
372 {
373 int offset;
374 int gridchanged;
375
376 gridchanged= 0;
377
378 if(event->modifiers() & Qt::ShiftModifier) {
379 if (PlotGridX)
380 offset= PageWidth - (PageWidth % PlotGridX);
381 else
382 offset= PageWidth;
383 } else
384 if(event->modifiers() & Qt::ControlModifier)
385 offset= 1;
386 else
387 offset= (int)(20 / GraphPixelsPerPoint);
388
389 switch(event->key()) {
390 case Qt::Key_Down:
391 if(GraphPixelsPerPoint <= 50) {
392 GraphPixelsPerPoint *= 2;
393 }
394 break;
395
396 case Qt::Key_Up:
397 if(GraphPixelsPerPoint >= 0.02) {
398 GraphPixelsPerPoint /= 2;
399 }
400 break;
401
402 case Qt::Key_Right:
403 if(GraphPixelsPerPoint < 20) {
404 if (PlotGridX && GridLocked && GraphStart < startMax){
405 GridOffset -= offset;
406 GridOffset %= PlotGridX;
407 gridchanged= 1;
408 }
409 GraphStart += offset;
410 } else {
411 if (PlotGridX && GridLocked && GraphStart < startMax){
412 GridOffset--;
413 GridOffset %= PlotGridX;
414 gridchanged= 1;
415 }
416 GraphStart++;
417 }
418 if(GridOffset < 0) {
419 GridOffset += PlotGridX;
420 }
421 if (gridchanged)
422 if (GraphStart > startMax) {
423 GridOffset += (GraphStart - startMax);
424 GridOffset %= PlotGridX;
425 }
426 break;
427
428 case Qt::Key_Left:
429 if(GraphPixelsPerPoint < 20) {
430 if (PlotGridX && GridLocked && GraphStart > 0){
431 GridOffset += offset;
432 GridOffset %= PlotGridX;
433 gridchanged= 1;
434 }
435 GraphStart -= offset;
436 } else {
437 if (PlotGridX && GridLocked && GraphStart > 0){
438 GridOffset++;
439 GridOffset %= PlotGridX;
440 gridchanged= 1;
441 }
442 GraphStart--;
443 }
444 if (gridchanged){
445 if (GraphStart < 0)
446 GridOffset += GraphStart;
447 if(GridOffset < 0)
448 GridOffset += PlotGridX;
449 GridOffset %= PlotGridX;
450 }
451 break;
452
453 case Qt::Key_G:
454 if(PlotGridX || PlotGridY) {
455 PlotGridX= 0;
456 PlotGridY= 0;
457 } else {
458 PlotGridX= PlotGridXdefault;
459 PlotGridY= PlotGridYdefault;
460 }
461 break;
462
463 case Qt::Key_H:
464 puts("Plot Window Keystrokes:\n");
465 puts(" Key Action\n");
466 puts(" DOWN Zoom in");
467 puts(" G Toggle grid display");
468 puts(" H Show help");
469 puts(" L Toggle lock grid relative to samples");
470 puts(" LEFT Move left");
471 puts(" <CTL>LEFT Move left 1 sample");
472 puts(" <SHIFT>LEFT Page left");
473 puts(" LEFT-MOUSE-CLICK Set yellow cursor");
474 puts(" Q Hide window");
475 puts(" RIGHT Move right");
476 puts(" <CTL>RIGHT Move right 1 sample");
477 puts(" <SHIFT>RIGHT Page right");
478 puts(" RIGHT-MOUSE-CLICK Set purple cursor");
479 puts(" UP Zoom out");
480 puts("");
481 puts("Use client window 'data help' for more plot commands\n");
482 break;
483
484 case Qt::Key_L:
485 GridLocked= !GridLocked;
486 break;
487
488 case Qt::Key_Q:
489 this->hide();
490 break;
491
492 default:
493 QWidget::keyPressEvent(event);
494 return;
495 break;
496 }
497
498 this->update();
499 }
Impressum, Datenschutz