]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/proxguiqt.cpp
077e202fb55782c61e02e63576a58bc649b4c4c7
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 //-----------------------------------------------------------------------------
10 #include "proxguiqt.h"
13 #include <QPainterPath>
17 #include <QCloseEvent>
18 #include <QMouseEvent>
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
),
84 argc(argc
), argv(argv
)
88 ProxGuiQT::~ProxGuiQT(void)
102 void ProxWidget::paintEvent(QPaintEvent
*event
)
104 QPainter
painter(this);
105 QPainterPath penPath
, whitePath
, greyPath
, lightgreyPath
, cursorAPath
, cursorBPath
, cursorCPath
, cursorDPath
;
107 QBrush
brush(QColor(100, 255, 100));
108 QPen
pen(QColor(100, 255, 100));
110 painter
.setFont(QFont("Arial", 10));
116 if (CursorAPos
> GraphTraceLen
)
118 if(CursorBPos
> GraphTraceLen
)
120 if(CursorCPos
> GraphTraceLen
)
122 if(CursorDPos
> GraphTraceLen
)
127 painter
.fillRect(r
, QColor(0, 0, 0));
129 whitePath
.moveTo(r
.left() + 40, r
.top());
130 whitePath
.lineTo(r
.left() + 40, r
.bottom());
132 int zeroHeight
= r
.top() + (r
.bottom() - r
.top()) / 2;
134 greyPath
.moveTo(r
.left(), zeroHeight
);
135 greyPath
.lineTo(r
.right(), zeroHeight
);
136 painter
.setPen(QColor(100, 100, 100));
137 painter
.drawPath(greyPath
);
139 PageWidth
= (int)((r
.right() - r
.left() - 40) / GraphPixelsPerPoint
);
141 // plot X and Y grid lines
143 if ((PlotGridX
> 0) && ((PlotGridX
* GraphPixelsPerPoint
) > 1)) {
144 for(i
= 40 + (GridOffset
* GraphPixelsPerPoint
); i
< r
.right(); i
+= (int)(PlotGridX
* GraphPixelsPerPoint
)) {
145 //SelectObject(hdc, GreyPenLite);
146 //MoveToEx(hdc, r.left + i, r.top, NULL);
147 //LineTo(hdc, r.left + i, r.bottom);
148 lightgreyPath
.moveTo(r
.left()+i
,r
.top());
149 lightgreyPath
.lineTo(r
.left()+i
,r
.bottom());
150 painter
.drawPath(lightgreyPath
);
153 if ((PlotGridY
> 0) && ((PlotGridY
* GraphPixelsPerPoint
) > 1)){
154 for(i
= 0; i
< ((r
.top() + r
.bottom())>>1); i
+= (int)(PlotGridY
* GraphPixelsPerPoint
)) {
155 lightgreyPath
.moveTo(r
.left() + 40,zeroHeight
+ i
);
156 lightgreyPath
.lineTo(r
.right(),zeroHeight
+ i
);
157 painter
.drawPath(lightgreyPath
);
158 lightgreyPath
.moveTo(r
.left() + 40,zeroHeight
- i
);
159 lightgreyPath
.lineTo(r
.right(),zeroHeight
- i
);
160 painter
.drawPath(lightgreyPath
);
164 startMax
= (GraphTraceLen
- (int)((r
.right() - r
.left() - 40) / GraphPixelsPerPoint
));
168 if(GraphStart
> startMax
) {
169 GraphStart
= startMax
;
174 for(i
= GraphStart
; ; i
++) {
175 if(i
>= GraphTraceLen
) {
178 if(fabs((double)GraphBuffer
[i
]) > absYMax
) {
179 absYMax
= (int)fabs((double)GraphBuffer
[i
]);
181 int x
= 40 + (int)((i
- GraphStart
)*GraphPixelsPerPoint
);
187 absYMax
= (int)(absYMax
*1.2 + 1);
189 // number of points that will be plotted
190 int span
= (int)((r
.right() - r
.left()) / GraphPixelsPerPoint
);
191 // one label every 100 pixels, let us say
192 int labels
= (r
.right() - r
.left() - 40) / 100;
193 if(labels
<= 0) labels
= 1;
194 int pointsPerLabel
= span
/ labels
;
195 if(pointsPerLabel
<= 0) pointsPerLabel
= 1;
202 for(i
= GraphStart
; ; i
++) {
203 if(i
>= GraphTraceLen
) {
206 int x
= 40 + (int)((i
- GraphStart
)*GraphPixelsPerPoint
);
207 if(x
> r
.right() + GraphPixelsPerPoint
) {
211 int y
= GraphBuffer
[i
];
221 y
= (y
* (r
.top() - r
.bottom()) / (2*absYMax
)) + zeroHeight
;
222 if(i
== GraphStart
) {
223 penPath
.moveTo(x
, y
);
225 penPath
.lineTo(x
, y
);
228 if(GraphPixelsPerPoint
> 10) {
229 QRect
f(QPoint(x
- 3, y
- 3),QPoint(x
+ 3, y
+ 3));
230 painter
.fillRect(f
, brush
);
233 if(((i
- GraphStart
) % pointsPerLabel
== 0) && i
!= GraphStart
) {
234 whitePath
.moveTo(x
, zeroHeight
- 3);
235 whitePath
.lineTo(x
, zeroHeight
+ 3);
238 sprintf(str
, "+%d", (i
- GraphStart
));
240 painter
.setPen(QColor(255, 255, 255));
242 QFontMetrics
metrics(painter
.font());
243 size
= metrics
.boundingRect(str
);
244 painter
.drawText(x
- (size
.right() - size
.left()), zeroHeight
+ 9, str
);
249 if(i
== CursorAPos
|| i
== CursorBPos
|| i
== CursorCPos
|| i
== CursorDPos
) {
250 QPainterPath
*cursorPath
;
252 if(i
== CursorAPos
) {
253 cursorPath
= &cursorAPath
;
254 } else if (i
== CursorBPos
) {
255 cursorPath
= &cursorBPath
;
256 } else if (i
== CursorCPos
) {
257 cursorPath
= &cursorCPath
;
259 cursorPath
= &cursorDPath
;
261 cursorPath
->moveTo(x
, r
.top());
262 cursorPath
->lineTo(x
, r
.bottom());
263 penPath
.moveTo(x
, y
);
271 painter
.setPen(QColor(255, 255, 255));
272 painter
.drawPath(whitePath
);
274 painter
.drawPath(penPath
);
275 painter
.setPen(QColor(255, 255, 0));
276 painter
.drawPath(cursorAPath
);
277 painter
.setPen(QColor(255, 0, 255));
278 painter
.drawPath(cursorBPath
);
279 painter
.setPen(QColor(255, 153, 0)); //orange
280 painter
.drawPath(cursorCPath
);
281 painter
.setPen(QColor(0, 0, 205)); //light blue
282 painter
.drawPath(cursorDPath
);
285 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)",
286 GraphStart
, yMax
, yMin
, yMean
, n
, GraphTraceLen
,
287 CursorBPos
- CursorAPos
, (CursorBPos
- CursorAPos
)/CursorScaleFactor
,GraphPixelsPerPoint
,CursorAPos
,GraphBuffer
[CursorAPos
],CursorBPos
,GraphBuffer
[CursorBPos
],PlotGridXdefault
,PlotGridYdefault
,GridLocked
?"Locked":"Unlocked");
289 painter
.setPen(QColor(255, 255, 255));
290 painter
.drawText(50, r
.bottom() - 20, str
);
293 ProxWidget::ProxWidget(QWidget
*parent
) : QWidget(parent
), GraphStart(0), GraphPixelsPerPoint(1)
297 QPalette
palette(QColor(0,0,0,0));
298 palette
.setColor(QPalette::WindowText
, QColor(255,255,255));
299 palette
.setColor(QPalette::Text
, QColor(255,255,255));
300 palette
.setColor(QPalette::Button
, QColor(100, 100, 100));
302 setAutoFillBackground(true);
307 void ProxWidget::closeEvent(QCloseEvent
*event
)
313 void ProxWidget::mouseMoveEvent(QMouseEvent
*event
)
317 x
= (int)(x
/ GraphPixelsPerPoint
);
319 if((event
->buttons() & Qt::LeftButton
)) {
321 } else if (event
->buttons() & Qt::RightButton
) {
329 void ProxWidget::keyPressEvent(QKeyEvent
*event
)
336 if(event
->modifiers() & Qt::ShiftModifier
) {
338 offset
= PageWidth
- (PageWidth
% PlotGridX
);
342 if(event
->modifiers() & Qt::ControlModifier
)
345 offset
= (int)(20 / GraphPixelsPerPoint
);
347 switch(event
->key()) {
349 if(GraphPixelsPerPoint
<= 50) {
350 GraphPixelsPerPoint
*= 2;
355 if(GraphPixelsPerPoint
>= 0.02) {
356 GraphPixelsPerPoint
/= 2;
361 if(GraphPixelsPerPoint
< 20) {
362 if (PlotGridX
&& GridLocked
&& GraphStart
< startMax
){
363 GridOffset
-= offset
;
364 GridOffset
%= PlotGridX
;
367 GraphStart
+= offset
;
369 if (PlotGridX
&& GridLocked
&& GraphStart
< startMax
){
371 GridOffset
%= PlotGridX
;
377 GridOffset
+= PlotGridX
;
380 if (GraphStart
> startMax
) {
381 GridOffset
+= (GraphStart
- startMax
);
382 GridOffset
%= PlotGridX
;
387 if(GraphPixelsPerPoint
< 20) {
388 if (PlotGridX
&& GridLocked
&& GraphStart
> 0){
389 GridOffset
+= offset
;
390 GridOffset
%= PlotGridX
;
393 GraphStart
-= offset
;
395 if (PlotGridX
&& GridLocked
&& GraphStart
> 0){
397 GridOffset
%= PlotGridX
;
404 GridOffset
+= GraphStart
;
406 GridOffset
+= PlotGridX
;
407 GridOffset
%= PlotGridX
;
412 if(PlotGridX
|| PlotGridY
) {
416 PlotGridX
= PlotGridXdefault
;
417 PlotGridY
= PlotGridYdefault
;
422 puts("Plot Window Keystrokes:\n");
423 puts(" Key Action\n");
424 puts(" DOWN Zoom in");
425 puts(" G Toggle grid display");
426 puts(" H Show help");
427 puts(" L Toggle lock grid relative to samples");
428 puts(" LEFT Move left");
429 puts(" <CTL>LEFT Move left 1 sample");
430 puts(" <SHIFT>LEFT Page left");
431 puts(" LEFT-MOUSE-CLICK Set yellow cursor");
432 puts(" Q Hide window");
433 puts(" RIGHT Move right");
434 puts(" <CTL>RIGHT Move right 1 sample");
435 puts(" <SHIFT>RIGHT Page right");
436 puts(" RIGHT-MOUSE-CLICK Set purple cursor");
437 puts(" UP Zoom out");
439 puts("Use client window 'data help' for more plot commands\n");
443 GridLocked
= !GridLocked
;
451 QWidget::keyPressEvent(event
);