]> git.zerfleddert.de Git - proxmark3-svn/blob - client/proxguiqt.cpp
82f0e60ffa0dc0df232f05859b5ed0175598c179
[proxmark3-svn] / client / proxguiqt.cpp
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2009 Michael Gernoth <michael at gernoth.net>
3 //
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
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>
21 #include <stdio.h>
22 #include "proxguiqt.h"
23 #include "proxgui.h"
24
25 int startMax;
26 int PageWidth;
27
28 void ProxGuiQT::ShowGraphWindow(void)
29 {
30 emit ShowGraphWindowSignal();
31 }
32
33 void ProxGuiQT::RepaintGraphWindow(void)
34 {
35 emit RepaintGraphWindowSignal();
36 }
37
38 void ProxGuiQT::HideGraphWindow(void)
39 {
40 emit HideGraphWindowSignal();
41 }
42
43 void ProxGuiQT::_ShowGraphWindow(void)
44 {
45 if(!plotapp)
46 return;
47
48 if (!plotwidget)
49 plotwidget = new ProxWidget();
50
51 plotwidget->show();
52 }
53
54 void ProxGuiQT::_RepaintGraphWindow(void)
55 {
56 if (!plotapp || !plotwidget)
57 return;
58
59 plotwidget->update();
60 }
61
62 void ProxGuiQT::_HideGraphWindow(void)
63 {
64 if (!plotapp || !plotwidget)
65 return;
66
67 plotwidget->hide();
68 }
69
70 void 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
81 ProxGuiQT::ProxGuiQT(int argc, char **argv) : plotapp(NULL), plotwidget(NULL),
82 argc(argc), argv(argv)
83 {
84 }
85
86 ProxGuiQT::~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
100 void ProxWidget::paintEvent(QPaintEvent *event)
101 {
102 QPainter painter(this);
103 QPainterPath penPath, whitePath, greyPath, lightgreyPath, cursorAPath, cursorBPath, cursorCPath, cursorDPath;
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
114 if (CursorAPos > GraphTraceLen)
115 CursorAPos= 0;
116 if(CursorBPos > GraphTraceLen)
117 CursorBPos= 0;
118 if(CursorCPos > GraphTraceLen)
119 CursorCPos= 0;
120 if(CursorDPos > GraphTraceLen)
121 CursorDPos= 0;
122
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);
136
137 PageWidth= (int)((r.right() - r.left() - 40) / GraphPixelsPerPoint);
138
139 // plot X and Y grid lines
140 int i;
141 if ((PlotGridX > 0) && ((PlotGridX * GraphPixelsPerPoint) > 1)) {
142 for(i = 40 + (GridOffset * GraphPixelsPerPoint); i < r.right(); i += (int)(PlotGridX * GraphPixelsPerPoint)) {
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);
149 }
150 }
151 if ((PlotGridY > 0) && ((PlotGridY * GraphPixelsPerPoint) > 1)){
152 for(i = 0; i < ((r.top() + r.bottom())>>1); i += (int)(PlotGridY * GraphPixelsPerPoint)) {
153 lightgreyPath.moveTo(r.left() + 40,zeroHeight + i);
154 lightgreyPath.lineTo(r.right(),zeroHeight + i);
155 painter.drawPath(lightgreyPath);
156 lightgreyPath.moveTo(r.left() + 40,zeroHeight - i);
157 lightgreyPath.lineTo(r.right(),zeroHeight - i);
158 painter.drawPath(lightgreyPath);
159 }
160 }
161
162 startMax = (GraphTraceLen - (int)((r.right() - r.left() - 40) / GraphPixelsPerPoint));
163 if(startMax < 0) {
164 startMax = 0;
165 }
166 if(GraphStart > startMax) {
167 GraphStart = startMax;
168 }
169
170 int absYMax = 1;
171
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
247 if(i == CursorAPos || i == CursorBPos || i == CursorCPos || i == CursorDPos) {
248 QPainterPath *cursorPath;
249
250 if(i == CursorAPos) {
251 cursorPath = &cursorAPath;
252 } else if (i == CursorBPos) {
253 cursorPath = &cursorBPath;
254 } else if (i == CursorCPos) {
255 cursorPath = &cursorCPath;
256 } else {
257 cursorPath = &cursorDPath;
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);
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);
281
282 char str[200];
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)",
284 GraphStart, yMax, yMin, yMean, n, GraphTraceLen,
285 CursorBPos - CursorAPos, (CursorBPos - CursorAPos)/CursorScaleFactor,GraphPixelsPerPoint,CursorAPos,GraphBuffer[CursorAPos],CursorBPos,GraphBuffer[CursorBPos],PlotGridXdefault,PlotGridYdefault,GridLocked?"Locked":"Unlocked");
286
287 painter.setPen(QColor(255, 255, 255));
288 painter.drawText(50, r.bottom() - 20, str);
289 }
290
291 ProxWidget::ProxWidget(QWidget *parent) : QWidget(parent), GraphStart(0), GraphPixelsPerPoint(1)
292 {
293 resize(600, 300);
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);
301 CursorAPos = 0;
302 CursorBPos = 0;
303 }
304
305 void ProxWidget::closeEvent(QCloseEvent *event)
306 {
307 event->ignore();
308 this->hide();
309 }
310
311 void 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
327 void ProxWidget::keyPressEvent(QKeyEvent *event)
328 {
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;
339 } else
340 if(event->modifiers() & Qt::ControlModifier)
341 offset= 1;
342 else
343 offset= (int)(20 / GraphPixelsPerPoint);
344
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) {
360 if (PlotGridX && GridLocked && GraphStart < startMax){
361 GridOffset -= offset;
362 GridOffset %= PlotGridX;
363 gridchanged= 1;
364 }
365 GraphStart += offset;
366 } else {
367 if (PlotGridX && GridLocked && GraphStart < startMax){
368 GridOffset--;
369 GridOffset %= PlotGridX;
370 gridchanged= 1;
371 }
372 GraphStart++;
373 }
374 if(GridOffset < 0) {
375 GridOffset += PlotGridX;
376 }
377 if (gridchanged)
378 if (GraphStart > startMax) {
379 GridOffset += (GraphStart - startMax);
380 GridOffset %= PlotGridX;
381 }
382 break;
383
384 case Qt::Key_Left:
385 if(GraphPixelsPerPoint < 20) {
386 if (PlotGridX && GridLocked && GraphStart > 0){
387 GridOffset += offset;
388 GridOffset %= PlotGridX;
389 gridchanged= 1;
390 }
391 GraphStart -= offset;
392 } else {
393 if (PlotGridX && GridLocked && GraphStart > 0){
394 GridOffset++;
395 GridOffset %= PlotGridX;
396 gridchanged= 1;
397 }
398 GraphStart--;
399 }
400 if (gridchanged){
401 if (GraphStart < 0)
402 GridOffset += GraphStart;
403 if(GridOffset < 0)
404 GridOffset += PlotGridX;
405 GridOffset %= PlotGridX;
406 }
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");
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");
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();
446 break;
447
448 default:
449 QWidget::keyPressEvent(event);
450 return;
451 break;
452 }
453
454 this->update();
455 }
Impressum, Datenschutz