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) {
87 void ProxGuiQT::MainLoop()
89 plotapp
= new QApplication(argc
, argv
);
91 connect(this, SIGNAL(ShowGraphWindowSignal()), this, SLOT(_ShowGraphWindow()));
92 connect(this, SIGNAL(RepaintGraphWindowSignal()), this, SLOT(_RepaintGraphWindow()));
93 connect(this, SIGNAL(HideGraphWindowSignal()), this, SLOT(_HideGraphWindow()));
94 connect(this, SIGNAL(ExitSignal()), this, SLOT(_Exit()));
99 ProxGuiQT::ProxGuiQT(int argc
, char **argv
) : plotapp(NULL
), plotwidget(NULL
),
100 argc(argc
), argv(argv
)
104 ProxGuiQT::~ProxGuiQT(void)
107 //plotwidget->destroy(true,true);
108 // delete plotwidget;
109 // plotwidget = NULL;
118 //--------------------
119 void ProxWidget::applyOperation()
121 //printf("ApplyOperation()");
122 save_restoreGB(GRAPH_SAVE
);
123 memcpy(GraphBuffer
, s_Buff
, sizeof(int) * GraphTraceLen
);
124 RepaintGraphWindow();
126 void ProxWidget::stickOperation()
128 save_restoreGB(GRAPH_RESTORE
);
129 //printf("stickOperation()");
131 void ProxWidget::vchange_autocorr(int v
)
134 ans
= AutoCorrelate(GraphBuffer
, s_Buff
, GraphTraceLen
, v
, true, false);
135 if (g_debugMode
) printf("vchange_autocorr(w:%d): %d\n", v
, ans
);
136 g_useOverlays
= true;
137 RepaintGraphWindow();
139 void ProxWidget::vchange_askedge(int v
)
142 //extern int AskEdgeDetect(const int *in, int *out, int len, int threshold);
143 ans
= AskEdgeDetect(GraphBuffer
, s_Buff
, GraphTraceLen
, v
);
144 if (g_debugMode
) printf("vchange_askedge(w:%d)%d\n", v
, ans
);
145 g_useOverlays
= true;
146 RepaintGraphWindow();
148 void ProxWidget::vchange_dthr_up(int v
)
150 int down
= opsController
->horizontalSlider_dirthr_down
->value();
151 directionalThreshold(GraphBuffer
, s_Buff
, GraphTraceLen
, v
, down
);
152 //printf("vchange_dthr_up(%d)", v);
153 g_useOverlays
= true;
154 RepaintGraphWindow();
156 void ProxWidget::vchange_dthr_down(int v
)
158 //printf("vchange_dthr_down(%d)", v);
159 int up
= opsController
->horizontalSlider_dirthr_up
->value();
160 directionalThreshold(GraphBuffer
,s_Buff
, GraphTraceLen
, v
, up
);
161 g_useOverlays
= true;
162 RepaintGraphWindow();
164 ProxWidget::ProxWidget(QWidget
*parent
, ProxGuiQT
*master
) : QWidget(parent
)
166 this->master
= master
;
169 /** Setup the controller widget **/
171 controlWidget
= new QWidget();
172 opsController
= new Ui::Form();
173 opsController
->setupUi(controlWidget
);
174 //Due to quirks in QT Designer, we need to fiddle a bit
175 opsController
->horizontalSlider_dirthr_down
->setMinimum(-128);
176 opsController
->horizontalSlider_dirthr_down
->setMaximum(0);
177 opsController
->horizontalSlider_dirthr_down
->setValue(-20);
178 opsController
->horizontalSlider_dirthr_up
->setMinimum(-40);
179 opsController
->horizontalSlider_dirthr_up
->setMaximum(128);
180 opsController
->horizontalSlider_dirthr_up
->setValue(20);
181 opsController
->horizontalSlider_askedge
->setValue(25);
182 opsController
->horizontalSlider_window
->setValue(4000);
185 QObject::connect(opsController
->pushButton_apply
, SIGNAL(clicked()), this, SLOT(applyOperation()));
186 QObject::connect(opsController
->pushButton_sticky
, SIGNAL(clicked()), this, SLOT(stickOperation()));
187 QObject::connect(opsController
->horizontalSlider_window
, SIGNAL(valueChanged(int)), this, SLOT(vchange_autocorr(int)));
188 QObject::connect(opsController
->horizontalSlider_dirthr_up
, SIGNAL(valueChanged(int)), this, SLOT(vchange_dthr_up(int)));
189 QObject::connect(opsController
->horizontalSlider_dirthr_down
, SIGNAL(valueChanged(int)), this, SLOT(vchange_dthr_down(int)));
190 QObject::connect(opsController
->horizontalSlider_askedge
, SIGNAL(valueChanged(int)), this, SLOT(vchange_askedge(int)));
192 controlWidget
->show();
194 // Set up the plot widget, which does the actual plotting
196 plot
= new Plot(this);
198 QSlider* slider = new QSlider(Qt::Horizontal);
199 slider->setFocusPolicy(Qt::StrongFocus);
200 slider->setTickPosition(QSlider::TicksBothSides);
201 slider->setTickInterval(10);
202 slider->setSingleStep(1);
204 QVBoxLayout
*layout
= new QVBoxLayout
;
205 //layout->addWidget(slider);
206 layout
->addWidget(plot
);
208 //printf("Proxwidget Constructor just set layout\r\n");
211 // not 100% sure what i need in this block
212 // feel free to fix - marshmellow...
213 ProxWidget::~ProxWidget(void)
216 controlWidget
->close();
217 delete controlWidget
;
218 controlWidget
= NULL
;
222 delete opsController
;
223 opsController
= NULL
;
232 void ProxWidget::closeEvent(QCloseEvent
*event
)
236 g_useOverlays
= false;
238 void ProxWidget::hideEvent(QHideEvent
*event
) {
239 controlWidget
->hide();
242 void ProxWidget::showEvent(QShowEvent
*event
) {
243 controlWidget
->show();
247 //----------- Plotting
249 int Plot::xCoordOf(int i
, QRect r
)
251 return r
.left() + (int)((i
- GraphStart
)*GraphPixelsPerPoint
);
254 int Plot::yCoordOf(int v
, QRect r
, int maxVal
)
256 int z
= (r
.bottom() - r
.top())/2;
257 return -(z
* v
) / maxVal
+ z
;
260 int Plot::valueOf_yCoord(int y
, QRect r
, int maxVal
)
262 int z
= (r
.bottom() - r
.top())/2;
263 return (y
-z
) * maxVal
/ z
;
265 static const QColor GREEN
= QColor(100,255,100);
266 static const QColor RED
= QColor(255,100,100);
267 static const QColor BLUE
= QColor(100,100,255);
268 static const QColor GRAY
= QColor(240,240,240);
270 QColor
Plot::getColor(int graphNum
)
273 case 0: return GREEN
; //Green
274 case 1: return RED
; //Red
275 case 2: return BLUE
; //Blue
276 default: return GRAY
; //Gray
280 void Plot::setMaxAndStart(int *buffer
, int len
, QRect plotRect
)
282 if (len
== 0) return;
283 startMax
= (len
- (int)((plotRect
.right() - plotRect
.left() - 40) / GraphPixelsPerPoint
));
287 if(GraphStart
> startMax
) {
288 GraphStart
= startMax
;
290 if (GraphStart
> len
) return;
291 int vMin
= INT_MAX
, vMax
= INT_MIN
, v
= 0;
292 int sample_index
= GraphStart
;
293 for( ; sample_index
< len
&& xCoordOf(sample_index
,plotRect
) < plotRect
.right() ; sample_index
++) {
295 v
= buffer
[sample_index
];
296 if(v
< vMin
) vMin
= v
;
297 if(v
> vMax
) vMax
= v
;
301 if(fabs( (double) vMin
) > g_absVMax
) g_absVMax
= (int)fabs( (double) vMin
);
302 if(fabs( (double) vMax
) > g_absVMax
) g_absVMax
= (int)fabs( (double) vMax
);
303 g_absVMax
= (int)(g_absVMax
*1.25 + 1);
306 void Plot::PlotDemod(uint8_t *buffer
, size_t len
, QRect plotRect
, QRect annotationRect
, QPainter
*painter
, int graphNum
, int plotOffset
)
308 if (len
== 0 || PlotGridX
<= 0) return;
309 //clock_t begin = clock();
310 QPainterPath penPath
;
312 int grid_delta_x
= PlotGridX
;
313 int first_delta_x
= grid_delta_x
; //(plotOffset > 0) ? PlotGridX : (PlotGridX +);
314 if (GraphStart
> plotOffset
) first_delta_x
-= (GraphStart
-plotOffset
);
315 int DemodStart
= GraphStart
;
316 if (plotOffset
> GraphStart
) DemodStart
= plotOffset
;
320 if (DemodStart
-plotOffset
> 0) BitStart
= (int)(((DemodStart
-plotOffset
)+(PlotGridX
-1))/PlotGridX
)-1;
321 first_delta_x
+= BitStart
* PlotGridX
;
322 if (BitStart
> (int)len
) return;
325 //printf("first_delta_x %i, grid_delta_x %i, DemodStart %i, BitStart %i\n",first_delta_x,grid_delta_x,DemodStart, BitStart);
327 painter
->setPen(getColor(graphNum
));
329 int absVMax
= (int)(100*1.05+1);
330 int x
= xCoordOf(DemodStart
, plotRect
);
331 int y
= yCoordOf((buffer
[BitStart
]*200-100)*-1,plotRect
,absVMax
);
332 penPath
.moveTo(x
, y
);
334 int clk
= first_delta_x
;
335 for(int i
= BitStart
; i
< (int)len
&& xCoordOf(delta_x
+DemodStart
, plotRect
) < plotRect
.right(); i
++) {
336 for (int ii
= 0; ii
< (clk
) && i
< (int)len
&& xCoordOf(DemodStart
+delta_x
+ii
, plotRect
) < plotRect
.right() ; ii
++ ) {
337 x
= xCoordOf(DemodStart
+delta_x
+ii
, plotRect
);
338 v
= buffer
[i
]*200-100;
340 y
= yCoordOf( v
, plotRect
, absVMax
);
342 penPath
.lineTo(x
, y
);
344 if(GraphPixelsPerPoint
> 10) {
345 QRect
f(QPoint(x
- 3, y
- 3),QPoint(x
+ 3, y
+ 3));
346 painter
->fillRect(f
, QColor(100, 255, 100));
348 if (ii
== (int)clk
/2) {
350 sprintf(str
, "%u",buffer
[i
]);
351 painter
->drawText(x
-8, y
+ ((buffer
[i
] > 0) ? 18 : -6), str
);
359 painter
->drawPath(penPath
);
362 void Plot::PlotGraph(int *buffer
, int len
, QRect plotRect
, QRect annotationRect
, QPainter
*painter
, int graphNum
)
364 if (len
== 0) return;
365 //clock_t begin = clock();
366 QPainterPath penPath
;
367 int vMin
= INT_MAX
, vMax
= INT_MIN
, vMean
= 0, v
= 0, i
= 0;
368 int x
= xCoordOf(GraphStart
, plotRect
);
369 int y
= yCoordOf(buffer
[GraphStart
],plotRect
,g_absVMax
);
370 penPath
.moveTo(x
, y
);
371 for(i
= GraphStart
; i
< len
&& xCoordOf(i
, plotRect
) < plotRect
.right(); i
++) {
373 x
= xCoordOf(i
, plotRect
);
376 y
= yCoordOf( v
, plotRect
, g_absVMax
);
378 penPath
.lineTo(x
, y
);
380 if(GraphPixelsPerPoint
> 10) {
381 QRect
f(QPoint(x
- 3, y
- 3),QPoint(x
+ 3, y
+ 3));
382 painter
->fillRect(f
, QColor(100, 255, 100));
385 if(v
< vMin
) vMin
= v
;
386 if(v
> vMax
) vMax
= v
;
389 vMean
/= (i
- GraphStart
);
391 painter
->setPen(getColor(graphNum
));
394 int xo
= 5+(graphNum
*40);
395 painter
->drawLine(xo
, plotRect
.top(),xo
, plotRect
.bottom());
397 int vMarkers
= (g_absVMax
- (g_absVMax
% 10)) / 5;
398 int minYDist
= 40; //Minimum pixel-distance between markers
405 for(v
= vMarkers
; yCoordOf(v
,plotRect
,g_absVMax
) > plotRect
.top() && n
< 20; v
+= vMarkers
,n
++)
407 int y0
= yCoordOf(v
,plotRect
,g_absVMax
);
408 int y1
= yCoordOf(-v
,plotRect
,g_absVMax
);
410 if(lasty0
- y0
< minYDist
) continue;
412 painter
->drawLine(xo
-5,y0
, xo
+5, y0
);
414 sprintf(yLbl
, "%d", v
);
415 painter
->drawText(xo
+8,y0
+7,yLbl
);
417 painter
->drawLine(xo
-5, y1
, xo
+5, y1
);
418 sprintf(yLbl
, "%d",-v
);
419 painter
->drawText(xo
+8, y1
+5 , yLbl
);
424 painter
->drawPath(penPath
);
426 sprintf(str
, "max=%d min=%d mean=%d n=%d/%d CursorAVal=[%d] CursorBVal=[%d]",
427 vMax
, vMin
, vMean
, i
, len
, buffer
[CursorAPos
], buffer
[CursorBPos
]);
428 painter
->drawText(20, annotationRect
.bottom() - 23 - 20 * graphNum
, str
);
430 //clock_t end = clock();
431 //double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
432 //printf("Plot time %f\n", elapsed_secs);
435 void Plot::plotGridLines(QPainter
* painter
,QRect r
)
438 if (PlotGridX
<= 0) return;
439 int offset
= GridOffset
;
440 if (GridLocked
&& PlotGridX
) {
441 offset
= GridOffset
+ PlotGridX
- (GraphStart
% PlotGridX
);
442 } else if (!GridLocked
&& GraphStart
> 0 && PlotGridX
) {
443 offset
= PlotGridX
-((GraphStart
- offset
) % PlotGridX
) + GraphStart
- unlockStart
;
446 if (offset
< 0) offset
+= PlotGridX
;
449 int grid_delta_x
= (int) (PlotGridX
* GraphPixelsPerPoint
);
450 int grid_delta_y
= PlotGridY
;
451 if ((PlotGridX
> 0) && ((PlotGridX
* GraphPixelsPerPoint
) > 1)) {
452 for(i
= (offset
* GraphPixelsPerPoint
); i
< r
.right(); i
+= grid_delta_x
) {
453 painter
->drawLine(r
.left()+i
, r
.top(), r
.left()+i
, r
.bottom());
457 for(i
= 0; yCoordOf(i
,r
,g_absVMax
) > r
.top(); i
+= grid_delta_y
) {
458 painter
->drawLine( r
.left(), yCoordOf(i
,r
,g_absVMax
), r
.right(), yCoordOf(i
,r
,g_absVMax
) );
459 painter
->drawLine( r
.left(), yCoordOf(i
*-1,r
,g_absVMax
), r
.right(), yCoordOf(i
*-1,r
,g_absVMax
) );
464 #define HEIGHT_INFO 60
465 #define WIDTH_AXES 80
467 void Plot::paintEvent(QPaintEvent
*event
)
470 QPainter
painter(this);
471 QBrush
brush(QColor(100, 255, 100));
472 QPen
pen(QColor(100, 255, 100));
474 painter
.setFont(QFont("Courier New", 10));
480 if (CursorAPos
> GraphTraceLen
)
482 if(CursorBPos
> GraphTraceLen
)
484 if(CursorCPos
> GraphTraceLen
)
486 if(CursorDPos
> GraphTraceLen
)
489 QRect
plotRect(WIDTH_AXES
, 0, width()-WIDTH_AXES
, height()-HEIGHT_INFO
);
490 QRect
infoRect(0, height()-HEIGHT_INFO
, width(), HEIGHT_INFO
);
493 painter
.fillRect(rect(), QColor(60, 60, 60));
495 painter
.fillRect(plotRect
, QColor(0, 0, 0));
497 //init graph variables
498 setMaxAndStart(GraphBuffer
,GraphTraceLen
,plotRect
);
501 int zeroHeight
= plotRect
.top() + (plotRect
.bottom() - plotRect
.top()) / 2;
502 painter
.setPen(QColor(100, 100, 100));
503 painter
.drawLine(plotRect
.left(), zeroHeight
, plotRect
.right(), zeroHeight
);
504 // plot X and Y grid lines
505 plotGridLines(&painter
, plotRect
);
507 //Start painting graph
508 PlotGraph(GraphBuffer
, GraphTraceLen
,plotRect
,infoRect
,&painter
,0);
509 if (showDemod
&& DemodBufferLen
> 8) {
510 PlotDemod(DemodBuffer
, DemodBufferLen
,plotRect
,infoRect
,&painter
,2,g_DemodStartIdx
);
513 //init graph variables
514 setMaxAndStart(s_Buff
,GraphTraceLen
,plotRect
);
515 PlotGraph(s_Buff
, GraphTraceLen
,plotRect
,infoRect
,&painter
,1);
520 if(CursorAPos
> GraphStart
&& xCoordOf(CursorAPos
, plotRect
) < plotRect
.right())
522 painter
.setPen(QColor(255, 255, 0));
523 painter
.drawLine(xCoordOf(CursorAPos
, plotRect
),plotRect
.top(),xCoordOf(CursorAPos
, plotRect
),plotRect
.bottom());
525 if(CursorBPos
> GraphStart
&& xCoordOf(CursorBPos
, plotRect
) < plotRect
.right())
527 painter
.setPen(QColor(255, 0, 255));
528 painter
.drawLine(xCoordOf(CursorBPos
, plotRect
),plotRect
.top(),xCoordOf(CursorBPos
, plotRect
),plotRect
.bottom());
530 if(CursorCPos
> GraphStart
&& xCoordOf(CursorCPos
, plotRect
) < plotRect
.right())
532 painter
.setPen(QColor(255, 153, 0)); //orange
533 painter
.drawLine(xCoordOf(CursorCPos
, plotRect
),plotRect
.top(),xCoordOf(CursorCPos
, plotRect
),plotRect
.bottom());
535 if(CursorDPos
> GraphStart
&& xCoordOf(CursorDPos
, plotRect
) < plotRect
.right())
537 painter
.setPen(QColor(0, 0, 205)); //light blue
538 painter
.drawLine(xCoordOf(CursorDPos
, plotRect
),plotRect
.top(),xCoordOf(CursorDPos
, plotRect
),plotRect
.bottom());
543 sprintf(str
, "@%d dt=%d [%2.2f] zoom=%2.2f CursorAPos=%d CursorBPos=%d GridX=%d GridY=%d (%s) GridXoffset=%d",
544 GraphStart
, CursorBPos
- CursorAPos
, (CursorBPos
- CursorAPos
)/CursorScaleFactor
,
545 GraphPixelsPerPoint
,CursorAPos
,CursorBPos
,PlotGridXdefault
,PlotGridYdefault
,GridLocked
?"Locked":"Unlocked",GridOffset
);
546 painter
.setPen(QColor(255, 255, 255));
547 painter
.drawText(20, infoRect
.bottom() - 3, str
);
551 Plot::Plot(QWidget
*parent
) : QWidget(parent
), GraphStart(0), GraphPixelsPerPoint(1)
553 //Need to set this, otherwise we don't receive keypress events
554 setFocusPolicy( Qt::StrongFocus
);
557 QPalette
palette(QColor(0,0,0,0));
558 palette
.setColor(QPalette::WindowText
, QColor(255,255,255));
559 palette
.setColor(QPalette::Text
, QColor(255,255,255));
560 palette
.setColor(QPalette::Button
, QColor(100, 100, 100));
562 setAutoFillBackground(true);
566 setWindowTitle(tr("Sliders"));
569 void Plot::closeEvent(QCloseEvent
*event
)
573 g_useOverlays
= false;
576 void Plot::mouseMoveEvent(QMouseEvent
*event
)
580 x
= (int)(x
/ GraphPixelsPerPoint
);
582 if((event
->buttons() & Qt::LeftButton
)) {
584 } else if (event
->buttons() & Qt::RightButton
) {
592 void Plot::keyPressEvent(QKeyEvent
*event
)
596 if(event
->modifiers() & Qt::ShiftModifier
) {
598 offset
= PageWidth
- (PageWidth
% PlotGridX
);
602 if(event
->modifiers() & Qt::ControlModifier
)
605 offset
= (int)(20 / GraphPixelsPerPoint
);
607 switch(event
->key()) {
609 if(GraphPixelsPerPoint
<= 50) {
610 GraphPixelsPerPoint
*= 2;
615 if(GraphPixelsPerPoint
>= 0.02) {
616 GraphPixelsPerPoint
/= 2;
621 if(GraphPixelsPerPoint
< 20) {
622 GraphStart
+= offset
;
629 if(GraphPixelsPerPoint
< 20) {
630 GraphStart
-= offset
;
637 if(PlotGridX
|| PlotGridY
) {
641 PlotGridX
= PlotGridXdefault
;
642 PlotGridY
= PlotGridYdefault
;
647 puts("Plot Window Keystrokes:\n");
648 puts(" Key Action\n");
649 puts(" DOWN Zoom in");
650 puts(" G Toggle grid display");
651 puts(" H Show help");
652 puts(" L Toggle lock grid relative to samples");
653 puts(" LEFT Move left");
654 puts(" <CTL>LEFT Move left 1 sample");
655 puts(" <SHIFT>LEFT Page left");
656 puts(" LEFT-MOUSE-CLICK Set yellow cursor");
657 puts(" Q Hide window");
658 puts(" RIGHT Move right");
659 puts(" <CTL>RIGHT Move right 1 sample");
660 puts(" <SHIFT>RIGHT Page right");
661 puts(" RIGHT-MOUSE-CLICK Set purple cursor");
662 puts(" UP Zoom out");
664 puts("Use client window 'data help' for more plot commands\n");
668 GridLocked
= !GridLocked
;
670 GridOffset
+= (GraphStart
- unlockStart
);
672 unlockStart
= GraphStart
;
680 QWidget::keyPressEvent(event
);