plotmagnifier.cpp
Go to the documentation of this file.
1 #include <limits>
2 #include <QDebug>
3 #include <QWheelEvent>
4 #include <QApplication>
5 #include "plotmagnifier.h"
6 #include "qwt_plot.h"
7 #include "qwt_scale_map.h"
8 
9 PlotMagnifier::PlotMagnifier(QWidget* canvas) : QwtPlotMagnifier(canvas), _default_mode(BOTH_AXES)
10 {
11  for (int axisId = 0; axisId < QwtPlot::axisCnt; axisId++)
12  {
15  }
16 }
17 
19 {
20 }
21 
22 void PlotMagnifier::setAxisLimits(int axis, double lower, double upper)
23 {
24  if (axis >= 0 && axis < QwtPlot::axisCnt)
25  {
26  _lower_bounds[axis] = lower;
27  _upper_bounds[axis] = upper;
28  }
29 }
30 
31 void PlotMagnifier::rescale(double factor, AxisMode axis)
32 {
33  factor = qAbs(1.0 / factor);
34 
35  QwtPlot* plt = plot();
36  if (plt == nullptr || factor == 1.0)
37  {
38  return;
39  }
40 
41  bool doReplot = false;
42 
43  const bool autoReplot = plt->autoReplot();
44  plt->setAutoReplot(false);
45 
46  const int axis_list[2] = { QwtPlot::xBottom, QwtPlot::yLeft };
47  QRectF new_rect;
48 
49  for (int i = 0; i < 2; i++)
50  {
51  double temp_factor = factor;
52  if (i == 1 && axis == X_AXIS)
53  {
54  temp_factor = 1.0;
55  }
56  if (i == 0 && axis == Y_AXIS)
57  {
58  temp_factor = 1.0;
59  }
60 
61  int axisId = axis_list[i];
62 
63  if (isAxisEnabled(axisId))
64  {
65  const QwtScaleMap scaleMap = plt->canvasMap(axisId);
66 
67  double v1 = scaleMap.s1();
68  double v2 = scaleMap.s2();
69  double center = _mouse_position.x();
70 
71  if (axisId == QwtPlot::yLeft)
72  {
73  center = _mouse_position.y();
74  }
75 
76  if (scaleMap.transformation())
77  {
78  // the coordinate system of the paint device is always linear
79  v1 = scaleMap.transform(v1); // scaleMap.p1()
80  v2 = scaleMap.transform(v2); // scaleMap.p2()
81  }
82 
83  const double width = (v2 - v1);
84  const double ratio = (v2 - center) / (width);
85 
86  v1 = center - width * temp_factor * (1 - ratio);
87  v2 = center + width * temp_factor * (ratio);
88 
89  if (v1 > v2)
90  std::swap(v1, v2);
91 
92  if (scaleMap.transformation())
93  {
94  v1 = scaleMap.invTransform(v1);
95  v2 = scaleMap.invTransform(v2);
96  }
97 
98  if (v1 < _lower_bounds[axisId])
99  v1 = _lower_bounds[axisId];
100  if (v2 > _upper_bounds[axisId])
101  v2 = _upper_bounds[axisId];
102 
103  plt->setAxisScale(axisId, v1, v2);
104 
105  if (axisId == QwtPlot::xBottom)
106  {
107  new_rect.setLeft(v1);
108  new_rect.setRight(v2);
109  }
110  else
111  {
112  new_rect.setBottom(v1);
113  new_rect.setTop(v2);
114  }
115 
116  doReplot = true;
117  }
118  }
119 
120  plt->setAutoReplot(autoReplot);
121 
122  if (doReplot)
123  {
124  emit rescaled(new_rect);
125  }
126 }
127 
128 QPointF PlotMagnifier::invTransform(QPoint pos)
129 {
132  return QPointF(xMap.invTransform(pos.x()), yMap.invTransform(pos.y()));
133 }
134 
135 void PlotMagnifier::widgetWheelEvent(QWheelEvent* event)
136 {
137  _mouse_position = invTransform(event->pos());
139 }
140 
141 void PlotMagnifier::widgetMousePressEvent(QMouseEvent* event)
142 {
143  _mouse_position = invTransform(event->pos());
145 }
virtual void widgetMousePressEvent(QMouseEvent *)
virtual void rescale(double factor) override
Definition: plotmagnifier.h:27
double s1() const
Definition: qwt_scale_map.h:83
QwtPlotMagnifier provides zooming, by magnifying in steps.
virtual void widgetWheelEvent(QWheelEvent *)
Number of axes.
Definition: qwt_plot.h:109
QPointF invTransform(QPoint pos)
void setAutoReplot(bool=true)
Set or reset the autoReplot option.
Definition: qwt_plot.cpp:307
virtual void widgetMousePressEvent(QMouseEvent *event) override
double _upper_bounds[QwtPlot::axisCnt]
Definition: plotmagnifier.h:43
A 2-D plotting widget.
Definition: qwt_plot.h:75
Y axis left of the canvas.
Definition: qwt_plot.h:97
QwtPlot * plot()
Return plot widget, containing the observed plot canvas.
double s2() const
Definition: qwt_scale_map.h:91
double _lower_bounds[QwtPlot::axisCnt]
Definition: plotmagnifier.h:42
QPointF _mouse_position
Definition: plotmagnifier.h:45
virtual void widgetWheelEvent(QWheelEvent *event) override
virtual ~PlotMagnifier() override
#define max(A, B)
Definition: Socket.h:88
bool autoReplot
Definition: qwt_plot.h:81
bool isAxisEnabled(int axis) const
virtual QwtScaleMap canvasMap(int axisId) const
Definition: qwt_plot.cpp:786
void rescaled(QRectF new_size)
char upper
Definition: utf-8.c:50
const QwtTransform * transformation() const
Get the transformation.
A scale map.
Definition: qwt_scale_map.h:26
double invTransform(double p) const
void setAxisScale(int axisId, double min, double max, double stepSize=0)
Disable autoscaling and specify a fixed scale for a selected axis.
void setAxisLimits(int axis, double lower, double upper)
PlotMagnifier(QWidget *canvas)
double transform(double s) const
char lower
Definition: utf-8.c:49
X axis below the canvas.
Definition: qwt_plot.h:103


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 03:48:10