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