Qwt User's Guide: qwt_plot_magnifier.cpp Source File

Qwt User's Guide 6.2.0

qwt_plot_magnifier.cpp
1 /******************************************************************************
2 * Qwt Widget Library
3 * Copyright (C) 1997 Josef Wilgen
4 * Copyright (C) 2002 Uwe Rathmann
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the Qwt License, Version 1.0
8 *****************************************************************************/
9
10 #include "qwt_plot.h"
11 #include "qwt_scale_map.h"
12 #include "qwt_plot_magnifier.h"
13
14 class QwtPlotMagnifier::PrivateData
15 {
16 public:
17 PrivateData()
18 {
19 for ( int axis = 0; axis < QwtAxis::AxisPositions; axis++ )
20 isAxisEnabled[axis] = true;
21 }
22
23 bool isAxisEnabled[QwtAxis::AxisPositions];
24 };
25
30 QwtPlotMagnifier::QwtPlotMagnifier( QWidget* canvas )
31 : QwtMagnifier( canvas )
32 {
33 m_data = new PrivateData();
34 }
35
37 QwtPlotMagnifier::~QwtPlotMagnifier()
38 {
39 delete m_data;
40 }
41
53 void QwtPlotMagnifier::setAxisEnabled( QwtAxisId axisId, bool on )
54 {
55 if ( QwtAxis::isValid( axisId ) )
56 m_data->isAxisEnabled[axisId] = on;
57 }
58
67 bool QwtPlotMagnifier::isAxisEnabled( QwtAxisId axisId ) const
68 {
69 if ( QwtAxis::isValid( axisId ) )
70 return m_data->isAxisEnabled[axisId];
71
72 return true;
73 }
74
76 QWidget* QwtPlotMagnifier::canvas()
77 {
78 return parentWidget();
79 }
80
82 const QWidget* QwtPlotMagnifier::canvas() const
83 {
84 return parentWidget();
85 }
86
88 QwtPlot* QwtPlotMagnifier::plot()
89 {
90 QWidget* w = canvas();
91 if ( w )
92 w = w->parentWidget();
93
94 return qobject_cast< QwtPlot* >( w );
95 }
96
98 const QwtPlot* QwtPlotMagnifier::plot() const
99 {
100 const QWidget* w = canvas();
101 if ( w )
102 w = w->parentWidget();
103
104 return qobject_cast< const QwtPlot* >( w );
105 }
106
111 void QwtPlotMagnifier::rescale( double factor )
112 {
113 QwtPlot* plt = plot();
114 if ( plt == NULL )
115 return;
116
117 factor = qAbs( factor );
118 if ( factor == 1.0 || factor == 0.0 )
119 return;
120
121 bool doReplot = false;
122
123 const bool autoReplot = plt->autoReplot();
124 plt->setAutoReplot( false );
125
126 for ( int axisPos = 0; axisPos < QwtAxis::AxisPositions; axisPos++ )
127 {
128 {
129 const QwtAxisId axisId( axisPos );
130
131 if ( isAxisEnabled( axisId ) )
132 {
133 const QwtScaleMap scaleMap = plt->canvasMap( axisId );
134
135 double v1 = scaleMap.s1();
136 double v2 = scaleMap.s2();
137
138 if ( scaleMap.transformation() )
139 {
140 // the coordinate system of the paint device is always linear
141
142 v1 = scaleMap.transform( v1 ); // scaleMap.p1()
143 v2 = scaleMap.transform( v2 ); // scaleMap.p2()
144 }
145
146 const double center = 0.5 * ( v1 + v2 );
147 const double width_2 = 0.5 * ( v2 - v1 ) * factor;
148
149 v1 = center - width_2;
150 v2 = center + width_2;
151
152 if ( scaleMap.transformation() )
153 {
154 v1 = scaleMap.invTransform( v1 );
155 v2 = scaleMap.invTransform( v2 );
156 }
157
158 plt->setAxisScale( axisId, v1, v2 );
159 doReplot = true;
160 }
161 }
162 }
163
164 plt->setAutoReplot( autoReplot );
165
166 if ( doReplot )
167 plt->replot();
168 }
169
170 #if QWT_MOC_INCLUDE
171 #include "moc_qwt_plot_magnifier.cpp"
172 #endif
QwtMagnifier
QwtMagnifier provides zooming, by magnifying in steps.
Definition: qwt_magnifier.h:28
QwtMagnifier::parentWidget
QWidget * parentWidget()
Definition: qwt_magnifier.cpp:499
QwtPlot
A 2-D plotting widget.
Definition: qwt_plot.h:79
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:473
QwtPlot::autoReplot
bool autoReplot() const
Definition: qwt_plot.cpp:319
QwtPlot::replot
virtual void replot()
Redraw the plot.
Definition: qwt_plot.cpp:545
QwtPlot::setAutoReplot
void setAutoReplot(bool=true)
Set or reset the autoReplot option.
Definition: qwt_plot.cpp:310
QwtPlot::canvasMap
virtual QwtScaleMap canvasMap(QwtAxisId) const
Definition: qwt_plot.cpp:800
QwtPlotMagnifier::~QwtPlotMagnifier
virtual ~QwtPlotMagnifier()
Destructor.
Definition: qwt_plot_magnifier.cpp:37
QwtPlotMagnifier::setAxisEnabled
void setAxisEnabled(QwtAxisId, bool on)
En/Disable an axis.
Definition: qwt_plot_magnifier.cpp:53
QwtPlotMagnifier::rescale
virtual void rescale(double factor) override
Definition: qwt_plot_magnifier.cpp:111
QwtPlotMagnifier::QwtPlotMagnifier
QwtPlotMagnifier(QWidget *)
Definition: qwt_plot_magnifier.cpp:30
QwtPlotMagnifier::canvas
QWidget * canvas()
Return observed plot canvas.
Definition: qwt_plot_magnifier.cpp:76
QwtPlotMagnifier::isAxisEnabled
bool isAxisEnabled(QwtAxisId) const
Definition: qwt_plot_magnifier.cpp:67
QwtPlotMagnifier::plot
QwtPlot * plot()
Return plot widget, containing the observed plot canvas.
Definition: qwt_plot_magnifier.cpp:88
QwtScaleMap
A scale map.
Definition: qwt_scale_map.h:27
QwtScaleMap::transform
double transform(double s) const
Definition: qwt_scale_map.h:137
QwtScaleMap::s1
double s1() const
Definition: qwt_scale_map.h:83
QwtScaleMap::s2
double s2() const
Definition: qwt_scale_map.h:91
QwtScaleMap::transformation
const QwtTransform * transformation() const
Get the transformation.
Definition: qwt_scale_map.cpp:88
QwtScaleMap::invTransform
double invTransform(double p) const
Definition: qwt_scale_map.h:154
QwtAxis::isValid
bool isValid(int axisPos)
Definition: qwt_axis.h:45