1 | #include "RangeFinderPlot.h"
|
---|
2 | #include "Layout.h"
|
---|
3 | #include <stdlib.h>
|
---|
4 | #include <qwt_plot_grid.h>
|
---|
5 | #include <qwt_plot_magnifier.h>
|
---|
6 | #include <qwt_scale_widget.h>
|
---|
7 | #include <qwt_plot_panner.h>
|
---|
8 | #include <qwt_plot_rescaler.h>
|
---|
9 | #include <qwt_plot_shapeitem.h>
|
---|
10 | #include <QMouseEvent>
|
---|
11 | #include <QMenu>
|
---|
12 | #include <QInputDialog>
|
---|
13 | #include <qendian.h>
|
---|
14 | #include <qmath.h>
|
---|
15 |
|
---|
16 | RangeFinderPlot::RangeFinderPlot(Layout* parent,int row, int col,QString name,
|
---|
17 | QString x_name,QString y_name,
|
---|
18 | float xmin, float xmax,float ymin, float ymax,
|
---|
19 | float start_angle,float end_angle,uint32_t nb_samples,QString data_type,
|
---|
20 | bool invert_axis,bool enabled,int period):
|
---|
21 | DataRemote(name,"RangeFinderPlot",parent,enabled,period)
|
---|
22 | {
|
---|
23 | invert_axis=true;
|
---|
24 | this->start_angle=start_angle;
|
---|
25 | this->end_angle=end_angle;
|
---|
26 | this->nb_samples=nb_samples;
|
---|
27 | this->data_type=data_type;
|
---|
28 | this->invert_axis=invert_axis;
|
---|
29 |
|
---|
30 | if(data_type=="float")
|
---|
31 | {
|
---|
32 | receivesize=nb_samples*sizeof(float);
|
---|
33 | }
|
---|
34 | else if(data_type=="int8_t")
|
---|
35 | {
|
---|
36 | receivesize=nb_samples*sizeof(int8_t);
|
---|
37 | }
|
---|
38 | else if(data_type=="int16_t")
|
---|
39 | {
|
---|
40 | receivesize=nb_samples*sizeof(int16_t);
|
---|
41 | }
|
---|
42 | else
|
---|
43 | {
|
---|
44 | printf("RangeFinderPlot::RangeFinderPlot unknown type %s\n",data_type.toLocal8Bit().constData());
|
---|
45 | }
|
---|
46 |
|
---|
47 |
|
---|
48 | plot=new QwtPlot(NULL);
|
---|
49 | plot->setEnabled(enabled);
|
---|
50 | //plot->setAutoReplot( false );
|
---|
51 |
|
---|
52 | parent->addWidget(plot,row,col);
|
---|
53 | visible_widget=plot;
|
---|
54 |
|
---|
55 | plot->setTitle(name);
|
---|
56 |
|
---|
57 | // grid
|
---|
58 | QwtPlotGrid *grid = new QwtPlotGrid;
|
---|
59 | grid->setPen(QPen(Qt::black, 0, Qt::DotLine));
|
---|
60 | grid->attach(plot);
|
---|
61 |
|
---|
62 | plot->updateAxes();
|
---|
63 |
|
---|
64 | //zoomer
|
---|
65 | QwtPlotMagnifier * zoomer = new QwtPlotMagnifier(plot->canvas());
|
---|
66 | zoomer->setMouseButton(Qt::RightButton,Qt::ControlModifier);
|
---|
67 |
|
---|
68 | //scroller
|
---|
69 | QwtPlotPanner *scroller =new QwtPlotPanner(plot->canvas());
|
---|
70 |
|
---|
71 | // Axis
|
---|
72 | if(!invert_axis) {
|
---|
73 | plot->setAxisTitle(QwtPlot::xBottom, x_name);
|
---|
74 | setXAxisScale(xmin,xmax);
|
---|
75 |
|
---|
76 | plot->setAxisTitle(QwtPlot::yLeft, y_name);
|
---|
77 | setYAxisScale(ymin,ymax);
|
---|
78 | } else {
|
---|
79 | plot->setAxisTitle(QwtPlot::xBottom, y_name);
|
---|
80 | setXAxisScale(ymin,ymax);
|
---|
81 |
|
---|
82 | plot->setAxisTitle(QwtPlot::yLeft, x_name);
|
---|
83 | setYAxisScale(xmin,xmax);
|
---|
84 | }
|
---|
85 |
|
---|
86 | QwtPlotRescaler* rescaler = new QwtPlotRescaler( plot->canvas() );
|
---|
87 | rescaler->setRescalePolicy( QwtPlotRescaler::Fixed );
|
---|
88 |
|
---|
89 | //( void ) new QwtPlotMagnifier( plot->canvas() );
|
---|
90 |
|
---|
91 | plot->canvas()->installEventFilter(this);
|
---|
92 |
|
---|
93 | for(uint32_t i=0;i<nb_samples;i++)
|
---|
94 | {
|
---|
95 | addTriangle(start_angle+i*(end_angle-start_angle+1)/nb_samples,start_angle+(i+1)*(end_angle-start_angle+1)/nb_samples);
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | RangeFinderPlot::~RangeFinderPlot()
|
---|
100 | {
|
---|
101 | delete plot;
|
---|
102 | }
|
---|
103 |
|
---|
104 | void RangeFinderPlot::XmlEvent(QDomElement dom)
|
---|
105 | {
|
---|
106 | XmlSetup(dom);
|
---|
107 | }
|
---|
108 |
|
---|
109 | bool RangeFinderPlot::eventFilter(QObject *o, QEvent *e)
|
---|
110 | {
|
---|
111 | if (o==plot->canvas())
|
---|
112 | {
|
---|
113 | switch(e->type())
|
---|
114 | {
|
---|
115 | case QEvent::MouseButtonPress:
|
---|
116 | {
|
---|
117 | mousePressEvent((QMouseEvent*)e);
|
---|
118 | break;
|
---|
119 | }
|
---|
120 |
|
---|
121 | default:
|
---|
122 | break;
|
---|
123 | }
|
---|
124 | }
|
---|
125 | return plot->eventFilter(o, e);
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | //
|
---|
130 | // Set a plain canvas frame and align the scales to it
|
---|
131 | //
|
---|
132 | void RangeFinderPlot::alignScales(void)
|
---|
133 | {
|
---|
134 | // The code below shows how to align the scales to
|
---|
135 | // the canvas frame, but is also a good example demonstrating
|
---|
136 | // why the spreaded API needs polishing.
|
---|
137 | /*
|
---|
138 | plot->canvas()->setFrameStyle(QFrame::Box | QFrame::Plain );
|
---|
139 | plot->canvas()->setLineWidth(1);
|
---|
140 | */
|
---|
141 | for ( int i = 0; i < QwtPlot::axisCnt; i++ )
|
---|
142 | {
|
---|
143 | QwtScaleWidget *scaleWidget = (QwtScaleWidget *)plot->axisWidget(i);
|
---|
144 | if ( scaleWidget )
|
---|
145 | scaleWidget->setMargin(0);
|
---|
146 |
|
---|
147 | QwtScaleDraw *scaleDraw = (QwtScaleDraw *)plot->axisScaleDraw(i);
|
---|
148 | if ( scaleDraw )
|
---|
149 | scaleDraw->enableComponent(QwtAbstractScaleDraw::Backbone, false);
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | void RangeFinderPlot::setXAxisScale(float xmin,float xmax)
|
---|
154 | {
|
---|
155 | xmin_orig=xmin;
|
---|
156 | xmax_orig=xmax;
|
---|
157 | plot->setAxisScale(QwtPlot::xBottom, xmin_orig, xmax_orig);
|
---|
158 |
|
---|
159 | }
|
---|
160 |
|
---|
161 | void RangeFinderPlot::setYAxisScale(float ymin,float ymax)
|
---|
162 | {
|
---|
163 | ymin_orig=ymin;
|
---|
164 | ymax_orig=ymax;
|
---|
165 | plot->setAxisScale(QwtPlot::yLeft, ymin_orig, ymax_orig);
|
---|
166 | }
|
---|
167 |
|
---|
168 | void RangeFinderPlot::BufEvent(char **buf,int *buf_size,uint16_t period,bool big_endian)
|
---|
169 | {
|
---|
170 | plot->setEnabled(IsEnabled());
|
---|
171 | if(IsEnabled()==false || RefreshRate_ms()!=period) return;
|
---|
172 |
|
---|
173 | for(uint32_t i=0;i<nb_samples;i++)
|
---|
174 | {
|
---|
175 | if(data_type=="float")
|
---|
176 | {
|
---|
177 | uint32_t data_raw;
|
---|
178 | float* data=(float*)&data_raw;
|
---|
179 |
|
---|
180 | memcpy((void*)&data_raw,*buf,sizeof(uint32_t));
|
---|
181 | *buf+=sizeof(uint32_t);
|
---|
182 | if(big_endian==true) data_raw=qFromBigEndian(data_raw);
|
---|
183 | SetTriangle(i,*data);
|
---|
184 | }
|
---|
185 | else if(data_type=="int8_t")
|
---|
186 | {
|
---|
187 | int8_t data;
|
---|
188 | memcpy((void*)&data,*buf,sizeof(data));
|
---|
189 | *buf+=sizeof(data);
|
---|
190 | SetTriangle(i,data);
|
---|
191 | }
|
---|
192 | else if(data_type=="int16_t")
|
---|
193 | {
|
---|
194 | int16_t data;
|
---|
195 | memcpy((void*)&data,*buf,sizeof(data));
|
---|
196 | *buf+=sizeof(data);
|
---|
197 | if(big_endian==true) data=qFromBigEndian(data);
|
---|
198 | SetTriangle(i,data);
|
---|
199 | }
|
---|
200 | else
|
---|
201 | {
|
---|
202 | printf("RangeFinderPlot::BufEvent unknown type %s\n",data_type.toLocal8Bit().constData());
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | plot->replot();
|
---|
207 | }
|
---|
208 |
|
---|
209 | void RangeFinderPlot::addTriangle(float angle_min, float angle_max)
|
---|
210 | {
|
---|
211 | QwtPlotShapeItem *item = new QwtPlotShapeItem();
|
---|
212 |
|
---|
213 | item->setRenderHint( QwtPlotItem::RenderAntialiased, true );
|
---|
214 | item->attach(plot);
|
---|
215 | triangles.append(item);
|
---|
216 |
|
---|
217 | }
|
---|
218 |
|
---|
219 | void RangeFinderPlot::SetTriangle(uint32_t id,float length)
|
---|
220 | {
|
---|
221 | QPainterPath path;
|
---|
222 | QPolygonF triangle;
|
---|
223 | float angle_min=start_angle+id*(end_angle-start_angle+1)/nb_samples;
|
---|
224 | float angle_max=start_angle+(id+1)*(end_angle-start_angle+1)/nb_samples;
|
---|
225 |
|
---|
226 | if(invert_axis) {
|
---|
227 | angle_min=90-angle_min;
|
---|
228 | angle_max=90-angle_max;
|
---|
229 | }
|
---|
230 |
|
---|
231 | triangle += QPointF( 0,0 );
|
---|
232 | triangle += QPointF( length*cos(angle_min*M_PI/180.),length*sin(angle_min*M_PI/180.) );
|
---|
233 | triangle += QPointF( length*cos(angle_max*M_PI/180.),length*sin(angle_max*M_PI/180.) );
|
---|
234 |
|
---|
235 | path.addPolygon( triangle );
|
---|
236 |
|
---|
237 | path.closeSubpath();
|
---|
238 | triangles.at(id)->setShape(path);
|
---|
239 |
|
---|
240 | QColor fillColor = "SandyBrown";
|
---|
241 | fillColor.setAlpha( 200 );
|
---|
242 |
|
---|
243 | QPen pen( QColor("SandyBrown"), 1 );
|
---|
244 | pen.setJoinStyle( Qt::MiterJoin );
|
---|
245 | triangles.at(id)->setPen( pen );
|
---|
246 | triangles.at(id)->setBrush( fillColor );
|
---|
247 | }
|
---|
248 |
|
---|
249 | //context menu
|
---|
250 | void RangeFinderPlot::mousePressEvent(QMouseEvent *event)
|
---|
251 | {
|
---|
252 | if (event->button() == Qt::RightButton)
|
---|
253 | {
|
---|
254 | QMenu *menu = new QMenu("nom", plot);
|
---|
255 | // ajout des actions
|
---|
256 | QAction *a,*z;
|
---|
257 |
|
---|
258 | a=menu->addAction("reset y zoom");
|
---|
259 |
|
---|
260 | appendmenu(menu);
|
---|
261 | z=execmenu(plot,menu,event->globalPos());
|
---|
262 | delete menu;
|
---|
263 |
|
---|
264 | if(z==a)
|
---|
265 | {
|
---|
266 | //zoom to original size
|
---|
267 | if(!invert_axis) {
|
---|
268 | plot->setAxisScale(QwtPlot::yLeft, ymin_orig, ymax_orig);
|
---|
269 | } else {
|
---|
270 | plot->setAxisScale(QwtPlot::yLeft, xmin_orig, xmax_orig);
|
---|
271 | }
|
---|
272 | plot->replot();
|
---|
273 | }
|
---|
274 | }
|
---|
275 | }
|
---|
276 |
|
---|
277 |
|
---|