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"
13 #include <QPainterPath>
17 #include <QCloseEvent>
18 #include <QMouseEvent>
24 #include <QHBoxLayout>
34 void ProxGuiQT::ShowGraphWindow(void)
36 emit
ShowGraphWindowSignal();
39 void ProxGuiQT::RepaintGraphWindow(void)
41 emit
RepaintGraphWindowSignal();
44 void ProxGuiQT::HideGraphWindow(void)
46 emit
HideGraphWindowSignal();
49 void ProxGuiQT::Exit(void)
54 void ProxGuiQT::_ShowGraphWindow(void)
60 plotwidget
= new ProxWidget();
65 void ProxGuiQT::_RepaintGraphWindow(void)
67 if (!plotapp
|| !plotwidget
)
73 void ProxGuiQT::_HideGraphWindow(void)
75 if (!plotapp
|| !plotwidget
)
81 void ProxGuiQT::_Exit(void) {
84 void ProxGuiQT::MainLoop()
86 plotapp
= new QApplication(argc
, argv
);
88 connect(this, SIGNAL(ShowGraphWindowSignal()), this, SLOT(_ShowGraphWindow()));
89 connect(this, SIGNAL(RepaintGraphWindowSignal()), this, SLOT(_RepaintGraphWindow()));
90 connect(this, SIGNAL(HideGraphWindowSignal()), this, SLOT(_HideGraphWindow()));
91 connect(this, SIGNAL(ExitSignal()), this, SLOT(_Exit()));
96 ProxGuiQT::ProxGuiQT(int argc
, char **argv
) : plotapp(NULL
), plotwidget(NULL
),
97 argc(argc
), argv(argv
)
101 ProxGuiQT::~ProxGuiQT(void)
104 //plotwidget->destroy(true,true);
105 // delete plotwidget;
106 // plotwidget = NULL;
115 //--------------------
116 void ProxWidget::applyOperation()
118 //printf("ApplyOperation()");
120 memcpy(GraphBuffer
, s_Buff
, sizeof(int) * GraphTraceLen
);
121 RepaintGraphWindow();
123 void ProxWidget::stickOperation()
126 //printf("stickOperation()");
128 void ProxWidget::vchange_autocorr(int v
)
131 ans
= AutoCorrelate(GraphBuffer
, s_Buff
, GraphTraceLen
, v
, true, false);
132 if (g_debugMode
) printf("vchange_autocorr(w:%d): %d\n", v
, ans
);
133 RepaintGraphWindow();
135 void ProxWidget::vchange_askedge(int v
)
138 //extern int AskEdgeDetect(const int *in, int *out, int len, int threshold);
139 ans
= AskEdgeDetect(GraphBuffer
, s_Buff
, GraphTraceLen
, v
);
140 if (g_debugMode
) printf("vchange_askedge(w:%d)%d\n", v
, ans
);
141 RepaintGraphWindow();
143 void ProxWidget::vchange_dthr_up(int v
)
145 int down
= opsController
->horizontalSlider_dirthr_down
->value();
146 directionalThreshold(GraphBuffer
, s_Buff
, GraphTraceLen
, v
, down
);
147 //printf("vchange_dthr_up(%d)", v);
148 RepaintGraphWindow();
151 void ProxWidget::vchange_dthr_down(int v
)
153 //printf("vchange_dthr_down(%d)", v);
154 int up
= opsController
->horizontalSlider_dirthr_up
->value();
155 directionalThreshold(GraphBuffer
,s_Buff
, GraphTraceLen
, v
, up
);
156 RepaintGraphWindow();
159 ProxWidget::ProxWidget(QWidget
*parent
, ProxGuiQT
*master
) : QWidget(parent
)
161 this->master
= master
;
164 /** Setup the controller widget **/
166 controlWidget
= new QWidget();
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);
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)));
180 QObject::connect(opsController
->horizontalSlider_askedge
, SIGNAL(valueChanged(int)), this, SLOT(vchange_askedge(int)));
182 controlWidget
->show();
184 // Set up the plot widget, which does the actual plotting
186 plot
= new Plot(this);
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);
194 QVBoxLayout
*layout
= new QVBoxLayout
;
195 //layout->addWidget(slider);
196 layout
->addWidget(plot
);
198 //printf("Proxwidget Constructor just set layout\r\n");
201 // not 100% sure what i need in this block
202 // feel free to fix - marshmellow...
203 ProxWidget::~ProxWidget(void)
206 controlWidget
->close();
207 delete controlWidget
;
208 controlWidget
= NULL
;
212 delete opsController
;
213 opsController
= NULL
;
222 void ProxWidget::closeEvent(QCloseEvent
*event
)
227 void ProxWidget::hideEvent(QHideEvent
*event
) {
228 controlWidget
->hide();
231 void ProxWidget::showEvent(QShowEvent
*event
) {
232 controlWidget
->show();
236 //----------- Plotting
238 int Plot::xCoordOf(int i
, QRect r
)
240 return r
.left() + (int)((i
- GraphStart
)*GraphPixelsPerPoint
);
243 int Plot::yCoordOf(int v
, QRect r
, int maxVal
)
245 int z
= (r
.bottom() - r
.top())/2;
246 return -(z
* v
) / maxVal
+ z
;
249 int Plot::valueOf_yCoord(int y
, QRect r
, int maxVal
)
251 int z
= (r
.bottom() - r
.top())/2;
252 return (y
-z
) * maxVal
/ z
;
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);
259 QColor
Plot::getColor(int graphNum
)
262 case 0: return GREEN
; //Green
263 case 1: return RED
; //Red
264 case 2: return BLUE
; //Blue
265 default: return GRAY
; //Gray
269 void Plot::PlotDemod(uint8_t *buffer
, size_t len
, QRect plotRect
, QRect annotationRect
, QPainter
*painter
, int graphNum
, int plotOffset
)
271 if (len
== 0 || PlotGridX
<= 0) return;
272 //clock_t begin = clock();
273 QPainterPath penPath
;
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
;
283 if (DemodStart
-plotOffset
> 0) BitStart
= (int)(((DemodStart
-plotOffset
)+(PlotGridX
-1))/PlotGridX
)-1;
284 first_delta_x
+= BitStart
* PlotGridX
;
285 if (BitStart
> (int)len
) return;
288 //printf("first_delta_x %i, grid_delta_x %i, DemodStart %i, BitStart %i\n",first_delta_x,grid_delta_x,DemodStart, BitStart);
290 painter
->setPen(getColor(graphNum
));
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
);
297 int clk
= first_delta_x
;
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
++ ) {
300 x
= xCoordOf(DemodStart
+delta_x
+ii
, plotRect
);
301 v
= buffer
[i
]*200-100;
303 y
= yCoordOf( v
, plotRect
, absVMax
);
305 penPath
.lineTo(x
, y
);
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));
311 if (ii
== (int)clk
/2) {
313 sprintf(str
, "%u",buffer
[i
]);
314 painter
->drawText(x
-8, y
+ ((buffer
[i
] > 0) ? 18 : -6), str
);
322 painter
->drawPath(penPath
);
325 void Plot::PlotGraph(int *buffer
, int len
, QRect plotRect
, QRect annotationRect
, QPainter
*painter
, int graphNum
)
327 if (len
== 0) return;
328 //clock_t begin = clock();
329 QPainterPath penPath
;
331 startMax
= (len
- (int)((plotRect
.right() - plotRect
.left() - 40) / GraphPixelsPerPoint
));
335 if(GraphStart
> startMax
) {
336 GraphStart
= startMax
;
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
++) {
343 v
= buffer
[sample_index
];
344 if(v
< vMin
) vMin
= v
;
345 if(v
> vMax
) vMax
= v
;
349 vMean
/= (sample_index
- GraphStart
);
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);
354 // number of points that will be plotted
355 int span
= (int)((plotRect
.right() - plotRect
.left()) / GraphPixelsPerPoint
);
356 // one label every 100 pixels, let us say
357 int labels
= (plotRect
.right() - plotRect
.left() - 40) / 100;
358 if(labels
<= 0) labels
= 1;
359 int pointsPerLabel
= span
/ labels
;
360 if(pointsPerLabel
<= 0) pointsPerLabel
= 1;
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
++) {
367 x
= xCoordOf(i
, plotRect
);
370 y
= yCoordOf( v
, plotRect
, absVMax
);//(y * (r.top() - r.bottom()) / (2*absYMax)) + zeroHeight;
372 penPath
.lineTo(x
, y
);
374 if(GraphPixelsPerPoint
> 10) {
375 QRect
f(QPoint(x
- 3, y
- 3),QPoint(x
+ 3, y
+ 3));
376 painter
->fillRect(f
, QColor(100, 255, 100));
380 painter
->setPen(getColor(graphNum
));
383 int xo
= 5+(graphNum
*40);
384 painter
->drawLine(xo
, plotRect
.top(),xo
, plotRect
.bottom());
386 int vMarkers
= (absVMax
- (absVMax
% 10)) / 5;
387 int minYDist
= 40; //Minimum pixel-distance between markers
394 for(int v
= vMarkers
; yCoordOf(v
,plotRect
,absVMax
) > plotRect
.top() && n
< 20; v
+= vMarkers
,n
++)
396 int y0
= yCoordOf(v
,plotRect
,absVMax
);
397 int y1
= yCoordOf(-v
,plotRect
,absVMax
);
399 if(lasty0
- y0
< minYDist
) continue;
401 painter
->drawLine(xo
-5,y0
, xo
+5, y0
);
403 sprintf(yLbl
, "%d", v
);
404 painter
->drawText(xo
+8,y0
+7,yLbl
);
406 painter
->drawLine(xo
-5, y1
, xo
+5, y1
);
407 sprintf(yLbl
, "%d",-v
);
408 painter
->drawText(xo
+8, y1
+5 , yLbl
);
413 painter
->drawPath(penPath
);
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
);
419 //clock_t end = clock();
420 //double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
421 //printf("Plot time %f\n", elapsed_secs);
424 void Plot::plotGridLines(QPainter
* painter
,QRect r
)
426 int zeroHeight
= r
.top() + (r
.bottom() - r
.top()) / 2;
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());
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
);
444 #define HEIGHT_INFO 60
445 #define WIDTH_AXES 80
447 void Plot::paintEvent(QPaintEvent
*event
)
449 QPainter
painter(this);
450 QBrush
brush(QColor(100, 255, 100));
451 QPen
pen(QColor(100, 255, 100));
453 painter
.setFont(QFont("Courier New", 10));
459 if (CursorAPos
> GraphTraceLen
)
461 if(CursorBPos
> GraphTraceLen
)
463 if(CursorCPos
> GraphTraceLen
)
465 if(CursorDPos
> GraphTraceLen
)
468 QRect
plotRect(WIDTH_AXES
, 0, width()-WIDTH_AXES
, height()-HEIGHT_INFO
);
469 QRect
infoRect(0, height()-HEIGHT_INFO
, width(), HEIGHT_INFO
);
472 painter
.fillRect(rect(), QColor(60, 60, 60));
474 painter
.fillRect(plotRect
, QColor(0, 0, 0));
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
);
483 //Start painting graph
484 if (showDemod
&& DemodBufferLen
> 8) {
485 PlotDemod(DemodBuffer
, DemodBufferLen
,plotRect
,infoRect
,&painter
,2,g_DemodStartIdx
);
487 PlotGraph(s_Buff
, GraphTraceLen
,plotRect
,infoRect
,&painter
,1);
488 PlotGraph(GraphBuffer
, GraphTraceLen
,plotRect
,infoRect
,&painter
,0);
492 if(CursorAPos
> GraphStart
&& xCoordOf(CursorAPos
, plotRect
) < plotRect
.right())
494 painter
.setPen(QColor(255, 255, 0));
495 painter
.drawLine(xCoordOf(CursorAPos
, plotRect
),plotRect
.top(),xCoordOf(CursorAPos
, plotRect
),plotRect
.bottom());
497 if(CursorBPos
> GraphStart
&& xCoordOf(CursorBPos
, plotRect
) < plotRect
.right())
499 painter
.setPen(QColor(255, 0, 255));
500 painter
.drawLine(xCoordOf(CursorBPos
, plotRect
),plotRect
.top(),xCoordOf(CursorBPos
, plotRect
),plotRect
.bottom());
502 if(CursorCPos
> GraphStart
&& xCoordOf(CursorCPos
, plotRect
) < plotRect
.right())
504 painter
.setPen(QColor(255, 153, 0)); //orange
505 painter
.drawLine(xCoordOf(CursorCPos
, plotRect
),plotRect
.top(),xCoordOf(CursorCPos
, plotRect
),plotRect
.bottom());
507 if(CursorDPos
> GraphStart
&& xCoordOf(CursorDPos
, plotRect
) < plotRect
.right())
509 painter
.setPen(QColor(0, 0, 205)); //light blue
510 painter
.drawLine(xCoordOf(CursorDPos
, plotRect
),plotRect
.top(),xCoordOf(CursorDPos
, plotRect
),plotRect
.bottom());
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
);
518 painter
.setPen(QColor(255, 255, 255));
519 painter
.drawText(20, infoRect
.bottom() - 3, str
);
523 Plot::Plot(QWidget
*parent
) : QWidget(parent
), GraphStart(0), GraphPixelsPerPoint(1)
525 //Need to set this, otherwise we don't receive keypress events
526 setFocusPolicy( Qt::StrongFocus
);
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));
534 setAutoFillBackground(true);
538 setWindowTitle(tr("Sliders"));
541 void Plot::closeEvent(QCloseEvent
*event
)
547 void Plot::mouseMoveEvent(QMouseEvent
*event
)
551 x
= (int)(x
/ GraphPixelsPerPoint
);
553 if((event
->buttons() & Qt::LeftButton
)) {
555 } else if (event
->buttons() & Qt::RightButton
) {
563 void Plot::keyPressEvent(QKeyEvent
*event
)
570 if(event
->modifiers() & Qt::ShiftModifier
) {
572 offset
= PageWidth
- (PageWidth
% PlotGridX
);
576 if(event
->modifiers() & Qt::ControlModifier
)
579 offset
= (int)(20 / GraphPixelsPerPoint
);
581 switch(event
->key()) {
583 if(GraphPixelsPerPoint
<= 50) {
584 GraphPixelsPerPoint
*= 2;
589 if(GraphPixelsPerPoint
>= 0.02) {
590 GraphPixelsPerPoint
/= 2;
595 if(GraphPixelsPerPoint
< 20) {
596 if (PlotGridX
&& GridLocked
&& GraphStart
< startMax
){
597 GridOffset
-= offset
;
598 GridOffset
%= PlotGridX
;
601 GraphStart
+= offset
;
603 if (PlotGridX
&& GridLocked
&& GraphStart
< startMax
){
605 GridOffset
%= PlotGridX
;
611 GridOffset
+= PlotGridX
;
614 if (GraphStart
> startMax
) {
615 GridOffset
+= (GraphStart
- startMax
);
616 GridOffset
%= PlotGridX
;
621 if(GraphPixelsPerPoint
< 20) {
622 if (PlotGridX
&& GridLocked
&& GraphStart
> 0){
623 GridOffset
+= offset
;
624 GridOffset
%= PlotGridX
;
627 GraphStart
-= offset
;
629 if (PlotGridX
&& GridLocked
&& GraphStart
> 0){
631 GridOffset
%= PlotGridX
;
638 GridOffset
+= GraphStart
;
640 GridOffset
+= PlotGridX
;
641 GridOffset
%= PlotGridX
;
646 if(PlotGridX
|| PlotGridY
) {
650 PlotGridX
= PlotGridXdefault
;
651 PlotGridY
= PlotGridYdefault
;
656 puts("Plot Window Keystrokes:\n");
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");
673 puts("Use client window 'data help' for more plot commands\n");
677 GridLocked
= !GridLocked
;
685 QWidget::keyPressEvent(event
);