]>
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)
99 void ProxWidget::paintEvent(QPaintEvent
*event
)
101 QPainter
painter(this);
102 QPainterPath penPath
, whitePath
, greyPath
, lightgreyPath
, cursorAPath
, cursorBPath
;
104 QBrush
brush(QColor(100, 255, 100));
105 QPen
pen(QColor(100, 255, 100));
107 painter
.setFont(QFont("Arial", 10));
112 if (CursorAPos
> GraphTraceLen
)
115 if(CursorBPos
> GraphTraceLen
)
120 painter
.fillRect(r
, QColor(0, 0, 0));
122 whitePath
.moveTo(r
.left() + 40, r
.top());
123 whitePath
.lineTo(r
.left() + 40, r
.bottom());
125 int zeroHeight
= r
.top() + (r
.bottom() - r
.top()) / 2;
127 greyPath
.moveTo(r
.left(), zeroHeight
);
128 greyPath
.lineTo(r
.right(), zeroHeight
);
129 painter
.setPen(QColor(100, 100, 100));
130 painter
.drawPath(greyPath
);
132 PageWidth
= (int)((r
.right() - r
.left() - 40) / GraphPixelsPerPoint
);
134 // plot X and Y grid lines
136 if ((PlotGridX
> 0) && ((PlotGridX
* GraphPixelsPerPoint
) > 1)) {
137 for(i
= 40 + (GridOffset
* GraphPixelsPerPoint
); i
< r
.right(); i
+= (int)(PlotGridX
* GraphPixelsPerPoint
)) {
138 //SelectObject(hdc, GreyPenLite);
139 //MoveToEx(hdc, r.left + i, r.top, NULL);
140 //LineTo(hdc, r.left + i, r.bottom);
141 lightgreyPath
.moveTo(r
.left()+i
,r
.top());
142 lightgreyPath
.lineTo(r
.left()+i
,r
.bottom());
143 painter
.drawPath(lightgreyPath
);
146 if ((PlotGridY
> 0) && ((PlotGridY
* GraphPixelsPerPoint
) > 1)){
147 for(i
= 0; i
< ((r
.top() + r
.bottom())>>1); i
+= (int)(PlotGridY
* GraphPixelsPerPoint
)) {
148 lightgreyPath
.moveTo(r
.left() + 40,zeroHeight
+ i
);
149 lightgreyPath
.lineTo(r
.right(),zeroHeight
+ i
);
150 painter
.drawPath(lightgreyPath
);
151 lightgreyPath
.moveTo(r
.left() + 40,zeroHeight
- i
);
152 lightgreyPath
.lineTo(r
.right(),zeroHeight
- i
);
153 painter
.drawPath(lightgreyPath
);
157 startMax
= (GraphTraceLen
- (int)((r
.right() - r
.left() - 40) / GraphPixelsPerPoint
));
162 if(GraphStart
> startMax
)
163 GraphStart
= startMax
;
167 for(i
= GraphStart
; ; i
++) {
169 if(i
>= GraphTraceLen
) break;
171 if(fabs((double)GraphBuffer
[i
]) > absYMax
)
172 absYMax
= (int)fabs((double)GraphBuffer
[i
]);
174 int x
= 40 + (int)((i
- GraphStart
)*GraphPixelsPerPoint
);
176 if(x
> r
.right()) break;
179 absYMax
= (int)(absYMax
*1.2 + 1);
181 // number of points that will be plotted
182 int span
= (int)((r
.right() - r
.left()) / GraphPixelsPerPoint
);
184 // one label every 100 pixels, let us say
185 int labels
= (r
.right() - r
.left() - 40) / 100;
186 if(labels
<= 0) labels
= 1;
188 int pointsPerLabel
= span
/ labels
;
189 if(pointsPerLabel
<= 0) pointsPerLabel
= 1;
196 for(i
= GraphStart
; ; i
++) {
197 if(i
>= GraphTraceLen
) break;
199 int x
= 40 + (int)((i
- GraphStart
)*GraphPixelsPerPoint
);
200 if(x
> r
.right() + GraphPixelsPerPoint
) break;
202 int y
= GraphBuffer
[i
];
212 y
= (y
* (r
.top() - r
.bottom()) / (2*absYMax
)) + zeroHeight
;
215 penPath
.moveTo(x
, y
);
217 penPath
.lineTo(x
, y
);
220 if(GraphPixelsPerPoint
> 10) {
221 QRect
f(QPoint(x
- 3, y
- 3),QPoint(x
+ 3, y
+ 3));
222 painter
.fillRect(f
, brush
);
225 if(((i
- GraphStart
) % pointsPerLabel
== 0) && i
!= GraphStart
) {
226 whitePath
.moveTo(x
, zeroHeight
- 3);
227 whitePath
.lineTo(x
, zeroHeight
+ 3);
230 sprintf(str
, "+%d", (i
- GraphStart
));
232 painter
.setPen(QColor(255, 255, 255));
234 QFontMetrics
metrics(painter
.font());
235 size
= metrics
.boundingRect(str
);
236 painter
.drawText(x
- (size
.right() - size
.left()), zeroHeight
+ 9, str
);
241 if(i
== CursorAPos
|| i
== CursorBPos
) {
242 QPainterPath
*cursorPath
;
245 cursorPath
= &cursorAPath
;
247 cursorPath
= &cursorBPath
;
249 cursorPath
->moveTo(x
, r
.top());
250 cursorPath
->lineTo(x
, r
.bottom());
251 penPath
.moveTo(x
, y
);
258 painter
.setPen(QColor(255, 255, 255));
259 painter
.drawPath(whitePath
);
261 painter
.drawPath(penPath
);
262 painter
.setPen(QColor(255, 255, 0));
263 painter
.drawPath(cursorAPath
);
264 painter
.setPen(QColor(255, 0, 255));
265 painter
.drawPath(cursorBPath
);
268 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)",
269 GraphStart
, yMax
, yMin
, yMean
, n
, GraphTraceLen
,
270 CursorBPos
- CursorAPos
,
271 (CursorBPos
- CursorAPos
)/CursorScaleFactor
,
274 GraphBuffer
[CursorAPos
],
276 GraphBuffer
[CursorBPos
],
279 GridLocked
? "Locked" : "Unlocked"
282 painter
.setPen(QColor(255, 255, 255));
283 painter
.drawText(50, r
.bottom() - 20, str
);
286 ProxWidget::ProxWidget(QWidget
*parent
) : QWidget(parent
), GraphStart(0), GraphPixelsPerPoint(1)
290 QPalette
palette(QColor(0,0,0,0));
291 palette
.setColor(QPalette::WindowText
, QColor(255,255,255));
292 palette
.setColor(QPalette::Text
, QColor(255,255,255));
293 palette
.setColor(QPalette::Button
, QColor(100, 100, 100));
295 setAutoFillBackground(true);
300 void ProxWidget::closeEvent(QCloseEvent
*event
)
306 void ProxWidget::mouseMoveEvent(QMouseEvent
*event
)
310 x
= (int)(x
/ GraphPixelsPerPoint
);
312 if((event
->buttons() & Qt::LeftButton
)) {
314 } else if (event
->buttons() & Qt::RightButton
) {
321 void ProxWidget::keyPressEvent(QKeyEvent
*event
)
328 if(event
->modifiers() & Qt::ShiftModifier
) {
330 offset
= PageWidth
- (PageWidth
% PlotGridX
);
334 if(event
->modifiers() & Qt::ControlModifier
)
337 offset
= (int)(20 / GraphPixelsPerPoint
);
339 switch(event
->key()) {
341 if(GraphPixelsPerPoint
<= 50) {
342 GraphPixelsPerPoint
*= 2;
347 if(GraphPixelsPerPoint
>= 0.02) {
348 GraphPixelsPerPoint
/= 2;
353 if(GraphPixelsPerPoint
< 20) {
354 if (PlotGridX
&& GridLocked
&& GraphStart
< startMax
){
355 GridOffset
-= offset
;
356 GridOffset
%= PlotGridX
;
359 GraphStart
+= offset
;
361 if (PlotGridX
&& GridLocked
&& GraphStart
< startMax
){
363 GridOffset
%= PlotGridX
;
369 GridOffset
+= PlotGridX
;
372 if (GraphStart
> startMax
) {
373 GridOffset
+= (GraphStart
- startMax
);
374 GridOffset
%= PlotGridX
;
379 if(GraphPixelsPerPoint
< 20) {
380 if (PlotGridX
&& GridLocked
&& GraphStart
> 0){
381 GridOffset
+= offset
;
382 GridOffset
%= PlotGridX
;
385 GraphStart
-= offset
;
387 if (PlotGridX
&& GridLocked
&& GraphStart
> 0){
389 GridOffset
%= PlotGridX
;
396 GridOffset
+= GraphStart
;
398 GridOffset
+= PlotGridX
;
399 GridOffset
%= PlotGridX
;
404 if(PlotGridX
|| PlotGridY
) {
408 PlotGridX
= PlotGridXdefault
;
409 PlotGridY
= PlotGridYdefault
;
414 puts("Plot Window Keystrokes:\n");
415 puts(" Key Action\n");
416 puts(" DOWN Zoom in");
417 puts(" G Toggle grid display");
418 puts(" H Show help");
419 puts(" L Toggle lock grid relative to samples");
420 puts(" LEFT Move left");
421 puts(" <CTL>LEFT Move left 1 sample");
422 puts(" <SHIFT>LEFT Page left");
423 puts(" LEFT-MOUSE-CLICK Set yellow cursor");
424 puts(" Q Hide window");
425 puts(" RIGHT Move right");
426 puts(" <CTL>RIGHT Move right 1 sample");
427 puts(" <SHIFT>RIGHT Page right");
428 puts(" RIGHT-MOUSE-CLICK Set purple cursor");
429 puts(" UP Zoom out");
431 puts("Use client window 'data help' for more plot commands\n");
435 GridLocked
= !GridLocked
;
443 QWidget::keyPressEvent(event
);