]> git.zerfleddert.de Git - proxmark3-svn/blame - client/proxguiqt.cpp
ADD: `data detectclock` - added clock blocks in plot window. Its a draft. Not working...
[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>
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>
c86cc308 21#include <stdio.h>
6658905f 22#include "proxguiqt.h"
23#include "proxgui.h"
24
7ddb9900 25int GridOffset= 0;
26bool GridLocked= 0;
27int startMax;
18856d88 28int PageWidth;
7ddb9900 29
6658905f 30void ProxGuiQT::ShowGraphWindow(void)
31{
32 emit ShowGraphWindowSignal();
33}
34
35void ProxGuiQT::RepaintGraphWindow(void)
36{
37 emit RepaintGraphWindowSignal();
38}
39
40void ProxGuiQT::HideGraphWindow(void)
41{
42 emit HideGraphWindowSignal();
43}
44
45void ProxGuiQT::_ShowGraphWindow(void)
46{
47 if(!plotapp)
48 return;
49
50 if (!plotwidget)
51 plotwidget = new ProxWidget();
52
53 plotwidget->show();
54}
55
56void ProxGuiQT::_RepaintGraphWindow(void)
57{
58 if (!plotapp || !plotwidget)
59 return;
60
61 plotwidget->update();
62}
63
64void ProxGuiQT::_HideGraphWindow(void)
65{
66 if (!plotapp || !plotwidget)
67 return;
68
69 plotwidget->hide();
70}
71
72void 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
6f79363d 83ProxGuiQT::ProxGuiQT(int argc, char **argv) : plotapp(NULL), plotwidget(NULL), argc(argc), argv(argv) {}
6658905f 84
85ProxGuiQT::~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
a47ded5b 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
6658905f 113void ProxWidget::paintEvent(QPaintEvent *event)
114{
115 QPainter painter(this);
a9eeb576 116 QPainterPath penPath, whitePath, greyPath, lightgreyPath, cursorAPath, cursorBPath, cursorCPath, cursorDPath;
6658905f 117 QRect r;
a47ded5b 118 QBrush brush(QT_LIGHTGREEN);
119 QPen pen(QT_LIGHTGREEN);
6658905f 120
121 painter.setFont(QFont("Arial", 10));
122
a47ded5b 123 if(GraphStart < 0)
6658905f 124 GraphStart = 0;
6658905f 125
a47ded5b 126 if (CursorAPos > GraphTraceLen) CursorAPos = 0;
127 if (CursorBPos > GraphTraceLen) CursorBPos = 0;
128 if (CursorCPos > GraphTraceLen) CursorCPos = 0;
129 if (CursorDPos > GraphTraceLen) CursorDPos = 0;
0bf5872f 130
6658905f 131 r = rect();
a47ded5b 132 painter.fillRect(r, QT_BLACK);
133
6658905f 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);
a47ded5b 141 painter.setPen(QT_GRAY);
6658905f 142 painter.drawPath(greyPath);
f4434ad2 143
a47ded5b 144 PageWidth = (int)((r.right() - r.left() - 40) / GraphPixelsPerPoint);
18856d88 145
a47ded5b 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 }
18856d88 168
7ddb9900 169 startMax = (GraphTraceLen - (int)((r.right() - r.left() - 40) / GraphPixelsPerPoint));
eb7eab85 170
171 if(startMax < 0)
6658905f 172 startMax = 0;
eb7eab85 173
174 if(GraphStart > startMax)
6658905f 175 GraphStart = startMax;
6658905f 176
177 int absYMax = 1;
178
a47ded5b 179 for(i = GraphStart; ;i++) {
eb7eab85 180
181 if(i >= GraphTraceLen) break;
182
a47ded5b 183 if (fabs((double)GraphBuffer[i]) > absYMax)
6658905f 184 absYMax = (int)fabs((double)GraphBuffer[i]);
eb7eab85 185
6658905f 186 int x = 40 + (int)((i - GraphStart)*GraphPixelsPerPoint);
eb7eab85 187
188 if(x > r.right()) break;
6658905f 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);
eb7eab85 195
6658905f 196 // one label every 100 pixels, let us say
197 int labels = (r.right() - r.left() - 40) / 100;
198 if(labels <= 0) labels = 1;
eb7eab85 199
6658905f 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;
a47ded5b 207 //int stt_x1 = 0, stt_x2 = 0;
6658905f 208
209 for(i = GraphStart; ; i++) {
eb7eab85 210 if(i >= GraphTraceLen) break;
211
a47ded5b 212 // x == pixel pos.
213 int x = 40 + (int)((i - GraphStart) * GraphPixelsPerPoint);
214
215 // if x reaches end of box, stop loop
eb7eab85 216 if(x > r.right() + GraphPixelsPerPoint) break;
6658905f 217
218 int y = GraphBuffer[i];
eb7eab85 219 if(y < yMin)
6658905f 220 yMin = y;
eb7eab85 221
222 if(y > yMax)
6658905f 223 yMax = y;
eb7eab85 224
6658905f 225 yMean += y;
226 n++;
227
228 y = (y * (r.top() - r.bottom()) / (2*absYMax)) + zeroHeight;
eb7eab85 229
230 if(i == GraphStart)
6658905f 231 penPath.moveTo(x, y);
eb7eab85 232 else
6658905f 233 penPath.lineTo(x, y);
eb7eab85 234
6658905f 235
a47ded5b 236 // small white boxes (the dots on the signal)
6658905f 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
a47ded5b 249 painter.setPen( QT_WHITE );
6658905f 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
a9eeb576 258 if(i == CursorAPos || i == CursorBPos || i == CursorCPos || i == CursorDPos) {
6658905f 259 QPainterPath *cursorPath;
260
a47ded5b 261 if ( i == CursorAPos )
6658905f 262 cursorPath = &cursorAPath;
a47ded5b 263 else if ( i == CursorBPos )
6658905f 264 cursorPath = &cursorBPath;
a47ded5b 265 else if ( i == CursorCPos )
a9eeb576 266 cursorPath = &cursorCPath;
a47ded5b 267 else
a9eeb576 268 cursorPath = &cursorDPath;
eb7eab85 269
6658905f 270 cursorPath->moveTo(x, r.top());
271 cursorPath->lineTo(x, r.bottom());
a47ded5b 272 penPath.moveTo(x, y);
6658905f 273 }
274 }
a47ded5b 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) {
6658905f 289
a47ded5b 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
eb7eab85 301 if(n != 0)
6658905f 302 yMean /= n;
6658905f 303
a47ded5b 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);
6658905f 310
346ad5fb 311 char str[200];
ff2e9c1c 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)",
6658905f 313 GraphStart, yMax, yMin, yMean, n, GraphTraceLen,
eb7eab85 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 );
6658905f 325
a47ded5b 326 painter.setPen( QT_WHITE );
6658905f 327 painter.drawText(50, r.bottom() - 20, str);
328}
329
330ProxWidget::ProxWidget(QWidget *parent) : QWidget(parent), GraphStart(0), GraphPixelsPerPoint(1)
331{
8d0a3e87 332 resize(600, 300);
6658905f 333
a47ded5b 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 );
6658905f 338 setPalette(palette);
339 setAutoFillBackground(true);
cee48e2b 340 CursorAPos = 0;
341 CursorBPos = 0;
6658905f 342}
343
344void ProxWidget::closeEvent(QCloseEvent *event)
345{
346 event->ignore();
347 this->hide();
348}
349
350void 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
6658905f 362 this->update();
363}
364
365void ProxWidget::keyPressEvent(QKeyEvent *event)
366{
18856d88 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;
ff2e9c1c 377 } else
378 if(event->modifiers() & Qt::ControlModifier)
379 offset= 1;
380 else
381 offset= (int)(20 / GraphPixelsPerPoint);
18856d88 382
6658905f 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) {
18856d88 398 if (PlotGridX && GridLocked && GraphStart < startMax){
399 GridOffset -= offset;
400 GridOffset %= PlotGridX;
401 gridchanged= 1;
402 }
403 GraphStart += offset;
6658905f 404 } else {
18856d88 405 if (PlotGridX && GridLocked && GraphStart < startMax){
7ddb9900 406 GridOffset--;
18856d88 407 GridOffset %= PlotGridX;
408 gridchanged= 1;
409 }
3bc2349d 410 GraphStart++;
6658905f 411 }
18856d88 412 if(GridOffset < 0) {
7ddb9900 413 GridOffset += PlotGridX;
18856d88 414 }
415 if (gridchanged)
416 if (GraphStart > startMax) {
417 GridOffset += (GraphStart - startMax);
418 GridOffset %= PlotGridX;
419 }
6658905f 420 break;
421
422 case Qt::Key_Left:
423 if(GraphPixelsPerPoint < 20) {
18856d88 424 if (PlotGridX && GridLocked && GraphStart > 0){
425 GridOffset += offset;
426 GridOffset %= PlotGridX;
427 gridchanged= 1;
428 }
429 GraphStart -= offset;
6658905f 430 } else {
18856d88 431 if (PlotGridX && GridLocked && GraphStart > 0){
7ddb9900 432 GridOffset++;
18856d88 433 GridOffset %= PlotGridX;
434 gridchanged= 1;
435 }
3bc2349d 436 GraphStart--;
6658905f 437 }
18856d88 438 if (gridchanged){
439 if (GraphStart < 0)
440 GridOffset += GraphStart;
441 if(GridOffset < 0)
442 GridOffset += PlotGridX;
443 GridOffset %= PlotGridX;
444 }
7ddb9900 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");
ff2e9c1c 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");
7ddb9900 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();
6658905f 484 break;
485
486 default:
487 QWidget::keyPressEvent(event);
488 return;
489 break;
490 }
491
492 this->update();
493}
Impressum, Datenschutz