]> git.zerfleddert.de Git - proxmark3-svn/blame - client/proxguiqt.cpp
fixed condition
[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;
272 return -(z * v) / maxVal + z;
273}
274
275int Plot::valueOf_yCoord(int y, QRect r, int maxVal)
276{
277 int z = (r.bottom() - r.top())/2;
278 return (y-z) * maxVal / z;
279}
280static const QColor GREEN = QColor(100,255,100);
281static const QColor RED = QColor(255,100,100);
282static const QColor BLUE = QColor(100,100,255);
283static const QColor GRAY = QColor(240,240,240);
284
285QColor Plot::getColor(int graphNum)
286{
287 switch (graphNum) {
288 case 0: return GREEN; //Green
289 case 1: return RED; //Red
290 case 2: return BLUE; //Blue
291 default: return GRAY; //Gray
6658905f 292 }
b8fdac9e 293}
6658905f 294
999d57c2 295void Plot::setMaxAndStart(int *buffer, int len, QRect plotRect)
296{
297 if (len == 0) return;
298 startMax = (len - (int)((plotRect.right() - plotRect.left() - 40) / GraphPixelsPerPoint));
299 if(startMax < 0) {
300 startMax = 0;
301 }
302 if(GraphStart > startMax) {
303 GraphStart = startMax;
304 }
305 if (GraphStart > len) return;
306 int vMin = INT_MAX, vMax = INT_MIN, v = 0;
307 int sample_index = GraphStart ;
308 for( ; sample_index < len && xCoordOf(sample_index,plotRect) < plotRect.right() ; sample_index++) {
309
310 v = buffer[sample_index];
311 if(v < vMin) vMin = v;
312 if(v > vMax) vMax = v;
313 }
314
315 g_absVMax = 0;
316 if(fabs( (double) vMin) > g_absVMax) g_absVMax = (int)fabs( (double) vMin);
317 if(fabs( (double) vMax) > g_absVMax) g_absVMax = (int)fabs( (double) vMax);
318 g_absVMax = (int)(g_absVMax*1.25 + 1);
319}
320
b8fdac9e 321void Plot::PlotDemod(uint8_t *buffer, size_t len, QRect plotRect, QRect annotationRect, QPainter *painter, int graphNum, int plotOffset)
322{
323 if (len == 0 || PlotGridX <= 0) return;
324 //clock_t begin = clock();
325 QPainterPath penPath;
326
327 int grid_delta_x = PlotGridX;
328 int first_delta_x = grid_delta_x; //(plotOffset > 0) ? PlotGridX : (PlotGridX +);
329 if (GraphStart > plotOffset) first_delta_x -= (GraphStart-plotOffset);
330 int DemodStart = GraphStart;
331 if (plotOffset > GraphStart) DemodStart = plotOffset;
332
333 int BitStart = 0;
334 // round down
335 if (DemodStart-plotOffset > 0) BitStart = (int)(((DemodStart-plotOffset)+(PlotGridX-1))/PlotGridX)-1;
336 first_delta_x += BitStart * PlotGridX;
1a3c0064 337 if (BitStart > (int)len) return;
b8fdac9e 338 int delta_x = 0;
339 int v = 0;
340 //printf("first_delta_x %i, grid_delta_x %i, DemodStart %i, BitStart %i\n",first_delta_x,grid_delta_x,DemodStart, BitStart);
341
342 painter->setPen(getColor(graphNum));
343 char str[5];
344 int absVMax = (int)(100*1.05+1);
345 int x = xCoordOf(DemodStart, plotRect);
346 int y = yCoordOf((buffer[BitStart]*200-100)*-1,plotRect,absVMax);
347 penPath.moveTo(x, y);
348 delta_x = 0;
349 int clk = first_delta_x;
1a3c0064 350 for(int i = BitStart; i < (int)len && xCoordOf(delta_x+DemodStart, plotRect) < plotRect.right(); i++) {
351 for (int ii = 0; ii < (clk) && i < (int)len && xCoordOf(DemodStart+delta_x+ii, plotRect) < plotRect.right() ; ii++ ) {
b8fdac9e 352 x = xCoordOf(DemodStart+delta_x+ii, plotRect);
353 v = buffer[i]*200-100;
354
355 y = yCoordOf( v, plotRect, absVMax);
0bf5872f 356
b8fdac9e 357 penPath.lineTo(x, y);
6658905f 358
b8fdac9e 359 if(GraphPixelsPerPoint > 10) {
360 QRect f(QPoint(x - 3, y - 3),QPoint(x + 3, y + 3));
361 painter->fillRect(f, QColor(100, 255, 100));
362 }
363 if (ii == (int)clk/2) {
364 //print label
365 sprintf(str, "%u",buffer[i]);
366 painter->drawText(x-8, y + ((buffer[i] > 0) ? 18 : -6), str);
367 }
368 }
369 delta_x += clk;
370 clk = grid_delta_x;
371 }
6658905f 372
b8fdac9e 373 //Graph annotations
374 painter->drawPath(penPath);
375}
6658905f 376
b8fdac9e 377void Plot::PlotGraph(int *buffer, int len, QRect plotRect, QRect annotationRect, QPainter *painter, int graphNum)
378{
379 if (len == 0) return;
380 //clock_t begin = clock();
381 QPainterPath penPath;
999d57c2 382 int vMin = INT_MAX, vMax = INT_MIN, vMean = 0, v = 0, i = 0;
b8fdac9e 383 int x = xCoordOf(GraphStart, plotRect);
999d57c2 384 int y = yCoordOf(buffer[GraphStart],plotRect,g_absVMax);
b8fdac9e 385 penPath.moveTo(x, y);
999d57c2 386 for(i = GraphStart; i < len && xCoordOf(i, plotRect) < plotRect.right(); i++) {
6658905f 387
b8fdac9e 388 x = xCoordOf(i, plotRect);
389 v = buffer[i];
6658905f 390
999d57c2 391 y = yCoordOf( v, plotRect, g_absVMax);
6658905f 392
b8fdac9e 393 penPath.lineTo(x, y);
6658905f 394
395 if(GraphPixelsPerPoint > 10) {
396 QRect f(QPoint(x - 3, y - 3),QPoint(x + 3, y + 3));
b8fdac9e 397 painter->fillRect(f, QColor(100, 255, 100));
6658905f 398 }
999d57c2 399 //catch stats
400 if(v < vMin) vMin = v;
401 if(v > vMax) vMax = v;
402 vMean += v;
b8fdac9e 403 }
999d57c2 404 vMean /= (i - GraphStart);
6658905f 405
b8fdac9e 406 painter->setPen(getColor(graphNum));
6658905f 407
b8fdac9e 408 //Draw y-axis
409 int xo = 5+(graphNum*40);
410 painter->drawLine(xo, plotRect.top(),xo, plotRect.bottom());
6658905f 411
999d57c2 412 int vMarkers = (g_absVMax - (g_absVMax % 10)) / 5;
b8fdac9e 413 int minYDist = 40; //Minimum pixel-distance between markers
6658905f 414
b8fdac9e 415 char yLbl[20];
6658905f 416
b8fdac9e 417 int n = 0;
418 int lasty0 = 65535;
6658905f 419
999d57c2 420 for(v = vMarkers; yCoordOf(v,plotRect,g_absVMax) > plotRect.top() && n < 20; v+= vMarkers ,n++)
b8fdac9e 421 {
999d57c2 422 int y0 = yCoordOf(v,plotRect,g_absVMax);
423 int y1 = yCoordOf(-v,plotRect,g_absVMax);
b8fdac9e 424
425 if(lasty0 - y0 < minYDist) continue;
426
427 painter->drawLine(xo-5,y0, xo+5, y0);
428
429 sprintf(yLbl, "%d", v);
430 painter->drawText(xo+8,y0+7,yLbl);
431
432 painter->drawLine(xo-5, y1, xo+5, y1);
433 sprintf(yLbl, "%d",-v);
434 painter->drawText(xo+8, y1+5 , yLbl);
435 lasty0 = y0;
436 }
437
438 //Graph annotations
439 painter->drawPath(penPath);
440 char str[200];
441 sprintf(str, "max=%d min=%d mean=%d n=%d/%d CursorAVal=[%d] CursorBVal=[%d]",
999d57c2 442 vMax, vMin, vMean, i, len, buffer[CursorAPos], buffer[CursorBPos]);
b8fdac9e 443 painter->drawText(20, annotationRect.bottom() - 23 - 20 * graphNum, str);
444
445 //clock_t end = clock();
446 //double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
447 //printf("Plot time %f\n", elapsed_secs);
448}
449
450void Plot::plotGridLines(QPainter* painter,QRect r)
451{
3fd7fce4 452 // set GridOffset
453 if (PlotGridX <= 0) return;
454 int offset = GridOffset;
455 if (GridLocked && PlotGridX) {
456 offset = GridOffset + PlotGridX - (GraphStart % PlotGridX);
457 } else if (!GridLocked && GraphStart > 0 && PlotGridX) {
458 offset = PlotGridX-((GraphStart - offset) % PlotGridX) + GraphStart - unlockStart;
459 }
460 offset %= PlotGridX;
461 if (offset < 0) offset += PlotGridX;
462
b8fdac9e 463 int i;
464 int grid_delta_x = (int) (PlotGridX * GraphPixelsPerPoint);
999d57c2 465 int grid_delta_y = PlotGridY;
b8fdac9e 466 if ((PlotGridX > 0) && ((PlotGridX * GraphPixelsPerPoint) > 1)) {
3fd7fce4 467 for(i = (offset * GraphPixelsPerPoint); i < r.right(); i += grid_delta_x) {
b8fdac9e 468 painter->drawLine(r.left()+i, r.top(), r.left()+i, r.bottom());
469 }
999d57c2 470 }
471 if (PlotGridY > 0) {
472 for(i = 0; yCoordOf(i,r,g_absVMax) > r.top(); i += grid_delta_y) {
473 painter->drawLine( r.left(), yCoordOf(i,r,g_absVMax), r.right(), yCoordOf(i,r,g_absVMax) );
474 painter->drawLine( r.left(), yCoordOf(i*-1,r,g_absVMax), r.right(), yCoordOf(i*-1,r,g_absVMax) );
6658905f 475 }
476 }
b8fdac9e 477}
478
479#define HEIGHT_INFO 60
480#define WIDTH_AXES 80
481
482void Plot::paintEvent(QPaintEvent *event)
483{
999d57c2 484
b8fdac9e 485 QPainter painter(this);
b8fdac9e 486 QBrush brush(QColor(100, 255, 100));
487 QPen pen(QColor(100, 255, 100));
488
489 painter.setFont(QFont("Courier New", 10));
490
491 if(GraphStart < 0) {
492 GraphStart = 0;
493 }
6658905f 494
b8fdac9e 495 if (CursorAPos > GraphTraceLen)
496 CursorAPos= 0;
497 if(CursorBPos > GraphTraceLen)
498 CursorBPos= 0;
499 if(CursorCPos > GraphTraceLen)
500 CursorCPos= 0;
501 if(CursorDPos > GraphTraceLen)
502 CursorDPos= 0;
503
504 QRect plotRect(WIDTH_AXES, 0, width()-WIDTH_AXES, height()-HEIGHT_INFO);
505 QRect infoRect(0, height()-HEIGHT_INFO, width(), HEIGHT_INFO);
506
507 //Grey background
508 painter.fillRect(rect(), QColor(60, 60, 60));
509 //Black foreground
510 painter.fillRect(plotRect, QColor(0, 0, 0));
511
999d57c2 512 //init graph variables
513 setMaxAndStart(GraphBuffer,GraphTraceLen,plotRect);
514
b8fdac9e 515 // center line
516 int zeroHeight = plotRect.top() + (plotRect.bottom() - plotRect.top()) / 2;
517 painter.setPen(QColor(100, 100, 100));
518 painter.drawLine(plotRect.left(), zeroHeight, plotRect.right(), zeroHeight);
519 // plot X and Y grid lines
520 plotGridLines(&painter, plotRect);
521
522 //Start painting graph
999d57c2 523 PlotGraph(GraphBuffer, GraphTraceLen,plotRect,infoRect,&painter,0);
b8fdac9e 524 if (showDemod && DemodBufferLen > 8) {
525 PlotDemod(DemodBuffer, DemodBufferLen,plotRect,infoRect,&painter,2,g_DemodStartIdx);
6658905f 526 }
999d57c2 527 if (g_useOverlays) {
528 //init graph variables
529 setMaxAndStart(s_Buff,GraphTraceLen,plotRect);
530 PlotGraph(s_Buff, GraphTraceLen,plotRect,infoRect,&painter,1);
531 }
b8fdac9e 532 // End graph drawing
6658905f 533
b8fdac9e 534 //Draw the cursors
535 if(CursorAPos > GraphStart && xCoordOf(CursorAPos, plotRect) < plotRect.right())
536 {
537 painter.setPen(QColor(255, 255, 0));
538 painter.drawLine(xCoordOf(CursorAPos, plotRect),plotRect.top(),xCoordOf(CursorAPos, plotRect),plotRect.bottom());
539 }
540 if(CursorBPos > GraphStart && xCoordOf(CursorBPos, plotRect) < plotRect.right())
541 {
542 painter.setPen(QColor(255, 0, 255));
543 painter.drawLine(xCoordOf(CursorBPos, plotRect),plotRect.top(),xCoordOf(CursorBPos, plotRect),plotRect.bottom());
544 }
545 if(CursorCPos > GraphStart && xCoordOf(CursorCPos, plotRect) < plotRect.right())
546 {
547 painter.setPen(QColor(255, 153, 0)); //orange
548 painter.drawLine(xCoordOf(CursorCPos, plotRect),plotRect.top(),xCoordOf(CursorCPos, plotRect),plotRect.bottom());
549 }
550 if(CursorDPos > GraphStart && xCoordOf(CursorDPos, plotRect) < plotRect.right())
551 {
552 painter.setPen(QColor(0, 0, 205)); //light blue
553 painter.drawLine(xCoordOf(CursorDPos, plotRect),plotRect.top(),xCoordOf(CursorDPos, plotRect),plotRect.bottom());
554 }
6658905f 555
b8fdac9e 556 //Draw annotations
346ad5fb 557 char str[200];
b8fdac9e 558 sprintf(str, "@%d dt=%d [%2.2f] zoom=%2.2f CursorAPos=%d CursorBPos=%d GridX=%d GridY=%d (%s) GridXoffset=%d",
559 GraphStart, CursorBPos - CursorAPos, (CursorBPos - CursorAPos)/CursorScaleFactor,
560 GraphPixelsPerPoint,CursorAPos,CursorBPos,PlotGridXdefault,PlotGridYdefault,GridLocked?"Locked":"Unlocked",GridOffset);
6658905f 561 painter.setPen(QColor(255, 255, 255));
b8fdac9e 562 painter.drawText(20, infoRect.bottom() - 3, str);
563
6658905f 564}
565
b8fdac9e 566Plot::Plot(QWidget *parent) : QWidget(parent), GraphStart(0), GraphPixelsPerPoint(1)
6658905f 567{
9fe4507c 568 //Need to set this, otherwise we don't receive keypress events
b8fdac9e 569 setFocusPolicy( Qt::StrongFocus);
9484ff3d 570 resize(600, 300);
6658905f 571
572 QPalette palette(QColor(0,0,0,0));
573 palette.setColor(QPalette::WindowText, QColor(255,255,255));
574 palette.setColor(QPalette::Text, QColor(255,255,255));
575 palette.setColor(QPalette::Button, QColor(100, 100, 100));
576 setPalette(palette);
577 setAutoFillBackground(true);
cee48e2b 578 CursorAPos = 0;
579 CursorBPos = 0;
b8fdac9e 580
581 setWindowTitle(tr("Sliders"));
6658905f 582}
583
b8fdac9e 584void Plot::closeEvent(QCloseEvent *event)
6658905f 585{
586 event->ignore();
587 this->hide();
999d57c2 588 g_useOverlays = false;
6658905f 589}
590
b8fdac9e 591void Plot::mouseMoveEvent(QMouseEvent *event)
6658905f 592{
593 int x = event->x();
b8fdac9e 594 x -= WIDTH_AXES;
6658905f 595 x = (int)(x / GraphPixelsPerPoint);
596 x += GraphStart;
597 if((event->buttons() & Qt::LeftButton)) {
598 CursorAPos = x;
599 } else if (event->buttons() & Qt::RightButton) {
600 CursorBPos = x;
601 }
602
603
604 this->update();
605}
606
b8fdac9e 607void Plot::keyPressEvent(QKeyEvent *event)
6658905f 608{
18856d88 609 int offset;
18856d88 610
611 if(event->modifiers() & Qt::ShiftModifier) {
612 if (PlotGridX)
613 offset= PageWidth - (PageWidth % PlotGridX);
614 else
615 offset= PageWidth;
ff2e9c1c 616 } else
617 if(event->modifiers() & Qt::ControlModifier)
618 offset= 1;
619 else
620 offset= (int)(20 / GraphPixelsPerPoint);
18856d88 621
6658905f 622 switch(event->key()) {
623 case Qt::Key_Down:
624 if(GraphPixelsPerPoint <= 50) {
625 GraphPixelsPerPoint *= 2;
626 }
627 break;
628
629 case Qt::Key_Up:
630 if(GraphPixelsPerPoint >= 0.02) {
631 GraphPixelsPerPoint /= 2;
632 }
633 break;
634
635 case Qt::Key_Right:
636 if(GraphPixelsPerPoint < 20) {
18856d88 637 GraphStart += offset;
6658905f 638 } else {
3bc2349d 639 GraphStart++;
6658905f 640 }
641 break;
642
643 case Qt::Key_Left:
644 if(GraphPixelsPerPoint < 20) {
18856d88 645 GraphStart -= offset;
6658905f 646 } else {
3bc2349d 647 GraphStart--;
6658905f 648 }
7ddb9900 649 break;
650
651 case Qt::Key_G:
652 if(PlotGridX || PlotGridY) {
653 PlotGridX= 0;
654 PlotGridY= 0;
655 } else {
656 PlotGridX= PlotGridXdefault;
657 PlotGridY= PlotGridYdefault;
3fd7fce4 658 }
7ddb9900 659 break;
660
661 case Qt::Key_H:
662 puts("Plot Window Keystrokes:\n");
ff2e9c1c 663 puts(" Key Action\n");
664 puts(" DOWN Zoom in");
665 puts(" G Toggle grid display");
666 puts(" H Show help");
667 puts(" L Toggle lock grid relative to samples");
668 puts(" LEFT Move left");
669 puts(" <CTL>LEFT Move left 1 sample");
670 puts(" <SHIFT>LEFT Page left");
671 puts(" LEFT-MOUSE-CLICK Set yellow cursor");
672 puts(" Q Hide window");
673 puts(" RIGHT Move right");
674 puts(" <CTL>RIGHT Move right 1 sample");
675 puts(" <SHIFT>RIGHT Page right");
676 puts(" RIGHT-MOUSE-CLICK Set purple cursor");
677 puts(" UP Zoom out");
7ddb9900 678 puts("");
679 puts("Use client window 'data help' for more plot commands\n");
680 break;
681
682 case Qt::Key_L:
3fd7fce4 683 GridLocked = !GridLocked;
684 if (GridLocked)
685 GridOffset += (GraphStart - unlockStart);
686 else
687 unlockStart = GraphStart;
7ddb9900 688 break;
689
690 case Qt::Key_Q:
691 this->hide();
6658905f 692 break;
693
694 default:
695 QWidget::keyPressEvent(event);
696 return;
697 break;
698 }
699
700 this->update();
701}
Impressum, Datenschutz