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