]> git.zerfleddert.de Git - proxmark3-svn/blame - client/proxguiqt.cpp
Added mifarePlus.lua script for communication with Mifare Plus. (#593)
[proxmark3-svn] / client / proxguiqt.cpp
CommitLineData
a553f267 1//-----------------------------------------------------------------------------
212ef3a0 2// Copyright (C) 2009 Michael Gernoth <michael at gernoth.net>
3//
a553f267 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
6// the license.
7//-----------------------------------------------------------------------------
8// GUI (QT)
9//-----------------------------------------------------------------------------
3472ebe5 10#include "proxguiqt.h"
a553f267 11
999d57c2 12#include <stdbool.h>
6658905f 13#include <iostream>
14#include <QPainterPath>
15#include <QBrush>
16#include <QPen>
17#include <QTimer>
18#include <QCloseEvent>
19#include <QMouseEvent>
20#include <QKeyEvent>
21#include <math.h>
22#include <limits.h>
c86cc308 23#include <stdio.h>
9fe4507c 24#include <QSlider>
b8fdac9e 25#include <QHBoxLayout>
26#include <string.h>
6658905f 27#include "proxgui.h"
b8fdac9e 28#include <QtGui>
29//#include <ctime>
30
999d57c2 31bool g_useOverlays = false;
32int g_absVMax = 0;
7ddb9900 33int startMax;
18856d88 34int PageWidth;
3fd7fce4 35int unlockStart = 0;
999d57c2 36
6658905f 37void ProxGuiQT::ShowGraphWindow(void)
38{
39 emit ShowGraphWindowSignal();
40}
41
42void ProxGuiQT::RepaintGraphWindow(void)
43{
44 emit RepaintGraphWindowSignal();
45}
46
47void ProxGuiQT::HideGraphWindow(void)
48{
49 emit HideGraphWindowSignal();
50}
51
1a3c0064 52void ProxGuiQT::Exit(void)
53{
54 emit ExitSignal();
55}
56
6658905f 57void ProxGuiQT::_ShowGraphWindow(void)
58{
59 if(!plotapp)
60 return;
61
62 if (!plotwidget)
63 plotwidget = new ProxWidget();
64
65 plotwidget->show();
66}
67
68void ProxGuiQT::_RepaintGraphWindow(void)
69{
70 if (!plotapp || !plotwidget)
71 return;
72
73 plotwidget->update();
74}
75
76void ProxGuiQT::_HideGraphWindow(void)
77{
78 if (!plotapp || !plotwidget)
79 return;
80
81 plotwidget->hide();
82}
83
1a3c0064 84void ProxGuiQT::_Exit(void) {
85 delete this;
86}
5acd195d 87
aa757f71
OM
88void ProxGuiQT::_StartProxmarkThread(void) {
89 if (!proxmarkThread)
90 return;
91
92 // if thread finished delete self and delete application
93 QObject::connect(proxmarkThread, SIGNAL(finished()), proxmarkThread, SLOT(deleteLater()));
94 QObject::connect(proxmarkThread, SIGNAL(finished()), this, SLOT(_Exit()));
95 // start proxmark thread
96 proxmarkThread->start();
97}
98
6658905f 99void ProxGuiQT::MainLoop()
100{
101 plotapp = new QApplication(argc, argv);
102
103 connect(this, SIGNAL(ShowGraphWindowSignal()), this, SLOT(_ShowGraphWindow()));
104 connect(this, SIGNAL(RepaintGraphWindowSignal()), this, SLOT(_RepaintGraphWindow()));
105 connect(this, SIGNAL(HideGraphWindowSignal()), this, SLOT(_HideGraphWindow()));
1a3c0064 106 connect(this, SIGNAL(ExitSignal()), this, SLOT(_Exit()));
6658905f 107
aa757f71
OM
108 //start proxmark thread after starting event loop
109 QTimer::singleShot(200, this, SLOT(_StartProxmarkThread()));
110
6658905f 111 plotapp->exec();
112}
113
aa757f71
OM
114ProxGuiQT::ProxGuiQT(int argc, char **argv, WorkerThread *wthread) : plotapp(NULL), plotwidget(NULL),
115 argc(argc), argv(argv), proxmarkThread(wthread)
6658905f 116{
117}
118
119ProxGuiQT::~ProxGuiQT(void)
120{
1a3c0064 121 //if (plotwidget) {
122 //plotwidget->destroy(true,true);
123 // delete plotwidget;
124 // plotwidget = NULL;
125 //}
6658905f 126 if (plotapp) {
127 plotapp->quit();
5acd195d 128 // delete plotapp;
6658905f 129 plotapp = NULL;
130 }
131}
132
b8fdac9e 133//--------------------
134void ProxWidget::applyOperation()
6658905f 135{
1a3c0064 136 //printf("ApplyOperation()");
3fd7fce4 137 save_restoreGB(GRAPH_SAVE);
c4f51073 138 memcpy(GraphBuffer, s_Buff, sizeof(int) * GraphTraceLen);
b8fdac9e 139 RepaintGraphWindow();
b8fdac9e 140}
141void ProxWidget::stickOperation()
142{
3fd7fce4 143 save_restoreGB(GRAPH_RESTORE);
1a3c0064 144 //printf("stickOperation()");
b8fdac9e 145}
146void ProxWidget::vchange_autocorr(int v)
147{
c4f51073 148 int ans;
149 ans = AutoCorrelate(GraphBuffer, s_Buff, GraphTraceLen, v, true, false);
1a3c0064 150 if (g_debugMode) printf("vchange_autocorr(w:%d): %d\n", v, ans);
999d57c2 151 g_useOverlays = true;
c4f51073 152 RepaintGraphWindow();
153}
154void ProxWidget::vchange_askedge(int v)
155{
156 int ans;
157 //extern int AskEdgeDetect(const int *in, int *out, int len, int threshold);
158 ans = AskEdgeDetect(GraphBuffer, s_Buff, GraphTraceLen, v);
1a3c0064 159 if (g_debugMode) printf("vchange_askedge(w:%d)%d\n", v, ans);
999d57c2 160 g_useOverlays = true;
b8fdac9e 161 RepaintGraphWindow();
162}
163void ProxWidget::vchange_dthr_up(int v)
164{
165 int down = opsController->horizontalSlider_dirthr_down->value();
c4f51073 166 directionalThreshold(GraphBuffer, s_Buff, GraphTraceLen, v, down);
1a3c0064 167 //printf("vchange_dthr_up(%d)", v);
999d57c2 168 g_useOverlays = true;
b8fdac9e 169 RepaintGraphWindow();
b8fdac9e 170}
171void ProxWidget::vchange_dthr_down(int v)
172{
1a3c0064 173 //printf("vchange_dthr_down(%d)", v);
b8fdac9e 174 int up = opsController->horizontalSlider_dirthr_up->value();
175 directionalThreshold(GraphBuffer,s_Buff, GraphTraceLen, v, up);
999d57c2 176 g_useOverlays = true;
b8fdac9e 177 RepaintGraphWindow();
b8fdac9e 178}
179ProxWidget::ProxWidget(QWidget *parent, ProxGuiQT *master) : QWidget(parent)
180{
181 this->master = master;
182 resize(800,500);
183
184 /** Setup the controller widget **/
185
1a3c0064 186 controlWidget = new QWidget();
b8fdac9e 187 opsController = new Ui::Form();
188 opsController->setupUi(controlWidget);
189 //Due to quirks in QT Designer, we need to fiddle a bit
190 opsController->horizontalSlider_dirthr_down->setMinimum(-128);
191 opsController->horizontalSlider_dirthr_down->setMaximum(0);
192 opsController->horizontalSlider_dirthr_down->setValue(-20);
999d57c2 193 opsController->horizontalSlider_dirthr_up->setMinimum(-40);
194 opsController->horizontalSlider_dirthr_up->setMaximum(128);
195 opsController->horizontalSlider_dirthr_up->setValue(20);
196 opsController->horizontalSlider_askedge->setValue(25);
197 opsController->horizontalSlider_window->setValue(4000);
b8fdac9e 198
199
200 QObject::connect(opsController->pushButton_apply, SIGNAL(clicked()), this, SLOT(applyOperation()));
201 QObject::connect(opsController->pushButton_sticky, SIGNAL(clicked()), this, SLOT(stickOperation()));
202 QObject::connect(opsController->horizontalSlider_window, SIGNAL(valueChanged(int)), this, SLOT(vchange_autocorr(int)));
203 QObject::connect(opsController->horizontalSlider_dirthr_up, SIGNAL(valueChanged(int)), this, SLOT(vchange_dthr_up(int)));
204 QObject::connect(opsController->horizontalSlider_dirthr_down, SIGNAL(valueChanged(int)), this, SLOT(vchange_dthr_down(int)));
c4f51073 205 QObject::connect(opsController->horizontalSlider_askedge, SIGNAL(valueChanged(int)), this, SLOT(vchange_askedge(int)));
b8fdac9e 206
207 controlWidget->show();
208
209 // Set up the plot widget, which does the actual plotting
210
211 plot = new Plot(this);
212 /*
213 QSlider* slider = new QSlider(Qt::Horizontal);
214 slider->setFocusPolicy(Qt::StrongFocus);
215 slider->setTickPosition(QSlider::TicksBothSides);
216 slider->setTickInterval(10);
217 slider->setSingleStep(1);
218 */
219 QVBoxLayout *layout = new QVBoxLayout;
220 //layout->addWidget(slider);
221 layout->addWidget(plot);
222 setLayout(layout);
1a3c0064 223 //printf("Proxwidget Constructor just set layout\r\n");
b8fdac9e 224}
225
1a3c0064 226// not 100% sure what i need in this block
227// feel free to fix - marshmellow...
228ProxWidget::~ProxWidget(void)
229{
230 if (controlWidget) {
231 controlWidget->close();
232 delete controlWidget;
233 controlWidget = NULL;
234 }
235
236 if (opsController) {
237 delete opsController;
238 opsController = NULL;
239 }
240
241 if (plot) {
242 plot->close();
243 delete plot;
244 plot = NULL;
245 }
246}
247void ProxWidget::closeEvent(QCloseEvent *event)
248{
249 event->ignore();
250 this->hide();
999d57c2 251 g_useOverlays = false;
1a3c0064 252}
253void ProxWidget::hideEvent(QHideEvent *event) {
254 controlWidget->hide();
255 plot->hide();
256}
257void ProxWidget::showEvent(QShowEvent *event) {
258 controlWidget->show();
259 plot->show();
260}
b8fdac9e 261
262//----------- Plotting
263
264int Plot::xCoordOf(int i, QRect r )
265{
266 return r.left() + (int)((i - GraphStart)*GraphPixelsPerPoint);
267}
268
269int Plot::yCoordOf(int v, QRect r, int maxVal)
270{
271 int z = (r.bottom() - r.top())/2;
e8924be8 272 if ( maxVal == 0 ) maxVal++;
b8fdac9e 273 return -(z * v) / maxVal + z;
274}
275
276int Plot::valueOf_yCoord(int y, QRect r, int maxVal)
277{
278 int z = (r.bottom() - r.top())/2;
279 return (y-z) * maxVal / z;
280}
281static const QColor GREEN = QColor(100,255,100);
282static const QColor RED = QColor(255,100,100);
283static const QColor BLUE = QColor(100,100,255);
284static const QColor GRAY = QColor(240,240,240);
285
286QColor Plot::getColor(int graphNum)
287{
288 switch (graphNum) {
289 case 0: return GREEN; //Green
290 case 1: return RED; //Red
291 case 2: return BLUE; //Blue
292 default: return GRAY; //Gray
6658905f 293 }
b8fdac9e 294}
6658905f 295
999d57c2 296void Plot::setMaxAndStart(int *buffer, int len, QRect plotRect)
297{
298 if (len == 0) return;
299 startMax = (len - (int)((plotRect.right() - plotRect.left() - 40) / GraphPixelsPerPoint));
300 if(startMax < 0) {
301 startMax = 0;
302 }
303 if(GraphStart > startMax) {
304 GraphStart = startMax;
305 }
306 if (GraphStart > len) return;
307 int vMin = INT_MAX, vMax = INT_MIN, v = 0;
308 int sample_index = GraphStart ;
309 for( ; sample_index < len && xCoordOf(sample_index,plotRect) < plotRect.right() ; sample_index++) {
310
311 v = buffer[sample_index];
312 if(v < vMin) vMin = v;
313 if(v > vMax) vMax = v;
314 }
315
316 g_absVMax = 0;
317 if(fabs( (double) vMin) > g_absVMax) g_absVMax = (int)fabs( (double) vMin);
318 if(fabs( (double) vMax) > g_absVMax) g_absVMax = (int)fabs( (double) vMax);
319 g_absVMax = (int)(g_absVMax*1.25 + 1);
320}
321
b8fdac9e 322void Plot::PlotDemod(uint8_t *buffer, size_t len, QRect plotRect, QRect annotationRect, QPainter *painter, int graphNum, int plotOffset)
323{
324 if (len == 0 || PlotGridX <= 0) return;
325 //clock_t begin = clock();
326 QPainterPath penPath;
327
328 int grid_delta_x = PlotGridX;
329 int first_delta_x = grid_delta_x; //(plotOffset > 0) ? PlotGridX : (PlotGridX +);
330 if (GraphStart > plotOffset) first_delta_x -= (GraphStart-plotOffset);
331 int DemodStart = GraphStart;
332 if (plotOffset > GraphStart) DemodStart = plotOffset;
333
334 int BitStart = 0;
335 // round down
336 if (DemodStart-plotOffset > 0) BitStart = (int)(((DemodStart-plotOffset)+(PlotGridX-1))/PlotGridX)-1;
337 first_delta_x += BitStart * PlotGridX;
1a3c0064 338 if (BitStart > (int)len) return;
b8fdac9e 339 int delta_x = 0;
340 int v = 0;
341 //printf("first_delta_x %i, grid_delta_x %i, DemodStart %i, BitStart %i\n",first_delta_x,grid_delta_x,DemodStart, BitStart);
342
343 painter->setPen(getColor(graphNum));
344 char str[5];
345 int absVMax = (int)(100*1.05+1);
346 int x = xCoordOf(DemodStart, plotRect);
347 int y = yCoordOf((buffer[BitStart]*200-100)*-1,plotRect,absVMax);
348 penPath.moveTo(x, y);
349 delta_x = 0;
350 int clk = first_delta_x;
1a3c0064 351 for(int i = BitStart; i < (int)len && xCoordOf(delta_x+DemodStart, plotRect) < plotRect.right(); i++) {
352 for (int ii = 0; ii < (clk) && i < (int)len && xCoordOf(DemodStart+delta_x+ii, plotRect) < plotRect.right() ; ii++ ) {
b8fdac9e 353 x = xCoordOf(DemodStart+delta_x+ii, plotRect);
354 v = buffer[i]*200-100;
355
356 y = yCoordOf( v, plotRect, absVMax);
0bf5872f 357
b8fdac9e 358 penPath.lineTo(x, y);
6658905f 359
b8fdac9e 360 if(GraphPixelsPerPoint > 10) {
361 QRect f(QPoint(x - 3, y - 3),QPoint(x + 3, y + 3));
362 painter->fillRect(f, QColor(100, 255, 100));
363 }
364 if (ii == (int)clk/2) {
365 //print label
366 sprintf(str, "%u",buffer[i]);
367 painter->drawText(x-8, y + ((buffer[i] > 0) ? 18 : -6), str);
368 }
369 }
370 delta_x += clk;
371 clk = grid_delta_x;
372 }
6658905f 373
b8fdac9e 374 //Graph annotations
375 painter->drawPath(penPath);
376}
6658905f 377
b8fdac9e 378void Plot::PlotGraph(int *buffer, int len, QRect plotRect, QRect annotationRect, QPainter *painter, int graphNum)
379{
380 if (len == 0) return;
381 //clock_t begin = clock();
382 QPainterPath penPath;
999d57c2 383 int vMin = INT_MAX, vMax = INT_MIN, vMean = 0, v = 0, i = 0;
b8fdac9e 384 int x = xCoordOf(GraphStart, plotRect);
999d57c2 385 int y = yCoordOf(buffer[GraphStart],plotRect,g_absVMax);
b8fdac9e 386 penPath.moveTo(x, y);
999d57c2 387 for(i = GraphStart; i < len && xCoordOf(i, plotRect) < plotRect.right(); i++) {
6658905f 388
b8fdac9e 389 x = xCoordOf(i, plotRect);
390 v = buffer[i];
6658905f 391
999d57c2 392 y = yCoordOf( v, plotRect, g_absVMax);
6658905f 393
b8fdac9e 394 penPath.lineTo(x, y);
6658905f 395
396 if(GraphPixelsPerPoint > 10) {
397 QRect f(QPoint(x - 3, y - 3),QPoint(x + 3, y + 3));
b8fdac9e 398 painter->fillRect(f, QColor(100, 255, 100));
6658905f 399 }
999d57c2 400 //catch stats
401 if(v < vMin) vMin = v;
402 if(v > vMax) vMax = v;
403 vMean += v;
b8fdac9e 404 }
999d57c2 405 vMean /= (i - GraphStart);
6658905f 406
b8fdac9e 407 painter->setPen(getColor(graphNum));
6658905f 408
b8fdac9e 409 //Draw y-axis
410 int xo = 5+(graphNum*40);
411 painter->drawLine(xo, plotRect.top(),xo, plotRect.bottom());
6658905f 412
999d57c2 413 int vMarkers = (g_absVMax - (g_absVMax % 10)) / 5;
b8fdac9e 414 int minYDist = 40; //Minimum pixel-distance between markers
6658905f 415
b8fdac9e 416 char yLbl[20];
6658905f 417
b8fdac9e 418 int n = 0;
419 int lasty0 = 65535;
6658905f 420
999d57c2 421 for(v = vMarkers; yCoordOf(v,plotRect,g_absVMax) > plotRect.top() && n < 20; v+= vMarkers ,n++)
b8fdac9e 422 {
999d57c2 423 int y0 = yCoordOf(v,plotRect,g_absVMax);
424 int y1 = yCoordOf(-v,plotRect,g_absVMax);
b8fdac9e 425
426 if(lasty0 - y0 < minYDist) continue;
427
428 painter->drawLine(xo-5,y0, xo+5, y0);
429
430 sprintf(yLbl, "%d", v);
431 painter->drawText(xo+8,y0+7,yLbl);
432
433 painter->drawLine(xo-5, y1, xo+5, y1);
434 sprintf(yLbl, "%d",-v);
435 painter->drawText(xo+8, y1+5 , yLbl);
436 lasty0 = y0;
437 }
438
439 //Graph annotations
440 painter->drawPath(penPath);
441 char str[200];
442 sprintf(str, "max=%d min=%d mean=%d n=%d/%d CursorAVal=[%d] CursorBVal=[%d]",
999d57c2 443 vMax, vMin, vMean, i, len, buffer[CursorAPos], buffer[CursorBPos]);
b8fdac9e 444 painter->drawText(20, annotationRect.bottom() - 23 - 20 * graphNum, str);
445
446 //clock_t end = clock();
447 //double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
448 //printf("Plot time %f\n", elapsed_secs);
449}
450
451void Plot::plotGridLines(QPainter* painter,QRect r)
452{
3fd7fce4 453 // set GridOffset
454 if (PlotGridX <= 0) return;
455 int offset = GridOffset;
456 if (GridLocked && PlotGridX) {
457 offset = GridOffset + PlotGridX - (GraphStart % PlotGridX);
458 } else if (!GridLocked && GraphStart > 0 && PlotGridX) {
459 offset = PlotGridX-((GraphStart - offset) % PlotGridX) + GraphStart - unlockStart;
460 }
461 offset %= PlotGridX;
462 if (offset < 0) offset += PlotGridX;
463
b8fdac9e 464 int i;
465 int grid_delta_x = (int) (PlotGridX * GraphPixelsPerPoint);
999d57c2 466 int grid_delta_y = PlotGridY;
b8fdac9e 467 if ((PlotGridX > 0) && ((PlotGridX * GraphPixelsPerPoint) > 1)) {
3fd7fce4 468 for(i = (offset * GraphPixelsPerPoint); i < r.right(); i += grid_delta_x) {
b8fdac9e 469 painter->drawLine(r.left()+i, r.top(), r.left()+i, r.bottom());
470 }
999d57c2 471 }
472 if (PlotGridY > 0) {
473 for(i = 0; yCoordOf(i,r,g_absVMax) > r.top(); i += grid_delta_y) {
474 painter->drawLine( r.left(), yCoordOf(i,r,g_absVMax), r.right(), yCoordOf(i,r,g_absVMax) );
475 painter->drawLine( r.left(), yCoordOf(i*-1,r,g_absVMax), r.right(), yCoordOf(i*-1,r,g_absVMax) );
6658905f 476 }
477 }
b8fdac9e 478}
479
480#define HEIGHT_INFO 60
481#define WIDTH_AXES 80
482
483void Plot::paintEvent(QPaintEvent *event)
484{
999d57c2 485
b8fdac9e 486 QPainter painter(this);
b8fdac9e 487 QBrush brush(QColor(100, 255, 100));
488 QPen pen(QColor(100, 255, 100));
489
490 painter.setFont(QFont("Courier New", 10));
491
492 if(GraphStart < 0) {
493 GraphStart = 0;
494 }
6658905f 495
b8fdac9e 496 if (CursorAPos > GraphTraceLen)
497 CursorAPos= 0;
498 if(CursorBPos > GraphTraceLen)
499 CursorBPos= 0;
500 if(CursorCPos > GraphTraceLen)
501 CursorCPos= 0;
502 if(CursorDPos > GraphTraceLen)
503 CursorDPos= 0;
504
505 QRect plotRect(WIDTH_AXES, 0, width()-WIDTH_AXES, height()-HEIGHT_INFO);
506 QRect infoRect(0, height()-HEIGHT_INFO, width(), HEIGHT_INFO);
507
508 //Grey background
509 painter.fillRect(rect(), QColor(60, 60, 60));
510 //Black foreground
511 painter.fillRect(plotRect, QColor(0, 0, 0));
512
999d57c2 513 //init graph variables
514 setMaxAndStart(GraphBuffer,GraphTraceLen,plotRect);
515
b8fdac9e 516 // center line
517 int zeroHeight = plotRect.top() + (plotRect.bottom() - plotRect.top()) / 2;
518 painter.setPen(QColor(100, 100, 100));
519 painter.drawLine(plotRect.left(), zeroHeight, plotRect.right(), zeroHeight);
520 // plot X and Y grid lines
521 plotGridLines(&painter, plotRect);
522
523 //Start painting graph
999d57c2 524 PlotGraph(GraphBuffer, GraphTraceLen,plotRect,infoRect,&painter,0);
b8fdac9e 525 if (showDemod && DemodBufferLen > 8) {
526 PlotDemod(DemodBuffer, DemodBufferLen,plotRect,infoRect,&painter,2,g_DemodStartIdx);
6658905f 527 }
999d57c2 528 if (g_useOverlays) {
529 //init graph variables
530 setMaxAndStart(s_Buff,GraphTraceLen,plotRect);
531 PlotGraph(s_Buff, GraphTraceLen,plotRect,infoRect,&painter,1);
532 }
b8fdac9e 533 // End graph drawing
6658905f 534
b8fdac9e 535 //Draw the cursors
536 if(CursorAPos > GraphStart && xCoordOf(CursorAPos, plotRect) < plotRect.right())
537 {
538 painter.setPen(QColor(255, 255, 0));
539 painter.drawLine(xCoordOf(CursorAPos, plotRect),plotRect.top(),xCoordOf(CursorAPos, plotRect),plotRect.bottom());
540 }
541 if(CursorBPos > GraphStart && xCoordOf(CursorBPos, plotRect) < plotRect.right())
542 {
543 painter.setPen(QColor(255, 0, 255));
544 painter.drawLine(xCoordOf(CursorBPos, plotRect),plotRect.top(),xCoordOf(CursorBPos, plotRect),plotRect.bottom());
545 }
546 if(CursorCPos > GraphStart && xCoordOf(CursorCPos, plotRect) < plotRect.right())
547 {
548 painter.setPen(QColor(255, 153, 0)); //orange
549 painter.drawLine(xCoordOf(CursorCPos, plotRect),plotRect.top(),xCoordOf(CursorCPos, plotRect),plotRect.bottom());
550 }
551 if(CursorDPos > GraphStart && xCoordOf(CursorDPos, plotRect) < plotRect.right())
552 {
553 painter.setPen(QColor(0, 0, 205)); //light blue
554 painter.drawLine(xCoordOf(CursorDPos, plotRect),plotRect.top(),xCoordOf(CursorDPos, plotRect),plotRect.bottom());
555 }
6658905f 556
b8fdac9e 557 //Draw annotations
346ad5fb 558 char str[200];
b8fdac9e 559 sprintf(str, "@%d dt=%d [%2.2f] zoom=%2.2f CursorAPos=%d CursorBPos=%d GridX=%d GridY=%d (%s) GridXoffset=%d",
560 GraphStart, CursorBPos - CursorAPos, (CursorBPos - CursorAPos)/CursorScaleFactor,
561 GraphPixelsPerPoint,CursorAPos,CursorBPos,PlotGridXdefault,PlotGridYdefault,GridLocked?"Locked":"Unlocked",GridOffset);
6658905f 562 painter.setPen(QColor(255, 255, 255));
b8fdac9e 563 painter.drawText(20, infoRect.bottom() - 3, str);
564
6658905f 565}
566
b8fdac9e 567Plot::Plot(QWidget *parent) : QWidget(parent), GraphStart(0), GraphPixelsPerPoint(1)
6658905f 568{
9fe4507c 569 //Need to set this, otherwise we don't receive keypress events
b8fdac9e 570 setFocusPolicy( Qt::StrongFocus);
9484ff3d 571 resize(600, 300);
6658905f 572
573 QPalette palette(QColor(0,0,0,0));
574 palette.setColor(QPalette::WindowText, QColor(255,255,255));
575 palette.setColor(QPalette::Text, QColor(255,255,255));
576 palette.setColor(QPalette::Button, QColor(100, 100, 100));
577 setPalette(palette);
578 setAutoFillBackground(true);
cee48e2b 579 CursorAPos = 0;
580 CursorBPos = 0;
b8fdac9e 581
582 setWindowTitle(tr("Sliders"));
e8924be8 583
584 master = parent;
6658905f 585}
586
b8fdac9e 587void Plot::closeEvent(QCloseEvent *event)
6658905f 588{
589 event->ignore();
590 this->hide();
999d57c2 591 g_useOverlays = false;
6658905f 592}
593
b8fdac9e 594void Plot::mouseMoveEvent(QMouseEvent *event)
6658905f 595{
596 int x = event->x();
b8fdac9e 597 x -= WIDTH_AXES;
6658905f 598 x = (int)(x / GraphPixelsPerPoint);
599 x += GraphStart;
600 if((event->buttons() & Qt::LeftButton)) {
601 CursorAPos = x;
602 } else if (event->buttons() & Qt::RightButton) {
603 CursorBPos = x;
604 }
605
606
607 this->update();
608}
609
b8fdac9e 610void Plot::keyPressEvent(QKeyEvent *event)
6658905f 611{
18856d88 612 int offset;
18856d88 613
614 if(event->modifiers() & Qt::ShiftModifier) {
615 if (PlotGridX)
616 offset= PageWidth - (PageWidth % PlotGridX);
617 else
618 offset= PageWidth;
ff2e9c1c 619 } else
620 if(event->modifiers() & Qt::ControlModifier)
621 offset= 1;
622 else
623 offset= (int)(20 / GraphPixelsPerPoint);
18856d88 624
6658905f 625 switch(event->key()) {
626 case Qt::Key_Down:
627 if(GraphPixelsPerPoint <= 50) {
628 GraphPixelsPerPoint *= 2;
629 }
630 break;
631
632 case Qt::Key_Up:
633 if(GraphPixelsPerPoint >= 0.02) {
634 GraphPixelsPerPoint /= 2;
635 }
636 break;
637
638 case Qt::Key_Right:
639 if(GraphPixelsPerPoint < 20) {
18856d88 640 GraphStart += offset;
6658905f 641 } else {
3bc2349d 642 GraphStart++;
6658905f 643 }
644 break;
645
646 case Qt::Key_Left:
647 if(GraphPixelsPerPoint < 20) {
18856d88 648 GraphStart -= offset;
6658905f 649 } else {
3bc2349d 650 GraphStart--;
6658905f 651 }
7ddb9900 652 break;
653
654 case Qt::Key_G:
655 if(PlotGridX || PlotGridY) {
656 PlotGridX= 0;
657 PlotGridY= 0;
658 } else {
659 PlotGridX= PlotGridXdefault;
660 PlotGridY= PlotGridYdefault;
3fd7fce4 661 }
7ddb9900 662 break;
663
664 case Qt::Key_H:
665 puts("Plot Window Keystrokes:\n");
ff2e9c1c 666 puts(" Key Action\n");
667 puts(" DOWN Zoom in");
668 puts(" G Toggle grid display");
669 puts(" H Show help");
670 puts(" L Toggle lock grid relative to samples");
671 puts(" LEFT Move left");
672 puts(" <CTL>LEFT Move left 1 sample");
673 puts(" <SHIFT>LEFT Page left");
674 puts(" LEFT-MOUSE-CLICK Set yellow cursor");
675 puts(" Q Hide window");
676 puts(" RIGHT Move right");
677 puts(" <CTL>RIGHT Move right 1 sample");
678 puts(" <SHIFT>RIGHT Page right");
679 puts(" RIGHT-MOUSE-CLICK Set purple cursor");
680 puts(" UP Zoom out");
7ddb9900 681 puts("");
682 puts("Use client window 'data help' for more plot commands\n");
683 break;
684
685 case Qt::Key_L:
3fd7fce4 686 GridLocked = !GridLocked;
687 if (GridLocked)
688 GridOffset += (GraphStart - unlockStart);
689 else
690 unlockStart = GraphStart;
7ddb9900 691 break;
692
693 case Qt::Key_Q:
e8924be8 694 master->hide();
6658905f 695 break;
696
697 default:
698 QWidget::keyPressEvent(event);
699 return;
700 break;
701 }
702
703 this->update();
704}
Impressum, Datenschutz