PlotPanner.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (C) 2015 by Ralf Kaestner *
3  * ralf.kaestner@gmail.com *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the Lesser GNU General Public License as published by*
7  * the Free Software Foundation; either version 3 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * Lesser GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the Lesser GNU General Public License *
16  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17  ******************************************************************************/
18 
19 #include <QEvent>
20 #include <QMouseEvent>
21 
22 #include <qwt/qwt_plot.h>
23 #include <qwt/qwt_plot_canvas.h>
24 #include <qwt/qwt_scale_div.h>
25 
26 #include <ros/package.h>
27 
29 
30 namespace rqt_multiplot {
31 
32 /*****************************************************************************/
33 /* Constructors and Destructor */
34 /*****************************************************************************/
35 
36 PlotPanner::PlotPanner(QwtPlotCanvas* canvas) :
37  QObject(canvas),
38  canvas_(canvas),
39  panning_(false) {
40  cursor_ = QCursor(QPixmap(QString::fromStdString(ros::package::getPath(
41  "rqt_multiplot").append("/resource/23x23/move.png"))), 11, 11);
42 
43  if (canvas)
44  canvas->installEventFilter(this);
45 }
46 
48 }
49 
50 /*****************************************************************************/
51 /* Methods */
52 /*****************************************************************************/
53 
54 bool PlotPanner::eventFilter(QObject* object, QEvent* event) {
55  if (object == canvas_) {
56  if (!panning_ && (event->type() == QEvent::MouseButtonPress)) {
57  QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
58 
59  if (mouseEvent->button() == Qt::LeftButton) {
60  position_ = mouseEvent->pos();
61 
62  xMap_ = canvas_->plot()->canvasMap(QwtPlot::xBottom);
63  yMap_ = canvas_->plot()->canvasMap(QwtPlot::yLeft);
64 
65  #if QWT_VERSION >= 0x060100
66  QPointF minimum(
67  canvas_->plot()->axisScaleDiv(QwtPlot::xBottom).lowerBound(),
68  canvas_->plot()->axisScaleDiv(QwtPlot::yLeft).lowerBound());
69  QPointF maximum(
70  canvas_->plot()->axisScaleDiv(QwtPlot::xBottom).upperBound(),
71  canvas_->plot()->axisScaleDiv(QwtPlot::yLeft).upperBound());
72  #else
73  QPointF minimum(
74  canvas_->plot()->axisScaleDiv(QwtPlot::xBottom)->lowerBound(),
75  canvas_->plot()->axisScaleDiv(QwtPlot::yLeft)->lowerBound());
76  QPointF maximum(
77  canvas_->plot()->axisScaleDiv(QwtPlot::xBottom)->upperBound(),
78  canvas_->plot()->axisScaleDiv(QwtPlot::yLeft)->upperBound());
79  #endif
80 
81  bounds_.setMinimum(minimum);
82  bounds_.setMaximum(maximum);
83 
84  canvasCursor_ = canvas_->cursor();
85  canvas_->setCursor(cursor_);
86 
87  panning_ = true;
88  }
89  }
90  else if (panning_ && (event->type() == QEvent::MouseMove)) {
91  QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
92 
93  double dx = mouseEvent->pos().x()-position_.x();
94  double dy = mouseEvent->pos().y()-position_.y();
95 
96  QPointF minimum(
97  xMap_.invTransform(xMap_.transform(bounds_.getMinimum().x())-dx),
98  yMap_.invTransform(yMap_.transform(bounds_.getMinimum().y())-dy));
99  QPointF maximum(
100  xMap_.invTransform(xMap_.transform(bounds_.getMaximum().x())-dx),
101  yMap_.invTransform(yMap_.transform(bounds_.getMaximum().y())-dy));
102 
103  bool autoReplot = canvas_->plot()->autoReplot();
104  canvas_->plot()->setAutoReplot(false);
105 
106  canvas_->plot()->setAxisScale(QwtPlot::xBottom, minimum.x(),
107  maximum.x());
108  canvas_->plot()->setAxisScale(QwtPlot::yLeft, minimum.y(),
109  maximum.y());
110 
111  canvas_->plot()->setAutoReplot(autoReplot);
112  canvas_->plot()->replot();
113  }
114  else if (panning_ && (event->type() == QEvent::MouseButtonRelease)) {
115  canvas_->setCursor(canvasCursor_);
116 
117  panning_ = false;
118  }
119  }
120 
121  return false;
122 }
123 
124 }
bool eventFilter(QObject *object, QEvent *event)
Definition: PlotPanner.cpp:54
BoundingRectangle bounds_
Definition: PlotPanner.h:54
void setMaximum(const QPointF &maximum)
PlotPanner(QwtPlotCanvas *canvas)
Definition: PlotPanner.cpp:36
void setMinimum(const QPointF &minimum)
ROSLIB_DECL std::string getPath(const std::string &package_name)
ROSCPP_DECL std::string append(const std::string &left, const std::string &right)
QwtPlotCanvas * canvas_
Definition: PlotPanner.h:44


rqt_multiplot_plugin
Author(s): Ralf Kaestner
autogenerated on Fri Jan 15 2021 03:47:53