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