PlotMagnifier.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 <cmath>
00020 
00021 #include <QMouseEvent>
00022 
00023 #include <qwt/qwt_plot.h>
00024 #include <qwt/qwt_plot_canvas.h>
00025 #include <qwt/qwt_scale_div.h>
00026 
00027 #include "rqt_multiplot/PlotMagnifier.h"
00028 
00029 namespace rqt_multiplot {
00030 
00031 /*****************************************************************************/
00032 /* Constructors and Destructor                                               */
00033 /*****************************************************************************/
00034 
00035 PlotMagnifier::PlotMagnifier(QwtPlotCanvas* canvas) :
00036   QwtPlotMagnifier(canvas),
00037   magnifying_(false) {
00038 }
00039 
00040 PlotMagnifier::~PlotMagnifier() {
00041 }
00042 
00043 /*****************************************************************************/
00044 /* Methods                                                                   */
00045 /*****************************************************************************/
00046 
00047 void PlotMagnifier::rescale(double xFactor, double yFactor) {
00048   double fx = std::fabs(xFactor);
00049   double fy = std::fabs(yFactor);
00050 
00051   if ((fx == 1.0) && (fy == 1.0))
00052     return;
00053 
00054   bool doReplot = false;
00055   bool autoReplot = plot()->autoReplot();
00056 
00057   plot()->setAutoReplot(false);
00058 
00059   #if QWT_VERSION >= 0x060100
00060     const QwtScaleDiv& xScaleDiv = plot()->axisScaleDiv(QwtPlot::xBottom);
00061     const QwtScaleDiv& yScaleDiv = plot()->axisScaleDiv(QwtPlot::yLeft);
00062   #else
00063     const QwtScaleDiv& xScaleDiv = *plot()->axisScaleDiv(QwtPlot::xBottom);
00064     const QwtScaleDiv& yScaleDiv = *plot()->axisScaleDiv(QwtPlot::yLeft);
00065   #endif
00066 
00067   #if QWT_VERSION < 0x060100
00068   if (xScaleDiv.isValid())
00069   #endif
00070   {
00071     double center = xScaleDiv.lowerBound()+0.5*xScaleDiv.range();
00072     double width = xScaleDiv.range()*fx;
00073 
00074     plot()->setAxisScale(QwtPlot::xBottom, center-0.5*width,
00075       center+0.5*width);
00076     doReplot = true;
00077   }
00078 
00079   #if QWT_VERSION < 0x060100
00080   if (yScaleDiv.isValid())
00081   #endif
00082   {
00083     double center = yScaleDiv.lowerBound()+0.5*yScaleDiv.range();
00084     double width = yScaleDiv.range()*fy;
00085 
00086     plot()->setAxisScale(QwtPlot::yLeft, center-0.5*width,
00087       center+0.5*width);
00088     doReplot = true;
00089   }
00090 
00091   plot()->setAutoReplot(autoReplot);
00092 
00093   if (doReplot)
00094     plot()->replot();
00095 }
00096 
00097 void PlotMagnifier::widgetMousePressEvent(QMouseEvent* event) {
00098   QwtPlotMagnifier::widgetMousePressEvent(event);
00099 
00100   #if QWT_VERSION >= 0x060100
00101     Qt::MouseButton button;
00102     Qt::KeyboardModifiers buttonState;
00103   #else
00104     int button, buttonState;
00105   #endif
00106 
00107   getMouseButton(button, buttonState);
00108 
00109   if (event->button() != button || !parentWidget())
00110     return;
00111 
00112   if ((event->modifiers() & Qt::KeyboardModifierMask) !=
00113       (int)(buttonState & Qt::KeyboardModifierMask))
00114     return;
00115 
00116   magnifying_ = true;
00117   position_ = event->pos();
00118 }
00119 
00120 void PlotMagnifier::widgetMouseMoveEvent(QMouseEvent* event) {
00121   if (!magnifying_)
00122     return;
00123 
00124   int dx = event->pos().x()-position_.x();
00125   int dy = event->pos().y()-position_.y();
00126 
00127   double fx = 1.0;
00128   double fy = 1.0;
00129 
00130   if (dx != 0) {
00131     fx = mouseFactor();
00132 
00133     if (dx < 0)
00134       fx = 1.0/fx;
00135   }
00136 
00137   if (dy != 0) {
00138     fy = mouseFactor();
00139 
00140     if (dy < 0)
00141       fy = 1.0/fy;
00142   }
00143 
00144   rescale(fx, fy);
00145 
00146   position_ = event->pos();
00147 }
00148 
00149 void PlotMagnifier::widgetMouseReleaseEvent(QMouseEvent* event) {
00150   QwtPlotMagnifier::widgetMouseReleaseEvent(event);
00151 
00152   magnifying_ = false;
00153 }
00154 
00155 }


rqt_multiplot
Author(s): Ralf Kaestner
autogenerated on Thu Jun 6 2019 21:49:11