plotmagnifier.cpp
Go to the documentation of this file.
1 /*
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5  */
6 
7 #include <limits>
8 #include <QDebug>
9 #include <QWheelEvent>
10 #include <QApplication>
11 
12 #include "plotmagnifier.h"
13 #include "qwt_plot.h"
14 #include "qwt_scale_map.h"
15 
17  : QwtPlotMagnifier(canvas), _default_mode(BOTH_AXES)
18 {
19  for (int axisId = 0; axisId < QwtPlot::axisCnt; axisId++)
20  {
21  _lower_bounds[axisId] = std::numeric_limits<double>::lowest();
22  _upper_bounds[axisId] = std::numeric_limits<double>::max();
23  }
24 }
25 
27 {
28 }
29 
30 void PlotMagnifier::setAxisLimits(int axis, double lower, double upper)
31 {
32  if (axis >= 0 && axis < QwtPlot::axisCnt)
33  {
34  _lower_bounds[axis] = lower;
35  _upper_bounds[axis] = upper;
36  }
37 }
38 
39 void PlotMagnifier::rescale(double factor, AxisMode axis)
40 {
41  factor = qAbs(1.0 / factor);
42 
43  QwtPlot* plt = plot();
44  if (plt == nullptr || factor == 1.0)
45  {
46  return;
47  }
48 
49  bool doReplot = false;
50 
51  const bool autoReplot = plt->autoReplot();
52  plt->setAutoReplot(false);
53 
54  const int axis_list[2] = { QwtPlot::xBottom, QwtPlot::yLeft };
55  QRectF new_rect;
56 
57  for (int i = 0; i < 2; i++)
58  {
59  double temp_factor = factor;
60  if (i == 1 && axis == X_AXIS)
61  {
62  temp_factor = 1.0;
63  }
64  if (i == 0 && axis == Y_AXIS)
65  {
66  temp_factor = 1.0;
67  }
68 
69  int axisId = axis_list[i];
70 
71  if (isAxisEnabled(axisId))
72  {
73  const QwtScaleMap scaleMap = plt->canvasMap(axisId);
74 
75  double v1 = scaleMap.s1();
76  double v2 = scaleMap.s2();
77  double center = _mouse_position.x();
78 
79  if (axisId == QwtPlot::yLeft)
80  {
81  center = _mouse_position.y();
82  }
83 
84  if (scaleMap.transformation())
85  {
86  // the coordinate system of the paint device is always linear
87  v1 = scaleMap.transform(v1); // scaleMap.p1()
88  v2 = scaleMap.transform(v2); // scaleMap.p2()
89  }
90 
91  const double width = (v2 - v1);
92  const double ratio = (v2 - center) / (width);
93 
94  v1 = center - width * temp_factor * (1 - ratio);
95  v2 = center + width * temp_factor * (ratio);
96 
97  bool reversed_axis = false;
98  if (v1 > v2)
99  {
100  reversed_axis = true;
101  std::swap(v1, v2);
102  }
103 
104  if (scaleMap.transformation())
105  {
106  v1 = scaleMap.invTransform(v1);
107  v2 = scaleMap.invTransform(v2);
108  }
109 
110  v1 = std::max(v1, _lower_bounds[axisId]);
111  v2 = std::min(v2, _upper_bounds[axisId]);
112 
113  if (reversed_axis)
114  {
115  plt->setAxisScale(axisId, v2, v1);
116  }
117  else
118  {
119  plt->setAxisScale(axisId, v1, v2);
120  }
121 
122  if (axisId == QwtPlot::xBottom)
123  {
124  new_rect.setLeft(v1);
125  new_rect.setRight(v2);
126  }
127  else
128  {
129  new_rect.setBottom(v1);
130  new_rect.setTop(v2);
131  }
132 
133  doReplot = true;
134  }
135  }
136 
137  plt->setAutoReplot(autoReplot);
138 
139  if (doReplot)
140  {
141  emit rescaled(new_rect);
142  }
143 }
144 
145 QPointF PlotMagnifier::invTransform(QPoint pos)
146 {
149  return QPointF(xMap.invTransform(pos.x()), yMap.invTransform(pos.y()));
150 }
151 
152 void PlotMagnifier::widgetWheelEvent(QWheelEvent* event)
153 {
154  _mouse_position = invTransform(event->pos());
156 }
157 
158 void PlotMagnifier::widgetMousePressEvent(QMouseEvent* event)
159 {
160  _mouse_position = invTransform(event->pos());
162 }
QwtScaleMap::invTransform
double invTransform(double p) const
Definition: qwt_scale_map.h:154
PlotMagnifier::setAxisLimits
void setAxisLimits(int axis, double lower, double upper)
Definition: plotmagnifier.cpp:30
QwtPlot
A 2-D plotting widget.
Definition: qwt_plot.h:78
QwtPlot::setAxisScale
void setAxisScale(QwtAxisId, double min, double max, double stepSize=0)
Disable autoscaling and specify a fixed scale for a selected axis.
Definition: qwt_plot_axis.cpp:477
QwtScaleMap::s2
double s2() const
Definition: qwt_scale_map.h:91
QwtScaleMap::s1
double s1() const
Definition: qwt_scale_map.h:83
PlotMagnifier::PlotMagnifier
PlotMagnifier(QWidget *canvas)
Definition: plotmagnifier.cpp:16
QwtPlot::setAutoReplot
void setAutoReplot(bool=true)
Set or reset the autoReplot option.
Definition: qwt_plot.cpp:310
PlotMagnifier::AxisMode
AxisMode
Definition: plotmagnifier.h:26
qwt_scale_map.h
QwtPlotMagnifier::plot
QwtPlot * plot()
Return plot widget, containing the observed plot canvas.
Definition: qwt_plot_magnifier.cpp:88
std::swap
NLOHMANN_BASIC_JSON_TPL_DECLARATION void swap(nlohmann::NLOHMANN_BASIC_JSON_TPL &j1, nlohmann::NLOHMANN_BASIC_JSON_TPL &j2) noexcept(//NOLINT(readability-inconsistent-declaration-parameter-name) is_nothrow_move_constructible< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value &&//NOLINT(misc-redundant-expression) is_nothrow_move_assignable< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value)
exchanges the values of two JSON objects
Definition: json.hpp:21884
PlotMagnifier::_upper_bounds
double _upper_bounds[QwtPlot::axisCnt]
Definition: plotmagnifier.h:49
PlotMagnifier::Y_AXIS
@ Y_AXIS
Definition: plotmagnifier.h:29
PlotMagnifier::rescale
virtual void rescale(double factor) override
Definition: plotmagnifier.h:33
PlotMagnifier::X_AXIS
@ X_AXIS
Definition: plotmagnifier.h:28
QwtScaleMap::transform
double transform(double s) const
Definition: qwt_scale_map.h:137
QwtPlot::autoReplot
bool autoReplot
Definition: qwt_plot.h:85
QwtScaleMap
A scale map.
Definition: qwt_scale_map.h:26
QwtPlot::yLeft
@ yLeft
Definition: qwt_plot.h:240
PlotMagnifier::widgetMousePressEvent
virtual void widgetMousePressEvent(QMouseEvent *event) override
Definition: plotmagnifier.cpp:158
QwtPlotMagnifier::isAxisEnabled
bool isAxisEnabled(QwtAxisId) const
Definition: qwt_plot_magnifier.cpp:67
QwtPlot::axisCnt
@ axisCnt
Definition: qwt_plot.h:245
QwtMagnifier::widgetWheelEvent
virtual void widgetWheelEvent(QWheelEvent *)
Definition: qwt_magnifier.cpp:431
PlotMagnifier::rescaled
void rescaled(QRectF new_size)
QwtMagnifier::widgetMousePressEvent
virtual void widgetMousePressEvent(QMouseEvent *)
Definition: qwt_magnifier.cpp:365
plotmagnifier.h
QwtPlot::xBottom
@ xBottom
Definition: qwt_plot.h:242
PlotMagnifier::invTransform
QPointF invTransform(QPoint pos)
Definition: plotmagnifier.cpp:145
PlotMagnifier::_mouse_position
QPointF _mouse_position
Definition: plotmagnifier.h:51
QwtScaleMap::transformation
const QwtTransform * transformation() const
Get the transformation.
Definition: qwt_scale_map.cpp:88
PlotMagnifier::_lower_bounds
double _lower_bounds[QwtPlot::axisCnt]
Definition: plotmagnifier.h:48
QwtPlot::canvasMap
virtual QwtScaleMap canvasMap(QwtAxisId) const
Definition: qwt_plot.cpp:800
PlotMagnifier::widgetWheelEvent
virtual void widgetWheelEvent(QWheelEvent *event) override
Definition: plotmagnifier.cpp:152
PlotMagnifier::~PlotMagnifier
virtual ~PlotMagnifier() override
Definition: plotmagnifier.cpp:26
qwt_plot.h
QwtPlotMagnifier
QwtPlotMagnifier provides zooming, by magnifying in steps.
Definition: qwt_plot_magnifier.h:30


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Aug 11 2024 02:24:23