PlotMagnifier.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 <cmath>
20 
21 #include <QMouseEvent>
22 
23 #include <qwt/qwt_plot.h>
24 #include <qwt/qwt_plot_canvas.h>
25 #include <qwt/qwt_scale_div.h>
26 
28 
29 namespace rqt_multiplot {
30 
31 /*****************************************************************************/
32 /* Constructors and Destructor */
33 /*****************************************************************************/
34 
35 PlotMagnifier::PlotMagnifier(QwtPlotCanvas* canvas) :
36  QwtPlotMagnifier(canvas),
37  magnifying_(false) {
38 }
39 
41 }
42 
43 /*****************************************************************************/
44 /* Methods */
45 /*****************************************************************************/
46 
47 void PlotMagnifier::rescale(double xFactor, double yFactor) {
48  double fx = std::fabs(xFactor);
49  double fy = std::fabs(yFactor);
50 
51  if ((fx == 1.0) && (fy == 1.0))
52  return;
53 
54  bool doReplot = false;
55  bool autoReplot = plot()->autoReplot();
56 
57  plot()->setAutoReplot(false);
58 
59  #if QWT_VERSION >= 0x060100
60  const QwtScaleDiv& xScaleDiv = plot()->axisScaleDiv(QwtPlot::xBottom);
61  const QwtScaleDiv& yScaleDiv = plot()->axisScaleDiv(QwtPlot::yLeft);
62  #else
63  const QwtScaleDiv& xScaleDiv = *plot()->axisScaleDiv(QwtPlot::xBottom);
64  const QwtScaleDiv& yScaleDiv = *plot()->axisScaleDiv(QwtPlot::yLeft);
65  #endif
66 
67  #if QWT_VERSION < 0x060100
68  if (xScaleDiv.isValid())
69  #endif
70  {
71  double center = xScaleDiv.lowerBound()+0.5*xScaleDiv.range();
72  double width = xScaleDiv.range()*fx;
73 
74  plot()->setAxisScale(QwtPlot::xBottom, center-0.5*width,
75  center+0.5*width);
76  doReplot = true;
77  }
78 
79  #if QWT_VERSION < 0x060100
80  if (yScaleDiv.isValid())
81  #endif
82  {
83  double center = yScaleDiv.lowerBound()+0.5*yScaleDiv.range();
84  double width = yScaleDiv.range()*fy;
85 
86  plot()->setAxisScale(QwtPlot::yLeft, center-0.5*width,
87  center+0.5*width);
88  doReplot = true;
89  }
90 
91  plot()->setAutoReplot(autoReplot);
92 
93  if (doReplot)
94  plot()->replot();
95 }
96 
97 void PlotMagnifier::widgetMousePressEvent(QMouseEvent* event) {
98  QwtPlotMagnifier::widgetMousePressEvent(event);
99 
100  #if QWT_VERSION >= 0x060100
101  Qt::MouseButton button;
102  Qt::KeyboardModifiers buttonState;
103  #else
104  int button, buttonState;
105  #endif
106 
107  getMouseButton(button, buttonState);
108 
109  if (event->button() != button || !parentWidget())
110  return;
111 
112  if ((event->modifiers() & Qt::KeyboardModifierMask) !=
113  (int)(buttonState & Qt::KeyboardModifierMask))
114  return;
115 
116  magnifying_ = true;
117  position_ = event->pos();
118 }
119 
120 void PlotMagnifier::widgetMouseMoveEvent(QMouseEvent* event) {
121  if (!magnifying_)
122  return;
123 
124  int dx = event->pos().x()-position_.x();
125  int dy = event->pos().y()-position_.y();
126 
127  double fx = 1.0;
128  double fy = 1.0;
129 
130  if (dx != 0) {
131  fx = mouseFactor();
132 
133  if (dx < 0)
134  fx = 1.0/fx;
135  }
136 
137  if (dy != 0) {
138  fy = mouseFactor();
139 
140  if (dy < 0)
141  fy = 1.0/fy;
142  }
143 
144  rescale(fx, fy);
145 
146  position_ = event->pos();
147 }
148 
149 void PlotMagnifier::widgetMouseReleaseEvent(QMouseEvent* event) {
150  QwtPlotMagnifier::widgetMouseReleaseEvent(event);
151 
152  magnifying_ = false;
153 }
154 
155 }
void widgetMouseMoveEvent(QMouseEvent *event)
void rescale(double xFactor, double yFactor)
PlotMagnifier(QwtPlotCanvas *canvas)
void widgetMouseReleaseEvent(QMouseEvent *event)
void widgetMousePressEvent(QMouseEvent *event)


rqt_multiplot
Author(s): Ralf Kaestner
autogenerated on Wed Jul 10 2019 03:49:44