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