]>
cvs.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)
100 #define QT_ORANGE QColor(255, 153, 0)
101 #define QT_WHITE QColor(255, 255, 255)
102 #define QT_YELLOW QColor(255, 255, 0)
103 #define QT_MAGENTA QColor(255, 0, 255)
104 #define QT_LIGHTBLUE QColor(0, 0, 205)
105 #define QT_LIGHTGREEN QColor(100, 255, 100)
106 #define QT_GRAY QColor(100,100,100)
107 #define QT_BLACK QColor(0,0,0)
108 // transparent colors
109 #define QT_ORANGE_TS QColor(255, 153, 0, 96)
110 #define QT_RED_TS QColor(255, 0, 0, 64)
111 #define QT_BLACK_TS QColor(0,0,0,0)
113 void ProxWidget::paintEvent(QPaintEvent
*event
)
115 QPainter
painter(this);
116 QPainterPath penPath
, whitePath
, greyPath
, lightgreyPath
, cursorAPath
, cursorBPath
, cursorCPath
, cursorDPath
;
118 QBrush
brush(QT_LIGHTGREEN
);
119 QPen
pen(QT_LIGHTGREEN
);
121 painter
.setFont(QFont("Arial", 10));
126 if (CursorAPos
> GraphTraceLen
) CursorAPos
= 0;
127 if (CursorBPos
> GraphTraceLen
) CursorBPos
= 0;
128 if (CursorCPos
> GraphTraceLen
) CursorCPos
= 0;
129 if (CursorDPos
> GraphTraceLen
) CursorDPos
= 0;
132 painter
.fillRect(r
, QT_BLACK
);
134 whitePath
.moveTo(r
.left() + 40, r
.top());
135 whitePath
.lineTo(r
.left() + 40, r
.bottom());
137 int zeroHeight
= r
.top() + (r
.bottom() - r
.top()) / 2;
139 greyPath
.moveTo(r
.left(), zeroHeight
);
140 greyPath
.lineTo(r
.right(), zeroHeight
);
141 painter
.setPen(QT_GRAY
);
142 painter
.drawPath(greyPath
);
144 PageWidth
= (int)((r
.right() - r
.left() - 40) / GraphPixelsPerPoint
);
146 // plot X and Y grid lines
148 if ((PlotGridX
> 0) && ((PlotGridX
* GraphPixelsPerPoint
) > 1)) {
149 for(i
= 40 + (GridOffset
* GraphPixelsPerPoint
); i
< r
.right(); i
+= (int)(PlotGridX
* GraphPixelsPerPoint
)) {
150 //SelectObject(hdc, GreyPenLite);
151 //MoveToEx(hdc, r.left + i, r.top, NULL);
152 //LineTo(hdc, r.left + i, r.bottom);
153 lightgreyPath
.moveTo(r
.left()+i
,r
.top());
154 lightgreyPath
.lineTo(r
.left()+i
,r
.bottom());
155 painter
.drawPath(lightgreyPath
);
158 if ((PlotGridY
> 0) && ((PlotGridY
* GraphPixelsPerPoint
) > 1)){
159 for(i
= 0; i
< ((r
.top() + r
.bottom())>>1); i
+= (int)(PlotGridY
* GraphPixelsPerPoint
)) {
160 lightgreyPath
.moveTo(r
.left() + 40,zeroHeight
+ i
);
161 lightgreyPath
.lineTo(r
.right(),zeroHeight
+ i
);
162 painter
.drawPath(lightgreyPath
);
163 lightgreyPath
.moveTo(r
.left() + 40,zeroHeight
- i
);
164 lightgreyPath
.lineTo(r
.right(),zeroHeight
- i
);
165 painter
.drawPath(lightgreyPath
);
169 startMax
= (GraphTraceLen
- (int)((r
.right() - r
.left() - 40) / GraphPixelsPerPoint
));
174 if(GraphStart
> startMax
)
175 GraphStart
= startMax
;
179 for(i
= GraphStart
; ;i
++) {
181 if(i
>= GraphTraceLen
) break;
183 if (fabs((double)GraphBuffer
[i
]) > absYMax
)
184 absYMax
= (int)fabs((double)GraphBuffer
[i
]);
186 int x
= 40 + (int)((i
- GraphStart
)*GraphPixelsPerPoint
);
188 if(x
> r
.right()) break;
191 absYMax
= (int)(absYMax
*1.2 + 1);
193 // number of points that will be plotted
194 int span
= (int)((r
.right() - r
.left()) / GraphPixelsPerPoint
);
196 // one label every 100 pixels, let us say
197 int labels
= (r
.right() - r
.left() - 40) / 100;
198 if(labels
<= 0) labels
= 1;
200 int pointsPerLabel
= span
/ labels
;
201 if(pointsPerLabel
<= 0) pointsPerLabel
= 1;
207 //int stt_x1 = 0, stt_x2 = 0;
209 for(i
= GraphStart
; ; i
++) {
210 if(i
>= GraphTraceLen
) break;
213 int x
= 40 + (int)((i
- GraphStart
) * GraphPixelsPerPoint
);
215 // if x reaches end of box, stop loop
216 if(x
> r
.right() + GraphPixelsPerPoint
) break;
218 int y
= GraphBuffer
[i
];
228 y
= (y
* (r
.top() - r
.bottom()) / (2*absYMax
)) + zeroHeight
;
231 penPath
.moveTo(x
, y
);
233 penPath
.lineTo(x
, y
);
236 // small white boxes (the dots on the signal)
237 if(GraphPixelsPerPoint
> 10) {
238 QRect
f(QPoint(x
- 3, y
- 3),QPoint(x
+ 3, y
+ 3));
239 painter
.fillRect(f
, brush
);
242 if(((i
- GraphStart
) % pointsPerLabel
== 0) && i
!= GraphStart
) {
243 whitePath
.moveTo(x
, zeroHeight
- 3);
244 whitePath
.lineTo(x
, zeroHeight
+ 3);
247 sprintf(str
, "+%d", (i
- GraphStart
));
249 painter
.setPen( QT_WHITE
);
251 QFontMetrics
metrics(painter
.font());
252 size
= metrics
.boundingRect(str
);
253 painter
.drawText(x
- (size
.right() - size
.left()), zeroHeight
+ 9, str
);
258 if(i
== CursorAPos
|| i
== CursorBPos
|| i
== CursorCPos
|| i
== CursorDPos
) {
259 QPainterPath
*cursorPath
;
261 if ( i
== CursorAPos
)
262 cursorPath
= &cursorAPath
;
263 else if ( i
== CursorBPos
)
264 cursorPath
= &cursorBPath
;
265 else if ( i
== CursorCPos
)
266 cursorPath
= &cursorCPath
;
268 cursorPath
= &cursorDPath
;
270 cursorPath
->moveTo(x
, r
.top());
271 cursorPath
->lineTo(x
, r
.bottom());
272 penPath
.moveTo(x
, y
);
276 // Mark STT block in signal
277 if ( CursorCPos
> 0 ){
278 int foo
= 40 + (int)((CursorCPos
- GraphStart
) * GraphPixelsPerPoint
);
279 int bar
= 40 + ((CursorDPos
- GraphStart
) * GraphPixelsPerPoint
);
280 QRect
r_stt(foo
, r
.top(), bar
-foo
, r
.bottom() );
281 painter
.fillRect(r_stt
, QBrush( QT_ORANGE_TS
));
282 painter
.drawRect(r_stt
);
286 //extern int PlotClock, PlockClockStartIndex;
288 for(int i
= PlockClockStartIndex
; ; i
+= PlotClock
* 2) {
290 if(i
>= GraphTraceLen
) break;
291 if ((CursorCPos
> 0) && (i
>= CursorCPos
)) break;
293 int foo
= 40 + (int)((i
- GraphStart
) * GraphPixelsPerPoint
);
294 int bar
= 40 + ((i
+ PlotClock
- GraphStart
) * GraphPixelsPerPoint
);
295 QRect
r_clock(foo
, r
.top(), bar
-foo
, r
.bottom() );
296 painter
.fillRect(r_clock
, QBrush( QT_RED_TS
));
297 painter
.drawRect(r_clock
);
304 painter
.setPen( QT_WHITE
); painter
.drawPath(whitePath
);
305 painter
.setPen(pen
); painter
.drawPath(penPath
);
306 painter
.setPen( QT_YELLOW
); painter
.drawPath(cursorAPath
);
307 painter
.setPen( QT_MAGENTA
); painter
.drawPath(cursorBPath
);
308 //painter.setPen( QT_ORANGE ); painter.drawPath(cursorCPath);
309 //painter.setPen( QT_LIGHTBLUE ); painter.drawPath(cursorDPath);
312 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)",
313 GraphStart
, yMax
, yMin
, yMean
, n
, GraphTraceLen
,
314 CursorBPos
- CursorAPos
,
315 (CursorBPos
- CursorAPos
)/CursorScaleFactor
,
318 GraphBuffer
[CursorAPos
],
320 GraphBuffer
[CursorBPos
],
323 GridLocked
? "Locked" : "Unlocked"
326 painter
.setPen( QT_WHITE
);
327 painter
.drawText(50, r
.bottom() - 20, str
);
330 ProxWidget::ProxWidget(QWidget
*parent
) : QWidget(parent
), GraphStart(0), GraphPixelsPerPoint(1)
334 QPalette
palette( QT_BLACK_TS
);
335 palette
.setColor(QPalette::WindowText
, QT_WHITE
);
336 palette
.setColor(QPalette::Text
, QT_WHITE
);
337 palette
.setColor(QPalette::Button
, QT_GRAY
);
339 setAutoFillBackground(true);
344 void ProxWidget::closeEvent(QCloseEvent
*event
)
350 void ProxWidget::mouseMoveEvent(QMouseEvent
*event
)
354 x
= (int)(x
/ GraphPixelsPerPoint
);
356 if((event
->buttons() & Qt::LeftButton
)) {
358 } else if (event
->buttons() & Qt::RightButton
) {
365 void ProxWidget::keyPressEvent(QKeyEvent
*event
)
372 if(event
->modifiers() & Qt::ShiftModifier
) {
374 offset
= PageWidth
- (PageWidth
% PlotGridX
);
378 if(event
->modifiers() & Qt::ControlModifier
)
381 offset
= (int)(20 / GraphPixelsPerPoint
);
383 switch(event
->key()) {
385 if(GraphPixelsPerPoint
<= 50) {
386 GraphPixelsPerPoint
*= 2;
391 if(GraphPixelsPerPoint
>= 0.02) {
392 GraphPixelsPerPoint
/= 2;
397 if(GraphPixelsPerPoint
< 20) {
398 if (PlotGridX
&& GridLocked
&& GraphStart
< startMax
){
399 GridOffset
-= offset
;
400 GridOffset
%= PlotGridX
;
403 GraphStart
+= offset
;
405 if (PlotGridX
&& GridLocked
&& GraphStart
< startMax
){
407 GridOffset
%= PlotGridX
;
413 GridOffset
+= PlotGridX
;
416 if (GraphStart
> startMax
) {
417 GridOffset
+= (GraphStart
- startMax
);
418 GridOffset
%= PlotGridX
;
423 if(GraphPixelsPerPoint
< 20) {
424 if (PlotGridX
&& GridLocked
&& GraphStart
> 0){
425 GridOffset
+= offset
;
426 GridOffset
%= PlotGridX
;
429 GraphStart
-= offset
;
431 if (PlotGridX
&& GridLocked
&& GraphStart
> 0){
433 GridOffset
%= PlotGridX
;
440 GridOffset
+= GraphStart
;
442 GridOffset
+= PlotGridX
;
443 GridOffset
%= PlotGridX
;
448 if(PlotGridX
|| PlotGridY
) {
452 PlotGridX
= PlotGridXdefault
;
453 PlotGridY
= PlotGridYdefault
;
458 puts("Plot Window Keystrokes:\n");
459 puts(" Key Action\n");
460 puts(" DOWN Zoom in");
461 puts(" G Toggle grid display");
462 puts(" H Show help");
463 puts(" L Toggle lock grid relative to samples");
464 puts(" LEFT Move left");
465 puts(" <CTL>LEFT Move left 1 sample");
466 puts(" <SHIFT>LEFT Page left");
467 puts(" LEFT-MOUSE-CLICK Set yellow cursor");
468 puts(" Q Hide window");
469 puts(" RIGHT Move right");
470 puts(" <CTL>RIGHT Move right 1 sample");
471 puts(" <SHIFT>RIGHT Page right");
472 puts(" RIGHT-MOUSE-CLICK Set purple cursor");
473 puts(" UP Zoom out");
475 puts("Use client window 'data help' for more plot commands\n");
479 GridLocked
= !GridLocked
;
487 QWidget::keyPressEvent(event
);