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