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>
23 #include <QHBoxLayout>
25 #include "proxguiqt.h"
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::_ShowGraphWindow(void)
55 plotwidget
= new ProxWidget();
60 void ProxGuiQT::_RepaintGraphWindow(void)
62 if (!plotapp
|| !plotwidget
)
68 void ProxGuiQT::_HideGraphWindow(void)
70 if (!plotapp
|| !plotwidget
)
76 void ProxGuiQT::MainLoop()
78 plotapp
= new QApplication(argc
, argv
);
80 connect(this, SIGNAL(ShowGraphWindowSignal()), this, SLOT(_ShowGraphWindow()));
81 connect(this, SIGNAL(RepaintGraphWindowSignal()), this, SLOT(_RepaintGraphWindow()));
82 connect(this, SIGNAL(HideGraphWindowSignal()), this, SLOT(_HideGraphWindow()));
87 ProxGuiQT::ProxGuiQT(int argc
, char **argv
) : plotapp(NULL
), plotwidget(NULL
),
88 argc(argc
), argv(argv
)
92 ProxGuiQT::~ProxGuiQT(void)
95 //plotwidget->close();
107 //--------------------
108 void ProxWidget::applyOperation()
110 printf("ApplyOperation()");
112 memcpy(GraphBuffer
, s_Buff
, sizeof(int) * GraphTraceLen
);
113 RepaintGraphWindow();
116 void ProxWidget::stickOperation()
119 printf("stickOperation()");
121 void ProxWidget::vchange_autocorr(int v
)
124 ans
= AutoCorrelate(GraphBuffer
, s_Buff
, GraphTraceLen
, v
, true, false);
125 printf("vchange_autocorr(w:%d): %d\n", v
, ans
);
126 RepaintGraphWindow();
128 void ProxWidget::vchange_askedge(int v
)
131 //extern int AskEdgeDetect(const int *in, int *out, int len, int threshold);
132 ans
= AskEdgeDetect(GraphBuffer
, s_Buff
, GraphTraceLen
, v
);
133 printf("vchange_askedge(w:%d)\n", v
);
134 RepaintGraphWindow();
136 void ProxWidget::vchange_dthr_up(int v
)
138 int down
= opsController
->horizontalSlider_dirthr_down
->value();
139 directionalThreshold(GraphBuffer
, s_Buff
, GraphTraceLen
, v
, down
);
140 printf("vchange_dthr_up(%d)", v
);
141 RepaintGraphWindow();
144 void ProxWidget::vchange_dthr_down(int v
)
146 printf("vchange_dthr_down(%d)", v
);
147 int up
= opsController
->horizontalSlider_dirthr_up
->value();
148 directionalThreshold(GraphBuffer
,s_Buff
, GraphTraceLen
, v
, up
);
149 RepaintGraphWindow();
152 ProxWidget::ProxWidget(QWidget
*parent
, ProxGuiQT
*master
) : QWidget(parent
)
154 this->master
= master
;
157 /** Setup the controller widget **/
159 QWidget
* controlWidget
= new QWidget();
160 opsController
= new Ui::Form();
161 opsController
->setupUi(controlWidget
);
162 //Due to quirks in QT Designer, we need to fiddle a bit
163 opsController
->horizontalSlider_dirthr_down
->setMinimum(-128);
164 opsController
->horizontalSlider_dirthr_down
->setMaximum(0);
165 opsController
->horizontalSlider_dirthr_down
->setValue(-20);
168 QObject::connect(opsController
->pushButton_apply
, SIGNAL(clicked()), this, SLOT(applyOperation()));
169 QObject::connect(opsController
->pushButton_sticky
, SIGNAL(clicked()), this, SLOT(stickOperation()));
170 QObject::connect(opsController
->horizontalSlider_window
, SIGNAL(valueChanged(int)), this, SLOT(vchange_autocorr(int)));
171 QObject::connect(opsController
->horizontalSlider_dirthr_up
, SIGNAL(valueChanged(int)), this, SLOT(vchange_dthr_up(int)));
172 QObject::connect(opsController
->horizontalSlider_dirthr_down
, SIGNAL(valueChanged(int)), this, SLOT(vchange_dthr_down(int)));
173 QObject::connect(opsController
->horizontalSlider_askedge
, SIGNAL(valueChanged(int)), this, SLOT(vchange_askedge(int)));
175 controlWidget
->show();
177 // Set up the plot widget, which does the actual plotting
179 plot
= new Plot(this);
181 QSlider* slider = new QSlider(Qt::Horizontal);
182 slider->setFocusPolicy(Qt::StrongFocus);
183 slider->setTickPosition(QSlider::TicksBothSides);
184 slider->setTickInterval(10);
185 slider->setSingleStep(1);
187 QVBoxLayout
*layout
= new QVBoxLayout
;
188 //layout->addWidget(slider);
189 layout
->addWidget(plot
);
191 printf("Proxwidget Constructor just set layout\r\n");
195 //----------- Plotting
197 int Plot::xCoordOf(int i
, QRect r
)
199 return r
.left() + (int)((i
- GraphStart
)*GraphPixelsPerPoint
);
202 int Plot::yCoordOf(int v
, QRect r
, int maxVal
)
204 int z
= (r
.bottom() - r
.top())/2;
205 return -(z
* v
) / maxVal
+ z
;
208 int Plot::valueOf_yCoord(int y
, QRect r
, int maxVal
)
210 int z
= (r
.bottom() - r
.top())/2;
211 return (y
-z
) * maxVal
/ z
;
213 static const QColor GREEN
= QColor(100,255,100);
214 static const QColor RED
= QColor(255,100,100);
215 static const QColor BLUE
= QColor(100,100,255);
216 static const QColor GRAY
= QColor(240,240,240);
218 QColor
Plot::getColor(int graphNum
)
221 case 0: return GREEN
; //Green
222 case 1: return RED
; //Red
223 case 2: return BLUE
; //Blue
224 default: return GRAY
; //Gray
228 void Plot::PlotDemod(uint8_t *buffer
, size_t len
, QRect plotRect
, QRect annotationRect
, QPainter
*painter
, int graphNum
, int plotOffset
)
230 if (len
== 0 || PlotGridX
<= 0) return;
231 //clock_t begin = clock();
232 QPainterPath penPath
;
234 int grid_delta_x
= PlotGridX
;
235 int first_delta_x
= grid_delta_x
; //(plotOffset > 0) ? PlotGridX : (PlotGridX +);
236 if (GraphStart
> plotOffset
) first_delta_x
-= (GraphStart
-plotOffset
);
237 int DemodStart
= GraphStart
;
238 if (plotOffset
> GraphStart
) DemodStart
= plotOffset
;
242 if (DemodStart
-plotOffset
> 0) BitStart
= (int)(((DemodStart
-plotOffset
)+(PlotGridX
-1))/PlotGridX
)-1;
243 first_delta_x
+= BitStart
* PlotGridX
;
244 if (BitStart
> len
) return;
247 //printf("first_delta_x %i, grid_delta_x %i, DemodStart %i, BitStart %i\n",first_delta_x,grid_delta_x,DemodStart, BitStart);
249 painter
->setPen(getColor(graphNum
));
251 int absVMax
= (int)(100*1.05+1);
252 int x
= xCoordOf(DemodStart
, plotRect
);
253 int y
= yCoordOf((buffer
[BitStart
]*200-100)*-1,plotRect
,absVMax
);
254 penPath
.moveTo(x
, y
);
256 int clk
= first_delta_x
;
257 for(int i
= BitStart
; i
< len
&& xCoordOf(delta_x
+DemodStart
, plotRect
) < plotRect
.right(); i
++) {
258 for (int ii
= 0; ii
< (clk
) && i
< len
&& xCoordOf(DemodStart
+delta_x
+ii
, plotRect
) < plotRect
.right() ; ii
++ ) {
259 x
= xCoordOf(DemodStart
+delta_x
+ii
, plotRect
);
260 v
= buffer
[i
]*200-100;
262 y
= yCoordOf( v
, plotRect
, absVMax
);
264 penPath
.lineTo(x
, y
);
266 if(GraphPixelsPerPoint
> 10) {
267 QRect
f(QPoint(x
- 3, y
- 3),QPoint(x
+ 3, y
+ 3));
268 painter
->fillRect(f
, QColor(100, 255, 100));
270 if (ii
== (int)clk
/2) {
272 sprintf(str
, "%u",buffer
[i
]);
273 painter
->drawText(x
-8, y
+ ((buffer
[i
] > 0) ? 18 : -6), str
);
281 painter
->drawPath(penPath
);
284 void Plot::PlotGraph(int *buffer
, int len
, QRect plotRect
, QRect annotationRect
, QPainter
*painter
, int graphNum
)
286 if (len
== 0) return;
287 //clock_t begin = clock();
288 QPainterPath penPath
;
290 startMax
= (len
- (int)((plotRect
.right() - plotRect
.left() - 40) / GraphPixelsPerPoint
));
294 if(GraphStart
> startMax
) {
295 GraphStart
= startMax
;
297 if (GraphStart
> len
) return;
298 int vMin
= INT_MAX
, vMax
= INT_MIN
, vMean
= 0, v
= 0, absVMax
= 0;
299 int sample_index
= GraphStart
;
300 for( ; sample_index
< len
&& xCoordOf(sample_index
,plotRect
) < plotRect
.right() ; sample_index
++) {
302 v
= buffer
[sample_index
];
303 if(v
< vMin
) vMin
= v
;
304 if(v
> vMax
) vMax
= v
;
308 vMean
/= (sample_index
- GraphStart
);
310 if(fabs( (double) vMin
) > absVMax
) absVMax
= (int)fabs( (double) vMin
);
311 if(fabs( (double) vMax
) > absVMax
) absVMax
= (int)fabs( (double) vMax
);
312 absVMax
= (int)(absVMax
*1.25 + 1);
313 // number of points that will be plotted
314 int span
= (int)((plotRect
.right() - plotRect
.left()) / GraphPixelsPerPoint
);
315 // one label every 100 pixels, let us say
316 int labels
= (plotRect
.right() - plotRect
.left() - 40) / 100;
317 if(labels
<= 0) labels
= 1;
318 int pointsPerLabel
= span
/ labels
;
319 if(pointsPerLabel
<= 0) pointsPerLabel
= 1;
321 int x
= xCoordOf(GraphStart
, plotRect
);
322 int y
= yCoordOf(buffer
[GraphStart
],plotRect
,absVMax
);
323 penPath
.moveTo(x
, y
);
324 for(int i
= GraphStart
; i
< len
&& xCoordOf(i
, plotRect
) < plotRect
.right(); i
++) {
326 x
= xCoordOf(i
, plotRect
);
329 y
= yCoordOf( v
, plotRect
, absVMax
);//(y * (r.top() - r.bottom()) / (2*absYMax)) + zeroHeight;
331 penPath
.lineTo(x
, y
);
333 if(GraphPixelsPerPoint
> 10) {
334 QRect
f(QPoint(x
- 3, y
- 3),QPoint(x
+ 3, y
+ 3));
335 painter
->fillRect(f
, QColor(100, 255, 100));
339 painter
->setPen(getColor(graphNum
));
342 int xo
= 5+(graphNum
*40);
343 painter
->drawLine(xo
, plotRect
.top(),xo
, plotRect
.bottom());
345 int vMarkers
= (absVMax
- (absVMax
% 10)) / 5;
346 int minYDist
= 40; //Minimum pixel-distance between markers
353 for(int v
= vMarkers
; yCoordOf(v
,plotRect
,absVMax
) > plotRect
.top() && n
< 20; v
+= vMarkers
,n
++)
355 int y0
= yCoordOf(v
,plotRect
,absVMax
);
356 int y1
= yCoordOf(-v
,plotRect
,absVMax
);
358 if(lasty0
- y0
< minYDist
) continue;
360 painter
->drawLine(xo
-5,y0
, xo
+5, y0
);
362 sprintf(yLbl
, "%d", v
);
363 painter
->drawText(xo
+8,y0
+7,yLbl
);
365 painter
->drawLine(xo
-5, y1
, xo
+5, y1
);
366 sprintf(yLbl
, "%d",-v
);
367 painter
->drawText(xo
+8, y1
+5 , yLbl
);
372 painter
->drawPath(penPath
);
374 sprintf(str
, "max=%d min=%d mean=%d n=%d/%d CursorAVal=[%d] CursorBVal=[%d]",
375 vMax
, vMin
, vMean
, sample_index
, len
, buffer
[CursorAPos
], buffer
[CursorBPos
]);
376 painter
->drawText(20, annotationRect
.bottom() - 23 - 20 * graphNum
, str
);
378 //clock_t end = clock();
379 //double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
380 //printf("Plot time %f\n", elapsed_secs);
383 void Plot::plotGridLines(QPainter
* painter
,QRect r
)
385 int zeroHeight
= r
.top() + (r
.bottom() - r
.top()) / 2;
388 int grid_delta_x
= (int) (PlotGridX
* GraphPixelsPerPoint
);
389 int grid_delta_y
= (int) (PlotGridY
* GraphPixelsPerPoint
);
390 if ((PlotGridX
> 0) && ((PlotGridX
* GraphPixelsPerPoint
) > 1)) {
391 for(i
= (GridOffset
* GraphPixelsPerPoint
); i
< r
.right(); i
+= grid_delta_x
) {
392 painter
->drawLine(r
.left()+i
, r
.top(), r
.left()+i
, r
.bottom());
395 if ((PlotGridY
> 0) && ((PlotGridY
* GraphPixelsPerPoint
) > 1)){
396 for(i
= 0; i
< ((r
.top() + r
.bottom())>>1); i
+= grid_delta_y
) {
397 painter
->drawLine(r
.left(),zeroHeight
+ i
,r
.right(),zeroHeight
+ i
);
398 painter
->drawLine(r
.left(),zeroHeight
- i
,r
.right(),zeroHeight
- i
);
403 #define HEIGHT_INFO 60
404 #define WIDTH_AXES 80
406 void Plot::paintEvent(QPaintEvent
*event
)
408 QPainter
painter(this);
409 //QPainterPath penPath, whitePath, greyPath, lightgreyPath, cursorAPath, cursorBPath, cursorCPath, cursorDPath;
411 QBrush
brush(QColor(100, 255, 100));
412 QPen
pen(QColor(100, 255, 100));
414 painter
.setFont(QFont("Courier New", 10));
420 if (CursorAPos
> GraphTraceLen
)
422 if(CursorBPos
> GraphTraceLen
)
424 if(CursorCPos
> GraphTraceLen
)
426 if(CursorDPos
> GraphTraceLen
)
429 QRect
plotRect(WIDTH_AXES
, 0, width()-WIDTH_AXES
, height()-HEIGHT_INFO
);
430 QRect
infoRect(0, height()-HEIGHT_INFO
, width(), HEIGHT_INFO
);
433 painter
.fillRect(rect(), QColor(60, 60, 60));
435 painter
.fillRect(plotRect
, QColor(0, 0, 0));
438 int zeroHeight
= plotRect
.top() + (plotRect
.bottom() - plotRect
.top()) / 2;
439 painter
.setPen(QColor(100, 100, 100));
440 painter
.drawLine(plotRect
.left(), zeroHeight
, plotRect
.right(), zeroHeight
);
441 // plot X and Y grid lines
442 plotGridLines(&painter
, plotRect
);
444 //Start painting graph
445 if (showDemod
&& DemodBufferLen
> 8) {
446 PlotDemod(DemodBuffer
, DemodBufferLen
,plotRect
,infoRect
,&painter
,2,g_DemodStartIdx
);
448 PlotGraph(s_Buff
, GraphTraceLen
,plotRect
,infoRect
,&painter
,1);
449 PlotGraph(GraphBuffer
, GraphTraceLen
,plotRect
,infoRect
,&painter
,0);
453 if(CursorAPos
> GraphStart
&& xCoordOf(CursorAPos
, plotRect
) < plotRect
.right())
455 painter
.setPen(QColor(255, 255, 0));
456 painter
.drawLine(xCoordOf(CursorAPos
, plotRect
),plotRect
.top(),xCoordOf(CursorAPos
, plotRect
),plotRect
.bottom());
458 if(CursorBPos
> GraphStart
&& xCoordOf(CursorBPos
, plotRect
) < plotRect
.right())
460 painter
.setPen(QColor(255, 0, 255));
461 painter
.drawLine(xCoordOf(CursorBPos
, plotRect
),plotRect
.top(),xCoordOf(CursorBPos
, plotRect
),plotRect
.bottom());
463 if(CursorCPos
> GraphStart
&& xCoordOf(CursorCPos
, plotRect
) < plotRect
.right())
465 painter
.setPen(QColor(255, 153, 0)); //orange
466 painter
.drawLine(xCoordOf(CursorCPos
, plotRect
),plotRect
.top(),xCoordOf(CursorCPos
, plotRect
),plotRect
.bottom());
468 if(CursorDPos
> GraphStart
&& xCoordOf(CursorDPos
, plotRect
) < plotRect
.right())
470 painter
.setPen(QColor(0, 0, 205)); //light blue
471 painter
.drawLine(xCoordOf(CursorDPos
, plotRect
),plotRect
.top(),xCoordOf(CursorDPos
, plotRect
),plotRect
.bottom());
476 sprintf(str
, "@%d dt=%d [%2.2f] zoom=%2.2f CursorAPos=%d CursorBPos=%d GridX=%d GridY=%d (%s) GridXoffset=%d",
477 GraphStart
, CursorBPos
- CursorAPos
, (CursorBPos
- CursorAPos
)/CursorScaleFactor
,
478 GraphPixelsPerPoint
,CursorAPos
,CursorBPos
,PlotGridXdefault
,PlotGridYdefault
,GridLocked
?"Locked":"Unlocked",GridOffset
);
479 painter
.setPen(QColor(255, 255, 255));
480 painter
.drawText(20, infoRect
.bottom() - 3, str
);
484 Plot::Plot(QWidget
*parent
) : QWidget(parent
), GraphStart(0), GraphPixelsPerPoint(1)
486 //Need to set this, otherwise we don't receive keypress events
487 setFocusPolicy( Qt::StrongFocus
);
490 QPalette
palette(QColor(0,0,0,0));
491 palette
.setColor(QPalette::WindowText
, QColor(255,255,255));
492 palette
.setColor(QPalette::Text
, QColor(255,255,255));
493 palette
.setColor(QPalette::Button
, QColor(100, 100, 100));
495 setAutoFillBackground(true);
499 setWindowTitle(tr("Sliders"));
502 void Plot::closeEvent(QCloseEvent
*event
)
508 void Plot::mouseMoveEvent(QMouseEvent
*event
)
512 x
= (int)(x
/ GraphPixelsPerPoint
);
514 if((event
->buttons() & Qt::LeftButton
)) {
516 } else if (event
->buttons() & Qt::RightButton
) {
524 void Plot::keyPressEvent(QKeyEvent
*event
)
531 if(event
->modifiers() & Qt::ShiftModifier
) {
533 offset
= PageWidth
- (PageWidth
% PlotGridX
);
537 if(event
->modifiers() & Qt::ControlModifier
)
540 offset
= (int)(20 / GraphPixelsPerPoint
);
542 switch(event
->key()) {
544 if(GraphPixelsPerPoint
<= 50) {
545 GraphPixelsPerPoint
*= 2;
550 if(GraphPixelsPerPoint
>= 0.02) {
551 GraphPixelsPerPoint
/= 2;
556 if(GraphPixelsPerPoint
< 20) {
557 if (PlotGridX
&& GridLocked
&& GraphStart
< startMax
){
558 GridOffset
-= offset
;
559 GridOffset
%= PlotGridX
;
562 GraphStart
+= offset
;
564 if (PlotGridX
&& GridLocked
&& GraphStart
< startMax
){
566 GridOffset
%= PlotGridX
;
572 GridOffset
+= PlotGridX
;
575 if (GraphStart
> startMax
) {
576 GridOffset
+= (GraphStart
- startMax
);
577 GridOffset
%= PlotGridX
;
582 if(GraphPixelsPerPoint
< 20) {
583 if (PlotGridX
&& GridLocked
&& GraphStart
> 0){
584 GridOffset
+= offset
;
585 GridOffset
%= PlotGridX
;
588 GraphStart
-= offset
;
590 if (PlotGridX
&& GridLocked
&& GraphStart
> 0){
592 GridOffset
%= PlotGridX
;
599 GridOffset
+= GraphStart
;
601 GridOffset
+= PlotGridX
;
602 GridOffset
%= PlotGridX
;
607 if(PlotGridX
|| PlotGridY
) {
611 PlotGridX
= PlotGridXdefault
;
612 PlotGridY
= PlotGridYdefault
;
617 puts("Plot Window Keystrokes:\n");
618 puts(" Key Action\n");
619 puts(" DOWN Zoom in");
620 puts(" G Toggle grid display");
621 puts(" H Show help");
622 puts(" L Toggle lock grid relative to samples");
623 puts(" LEFT Move left");
624 puts(" <CTL>LEFT Move left 1 sample");
625 puts(" <SHIFT>LEFT Page left");
626 puts(" LEFT-MOUSE-CLICK Set yellow cursor");
627 puts(" Q Hide window");
628 puts(" RIGHT Move right");
629 puts(" <CTL>RIGHT Move right 1 sample");
630 puts(" <SHIFT>RIGHT Page right");
631 puts(" RIGHT-MOUSE-CLICK Set purple cursor");
632 puts(" UP Zoom out");
634 puts("Use client window 'data help' for more plot commands\n");
638 GridLocked
= !GridLocked
;
646 QWidget::keyPressEvent(event
);