PlotPanner.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002  * Copyright (C) 2015 by Ralf Kaestner                                        *
00003  * ralf.kaestner@gmail.com                                                    *
00004  *                                                                            *
00005  * This program is free software; you can redistribute it and/or modify       *
00006  * it under the terms of the Lesser GNU General Public License as published by*
00007  * the Free Software Foundation; either version 3 of the License, or          *
00008  * (at your option) any later version.                                        *
00009  *                                                                            *
00010  * This program is distributed in the hope that it will be useful,            *
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the               *
00013  * Lesser GNU General Public License for more details.                        *
00014  *                                                                            *
00015  * You should have received a copy of the Lesser GNU General Public License   *
00016  * along with this program. If not, see <http://www.gnu.org/licenses/>.       *
00017  ******************************************************************************/
00018 
00019 #include <QEvent>
00020 #include <QMouseEvent>
00021 
00022 #include <qwt/qwt_plot.h>
00023 #include <qwt/qwt_plot_canvas.h>
00024 #include <qwt/qwt_scale_div.h>
00025 
00026 #include <ros/package.h>
00027 
00028 #include "rqt_multiplot/PlotPanner.h"
00029 
00030 namespace rqt_multiplot {
00031 
00032 /*****************************************************************************/
00033 /* Constructors and Destructor                                               */
00034 /*****************************************************************************/
00035 
00036 PlotPanner::PlotPanner(QwtPlotCanvas* canvas) :
00037   QObject(canvas),
00038   canvas_(canvas),
00039   panning_(false) {
00040   cursor_ = QCursor(QPixmap(QString::fromStdString(ros::package::getPath(
00041     "rqt_multiplot").append("/resource/23x23/move.png"))), 11, 11);
00042 
00043   if (canvas)
00044     canvas->installEventFilter(this);
00045 }
00046 
00047 PlotPanner::~PlotPanner() {
00048 }
00049 
00050 /*****************************************************************************/
00051 /* Methods                                                                   */
00052 /*****************************************************************************/
00053 
00054 bool PlotPanner::eventFilter(QObject* object, QEvent* event) {
00055   if (object == canvas_) {
00056     if (!panning_ && (event->type() == QEvent::MouseButtonPress)) {
00057       QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
00058 
00059       if (mouseEvent->button() == Qt::LeftButton) {
00060         position_ = mouseEvent->pos();
00061 
00062         xMap_ = canvas_->plot()->canvasMap(QwtPlot::xBottom);
00063         yMap_ = canvas_->plot()->canvasMap(QwtPlot::yLeft);
00064 
00065         #if QWT_VERSION >= 0x060100
00066           QPointF minimum(
00067             canvas_->plot()->axisScaleDiv(QwtPlot::xBottom).lowerBound(),
00068             canvas_->plot()->axisScaleDiv(QwtPlot::yLeft).lowerBound());
00069           QPointF maximum(
00070             canvas_->plot()->axisScaleDiv(QwtPlot::xBottom).upperBound(),
00071             canvas_->plot()->axisScaleDiv(QwtPlot::yLeft).upperBound());
00072         #else
00073           QPointF minimum(
00074             canvas_->plot()->axisScaleDiv(QwtPlot::xBottom)->lowerBound(),
00075             canvas_->plot()->axisScaleDiv(QwtPlot::yLeft)->lowerBound());
00076           QPointF maximum(
00077             canvas_->plot()->axisScaleDiv(QwtPlot::xBottom)->upperBound(),
00078             canvas_->plot()->axisScaleDiv(QwtPlot::yLeft)->upperBound());
00079         #endif
00080 
00081         bounds_.setMinimum(minimum);
00082         bounds_.setMaximum(maximum);
00083 
00084         canvasCursor_ = canvas_->cursor();
00085         canvas_->setCursor(cursor_);
00086 
00087         panning_ = true;
00088       }
00089     }
00090     else if (panning_ && (event->type() == QEvent::MouseMove)) {
00091       QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
00092 
00093       double dx = mouseEvent->pos().x()-position_.x();
00094       double dy = mouseEvent->pos().y()-position_.y();
00095 
00096       QPointF minimum(
00097         xMap_.invTransform(xMap_.transform(bounds_.getMinimum().x())-dx),
00098         yMap_.invTransform(yMap_.transform(bounds_.getMinimum().y())-dy));
00099       QPointF maximum(
00100         xMap_.invTransform(xMap_.transform(bounds_.getMaximum().x())-dx),
00101         yMap_.invTransform(yMap_.transform(bounds_.getMaximum().y())-dy));
00102 
00103       bool autoReplot = canvas_->plot()->autoReplot();
00104       canvas_->plot()->setAutoReplot(false);
00105 
00106       canvas_->plot()->setAxisScale(QwtPlot::xBottom, minimum.x(),
00107         maximum.x());
00108       canvas_->plot()->setAxisScale(QwtPlot::yLeft, minimum.y(),
00109         maximum.y());
00110 
00111       canvas_->plot()->setAutoReplot(autoReplot);
00112       canvas_->plot()->replot();
00113     }
00114     else if (panning_ && (event->type() == QEvent::MouseButtonRelease)) {
00115       canvas_->setCursor(canvasCursor_);
00116 
00117       panning_ = false;
00118     }
00119   }
00120 
00121   return false;
00122 }
00123 
00124 }


rqt_multiplot
Author(s): Ralf Kaestner
autogenerated on Tue May 9 2017 02:16:02