]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/proxguiqt.cpp
82f0e60ffa0dc0df232f05859b5ed0175598c179
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"
28 void ProxGuiQT::ShowGraphWindow(void)
30 emit
ShowGraphWindowSignal();
33 void ProxGuiQT::RepaintGraphWindow(void)
35 emit
RepaintGraphWindowSignal();
38 void ProxGuiQT::HideGraphWindow(void)
40 emit
HideGraphWindowSignal();
43 void ProxGuiQT::_ShowGraphWindow(void)
49 plotwidget
= new ProxWidget();
54 void ProxGuiQT::_RepaintGraphWindow(void)
56 if (!plotapp
|| !plotwidget
)
62 void ProxGuiQT::_HideGraphWindow(void)
64 if (!plotapp
|| !plotwidget
)
70 void ProxGuiQT::MainLoop()
72 plotapp
= new QApplication(argc
, argv
);
74 connect(this, SIGNAL(ShowGraphWindowSignal()), this, SLOT(_ShowGraphWindow()));
75 connect(this, SIGNAL(RepaintGraphWindowSignal()), this, SLOT(_RepaintGraphWindow()));
76 connect(this, SIGNAL(HideGraphWindowSignal()), this, SLOT(_HideGraphWindow()));
81 ProxGuiQT::ProxGuiQT(int argc
, char **argv
) : plotapp(NULL
), plotwidget(NULL
),
82 argc(argc
), argv(argv
)
86 ProxGuiQT::~ProxGuiQT(void)
100 void ProxWidget::paintEvent(QPaintEvent
*event
)
102 QPainter
painter(this);
103 QPainterPath penPath
, whitePath
, greyPath
, lightgreyPath
, cursorAPath
, cursorBPath
, cursorCPath
, cursorDPath
;
105 QBrush
brush(QColor(100, 255, 100));
106 QPen
pen(QColor(100, 255, 100));
108 painter
.setFont(QFont("Arial", 10));
114 if (CursorAPos
> GraphTraceLen
)
116 if(CursorBPos
> GraphTraceLen
)
118 if(CursorCPos
> GraphTraceLen
)
120 if(CursorDPos
> GraphTraceLen
)
125 painter
.fillRect(r
, QColor(0, 0, 0));
127 whitePath
.moveTo(r
.left() + 40, r
.top());
128 whitePath
.lineTo(r
.left() + 40, r
.bottom());
130 int zeroHeight
= r
.top() + (r
.bottom() - r
.top()) / 2;
132 greyPath
.moveTo(r
.left(), zeroHeight
);
133 greyPath
.lineTo(r
.right(), zeroHeight
);
134 painter
.setPen(QColor(100, 100, 100));
135 painter
.drawPath(greyPath
);
137 PageWidth
= (int)((r
.right() - r
.left() - 40) / GraphPixelsPerPoint
);
139 // plot X and Y grid lines
141 if ((PlotGridX
> 0) && ((PlotGridX
* GraphPixelsPerPoint
) > 1)) {
142 for(i
= 40 + (GridOffset
* GraphPixelsPerPoint
); i
< r
.right(); i
+= (int)(PlotGridX
* GraphPixelsPerPoint
)) {
143 //SelectObject(hdc, GreyPenLite);
144 //MoveToEx(hdc, r.left + i, r.top, NULL);
145 //LineTo(hdc, r.left + i, r.bottom);
146 lightgreyPath
.moveTo(r
.left()+i
,r
.top());
147 lightgreyPath
.lineTo(r
.left()+i
,r
.bottom());
148 painter
.drawPath(lightgreyPath
);
151 if ((PlotGridY
> 0) && ((PlotGridY
* GraphPixelsPerPoint
) > 1)){
152 for(i
= 0; i
< ((r
.top() + r
.bottom())>>1); i
+= (int)(PlotGridY
* GraphPixelsPerPoint
)) {
153 lightgreyPath
.moveTo(r
.left() + 40,zeroHeight
+ i
);
154 lightgreyPath
.lineTo(r
.right(),zeroHeight
+ i
);
155 painter
.drawPath(lightgreyPath
);
156 lightgreyPath
.moveTo(r
.left() + 40,zeroHeight
- i
);
157 lightgreyPath
.lineTo(r
.right(),zeroHeight
- i
);
158 painter
.drawPath(lightgreyPath
);
162 startMax
= (GraphTraceLen
- (int)((r
.right() - r
.left() - 40) / GraphPixelsPerPoint
));
166 if(GraphStart
> startMax
) {
167 GraphStart
= startMax
;
172 for(i
= GraphStart
; ; i
++) {
173 if(i
>= GraphTraceLen
) {
176 if(fabs((double)GraphBuffer
[i
]) > absYMax
) {
177 absYMax
= (int)fabs((double)GraphBuffer
[i
]);
179 int x
= 40 + (int)((i
- GraphStart
)*GraphPixelsPerPoint
);
185 absYMax
= (int)(absYMax
*1.2 + 1);
187 // number of points that will be plotted
188 int span
= (int)((r
.right() - r
.left()) / GraphPixelsPerPoint
);
189 // one label every 100 pixels, let us say
190 int labels
= (r
.right() - r
.left() - 40) / 100;
191 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
) {
204 int x
= 40 + (int)((i
- GraphStart
)*GraphPixelsPerPoint
);
205 if(x
> r
.right() + GraphPixelsPerPoint
) {
209 int y
= GraphBuffer
[i
];
219 y
= (y
* (r
.top() - r
.bottom()) / (2*absYMax
)) + zeroHeight
;
220 if(i
== GraphStart
) {
221 penPath
.moveTo(x
, y
);
223 penPath
.lineTo(x
, y
);
226 if(GraphPixelsPerPoint
> 10) {
227 QRect
f(QPoint(x
- 3, y
- 3),QPoint(x
+ 3, y
+ 3));
228 painter
.fillRect(f
, brush
);
231 if(((i
- GraphStart
) % pointsPerLabel
== 0) && i
!= GraphStart
) {
232 whitePath
.moveTo(x
, zeroHeight
- 3);
233 whitePath
.lineTo(x
, zeroHeight
+ 3);
236 sprintf(str
, "+%d", (i
- GraphStart
));
238 painter
.setPen(QColor(255, 255, 255));
240 QFontMetrics
metrics(painter
.font());
241 size
= metrics
.boundingRect(str
);
242 painter
.drawText(x
- (size
.right() - size
.left()), zeroHeight
+ 9, str
);
247 if(i
== CursorAPos
|| i
== CursorBPos
|| i
== CursorCPos
|| i
== CursorDPos
) {
248 QPainterPath
*cursorPath
;
250 if(i
== CursorAPos
) {
251 cursorPath
= &cursorAPath
;
252 } else if (i
== CursorBPos
) {
253 cursorPath
= &cursorBPath
;
254 } else if (i
== CursorCPos
) {
255 cursorPath
= &cursorCPath
;
257 cursorPath
= &cursorDPath
;
259 cursorPath
->moveTo(x
, r
.top());
260 cursorPath
->lineTo(x
, r
.bottom());
261 penPath
.moveTo(x
, y
);
269 painter
.setPen(QColor(255, 255, 255));
270 painter
.drawPath(whitePath
);
272 painter
.drawPath(penPath
);
273 painter
.setPen(QColor(255, 255, 0));
274 painter
.drawPath(cursorAPath
);
275 painter
.setPen(QColor(255, 0, 255));
276 painter
.drawPath(cursorBPath
);
277 painter
.setPen(QColor(255, 153, 0)); //orange
278 painter
.drawPath(cursorCPath
);
279 painter
.setPen(QColor(0, 0, 205)); //light blue
280 painter
.drawPath(cursorDPath
);
283 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)",
284 GraphStart
, yMax
, yMin
, yMean
, n
, GraphTraceLen
,
285 CursorBPos
- CursorAPos
, (CursorBPos
- CursorAPos
)/CursorScaleFactor
,GraphPixelsPerPoint
,CursorAPos
,GraphBuffer
[CursorAPos
],CursorBPos
,GraphBuffer
[CursorBPos
],PlotGridXdefault
,PlotGridYdefault
,GridLocked
?"Locked":"Unlocked");
287 painter
.setPen(QColor(255, 255, 255));
288 painter
.drawText(50, r
.bottom() - 20, str
);
291 ProxWidget::ProxWidget(QWidget
*parent
) : QWidget(parent
), GraphStart(0), GraphPixelsPerPoint(1)
295 QPalette
palette(QColor(0,0,0,0));
296 palette
.setColor(QPalette::WindowText
, QColor(255,255,255));
297 palette
.setColor(QPalette::Text
, QColor(255,255,255));
298 palette
.setColor(QPalette::Button
, QColor(100, 100, 100));
300 setAutoFillBackground(true);
305 void ProxWidget::closeEvent(QCloseEvent
*event
)
311 void ProxWidget::mouseMoveEvent(QMouseEvent
*event
)
315 x
= (int)(x
/ GraphPixelsPerPoint
);
317 if((event
->buttons() & Qt::LeftButton
)) {
319 } else if (event
->buttons() & Qt::RightButton
) {
327 void ProxWidget::keyPressEvent(QKeyEvent
*event
)
334 if(event
->modifiers() & Qt::ShiftModifier
) {
336 offset
= PageWidth
- (PageWidth
% PlotGridX
);
340 if(event
->modifiers() & Qt::ControlModifier
)
343 offset
= (int)(20 / GraphPixelsPerPoint
);
345 switch(event
->key()) {
347 if(GraphPixelsPerPoint
<= 50) {
348 GraphPixelsPerPoint
*= 2;
353 if(GraphPixelsPerPoint
>= 0.02) {
354 GraphPixelsPerPoint
/= 2;
359 if(GraphPixelsPerPoint
< 20) {
360 if (PlotGridX
&& GridLocked
&& GraphStart
< startMax
){
361 GridOffset
-= offset
;
362 GridOffset
%= PlotGridX
;
365 GraphStart
+= offset
;
367 if (PlotGridX
&& GridLocked
&& GraphStart
< startMax
){
369 GridOffset
%= PlotGridX
;
375 GridOffset
+= PlotGridX
;
378 if (GraphStart
> startMax
) {
379 GridOffset
+= (GraphStart
- startMax
);
380 GridOffset
%= PlotGridX
;
385 if(GraphPixelsPerPoint
< 20) {
386 if (PlotGridX
&& GridLocked
&& GraphStart
> 0){
387 GridOffset
+= offset
;
388 GridOffset
%= PlotGridX
;
391 GraphStart
-= offset
;
393 if (PlotGridX
&& GridLocked
&& GraphStart
> 0){
395 GridOffset
%= PlotGridX
;
402 GridOffset
+= GraphStart
;
404 GridOffset
+= PlotGridX
;
405 GridOffset
%= PlotGridX
;
410 if(PlotGridX
|| PlotGridY
) {
414 PlotGridX
= PlotGridXdefault
;
415 PlotGridY
= PlotGridYdefault
;
420 puts("Plot Window Keystrokes:\n");
421 puts(" Key Action\n");
422 puts(" DOWN Zoom in");
423 puts(" G Toggle grid display");
424 puts(" H Show help");
425 puts(" L Toggle lock grid relative to samples");
426 puts(" LEFT Move left");
427 puts(" <CTL>LEFT Move left 1 sample");
428 puts(" <SHIFT>LEFT Page left");
429 puts(" LEFT-MOUSE-CLICK Set yellow cursor");
430 puts(" Q Hide window");
431 puts(" RIGHT Move right");
432 puts(" <CTL>RIGHT Move right 1 sample");
433 puts(" <SHIFT>RIGHT Page right");
434 puts(" RIGHT-MOUSE-CLICK Set purple cursor");
435 puts(" UP Zoom out");
437 puts("Use client window 'data help' for more plot commands\n");
441 GridLocked
= !GridLocked
;
449 QWidget::keyPressEvent(event
);