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