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