]>
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
),
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
;
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
)
123 painter
.fillRect(r
, QColor(0, 0, 0));
125 whitePath
.moveTo(r
.left() + 40, r
.top());
126 whitePath
.lineTo(r
.left() + 40, r
.bottom());
128 int zeroHeight
= r
.top() + (r
.bottom() - r
.top()) / 2;
130 greyPath
.moveTo(r
.left(), zeroHeight
);
131 greyPath
.lineTo(r
.right(), zeroHeight
);
132 painter
.setPen(QColor(100, 100, 100));
133 painter
.drawPath(greyPath
);
135 PageWidth
= (int)((r
.right() - r
.left() - 40) / GraphPixelsPerPoint
);
137 // plot X and Y grid lines
139 if ((PlotGridX
> 0) && ((PlotGridX
* GraphPixelsPerPoint
) > 1)) {
140 for(i
= 40 + (GridOffset
* GraphPixelsPerPoint
); i
< r
.right(); i
+= (int)(PlotGridX
* GraphPixelsPerPoint
)) {
141 //SelectObject(hdc, GreyPenLite);
142 //MoveToEx(hdc, r.left + i, r.top, NULL);
143 //LineTo(hdc, r.left + i, r.bottom);
144 lightgreyPath
.moveTo(r
.left()+i
,r
.top());
145 lightgreyPath
.lineTo(r
.left()+i
,r
.bottom());
146 painter
.drawPath(lightgreyPath
);
149 if ((PlotGridY
> 0) && ((PlotGridY
* GraphPixelsPerPoint
) > 1)){
150 for(i
= 0; i
< ((r
.top() + r
.bottom())>>1); i
+= (int)(PlotGridY
* GraphPixelsPerPoint
)) {
151 lightgreyPath
.moveTo(r
.left() + 40,zeroHeight
+ i
);
152 lightgreyPath
.lineTo(r
.right(),zeroHeight
+ i
);
153 painter
.drawPath(lightgreyPath
);
154 lightgreyPath
.moveTo(r
.left() + 40,zeroHeight
- i
);
155 lightgreyPath
.lineTo(r
.right(),zeroHeight
- i
);
156 painter
.drawPath(lightgreyPath
);
160 startMax
= (GraphTraceLen
- (int)((r
.right() - r
.left() - 40) / GraphPixelsPerPoint
));
164 if(GraphStart
> startMax
) {
165 GraphStart
= startMax
;
170 for(i
= GraphStart
; ; i
++) {
171 if(i
>= GraphTraceLen
) {
174 if(fabs((double)GraphBuffer
[i
]) > absYMax
) {
175 absYMax
= (int)fabs((double)GraphBuffer
[i
]);
177 int x
= 40 + (int)((i
- GraphStart
)*GraphPixelsPerPoint
);
183 absYMax
= (int)(absYMax
*1.2 + 1);
185 // number of points that will be plotted
186 int span
= (int)((r
.right() - r
.left()) / GraphPixelsPerPoint
);
187 // one label every 100 pixels, let us say
188 int labels
= (r
.right() - r
.left() - 40) / 100;
189 if(labels
<= 0) labels
= 1;
190 int pointsPerLabel
= span
/ labels
;
191 if(pointsPerLabel
<= 0) pointsPerLabel
= 1;
198 for(i
= GraphStart
; ; i
++) {
199 if(i
>= GraphTraceLen
) {
202 int x
= 40 + (int)((i
- GraphStart
)*GraphPixelsPerPoint
);
203 if(x
> r
.right() + GraphPixelsPerPoint
) {
207 int y
= GraphBuffer
[i
];
217 y
= (y
* (r
.top() - r
.bottom()) / (2*absYMax
)) + zeroHeight
;
218 if(i
== GraphStart
) {
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
) {
246 QPainterPath
*cursorPath
;
248 if(i
== CursorAPos
) {
249 cursorPath
= &cursorAPath
;
251 cursorPath
= &cursorBPath
;
253 cursorPath
->moveTo(x
, r
.top());
254 cursorPath
->lineTo(x
, r
.bottom());
255 penPath
.moveTo(x
, y
);
263 painter
.setPen(QColor(255, 255, 255));
264 painter
.drawPath(whitePath
);
266 painter
.drawPath(penPath
);
267 painter
.setPen(QColor(255, 255, 0));
268 painter
.drawPath(cursorAPath
);
269 painter
.setPen(QColor(255, 0, 255));
270 painter
.drawPath(cursorBPath
);
273 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)",
274 GraphStart
, yMax
, yMin
, yMean
, n
, GraphTraceLen
,
275 CursorBPos
- CursorAPos
, (CursorBPos
- CursorAPos
)/CursorScaleFactor
,GraphPixelsPerPoint
,CursorAPos
,GraphBuffer
[CursorAPos
],CursorBPos
,GraphBuffer
[CursorBPos
],PlotGridXdefault
,PlotGridYdefault
,GridLocked
?"Locked":"Unlocked");
277 painter
.setPen(QColor(255, 255, 255));
278 painter
.drawText(50, r
.bottom() - 20, str
);
281 ProxWidget::ProxWidget(QWidget
*parent
) : QWidget(parent
), GraphStart(0), GraphPixelsPerPoint(1)
285 QPalette
palette(QColor(0,0,0,0));
286 palette
.setColor(QPalette::WindowText
, QColor(255,255,255));
287 palette
.setColor(QPalette::Text
, QColor(255,255,255));
288 palette
.setColor(QPalette::Button
, QColor(100, 100, 100));
290 setAutoFillBackground(true);
293 void ProxWidget::closeEvent(QCloseEvent
*event
)
299 void ProxWidget::mouseMoveEvent(QMouseEvent
*event
)
303 x
= (int)(x
/ GraphPixelsPerPoint
);
305 if((event
->buttons() & Qt::LeftButton
)) {
307 } else if (event
->buttons() & Qt::RightButton
) {
315 void ProxWidget::keyPressEvent(QKeyEvent
*event
)
322 if(event
->modifiers() & Qt::ShiftModifier
) {
324 offset
= PageWidth
- (PageWidth
% PlotGridX
);
328 if(event
->modifiers() & Qt::ControlModifier
)
331 offset
= (int)(20 / GraphPixelsPerPoint
);
333 switch(event
->key()) {
335 if(GraphPixelsPerPoint
<= 50) {
336 GraphPixelsPerPoint
*= 2;
341 if(GraphPixelsPerPoint
>= 0.02) {
342 GraphPixelsPerPoint
/= 2;
347 if(GraphPixelsPerPoint
< 20) {
348 if (PlotGridX
&& GridLocked
&& GraphStart
< startMax
){
349 GridOffset
-= offset
;
350 GridOffset
%= PlotGridX
;
353 GraphStart
+= offset
;
355 if (PlotGridX
&& GridLocked
&& GraphStart
< startMax
){
357 GridOffset
%= PlotGridX
;
363 GridOffset
+= PlotGridX
;
366 if (GraphStart
> startMax
) {
367 GridOffset
+= (GraphStart
- startMax
);
368 GridOffset
%= PlotGridX
;
373 if(GraphPixelsPerPoint
< 20) {
374 if (PlotGridX
&& GridLocked
&& GraphStart
> 0){
375 GridOffset
+= offset
;
376 GridOffset
%= PlotGridX
;
379 GraphStart
-= offset
;
381 if (PlotGridX
&& GridLocked
&& GraphStart
> 0){
383 GridOffset
%= PlotGridX
;
390 GridOffset
+= GraphStart
;
392 GridOffset
+= PlotGridX
;
393 GridOffset
%= PlotGridX
;
398 if(PlotGridX
|| PlotGridY
) {
402 PlotGridX
= PlotGridXdefault
;
403 PlotGridY
= PlotGridYdefault
;
408 puts("Plot Window Keystrokes:\n");
409 puts(" Key Action\n");
410 puts(" DOWN Zoom in");
411 puts(" G Toggle grid display");
412 puts(" H Show help");
413 puts(" L Toggle lock grid relative to samples");
414 puts(" LEFT Move left");
415 puts(" <CTL>LEFT Move left 1 sample");
416 puts(" <SHIFT>LEFT Page left");
417 puts(" LEFT-MOUSE-CLICK Set yellow cursor");
418 puts(" Q Hide window");
419 puts(" RIGHT Move right");
420 puts(" <CTL>RIGHT Move right 1 sample");
421 puts(" <SHIFT>RIGHT Page right");
422 puts(" RIGHT-MOUSE-CLICK Set purple cursor");
423 puts(" UP Zoom out");
425 puts("Use client window 'data help' for more plot commands\n");
429 GridLocked
= !GridLocked
;
437 QWidget::keyPressEvent(event
);