1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2009 Michael Gernoth <michael at gernoth.net>
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
7 //-----------------------------------------------------------------------------
9 //-----------------------------------------------------------------------------
12 #include <QPainterPath>
16 #include <QCloseEvent>
17 #include <QMouseEvent>
22 #include <QHBoxLayout>
24 #include "proxguiqt.h"
33 void ProxGuiQT::ShowGraphWindow(void)
35 emit
ShowGraphWindowSignal();
38 void ProxGuiQT::RepaintGraphWindow(void)
40 emit
RepaintGraphWindowSignal();
43 void ProxGuiQT::HideGraphWindow(void)
45 emit
HideGraphWindowSignal();
48 void ProxGuiQT::_ShowGraphWindow(void)
54 plotwidget
= new ProxWidget();
59 void ProxGuiQT::_RepaintGraphWindow(void)
61 if (!plotapp
|| !plotwidget
)
67 void ProxGuiQT::_HideGraphWindow(void)
69 if (!plotapp
|| !plotwidget
)
75 void ProxGuiQT::MainLoop()
77 plotapp
= new QApplication(argc
, argv
);
79 connect(this, SIGNAL(ShowGraphWindowSignal()), this, SLOT(_ShowGraphWindow()));
80 connect(this, SIGNAL(RepaintGraphWindowSignal()), this, SLOT(_RepaintGraphWindow()));
81 connect(this, SIGNAL(HideGraphWindowSignal()), this, SLOT(_HideGraphWindow()));
86 ProxGuiQT::ProxGuiQT(int argc
, char **argv
) : plotapp(NULL
), plotwidget(NULL
),
87 argc(argc
), argv(argv
)
91 ProxGuiQT::~ProxGuiQT(void)
94 //plotwidget->close();
106 //--------------------
107 void ProxWidget::applyOperation()
109 printf("ApplyOperation()");
111 memcpy(GraphBuffer
,s_Buff
, sizeof(int) * GraphTraceLen
);
112 RepaintGraphWindow();
115 void ProxWidget::stickOperation()
118 printf("stickOperation()");
120 void ProxWidget::vchange_autocorr(int v
)
122 autoCorr(GraphBuffer
,s_Buff
, GraphTraceLen
, v
);
123 printf("vchange_autocorr(%d)\n", v
);
124 RepaintGraphWindow();
126 void ProxWidget::vchange_dthr_up(int v
)
128 int down
= opsController
->horizontalSlider_dirthr_down
->value();
129 directionalThreshold(GraphBuffer
,s_Buff
, GraphTraceLen
, v
, down
);
130 printf("vchange_dthr_up(%d)", v
);
131 RepaintGraphWindow();
134 void ProxWidget::vchange_dthr_down(int v
)
136 printf("vchange_dthr_down(%d)", v
);
137 int up
= opsController
->horizontalSlider_dirthr_up
->value();
138 directionalThreshold(GraphBuffer
,s_Buff
, GraphTraceLen
, v
, up
);
139 RepaintGraphWindow();
142 ProxWidget::ProxWidget(QWidget
*parent
, ProxGuiQT
*master
) : QWidget(parent
)
144 this->master
= master
;
147 /** Setup the controller widget **/
149 QWidget
* controlWidget
= new QWidget();
150 opsController
= new Ui::Form();
151 opsController
->setupUi(controlWidget
);
152 //Due to quirks in QT Designer, we need to fiddle a bit
153 opsController
->horizontalSlider_dirthr_down
->setMinimum(-128);
154 opsController
->horizontalSlider_dirthr_down
->setMaximum(0);
155 opsController
->horizontalSlider_dirthr_down
->setValue(-20);
158 QObject::connect(opsController
->pushButton_apply
, SIGNAL(clicked()), this, SLOT(applyOperation()));
159 QObject::connect(opsController
->pushButton_sticky
, SIGNAL(clicked()), this, SLOT(stickOperation()));
160 QObject::connect(opsController
->horizontalSlider_window
, SIGNAL(valueChanged(int)), this, SLOT(vchange_autocorr(int)));
161 QObject::connect(opsController
->horizontalSlider_dirthr_up
, SIGNAL(valueChanged(int)), this, SLOT(vchange_dthr_up(int)));
162 QObject::connect(opsController
->horizontalSlider_dirthr_down
, SIGNAL(valueChanged(int)), this, SLOT(vchange_dthr_down(int)));
164 controlWidget
->show();
166 // Set up the plot widget, which does the actual plotting
168 plot
= new Plot(this);
170 QSlider* slider = new QSlider(Qt::Horizontal);
171 slider->setFocusPolicy(Qt::StrongFocus);
172 slider->setTickPosition(QSlider::TicksBothSides);
173 slider->setTickInterval(10);
174 slider->setSingleStep(1);
176 QVBoxLayout
*layout
= new QVBoxLayout
;
177 //layout->addWidget(slider);
178 layout
->addWidget(plot
);
180 //printf("Proxwidget Constructor just set layout\r\n");
184 //----------- Plotting
186 int Plot::xCoordOf(int i
, QRect r
)
188 return r
.left() + (int)((i
- GraphStart
)*GraphPixelsPerPoint
);
191 int Plot::yCoordOf(int v
, QRect r
, int maxVal
)
193 int z
= (r
.bottom() - r
.top())/2;
194 return -(z
* v
) / maxVal
+ z
;
197 int Plot::valueOf_yCoord(int y
, QRect r
, int maxVal
)
199 int z
= (r
.bottom() - r
.top())/2;
200 return (y
-z
) * maxVal
/ z
;
202 static const QColor GREEN
= QColor(100,255,100);
203 static const QColor RED
= QColor(255,100,100);
204 static const QColor BLUE
= QColor(100,100,255);
205 static const QColor GRAY
= QColor(240,240,240);
207 QColor
Plot::getColor(int graphNum
)
210 case 0: return GREEN
; //Green
211 case 1: return RED
; //Red
212 case 2: return BLUE
; //Blue
213 default: return GRAY
; //Gray
217 void Plot::PlotDemod(uint8_t *buffer
, size_t len
, QRect plotRect
, QRect annotationRect
, QPainter
*painter
, int graphNum
, int plotOffset
)
219 if (len
== 0 || PlotGridX
<= 0) return;
220 //clock_t begin = clock();
221 QPainterPath penPath
;
223 int grid_delta_x
= PlotGridX
;
224 int first_delta_x
= grid_delta_x
; //(plotOffset > 0) ? PlotGridX : (PlotGridX +);
225 if (GraphStart
> plotOffset
) first_delta_x
-= (GraphStart
-plotOffset
);
226 int DemodStart
= GraphStart
;
227 if (plotOffset
> GraphStart
) DemodStart
= plotOffset
;
231 if (DemodStart
-plotOffset
> 0) BitStart
= (int)(((DemodStart
-plotOffset
)+(PlotGridX
-1))/PlotGridX
)-1;
232 first_delta_x
+= BitStart
* PlotGridX
;
233 if (BitStart
> len
) return;
236 //printf("first_delta_x %i, grid_delta_x %i, DemodStart %i, BitStart %i\n",first_delta_x,grid_delta_x,DemodStart, BitStart);
238 painter
->setPen(getColor(graphNum
));
240 int absVMax
= (int)(100*1.05+1);
241 int x
= xCoordOf(DemodStart
, plotRect
);
242 int y
= yCoordOf((buffer
[BitStart
]*200-100)*-1,plotRect
,absVMax
);
243 penPath
.moveTo(x
, y
);
245 int clk
= first_delta_x
;
246 for(int i
= BitStart
; i
< len
&& xCoordOf(delta_x
+DemodStart
, plotRect
) < plotRect
.right(); i
++) {
247 for (int ii
= 0; ii
< (clk
) && i
< len
&& xCoordOf(DemodStart
+delta_x
+ii
, plotRect
) < plotRect
.right() ; ii
++ ) {
248 x
= xCoordOf(DemodStart
+delta_x
+ii
, plotRect
);
249 v
= buffer
[i
]*200-100;
251 y
= yCoordOf( v
, plotRect
, absVMax
);
253 penPath
.lineTo(x
, y
);
255 if(GraphPixelsPerPoint
> 10) {
256 QRect
f(QPoint(x
- 3, y
- 3),QPoint(x
+ 3, y
+ 3));
257 painter
->fillRect(f
, QColor(100, 255, 100));
259 if (ii
== (int)clk
/2) {
261 sprintf(str
, "%u",buffer
[i
]);
262 painter
->drawText(x
-8, y
+ ((buffer
[i
] > 0) ? 18 : -6), str
);
270 painter
->drawPath(penPath
);
273 void Plot::PlotGraph(int *buffer
, int len
, QRect plotRect
, QRect annotationRect
, QPainter
*painter
, int graphNum
)
275 if (len
== 0) return;
276 //clock_t begin = clock();
277 QPainterPath penPath
;
279 startMax
= (len
- (int)((plotRect
.right() - plotRect
.left() - 40) / GraphPixelsPerPoint
));
283 if(GraphStart
> startMax
) {
284 GraphStart
= startMax
;
286 if (GraphStart
> len
) return;
287 int vMin
= INT_MAX
, vMax
= INT_MIN
, vMean
= 0, v
= 0, absVMax
= 0;
288 int sample_index
= GraphStart
;
289 for( ; sample_index
< len
&& xCoordOf(sample_index
,plotRect
) < plotRect
.right() ; sample_index
++) {
291 v
= buffer
[sample_index
];
292 if(v
< vMin
) vMin
= v
;
293 if(v
> vMax
) vMax
= v
;
297 vMean
/= (sample_index
- GraphStart
);
299 if(fabs( (double) vMin
) > absVMax
) absVMax
= (int)fabs( (double) vMin
);
300 if(fabs( (double) vMax
) > absVMax
) absVMax
= (int)fabs( (double) vMax
);
301 absVMax
= (int)(absVMax
*1.25 + 1);
302 // number of points that will be plotted
303 int span
= (int)((plotRect
.right() - plotRect
.left()) / GraphPixelsPerPoint
);
304 // one label every 100 pixels, let us say
305 int labels
= (plotRect
.right() - plotRect
.left() - 40) / 100;
306 if(labels
<= 0) labels
= 1;
307 int pointsPerLabel
= span
/ labels
;
308 if(pointsPerLabel
<= 0) pointsPerLabel
= 1;
310 int x
= xCoordOf(GraphStart
, plotRect
);
311 int y
= yCoordOf(buffer
[GraphStart
],plotRect
,absVMax
);
312 penPath
.moveTo(x
, y
);
313 for(int i
= GraphStart
; i
< len
&& xCoordOf(i
, plotRect
) < plotRect
.right(); i
++) {
315 x
= xCoordOf(i
, plotRect
);
318 y
= yCoordOf( v
, plotRect
, absVMax
);//(y * (r.top() - r.bottom()) / (2*absYMax)) + zeroHeight;
320 penPath
.lineTo(x
, y
);
322 if(GraphPixelsPerPoint
> 10) {
323 QRect
f(QPoint(x
- 3, y
- 3),QPoint(x
+ 3, y
+ 3));
324 painter
->fillRect(f
, QColor(100, 255, 100));
328 painter
->setPen(getColor(graphNum
));
331 int xo
= 5+(graphNum
*40);
332 painter
->drawLine(xo
, plotRect
.top(),xo
, plotRect
.bottom());
334 int vMarkers
= (absVMax
- (absVMax
% 10)) / 5;
335 int minYDist
= 40; //Minimum pixel-distance between markers
342 for(int v
= vMarkers
; yCoordOf(v
,plotRect
,absVMax
) > plotRect
.top() && n
< 20; v
+= vMarkers
,n
++)
344 int y0
= yCoordOf(v
,plotRect
,absVMax
);
345 int y1
= yCoordOf(-v
,plotRect
,absVMax
);
347 if(lasty0
- y0
< minYDist
) continue;
349 painter
->drawLine(xo
-5,y0
, xo
+5, y0
);
351 sprintf(yLbl
, "%d", v
);
352 painter
->drawText(xo
+8,y0
+7,yLbl
);
354 painter
->drawLine(xo
-5, y1
, xo
+5, y1
);
355 sprintf(yLbl
, "%d",-v
);
356 painter
->drawText(xo
+8, y1
+5 , yLbl
);
361 painter
->drawPath(penPath
);
363 sprintf(str
, "max=%d min=%d mean=%d n=%d/%d CursorAVal=[%d] CursorBVal=[%d]",
364 vMax
, vMin
, vMean
, sample_index
, len
, buffer
[CursorAPos
], buffer
[CursorBPos
]);
365 painter
->drawText(20, annotationRect
.bottom() - 23 - 20 * graphNum
, str
);
367 //clock_t end = clock();
368 //double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
369 //printf("Plot time %f\n", elapsed_secs);
372 void Plot::plotGridLines(QPainter
* painter
,QRect r
)
374 int zeroHeight
= r
.top() + (r
.bottom() - r
.top()) / 2;
377 int grid_delta_x
= (int) (PlotGridX
* GraphPixelsPerPoint
);
378 int grid_delta_y
= (int) (PlotGridY
* GraphPixelsPerPoint
);
379 if ((PlotGridX
> 0) && ((PlotGridX
* GraphPixelsPerPoint
) > 1)) {
380 for(i
= (GridOffset
* GraphPixelsPerPoint
); i
< r
.right(); i
+= grid_delta_x
) {
381 painter
->drawLine(r
.left()+i
, r
.top(), r
.left()+i
, r
.bottom());
384 if ((PlotGridY
> 0) && ((PlotGridY
* GraphPixelsPerPoint
) > 1)){
385 for(i
= 0; i
< ((r
.top() + r
.bottom())>>1); i
+= grid_delta_y
) {
386 painter
->drawLine(r
.left(),zeroHeight
+ i
,r
.right(),zeroHeight
+ i
);
387 painter
->drawLine(r
.left(),zeroHeight
- i
,r
.right(),zeroHeight
- i
);
392 #define HEIGHT_INFO 60
393 #define WIDTH_AXES 80
395 void Plot::paintEvent(QPaintEvent
*event
)
397 QPainter
painter(this);
398 //QPainterPath penPath, whitePath, greyPath, lightgreyPath, cursorAPath, cursorBPath, cursorCPath, cursorDPath;
400 QBrush
brush(QColor(100, 255, 100));
401 QPen
pen(QColor(100, 255, 100));
403 painter
.setFont(QFont("Courier New", 10));
409 if (CursorAPos
> GraphTraceLen
)
411 if(CursorBPos
> GraphTraceLen
)
413 if(CursorCPos
> GraphTraceLen
)
415 if(CursorDPos
> GraphTraceLen
)
418 QRect
plotRect(WIDTH_AXES
, 0, width()-WIDTH_AXES
, height()-HEIGHT_INFO
);
419 QRect
infoRect(0, height()-HEIGHT_INFO
, width(), HEIGHT_INFO
);
422 painter
.fillRect(rect(), QColor(60, 60, 60));
424 painter
.fillRect(plotRect
, QColor(0, 0, 0));
427 int zeroHeight
= plotRect
.top() + (plotRect
.bottom() - plotRect
.top()) / 2;
428 painter
.setPen(QColor(100, 100, 100));
429 painter
.drawLine(plotRect
.left(), zeroHeight
, plotRect
.right(), zeroHeight
);
430 // plot X and Y grid lines
431 plotGridLines(&painter
, plotRect
);
433 //Start painting graph
434 PlotGraph(GraphBuffer
, GraphTraceLen
,plotRect
,infoRect
,&painter
,0);
435 PlotGraph(s_Buff
, GraphTraceLen
,plotRect
,infoRect
,&painter
,1);
436 if (showDemod
&& DemodBufferLen
> 8) {
437 PlotDemod(DemodBuffer
, DemodBufferLen
,plotRect
,infoRect
,&painter
,2,g_DemodStartIdx
);
442 if(CursorAPos
> GraphStart
&& xCoordOf(CursorAPos
, plotRect
) < plotRect
.right())
444 painter
.setPen(QColor(255, 255, 0));
445 painter
.drawLine(xCoordOf(CursorAPos
, plotRect
),plotRect
.top(),xCoordOf(CursorAPos
, plotRect
),plotRect
.bottom());
447 if(CursorBPos
> GraphStart
&& xCoordOf(CursorBPos
, plotRect
) < plotRect
.right())
449 painter
.setPen(QColor(255, 0, 255));
450 painter
.drawLine(xCoordOf(CursorBPos
, plotRect
),plotRect
.top(),xCoordOf(CursorBPos
, plotRect
),plotRect
.bottom());
452 if(CursorCPos
> GraphStart
&& xCoordOf(CursorCPos
, plotRect
) < plotRect
.right())
454 painter
.setPen(QColor(255, 153, 0)); //orange
455 painter
.drawLine(xCoordOf(CursorCPos
, plotRect
),plotRect
.top(),xCoordOf(CursorCPos
, plotRect
),plotRect
.bottom());
457 if(CursorDPos
> GraphStart
&& xCoordOf(CursorDPos
, plotRect
) < plotRect
.right())
459 painter
.setPen(QColor(0, 0, 205)); //light blue
460 painter
.drawLine(xCoordOf(CursorDPos
, plotRect
),plotRect
.top(),xCoordOf(CursorDPos
, plotRect
),plotRect
.bottom());
465 sprintf(str
, "@%d dt=%d [%2.2f] zoom=%2.2f CursorAPos=%d CursorBPos=%d GridX=%d GridY=%d (%s) GridXoffset=%d",
466 GraphStart
, CursorBPos
- CursorAPos
, (CursorBPos
- CursorAPos
)/CursorScaleFactor
,
467 GraphPixelsPerPoint
,CursorAPos
,CursorBPos
,PlotGridXdefault
,PlotGridYdefault
,GridLocked
?"Locked":"Unlocked",GridOffset
);
468 painter
.setPen(QColor(255, 255, 255));
469 painter
.drawText(20, infoRect
.bottom() - 3, str
);
473 Plot::Plot(QWidget
*parent
) : QWidget(parent
), GraphStart(0), GraphPixelsPerPoint(1)
475 setFocusPolicy( Qt::StrongFocus
);
478 QPalette
palette(QColor(0,0,0,0));
479 palette
.setColor(QPalette::WindowText
, QColor(255,255,255));
480 palette
.setColor(QPalette::Text
, QColor(255,255,255));
481 palette
.setColor(QPalette::Button
, QColor(100, 100, 100));
483 setAutoFillBackground(true);
487 setWindowTitle(tr("Sliders"));
490 void Plot::closeEvent(QCloseEvent
*event
)
496 void Plot::mouseMoveEvent(QMouseEvent
*event
)
500 x
= (int)(x
/ GraphPixelsPerPoint
);
502 if((event
->buttons() & Qt::LeftButton
)) {
504 } else if (event
->buttons() & Qt::RightButton
) {
512 void Plot::keyPressEvent(QKeyEvent
*event
)
519 if(event
->modifiers() & Qt::ShiftModifier
) {
521 offset
= PageWidth
- (PageWidth
% PlotGridX
);
525 if(event
->modifiers() & Qt::ControlModifier
)
528 offset
= (int)(20 / GraphPixelsPerPoint
);
530 switch(event
->key()) {
532 if(GraphPixelsPerPoint
<= 50) {
533 GraphPixelsPerPoint
*= 2;
538 if(GraphPixelsPerPoint
>= 0.02) {
539 GraphPixelsPerPoint
/= 2;
544 if(GraphPixelsPerPoint
< 20) {
545 if (PlotGridX
&& GridLocked
&& GraphStart
< startMax
){
546 GridOffset
-= offset
;
547 GridOffset
%= PlotGridX
;
550 GraphStart
+= offset
;
552 if (PlotGridX
&& GridLocked
&& GraphStart
< startMax
){
554 GridOffset
%= PlotGridX
;
560 GridOffset
+= PlotGridX
;
563 if (GraphStart
> startMax
) {
564 GridOffset
+= (GraphStart
- startMax
);
565 GridOffset
%= PlotGridX
;
570 if(GraphPixelsPerPoint
< 20) {
571 if (PlotGridX
&& GridLocked
&& GraphStart
> 0){
572 GridOffset
+= offset
;
573 GridOffset
%= PlotGridX
;
576 GraphStart
-= offset
;
578 if (PlotGridX
&& GridLocked
&& GraphStart
> 0){
580 GridOffset
%= PlotGridX
;
587 GridOffset
+= GraphStart
;
589 GridOffset
+= PlotGridX
;
590 GridOffset
%= PlotGridX
;
595 if(PlotGridX
|| PlotGridY
) {
599 PlotGridX
= PlotGridXdefault
;
600 PlotGridY
= PlotGridYdefault
;
605 puts("Plot Window Keystrokes:\n");
606 puts(" Key Action\n");
607 puts(" DOWN Zoom in");
608 puts(" G Toggle grid display");
609 puts(" H Show help");
610 puts(" L Toggle lock grid relative to samples");
611 puts(" LEFT Move left");
612 puts(" <CTL>LEFT Move left 1 sample");
613 puts(" <SHIFT>LEFT Page left");
614 puts(" LEFT-MOUSE-CLICK Set yellow cursor");
615 puts(" Q Hide window");
616 puts(" RIGHT Move right");
617 puts(" <CTL>RIGHT Move right 1 sample");
618 puts(" <SHIFT>RIGHT Page right");
619 puts(" RIGHT-MOUSE-CLICK Set purple cursor");
620 puts(" UP Zoom out");
622 puts("Use client window 'data help' for more plot commands\n");
626 GridLocked
= !GridLocked
;
634 QWidget::keyPressEvent(event
);