source: flair-src/trunk/tools/FlairGCS/src/UsSensorPlot.cpp@ 474

Last change on this file since 474 was 444, checked in by Sanahuja Guillaume, 3 years ago

update buffering (gcs part)
seems to work!

File size: 8.4 KB
Line 
1// %flair:license{
2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
4// %flair:license}
5#include "UsSensorPlot.h"
6#include "Layout.h"
7#include <qwt_plot.h>
8#include <qwt_plot_curve.h>
9#include <qwt_plot_grid.h>
10#include <qwt_plot_magnifier.h>
11#include <qwt_plot_canvas.h>
12#include <qwt_scale_widget.h>
13#include <qwt_legend_label.h>
14#include <qwt_legend.h>
15#include <qwt_plot_panner.h>
16#include <qwt_plot_marker.h>
17#include <QMouseEvent>
18#include <QMenu>
19#include <QInputDialog>
20#include <QVBoxLayout>
21#include <QScrollBar>
22#include <qendian.h>
23#include <qwt_symbol.h>
24
25UsSensorPlot::UsSensorPlot(Layout *parent, int row, int col, QString plot_name,
26 QString y_name, float ymin, float ymax, uint32_t nbSamples,QString datasType,bool enabled, uint16_t period)
27 : DataRemote(plot_name, "UsSensorPlot", parent, enabled, period) {
28 visible_widget = new QWidget();
29
30 parent->addWidget(visible_widget, row, col);
31 QVBoxLayout* layout = new QVBoxLayout(visible_widget);
32
33 plot = new QwtPlot(NULL);
34 plot->setEnabled(enabled);
35
36 this->nbSamples=nbSamples;
37 this->datasType=datasType;
38
39#if QT_VERSION >= 0x040000
40#ifdef Q_WS_X11
41 /*
42 Qt::WA_PaintOnScreen is only supported for X11, but leads
43 to substantial bugs with Qt 4.2.x/Windows
44 */
45 plot->canvas()->setAttribute(Qt::WA_PaintOnScreen, true);
46#endif
47#endif
48
49 //scroll bar
50 scrollbar=new QScrollBar(Qt::Horizontal);
51 scrollbar->setSingleStep(1);
52 scrollbar->setRange(0,0);
53 scrolling=true;
54 connect(scrollbar,SIGNAL(valueChanged(int)),this,SLOT(scrollBarMoved(int)));
55 layout->addWidget(scrollbar);
56 layout->addWidget(plot);
57
58 alignScales();
59
60 // Assign a title
61 plot->setTitle(plot_name);
62
63 // Axis
64 plot->setAxisTitle(QwtPlot::xBottom, "samples");
65 setXAxisScale(0, nbSamples-1);
66
67 plot->setAxisTitle(QwtPlot::yLeft, y_name);
68 setYAxisScale(ymin, ymax);
69
70 // grid
71 QwtPlotGrid *grid = new QwtPlotGrid;
72 grid->setPen(QPen(Qt::black, 0, Qt::DotLine));
73 grid->attach(plot);
74
75 // zoomer
76 QwtPlotMagnifier *zoomer = new QwtPlotMagnifier(plot->canvas());
77 zoomer->setMouseButton(Qt::RightButton, Qt::ControlModifier);
78
79 // scroller
80 QwtPlotPanner *scroller = new QwtPlotPanner(plot->canvas());
81
82 plot->canvas()->installEventFilter(this);
83
84 dx = (double *)malloc(nbSamples* sizeof(double));
85 for (int i = 0; i < nbSamples; i++) dx[i] = i;
86
87 // Insert new curve
88 datas = new QwtPlotCurve();
89 datas->attach(plot);
90 datas->setPen(QPen(QColor(255, 0, 0, 255)));
91
92 datas->setVisible(true);
93
94 if (datasType == "float") {
95 receivesize += sizeof(float);
96 } else if (datasType == "int8_t" || datasType == "uint8_t") {
97 receivesize += sizeof(int8_t);
98 } else if (datasType == "int16_t" || datasType == "uint16_t") {
99 receivesize += sizeof(int16_t);
100 } else {
101 fprintf(stderr,"UsSensorPlot::unknown type %s\n",
102 datasType.toLocal8Bit().constData());
103 }
104
105 firstPeakStart = new QwtPlotMarker();
106 QwtSymbol* s1=new QwtSymbol( QwtSymbol::Diamond, Qt::blue, Qt::NoPen, QSize( 10, 10 ) );
107 firstPeakStart->setSymbol(s1);
108 firstPeakStart->attach( plot );
109
110 firstPeakEnd = new QwtPlotMarker();
111 QwtSymbol* s2=new QwtSymbol( QwtSymbol::Diamond, Qt::blue, Qt::NoPen, QSize( 10, 10 ) );
112 firstPeakEnd->setSymbol(s2);
113 firstPeakEnd->attach( plot );
114
115 secondPeakStart = new QwtPlotMarker();
116 QwtSymbol* s3=new QwtSymbol( QwtSymbol::Diamond, Qt::black, Qt::NoPen, QSize( 10, 10 ) );
117 secondPeakStart->setSymbol(s3);
118 secondPeakStart->attach( plot );
119
120 secondPeakEnd = new QwtPlotMarker();
121 QwtSymbol* s4=new QwtSymbol( QwtSymbol::Diamond, Qt::black, Qt::NoPen, QSize( 10, 10 ) );
122 secondPeakEnd->setSymbol(s4);
123 secondPeakEnd->attach( plot );
124}
125
126UsSensorPlot::~UsSensorPlot() {
127 free(dx);
128 for(int i=0;i<measures.count();i++) {
129 free(measures.at(i).dy);
130 }
131}
132
133void UsSensorPlot::XmlEvent(QDomElement *dom) {
134 XmlSetup(dom);
135}
136
137bool UsSensorPlot::eventFilter(QObject *o, QEvent *e) {
138 if (o == plot->canvas()) {
139 switch (e->type()) {
140 case QEvent::MouseButtonPress: {
141 mousePressEvent((QMouseEvent *)e);
142 break;
143 }
144
145 default:
146 break;
147 }
148 }
149 return plot->eventFilter(o, e);
150}
151
152void UsSensorPlot::setXAxisScale(float xmin, float xmax) {
153 xmin_orig = xmin;
154 xmax_orig = xmax;
155 plot->setAxisScale(QwtPlot::xBottom, xmin_orig, xmax_orig);
156}
157
158void UsSensorPlot::setYAxisScale(float ymin, float ymax) {
159 ymin_orig = ymin;
160 ymax_orig = ymax;
161 plot->setAxisScale(QwtPlot::yLeft, ymin_orig, ymax_orig);
162}
163
164//
165// Set a plain canvas frame and align the scales to it
166//
167void UsSensorPlot::alignScales(void) {
168 // The code below shows how to align the scales to
169 // the canvas frame, but is also a good example demonstrating
170 // why the spreaded API needs polishing.
171 /*
172 plot->canvas()->setFrameStyle(QFrame::Box | QFrame::Plain );
173 plot->canvas()->setLineWidth(1);
174 */
175 for (int i = 0; i < QwtPlot::axisCnt; i++) {
176 QwtScaleWidget *scaleWidget = (QwtScaleWidget *)plot->axisWidget(i);
177 if (scaleWidget)
178 scaleWidget->setMargin(0);
179
180 QwtScaleDraw *scaleDraw = (QwtScaleDraw *)plot->axisScaleDraw(i);
181 if (scaleDraw)
182 scaleDraw->enableComponent(QwtAbstractScaleDraw::Backbone, false);
183 }
184}
185
186void UsSensorPlot::BufEvent(char **buf, int *buf_size, uint16_t period,uint16_t nb_buffering,bool big_endian) {
187 plot->setEnabled(IsEnabled());
188 if (IsEnabled() == false || RefreshRate_ms() != period || NbBuffering()!=nb_buffering) return;
189
190 measure_t measure;
191 measure.dy = (double *)malloc(nbSamples* sizeof(double));
192
193 if (datasType == "float") {
194 float *data=(float*)*buf;
195 for (int i = 0; i < nbSamples; i++) {
196 measure.dy[i]=data[i];
197 }
198 *buf += nbSamples*sizeof(float);
199 } else if (datasType == "int8_t") {
200 int8_t *data=(int8_t*)*buf;
201 for (int i = 0; i < nbSamples; i++) {
202 measure.dy[i]=data[i];
203 }
204 *buf += nbSamples*sizeof(int8_t);
205 } else if (datasType == "uint8_t") {
206 uint8_t *data=(uint8_t*)*buf;
207 for (int i = 0; i < nbSamples; i++) {
208 measure.dy[i]=data[i];
209 }
210 *buf += nbSamples*sizeof(uint8_t);
211 } else if (datasType == "int16_t") {
212 int16_t *data=(int16_t*)*buf;
213 for (int i = 0; i < nbSamples; i++) {
214 measure.dy[i]=data[i];
215 }
216 *buf += nbSamples*sizeof(int16_t);
217 } else if (datasType == "uint16_t") {
218 uint16_t *data=(uint16_t*)*buf;
219 for (int i = 0; i < nbSamples; i++) {
220 measure.dy[i]=data[i];
221 }
222 *buf += nbSamples*sizeof(uint16_t);
223 } else {
224 fprintf(stderr,"UsSensorPlot::DrawDatas type non connu %s\n",datasType.toLocal8Bit().constData());
225 }
226 uint16_t *data=(uint16_t*)*buf;
227 *buf += 4*sizeof(uint16_t);
228
229 measure.firstPeakStartIndex=data[0];
230 measure.firstPeakEndIndex=data[1];
231 measure.secondPeakStartIndex=data[2];
232 measure.secondPeakEndIndex=data[3];
233 measures.append(measure);
234
235 scrollbar->setRange(0,measures.count()-1);
236 if(scrolling) {
237 scrollbar->blockSignals(true);//block valuechanged signal
238 scrollbar->setValue(measures.count()-1);
239 scrollbar->blockSignals(false);
240
241 plotMeasure(measures.count()-1);
242 }
243}
244
245void UsSensorPlot::plotMeasure(int index) {
246 firstPeakStart->setValue( QPointF(measures.at(index).firstPeakStartIndex,measures.at(index).dy[measures.at(index).firstPeakStartIndex] ) );
247 firstPeakEnd->setValue( QPointF(measures.at(index).firstPeakEndIndex,measures.at(index).dy[measures.at(index).firstPeakEndIndex] ) );
248 secondPeakStart->setValue( QPointF(measures.at(index).secondPeakStartIndex,measures.at(index).dy[measures.at(index).secondPeakStartIndex] ) );
249 secondPeakEnd->setValue( QPointF(measures.at(index).secondPeakEndIndex,measures.at(index).dy[measures.at(index).secondPeakEndIndex] ) );
250
251 // Attach (don't copy) data
252 datas->setRawSamples(dx,measures.at(index).dy, nbSamples);
253
254 plot->replot();
255}
256// context menu
257void UsSensorPlot::mousePressEvent(QMouseEvent *event) {
258 if (event->button() == Qt::RightButton) {
259
260 QMenu *menu = new QMenu("nom", plot);
261 // ajout des actions
262 QAction *a, *z;
263
264 a = menu->addAction("reset zoom");
265 menu->addSeparator();
266
267 appendmenu(menu);
268 z = execmenu(plot, menu, event->globalPos());
269 delete menu;
270
271 if (z == a) {
272 // zoom to original size
273 plot->setAxisScale(QwtPlot::yLeft, ymin_orig, ymax_orig);
274 plot->setAxisScale(QwtPlot::xBottom, xmin_orig, xmax_orig);
275 }
276 }
277}
278
279void UsSensorPlot::scrollBarMoved(int value) {
280 if(value==scrollbar->maximum()) {
281 scrolling=true;
282 } else {
283 scrolling=false;
284 }
285 plotMeasure(value);
286}
Note: See TracBrowser for help on using the repository browser.