1 | /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
|
---|
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_scale_map.h"
|
---|
11 | #include <qrect.h>
|
---|
12 | #include <qalgorithms.h>
|
---|
13 | #include <qmath.h>
|
---|
14 | #include <qdebug.h>
|
---|
15 |
|
---|
16 | #if QT_VERSION < 0x040601
|
---|
17 | #define qExp(x) ::exp(x)
|
---|
18 | #endif
|
---|
19 |
|
---|
20 | //! Smallest allowed value for logarithmic scales: 1.0e-150
|
---|
21 | QT_STATIC_CONST_IMPL double QwtScaleMap::LogMin = 1.0e-150;
|
---|
22 |
|
---|
23 | //! Largest allowed value for logarithmic scales: 1.0e150
|
---|
24 | QT_STATIC_CONST_IMPL double QwtScaleMap::LogMax = 1.0e150;
|
---|
25 |
|
---|
26 | //! Constructor for a linear transformation
|
---|
27 | QwtScaleTransformation::QwtScaleTransformation( Type type ):
|
---|
28 | d_type( type )
|
---|
29 | {
|
---|
30 | }
|
---|
31 |
|
---|
32 | //! Destructor
|
---|
33 | QwtScaleTransformation::~QwtScaleTransformation()
|
---|
34 | {
|
---|
35 | }
|
---|
36 |
|
---|
37 | //! Create a clone of the transformation
|
---|
38 | QwtScaleTransformation *QwtScaleTransformation::copy() const
|
---|
39 | {
|
---|
40 | return new QwtScaleTransformation( d_type );
|
---|
41 | }
|
---|
42 |
|
---|
43 | /*!
|
---|
44 | \brief Transform a value from the coordinate system of a scale
|
---|
45 | into the coordinate system of the paint device
|
---|
46 |
|
---|
47 | \param s Value related to the coordinate system of the scale
|
---|
48 | \param s1 First border of the coordinate system of the scale
|
---|
49 | \param s2 Second border of the coordinate system of the scale
|
---|
50 | \param p1 First border of the coordinate system of the paint device
|
---|
51 | \param p2 Second border of the coordinate system of the paint device
|
---|
52 | \return
|
---|
53 | <dl>
|
---|
54 | <dt>linear mapping:<dd>p1 + (p2 - p1) / (s2 - s1) * (s - s1);</dd>
|
---|
55 | </dl>
|
---|
56 | <dl>
|
---|
57 | <dt>log10 mapping: <dd>p1 + (p2 - p1) / log(s2 / s1) * log(s / s1);</dd>
|
---|
58 | </dl>
|
---|
59 | */
|
---|
60 |
|
---|
61 | double QwtScaleTransformation::xForm(
|
---|
62 | double s, double s1, double s2, double p1, double p2 ) const
|
---|
63 | {
|
---|
64 | if ( d_type == Log10 )
|
---|
65 | return p1 + ( p2 - p1 ) / log( s2 / s1 ) * log( s / s1 );
|
---|
66 | else
|
---|
67 | return p1 + ( p2 - p1 ) / ( s2 - s1 ) * ( s - s1 );
|
---|
68 | }
|
---|
69 |
|
---|
70 | /*!
|
---|
71 | \brief Transform a value from the coordinate system of the paint device
|
---|
72 | into the coordinate system of a scale.
|
---|
73 |
|
---|
74 | \param p Value related to the coordinate system of the paint device
|
---|
75 | \param p1 First border of the coordinate system of the paint device
|
---|
76 | \param p2 Second border of the coordinate system of the paint device
|
---|
77 | \param s1 First border of the coordinate system of the scale
|
---|
78 | \param s2 Second border of the coordinate system of the scale
|
---|
79 | \return
|
---|
80 | <dl>
|
---|
81 | <dt>linear mapping:<dd>s1 + ( s2 - s1 ) / ( p2 - p1 ) * ( p - p1 );</dd>
|
---|
82 | </dl>
|
---|
83 | <dl>
|
---|
84 | <dt>log10 mapping:<dd>exp((p - p1) / (p2 - p1) * log(s2 / s1)) * s1;</dd>
|
---|
85 | </dl>
|
---|
86 | */
|
---|
87 |
|
---|
88 | double QwtScaleTransformation::invXForm( double p, double p1, double p2,
|
---|
89 | double s1, double s2 ) const
|
---|
90 | {
|
---|
91 | if ( d_type == Log10 )
|
---|
92 | return qExp( ( p - p1 ) / ( p2 - p1 ) * log( s2 / s1 ) ) * s1;
|
---|
93 | else
|
---|
94 | return s1 + ( s2 - s1 ) / ( p2 - p1 ) * ( p - p1 );
|
---|
95 | }
|
---|
96 |
|
---|
97 | /*!
|
---|
98 | \brief Constructor
|
---|
99 |
|
---|
100 | The scale and paint device intervals are both set to [0,1].
|
---|
101 | */
|
---|
102 | QwtScaleMap::QwtScaleMap():
|
---|
103 | d_s1( 0.0 ),
|
---|
104 | d_s2( 1.0 ),
|
---|
105 | d_p1( 0.0 ),
|
---|
106 | d_p2( 1.0 ),
|
---|
107 | d_cnv( 1.0 )
|
---|
108 | {
|
---|
109 | d_transformation = new QwtScaleTransformation(
|
---|
110 | QwtScaleTransformation::Linear );
|
---|
111 | }
|
---|
112 |
|
---|
113 | //! Copy constructor
|
---|
114 | QwtScaleMap::QwtScaleMap( const QwtScaleMap& other ):
|
---|
115 | d_s1( other.d_s1 ),
|
---|
116 | d_s2( other.d_s2 ),
|
---|
117 | d_p1( other.d_p1 ),
|
---|
118 | d_p2( other.d_p2 ),
|
---|
119 | d_cnv( other.d_cnv )
|
---|
120 | {
|
---|
121 | d_transformation = other.d_transformation->copy();
|
---|
122 | }
|
---|
123 |
|
---|
124 | /*!
|
---|
125 | Destructor
|
---|
126 | */
|
---|
127 | QwtScaleMap::~QwtScaleMap()
|
---|
128 | {
|
---|
129 | delete d_transformation;
|
---|
130 | }
|
---|
131 |
|
---|
132 | //! Assignment operator
|
---|
133 | QwtScaleMap &QwtScaleMap::operator=( const QwtScaleMap & other )
|
---|
134 | {
|
---|
135 | d_s1 = other.d_s1;
|
---|
136 | d_s2 = other.d_s2;
|
---|
137 | d_p1 = other.d_p1;
|
---|
138 | d_p2 = other.d_p2;
|
---|
139 | d_cnv = other.d_cnv;
|
---|
140 |
|
---|
141 | delete d_transformation;
|
---|
142 | d_transformation = other.d_transformation->copy();
|
---|
143 |
|
---|
144 | return *this;
|
---|
145 | }
|
---|
146 |
|
---|
147 | /*!
|
---|
148 | Initialize the map with a transformation
|
---|
149 | */
|
---|
150 | void QwtScaleMap::setTransformation(
|
---|
151 | QwtScaleTransformation *transformation )
|
---|
152 | {
|
---|
153 | if ( transformation == NULL )
|
---|
154 | return;
|
---|
155 |
|
---|
156 | if ( transformation != d_transformation )
|
---|
157 | {
|
---|
158 | delete d_transformation;
|
---|
159 | d_transformation = transformation;
|
---|
160 | }
|
---|
161 |
|
---|
162 | setScaleInterval( d_s1, d_s2 );
|
---|
163 | }
|
---|
164 |
|
---|
165 | //! Get the transformation
|
---|
166 | const QwtScaleTransformation *QwtScaleMap::transformation() const
|
---|
167 | {
|
---|
168 | return d_transformation;
|
---|
169 | }
|
---|
170 |
|
---|
171 | /*!
|
---|
172 | \brief Specify the borders of the scale interval
|
---|
173 | \param s1 first border
|
---|
174 | \param s2 second border
|
---|
175 | \warning logarithmic scales might be aligned to [LogMin, LogMax]
|
---|
176 | */
|
---|
177 | void QwtScaleMap::setScaleInterval( double s1, double s2 )
|
---|
178 | {
|
---|
179 | if ( d_transformation->type() == QwtScaleTransformation::Log10 )
|
---|
180 | {
|
---|
181 | if ( s1 < LogMin )
|
---|
182 | s1 = LogMin;
|
---|
183 | else if ( s1 > LogMax )
|
---|
184 | s1 = LogMax;
|
---|
185 |
|
---|
186 | if ( s2 < LogMin )
|
---|
187 | s2 = LogMin;
|
---|
188 | else if ( s2 > LogMax )
|
---|
189 | s2 = LogMax;
|
---|
190 | }
|
---|
191 |
|
---|
192 | d_s1 = s1;
|
---|
193 | d_s2 = s2;
|
---|
194 |
|
---|
195 | if ( d_transformation->type() != QwtScaleTransformation::Other )
|
---|
196 | newFactor();
|
---|
197 | }
|
---|
198 |
|
---|
199 | /*!
|
---|
200 | \brief Specify the borders of the paint device interval
|
---|
201 | \param p1 first border
|
---|
202 | \param p2 second border
|
---|
203 | */
|
---|
204 | void QwtScaleMap::setPaintInterval( double p1, double p2 )
|
---|
205 | {
|
---|
206 | d_p1 = p1;
|
---|
207 | d_p2 = p2;
|
---|
208 |
|
---|
209 | if ( d_transformation->type() != QwtScaleTransformation::Other )
|
---|
210 | newFactor();
|
---|
211 | }
|
---|
212 |
|
---|
213 | /*!
|
---|
214 | \brief Re-calculate the conversion factor.
|
---|
215 | */
|
---|
216 | void QwtScaleMap::newFactor()
|
---|
217 | {
|
---|
218 | d_cnv = 0.0;
|
---|
219 |
|
---|
220 | switch ( d_transformation->type() )
|
---|
221 | {
|
---|
222 | case QwtScaleTransformation::Linear:
|
---|
223 | {
|
---|
224 | if ( d_s2 != d_s1 )
|
---|
225 | d_cnv = ( d_p2 - d_p1 ) / ( d_s2 - d_s1 );
|
---|
226 | break;
|
---|
227 | }
|
---|
228 | case QwtScaleTransformation::Log10:
|
---|
229 | {
|
---|
230 | if ( d_s1 != 0 )
|
---|
231 | d_cnv = ( d_p2 - d_p1 ) / log( d_s2 / d_s1 );
|
---|
232 | break;
|
---|
233 | }
|
---|
234 | default:;
|
---|
235 | }
|
---|
236 | }
|
---|
237 |
|
---|
238 | /*!
|
---|
239 | Transform a rectangle from scale to paint coordinates
|
---|
240 |
|
---|
241 | \param xMap X map
|
---|
242 | \param yMap Y map
|
---|
243 | \param rect Rectangle in scale coordinates
|
---|
244 | \return Rectangle in paint coordinates
|
---|
245 |
|
---|
246 | \sa invTransform()
|
---|
247 | */
|
---|
248 | QRectF QwtScaleMap::transform( const QwtScaleMap &xMap,
|
---|
249 | const QwtScaleMap &yMap, const QRectF &rect )
|
---|
250 | {
|
---|
251 | double x1 = xMap.transform( rect.left() );
|
---|
252 | double x2 = xMap.transform( rect.right() );
|
---|
253 | double y1 = yMap.transform( rect.top() );
|
---|
254 | double y2 = yMap.transform( rect.bottom() );
|
---|
255 |
|
---|
256 | if ( x2 < x1 )
|
---|
257 | qSwap( x1, x2 );
|
---|
258 | if ( y2 < y1 )
|
---|
259 | qSwap( y1, y2 );
|
---|
260 |
|
---|
261 | if ( qwtFuzzyCompare( x1, 0.0, x2 - x1 ) == 0 )
|
---|
262 | x1 = 0.0;
|
---|
263 | if ( qwtFuzzyCompare( x2, 0.0, x2 - x1 ) == 0 )
|
---|
264 | x2 = 0.0;
|
---|
265 | if ( qwtFuzzyCompare( y1, 0.0, y2 - y1 ) == 0 )
|
---|
266 | y1 = 0.0;
|
---|
267 | if ( qwtFuzzyCompare( y2, 0.0, y2 - y1 ) == 0 )
|
---|
268 | y2 = 0.0;
|
---|
269 |
|
---|
270 | return QRectF( x1, y1, x2 - x1 + 1, y2 - y1 + 1 );
|
---|
271 | }
|
---|
272 |
|
---|
273 | /*!
|
---|
274 | Transform a rectangle from paint to scale coordinates
|
---|
275 |
|
---|
276 | \param xMap X map
|
---|
277 | \param yMap Y map
|
---|
278 | \param pos Position in paint coordinates
|
---|
279 | \return Position in scale coordinates
|
---|
280 | \sa transform()
|
---|
281 | */
|
---|
282 | QPointF QwtScaleMap::invTransform( const QwtScaleMap &xMap,
|
---|
283 | const QwtScaleMap &yMap, const QPointF &pos )
|
---|
284 | {
|
---|
285 | return QPointF(
|
---|
286 | xMap.invTransform( pos.x() ),
|
---|
287 | yMap.invTransform( pos.y() )
|
---|
288 | );
|
---|
289 | }
|
---|
290 |
|
---|
291 | /*!
|
---|
292 | Transform a point from scale to paint coordinates
|
---|
293 |
|
---|
294 | \param xMap X map
|
---|
295 | \param yMap Y map
|
---|
296 | \param pos Position in scale coordinates
|
---|
297 | \return Position in paint coordinates
|
---|
298 |
|
---|
299 | \sa invTransform()
|
---|
300 | */
|
---|
301 | QPointF QwtScaleMap::transform( const QwtScaleMap &xMap,
|
---|
302 | const QwtScaleMap &yMap, const QPointF &pos )
|
---|
303 | {
|
---|
304 | return QPointF(
|
---|
305 | xMap.transform( pos.x() ),
|
---|
306 | yMap.transform( pos.y() )
|
---|
307 | );
|
---|
308 | }
|
---|
309 |
|
---|
310 | /*!
|
---|
311 | Transform a rectangle from paint to scale coordinates
|
---|
312 |
|
---|
313 | \param xMap X map
|
---|
314 | \param yMap Y map
|
---|
315 | \param rect Rectangle in paint coordinates
|
---|
316 | \return Rectangle in scale coordinates
|
---|
317 | \sa transform()
|
---|
318 | */
|
---|
319 | QRectF QwtScaleMap::invTransform( const QwtScaleMap &xMap,
|
---|
320 | const QwtScaleMap &yMap, const QRectF &rect )
|
---|
321 | {
|
---|
322 | const double x1 = xMap.invTransform( rect.left() );
|
---|
323 | const double x2 = xMap.invTransform( rect.right() - 1 );
|
---|
324 | const double y1 = yMap.invTransform( rect.top() );
|
---|
325 | const double y2 = yMap.invTransform( rect.bottom() - 1 );
|
---|
326 |
|
---|
327 | const QRectF r( x1, y1, x2 - x1, y2 - y1 );
|
---|
328 | return r.normalized();
|
---|
329 | }
|
---|
330 |
|
---|
331 | #ifndef QT_NO_DEBUG_STREAM
|
---|
332 |
|
---|
333 | QDebug operator<<( QDebug debug, const QwtScaleMap &map )
|
---|
334 | {
|
---|
335 | debug.nospace() << "QwtScaleMap("
|
---|
336 | << static_cast<int>( map.transformation()->type() )
|
---|
337 | << ", s:" << map.s1() << "->" << map.s2()
|
---|
338 | << ", p:" << map.p1() << "->" << map.p2()
|
---|
339 | << ")";
|
---|
340 |
|
---|
341 | return debug.space();
|
---|
342 | }
|
---|
343 |
|
---|
344 | #endif
|
---|