1 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
3 * Copyright (C) 1997 Josef Wilgen
4 * Copyright (C) 2002 Uwe Rathmann
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 *****************************************************************************/
10 #include "qwt_abstract_scale.h"
11 #include "qwt_scale_engine.h"
12 #include "qwt_scale_draw.h"
13 #include "qwt_scale_div.h"
14 #include "qwt_scale_map.h"
15 #include "qwt_interval.h"
17 class QwtAbstractScale::PrivateData
25 scaleEngine
= new QwtLinearScaleEngine();
26 scaleDraw
= new QwtScaleDraw();
35 QwtScaleEngine
*scaleEngine
;
36 QwtAbstractScaleDraw
*scaleDraw
;
46 \param parent Parent widget
48 Creates a default QwtScaleDraw and a QwtLinearScaleEngine.
49 The initial scale boundaries are set to [ 0.0, 100.0 ]
51 The scaleStepSize() is initialized to 0.0, scaleMaxMajor() to 5
52 and scaleMaxMajor to 3.
55 QwtAbstractScale::QwtAbstractScale( QWidget
*parent
):
58 d_data
= new PrivateData
;
59 rescale( 0.0, 100.0, d_data
->stepSize
);
63 QwtAbstractScale::~QwtAbstractScale()
69 Set the lower bound of the scale
71 \param value Lower bound
73 \sa lowerBound(), setScale(), setUpperBound()
74 \note For inverted scales the lower bound
75 is greater than the upper bound
77 void QwtAbstractScale::setLowerBound( double value
)
79 setScale( value
, upperBound() );
83 \return Lower bound of the scale
84 \sa setLowerBound(), setScale(), upperBound()
86 double QwtAbstractScale::lowerBound() const
88 return d_data
->scaleDraw
->scaleDiv().lowerBound();
92 Set the upper bound of the scale
94 \param value Upper bound
96 \sa upperBound(), setScale(), setLowerBound()
97 \note For inverted scales the lower bound
98 is greater than the upper bound
100 void QwtAbstractScale::setUpperBound( double value
)
102 setScale( lowerBound(), value
);
106 \return Upper bound of the scale
107 \sa setUpperBound(), setScale(), lowerBound()
109 double QwtAbstractScale::upperBound() const
111 return d_data
->scaleDraw
->scaleDiv().upperBound();
115 \brief Specify a scale.
117 Define a scale by an interval
119 The ticks are calculated using scaleMaxMinor(),
120 scaleMaxMajor() and scaleStepSize().
122 \param lowerBound lower limit of the scale interval
123 \param upperBound upper limit of the scale interval
125 \note For inverted scales the lower bound
126 is greater than the upper bound
128 void QwtAbstractScale::setScale( double lowerBound
, double upperBound
)
130 rescale( lowerBound
, upperBound
, d_data
->stepSize
);
134 \brief Specify a scale.
136 Define a scale by an interval
138 The ticks are calculated using scaleMaxMinor(),
139 scaleMaxMajor() and scaleStepSize().
141 \param interval Interval
143 void QwtAbstractScale::setScale( const QwtInterval
&interval
)
145 setScale( interval
.minValue(), interval
.maxValue() );
149 \brief Specify a scale.
151 scaleMaxMinor(), scaleMaxMajor() and scaleStepSize() and have no effect.
153 \param scaleDiv Scale division
156 void QwtAbstractScale::setScale( const QwtScaleDiv
&scaleDiv
)
158 if ( scaleDiv
!= d_data
->scaleDraw
->scaleDiv() )
161 if ( d_data
->scaleEngine
)
163 d_data
->scaleDraw
->setTransformation(
164 d_data
->scaleEngine
->transformation() );
168 d_data
->scaleDraw
->setScaleDiv( scaleDiv
);
175 \brief Set the maximum number of major tick intervals.
177 The scale's major ticks are calculated automatically such that
178 the number of major intervals does not exceed ticks.
180 The default value is 5.
182 \param ticks Maximal number of major ticks.
184 \sa scaleMaxMajor(), setScaleMaxMinor(),
185 setScaleStepSize(), QwtScaleEngine::divideInterval()
187 void QwtAbstractScale::setScaleMaxMajor( int ticks
)
189 if ( ticks
!= d_data
->maxMajor
)
191 d_data
->maxMajor
= ticks
;
197 \return Maximal number of major tick intervals
198 \sa setScaleMaxMajor(), scaleMaxMinor()
200 int QwtAbstractScale::scaleMaxMajor() const
202 return d_data
->maxMajor
;
206 \brief Set the maximum number of minor tick intervals
208 The scale's minor ticks are calculated automatically such that
209 the number of minor intervals does not exceed ticks.
210 The default value is 3.
212 \param ticks Maximal number of minor ticks.
214 \sa scaleMaxMajor(), setScaleMaxMinor(),
215 setScaleStepSize(), QwtScaleEngine::divideInterval()
217 void QwtAbstractScale::setScaleMaxMinor( int ticks
)
219 if ( ticks
!= d_data
->maxMinor
)
221 d_data
->maxMinor
= ticks
;
227 \return Maximal number of minor tick intervals
228 \sa setScaleMaxMinor(), scaleMaxMajor()
230 int QwtAbstractScale::scaleMaxMinor() const
232 return d_data
->maxMinor
;
236 \brief Set the step size used for calculating a scale division
238 The step size is hint for calculating the intervals for
239 the major ticks of the scale. A value of 0.0 is interpreted
242 \param stepSize Hint for the step size of the scale
244 \sa scaleStepSize(), QwtScaleEngine::divideScale()
246 \note Position and distance between the major ticks also
247 depends on scaleMaxMajor().
249 void QwtAbstractScale::setScaleStepSize( double stepSize
)
251 if ( stepSize
!= d_data
->stepSize
)
253 d_data
->stepSize
= stepSize
;
259 \return Hint for the step size of the scale
260 \sa setScaleStepSize(), QwtScaleEngine::divideScale()
262 double QwtAbstractScale::scaleStepSize() const
264 return d_data
->stepSize
;
268 \brief Set a scale draw
270 scaleDraw has to be created with new and will be deleted in
271 the destructor or the next call of setAbstractScaleDraw().
273 \sa abstractScaleDraw()
275 void QwtAbstractScale::setAbstractScaleDraw( QwtAbstractScaleDraw
*scaleDraw
)
277 if ( scaleDraw
== NULL
|| scaleDraw
== d_data
->scaleDraw
)
280 if ( d_data
->scaleDraw
!= NULL
)
281 scaleDraw
->setScaleDiv( d_data
->scaleDraw
->scaleDiv() );
283 delete d_data
->scaleDraw
;
284 d_data
->scaleDraw
= scaleDraw
;
289 \sa setAbstractScaleDraw()
291 QwtAbstractScaleDraw
*QwtAbstractScale::abstractScaleDraw()
293 return d_data
->scaleDraw
;
298 \sa setAbstractScaleDraw()
300 const QwtAbstractScaleDraw
*QwtAbstractScale::abstractScaleDraw() const
302 return d_data
->scaleDraw
;
306 \brief Set a scale engine
308 The scale engine is responsible for calculating the scale division
309 and provides a transformation between scale and widget coordinates.
311 scaleEngine has to be created with new and will be deleted in
312 the destructor or the next call of setScaleEngine.
314 void QwtAbstractScale::setScaleEngine( QwtScaleEngine
*scaleEngine
)
316 if ( scaleEngine
!= NULL
&& scaleEngine
!= d_data
->scaleEngine
)
318 delete d_data
->scaleEngine
;
319 d_data
->scaleEngine
= scaleEngine
;
327 const QwtScaleEngine
*QwtAbstractScale::scaleEngine() const
329 return d_data
->scaleEngine
;
336 QwtScaleEngine
*QwtAbstractScale::scaleEngine()
338 return d_data
->scaleEngine
;
342 \return Scale boundaries and positions of the ticks
344 The scale division might have been assigned explicitly
345 or calculated implicitly by rescale().
347 const QwtScaleDiv
&QwtAbstractScale::scaleDiv() const
349 return d_data
->scaleDraw
->scaleDiv();
353 \return Map to translate between scale and widget coordinates
355 const QwtScaleMap
&QwtAbstractScale::scaleMap() const
357 return d_data
->scaleDraw
->scaleMap();
361 Translate a scale value into a widget coordinate
363 \param value Scale value
364 \return Corresponding widget coordinate for value
365 \sa scaleMap(), invTransform()
367 int QwtAbstractScale::transform( double value
) const
369 return qRound( d_data
->scaleDraw
->scaleMap().transform( value
) );
373 Translate a widget coordinate into a scale value
375 \param value Widget coordinate
376 \return Corresponding scale coordinate for value
377 \sa scaleMap(), transform()
379 double QwtAbstractScale::invTransform( int value
) const
381 return d_data
->scaleDraw
->scaleMap().invTransform( value
);
385 \return True, when the scale is increasing in opposite direction
386 to the widget coordinates
388 bool QwtAbstractScale::isInverted() const
390 return d_data
->scaleDraw
->scaleMap().isInverting();
394 \return The boundary with the smaller value
395 \sa maximum(), lowerBound(), upperBound()
397 double QwtAbstractScale::minimum() const
399 return qMin( d_data
->scaleDraw
->scaleDiv().lowerBound(),
400 d_data
->scaleDraw
->scaleDiv().upperBound() );
404 \return The boundary with the larger value
405 \sa minimum(), lowerBound(), upperBound()
407 double QwtAbstractScale::maximum() const
409 return qMax( d_data
->scaleDraw
->scaleDiv().lowerBound(),
410 d_data
->scaleDraw
->scaleDiv().upperBound() );
413 //! Notify changed scale
414 void QwtAbstractScale::scaleChange()
419 Recalculate the scale division and update the scale.
421 \param lowerBound Lower limit of the scale interval
422 \param upperBound Upper limit of the scale interval
423 \param stepSize Major step size
427 void QwtAbstractScale::rescale(
428 double lowerBound
, double upperBound
, double stepSize
)
430 const QwtScaleDiv scaleDiv
= d_data
->scaleEngine
->divideScale(
431 lowerBound
, upperBound
, d_data
->maxMajor
, d_data
->maxMinor
, stepSize
);
433 if ( scaleDiv
!= d_data
->scaleDraw
->scaleDiv() )
436 d_data
->scaleDraw
->setTransformation(
437 d_data
->scaleEngine
->transformation() );
440 d_data
->scaleDraw
->setScaleDiv( scaleDiv
);
445 void QwtAbstractScale::updateScaleDraw()
447 rescale( d_data
->scaleDraw
->scaleDiv().lowerBound(),
448 d_data
->scaleDraw
->scaleDiv().upperBound(), d_data
->stepSize
);