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