source: ntrip/trunk/BNC/qwt/qwt_scale_engine.cpp

Last change on this file was 9383, checked in by stoecker, 3 years ago

update to qwt verion 6.1.1 to fix build with newer Qt5

File size: 28.2 KB
RevLine 
[4271]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_engine.h"
11#include "qwt_math.h"
12#include "qwt_scale_map.h"
13#include <qalgorithms.h>
14#include <qmath.h>
[8127]15#include <float.h>
16#include <limits>
[4271]17
18#if QT_VERSION < 0x040601
19#define qFabs(x) ::fabs(x)
20#define qExp(x) ::exp(x)
21#endif
22
[8127]23static inline double qwtLog( double base, double value )
24{
25 return log( value ) / log( base );
26}
27
28static inline QwtInterval qwtLogInterval( double base, const QwtInterval &interval )
29{
30 return QwtInterval( qwtLog( base, interval.minValue() ),
31 qwtLog( base, interval.maxValue() ) );
32}
33
[9383]34static inline QwtInterval qwtPowInterval( double base, const QwtInterval &interval )
[8127]35{
36 return QwtInterval( qPow( base, interval.minValue() ),
37 qPow( base, interval.maxValue() ) );
38}
39
40static inline long double qwtIntervalWidthL( const QwtInterval &interval )
41{
42 if ( !interval.isValid() )
43 return 0.0;
44
45 return static_cast<long double>( interval.maxValue() )
46 - static_cast<long double>( interval.minValue() );
47}
48
49#if 1
50
51// this version often doesn't find the best ticks: f.e for 15: 5, 10
52static double qwtStepSize( double intervalSize, int maxSteps, uint base )
53{
[9383]54 const double minStep =
[8127]55 QwtScaleArithmetic::divideInterval( intervalSize, maxSteps, base );
56
57 if ( minStep != 0.0 )
58 {
59 // # ticks per interval
60 const int numTicks = qCeil( qAbs( intervalSize / minStep ) ) - 1;
61
62 // Do the minor steps fit into the interval?
63 if ( qwtFuzzyCompare( ( numTicks + 1 ) * qAbs( minStep ),
64 qAbs( intervalSize ), intervalSize ) > 0 )
65 {
66 // The minor steps doesn't fit into the interval
67 return 0.5 * intervalSize;
68 }
69 }
70
71 return minStep;
72}
73
74#else
75
76static double qwtStepSize( double intervalSize, int maxSteps, uint base )
77{
78 if ( maxSteps <= 0 )
79 return 0.0;
80
81 if ( maxSteps > 2 )
82 {
83 for ( int numSteps = maxSteps; numSteps > 1; numSteps-- )
84 {
85 const double stepSize = intervalSize / numSteps;
86
87 const double p = ::floor( ::log( stepSize ) / ::log( base ) );
88 const double fraction = qPow( base, p );
89
90 for ( uint n = base; n > 1; n /= 2 )
91 {
92 if ( qFuzzyCompare( stepSize, n * fraction ) )
93 return stepSize;
94
95 if ( n == 3 && ( base % 2 ) == 0 )
96 {
97 if ( qFuzzyCompare( stepSize, 2 * fraction ) )
98 return stepSize;
99 }
100 }
101 }
102 }
103
104 return intervalSize * 0.5;
105}
106
107#endif
108
[4271]109static const double _eps = 1.0e-6;
110
111/*!
112 Ceil a value, relative to an interval
113
[8127]114 \param value Value to be ceiled
[4271]115 \param intervalSize Interval size
116
[8127]117 \return Rounded value
118
[4271]119 \sa floorEps()
120*/
121double QwtScaleArithmetic::ceilEps( double value,
122 double intervalSize )
123{
124 const double eps = _eps * intervalSize;
125
126 value = ( value - eps ) / intervalSize;
[8127]127 return ::ceil( value ) * intervalSize;
[4271]128}
129
130/*!
131 Floor a value, relative to an interval
132
[8127]133 \param value Value to be floored
[4271]134 \param intervalSize Interval size
135
[8127]136 \return Rounded value
[4271]137 \sa floorEps()
138*/
139double QwtScaleArithmetic::floorEps( double value, double intervalSize )
140{
141 const double eps = _eps * intervalSize;
142
143 value = ( value + eps ) / intervalSize;
[8127]144 return ::floor( value ) * intervalSize;
[4271]145}
146
147/*!
148 \brief Divide an interval into steps
149
150 \f$stepSize = (intervalSize - intervalSize * 10e^{-6}) / numSteps\f$
151
152 \param intervalSize Interval size
153 \param numSteps Number of steps
154 \return Step size
155*/
156double QwtScaleArithmetic::divideEps( double intervalSize, double numSteps )
157{
158 if ( numSteps == 0.0 || intervalSize == 0.0 )
159 return 0.0;
160
161 return ( intervalSize - ( _eps * intervalSize ) ) / numSteps;
162}
163
164/*!
[8127]165 Calculate a step size for a given interval
[4271]166
[8127]167 \param intervalSize Interval size
168 \param numSteps Number of steps
169 \param base Base for the division ( usually 10 )
170
171 \return Calculated step size
172 */
[9383]173double QwtScaleArithmetic::divideInterval(
174 double intervalSize, int numSteps, uint base )
[4271]175{
[8127]176 if ( numSteps <= 0 )
[4271]177 return 0.0;
178
[8127]179 const double v = QwtScaleArithmetic::divideEps( intervalSize, numSteps );
180 if ( v == 0.0 )
181 return 0.0;
[4271]182
[8127]183 const double lx = qwtLog( base, qFabs( v ) );
184 const double p = ::floor( lx );
[4271]185
[8127]186 const double fraction = qPow( base, lx - p );
[4271]187
[8127]188 uint n = base;
189 while ( ( n > 1 ) && ( fraction <= n / 2 ) )
190 n /= 2;
[4271]191
[8127]192 double stepSize = n * qPow( base, p );
193 if ( v < 0 )
194 stepSize = -stepSize;
[4271]195
[8127]196 return stepSize;
[4271]197}
198
199class QwtScaleEngine::PrivateData
200{
201public:
202 PrivateData():
203 attributes( QwtScaleEngine::NoAttribute ),
204 lowerMargin( 0.0 ),
205 upperMargin( 0.0 ),
[8127]206 referenceValue( 0.0 ),
207 base( 10 ),
208 transform( NULL )
[4271]209 {
210 }
211
[8127]212 ~PrivateData()
213 {
214 delete transform;
215 }
[4271]216
[8127]217 QwtScaleEngine::Attributes attributes;
218
219 double lowerMargin;
[4271]220 double upperMargin;
221
[8127]222 double referenceValue;
[4271]223
[8127]224 uint base;
225
226 QwtTransform* transform;
[4271]227};
228
[8127]229/*!
230 Constructor
231
232 \param base Base of the scale engine
233 \sa setBase()
234 */
235QwtScaleEngine::QwtScaleEngine( uint base )
[4271]236{
237 d_data = new PrivateData;
[8127]238 setBase( base );
[4271]239}
240
241
242//! Destructor
243QwtScaleEngine::~QwtScaleEngine ()
244{
245 delete d_data;
246}
247
248/*!
[8127]249 Assign a transformation
250
251 \param transform Transformation
252
253 The transformation object is used as factory for clones
254 that are returned by transformation()
255
256 The scale engine takes ownership of the transformation.
257
258 \sa QwtTransform::copy(), transformation()
259
260 */
261void QwtScaleEngine::setTransformation( QwtTransform *transform )
262{
263 if ( transform != d_data->transform )
264 {
265 delete d_data->transform;
266 d_data->transform = transform;
267 }
268}
269
270/*!
[9383]271 Create and return a clone of the transformation
[8127]272 of the engine. When the engine has no special transformation
273 NULL is returned, indicating no transformation.
274
275 \return A clone of the transfomation
276 \sa setTransformation()
277 */
278QwtTransform *QwtScaleEngine::transformation() const
279{
280 QwtTransform *transform = NULL;
281 if ( d_data->transform )
282 transform = d_data->transform->copy();
283
284 return transform;
285}
286
287/*!
[4271]288 \return the margin at the lower end of the scale
289 The default margin is 0.
290
291 \sa setMargins()
292*/
293double QwtScaleEngine::lowerMargin() const
294{
295 return d_data->lowerMargin;
296}
297
298/*!
299 \return the margin at the upper end of the scale
300 The default margin is 0.
301
302 \sa setMargins()
303*/
304double QwtScaleEngine::upperMargin() const
305{
306 return d_data->upperMargin;
307}
308
309/*!
310 \brief Specify margins at the scale's endpoints
311 \param lower minimum distance between the scale's lower boundary and the
312 smallest enclosed value
313 \param upper minimum distance between the scale's upper boundary and the
314 greatest enclosed value
315
316 Margins can be used to leave a minimum amount of space between
317 the enclosed intervals and the boundaries of the scale.
318
319 \warning
[8127]320 \li QwtLogScaleEngine measures the margins in decades.
[4271]321
322 \sa upperMargin(), lowerMargin()
323*/
324
325void QwtScaleEngine::setMargins( double lower, double upper )
326{
327 d_data->lowerMargin = qMax( lower, 0.0 );
328 d_data->upperMargin = qMax( upper, 0.0 );
329}
330
331/*!
332 Calculate a step size for an interval size
333
334 \param intervalSize Interval size
335 \param numSteps Number of steps
336
337 \return Step size
338*/
339double QwtScaleEngine::divideInterval(
340 double intervalSize, int numSteps ) const
341{
[9383]342 return QwtScaleArithmetic::divideInterval(
[8127]343 intervalSize, numSteps, d_data->base );
[4271]344}
345
346/*!
347 Check if an interval "contains" a value
348
349 \param interval Interval
350 \param value Value
351
[8127]352 \return True, when the value is inside the interval
[4271]353*/
354bool QwtScaleEngine::contains(
355 const QwtInterval &interval, double value ) const
356{
357 if ( !interval.isValid() )
358 return false;
359
360 if ( qwtFuzzyCompare( value, interval.minValue(), interval.width() ) < 0 )
361 return false;
362
363 if ( qwtFuzzyCompare( value, interval.maxValue(), interval.width() ) > 0 )
364 return false;
365
366 return true;
367}
368
369/*!
370 Remove ticks from a list, that are not inside an interval
371
372 \param ticks Tick list
373 \param interval Interval
374
375 \return Stripped tick list
376*/
377QList<double> QwtScaleEngine::strip( const QList<double>& ticks,
378 const QwtInterval &interval ) const
379{
380 if ( !interval.isValid() || ticks.count() == 0 )
381 return QList<double>();
382
383 if ( contains( interval, ticks.first() )
384 && contains( interval, ticks.last() ) )
385 {
386 return ticks;
387 }
388
389 QList<double> strippedTicks;
390 for ( int i = 0; i < ticks.count(); i++ )
391 {
392 if ( contains( interval, ticks[i] ) )
393 strippedTicks += ticks[i];
394 }
395 return strippedTicks;
396}
397
398/*!
[8127]399 \brief Build an interval around a value
[4271]400
401 In case of v == 0.0 the interval is [-0.5, 0.5],
402 otherwide it is [0.5 * v, 1.5 * v]
[8127]403
404 \param value Initial value
405 \return Calculated interval
[4271]406*/
407
[8127]408QwtInterval QwtScaleEngine::buildInterval( double value ) const
[4271]409{
[8127]410 const double delta = ( value == 0.0 ) ? 0.5 : qAbs( 0.5 * value );
411
412 if ( DBL_MAX - delta < value )
413 return QwtInterval( DBL_MAX - delta, DBL_MAX );
414
415 if ( -DBL_MAX + delta > value )
416 return QwtInterval( -DBL_MAX, -DBL_MAX + delta );
417
418 return QwtInterval( value - delta, value + delta );
[4271]419}
420
421/*!
422 Change a scale attribute
423
424 \param attribute Attribute to change
425 \param on On/Off
426
427 \sa Attribute, testAttribute()
428*/
429void QwtScaleEngine::setAttribute( Attribute attribute, bool on )
430{
431 if ( on )
432 d_data->attributes |= attribute;
433 else
434 d_data->attributes &= ~attribute;
435}
436
437/*!
[8127]438 \return True, if attribute is enabled.
[4271]439
440 \param attribute Attribute to be tested
441 \sa Attribute, setAttribute()
442*/
443bool QwtScaleEngine::testAttribute( Attribute attribute ) const
444{
445 return ( d_data->attributes & attribute );
446}
447
448/*!
449 Change the scale attribute
450
451 \param attributes Set scale attributes
452 \sa Attribute, attributes()
453*/
454void QwtScaleEngine::setAttributes( Attributes attributes )
455{
456 d_data->attributes = attributes;
457}
458
459/*!
[8127]460 \return Scale attributes
[4271]461 \sa Attribute, setAttributes(), testAttribute()
462*/
463QwtScaleEngine::Attributes QwtScaleEngine::attributes() const
464{
465 return d_data->attributes;
466}
467
468/*!
469 \brief Specify a reference point
[9383]470 \param reference New reference value
[4271]471
472 The reference point is needed if options IncludeReference or
473 Symmetric are active. Its default value is 0.0.
474
475 \sa Attribute
476*/
[9383]477void QwtScaleEngine::setReference( double reference )
[4271]478{
[9383]479 d_data->referenceValue = reference;
[4271]480}
481
482/*!
[8127]483 \return the reference value
484 \sa setReference(), setAttribute()
[4271]485*/
486double QwtScaleEngine::reference() const
487{
488 return d_data->referenceValue;
489}
490
491/*!
[8127]492 Set the base of the scale engine
493
494 While a base of 10 is what 99.9% of all applications need
495 certain scales might need a different base: f.e 2
496
497 The default setting is 10
498
499 \param base Base of the engine
500
501 \sa base()
502 */
503void QwtScaleEngine::setBase( uint base )
[9383]504{
[8127]505 d_data->base = qMax( base, 2U );
506}
507
508/*!
509 \return base Base of the scale engine
510 \sa setBase()
511 */
512uint QwtScaleEngine::base() const
[4271]513{
[8127]514 return d_data->base;
[4271]515}
516
517/*!
[8127]518 Constructor
[4271]519
[8127]520 \param base Base of the scale engine
521 \sa setBase()
522 */
523QwtLinearScaleEngine::QwtLinearScaleEngine( uint base ):
524 QwtScaleEngine( base )
525{
526}
[4271]527
[8127]528//! Destructor
529QwtLinearScaleEngine::~QwtLinearScaleEngine()
530{
531}
532
533/*!
534 Align and divide an interval
535
536 \param maxNumSteps Max. number of steps
537 \param x1 First limit of the interval (In/Out)
538 \param x2 Second limit of the interval (In/Out)
539 \param stepSize Step size (Out)
540
541 \sa setAttribute()
[4271]542*/
543void QwtLinearScaleEngine::autoScale( int maxNumSteps,
544 double &x1, double &x2, double &stepSize ) const
545{
546 QwtInterval interval( x1, x2 );
547 interval = interval.normalized();
548
549 interval.setMinValue( interval.minValue() - lowerMargin() );
550 interval.setMaxValue( interval.maxValue() + upperMargin() );
551
552 if ( testAttribute( QwtScaleEngine::Symmetric ) )
553 interval = interval.symmetrize( reference() );
554
555 if ( testAttribute( QwtScaleEngine::IncludeReference ) )
556 interval = interval.extend( reference() );
557
558 if ( interval.width() == 0.0 )
559 interval = buildInterval( interval.minValue() );
560
[9383]561 stepSize = QwtScaleArithmetic::divideInterval(
[8127]562 interval.width(), qMax( maxNumSteps, 1 ), base() );
[4271]563
564 if ( !testAttribute( QwtScaleEngine::Floating ) )
565 interval = align( interval, stepSize );
566
567 x1 = interval.minValue();
568 x2 = interval.maxValue();
569
570 if ( testAttribute( QwtScaleEngine::Inverted ) )
571 {
572 qSwap( x1, x2 );
573 stepSize = -stepSize;
574 }
575}
576
577/*!
[8127]578 \brief Calculate a scale division for an interval
[4271]579
580 \param x1 First interval limit
581 \param x2 Second interval limit
[8127]582 \param maxMajorSteps Maximum for the number of major steps
583 \param maxMinorSteps Maximum number of minor steps
584 \param stepSize Step size. If stepSize == 0, the engine
[4271]585 calculates one.
586
[8127]587 \return Calculated scale division
[4271]588*/
589QwtScaleDiv QwtLinearScaleEngine::divideScale( double x1, double x2,
[8127]590 int maxMajorSteps, int maxMinorSteps, double stepSize ) const
[4271]591{
592 QwtInterval interval = QwtInterval( x1, x2 ).normalized();
[8127]593
594 if ( qwtIntervalWidthL( interval ) > std::numeric_limits<double>::max() )
595 {
596 qWarning() << "QwtLinearScaleEngine::divideScale: overflow";
597 return QwtScaleDiv();
598 }
599
[4271]600 if ( interval.width() <= 0 )
601 return QwtScaleDiv();
602
603 stepSize = qAbs( stepSize );
604 if ( stepSize == 0.0 )
605 {
[8127]606 if ( maxMajorSteps < 1 )
607 maxMajorSteps = 1;
[4271]608
[9383]609 stepSize = QwtScaleArithmetic::divideInterval(
[8127]610 interval.width(), maxMajorSteps, base() );
[4271]611 }
612
613 QwtScaleDiv scaleDiv;
614
615 if ( stepSize != 0.0 )
616 {
617 QList<double> ticks[QwtScaleDiv::NTickTypes];
[8127]618 buildTicks( interval, stepSize, maxMinorSteps, ticks );
[4271]619
620 scaleDiv = QwtScaleDiv( interval, ticks );
621 }
622
623 if ( x1 > x2 )
624 scaleDiv.invert();
625
626 return scaleDiv;
627}
628
629/*!
630 \brief Calculate ticks for an interval
631
632 \param interval Interval
633 \param stepSize Step size
[8127]634 \param maxMinorSteps Maximum number of minor steps
[4271]635 \param ticks Arrays to be filled with the calculated ticks
636
637 \sa buildMajorTicks(), buildMinorTicks
638*/
639void QwtLinearScaleEngine::buildTicks(
[8127]640 const QwtInterval& interval, double stepSize, int maxMinorSteps,
[4271]641 QList<double> ticks[QwtScaleDiv::NTickTypes] ) const
642{
[8127]643 const QwtInterval boundingInterval = align( interval, stepSize );
[4271]644
645 ticks[QwtScaleDiv::MajorTick] =
646 buildMajorTicks( boundingInterval, stepSize );
647
[8127]648 if ( maxMinorSteps > 0 )
[4271]649 {
[8127]650 buildMinorTicks( ticks[QwtScaleDiv::MajorTick], maxMinorSteps, stepSize,
[4271]651 ticks[QwtScaleDiv::MinorTick], ticks[QwtScaleDiv::MediumTick] );
652 }
653
654 for ( int i = 0; i < QwtScaleDiv::NTickTypes; i++ )
655 {
656 ticks[i] = strip( ticks[i], interval );
657
658 // ticks very close to 0.0 are
659 // explicitely set to 0.0
660
661 for ( int j = 0; j < ticks[i].count(); j++ )
662 {
663 if ( qwtFuzzyCompare( ticks[i][j], 0.0, stepSize ) == 0 )
664 ticks[i][j] = 0.0;
665 }
666 }
667}
668
669/*!
670 \brief Calculate major ticks for an interval
671
672 \param interval Interval
673 \param stepSize Step size
674
675 \return Calculated ticks
676*/
677QList<double> QwtLinearScaleEngine::buildMajorTicks(
678 const QwtInterval &interval, double stepSize ) const
679{
680 int numTicks = qRound( interval.width() / stepSize ) + 1;
681 if ( numTicks > 10000 )
682 numTicks = 10000;
683
684 QList<double> ticks;
685
686 ticks += interval.minValue();
687 for ( int i = 1; i < numTicks - 1; i++ )
688 ticks += interval.minValue() + i * stepSize;
689 ticks += interval.maxValue();
690
691 return ticks;
692}
693
694/*!
695 \brief Calculate minor/medium ticks for major ticks
696
697 \param majorTicks Major ticks
[8127]698 \param maxMinorSteps Maximum number of minor steps
[4271]699 \param stepSize Step size
700 \param minorTicks Array to be filled with the calculated minor ticks
701 \param mediumTicks Array to be filled with the calculated medium ticks
702
703*/
704void QwtLinearScaleEngine::buildMinorTicks(
705 const QList<double>& majorTicks,
[8127]706 int maxMinorSteps, double stepSize,
[4271]707 QList<double> &minorTicks,
708 QList<double> &mediumTicks ) const
709{
[8127]710 double minStep = qwtStepSize( stepSize, maxMinorSteps, base() );
[4271]711 if ( minStep == 0.0 )
712 return;
713
714 // # ticks per interval
[8127]715 const int numTicks = qCeil( qAbs( stepSize / minStep ) ) - 1;
[4271]716
717 int medIndex = -1;
718 if ( numTicks % 2 )
719 medIndex = numTicks / 2;
720
721 // calculate minor ticks
722
723 for ( int i = 0; i < majorTicks.count(); i++ )
724 {
725 double val = majorTicks[i];
726 for ( int k = 0; k < numTicks; k++ )
727 {
728 val += minStep;
729
730 double alignedValue = val;
731 if ( qwtFuzzyCompare( val, 0.0, stepSize ) == 0 )
732 alignedValue = 0.0;
733
734 if ( k == medIndex )
735 mediumTicks += alignedValue;
736 else
737 minorTicks += alignedValue;
738 }
739 }
740}
741
742/*!
743 \brief Align an interval to a step size
744
745 The limits of an interval are aligned that both are integer
746 multiples of the step size.
747
748 \param interval Interval
749 \param stepSize Step size
750
751 \return Aligned interval
752*/
753QwtInterval QwtLinearScaleEngine::align(
754 const QwtInterval &interval, double stepSize ) const
755{
[8127]756 double x1 = interval.minValue();
757 double x2 = interval.maxValue();
[4271]758
[9383]759 // when there is no rounding beside some effect, when
[8127]760 // calculating with doubles, we keep the original value
[4271]761
[8127]762 const double eps = 0.000000000001; // since Qt 4.8: qFuzzyIsNull
763 if ( -DBL_MAX + stepSize <= x1 )
764 {
765 const double x = QwtScaleArithmetic::floorEps( x1, stepSize );
766 if ( qAbs(x) <= eps || !qFuzzyCompare( x1, x ) )
767 x1 = x;
768 }
769
770 if ( DBL_MAX - stepSize >= x2 )
771 {
772 const double x = QwtScaleArithmetic::ceilEps( x2, stepSize );
773 if ( qAbs(x) <= eps || !qFuzzyCompare( x2, x ) )
774 x2 = x;
775 }
776
[4271]777 return QwtInterval( x1, x2 );
778}
779
780/*!
[8127]781 Constructor
782
783 \param base Base of the scale engine
784 \sa setBase()
785 */
786QwtLogScaleEngine::QwtLogScaleEngine( uint base ):
787 QwtScaleEngine( base )
[4271]788{
[8127]789 setTransformation( new QwtLogTransform() );
[4271]790}
791
[8127]792//! Destructor
793QwtLogScaleEngine::~QwtLogScaleEngine()
794{
795}
796
[4271]797/*!
798 Align and divide an interval
799
800 \param maxNumSteps Max. number of steps
801 \param x1 First limit of the interval (In/Out)
802 \param x2 Second limit of the interval (In/Out)
803 \param stepSize Step size (Out)
804
805 \sa QwtScaleEngine::setAttribute()
806*/
[8127]807void QwtLogScaleEngine::autoScale( int maxNumSteps,
808 double &x1, double &x2, double &stepSize ) const
[4271]809{
810 if ( x1 > x2 )
811 qSwap( x1, x2 );
812
[8127]813 const double logBase = base();
[4271]814
[8127]815 QwtInterval interval( x1 / qPow( logBase, lowerMargin() ),
816 x2 * qPow( logBase, upperMargin() ) );
817
818 if ( interval.maxValue() / interval.minValue() < logBase )
[4271]819 {
[8127]820 // scale width is less than one step -> try to build a linear scale
[4271]821
822 QwtLinearScaleEngine linearScaler;
823 linearScaler.setAttributes( attributes() );
824 linearScaler.setReference( reference() );
825 linearScaler.setMargins( lowerMargin(), upperMargin() );
826
827 linearScaler.autoScale( maxNumSteps, x1, x2, stepSize );
828
[8127]829 QwtInterval linearInterval = QwtInterval( x1, x2 ).normalized();
830 linearInterval = linearInterval.limited( LOG_MIN, LOG_MAX );
831
832 if ( linearInterval.maxValue() / linearInterval.minValue() < logBase )
833 {
834 // the aligned scale is still less than one step
835
836#if 1
837 // this code doesn't make any sense, but for compatibility
838 // reasons we keep it until 6.2. But it will be ignored
839 // in divideScale
840
841 if ( stepSize < 0.0 )
842 stepSize = -qwtLog( logBase, qAbs( stepSize ) );
843 else
844 stepSize = qwtLog( logBase, stepSize );
845#endif
846
847 return;
848 }
[4271]849 }
850
851 double logRef = 1.0;
852 if ( reference() > LOG_MIN / 2 )
853 logRef = qMin( reference(), LOG_MAX / 2 );
854
855 if ( testAttribute( QwtScaleEngine::Symmetric ) )
856 {
857 const double delta = qMax( interval.maxValue() / logRef,
858 logRef / interval.minValue() );
859 interval.setInterval( logRef / delta, logRef * delta );
860 }
861
862 if ( testAttribute( QwtScaleEngine::IncludeReference ) )
863 interval = interval.extend( logRef );
864
865 interval = interval.limited( LOG_MIN, LOG_MAX );
866
867 if ( interval.width() == 0.0 )
868 interval = buildInterval( interval.minValue() );
869
[9383]870 stepSize = divideInterval( qwtLogInterval( logBase, interval ).width(),
[8127]871 qMax( maxNumSteps, 1 ) );
[4271]872 if ( stepSize < 1.0 )
873 stepSize = 1.0;
874
875 if ( !testAttribute( QwtScaleEngine::Floating ) )
876 interval = align( interval, stepSize );
877
878 x1 = interval.minValue();
879 x2 = interval.maxValue();
880
881 if ( testAttribute( QwtScaleEngine::Inverted ) )
882 {
883 qSwap( x1, x2 );
884 stepSize = -stepSize;
885 }
886}
887
888/*!
[8127]889 \brief Calculate a scale division for an interval
[4271]890
891 \param x1 First interval limit
892 \param x2 Second interval limit
[8127]893 \param maxMajorSteps Maximum for the number of major steps
894 \param maxMinorSteps Maximum number of minor steps
895 \param stepSize Step size. If stepSize == 0, the engine
[4271]896 calculates one.
897
[8127]898 \return Calculated scale division
[4271]899*/
[8127]900QwtScaleDiv QwtLogScaleEngine::divideScale( double x1, double x2,
901 int maxMajorSteps, int maxMinorSteps, double stepSize ) const
[4271]902{
903 QwtInterval interval = QwtInterval( x1, x2 ).normalized();
904 interval = interval.limited( LOG_MIN, LOG_MAX );
905
906 if ( interval.width() <= 0 )
907 return QwtScaleDiv();
908
[8127]909 const double logBase = base();
910
911 if ( interval.maxValue() / interval.minValue() < logBase )
[4271]912 {
913 // scale width is less than one decade -> build linear scale
914
915 QwtLinearScaleEngine linearScaler;
916 linearScaler.setAttributes( attributes() );
917 linearScaler.setReference( reference() );
918 linearScaler.setMargins( lowerMargin(), upperMargin() );
919
920 return linearScaler.divideScale( x1, x2,
[8127]921 maxMajorSteps, maxMinorSteps, 0.0 );
[4271]922 }
923
924 stepSize = qAbs( stepSize );
925 if ( stepSize == 0.0 )
926 {
[8127]927 if ( maxMajorSteps < 1 )
928 maxMajorSteps = 1;
[4271]929
[9383]930 stepSize = divideInterval(
[8127]931 qwtLogInterval( logBase, interval ).width(), maxMajorSteps );
[4271]932 if ( stepSize < 1.0 )
933 stepSize = 1.0; // major step must be >= 1 decade
934 }
935
936 QwtScaleDiv scaleDiv;
937 if ( stepSize != 0.0 )
938 {
939 QList<double> ticks[QwtScaleDiv::NTickTypes];
[8127]940 buildTicks( interval, stepSize, maxMinorSteps, ticks );
[4271]941
942 scaleDiv = QwtScaleDiv( interval, ticks );
943 }
944
945 if ( x1 > x2 )
946 scaleDiv.invert();
947
948 return scaleDiv;
949}
950
951/*!
952 \brief Calculate ticks for an interval
953
954 \param interval Interval
[8127]955 \param maxMinorSteps Maximum number of minor steps
[4271]956 \param stepSize Step size
957 \param ticks Arrays to be filled with the calculated ticks
958
959 \sa buildMajorTicks(), buildMinorTicks
960*/
[8127]961void QwtLogScaleEngine::buildTicks(
962 const QwtInterval& interval, double stepSize, int maxMinorSteps,
[4271]963 QList<double> ticks[QwtScaleDiv::NTickTypes] ) const
964{
965 const QwtInterval boundingInterval = align( interval, stepSize );
966
967 ticks[QwtScaleDiv::MajorTick] =
968 buildMajorTicks( boundingInterval, stepSize );
969
[8127]970 if ( maxMinorSteps > 0 )
[4271]971 {
[8127]972 buildMinorTicks( ticks[QwtScaleDiv::MajorTick], maxMinorSteps, stepSize,
973 ticks[QwtScaleDiv::MinorTick], ticks[QwtScaleDiv::MediumTick] );
[4271]974 }
975
976 for ( int i = 0; i < QwtScaleDiv::NTickTypes; i++ )
977 ticks[i] = strip( ticks[i], interval );
978}
979
980/*!
981 \brief Calculate major ticks for an interval
982
983 \param interval Interval
984 \param stepSize Step size
985
986 \return Calculated ticks
987*/
[8127]988QList<double> QwtLogScaleEngine::buildMajorTicks(
[4271]989 const QwtInterval &interval, double stepSize ) const
990{
[8127]991 double width = qwtLogInterval( base(), interval ).width();
[4271]992
993 int numTicks = qRound( width / stepSize ) + 1;
994 if ( numTicks > 10000 )
995 numTicks = 10000;
996
997 const double lxmin = ::log( interval.minValue() );
998 const double lxmax = ::log( interval.maxValue() );
999 const double lstep = ( lxmax - lxmin ) / double( numTicks - 1 );
1000
1001 QList<double> ticks;
1002
1003 ticks += interval.minValue();
1004
1005 for ( int i = 1; i < numTicks - 1; i++ )
1006 ticks += qExp( lxmin + double( i ) * lstep );
1007
1008 ticks += interval.maxValue();
1009
1010 return ticks;
1011}
1012
1013/*!
1014 \brief Calculate minor/medium ticks for major ticks
1015
1016 \param majorTicks Major ticks
[8127]1017 \param maxMinorSteps Maximum number of minor steps
[4271]1018 \param stepSize Step size
[8127]1019 \param minorTicks Array to be filled with the calculated minor ticks
1020 \param mediumTicks Array to be filled with the calculated medium ticks
[4271]1021*/
[8127]1022void QwtLogScaleEngine::buildMinorTicks(
[4271]1023 const QList<double> &majorTicks,
[8127]1024 int maxMinorSteps, double stepSize,
1025 QList<double> &minorTicks,
1026 QList<double> &mediumTicks ) const
[4271]1027{
[8127]1028 const double logBase = base();
1029
1030 if ( stepSize < 1.1 ) // major step width is one base
[4271]1031 {
[8127]1032 double minStep = divideInterval( stepSize, maxMinorSteps + 1 );
1033 if ( minStep == 0.0 )
1034 return;
[4271]1035
[9383]1036 const int numSteps = qRound( stepSize / minStep );
1037
[8127]1038 int mediumTickIndex = -1;
1039 if ( ( numSteps > 2 ) && ( numSteps % 2 == 0 ) )
1040 mediumTickIndex = numSteps / 2;
[4271]1041
[8127]1042 for ( int i = 0; i < majorTicks.count() - 1; i++ )
[4271]1043 {
[8127]1044 const double v = majorTicks[i];
1045 const double s = logBase / numSteps;
[4271]1046
[8127]1047 if ( s >= 1.0 )
1048 {
1049 if ( !qFuzzyCompare( s, 1.0 ) )
1050 minorTicks += v * s;
[4271]1051
[8127]1052 for ( int j = 2; j < numSteps; j++ )
1053 {
1054 minorTicks += v * j * s;
1055 }
1056 }
1057 else
1058 {
1059 for ( int j = 1; j < numSteps; j++ )
1060 {
1061 const double tick = v + j * v * ( logBase - 1 ) / numSteps;
1062 if ( j == mediumTickIndex )
1063 mediumTicks += tick;
1064 else
1065 minorTicks += tick;
1066 }
1067 }
[4271]1068 }
1069 }
[8127]1070 else
[4271]1071 {
[8127]1072 double minStep = divideInterval( stepSize, maxMinorSteps );
[4271]1073 if ( minStep == 0.0 )
[8127]1074 return;
[4271]1075
1076 if ( minStep < 1.0 )
1077 minStep = 1.0;
1078
1079 // # subticks per interval
[8127]1080 int numTicks = qRound( stepSize / minStep ) - 1;
[4271]1081
1082 // Do the minor steps fit into the interval?
[8127]1083 if ( qwtFuzzyCompare( ( numTicks + 1 ) * minStep,
1084 stepSize, stepSize ) > 0 )
[4271]1085 {
[8127]1086 numTicks = 0;
[4271]1087 }
1088
[8127]1089 if ( numTicks < 1 )
[9383]1090 return;
[4271]1091
[8127]1092 int mediumTickIndex = -1;
1093 if ( ( numTicks > 2 ) && ( numTicks % 2 ) )
1094 mediumTickIndex = numTicks / 2;
[4271]1095
[8127]1096 // substep factor = base^substeps
1097 const qreal minFactor = qMax( qPow( logBase, minStep ), qreal( logBase ) );
1098
[4271]1099 for ( int i = 0; i < majorTicks.count(); i++ )
1100 {
[8127]1101 double tick = majorTicks[i];
1102 for ( int j = 0; j < numTicks; j++ )
[4271]1103 {
[8127]1104 tick *= minFactor;
1105
1106 if ( j == mediumTickIndex )
1107 mediumTicks += tick;
1108 else
1109 minorTicks += tick;
[4271]1110 }
1111 }
1112 }
1113}
1114
1115/*!
1116 \brief Align an interval to a step size
1117
1118 The limits of an interval are aligned that both are integer
1119 multiples of the step size.
1120
1121 \param interval Interval
1122 \param stepSize Step size
1123
1124 \return Aligned interval
1125*/
[8127]1126QwtInterval QwtLogScaleEngine::align(
[4271]1127 const QwtInterval &interval, double stepSize ) const
1128{
[8127]1129 const QwtInterval intv = qwtLogInterval( base(), interval );
[4271]1130
1131 double x1 = QwtScaleArithmetic::floorEps( intv.minValue(), stepSize );
1132 if ( qwtFuzzyCompare( interval.minValue(), x1, stepSize ) == 0 )
1133 x1 = interval.minValue();
1134
1135 double x2 = QwtScaleArithmetic::ceilEps( intv.maxValue(), stepSize );
1136 if ( qwtFuzzyCompare( interval.maxValue(), x2, stepSize ) == 0 )
1137 x2 = interval.maxValue();
1138
[8127]1139 return qwtPowInterval( base(), QwtInterval( x1, x2 ) );
[4271]1140}
Note: See TracBrowser for help on using the repository browser.