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 |
|
---|
25 | UsSensorPlot::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, int 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 |
|
---|
126 | UsSensorPlot::~UsSensorPlot() {
|
---|
127 | free(dx);
|
---|
128 | for(int i=0;i<measures.count();i++) {
|
---|
129 | free(measures.at(i).dy);
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | void UsSensorPlot::XmlEvent(QDomElement dom) {
|
---|
134 | XmlSetup(dom);
|
---|
135 | }
|
---|
136 |
|
---|
137 | bool 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 |
|
---|
152 | void 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 |
|
---|
158 | void 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 | //
|
---|
167 | void 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 |
|
---|
186 | void UsSensorPlot::BufEvent(char **buf, int *buf_size, uint16_t period,bool big_endian) {
|
---|
187 | plot->setEnabled(IsEnabled());
|
---|
188 | if (IsEnabled() == false || RefreshRate_ms() != period)
|
---|
189 | return;
|
---|
190 |
|
---|
191 | measure_t measure;
|
---|
192 | measure.dy = (double *)malloc(nbSamples* sizeof(double));
|
---|
193 |
|
---|
194 | if (datasType == "float") {
|
---|
195 | float *data=(float*)*buf;
|
---|
196 | for (int i = 0; i < nbSamples; i++) {
|
---|
197 | measure.dy[i]=data[i];
|
---|
198 | }
|
---|
199 | *buf += nbSamples*sizeof(float);
|
---|
200 | } else if (datasType == "int8_t") {
|
---|
201 | int8_t *data=(int8_t*)*buf;
|
---|
202 | for (int i = 0; i < nbSamples; i++) {
|
---|
203 | measure.dy[i]=data[i];
|
---|
204 | }
|
---|
205 | *buf += nbSamples*sizeof(int8_t);
|
---|
206 | } else if (datasType == "uint8_t") {
|
---|
207 | uint8_t *data=(uint8_t*)*buf;
|
---|
208 | for (int i = 0; i < nbSamples; i++) {
|
---|
209 | measure.dy[i]=data[i];
|
---|
210 | }
|
---|
211 | *buf += nbSamples*sizeof(uint8_t);
|
---|
212 | } else if (datasType == "int16_t") {
|
---|
213 | int16_t *data=(int16_t*)*buf;
|
---|
214 | for (int i = 0; i < nbSamples; i++) {
|
---|
215 | measure.dy[i]=data[i];
|
---|
216 | }
|
---|
217 | *buf += nbSamples*sizeof(int16_t);
|
---|
218 | } else if (datasType == "uint16_t") {
|
---|
219 | uint16_t *data=(uint16_t*)*buf;
|
---|
220 | for (int i = 0; i < nbSamples; i++) {
|
---|
221 | measure.dy[i]=data[i];
|
---|
222 | }
|
---|
223 | *buf += nbSamples*sizeof(uint16_t);
|
---|
224 | } else {
|
---|
225 | fprintf(stderr,"UsSensorPlot::DrawDatas type non connu %s\n",datasType.toLocal8Bit().constData());
|
---|
226 | }
|
---|
227 | uint16_t *data=(uint16_t*)*buf;
|
---|
228 | *buf += 4*sizeof(uint16_t);
|
---|
229 |
|
---|
230 | measure.firstPeakStartIndex=data[0];
|
---|
231 | measure.firstPeakEndIndex=data[1];
|
---|
232 | measure.secondPeakStartIndex=data[2];
|
---|
233 | measure.secondPeakEndIndex=data[3];
|
---|
234 | measures.append(measure);
|
---|
235 |
|
---|
236 | scrollbar->setRange(0,measures.count()-1);
|
---|
237 | if(scrolling) {
|
---|
238 | scrollbar->blockSignals(true);//block valuechanged signal
|
---|
239 | scrollbar->setValue(measures.count()-1);
|
---|
240 | scrollbar->blockSignals(false);
|
---|
241 |
|
---|
242 | plotMeasure(measures.count()-1);
|
---|
243 | }
|
---|
244 | }
|
---|
245 |
|
---|
246 | void UsSensorPlot::plotMeasure(int index) {
|
---|
247 | firstPeakStart->setValue( QPointF(measures.at(index).firstPeakStartIndex,measures.at(index).dy[measures.at(index).firstPeakStartIndex] ) );
|
---|
248 | firstPeakEnd->setValue( QPointF(measures.at(index).firstPeakEndIndex,measures.at(index).dy[measures.at(index).firstPeakEndIndex] ) );
|
---|
249 | secondPeakStart->setValue( QPointF(measures.at(index).secondPeakStartIndex,measures.at(index).dy[measures.at(index).secondPeakStartIndex] ) );
|
---|
250 | secondPeakEnd->setValue( QPointF(measures.at(index).secondPeakEndIndex,measures.at(index).dy[measures.at(index).secondPeakEndIndex] ) );
|
---|
251 |
|
---|
252 | // Attach (don't copy) data
|
---|
253 | datas->setRawSamples(dx,measures.at(index).dy, nbSamples);
|
---|
254 |
|
---|
255 | plot->replot();
|
---|
256 | }
|
---|
257 | // context menu
|
---|
258 | void UsSensorPlot::mousePressEvent(QMouseEvent *event) {
|
---|
259 | if (event->button() == Qt::RightButton) {
|
---|
260 |
|
---|
261 | QMenu *menu = new QMenu("nom", plot);
|
---|
262 | // ajout des actions
|
---|
263 | QAction *a, *z;
|
---|
264 |
|
---|
265 | a = menu->addAction("reset zoom");
|
---|
266 | menu->addSeparator();
|
---|
267 |
|
---|
268 | appendmenu(menu);
|
---|
269 | z = execmenu(plot, menu, event->globalPos());
|
---|
270 | delete menu;
|
---|
271 |
|
---|
272 | if (z == a) {
|
---|
273 | // zoom to original size
|
---|
274 | plot->setAxisScale(QwtPlot::yLeft, ymin_orig, ymax_orig);
|
---|
275 | plot->setAxisScale(QwtPlot::xBottom, xmin_orig, xmax_orig);
|
---|
276 | }
|
---|
277 | }
|
---|
278 | }
|
---|
279 |
|
---|
280 | void UsSensorPlot::scrollBarMoved(int value) {
|
---|
281 | if(value==scrollbar->maximum()) {
|
---|
282 | scrolling=true;
|
---|
283 | } else {
|
---|
284 | scrolling=false;
|
---|
285 | }
|
---|
286 | plotMeasure(value);
|
---|
287 | }
|
---|