6719fcacbfc05992e30828b4e87ebea1253b2bc6
[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 void ProxGuiQT::ShowGraphWindow(void)
26 {
27 emit ShowGraphWindowSignal();
28 }
29
30 void ProxGuiQT::RepaintGraphWindow(void)
31 {
32 emit RepaintGraphWindowSignal();
33 }
34
35 void ProxGuiQT::HideGraphWindow(void)
36 {
37 emit HideGraphWindowSignal();
38 }
39
40 void ProxGuiQT::_ShowGraphWindow(void)
41 {
42 if(!plotapp)
43 return;
44
45 if (!plotwidget)
46 plotwidget = new ProxWidget();
47
48 plotwidget->show();
49 }
50
51 void ProxGuiQT::_RepaintGraphWindow(void)
52 {
53 if (!plotapp || !plotwidget)
54 return;
55
56 plotwidget->update();
57 }
58
59 void ProxGuiQT::_HideGraphWindow(void)
60 {
61 if (!plotapp || !plotwidget)
62 return;
63
64 plotwidget->hide();
65 }
66
67 void ProxGuiQT::MainLoop()
68 {
69 plotapp = new QApplication(argc, argv);
70
71 connect(this, SIGNAL(ShowGraphWindowSignal()), this, SLOT(_ShowGraphWindow()));
72 connect(this, SIGNAL(RepaintGraphWindowSignal()), this, SLOT(_RepaintGraphWindow()));
73 connect(this, SIGNAL(HideGraphWindowSignal()), this, SLOT(_HideGraphWindow()));
74
75 plotapp->exec();
76 }
77
78 ProxGuiQT::ProxGuiQT(int argc, char **argv) : plotapp(NULL), plotwidget(NULL),
79 argc(argc), argv(argv)
80 {
81 }
82
83 ProxGuiQT::~ProxGuiQT(void)
84 {
85 if (plotwidget) {
86 delete plotwidget;
87 plotwidget = NULL;
88 }
89
90 if (plotapp) {
91 plotapp->quit();
92 delete plotapp;
93 plotapp = NULL;
94 }
95 }
96
97 void ProxWidget::paintEvent(QPaintEvent *event)
98 {
99 QPainter painter(this);
100 QPainterPath penPath, whitePath, greyPath, lightgreyPath, cursorAPath, cursorBPath;
101 QRect r;
102 QBrush brush(QColor(100, 255, 100));
103 QPen pen(QColor(100, 255, 100));
104
105 painter.setFont(QFont("Arial", 10));
106
107 if(GraphStart < 0) {
108 GraphStart = 0;
109 }
110
111 if (CursorAPos > GraphTraceLen)
112 CursorAPos= 0;
113 if(CursorBPos > GraphTraceLen)
114 CursorBPos= 0;
115
116 r = rect();
117
118 painter.fillRect(r, QColor(0, 0, 0));
119
120 whitePath.moveTo(r.left() + 40, r.top());
121 whitePath.lineTo(r.left() + 40, r.bottom());
122
123 int zeroHeight = r.top() + (r.bottom() - r.top()) / 2;
124
125 greyPath.moveTo(r.left(), zeroHeight);
126 greyPath.lineTo(r.right(), zeroHeight);
127 painter.setPen(QColor(100, 100, 100));
128 painter.drawPath(greyPath);
129
130 // plot X and Y grid lines
131 int i;
132 if ((PlotGridX > 0) && ((PlotGridX * GraphPixelsPerPoint) > 1)) {
133 for(i = 40; i < r.right(); i += (int)(PlotGridX * GraphPixelsPerPoint)) {
134 //SelectObject(hdc, GreyPenLite);
135 //MoveToEx(hdc, r.left + i, r.top, NULL);
136 //LineTo(hdc, r.left + i, r.bottom);
137 lightgreyPath.moveTo(r.left()+i,r.top());
138 lightgreyPath.lineTo(r.left()+i,r.bottom());
139 painter.drawPath(lightgreyPath);
140 }
141 }
142 if ((PlotGridY > 0) && ((PlotGridY * GraphPixelsPerPoint) > 1)){
143 for(i = 0; i < ((r.top() + r.bottom())>>1); i += (int)(PlotGridY * GraphPixelsPerPoint)) {
144 lightgreyPath.moveTo(r.left() + 40,zeroHeight + i);
145 lightgreyPath.lineTo(r.right(),zeroHeight + i);
146 painter.drawPath(lightgreyPath);
147 lightgreyPath.moveTo(r.left() + 40,zeroHeight - i);
148 lightgreyPath.lineTo(r.right(),zeroHeight - i);
149 painter.drawPath(lightgreyPath);
150 }
151 }
152
153 int startMax =
154 (GraphTraceLen - (int)((r.right() - r.left() - 40) / GraphPixelsPerPoint));
155 if(startMax < 0) {
156 startMax = 0;
157 }
158 if(GraphStart > startMax) {
159 GraphStart = startMax;
160 }
161
162 int absYMax = 1;
163
164 for(i = GraphStart; ; i++) {
165 if(i >= GraphTraceLen) {
166 break;
167 }
168 if(fabs((double)GraphBuffer[i]) > absYMax) {
169 absYMax = (int)fabs((double)GraphBuffer[i]);
170 }
171 int x = 40 + (int)((i - GraphStart)*GraphPixelsPerPoint);
172 if(x > r.right()) {
173 break;
174 }
175 }
176
177 absYMax = (int)(absYMax*1.2 + 1);
178
179 // number of points that will be plotted
180 int span = (int)((r.right() - r.left()) / GraphPixelsPerPoint);
181 // one label every 100 pixels, let us say
182 int labels = (r.right() - r.left() - 40) / 100;
183 if(labels <= 0) labels = 1;
184 int pointsPerLabel = span / labels;
185 if(pointsPerLabel <= 0) pointsPerLabel = 1;
186
187 int yMin = INT_MAX;
188 int yMax = INT_MIN;
189 int yMean = 0;
190 int n = 0;
191
192 for(i = GraphStart; ; i++) {
193 if(i >= GraphTraceLen) {
194 break;
195 }
196 int x = 40 + (int)((i - GraphStart)*GraphPixelsPerPoint);
197 if(x > r.right() + GraphPixelsPerPoint) {
198 break;
199 }
200
201 int y = GraphBuffer[i];
202 if(y < yMin) {
203 yMin = y;
204 }
205 if(y > yMax) {
206 yMax = y;
207 }
208 yMean += y;
209 n++;
210
211 y = (y * (r.top() - r.bottom()) / (2*absYMax)) + zeroHeight;
212 if(i == GraphStart) {
213 penPath.moveTo(x, y);
214 } else {
215 penPath.lineTo(x, y);
216 }
217
218 if(GraphPixelsPerPoint > 10) {
219 QRect f(QPoint(x - 3, y - 3),QPoint(x + 3, y + 3));
220 painter.fillRect(f, brush);
221 }
222
223 if(((i - GraphStart) % pointsPerLabel == 0) && i != GraphStart) {
224 whitePath.moveTo(x, zeroHeight - 3);
225 whitePath.lineTo(x, zeroHeight + 3);
226
227 char str[100];
228 sprintf(str, "+%d", (i - GraphStart));
229
230 painter.setPen(QColor(255, 255, 255));
231 QRect size;
232 QFontMetrics metrics(painter.font());
233 size = metrics.boundingRect(str);
234 painter.drawText(x - (size.right() - size.left()), zeroHeight + 9, str);
235
236 penPath.moveTo(x,y);
237 }
238
239 if(i == CursorAPos || i == CursorBPos) {
240 QPainterPath *cursorPath;
241
242 if(i == CursorAPos) {
243 cursorPath = &cursorAPath;
244 } else {
245 cursorPath = &cursorBPath;
246 }
247 cursorPath->moveTo(x, r.top());
248 cursorPath->lineTo(x, r.bottom());
249 penPath.moveTo(x, y);
250 }
251 }
252
253 if(n != 0) {
254 yMean /= n;
255 }
256
257 painter.setPen(QColor(255, 255, 255));
258 painter.drawPath(whitePath);
259 painter.setPen(pen);
260 painter.drawPath(penPath);
261 painter.setPen(QColor(255, 255, 0));
262 painter.drawPath(cursorAPath);
263 painter.setPen(QColor(255, 0, 255));
264 painter.drawPath(cursorBPath);
265
266 char str[200];
267 sprintf(str, "@%d max=%d min=%d mean=%d n=%d/%d dt=%d [%.3f] zoom=%.3f CursorA=%d [%d] CursorB=%d [%d]",
268 GraphStart, yMax, yMin, yMean, n, GraphTraceLen,
269 CursorBPos - CursorAPos, (CursorBPos - CursorAPos)/CursorScaleFactor,GraphPixelsPerPoint,CursorAPos,GraphBuffer[CursorAPos],CursorBPos,GraphBuffer[CursorBPos]);
270
271 painter.setPen(QColor(255, 255, 255));
272 painter.drawText(50, r.bottom() - 20, str);
273 }
274
275 ProxWidget::ProxWidget(QWidget *parent) : QWidget(parent), GraphStart(0), GraphPixelsPerPoint(1)
276 {
277 resize(600, 500);
278
279 QPalette palette(QColor(0,0,0,0));
280 palette.setColor(QPalette::WindowText, QColor(255,255,255));
281 palette.setColor(QPalette::Text, QColor(255,255,255));
282 palette.setColor(QPalette::Button, QColor(100, 100, 100));
283 setPalette(palette);
284 setAutoFillBackground(true);
285 }
286
287 void ProxWidget::closeEvent(QCloseEvent *event)
288 {
289 event->ignore();
290 this->hide();
291 }
292
293 void ProxWidget::mouseMoveEvent(QMouseEvent *event)
294 {
295 int x = event->x();
296 x -= 40;
297 x = (int)(x / GraphPixelsPerPoint);
298 x += GraphStart;
299 if((event->buttons() & Qt::LeftButton)) {
300 CursorAPos = x;
301 } else if (event->buttons() & Qt::RightButton) {
302 CursorBPos = x;
303 }
304
305
306 this->update();
307 }
308
309 void ProxWidget::keyPressEvent(QKeyEvent *event)
310 {
311 switch(event->key()) {
312 case Qt::Key_Down:
313 if(GraphPixelsPerPoint <= 50) {
314 GraphPixelsPerPoint *= 2;
315 }
316 break;
317
318 case Qt::Key_Up:
319 if(GraphPixelsPerPoint >= 0.02) {
320 GraphPixelsPerPoint /= 2;
321 }
322 break;
323
324 case Qt::Key_Right:
325 if(GraphPixelsPerPoint < 20) {
326 GraphStart += (int)(20 / GraphPixelsPerPoint);
327 } else {
328 GraphStart++;
329 }
330 break;
331
332 case Qt::Key_Left:
333 if(GraphPixelsPerPoint < 20) {
334 GraphStart -= (int)(20 / GraphPixelsPerPoint);
335 } else {
336 GraphStart--;
337 }
338 break;
339
340 default:
341 QWidget::keyPressEvent(event);
342 return;
343 break;
344 }
345
346 this->update();
347 }
Impressum, Datenschutz