##// END OF EJS Templates
Improved zoom features on SocExplorer Plot.
jeandet -
r74:7e54838aea98 default
parent child
Show More
@@ -3,7 +3,7
3
3
4
4
5 SocExplorerPlot::SocExplorerPlot(QWidget *parent) :
5 SocExplorerPlot::SocExplorerPlot(QWidget *parent) :
6 QWidget(parent)
6 QWidget(parent), mRubberBand(new QRubberBand(QRubberBand::Rectangle, this))
7 {
7 {
8 this->m_plot = new QCustomPlot(this);
8 this->m_plot = new QCustomPlot(this);
9 this->m_plot->setInteractions(QCP::iRangeDrag | QCP::iSelectAxes |
9 this->m_plot->setInteractions(QCP::iRangeDrag | QCP::iSelectAxes |
@@ -21,7 +21,11 SocExplorerPlot::SocExplorerPlot(QWidget
21 this->mouse_hold = false;
21 this->mouse_hold = false;
22 this->m_plot->setNoAntialiasingOnDrag(true);
22 this->m_plot->setNoAntialiasingOnDrag(true);
23 this->show();
23 this->show();
24 }
24
25
26 SocExplorerPlot::~SocExplorerPlot()
27 {
28 delete mRubberBand;
25 }
29 }
26
30
27 void SocExplorerPlot::show()
31 void SocExplorerPlot::show()
@@ -29,6 +33,11 void SocExplorerPlot::show()
29 QWidget::show();
33 QWidget::show();
30 }
34 }
31
35
36 void SocExplorerPlot::replot()
37 {
38 this->m_plot->replot();
39 }
40
32 void SocExplorerPlot::setTitle(QString title)
41 void SocExplorerPlot::setTitle(QString title)
33 {
42 {
34 Q_UNUSED(title)
43 Q_UNUSED(title)
@@ -91,6 +100,20 int SocExplorerPlot::addGraph()
91 return this->m_plot->graphCount() -1;
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 void SocExplorerPlot::setGraphName(int graphIndex,QString name)
118 void SocExplorerPlot::setGraphName(int graphIndex,QString name)
96 {
119 {
@@ -290,6 +313,16 void SocExplorerPlot::setGraphScatterSty
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
@@ -300,6 +333,7 void SocExplorerPlot::keyPressEvent(QKey
300 {
333 {
301 case Qt::Key_Control:
334 case Qt::Key_Control:
302 this->ctrl_hold = true;
335 this->ctrl_hold = true;
336 setCursor(Qt::CrossCursor);
303 break;
337 break;
304 case Qt::Key_Shift:
338 case Qt::Key_Shift:
305 this->shift_hold = true;
339 this->shift_hold = true;
@@ -307,6 +341,46 void SocExplorerPlot::keyPressEvent(QKey
307 case Qt::Key_M:
341 case Qt::Key_M:
308 this->rescaleAxis();
342 this->rescaleAxis();
309 break;
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 default:
384 default:
311 QWidget::keyPressEvent(event);
385 QWidget::keyPressEvent(event);
312 break;
386 break;
@@ -329,6 +403,7 void SocExplorerPlot::keyReleaseEvent(QK
329 QWidget::keyReleaseEvent(event);
403 QWidget::keyReleaseEvent(event);
330 break;
404 break;
331 }
405 }
406 setCursor(Qt::ArrowCursor);
332 }
407 }
333
408
334 void SocExplorerPlot::wheelEvent(QWheelEvent * event)
409 void SocExplorerPlot::wheelEvent(QWheelEvent * event)
@@ -339,11 +414,10 void SocExplorerPlot::wheelEvent(QWheelE
339 {
414 {
340 if (event->orientation()==Qt::Vertical)//mRangeZoom.testFlag(Qt::Vertical))
415 if (event->orientation()==Qt::Vertical)//mRangeZoom.testFlag(Qt::Vertical))
341 {
416 {
417 setCursor(Qt::SizeVerCursor);
342 factor = pow(this->m_plot->axisRect()->rangeZoomFactor(Qt::Vertical), wheelSteps);
418 factor = pow(this->m_plot->axisRect()->rangeZoomFactor(Qt::Vertical), wheelSteps);
343 QCPAxis* axis = this->m_plot->axisRect()->rangeZoomAxis(Qt::Vertical);
419 zoom(factor,event->pos().y(),Qt::Vertical);
344 axis->scaleRange(factor, axis->pixelToCoord(event->pos().y()));
345 }
420 }
346 this->m_plot->replot();
347 QWidget::wheelEvent(event);
421 QWidget::wheelEvent(event);
348 return;
422 return;
349 }
423 }
@@ -351,18 +425,14 void SocExplorerPlot::wheelEvent(QWheelE
351 {
425 {
352 if (event->orientation()==Qt::Vertical)//mRangeZoom.testFlag(Qt::Vertical))
426 if (event->orientation()==Qt::Vertical)//mRangeZoom.testFlag(Qt::Vertical))
353 {
427 {
428 setCursor(Qt::SizeHorCursor);
354 factor = pow(this->m_plot->axisRect()->rangeZoomFactor(Qt::Horizontal), wheelSteps);
429 factor = pow(this->m_plot->axisRect()->rangeZoomFactor(Qt::Horizontal), wheelSteps);
355 QCPAxis* axis = this->m_plot->axisRect()->rangeZoomAxis(Qt::Horizontal);
430 zoom(factor,event->pos().x(),Qt::Horizontal);
356 axis->scaleRange(factor, axis->pixelToCoord(event->pos().x()));
357 }
431 }
358 this->m_plot->replot();
359 QWidget::wheelEvent(event);
432 QWidget::wheelEvent(event);
360 return;
433 return;
361 }
434 }
362 QCPAxis* Haxis = this->m_plot->axisRect()->rangeDragAxis(Qt::Horizontal);
435 move(wheelSteps/10,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();
366 QWidget::wheelEvent(event);
436 QWidget::wheelEvent(event);
367 }
437 }
368
438
@@ -373,11 +443,22 void SocExplorerPlot::mousePressEvent(QM
373 {
443 {
374 if(event->button()==Qt::LeftButton)
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 mDragStart = event->pos();
456 mDragStart = event->pos();
377 this->mouse_hold = true;
457 this->mouse_hold = true;
378 DragStartHorzRange = this->m_plot->axisRect()->rangeDragAxis(Qt::Horizontal)->range();
458 DragStartHorzRange = this->m_plot->axisRect()->rangeDragAxis(Qt::Horizontal)->range();
379 DragStartVertRange = this->m_plot->axisRect()->rangeDragAxis(Qt::Vertical)->range();
459 DragStartVertRange = this->m_plot->axisRect()->rangeDragAxis(Qt::Vertical)->range();
380 }
460 }
461 }
381 QWidget::mousePressEvent(event);
462 QWidget::mousePressEvent(event);
382 }
463 }
383
464
@@ -387,9 +468,42 void SocExplorerPlot::mouseReleaseEvent(
387 {
468 {
388 this->mouse_hold = false;
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 QWidget::mouseReleaseEvent(event);
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 void SocExplorerPlot::mouseMoveEvent(QMouseEvent *event)
507 void SocExplorerPlot::mouseMoveEvent(QMouseEvent *event)
394 {
508 {
395 if(mouse_hold)
509 if(mouse_hold)
@@ -402,6 +516,10 void SocExplorerPlot::mouseMoveEvent(QMo
402 Vaxis->setRange(DragStartVertRange.lower+diff, DragStartVertRange.upper+diff);
516 Vaxis->setRange(DragStartVertRange.lower+diff, DragStartVertRange.upper+diff);
403 this->m_plot->replot();
517 this->m_plot->replot();
404 }
518 }
519 if (mRubberBand->isVisible())
520 {
521 mRubberBand->setGeometry(QRect(mOrigin, event->pos()).normalized());
522 }
405 QWidget::mouseMoveEvent(event);
523 QWidget::mouseMoveEvent(event);
406 }
524 }
407
525
@@ -4,12 +4,15
4 #include <QWidget>
4 #include <QWidget>
5 #include <QGridLayout>
5 #include <QGridLayout>
6 #include <qcustomplot.h>
6 #include <qcustomplot.h>
7 #include <QRubberBand>
8 #include <QPoint>
7
9
8 class SocExplorerPlot : public QWidget
10 class SocExplorerPlot : public QWidget
9 {
11 {
10 Q_OBJECT
12 Q_OBJECT
11 public:
13 public:
12 explicit SocExplorerPlot(QWidget *parent = 0);
14 explicit SocExplorerPlot(QWidget *parent = 0);
15 ~SocExplorerPlot();
13 void setTitle(QString title);
16 void setTitle(QString title);
14 void setXaxisLabel(QString label);
17 void setXaxisLabel(QString label);
15 void setXaxisRange(double lower, double upper);
18 void setXaxisRange(double lower, double upper);
@@ -20,15 +23,21 public:
20 void setLegendSelectedFont(QFont font);
23 void setLegendSelectedFont(QFont font);
21 void setAdaptativeSampling(int graphIndex,bool enable);
24 void setAdaptativeSampling(int graphIndex,bool enable);
22 int addGraph();
25 int addGraph();
26 bool removeGraph(int graphIndex);
27 void removeAllGraphs();
23 void setGraphName(int graphIndex,QString name);
28 void setGraphName(int graphIndex,QString name);
24 void setGraphData(int graphIndex, QList<QVariant> x, QList<QVariant> y);
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 void addGraphData(int graphIndex, QList<QVariant> x, QList<QVariant> y);
31 void addGraphData(int graphIndex, QList<QVariant> x, QList<QVariant> y);
26 void addGraphData(int graphIndex, QVariant x, QVariant y);
32 void addGraphData(int graphIndex, QVariant x, QVariant y);
27 void setGraphPen(int graphIndex,QPen pen);
33 void setGraphPen(int graphIndex,QPen pen);
28 QPen getGraphPen(int graphIndex);
34 QPen getGraphPen(int graphIndex);
29 void setGraphLineStyle(int graphIndex,QString lineStyle);
35 void setGraphLineStyle(int graphIndex,QString lineStyle);
30 void setGraphScatterStyle(int graphIndex,QString scatterStyle);
36 void setGraphScatterStyle(int graphIndex,QString scatterStyle);
37 void setXaxisTickLabelType(QCPAxis::LabelType type);
38 void setXaxisDateTimeFormat(const QString &format);
31 void show();
39 void show();
40 void replot();
32
41
33 signals:
42 signals:
34
43
@@ -43,6 +52,8 protected:
43 void mouseReleaseEvent(QMouseEvent *);
52 void mouseReleaseEvent(QMouseEvent *);
44
53
45 private:
54 private:
55 void zoom(double factor, int center, Qt::Orientation orientation);
56 void move(double factor, Qt::Orientation orientation);
46 QCustomPlot* m_plot;
57 QCustomPlot* m_plot;
47 QGridLayout* m_mainlayout;
58 QGridLayout* m_mainlayout;
48 bool ctrl_hold;
59 bool ctrl_hold;
@@ -51,6 +62,9 private:
51 QCPRange DragStartHorzRange;
62 QCPRange DragStartHorzRange;
52 QCPRange DragStartVertRange;
63 QCPRange DragStartVertRange;
53 QPoint mDragStart;
64 QPoint mDragStart;
65 bool mZoomMode;
66 QRubberBand * mRubberBand;
67 QPoint mOrigin;
54 };
68 };
55
69
56 #endif // SOCEXPLORERPLOT_H
70 #endif // SOCEXPLORERPLOT_H
General Comments 0
You need to be logged in to leave comments. Login now