]>
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 //-----------------------------------------------------------------------------
13 #include <QPainterPath>
17 #include <QCloseEvent>
18 #include <QMouseEvent>
23 #include "proxguiqt.h"
31 void ProxGuiQT::ShowGraphWindow(void)
33 emit
ShowGraphWindowSignal();
36 void ProxGuiQT::RepaintGraphWindow(void)
38 emit
RepaintGraphWindowSignal();
41 void ProxGuiQT::HideGraphWindow(void)
43 emit
HideGraphWindowSignal();
46 void ProxGuiQT::_ShowGraphWindow(void)
52 plotwidget
= new ProxWidget();
57 void ProxGuiQT::_RepaintGraphWindow(void)
59 if (!plotapp
|| !plotwidget
)
65 void ProxGuiQT::_HideGraphWindow(void)
67 if (!plotapp
|| !plotwidget
)
73 void ProxGuiQT::MainLoop()
75 plotapp
= new QApplication(argc
, argv
);
77 connect(this, SIGNAL(ShowGraphWindowSignal()), this, SLOT(_ShowGraphWindow()));
78 connect(this, SIGNAL(RepaintGraphWindowSignal()), this, SLOT(_RepaintGraphWindow()));
79 connect(this, SIGNAL(HideGraphWindowSignal()), this, SLOT(_HideGraphWindow()));
84 ProxGuiQT::ProxGuiQT(int argc
, char **argv
) : plotapp(NULL
), plotwidget(NULL
), argc(argc
), argv(argv
) {}
86 ProxGuiQT::~ProxGuiQT(void)
101 #define QT_ORANGE QColor(255, 153, 0)
102 #define QT_WHITE QColor(255, 255, 255)
103 #define QT_YELLOW QColor(255, 255, 0)
104 #define QT_MAGENTA QColor(255, 0, 255)
105 #define QT_LIGHTBLUE QColor(0, 0, 205)
106 #define QT_LIGHTGREEN QColor(100, 255, 100)
107 #define QT_GRAY QColor(100,100,100)
108 #define QT_BLACK QColor(0,0,0)
109 // transparent colors
110 #define QT_ORANGE_TS QColor(255, 153, 0, 96)
111 #define QT_RED_TS QColor(255, 0, 0, 64)
112 #define QT_BLACK_TS QColor(0,0,0,0)
114 void ProxWidget::paintEvent(QPaintEvent
*event
)
116 QPainter
painter(this);
117 QPainterPath penPath
, whitePath
, greyPath
, lightgreyPath
, cursorAPath
, cursorBPath
, cursorCPath
, cursorDPath
;
119 QBrush
brush(QT_LIGHTGREEN
);
120 QPen
pen(QT_LIGHTGREEN
);
122 painter
.setFont(QFont("Arial", 10));
127 if (CursorAPos
> GraphTraceLen
) CursorAPos
= 0;
128 if (CursorBPos
> GraphTraceLen
) CursorBPos
= 0;
129 if (CursorCPos
> GraphTraceLen
) CursorCPos
= 0;
130 if (CursorDPos
> GraphTraceLen
) CursorDPos
= 0;
133 painter
.fillRect(r
, QT_BLACK
);
135 whitePath
.moveTo(r
.left() + 40, r
.top());
136 whitePath
.lineTo(r
.left() + 40, r
.bottom());
138 int zeroHeight
= r
.top() + (r
.bottom() - r
.top()) / 2;
140 greyPath
.moveTo(r
.left(), zeroHeight
);
141 greyPath
.lineTo(r
.right(), zeroHeight
);
142 painter
.setPen(QT_GRAY
);
143 painter
.drawPath(greyPath
);
145 PageWidth
= (int)((r
.right() - r
.left() - 40) / GraphPixelsPerPoint
);
147 // plot X and Y grid lines
149 if ((PlotGridX
> 0) && ((PlotGridX
* GraphPixelsPerPoint
) > 1)) {
150 for(i
= 40 + (GridOffset
* GraphPixelsPerPoint
); i
< r
.right(); i
+= (int)(PlotGridX
* GraphPixelsPerPoint
)) {
151 //SelectObject(hdc, GreyPenLite);
152 //MoveToEx(hdc, r.left + i, r.top, NULL);
153 //LineTo(hdc, r.left + i, r.bottom);
154 lightgreyPath
.moveTo(r
.left()+i
,r
.top());
155 lightgreyPath
.lineTo(r
.left()+i
,r
.bottom());
156 painter
.drawPath(lightgreyPath
);
159 if ((PlotGridY
> 0) && ((PlotGridY
* GraphPixelsPerPoint
) > 1)){
160 for(i
= 0; i
< ((r
.top() + r
.bottom())>>1); i
+= (int)(PlotGridY
* GraphPixelsPerPoint
)) {
161 lightgreyPath
.moveTo(r
.left() + 40,zeroHeight
+ i
);
162 lightgreyPath
.lineTo(r
.right(),zeroHeight
+ i
);
163 painter
.drawPath(lightgreyPath
);
164 lightgreyPath
.moveTo(r
.left() + 40,zeroHeight
- i
);
165 lightgreyPath
.lineTo(r
.right(),zeroHeight
- i
);
166 painter
.drawPath(lightgreyPath
);
170 startMax
= (GraphTraceLen
- (int)((r
.right() - r
.left() - 40) / GraphPixelsPerPoint
));
175 if(GraphStart
> startMax
)
176 GraphStart
= startMax
;
180 for(i
= GraphStart
; ;i
++) {
182 if(i
>= GraphTraceLen
) break;
184 if (fabs((double)GraphBuffer
[i
]) > absYMax
)
185 absYMax
= (int)fabs((double)GraphBuffer
[i
]);
187 int x
= 40 + (int)((i
- GraphStart
)*GraphPixelsPerPoint
);
189 if(x
> r
.right()) break;
192 absYMax
= (int)(absYMax
*1.2 + 1);
194 // number of points that will be plotted
195 int span
= (int)((r
.right() - r
.left()) / GraphPixelsPerPoint
);
197 // one label every 100 pixels, let us say
198 int labels
= (r
.right() - r
.left() - 40) / 100;
199 if(labels
<= 0) labels
= 1;
201 int pointsPerLabel
= span
/ labels
;
202 if(pointsPerLabel
<= 0) pointsPerLabel
= 1;
208 //int stt_x1 = 0, stt_x2 = 0;
210 for(i
= GraphStart
; ; i
++) {
211 if(i
>= GraphTraceLen
) break;
214 int x
= 40 + (int)((i
- GraphStart
) * GraphPixelsPerPoint
);
216 // if x reaches end of box, stop loop
217 if(x
> r
.right() + GraphPixelsPerPoint
) break;
219 int y
= GraphBuffer
[i
];
229 y
= (y
* (r
.top() - r
.bottom()) / (2*absYMax
)) + zeroHeight
;
232 penPath
.moveTo(x
, y
);
234 penPath
.lineTo(x
, y
);
237 // small white boxes (the dots on the signal)
238 if(GraphPixelsPerPoint
> 10) {
239 QRect
f(QPoint(x
- 3, y
- 3),QPoint(x
+ 3, y
+ 3));
240 painter
.fillRect(f
, brush
);
243 if(((i
- GraphStart
) % pointsPerLabel
== 0) && i
!= GraphStart
) {
244 whitePath
.moveTo(x
, zeroHeight
- 3);
245 whitePath
.lineTo(x
, zeroHeight
+ 3);
248 sprintf(str
, "+%d", (i
- GraphStart
));
250 painter
.setPen( QT_WHITE
);
252 QFontMetrics
metrics(painter
.font());
253 size
= metrics
.boundingRect(str
);
254 painter
.drawText(x
- (size
.right() - size
.left()), zeroHeight
+ 9, str
);
259 if(i
== CursorAPos
|| i
== CursorBPos
|| i
== CursorCPos
|| i
== CursorDPos
) {
260 QPainterPath
*cursorPath
;
262 if ( i
== CursorAPos
)
263 cursorPath
= &cursorAPath
;
264 else if ( i
== CursorBPos
)
265 cursorPath
= &cursorBPath
;
266 else if ( i
== CursorCPos
)
267 cursorPath
= &cursorCPath
;
269 cursorPath
= &cursorDPath
;
271 cursorPath
->moveTo(x
, r
.top());
272 cursorPath
->lineTo(x
, r
.bottom());
273 penPath
.moveTo(x
, y
);
277 // Mark STT block in signal
278 if ( CursorCPos
> 0 ){
279 int foo
= 40 + (int)((CursorCPos
- GraphStart
) * GraphPixelsPerPoint
);
280 int bar
= 40 + ((CursorDPos
- GraphStart
) * GraphPixelsPerPoint
);
281 QRect
r_stt(foo
, r
.top(), bar
-foo
, r
.bottom() );
282 QBrush
b_stt( QBrush( QT_ORANGE_TS
));
283 b_stt
.setStyle(Qt::Dense1Pattern
);
284 painter
.setPen(Qt::NoPen
);
285 painter
.fillRect(r_stt
, b_stt
);
289 //extern int PlotClock, PlockClockStartIndex;
291 for(int i
= PlockClockStartIndex
; ; i
+= PlotClock
* 2) {
293 if(i
>= GraphTraceLen
) break;
294 if ((CursorCPos
> 0) && (i
>= CursorCPos
)) break;
296 int foo
= 40 + (int)((i
- GraphStart
) * GraphPixelsPerPoint
);
297 int bar
= 40 + ((i
+ PlotClock
- GraphStart
) * GraphPixelsPerPoint
);
298 QRect
r_clock(foo
, r
.top(), bar
-foo
, r
.bottom() );
300 QBrush
b_clk( QBrush( QT_RED_TS
));
301 b_clk
.setStyle(Qt::Dense1Pattern
);
302 painter
.setPen(Qt::NoPen
);
303 painter
.fillRect(r_clock
, b_clk
);
310 painter
.setPen( QT_WHITE
); painter
.drawPath(whitePath
);
311 painter
.setPen(pen
); painter
.drawPath(penPath
);
312 painter
.setPen( QT_YELLOW
); painter
.drawPath(cursorAPath
);
313 painter
.setPen( QT_MAGENTA
); painter
.drawPath(cursorBPath
);
314 //painter.setPen( QT_ORANGE ); painter.drawPath(cursorCPath);
315 //painter.setPen( QT_LIGHTBLUE ); painter.drawPath(cursorDPath);
318 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) [Clock RF/%d]",
319 GraphStart
, yMax
, yMin
, yMean
, n
, GraphTraceLen
,
320 CursorBPos
- CursorAPos
,
321 (CursorBPos
- CursorAPos
)/CursorScaleFactor
,
324 GraphBuffer
[CursorAPos
],
326 GraphBuffer
[CursorBPos
],
329 GridLocked
? "Locked" : "Unlocked",
333 painter
.setPen( QT_WHITE
);
334 painter
.drawText(50, r
.bottom() - 10, str
);
337 ProxWidget::ProxWidget(QWidget
*parent
) : QWidget(parent
), GraphStart(0), GraphPixelsPerPoint(1)
341 QPalette
palette( QT_BLACK_TS
);
342 palette
.setColor(QPalette::WindowText
, QT_WHITE
);
343 palette
.setColor(QPalette::Text
, QT_WHITE
);
344 palette
.setColor(QPalette::Button
, QT_GRAY
);
346 setAutoFillBackground(true);
352 void ProxWidget::closeEvent(QCloseEvent
*event
)
358 void ProxWidget::mouseMoveEvent(QMouseEvent
*event
)
362 x
= (int)(x
/ GraphPixelsPerPoint
);
364 if((event
->buttons() & Qt::LeftButton
)) {
366 } else if (event
->buttons() & Qt::RightButton
) {
373 void ProxWidget::keyPressEvent(QKeyEvent
*event
)
380 if(event
->modifiers() & Qt::ShiftModifier
) {
382 offset
= PageWidth
- (PageWidth
% PlotGridX
);
386 if(event
->modifiers() & Qt::ControlModifier
)
389 offset
= (int)(20 / GraphPixelsPerPoint
);
391 switch(event
->key()) {
393 if(GraphPixelsPerPoint
<= 50) {
394 GraphPixelsPerPoint
*= 2;
399 if(GraphPixelsPerPoint
>= 0.02) {
400 GraphPixelsPerPoint
/= 2;
405 if(GraphPixelsPerPoint
< 20) {
406 if (PlotGridX
&& GridLocked
&& GraphStart
< startMax
){
407 GridOffset
-= offset
;
408 GridOffset
%= PlotGridX
;
411 GraphStart
+= offset
;
413 if (PlotGridX
&& GridLocked
&& GraphStart
< startMax
){
415 GridOffset
%= PlotGridX
;
421 GridOffset
+= PlotGridX
;
424 if (GraphStart
> startMax
) {
425 GridOffset
+= (GraphStart
- startMax
);
426 GridOffset
%= PlotGridX
;
431 if(GraphPixelsPerPoint
< 20) {
432 if (PlotGridX
&& GridLocked
&& GraphStart
> 0){
433 GridOffset
+= offset
;
434 GridOffset
%= PlotGridX
;
437 GraphStart
-= offset
;
439 if (PlotGridX
&& GridLocked
&& GraphStart
> 0){
441 GridOffset
%= PlotGridX
;
448 GridOffset
+= GraphStart
;
450 GridOffset
+= PlotGridX
;
451 GridOffset
%= PlotGridX
;
456 if(PlotGridX
|| PlotGridY
) {
460 PlotGridX
= PlotGridXdefault
;
461 PlotGridY
= PlotGridYdefault
;
466 puts("Plot Window Keystrokes:\n");
467 puts(" Key Action\n");
468 puts(" DOWN Zoom in");
469 puts(" G Toggle grid display");
470 puts(" H Show help");
471 puts(" L Toggle lock grid relative to samples");
472 puts(" LEFT Move left");
473 puts(" <CTL>LEFT Move left 1 sample");
474 puts(" <SHIFT>LEFT Page left");
475 puts(" LEFT-MOUSE-CLICK Set yellow cursor");
476 puts(" Q Hide window");
477 puts(" RIGHT Move right");
478 puts(" <CTL>RIGHT Move right 1 sample");
479 puts(" <SHIFT>RIGHT Page right");
480 puts(" RIGHT-MOUSE-CLICK Set purple cursor");
481 puts(" UP Zoom out");
483 puts("Use client window 'data help' for more plot commands\n");
487 GridLocked
= !GridLocked
;
495 QWidget::keyPressEvent(event
);