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

Last change on this file since 250 was 244, checked in by Sanahuja Guillaume, 6 years ago

modifs segfault when closing connection

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