]> git.zerfleddert.de Git - proxmark3-svn/blame - client/proxguiqt.cpp
fixes #85
[proxmark3-svn] / client / proxguiqt.cpp
CommitLineData
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>
aa9b584f 12//#include <QT>
6658905f 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>
c86cc308 22#include <stdio.h>
6658905f 23#include "proxguiqt.h"
24#include "proxgui.h"
25
7ddb9900 26int GridOffset= 0;
27bool GridLocked= 0;
28int startMax;
18856d88 29int PageWidth;
7ddb9900 30
6658905f 31void ProxGuiQT::ShowGraphWindow(void)
32{
33 emit ShowGraphWindowSignal();
34}
35
36void ProxGuiQT::RepaintGraphWindow(void)
37{
38 emit RepaintGraphWindowSignal();
39}
40
41void ProxGuiQT::HideGraphWindow(void)
42{
43 emit HideGraphWindowSignal();
44}
45
46void ProxGuiQT::_ShowGraphWindow(void)
47{
48 if(!plotapp)
49 return;
50
51 if (!plotwidget)
52 plotwidget = new ProxWidget();
53
54 plotwidget->show();
55}
56
57void ProxGuiQT::_RepaintGraphWindow(void)
58{
59 if (!plotapp || !plotwidget)
60 return;
61
62 plotwidget->update();
63}
64
65void ProxGuiQT::_HideGraphWindow(void)
66{
67 if (!plotapp || !plotwidget)
68 return;
69
70 plotwidget->hide();
71}
72
73void 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
6f79363d 84ProxGuiQT::ProxGuiQT(int argc, char **argv) : plotapp(NULL), plotwidget(NULL), argc(argc), argv(argv) {}
6658905f 85
86ProxGuiQT::~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
a47ded5b 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
6658905f 114void ProxWidget::paintEvent(QPaintEvent *event)
115{
116 QPainter painter(this);
a9eeb576 117 QPainterPath penPath, whitePath, greyPath, lightgreyPath, cursorAPath, cursorBPath, cursorCPath, cursorDPath;
6658905f 118 QRect r;
a47ded5b 119 QBrush brush(QT_LIGHTGREEN);
120 QPen pen(QT_LIGHTGREEN);
6658905f 121
122 painter.setFont(QFont("Arial", 10));
123
a47ded5b 124 if(GraphStart < 0)
6658905f 125 GraphStart = 0;
6658905f 126
a47ded5b 127 if (CursorAPos > GraphTraceLen) CursorAPos = 0;
128 if (CursorBPos > GraphTraceLen) CursorBPos = 0;
129 if (CursorCPos > GraphTraceLen) CursorCPos = 0;
130 if (CursorDPos > GraphTraceLen) CursorDPos = 0;
0bf5872f 131
6658905f 132 r = rect();
a47ded5b 133 painter.fillRect(r, QT_BLACK);
134
6658905f 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);
a47ded5b 142 painter.setPen(QT_GRAY);
6658905f 143 painter.drawPath(greyPath);
f4434ad2 144
a47ded5b 145 PageWidth = (int)((r.right() - r.left() - 40) / GraphPixelsPerPoint);
18856d88 146
a47ded5b 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 }
18856d88 169
7ddb9900 170 startMax = (GraphTraceLen - (int)((r.right() - r.left() - 40) / GraphPixelsPerPoint));
eb7eab85 171
172 if(startMax < 0)
6658905f 173 startMax = 0;
eb7eab85 174
175 if(GraphStart > startMax)
6658905f 176 GraphStart = startMax;
6658905f 177
178 int absYMax = 1;
179
a47ded5b 180 for(i = GraphStart; ;i++) {
eb7eab85 181
182 if(i >= GraphTraceLen) break;
183
a47ded5b 184 if (fabs((double)GraphBuffer[i]) > absYMax)
6658905f 185 absYMax = (int)fabs((double)GraphBuffer[i]);
eb7eab85 186
6658905f 187 int x = 40 + (int)((i - GraphStart)*GraphPixelsPerPoint);
eb7eab85 188
189 if(x > r.right()) break;
6658905f 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);
eb7eab85 196
6658905f 197 // one label every 100 pixels, let us say
198 int labels = (r.right() - r.left() - 40) / 100;
199 if(labels <= 0) labels = 1;
eb7eab85 200
6658905f 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;
a47ded5b 208 //int stt_x1 = 0, stt_x2 = 0;
6658905f 209
210 for(i = GraphStart; ; i++) {
eb7eab85 211 if(i >= GraphTraceLen) break;
212
a47ded5b 213 // x == pixel pos.
214 int x = 40 + (int)((i - GraphStart) * GraphPixelsPerPoint);
215
216 // if x reaches end of box, stop loop
eb7eab85 217 if(x > r.right() + GraphPixelsPerPoint) break;
6658905f 218
219 int y = GraphBuffer[i];
eb7eab85 220 if(y < yMin)
6658905f 221 yMin = y;
eb7eab85 222
223 if(y > yMax)
6658905f 224 yMax = y;
eb7eab85 225
6658905f 226 yMean += y;
227 n++;
228
229 y = (y * (r.top() - r.bottom()) / (2*absYMax)) + zeroHeight;
eb7eab85 230
231 if(i == GraphStart)
6658905f 232 penPath.moveTo(x, y);
eb7eab85 233 else
6658905f 234 penPath.lineTo(x, y);
eb7eab85 235
6658905f 236
a47ded5b 237 // small white boxes (the dots on the signal)
6658905f 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
a47ded5b 250 painter.setPen( QT_WHITE );
6658905f 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
a9eeb576 259 if(i == CursorAPos || i == CursorBPos || i == CursorCPos || i == CursorDPos) {
6658905f 260 QPainterPath *cursorPath;
261
a47ded5b 262 if ( i == CursorAPos )
6658905f 263 cursorPath = &cursorAPath;
a47ded5b 264 else if ( i == CursorBPos )
6658905f 265 cursorPath = &cursorBPath;
a47ded5b 266 else if ( i == CursorCPos )
a9eeb576 267 cursorPath = &cursorCPath;
a47ded5b 268 else
a9eeb576 269 cursorPath = &cursorDPath;
eb7eab85 270
6658905f 271 cursorPath->moveTo(x, r.top());
272 cursorPath->lineTo(x, r.bottom());
a47ded5b 273 penPath.moveTo(x, y);
6658905f 274 }
275 }
a47ded5b 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() );
c6e5c7ea 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);
a47ded5b 286 }
287
288 // Mark Clock pulse
289 //extern int PlotClock, PlockClockStartIndex;
290 if ( PlotClock > 0){
291 for(int i = PlockClockStartIndex; ; i += PlotClock * 2) {
6658905f 292
a47ded5b 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() );
c6e5c7ea 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);
a47ded5b 304 }
305 }
306
eb7eab85 307 if(n != 0)
6658905f 308 yMean /= n;
6658905f 309
a47ded5b 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);
6658905f 316
346ad5fb 317 char str[200];
e0a241f7 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]",
6658905f 319 GraphStart, yMax, yMin, yMean, n, GraphTraceLen,
eb7eab85 320 CursorBPos - CursorAPos,
321 (CursorBPos - CursorAPos)/CursorScaleFactor,
322 GraphPixelsPerPoint,
323 CursorAPos,
324 GraphBuffer[CursorAPos],
325 CursorBPos,
326 GraphBuffer[CursorBPos],
327 PlotGridXdefault,
328 PlotGridYdefault,
e0a241f7 329 GridLocked ? "Locked" : "Unlocked",
330 PlotClock
eb7eab85 331 );
6658905f 332
a47ded5b 333 painter.setPen( QT_WHITE );
e0a241f7 334 painter.drawText(50, r.bottom() - 10, str);
6658905f 335}
336
337ProxWidget::ProxWidget(QWidget *parent) : QWidget(parent), GraphStart(0), GraphPixelsPerPoint(1)
338{
8d0a3e87 339 resize(600, 300);
6658905f 340
a47ded5b 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 );
6658905f 345 setPalette(palette);
346 setAutoFillBackground(true);
cee48e2b 347 CursorAPos = 0;
348 CursorBPos = 0;
e0a241f7 349 PlotClock = 0;
6658905f 350}
351
352void ProxWidget::closeEvent(QCloseEvent *event)
353{
354 event->ignore();
355 this->hide();
356}
357
358void 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
6658905f 370 this->update();
371}
372
373void ProxWidget::keyPressEvent(QKeyEvent *event)
374{
18856d88 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;
ff2e9c1c 385 } else
386 if(event->modifiers() & Qt::ControlModifier)
387 offset= 1;
388 else
389 offset= (int)(20 / GraphPixelsPerPoint);
18856d88 390
6658905f 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) {
18856d88 406 if (PlotGridX && GridLocked && GraphStart < startMax){
407 GridOffset -= offset;
408 GridOffset %= PlotGridX;
409 gridchanged= 1;
410 }
411 GraphStart += offset;
6658905f 412 } else {
18856d88 413 if (PlotGridX && GridLocked && GraphStart < startMax){
7ddb9900 414 GridOffset--;
18856d88 415 GridOffset %= PlotGridX;
416 gridchanged= 1;
417 }
3bc2349d 418 GraphStart++;
6658905f 419 }
18856d88 420 if(GridOffset < 0) {
7ddb9900 421 GridOffset += PlotGridX;
18856d88 422 }
423 if (gridchanged)
424 if (GraphStart > startMax) {
425 GridOffset += (GraphStart - startMax);
426 GridOffset %= PlotGridX;
427 }
6658905f 428 break;
429
430 case Qt::Key_Left:
431 if(GraphPixelsPerPoint < 20) {
18856d88 432 if (PlotGridX && GridLocked && GraphStart > 0){
433 GridOffset += offset;
434 GridOffset %= PlotGridX;
435 gridchanged= 1;
436 }
437 GraphStart -= offset;
6658905f 438 } else {
18856d88 439 if (PlotGridX && GridLocked && GraphStart > 0){
7ddb9900 440 GridOffset++;
18856d88 441 GridOffset %= PlotGridX;
442 gridchanged= 1;
443 }
3bc2349d 444 GraphStart--;
6658905f 445 }
18856d88 446 if (gridchanged){
447 if (GraphStart < 0)
448 GridOffset += GraphStart;
449 if(GridOffset < 0)
450 GridOffset += PlotGridX;
451 GridOffset %= PlotGridX;
452 }
7ddb9900 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");
ff2e9c1c 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");
7ddb9900 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();
6658905f 492 break;
493
494 default:
495 QWidget::keyPressEvent(event);
496 return;
497 break;
498 }
499
500 this->update();
501}
Impressum, Datenschutz