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 //-----------------------------------------------------------------------------
10 #include "proxguiqt.h"
14 #include <QPainterPath>
18 #include <QCloseEvent>
19 #include <QMouseEvent>
25 #include <QHBoxLayout>
31 bool g_useOverlays
= false;
37 void ProxGuiQT::ShowGraphWindow(void)
39 emit
ShowGraphWindowSignal();
42 void ProxGuiQT::RepaintGraphWindow(void)
44 emit
RepaintGraphWindowSignal();
47 void ProxGuiQT::HideGraphWindow(void)
49 emit
HideGraphWindowSignal();
52 void ProxGuiQT::Exit(void)
57 void ProxGuiQT::_ShowGraphWindow(void)
63 plotwidget
= new ProxWidget();
68 void ProxGuiQT::_RepaintGraphWindow(void)
70 if (!plotapp
|| !plotwidget
)
76 void ProxGuiQT::_HideGraphWindow(void)
78 if (!plotapp
|| !plotwidget
)
84 void ProxGuiQT::_Exit(void) {
88 void ProxGuiQT::MainLoop()
90 plotapp
= new QApplication(argc
, argv
);
92 connect(this, SIGNAL(ShowGraphWindowSignal()), this, SLOT(_ShowGraphWindow()));
93 connect(this, SIGNAL(RepaintGraphWindowSignal()), this, SLOT(_RepaintGraphWindow()));
94 connect(this, SIGNAL(HideGraphWindowSignal()), this, SLOT(_HideGraphWindow()));
95 connect(this, SIGNAL(ExitSignal()), this, SLOT(_Exit()));
100 ProxGuiQT::ProxGuiQT(int argc
, char **argv
) : plotapp(NULL
), plotwidget(NULL
),
101 argc(argc
), argv(argv
)
105 ProxGuiQT::~ProxGuiQT(void)
108 //plotwidget->destroy(true,true);
109 // delete plotwidget;
110 // plotwidget = NULL;
119 //--------------------
120 void ProxWidget::applyOperation()
122 //printf("ApplyOperation()");
123 save_restoreGB(GRAPH_SAVE
);
124 memcpy(GraphBuffer
, s_Buff
, sizeof(int) * GraphTraceLen
);
125 RepaintGraphWindow();
127 void ProxWidget::stickOperation()
129 save_restoreGB(GRAPH_RESTORE
);
130 //printf("stickOperation()");
132 void ProxWidget::vchange_autocorr(int v
)
135 ans
= AutoCorrelate(GraphBuffer
, s_Buff
, GraphTraceLen
, v
, true, false);
136 if (g_debugMode
) printf("vchange_autocorr(w:%d): %d\n", v
, ans
);
137 g_useOverlays
= true;
138 RepaintGraphWindow();
140 void ProxWidget::vchange_askedge(int v
)
143 //extern int AskEdgeDetect(const int *in, int *out, int len, int threshold);
144 ans
= AskEdgeDetect(GraphBuffer
, s_Buff
, GraphTraceLen
, v
);
145 if (g_debugMode
) printf("vchange_askedge(w:%d)%d\n", v
, ans
);
146 g_useOverlays
= true;
147 RepaintGraphWindow();
149 void ProxWidget::vchange_dthr_up(int v
)
151 int down
= opsController
->horizontalSlider_dirthr_down
->value();
152 directionalThreshold(GraphBuffer
, s_Buff
, GraphTraceLen
, v
, down
);
153 //printf("vchange_dthr_up(%d)", v);
154 g_useOverlays
= true;
155 RepaintGraphWindow();
157 void ProxWidget::vchange_dthr_down(int v
)
159 //printf("vchange_dthr_down(%d)", v);
160 int up
= opsController
->horizontalSlider_dirthr_up
->value();
161 directionalThreshold(GraphBuffer
,s_Buff
, GraphTraceLen
, v
, up
);
162 g_useOverlays
= true;
163 RepaintGraphWindow();
165 ProxWidget::ProxWidget(QWidget
*parent
, ProxGuiQT
*master
) : QWidget(parent
)
167 this->master
= master
;
170 /** Setup the controller widget **/
172 controlWidget
= new QWidget();
173 opsController
= new Ui::Form();
174 opsController
->setupUi(controlWidget
);
175 //Due to quirks in QT Designer, we need to fiddle a bit
176 opsController
->horizontalSlider_dirthr_down
->setMinimum(-128);
177 opsController
->horizontalSlider_dirthr_down
->setMaximum(0);
178 opsController
->horizontalSlider_dirthr_down
->setValue(-20);
179 opsController
->horizontalSlider_dirthr_up
->setMinimum(-40);
180 opsController
->horizontalSlider_dirthr_up
->setMaximum(128);
181 opsController
->horizontalSlider_dirthr_up
->setValue(20);
182 opsController
->horizontalSlider_askedge
->setValue(25);
183 opsController
->horizontalSlider_window
->setValue(4000);
186 QObject::connect(opsController
->pushButton_apply
, SIGNAL(clicked()), this, SLOT(applyOperation()));
187 QObject::connect(opsController
->pushButton_sticky
, SIGNAL(clicked()), this, SLOT(stickOperation()));
188 QObject::connect(opsController
->horizontalSlider_window
, SIGNAL(valueChanged(int)), this, SLOT(vchange_autocorr(int)));
189 QObject::connect(opsController
->horizontalSlider_dirthr_up
, SIGNAL(valueChanged(int)), this, SLOT(vchange_dthr_up(int)));
190 QObject::connect(opsController
->horizontalSlider_dirthr_down
, SIGNAL(valueChanged(int)), this, SLOT(vchange_dthr_down(int)));
191 QObject::connect(opsController
->horizontalSlider_askedge
, SIGNAL(valueChanged(int)), this, SLOT(vchange_askedge(int)));
193 controlWidget
->show();
195 // Set up the plot widget, which does the actual plotting
197 plot
= new Plot(this);
199 QSlider* slider = new QSlider(Qt::Horizontal);
200 slider->setFocusPolicy(Qt::StrongFocus);
201 slider->setTickPosition(QSlider::TicksBothSides);
202 slider->setTickInterval(10);
203 slider->setSingleStep(1);
205 QVBoxLayout
*layout
= new QVBoxLayout
;
206 //layout->addWidget(slider);
207 layout
->addWidget(plot
);
209 //printf("Proxwidget Constructor just set layout\r\n");
212 // not 100% sure what i need in this block
213 // feel free to fix - marshmellow...
214 ProxWidget::~ProxWidget(void)
217 controlWidget
->close();
218 delete controlWidget
;
219 controlWidget
= NULL
;
223 delete opsController
;
224 opsController
= NULL
;
233 void ProxWidget::closeEvent(QCloseEvent
*event
)
237 g_useOverlays
= false;
239 void ProxWidget::hideEvent(QHideEvent
*event
) {
240 controlWidget
->hide();
243 void ProxWidget::showEvent(QShowEvent
*event
) {
244 controlWidget
->show();
248 //----------- Plotting
250 int Plot::xCoordOf(int i
, QRect r
)
252 return r
.left() + (int)((i
- GraphStart
)*GraphPixelsPerPoint
);
255 int Plot::yCoordOf(int v
, QRect r
, int maxVal
)
257 int z
= (r
.bottom() - r
.top())/2;
258 return -(z
* v
) / maxVal
+ z
;
261 int Plot::valueOf_yCoord(int y
, QRect r
, int maxVal
)
263 int z
= (r
.bottom() - r
.top())/2;
264 return (y
-z
) * maxVal
/ z
;
266 static const QColor GREEN
= QColor(100,255,100);
267 static const QColor RED
= QColor(255,100,100);
268 static const QColor BLUE
= QColor(100,100,255);
269 static const QColor GRAY
= QColor(240,240,240);
271 QColor
Plot::getColor(int graphNum
)
274 case 0: return GREEN
; //Green
275 case 1: return RED
; //Red
276 case 2: return BLUE
; //Blue
277 default: return GRAY
; //Gray
281 void Plot::setMaxAndStart(int *buffer
, int len
, QRect plotRect
)
283 if (len
== 0) return;
284 startMax
= (len
- (int)((plotRect
.right() - plotRect
.left() - 40) / GraphPixelsPerPoint
));
288 if(GraphStart
> startMax
) {
289 GraphStart
= startMax
;
291 if (GraphStart
> len
) return;
292 int vMin
= INT_MAX
, vMax
= INT_MIN
, v
= 0;
293 int sample_index
= GraphStart
;
294 for( ; sample_index
< len
&& xCoordOf(sample_index
,plotRect
) < plotRect
.right() ; sample_index
++) {
296 v
= buffer
[sample_index
];
297 if(v
< vMin
) vMin
= v
;
298 if(v
> vMax
) vMax
= v
;
302 if(fabs( (double) vMin
) > g_absVMax
) g_absVMax
= (int)fabs( (double) vMin
);
303 if(fabs( (double) vMax
) > g_absVMax
) g_absVMax
= (int)fabs( (double) vMax
);
304 g_absVMax
= (int)(g_absVMax
*1.25 + 1);
307 void Plot::PlotDemod(uint8_t *buffer
, size_t len
, QRect plotRect
, QRect annotationRect
, QPainter
*painter
, int graphNum
, int plotOffset
)
309 if (len
== 0 || PlotGridX
<= 0) return;
310 //clock_t begin = clock();
311 QPainterPath penPath
;
313 int grid_delta_x
= PlotGridX
;
314 int first_delta_x
= grid_delta_x
; //(plotOffset > 0) ? PlotGridX : (PlotGridX +);
315 if (GraphStart
> plotOffset
) first_delta_x
-= (GraphStart
-plotOffset
);
316 int DemodStart
= GraphStart
;
317 if (plotOffset
> GraphStart
) DemodStart
= plotOffset
;
321 if (DemodStart
-plotOffset
> 0) BitStart
= (int)(((DemodStart
-plotOffset
)+(PlotGridX
-1))/PlotGridX
)-1;
322 first_delta_x
+= BitStart
* PlotGridX
;
323 if (BitStart
> (int)len
) return;
326 //printf("first_delta_x %i, grid_delta_x %i, DemodStart %i, BitStart %i\n",first_delta_x,grid_delta_x,DemodStart, BitStart);
328 painter
->setPen(getColor(graphNum
));
330 int absVMax
= (int)(100*1.05+1);
331 int x
= xCoordOf(DemodStart
, plotRect
);
332 int y
= yCoordOf((buffer
[BitStart
]*200-100)*-1,plotRect
,absVMax
);
333 penPath
.moveTo(x
, y
);
335 int clk
= first_delta_x
;
336 for(int i
= BitStart
; i
< (int)len
&& xCoordOf(delta_x
+DemodStart
, plotRect
) < plotRect
.right(); i
++) {
337 for (int ii
= 0; ii
< (clk
) && i
< (int)len
&& xCoordOf(DemodStart
+delta_x
+ii
, plotRect
) < plotRect
.right() ; ii
++ ) {
338 x
= xCoordOf(DemodStart
+delta_x
+ii
, plotRect
);
339 v
= buffer
[i
]*200-100;
341 y
= yCoordOf( v
, plotRect
, absVMax
);
343 penPath
.lineTo(x
, y
);
345 if(GraphPixelsPerPoint
> 10) {
346 QRect
f(QPoint(x
- 3, y
- 3),QPoint(x
+ 3, y
+ 3));
347 painter
->fillRect(f
, QColor(100, 255, 100));
349 if (ii
== (int)clk
/2) {
351 sprintf(str
, "%u",buffer
[i
]);
352 painter
->drawText(x
-8, y
+ ((buffer
[i
] > 0) ? 18 : -6), str
);
360 painter
->drawPath(penPath
);
363 void Plot::PlotGraph(int *buffer
, int len
, QRect plotRect
, QRect annotationRect
, QPainter
*painter
, int graphNum
)
365 if (len
== 0) return;
366 //clock_t begin = clock();
367 QPainterPath penPath
;
368 int vMin
= INT_MAX
, vMax
= INT_MIN
, vMean
= 0, v
= 0, i
= 0;
369 int x
= xCoordOf(GraphStart
, plotRect
);
370 int y
= yCoordOf(buffer
[GraphStart
],plotRect
,g_absVMax
);
371 penPath
.moveTo(x
, y
);
372 for(i
= GraphStart
; i
< len
&& xCoordOf(i
, plotRect
) < plotRect
.right(); i
++) {
374 x
= xCoordOf(i
, plotRect
);
377 y
= yCoordOf( v
, plotRect
, g_absVMax
);
379 penPath
.lineTo(x
, y
);
381 if(GraphPixelsPerPoint
> 10) {
382 QRect
f(QPoint(x
- 3, y
- 3),QPoint(x
+ 3, y
+ 3));
383 painter
->fillRect(f
, QColor(100, 255, 100));
386 if(v
< vMin
) vMin
= v
;
387 if(v
> vMax
) vMax
= v
;
390 vMean
/= (i
- GraphStart
);
392 painter
->setPen(getColor(graphNum
));
395 int xo
= 5+(graphNum
*40);
396 painter
->drawLine(xo
, plotRect
.top(),xo
, plotRect
.bottom());
398 int vMarkers
= (g_absVMax
- (g_absVMax
% 10)) / 5;
399 int minYDist
= 40; //Minimum pixel-distance between markers
406 for(v
= vMarkers
; yCoordOf(v
,plotRect
,g_absVMax
) > plotRect
.top() && n
< 20; v
+= vMarkers
,n
++)
408 int y0
= yCoordOf(v
,plotRect
,g_absVMax
);
409 int y1
= yCoordOf(-v
,plotRect
,g_absVMax
);
411 if(lasty0
- y0
< minYDist
) continue;
413 painter
->drawLine(xo
-5,y0
, xo
+5, y0
);
415 sprintf(yLbl
, "%d", v
);
416 painter
->drawText(xo
+8,y0
+7,yLbl
);
418 painter
->drawLine(xo
-5, y1
, xo
+5, y1
);
419 sprintf(yLbl
, "%d",-v
);
420 painter
->drawText(xo
+8, y1
+5 , yLbl
);
425 painter
->drawPath(penPath
);
427 sprintf(str
, "max=%d min=%d mean=%d n=%d/%d CursorAVal=[%d] CursorBVal=[%d]",
428 vMax
, vMin
, vMean
, i
, len
, buffer
[CursorAPos
], buffer
[CursorBPos
]);
429 painter
->drawText(20, annotationRect
.bottom() - 23 - 20 * graphNum
, str
);
431 //clock_t end = clock();
432 //double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
433 //printf("Plot time %f\n", elapsed_secs);
436 void Plot::plotGridLines(QPainter
* painter
,QRect r
)
439 if (PlotGridX
<= 0) return;
440 int offset
= GridOffset
;
441 if (GridLocked
&& PlotGridX
) {
442 offset
= GridOffset
+ PlotGridX
- (GraphStart
% PlotGridX
);
443 } else if (!GridLocked
&& GraphStart
> 0 && PlotGridX
) {
444 offset
= PlotGridX
-((GraphStart
- offset
) % PlotGridX
) + GraphStart
- unlockStart
;
447 if (offset
< 0) offset
+= PlotGridX
;
450 int grid_delta_x
= (int) (PlotGridX
* GraphPixelsPerPoint
);
451 int grid_delta_y
= PlotGridY
;
452 if ((PlotGridX
> 0) && ((PlotGridX
* GraphPixelsPerPoint
) > 1)) {
453 for(i
= (offset
* GraphPixelsPerPoint
); i
< r
.right(); i
+= grid_delta_x
) {
454 painter
->drawLine(r
.left()+i
, r
.top(), r
.left()+i
, r
.bottom());
458 for(i
= 0; yCoordOf(i
,r
,g_absVMax
) > r
.top(); i
+= grid_delta_y
) {
459 painter
->drawLine( r
.left(), yCoordOf(i
,r
,g_absVMax
), r
.right(), yCoordOf(i
,r
,g_absVMax
) );
460 painter
->drawLine( r
.left(), yCoordOf(i
*-1,r
,g_absVMax
), r
.right(), yCoordOf(i
*-1,r
,g_absVMax
) );
465 #define HEIGHT_INFO 60
466 #define WIDTH_AXES 80
468 void Plot::paintEvent(QPaintEvent
*event
)
471 QPainter
painter(this);
472 QBrush
brush(QColor(100, 255, 100));
473 QPen
pen(QColor(100, 255, 100));
475 painter
.setFont(QFont("Courier New", 10));
481 if (CursorAPos
> GraphTraceLen
)
483 if(CursorBPos
> GraphTraceLen
)
485 if(CursorCPos
> GraphTraceLen
)
487 if(CursorDPos
> GraphTraceLen
)
490 QRect
plotRect(WIDTH_AXES
, 0, width()-WIDTH_AXES
, height()-HEIGHT_INFO
);
491 QRect
infoRect(0, height()-HEIGHT_INFO
, width(), HEIGHT_INFO
);
494 painter
.fillRect(rect(), QColor(60, 60, 60));
496 painter
.fillRect(plotRect
, QColor(0, 0, 0));
498 //init graph variables
499 setMaxAndStart(GraphBuffer
,GraphTraceLen
,plotRect
);
502 int zeroHeight
= plotRect
.top() + (plotRect
.bottom() - plotRect
.top()) / 2;
503 painter
.setPen(QColor(100, 100, 100));
504 painter
.drawLine(plotRect
.left(), zeroHeight
, plotRect
.right(), zeroHeight
);
505 // plot X and Y grid lines
506 plotGridLines(&painter
, plotRect
);
508 //Start painting graph
509 PlotGraph(GraphBuffer
, GraphTraceLen
,plotRect
,infoRect
,&painter
,0);
510 if (showDemod
&& DemodBufferLen
> 8) {
511 PlotDemod(DemodBuffer
, DemodBufferLen
,plotRect
,infoRect
,&painter
,2,g_DemodStartIdx
);
514 //init graph variables
515 setMaxAndStart(s_Buff
,GraphTraceLen
,plotRect
);
516 PlotGraph(s_Buff
, GraphTraceLen
,plotRect
,infoRect
,&painter
,1);
521 if(CursorAPos
> GraphStart
&& xCoordOf(CursorAPos
, plotRect
) < plotRect
.right())
523 painter
.setPen(QColor(255, 255, 0));
524 painter
.drawLine(xCoordOf(CursorAPos
, plotRect
),plotRect
.top(),xCoordOf(CursorAPos
, plotRect
),plotRect
.bottom());
526 if(CursorBPos
> GraphStart
&& xCoordOf(CursorBPos
, plotRect
) < plotRect
.right())
528 painter
.setPen(QColor(255, 0, 255));
529 painter
.drawLine(xCoordOf(CursorBPos
, plotRect
),plotRect
.top(),xCoordOf(CursorBPos
, plotRect
),plotRect
.bottom());
531 if(CursorCPos
> GraphStart
&& xCoordOf(CursorCPos
, plotRect
) < plotRect
.right())
533 painter
.setPen(QColor(255, 153, 0)); //orange
534 painter
.drawLine(xCoordOf(CursorCPos
, plotRect
),plotRect
.top(),xCoordOf(CursorCPos
, plotRect
),plotRect
.bottom());
536 if(CursorDPos
> GraphStart
&& xCoordOf(CursorDPos
, plotRect
) < plotRect
.right())
538 painter
.setPen(QColor(0, 0, 205)); //light blue
539 painter
.drawLine(xCoordOf(CursorDPos
, plotRect
),plotRect
.top(),xCoordOf(CursorDPos
, plotRect
),plotRect
.bottom());
544 sprintf(str
, "@%d dt=%d [%2.2f] zoom=%2.2f CursorAPos=%d CursorBPos=%d GridX=%d GridY=%d (%s) GridXoffset=%d",
545 GraphStart
, CursorBPos
- CursorAPos
, (CursorBPos
- CursorAPos
)/CursorScaleFactor
,
546 GraphPixelsPerPoint
,CursorAPos
,CursorBPos
,PlotGridXdefault
,PlotGridYdefault
,GridLocked
?"Locked":"Unlocked",GridOffset
);
547 painter
.setPen(QColor(255, 255, 255));
548 painter
.drawText(20, infoRect
.bottom() - 3, str
);
552 Plot::Plot(QWidget
*parent
) : QWidget(parent
), GraphStart(0), GraphPixelsPerPoint(1)
554 //Need to set this, otherwise we don't receive keypress events
555 setFocusPolicy( Qt::StrongFocus
);
558 QPalette
palette(QColor(0,0,0,0));
559 palette
.setColor(QPalette::WindowText
, QColor(255,255,255));
560 palette
.setColor(QPalette::Text
, QColor(255,255,255));
561 palette
.setColor(QPalette::Button
, QColor(100, 100, 100));
563 setAutoFillBackground(true);
567 setWindowTitle(tr("Sliders"));
570 void Plot::closeEvent(QCloseEvent
*event
)
574 g_useOverlays
= false;
577 void Plot::mouseMoveEvent(QMouseEvent
*event
)
581 x
= (int)(x
/ GraphPixelsPerPoint
);
583 if((event
->buttons() & Qt::LeftButton
)) {
585 } else if (event
->buttons() & Qt::RightButton
) {
593 void Plot::keyPressEvent(QKeyEvent
*event
)
597 if(event
->modifiers() & Qt::ShiftModifier
) {
599 offset
= PageWidth
- (PageWidth
% PlotGridX
);
603 if(event
->modifiers() & Qt::ControlModifier
)
606 offset
= (int)(20 / GraphPixelsPerPoint
);
608 switch(event
->key()) {
610 if(GraphPixelsPerPoint
<= 50) {
611 GraphPixelsPerPoint
*= 2;
616 if(GraphPixelsPerPoint
>= 0.02) {
617 GraphPixelsPerPoint
/= 2;
622 if(GraphPixelsPerPoint
< 20) {
623 GraphStart
+= offset
;
630 if(GraphPixelsPerPoint
< 20) {
631 GraphStart
-= offset
;
638 if(PlotGridX
|| PlotGridY
) {
642 PlotGridX
= PlotGridXdefault
;
643 PlotGridY
= PlotGridYdefault
;
648 puts("Plot Window Keystrokes:\n");
649 puts(" Key Action\n");
650 puts(" DOWN Zoom in");
651 puts(" G Toggle grid display");
652 puts(" H Show help");
653 puts(" L Toggle lock grid relative to samples");
654 puts(" LEFT Move left");
655 puts(" <CTL>LEFT Move left 1 sample");
656 puts(" <SHIFT>LEFT Page left");
657 puts(" LEFT-MOUSE-CLICK Set yellow cursor");
658 puts(" Q Hide window");
659 puts(" RIGHT Move right");
660 puts(" <CTL>RIGHT Move right 1 sample");
661 puts(" <SHIFT>RIGHT Page right");
662 puts(" RIGHT-MOUSE-CLICK Set purple cursor");
663 puts(" UP Zoom out");
665 puts("Use client window 'data help' for more plot commands\n");
669 GridLocked
= !GridLocked
;
671 GridOffset
+= (GraphStart
- unlockStart
);
673 unlockStart
= GraphStart
;
681 QWidget::keyPressEvent(event
);