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