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>
26 #include "proxguiqt.h"
35 void ProxGuiQT::ShowGraphWindow(void)
37 emit
ShowGraphWindowSignal();
40 void ProxGuiQT::RepaintGraphWindow(void)
42 emit
RepaintGraphWindowSignal();
45 void ProxGuiQT::HideGraphWindow(void)
47 emit
HideGraphWindowSignal();
50 void ProxGuiQT::_ShowGraphWindow(void)
56 plotwidget
= new ProxWidget();
61 void ProxGuiQT::_RepaintGraphWindow(void)
63 if (!plotapp
|| !plotwidget
)
69 void ProxGuiQT::_HideGraphWindow(void)
71 if (!plotapp
|| !plotwidget
)
77 void ProxGuiQT::MainLoop()
79 plotapp
= new QApplication(argc
, argv
);
81 connect(this, SIGNAL(ShowGraphWindowSignal()), this, SLOT(_ShowGraphWindow()));
82 connect(this, SIGNAL(RepaintGraphWindowSignal()), this, SLOT(_RepaintGraphWindow()));
83 connect(this, SIGNAL(HideGraphWindowSignal()), this, SLOT(_HideGraphWindow()));
88 ProxGuiQT::ProxGuiQT(int argc
, char **argv
) : plotapp(NULL
), plotwidget(NULL
),
89 argc(argc
), argv(argv
)
93 ProxGuiQT::~ProxGuiQT(void)
96 //plotwidget->close();
108 //--------------------
109 void ProxWidget::applyOperation()
111 printf("ApplyOperation()");
113 memcpy(GraphBuffer
, s_Buff
, sizeof(int) * GraphTraceLen
);
114 RepaintGraphWindow();
117 void ProxWidget::stickOperation()
120 printf("stickOperation()");
122 void ProxWidget::vchange_autocorr(int v
)
125 ans
= AutoCorrelate(GraphBuffer
, s_Buff
, GraphTraceLen
, v
, true, false);
126 printf("vchange_autocorr(w:%d): %d\n", v
, ans
);
127 RepaintGraphWindow();
129 void ProxWidget::vchange_askedge(int v
)
132 //extern int AskEdgeDetect(const int *in, int *out, int len, int threshold);
133 ans
= AskEdgeDetect(GraphBuffer
, s_Buff
, GraphTraceLen
, v
);
134 printf("vchange_askedge(w:%d)\n", v
);
135 RepaintGraphWindow();
137 void ProxWidget::vchange_dthr_up(int v
)
139 int down
= opsController
->horizontalSlider_dirthr_down
->value();
140 directionalThreshold(GraphBuffer
, s_Buff
, GraphTraceLen
, v
, down
);
141 printf("vchange_dthr_up(%d)", v
);
142 RepaintGraphWindow();
145 void ProxWidget::vchange_dthr_down(int v
)
147 printf("vchange_dthr_down(%d)", v
);
148 int up
= opsController
->horizontalSlider_dirthr_up
->value();
149 directionalThreshold(GraphBuffer
,s_Buff
, GraphTraceLen
, v
, up
);
150 RepaintGraphWindow();
153 ProxWidget::ProxWidget(QWidget
*parent
, ProxGuiQT
*master
) : QWidget(parent
)
155 this->master
= master
;
158 /** Setup the controller widget **/
160 QWidget
* controlWidget
= new QWidget();
161 opsController
= new Ui::Form();
162 opsController
->setupUi(controlWidget
);
163 //Due to quirks in QT Designer, we need to fiddle a bit
164 opsController
->horizontalSlider_dirthr_down
->setMinimum(-128);
165 opsController
->horizontalSlider_dirthr_down
->setMaximum(0);
166 opsController
->horizontalSlider_dirthr_down
->setValue(-20);
169 QObject::connect(opsController
->pushButton_apply
, SIGNAL(clicked()), this, SLOT(applyOperation()));
170 QObject::connect(opsController
->pushButton_sticky
, SIGNAL(clicked()), this, SLOT(stickOperation()));
171 QObject::connect(opsController
->horizontalSlider_window
, SIGNAL(valueChanged(int)), this, SLOT(vchange_autocorr(int)));
172 QObject::connect(opsController
->horizontalSlider_dirthr_up
, SIGNAL(valueChanged(int)), this, SLOT(vchange_dthr_up(int)));
173 QObject::connect(opsController
->horizontalSlider_dirthr_down
, SIGNAL(valueChanged(int)), this, SLOT(vchange_dthr_down(int)));
174 QObject::connect(opsController
->horizontalSlider_askedge
, SIGNAL(valueChanged(int)), this, SLOT(vchange_askedge(int)));
176 controlWidget
->show();
178 // Set up the plot widget, which does the actual plotting
180 plot
= new Plot(this);
182 QSlider* slider = new QSlider(Qt::Horizontal);
183 slider->setFocusPolicy(Qt::StrongFocus);
184 slider->setTickPosition(QSlider::TicksBothSides);
185 slider->setTickInterval(10);
186 slider->setSingleStep(1);
188 QVBoxLayout
*layout
= new QVBoxLayout
;
189 //layout->addWidget(slider);
190 layout
->addWidget(plot
);
192 printf("Proxwidget Constructor just set layout\r\n");
196 //----------- Plotting
198 int Plot::xCoordOf(int i
, QRect r
)
200 return r
.left() + (int)((i
- GraphStart
)*GraphPixelsPerPoint
);
203 int Plot::yCoordOf(int v
, QRect r
, int maxVal
)
205 int z
= (r
.bottom() - r
.top())/2;
206 return -(z
* v
) / maxVal
+ z
;
209 int Plot::valueOf_yCoord(int y
, QRect r
, int maxVal
)
211 int z
= (r
.bottom() - r
.top())/2;
212 return (y
-z
) * maxVal
/ z
;
214 static const QColor GREEN
= QColor(100,255,100);
215 static const QColor RED
= QColor(255,100,100);
216 static const QColor BLUE
= QColor(100,100,255);
217 static const QColor GRAY
= QColor(240,240,240);
219 QColor
Plot::getColor(int graphNum
)
222 case 0: return GREEN
; //Green
223 case 1: return RED
; //Red
224 case 2: return BLUE
; //Blue
225 default: return GRAY
; //Gray
229 void Plot::PlotDemod(uint8_t *buffer
, size_t len
, QRect plotRect
, QRect annotationRect
, QPainter
*painter
, int graphNum
, int plotOffset
)
231 if (len
== 0 || PlotGridX
<= 0) return;
232 //clock_t begin = clock();
233 QPainterPath penPath
;
235 int grid_delta_x
= PlotGridX
;
236 int first_delta_x
= grid_delta_x
; //(plotOffset > 0) ? PlotGridX : (PlotGridX +);
237 if (GraphStart
> plotOffset
) first_delta_x
-= (GraphStart
-plotOffset
);
238 int DemodStart
= GraphStart
;
239 if (plotOffset
> GraphStart
) DemodStart
= plotOffset
;
243 if (DemodStart
-plotOffset
> 0) BitStart
= (int)(((DemodStart
-plotOffset
)+(PlotGridX
-1))/PlotGridX
)-1;
244 first_delta_x
+= BitStart
* PlotGridX
;
245 if (BitStart
> len
) return;
248 //printf("first_delta_x %i, grid_delta_x %i, DemodStart %i, BitStart %i\n",first_delta_x,grid_delta_x,DemodStart, BitStart);
250 painter
->setPen(getColor(graphNum
));
252 int absVMax
= (int)(100*1.05+1);
253 int x
= xCoordOf(DemodStart
, plotRect
);
254 int y
= yCoordOf((buffer
[BitStart
]*200-100)*-1,plotRect
,absVMax
);
255 penPath
.moveTo(x
, y
);
257 int clk
= first_delta_x
;
258 for(int i
= BitStart
; i
< len
&& xCoordOf(delta_x
+DemodStart
, plotRect
) < plotRect
.right(); i
++) {
259 for (int ii
= 0; ii
< (clk
) && i
< len
&& xCoordOf(DemodStart
+delta_x
+ii
, plotRect
) < plotRect
.right() ; ii
++ ) {
260 x
= xCoordOf(DemodStart
+delta_x
+ii
, plotRect
);
261 v
= buffer
[i
]*200-100;
263 y
= yCoordOf( v
, plotRect
, absVMax
);
265 penPath
.lineTo(x
, y
);
267 if(GraphPixelsPerPoint
> 10) {
268 QRect
f(QPoint(x
- 3, y
- 3),QPoint(x
+ 3, y
+ 3));
269 painter
->fillRect(f
, QColor(100, 255, 100));
271 if (ii
== (int)clk
/2) {
273 sprintf(str
, "%u",buffer
[i
]);
274 painter
->drawText(x
-8, y
+ ((buffer
[i
] > 0) ? 18 : -6), str
);
282 painter
->drawPath(penPath
);
285 void Plot::PlotGraph(int *buffer
, int len
, QRect plotRect
, QRect annotationRect
, QPainter
*painter
, int graphNum
)
287 if (len
== 0) return;
288 //clock_t begin = clock();
289 QPainterPath penPath
;
291 startMax
= (len
- (int)((plotRect
.right() - plotRect
.left() - 40) / GraphPixelsPerPoint
));
295 if(GraphStart
> startMax
) {
296 GraphStart
= startMax
;
298 if (GraphStart
> len
) return;
299 int vMin
= INT_MAX
, vMax
= INT_MIN
, vMean
= 0, v
= 0, absVMax
= 0;
300 int sample_index
= GraphStart
;
301 for( ; sample_index
< len
&& xCoordOf(sample_index
,plotRect
) < plotRect
.right() ; sample_index
++) {
303 v
= buffer
[sample_index
];
304 if(v
< vMin
) vMin
= v
;
305 if(v
> vMax
) vMax
= v
;
309 vMean
/= (sample_index
- GraphStart
);
311 if(fabs( (double) vMin
) > absVMax
) absVMax
= (int)fabs( (double) vMin
);
312 if(fabs( (double) vMax
) > absVMax
) absVMax
= (int)fabs( (double) vMax
);
313 absVMax
= (int)(absVMax
*1.25 + 1);
314 // number of points that will be plotted
315 int span
= (int)((plotRect
.right() - plotRect
.left()) / GraphPixelsPerPoint
);
316 // one label every 100 pixels, let us say
317 int labels
= (plotRect
.right() - plotRect
.left() - 40) / 100;
318 if(labels
<= 0) labels
= 1;
319 int pointsPerLabel
= span
/ labels
;
320 if(pointsPerLabel
<= 0) pointsPerLabel
= 1;
322 int x
= xCoordOf(GraphStart
, plotRect
);
323 int y
= yCoordOf(buffer
[GraphStart
],plotRect
,absVMax
);
324 penPath
.moveTo(x
, y
);
325 for(int i
= GraphStart
; i
< len
&& xCoordOf(i
, plotRect
) < plotRect
.right(); i
++) {
327 x
= xCoordOf(i
, plotRect
);
330 y
= yCoordOf( v
, plotRect
, absVMax
);//(y * (r.top() - r.bottom()) / (2*absYMax)) + zeroHeight;
332 penPath
.lineTo(x
, y
);
334 if(GraphPixelsPerPoint
> 10) {
335 QRect
f(QPoint(x
- 3, y
- 3),QPoint(x
+ 3, y
+ 3));
336 painter
->fillRect(f
, QColor(100, 255, 100));
340 painter
->setPen(getColor(graphNum
));
343 int xo
= 5+(graphNum
*40);
344 painter
->drawLine(xo
, plotRect
.top(),xo
, plotRect
.bottom());
346 int vMarkers
= (absVMax
- (absVMax
% 10)) / 5;
347 int minYDist
= 40; //Minimum pixel-distance between markers
354 for(int v
= vMarkers
; yCoordOf(v
,plotRect
,absVMax
) > plotRect
.top() && n
< 20; v
+= vMarkers
,n
++)
356 int y0
= yCoordOf(v
,plotRect
,absVMax
);
357 int y1
= yCoordOf(-v
,plotRect
,absVMax
);
359 if(lasty0
- y0
< minYDist
) continue;
361 painter
->drawLine(xo
-5,y0
, xo
+5, y0
);
363 sprintf(yLbl
, "%d", v
);
364 painter
->drawText(xo
+8,y0
+7,yLbl
);
366 painter
->drawLine(xo
-5, y1
, xo
+5, y1
);
367 sprintf(yLbl
, "%d",-v
);
368 painter
->drawText(xo
+8, y1
+5 , yLbl
);
373 painter
->drawPath(penPath
);
375 sprintf(str
, "max=%d min=%d mean=%d n=%d/%d CursorAVal=[%d] CursorBVal=[%d]",
376 vMax
, vMin
, vMean
, sample_index
, len
, buffer
[CursorAPos
], buffer
[CursorBPos
]);
377 painter
->drawText(20, annotationRect
.bottom() - 23 - 20 * graphNum
, str
);
379 //clock_t end = clock();
380 //double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
381 //printf("Plot time %f\n", elapsed_secs);
384 void Plot::plotGridLines(QPainter
* painter
,QRect r
)
386 int zeroHeight
= r
.top() + (r
.bottom() - r
.top()) / 2;
389 int grid_delta_x
= (int) (PlotGridX
* GraphPixelsPerPoint
);
390 int grid_delta_y
= (int) (PlotGridY
* GraphPixelsPerPoint
);
391 if ((PlotGridX
> 0) && ((PlotGridX
* GraphPixelsPerPoint
) > 1)) {
392 for(i
= (GridOffset
* GraphPixelsPerPoint
); i
< r
.right(); i
+= grid_delta_x
) {
393 painter
->drawLine(r
.left()+i
, r
.top(), r
.left()+i
, r
.bottom());
396 if ((PlotGridY
> 0) && ((PlotGridY
* GraphPixelsPerPoint
) > 1)){
397 for(i
= 0; i
< ((r
.top() + r
.bottom())>>1); i
+= grid_delta_y
) {
398 painter
->drawLine(r
.left(),zeroHeight
+ i
,r
.right(),zeroHeight
+ i
);
399 painter
->drawLine(r
.left(),zeroHeight
- i
,r
.right(),zeroHeight
- i
);
404 #define HEIGHT_INFO 60
405 #define WIDTH_AXES 80
407 void Plot::paintEvent(QPaintEvent
*event
)
409 QPainter
painter(this);
410 //QPainterPath penPath, whitePath, greyPath, lightgreyPath, cursorAPath, cursorBPath, cursorCPath, cursorDPath;
412 QBrush
brush(QColor(100, 255, 100));
413 QPen
pen(QColor(100, 255, 100));
415 painter
.setFont(QFont("Courier New", 10));
421 if (CursorAPos
> GraphTraceLen
)
423 if(CursorBPos
> GraphTraceLen
)
425 if(CursorCPos
> GraphTraceLen
)
427 if(CursorDPos
> GraphTraceLen
)
430 QRect
plotRect(WIDTH_AXES
, 0, width()-WIDTH_AXES
, height()-HEIGHT_INFO
);
431 QRect
infoRect(0, height()-HEIGHT_INFO
, width(), HEIGHT_INFO
);
434 painter
.fillRect(rect(), QColor(60, 60, 60));
436 painter
.fillRect(plotRect
, QColor(0, 0, 0));
439 int zeroHeight
= plotRect
.top() + (plotRect
.bottom() - plotRect
.top()) / 2;
440 painter
.setPen(QColor(100, 100, 100));
441 painter
.drawLine(plotRect
.left(), zeroHeight
, plotRect
.right(), zeroHeight
);
442 // plot X and Y grid lines
443 plotGridLines(&painter
, plotRect
);
445 //Start painting graph
446 if (showDemod
&& DemodBufferLen
> 8) {
447 PlotDemod(DemodBuffer
, DemodBufferLen
,plotRect
,infoRect
,&painter
,2,g_DemodStartIdx
);
449 PlotGraph(s_Buff
, GraphTraceLen
,plotRect
,infoRect
,&painter
,1);
450 PlotGraph(GraphBuffer
, GraphTraceLen
,plotRect
,infoRect
,&painter
,0);
454 if(CursorAPos
> GraphStart
&& xCoordOf(CursorAPos
, plotRect
) < plotRect
.right())
456 painter
.setPen(QColor(255, 255, 0));
457 painter
.drawLine(xCoordOf(CursorAPos
, plotRect
),plotRect
.top(),xCoordOf(CursorAPos
, plotRect
),plotRect
.bottom());
459 if(CursorBPos
> GraphStart
&& xCoordOf(CursorBPos
, plotRect
) < plotRect
.right())
461 painter
.setPen(QColor(255, 0, 255));
462 painter
.drawLine(xCoordOf(CursorBPos
, plotRect
),plotRect
.top(),xCoordOf(CursorBPos
, plotRect
),plotRect
.bottom());
464 if(CursorCPos
> GraphStart
&& xCoordOf(CursorCPos
, plotRect
) < plotRect
.right())
466 painter
.setPen(QColor(255, 153, 0)); //orange
467 painter
.drawLine(xCoordOf(CursorCPos
, plotRect
),plotRect
.top(),xCoordOf(CursorCPos
, plotRect
),plotRect
.bottom());
469 if(CursorDPos
> GraphStart
&& xCoordOf(CursorDPos
, plotRect
) < plotRect
.right())
471 painter
.setPen(QColor(0, 0, 205)); //light blue
472 painter
.drawLine(xCoordOf(CursorDPos
, plotRect
),plotRect
.top(),xCoordOf(CursorDPos
, plotRect
),plotRect
.bottom());
477 sprintf(str
, "@%d dt=%d [%2.2f] zoom=%2.2f CursorAPos=%d CursorBPos=%d GridX=%d GridY=%d (%s) GridXoffset=%d",
478 GraphStart
, CursorBPos
- CursorAPos
, (CursorBPos
- CursorAPos
)/CursorScaleFactor
,
479 GraphPixelsPerPoint
,CursorAPos
,CursorBPos
,PlotGridXdefault
,PlotGridYdefault
,GridLocked
?"Locked":"Unlocked",GridOffset
);
480 painter
.setPen(QColor(255, 255, 255));
481 painter
.drawText(20, infoRect
.bottom() - 3, str
);
485 Plot::Plot(QWidget
*parent
) : QWidget(parent
), GraphStart(0), GraphPixelsPerPoint(1)
487 //Need to set this, otherwise we don't receive keypress events
488 setFocusPolicy( Qt::StrongFocus
);
491 QPalette
palette(QColor(0,0,0,0));
492 palette
.setColor(QPalette::WindowText
, QColor(255,255,255));
493 palette
.setColor(QPalette::Text
, QColor(255,255,255));
494 palette
.setColor(QPalette::Button
, QColor(100, 100, 100));
496 setAutoFillBackground(true);
500 setWindowTitle(tr("Sliders"));
503 void Plot::closeEvent(QCloseEvent
*event
)
509 void Plot::mouseMoveEvent(QMouseEvent
*event
)
513 x
= (int)(x
/ GraphPixelsPerPoint
);
515 if((event
->buttons() & Qt::LeftButton
)) {
517 } else if (event
->buttons() & Qt::RightButton
) {
525 void Plot::keyPressEvent(QKeyEvent
*event
)
532 if(event
->modifiers() & Qt::ShiftModifier
) {
534 offset
= PageWidth
- (PageWidth
% PlotGridX
);
538 if(event
->modifiers() & Qt::ControlModifier
)
541 offset
= (int)(20 / GraphPixelsPerPoint
);
543 switch(event
->key()) {
545 if(GraphPixelsPerPoint
<= 50) {
546 GraphPixelsPerPoint
*= 2;
551 if(GraphPixelsPerPoint
>= 0.02) {
552 GraphPixelsPerPoint
/= 2;
557 if(GraphPixelsPerPoint
< 20) {
558 if (PlotGridX
&& GridLocked
&& GraphStart
< startMax
){
559 GridOffset
-= offset
;
560 GridOffset
%= PlotGridX
;
563 GraphStart
+= offset
;
565 if (PlotGridX
&& GridLocked
&& GraphStart
< startMax
){
567 GridOffset
%= PlotGridX
;
573 GridOffset
+= PlotGridX
;
576 if (GraphStart
> startMax
) {
577 GridOffset
+= (GraphStart
- startMax
);
578 GridOffset
%= PlotGridX
;
583 if(GraphPixelsPerPoint
< 20) {
584 if (PlotGridX
&& GridLocked
&& GraphStart
> 0){
585 GridOffset
+= offset
;
586 GridOffset
%= PlotGridX
;
589 GraphStart
-= offset
;
591 if (PlotGridX
&& GridLocked
&& GraphStart
> 0){
593 GridOffset
%= PlotGridX
;
600 GridOffset
+= GraphStart
;
602 GridOffset
+= PlotGridX
;
603 GridOffset
%= PlotGridX
;
608 if(PlotGridX
|| PlotGridY
) {
612 PlotGridX
= PlotGridXdefault
;
613 PlotGridY
= PlotGridYdefault
;
618 puts("Plot Window Keystrokes:\n");
619 puts(" Key Action\n");
620 puts(" DOWN Zoom in");
621 puts(" G Toggle grid display");
622 puts(" H Show help");
623 puts(" L Toggle lock grid relative to samples");
624 puts(" LEFT Move left");
625 puts(" <CTL>LEFT Move left 1 sample");
626 puts(" <SHIFT>LEFT Page left");
627 puts(" LEFT-MOUSE-CLICK Set yellow cursor");
628 puts(" Q Hide window");
629 puts(" RIGHT Move right");
630 puts(" <CTL>RIGHT Move right 1 sample");
631 puts(" <SHIFT>RIGHT Page right");
632 puts(" RIGHT-MOUSE-CLICK Set purple cursor");
633 puts(" UP Zoom out");
635 puts("Use client window 'data help' for more plot commands\n");
639 GridLocked
= !GridLocked
;
647 QWidget::keyPressEvent(event
);