##// END OF EJS Templates
Improved zoom features on SocExplorer Plot.
jeandet -
r74:7e54838aea98 default
parent child
Show More
@@ -1,417 +1,535
1 1 #include "SocExplorerPlot.h"
2 2
3 3
4 4
5 5 SocExplorerPlot::SocExplorerPlot(QWidget *parent) :
6 QWidget(parent)
6 QWidget(parent), mRubberBand(new QRubberBand(QRubberBand::Rectangle, this))
7 7 {
8 8 this->m_plot = new QCustomPlot(this);
9 9 this->m_plot->setInteractions(QCP::iRangeDrag | QCP::iSelectAxes |
10 10 QCP::iSelectLegend | QCP::iSelectPlottables);
11 11 this->m_plot->axisRect()->setRangeDrag(Qt::Horizontal|Qt::Vertical);
12 12 this->m_plot->axisRect()->setRangeZoom(Qt::Horizontal|Qt::Vertical);
13 13 this->m_mainlayout = new QGridLayout(this);
14 14 this->setLayout(this->m_mainlayout);
15 15 this->m_mainlayout->addWidget(this->m_plot);
16 16 this->setMinimumSize(400,300);
17 17 this->setFocusPolicy(Qt::WheelFocus);
18 18 this->m_plot->setAttribute(Qt::WA_TransparentForMouseEvents);
19 19 this->ctrl_hold = false;
20 20 this->shift_hold = false;
21 21 this->mouse_hold = false;
22 22 this->m_plot->setNoAntialiasingOnDrag(true);
23 23 this->show();
24 }
24 25
26 SocExplorerPlot::~SocExplorerPlot()
27 {
28 delete mRubberBand;
25 29 }
26 30
27 31 void SocExplorerPlot::show()
28 32 {
29 33 QWidget::show();
30 34 }
31 35
36 void SocExplorerPlot::replot()
37 {
38 this->m_plot->replot();
39 }
40
32 41 void SocExplorerPlot::setTitle(QString title)
33 42 {
34 43 Q_UNUSED(title)
35 44 //this->m_plot->setTitle(title);
36 45 /*!
37 46 @todo Function borcken fixe this!
38 47 */
39 48 this->repaint();
40 49 }
41 50
42 51 void SocExplorerPlot::setXaxisLabel(QString label)
43 52 {
44 53 this->m_plot->xAxis->setLabel(label);
45 54 this->repaint();
46 55 }
47 56
48 57 void SocExplorerPlot::setYaxisLabel(QString label)
49 58 {
50 59 this->m_plot->yAxis->setLabel(label);
51 60 this->repaint();
52 61 }
53 62
54 63 void SocExplorerPlot::setXaxisRange(double lower, double upper)
55 64 {
56 65 this->m_plot->xAxis->setRange(lower,upper);
57 66 }
58 67
59 68 void SocExplorerPlot::setYaxisRange(double lower, double upper)
60 69 {
61 70 this->m_plot->yAxis->setRange(lower,upper);
62 71 }
63 72
64 73
65 74 void SocExplorerPlot::rescaleAxis()
66 75 {
67 76 this->m_plot->rescaleAxes();
68 77 this->m_plot->replot();
69 78 }
70 79
71 80 void SocExplorerPlot::setLegendFont(QFont font)
72 81 {
73 82 this->m_plot->legend->setFont(font);
74 83 this->repaint();
75 84 }
76 85
77 86 void SocExplorerPlot::setLegendSelectedFont(QFont font)
78 87 {
79 88 this->m_plot->legend->setSelectedFont(font);
80 89 this->repaint();
81 90 }
82 91
83 92 void SocExplorerPlot::setAdaptativeSampling(int graphIndex, bool enable)
84 93 {
85 94 this->m_plot->graph(graphIndex)->setAdaptiveSampling(enable);
86 95 }
87 96
88 97 int SocExplorerPlot::addGraph()
89 98 {
90 99 this->m_plot->addGraph();
91 100 return this->m_plot->graphCount() -1;
92 101 }
93 102
103 bool SocExplorerPlot::removeGraph(int graphIndex)
104 {
105 return this->m_plot->removeGraph(graphIndex);
106 }
107
108 void SocExplorerPlot::removeAllGraphs()
109 {
110 int graphCount=this->m_plot->graphCount();
111 for(int i=0;i<graphCount;i++)
112 {
113 this->m_plot->removeGraph(0);
114 }
115 }
116
94 117
95 118 void SocExplorerPlot::setGraphName(int graphIndex,QString name)
96 119 {
97 120 if(graphIndex<this->m_plot->graphCount())
98 121 {
99 122 this->m_plot->graph(graphIndex)->setName(name);
100 123 }
101 124 }
102 125
103 126
104 127 void SocExplorerPlot::setGraphData(int graphIndex, QList<QVariant> x, QList<QVariant> y)
105 128 {
106 129 if((graphIndex<this->m_plot->graphCount()) && (x.count()==y.count()))// && (x.at(0).type()==QVariant::Double))
107 130 {
108 131 QVector<double> _x(x.count()), _y(y.count());
109 132 for(int i=0;i<x.count();i++)
110 133 {
111 134 /*_x[i] = x.at(i).value<double>();
112 135 _y[i] = y.at(i).value<double>();*/
113 136 _x[i] = x.at(i).toDouble();
114 137 _y[i] = y.at(i).toDouble();
115 138 }
116 139 this->m_plot->graph(graphIndex)->setData(_x,_y);
117 140 }
118 141 this->m_plot->replot();
119 142 }
120 143
121 144 void SocExplorerPlot::addGraphData(int graphIndex, QList<QVariant> x, QList<QVariant> y)
122 145 {
123 146 if((graphIndex<this->m_plot->graphCount()) && (x.count()==y.count()))// && (x.at(0).type()==QVariant::Double))
124 147 {
125 148 QVector<double> _x(x.count()), _y(y.count());
126 149 for(int i=0;i<x.count();i++)
127 150 {
128 151 /*_x[i] = x.at(i).value<double>();
129 152 _y[i] = y.at(i).value<double>();*/
130 153 _x[i] = x.at(i).toDouble();
131 154 _y[i] = y.at(i).toDouble();
132 155 }
133 156 this->m_plot->graph(graphIndex)->addData(_x,_y);
134 157 }
135 158 this->m_plot->replot();
136 159 }
137 160
138 161 void SocExplorerPlot::addGraphData(int graphIndex, QVariant x, QVariant y)
139 162 {
140 163 if(graphIndex<this->m_plot->graphCount())// && (x.at(0).type()==QVariant::Double))
141 164 {
142 165 this->m_plot->graph(graphIndex)->addData(x.toDouble(),y.toDouble());
143 166 }
144 167 this->m_plot->replot();
145 168 }
146 169
147 170 void SocExplorerPlot::setGraphPen(int graphIndex,QPen pen)
148 171 {
149 172 if(graphIndex<this->m_plot->graphCount())
150 173 {
151 174 this->m_plot->graph(graphIndex)->setPen(pen);
152 175 }
153 176 }
154 177
155 178 QPen SocExplorerPlot::getGraphPen(int graphIndex)
156 179 {
157 180 if(graphIndex<this->m_plot->graphCount())
158 181 {
159 182 return this->m_plot->graph(graphIndex)->pen();
160 183 }
161 184 return this->m_plot->graph()->pen();
162 185 }
163 186
164 187
165 188
166 189 void SocExplorerPlot::setGraphLineStyle(int graphIndex,QString lineStyle)
167 190 {
168 191 if(graphIndex<this->m_plot->graphCount())
169 192 {
170 193 if(!lineStyle.compare("none"))
171 194 {
172 195 this->m_plot->graph(graphIndex)->setLineStyle(QCPGraph::lsNone);
173 196 return;
174 197 }
175 198 if(!lineStyle.compare("line"))
176 199 {
177 200 this->m_plot->graph(graphIndex)->setLineStyle(QCPGraph::lsLine);
178 201 return;
179 202 }
180 203 if(!lineStyle.compare("stepleft"))
181 204 {
182 205 this->m_plot->graph(graphIndex)->setLineStyle(QCPGraph::lsStepLeft);
183 206 return;
184 207 }
185 208 if(!lineStyle.compare("stepright"))
186 209 {
187 210 this->m_plot->graph(graphIndex)->setLineStyle(QCPGraph::lsStepRight);
188 211 return;
189 212 }
190 213 if(!lineStyle.compare("stepcenter"))
191 214 {
192 215 this->m_plot->graph(graphIndex)->setLineStyle(QCPGraph::lsStepCenter);
193 216 return;
194 217 }
195 218 if(!lineStyle.compare("impulse"))
196 219 {
197 220 this->m_plot->graph(graphIndex)->setLineStyle(QCPGraph::lsImpulse);
198 221 return;
199 222 }
200 223
201 224
202 225 }
203 226 }
204 227
205 228 void SocExplorerPlot::setGraphScatterStyle(int graphIndex,QString scatterStyle)
206 229 {
207 230 if(graphIndex<this->m_plot->graphCount())
208 231 {
209 232 if(!scatterStyle.compare("none"))
210 233 {
211 234 this->m_plot->graph(graphIndex)->setScatterStyle(QCPScatterStyle::ssNone);
212 235 return;
213 236 }
214 237 if(!scatterStyle.compare("dot"))
215 238 {
216 239 this->m_plot->graph(graphIndex)->setScatterStyle(QCPScatterStyle::ssDot);
217 240 return;
218 241 }
219 242 if(!scatterStyle.compare("cross"))
220 243 {
221 244 this->m_plot->graph(graphIndex)->setScatterStyle(QCPScatterStyle::ssCross);
222 245 return;
223 246 }
224 247 if(!scatterStyle.compare("plus"))
225 248 {
226 249 this->m_plot->graph(graphIndex)->setScatterStyle(QCPScatterStyle::ssPlus);
227 250 return;
228 251 }
229 252 if(!scatterStyle.compare("circle"))
230 253 {
231 254 this->m_plot->graph(graphIndex)->setScatterStyle(QCPScatterStyle::ssCircle);
232 255 return;
233 256 }
234 257 if(!scatterStyle.compare("disc"))
235 258 {
236 259 this->m_plot->graph(graphIndex)->setScatterStyle(QCPScatterStyle::ssDisc);
237 260 return;
238 261 }
239 262 if(!scatterStyle.compare("square"))
240 263 {
241 264 this->m_plot->graph(graphIndex)->setScatterStyle(QCPScatterStyle::ssSquare);
242 265 return;
243 266 }
244 267 if(!scatterStyle.compare("diamond"))
245 268 {
246 269 this->m_plot->graph(graphIndex)->setScatterStyle(QCPScatterStyle::ssDiamond);
247 270 return;
248 271 }
249 272 if(!scatterStyle.compare("star"))
250 273 {
251 274 this->m_plot->graph(graphIndex)->setScatterStyle(QCPScatterStyle::ssStar);
252 275 return;
253 276 }
254 277 if(!scatterStyle.compare("triangle"))
255 278 {
256 279 this->m_plot->graph(graphIndex)->setScatterStyle(QCPScatterStyle::ssTriangle);
257 280 return;
258 281 }
259 282 if(!scatterStyle.compare("invertedtriangle"))
260 283 {
261 284 this->m_plot->graph(graphIndex)->setScatterStyle(QCPScatterStyle::ssTriangleInverted);
262 285 return;
263 286 }
264 287 if(!scatterStyle.compare("crosssquare"))
265 288 {
266 289 this->m_plot->graph(graphIndex)->setScatterStyle(QCPScatterStyle::ssCrossSquare);
267 290 return;
268 291 }
269 292 if(!scatterStyle.compare("plussquare"))
270 293 {
271 294 this->m_plot->graph(graphIndex)->setScatterStyle(QCPScatterStyle::ssPlusSquare);
272 295 return;
273 296 }
274 297 if(!scatterStyle.compare("crosscircle"))
275 298 {
276 299 this->m_plot->graph(graphIndex)->setScatterStyle(QCPScatterStyle::ssCrossCircle);
277 300 return;
278 301 }
279 302 if(!scatterStyle.compare("pluscircle"))
280 303 {
281 304 this->m_plot->graph(graphIndex)->setScatterStyle(QCPScatterStyle::ssPlusCircle);
282 305 return;
283 306 }
284 307 if(!scatterStyle.compare("peace"))
285 308 {
286 309 this->m_plot->graph(graphIndex)->setScatterStyle(QCPScatterStyle::ssPeace);
287 310 return;
288 311 }
289 312
290 313 }
291 314 }
292 315
316 void SocExplorerPlot::setXaxisTickLabelType(QCPAxis::LabelType type)
317 {
318 this->m_plot->xAxis->setTickLabelType(type);
319 }
320
321 void SocExplorerPlot::setXaxisDateTimeFormat(const QString &format)
322 {
323 this->m_plot->xAxis->setDateTimeFormat(format);
324 }
325
293 326
294 327
295 328
296 329
297 330 void SocExplorerPlot::keyPressEvent(QKeyEvent * event)
298 331 {
299 332 switch(event->key())
300 333 {
301 334 case Qt::Key_Control:
302 335 this->ctrl_hold = true;
336 setCursor(Qt::CrossCursor);
303 337 break;
304 338 case Qt::Key_Shift:
305 339 this->shift_hold = true;
306 340 break;
307 341 case Qt::Key_M:
308 342 this->rescaleAxis();
309 343 break;
344 case Qt::Key_Left:
345 if(!ctrl_hold)
346 {
347 move(-0.1,Qt::Horizontal);
348 }
349 else
350 {
351 zoom(2,this->width()/2,Qt::Horizontal);
352 }
353 break;
354 case Qt::Key_Right:
355 if(!ctrl_hold)
356 {
357 move(0.1,Qt::Horizontal);
358 }
359 else
360 {
361 zoom(0.5,this->width()/2,Qt::Horizontal);
362 }
363 break;
364 case Qt::Key_Up:
365 if(!ctrl_hold)
366 {
367 move(0.1,Qt::Vertical);
368 }
369 else
370 {
371 zoom(0.5,this->height()/2,Qt::Vertical);
372 }
373 break;
374 case Qt::Key_Down:
375 if(!ctrl_hold)
376 {
377 move(-0.1,Qt::Vertical);
378 }
379 else
380 {
381 zoom(2,this->height()/2,Qt::Vertical);
382 }
383 break;
310 384 default:
311 385 QWidget::keyPressEvent(event);
312 386 break;
313 387 }
314 388 }
315 389
316 390 void SocExplorerPlot::keyReleaseEvent(QKeyEvent * event)
317 391 {
318 392 switch(event->key())
319 393 {
320 394 case Qt::Key_Control:
321 395 event->accept();
322 396 this->ctrl_hold = false;
323 397 break;
324 398 case Qt::Key_Shift:
325 399 event->accept();
326 400 this->shift_hold = false;
327 401 break;
328 402 default:
329 403 QWidget::keyReleaseEvent(event);
330 404 break;
331 405 }
406 setCursor(Qt::ArrowCursor);
332 407 }
333 408
334 409 void SocExplorerPlot::wheelEvent(QWheelEvent * event)
335 410 {
336 411 double factor;
337 412 double wheelSteps = event->delta()/120.0; // a single step delta is +/-120 usually
338 413 if(ctrl_hold)
339 414 {
340 415 if (event->orientation()==Qt::Vertical)//mRangeZoom.testFlag(Qt::Vertical))
341 416 {
417 setCursor(Qt::SizeVerCursor);
342 418 factor = pow(this->m_plot->axisRect()->rangeZoomFactor(Qt::Vertical), wheelSteps);
343 QCPAxis* axis = this->m_plot->axisRect()->rangeZoomAxis(Qt::Vertical);
344 axis->scaleRange(factor, axis->pixelToCoord(event->pos().y()));
419 zoom(factor,event->pos().y(),Qt::Vertical);
345 420 }
346 this->m_plot->replot();
347 421 QWidget::wheelEvent(event);
348 422 return;
349 423 }
350 424 if(shift_hold)
351 425 {
352 426 if (event->orientation()==Qt::Vertical)//mRangeZoom.testFlag(Qt::Vertical))
353 427 {
428 setCursor(Qt::SizeHorCursor);
354 429 factor = pow(this->m_plot->axisRect()->rangeZoomFactor(Qt::Horizontal), wheelSteps);
355 QCPAxis* axis = this->m_plot->axisRect()->rangeZoomAxis(Qt::Horizontal);
356 axis->scaleRange(factor, axis->pixelToCoord(event->pos().x()));
430 zoom(factor,event->pos().x(),Qt::Horizontal);
357 431 }
358 this->m_plot->replot();
359 432 QWidget::wheelEvent(event);
360 433 return;
361 434 }
362 QCPAxis* Haxis = this->m_plot->axisRect()->rangeDragAxis(Qt::Horizontal);
363 double rg = (Haxis->range().upper - Haxis->range().lower)*(wheelSteps/10);
364 Haxis->setRange(Haxis->range().lower+(rg), Haxis->range().upper+(rg));
365 this->m_plot->replot();
435 move(wheelSteps/10,Qt::Horizontal);
366 436 QWidget::wheelEvent(event);
367 437 }
368 438
369 439
370 440
371 441
372 442 void SocExplorerPlot::mousePressEvent(QMouseEvent *event)
373 443 {
374 444 if(event->button()==Qt::LeftButton)
375 445 {
446 if(ctrl_hold)
447 {
448 setCursor(Qt::CrossCursor);
449 mOrigin = event->pos();
450 mRubberBand->setGeometry(QRect(mOrigin, QSize()));
451 mRubberBand->show();
452 }
453 else
454 {
455 setCursor(Qt::ClosedHandCursor);
376 456 mDragStart = event->pos();
377 457 this->mouse_hold = true;
378 458 DragStartHorzRange = this->m_plot->axisRect()->rangeDragAxis(Qt::Horizontal)->range();
379 459 DragStartVertRange = this->m_plot->axisRect()->rangeDragAxis(Qt::Vertical)->range();
380 460 }
461 }
381 462 QWidget::mousePressEvent(event);
382 463 }
383 464
384 465 void SocExplorerPlot::mouseReleaseEvent(QMouseEvent *event)
385 466 {
386 467 if(event->button()==Qt::LeftButton)
387 468 {
388 469 this->mouse_hold = false;
389 470 }
471 if (mRubberBand->isVisible())
472 {
473 const QRect & zoomRect = mRubberBand->geometry();
474 int xp1, yp1, xp2, yp2;
475 zoomRect.getCoords(&xp1, &yp1, &xp2, &yp2);
476 double x1 = this->m_plot->xAxis->pixelToCoord(xp1);
477 double x2 = this->m_plot->xAxis->pixelToCoord(xp2);
478 double y1 = this->m_plot->yAxis->pixelToCoord(yp1);
479 double y2 = this->m_plot->yAxis->pixelToCoord(yp2);
480
481 this->m_plot->xAxis->setRange(x1, x2);
482 this->m_plot->yAxis->setRange(y1, y2);
483
484 mRubberBand->hide();
485 this->m_plot->replot();
486 }
487 setCursor(Qt::ArrowCursor);
390 488 QWidget::mouseReleaseEvent(event);
391 489 }
392 490
491 void SocExplorerPlot::zoom(double factor, int center, Qt::Orientation orientation)
492 {
493 QCPAxis* axis = this->m_plot->axisRect()->rangeZoomAxis(orientation);
494 axis->scaleRange(factor, axis->pixelToCoord(center));
495 this->m_plot->replot();
496 }
497
498 void SocExplorerPlot::move(double factor, Qt::Orientation orientation)
499 {
500 QCPAxis* axis = this->m_plot->axisRect()->rangeDragAxis(orientation);
501 double rg = (axis->range().upper - axis->range().lower)*(factor);
502 axis->setRange(axis->range().lower+(rg), axis->range().upper+(rg));
503 this->m_plot->replot();
504 }
505
506
393 507 void SocExplorerPlot::mouseMoveEvent(QMouseEvent *event)
394 508 {
395 509 if(mouse_hold)
396 510 {
397 511 QCPAxis* Haxis = this->m_plot->axisRect()->rangeDragAxis(Qt::Horizontal);
398 512 QCPAxis* Vaxis = this->m_plot->axisRect()->rangeDragAxis(Qt::Vertical);
399 513 double diff = Haxis->pixelToCoord(mDragStart.x()) - Haxis->pixelToCoord(event->pos().x());
400 514 Haxis->setRange(DragStartHorzRange.lower+diff, DragStartHorzRange.upper+diff);
401 515 diff = Vaxis->pixelToCoord(mDragStart.y()) - Vaxis->pixelToCoord(event->pos().y());
402 516 Vaxis->setRange(DragStartVertRange.lower+diff, DragStartVertRange.upper+diff);
403 517 this->m_plot->replot();
404 518 }
519 if (mRubberBand->isVisible())
520 {
521 mRubberBand->setGeometry(QRect(mOrigin, event->pos()).normalized());
522 }
405 523 QWidget::mouseMoveEvent(event);
406 524 }
407 525
408 526
409 527
410 528
411 529
412 530
413 531
414 532
415 533
416 534
417 535
@@ -1,56 +1,70
1 1 #ifndef SOCEXPLORERPLOT_H
2 2 #define SOCEXPLORERPLOT_H
3 3
4 4 #include <QWidget>
5 5 #include <QGridLayout>
6 6 #include <qcustomplot.h>
7 #include <QRubberBand>
8 #include <QPoint>
7 9
8 10 class SocExplorerPlot : public QWidget
9 11 {
10 12 Q_OBJECT
11 13 public:
12 14 explicit SocExplorerPlot(QWidget *parent = 0);
15 ~SocExplorerPlot();
13 16 void setTitle(QString title);
14 17 void setXaxisLabel(QString label);
15 18 void setXaxisRange(double lower, double upper);
16 19 void setYaxisLabel(QString label);
17 20 void setYaxisRange(double lower, double upper);
18 21 void rescaleAxis();
19 22 void setLegendFont(QFont font);
20 23 void setLegendSelectedFont(QFont font);
21 24 void setAdaptativeSampling(int graphIndex,bool enable);
22 25 int addGraph();
26 bool removeGraph(int graphIndex);
27 void removeAllGraphs();
23 28 void setGraphName(int graphIndex,QString name);
24 29 void setGraphData(int graphIndex, QList<QVariant> x, QList<QVariant> y);
30 void setGraphData(int graphIndex, QCPDataMap* data,bool copy = true,bool replot=true);
25 31 void addGraphData(int graphIndex, QList<QVariant> x, QList<QVariant> y);
26 32 void addGraphData(int graphIndex, QVariant x, QVariant y);
27 33 void setGraphPen(int graphIndex,QPen pen);
28 34 QPen getGraphPen(int graphIndex);
29 35 void setGraphLineStyle(int graphIndex,QString lineStyle);
30 36 void setGraphScatterStyle(int graphIndex,QString scatterStyle);
37 void setXaxisTickLabelType(QCPAxis::LabelType type);
38 void setXaxisDateTimeFormat(const QString &format);
31 39 void show();
40 void replot();
32 41
33 42 signals:
34 43
35 44 public slots:
36 45
37 46 protected:
38 47 void keyPressEvent(QKeyEvent *);
39 48 void keyReleaseEvent(QKeyEvent *);
40 49 void wheelEvent(QWheelEvent *);
41 50 void mousePressEvent(QMouseEvent *);
42 51 void mouseMoveEvent(QMouseEvent *);
43 52 void mouseReleaseEvent(QMouseEvent *);
44 53
45 54 private:
55 void zoom(double factor, int center, Qt::Orientation orientation);
56 void move(double factor, Qt::Orientation orientation);
46 57 QCustomPlot* m_plot;
47 58 QGridLayout* m_mainlayout;
48 59 bool ctrl_hold;
49 60 bool shift_hold;
50 61 bool mouse_hold;
51 62 QCPRange DragStartHorzRange;
52 63 QCPRange DragStartVertRange;
53 64 QPoint mDragStart;
65 bool mZoomMode;
66 QRubberBand * mRubberBand;
67 QPoint mOrigin;
54 68 };
55 69
56 70 #endif // SOCEXPLORERPLOT_H
General Comments 0
You need to be logged in to leave comments. Login now