]> git.zerfleddert.de Git - proxmark3-svn/blame - client/proxguiqt.cpp
FIX: had to make it clientside only
[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];
ff2e9c1c 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)",
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,
329 GridLocked ? "Locked" : "Unlocked"
330 );
6658905f 331
a47ded5b 332 painter.setPen( QT_WHITE );
6658905f 333 painter.drawText(50, r.bottom() - 20, str);
334}
335
336ProxWidget::ProxWidget(QWidget *parent) : QWidget(parent), GraphStart(0), GraphPixelsPerPoint(1)
337{
8d0a3e87 338 resize(600, 300);
6658905f 339
a47ded5b 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 );
6658905f 344 setPalette(palette);
345 setAutoFillBackground(true);
cee48e2b 346 CursorAPos = 0;
347 CursorBPos = 0;
6658905f 348}
349
350void ProxWidget::closeEvent(QCloseEvent *event)
351{
352 event->ignore();
353 this->hide();
354}
355
356void 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
6658905f 368 this->update();
369}
370
371void ProxWidget::keyPressEvent(QKeyEvent *event)
372{
18856d88 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;
ff2e9c1c 383 } else
384 if(event->modifiers() & Qt::ControlModifier)
385 offset= 1;
386 else
387 offset= (int)(20 / GraphPixelsPerPoint);
18856d88 388
6658905f 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) {
18856d88 404 if (PlotGridX && GridLocked && GraphStart < startMax){
405 GridOffset -= offset;
406 GridOffset %= PlotGridX;
407 gridchanged= 1;
408 }
409 GraphStart += offset;
6658905f 410 } else {
18856d88 411 if (PlotGridX && GridLocked && GraphStart < startMax){
7ddb9900 412 GridOffset--;
18856d88 413 GridOffset %= PlotGridX;
414 gridchanged= 1;
415 }
3bc2349d 416 GraphStart++;
6658905f 417 }
18856d88 418 if(GridOffset < 0) {
7ddb9900 419 GridOffset += PlotGridX;
18856d88 420 }
421 if (gridchanged)
422 if (GraphStart > startMax) {
423 GridOffset += (GraphStart - startMax);
424 GridOffset %= PlotGridX;
425 }
6658905f 426 break;
427
428 case Qt::Key_Left:
429 if(GraphPixelsPerPoint < 20) {
18856d88 430 if (PlotGridX && GridLocked && GraphStart > 0){
431 GridOffset += offset;
432 GridOffset %= PlotGridX;
433 gridchanged= 1;
434 }
435 GraphStart -= offset;
6658905f 436 } else {
18856d88 437 if (PlotGridX && GridLocked && GraphStart > 0){
7ddb9900 438 GridOffset++;
18856d88 439 GridOffset %= PlotGridX;
440 gridchanged= 1;
441 }
3bc2349d 442 GraphStart--;
6658905f 443 }
18856d88 444 if (gridchanged){
445 if (GraphStart < 0)
446 GridOffset += GraphStart;
447 if(GridOffset < 0)
448 GridOffset += PlotGridX;
449 GridOffset %= PlotGridX;
450 }
7ddb9900 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");
ff2e9c1c 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");
7ddb9900 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();
6658905f 490 break;
491
492 default:
493 QWidget::keyPressEvent(event);
494 return;
495 break;
496 }
497
498 this->update();
499}
Impressum, Datenschutz