]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/proxguiqt.cpp
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2009 Michael Gernoth <michael at gernoth.net>
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
7 //-----------------------------------------------------------------------------
9 //-----------------------------------------------------------------------------
12 #include <QPainterPath>
16 #include <QCloseEvent>
17 #include <QMouseEvent>
22 #include "proxguiqt.h"
30 void ProxGuiQT::ShowGraphWindow(void)
32 emit
ShowGraphWindowSignal();
35 void ProxGuiQT::RepaintGraphWindow(void)
37 emit
RepaintGraphWindowSignal();
40 void ProxGuiQT::HideGraphWindow(void)
42 emit
HideGraphWindowSignal();
45 void ProxGuiQT::_ShowGraphWindow(void)
51 plotwidget
= new ProxWidget();
56 void ProxGuiQT::_RepaintGraphWindow(void)
58 if (!plotapp
|| !plotwidget
)
64 void ProxGuiQT::_HideGraphWindow(void)
66 if (!plotapp
|| !plotwidget
)
72 void ProxGuiQT::MainLoop()
74 plotapp
= new QApplication(argc
, argv
);
76 connect(this, SIGNAL(ShowGraphWindowSignal()), this, SLOT(_ShowGraphWindow()));
77 connect(this, SIGNAL(RepaintGraphWindowSignal()), this, SLOT(_RepaintGraphWindow()));
78 connect(this, SIGNAL(HideGraphWindowSignal()), this, SLOT(_HideGraphWindow()));
83 ProxGuiQT::ProxGuiQT(int argc
, char **argv
) : plotapp(NULL
), plotwidget(NULL
), argc(argc
), argv(argv
) {}
85 ProxGuiQT::~ProxGuiQT(void)
99 void ProxWidget::paintEvent(QPaintEvent
*event
)
101 QPainter
painter(this);
102 QPainterPath penPath
, whitePath
, greyPath
, lightgreyPath
, cursorAPath
, cursorBPath
, cursorCPath
, cursorDPath
;
104 QBrush
brush(QColor(100, 255, 100));
105 QPen
pen(QColor(100, 255, 100));
107 painter
.setFont(QFont("Arial", 10));
112 if (CursorAPos
> GraphTraceLen
)
115 if(CursorBPos
> GraphTraceLen
)
117 if(CursorCPos
> GraphTraceLen
)
119 if(CursorDPos
> GraphTraceLen
)
124 painter
.fillRect(r
, QColor(0, 0, 0));
126 whitePath
.moveTo(r
.left() + 40, r
.top());
127 whitePath
.lineTo(r
.left() + 40, r
.bottom());
129 int zeroHeight
= r
.top() + (r
.bottom() - r
.top()) / 2;
131 greyPath
.moveTo(r
.left(), zeroHeight
);
132 greyPath
.lineTo(r
.right(), zeroHeight
);
133 painter
.setPen(QColor(100, 100, 100));
134 painter
.drawPath(greyPath
);
136 PageWidth
= (int)((r
.right() - r
.left() - 40) / GraphPixelsPerPoint
);
138 // plot X and Y grid lines
140 if ((PlotGridX
> 0) && ((PlotGridX
* GraphPixelsPerPoint
) > 1)) {
141 for(i
= 40 + (GridOffset
* GraphPixelsPerPoint
); i
< r
.right(); i
+= (int)(PlotGridX
* GraphPixelsPerPoint
)) {
142 //SelectObject(hdc, GreyPenLite);
143 //MoveToEx(hdc, r.left + i, r.top, NULL);
144 //LineTo(hdc, r.left + i, r.bottom);
145 lightgreyPath
.moveTo(r
.left()+i
,r
.top());
146 lightgreyPath
.lineTo(r
.left()+i
,r
.bottom());
147 painter
.drawPath(lightgreyPath
);
150 if ((PlotGridY
> 0) && ((PlotGridY
* GraphPixelsPerPoint
) > 1)){
151 for(i
= 0; i
< ((r
.top() + r
.bottom())>>1); i
+= (int)(PlotGridY
* GraphPixelsPerPoint
)) {
152 lightgreyPath
.moveTo(r
.left() + 40,zeroHeight
+ i
);
153 lightgreyPath
.lineTo(r
.right(),zeroHeight
+ i
);
154 painter
.drawPath(lightgreyPath
);
155 lightgreyPath
.moveTo(r
.left() + 40,zeroHeight
- i
);
156 lightgreyPath
.lineTo(r
.right(),zeroHeight
- i
);
157 painter
.drawPath(lightgreyPath
);
161 startMax
= (GraphTraceLen
- (int)((r
.right() - r
.left() - 40) / GraphPixelsPerPoint
));
166 if(GraphStart
> startMax
)
167 GraphStart
= startMax
;
171 for(i
= GraphStart
; ; i
++) {
173 if(i
>= GraphTraceLen
) break;
175 if(fabs((double)GraphBuffer
[i
]) > absYMax
)
176 absYMax
= (int)fabs((double)GraphBuffer
[i
]);
178 int x
= 40 + (int)((i
- GraphStart
)*GraphPixelsPerPoint
);
180 if(x
> r
.right()) break;
183 absYMax
= (int)(absYMax
*1.2 + 1);
185 // number of points that will be plotted
186 int span
= (int)((r
.right() - r
.left()) / GraphPixelsPerPoint
);
188 // one label every 100 pixels, let us say
189 int labels
= (r
.right() - r
.left() - 40) / 100;
190 if(labels
<= 0) labels
= 1;
192 int pointsPerLabel
= span
/ labels
;
193 if(pointsPerLabel
<= 0) pointsPerLabel
= 1;
200 for(i
= GraphStart
; ; i
++) {
201 if(i
>= GraphTraceLen
) break;
203 int x
= 40 + (int)((i
- GraphStart
)*GraphPixelsPerPoint
);
204 if(x
> r
.right() + GraphPixelsPerPoint
) break;
206 int y
= GraphBuffer
[i
];
216 y
= (y
* (r
.top() - r
.bottom()) / (2*absYMax
)) + zeroHeight
;
219 penPath
.moveTo(x
, y
);
221 penPath
.lineTo(x
, y
);
224 if(GraphPixelsPerPoint
> 10) {
225 QRect
f(QPoint(x
- 3, y
- 3),QPoint(x
+ 3, y
+ 3));
226 painter
.fillRect(f
, brush
);
229 if(((i
- GraphStart
) % pointsPerLabel
== 0) && i
!= GraphStart
) {
230 whitePath
.moveTo(x
, zeroHeight
- 3);
231 whitePath
.lineTo(x
, zeroHeight
+ 3);
234 sprintf(str
, "+%d", (i
- GraphStart
));
236 painter
.setPen(QColor(255, 255, 255));
238 QFontMetrics
metrics(painter
.font());
239 size
= metrics
.boundingRect(str
);
240 painter
.drawText(x
- (size
.right() - size
.left()), zeroHeight
+ 9, str
);
245 if(i
== CursorAPos
|| i
== CursorBPos
|| i
== CursorCPos
|| i
== CursorDPos
) {
246 QPainterPath
*cursorPath
;
249 cursorPath
= &cursorAPath
;
250 else if (i
== CursorBPos
)
251 cursorPath
= &cursorBPath
;
252 else if (i
== CursorCPos
)
253 cursorPath
= &cursorCPath
;
255 cursorPath
= &cursorDPath
;
257 cursorPath
->moveTo(x
, r
.top());
258 cursorPath
->lineTo(x
, r
.bottom());
259 penPath
.moveTo(x
, y
);
266 painter
.setPen(QColor(255, 255, 255));
267 painter
.drawPath(whitePath
);
269 painter
.drawPath(penPath
);
270 painter
.setPen(QColor(255, 255, 0));
271 painter
.drawPath(cursorAPath
);
272 painter
.setPen(QColor(255, 0, 255));
273 painter
.drawPath(cursorBPath
);
274 painter
.setPen(QColor(255, 153, 0)); //orange
275 painter
.drawPath(cursorCPath
);
276 painter
.setPen(QColor(0, 0, 205)); //light blue
277 painter
.drawPath(cursorDPath
);
280 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)",
281 GraphStart
, yMax
, yMin
, yMean
, n
, GraphTraceLen
,
282 CursorBPos
- CursorAPos
,
283 (CursorBPos
- CursorAPos
)/CursorScaleFactor
,
286 GraphBuffer
[CursorAPos
],
288 GraphBuffer
[CursorBPos
],
291 GridLocked
? "Locked" : "Unlocked"
294 painter
.setPen(QColor(255, 255, 255));
295 painter
.drawText(50, r
.bottom() - 20, str
);
298 ProxWidget::ProxWidget(QWidget
*parent
) : QWidget(parent
), GraphStart(0), GraphPixelsPerPoint(1)
302 QPalette
palette(QColor(0,0,0,0));
303 palette
.setColor(QPalette::WindowText
, QColor(255,255,255));
304 palette
.setColor(QPalette::Text
, QColor(255,255,255));
305 palette
.setColor(QPalette::Button
, QColor(100, 100, 100));
307 setAutoFillBackground(true);
312 void ProxWidget::closeEvent(QCloseEvent
*event
)
318 void ProxWidget::mouseMoveEvent(QMouseEvent
*event
)
322 x
= (int)(x
/ GraphPixelsPerPoint
);
324 if((event
->buttons() & Qt::LeftButton
)) {
326 } else if (event
->buttons() & Qt::RightButton
) {
333 void ProxWidget::keyPressEvent(QKeyEvent
*event
)
340 if(event
->modifiers() & Qt::ShiftModifier
) {
342 offset
= PageWidth
- (PageWidth
% PlotGridX
);
346 if(event
->modifiers() & Qt::ControlModifier
)
349 offset
= (int)(20 / GraphPixelsPerPoint
);
351 switch(event
->key()) {
353 if(GraphPixelsPerPoint
<= 50) {
354 GraphPixelsPerPoint
*= 2;
359 if(GraphPixelsPerPoint
>= 0.02) {
360 GraphPixelsPerPoint
/= 2;
365 if(GraphPixelsPerPoint
< 20) {
366 if (PlotGridX
&& GridLocked
&& GraphStart
< startMax
){
367 GridOffset
-= offset
;
368 GridOffset
%= PlotGridX
;
371 GraphStart
+= offset
;
373 if (PlotGridX
&& GridLocked
&& GraphStart
< startMax
){
375 GridOffset
%= PlotGridX
;
381 GridOffset
+= PlotGridX
;
384 if (GraphStart
> startMax
) {
385 GridOffset
+= (GraphStart
- startMax
);
386 GridOffset
%= PlotGridX
;
391 if(GraphPixelsPerPoint
< 20) {
392 if (PlotGridX
&& GridLocked
&& GraphStart
> 0){
393 GridOffset
+= offset
;
394 GridOffset
%= PlotGridX
;
397 GraphStart
-= offset
;
399 if (PlotGridX
&& GridLocked
&& GraphStart
> 0){
401 GridOffset
%= PlotGridX
;
408 GridOffset
+= GraphStart
;
410 GridOffset
+= PlotGridX
;
411 GridOffset
%= PlotGridX
;
416 if(PlotGridX
|| PlotGridY
) {
420 PlotGridX
= PlotGridXdefault
;
421 PlotGridY
= PlotGridYdefault
;
426 puts("Plot Window Keystrokes:\n");
427 puts(" Key Action\n");
428 puts(" DOWN Zoom in");
429 puts(" G Toggle grid display");
430 puts(" H Show help");
431 puts(" L Toggle lock grid relative to samples");
432 puts(" LEFT Move left");
433 puts(" <CTL>LEFT Move left 1 sample");
434 puts(" <SHIFT>LEFT Page left");
435 puts(" LEFT-MOUSE-CLICK Set yellow cursor");
436 puts(" Q Hide window");
437 puts(" RIGHT Move right");
438 puts(" <CTL>RIGHT Move right 1 sample");
439 puts(" <SHIFT>RIGHT Page right");
440 puts(" RIGHT-MOUSE-CLICK Set purple cursor");
441 puts(" UP Zoom out");
443 puts("Use client window 'data help' for more plot commands\n");
447 GridLocked
= !GridLocked
;
455 QWidget::keyPressEvent(event
);