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


plotjuggler
Author(s): Davide Faconti
autogenerated on Sat Jul 6 2019 03:44:17