Qwt User's Guide 6.3.0
Loading...
Searching...
No Matches
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
14class 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
31 : QwtMagnifier( canvas )
32{
33 m_data = new PrivateData();
34}
35
38{
39 delete m_data;
40}
41
53void QwtPlotMagnifier::setAxisEnabled( QwtAxisId axisId, bool on )
54{
55 if ( QwtAxis::isValid( axisId ) )
56 m_data->isAxisEnabled[axisId] = on;
57}
58
67bool QwtPlotMagnifier::isAxisEnabled( QwtAxisId axisId ) const
68{
69 if ( QwtAxis::isValid( axisId ) )
70 return m_data->isAxisEnabled[axisId];
71
72 return true;
73}
74
77{
78 return parentWidget();
79}
80
82const QWidget* QwtPlotMagnifier::canvas() const
83{
84 return parentWidget();
85}
86
89{
90 QWidget* w = canvas();
91 if ( w )
92 w = w->parentWidget();
93
94 return qobject_cast< QwtPlot* >( w );
95}
96
99{
100 const QWidget* w = canvas();
101 if ( w )
102 w = w->parentWidget();
103
104 return qobject_cast< const QwtPlot* >( w );
105}
106
111void 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#include "moc_qwt_plot_magnifier.cpp"
QwtMagnifier provides zooming, by magnifying in steps.
QWidget * parentWidget()
A 2-D plotting widget.
Definition qwt_plot.h:79
void setAxisScale(QwtAxisId, double min, double max, double stepSize=0)
Disable autoscaling and specify a fixed scale for a selected axis.
bool autoReplot() const
Definition qwt_plot.cpp:319
virtual void replot()
Redraw the plot.
Definition qwt_plot.cpp:545
void setAutoReplot(bool=true)
Set or reset the autoReplot option.
Definition qwt_plot.cpp:310
virtual QwtScaleMap canvasMap(QwtAxisId) const
Definition qwt_plot.cpp:800
virtual ~QwtPlotMagnifier()
Destructor.
void setAxisEnabled(QwtAxisId, bool on)
En/Disable an axis.
virtual void rescale(double factor) override
QWidget * canvas()
Return observed plot canvas.
bool isAxisEnabled(QwtAxisId) const
QwtPlot * plot()
Return plot widget, containing the observed plot canvas.
A scale map.
double transform(double s) const
double s1() const
double s2() const
const QwtTransform * transformation() const
Get the transformation.
double invTransform(double p) const
bool isValid(int axisPos)
Definition qwt_axis.h:45