qwt_scale_engine.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. #ifndef QWT_SCALE_ENGINE_H
  10. #define QWT_SCALE_ENGINE_H
  11. #include "qwt_global.h"
  12. #include "qwt_scale_div.h"
  13. #include "qwt_interval.h"
  14. class QwtTransform;
  15. /*!
  16. \brief Arithmetic including a tolerance
  17. */
  18. class QWT_EXPORT QwtScaleArithmetic
  19. {
  20. public:
  21. static double ceilEps( double value, double intervalSize );
  22. static double floorEps( double value, double intervalSize );
  23. static double divideEps( double intervalSize, double numSteps );
  24. static double divideInterval( double intervalSize,
  25. int numSteps, uint base );
  26. };
  27. /*!
  28. \brief Base class for scale engines.
  29. A scale engine tries to find "reasonable" ranges and step sizes
  30. for scales.
  31. The layout of the scale can be varied with setAttribute().
  32. Qwt offers implementations for logarithmic and linear scales.
  33. */
  34. class QWT_EXPORT QwtScaleEngine
  35. {
  36. public:
  37. /*!
  38. Layout attributes
  39. \sa setAttribute(), testAttribute(), reference(),
  40. lowerMargin(), upperMargin()
  41. */
  42. enum Attribute
  43. {
  44. //! No attributes
  45. NoAttribute = 0x00,
  46. //! Build a scale which includes the reference() value.
  47. IncludeReference = 0x01,
  48. //! Build a scale which is symmetric to the reference() value.
  49. Symmetric = 0x02,
  50. /*!
  51. The endpoints of the scale are supposed to be equal the
  52. outmost included values plus the specified margins
  53. (see setMargins()).
  54. If this attribute is *not* set, the endpoints of the scale will
  55. be integer multiples of the step size.
  56. */
  57. Floating = 0x04,
  58. //! Turn the scale upside down.
  59. Inverted = 0x08
  60. };
  61. //! Layout attributes
  62. typedef QFlags<Attribute> Attributes;
  63. explicit QwtScaleEngine( uint base = 10 );
  64. virtual ~QwtScaleEngine();
  65. void setBase( uint base );
  66. uint base() const;
  67. void setAttribute( Attribute, bool on = true );
  68. bool testAttribute( Attribute ) const;
  69. void setAttributes( Attributes );
  70. Attributes attributes() const;
  71. void setReference( double );
  72. double reference() const;
  73. void setMargins( double lower, double upper );
  74. double lowerMargin() const;
  75. double upperMargin() const;
  76. /*!
  77. Align and divide an interval
  78. \param maxNumSteps Max. number of steps
  79. \param x1 First limit of the interval (In/Out)
  80. \param x2 Second limit of the interval (In/Out)
  81. \param stepSize Step size (Return value)
  82. */
  83. virtual void autoScale( int maxNumSteps,
  84. double &x1, double &x2, double &stepSize ) const = 0;
  85. /*!
  86. \brief Calculate a scale division
  87. \param x1 First interval limit
  88. \param x2 Second interval limit
  89. \param maxMajorSteps Maximum for the number of major steps
  90. \param maxMinorSteps Maximum number of minor steps
  91. \param stepSize Step size. If stepSize == 0.0, the scaleEngine
  92. calculates one.
  93. \return Calculated scale division
  94. */
  95. virtual QwtScaleDiv divideScale( double x1, double x2,
  96. int maxMajorSteps, int maxMinorSteps,
  97. double stepSize = 0.0 ) const = 0;
  98. void setTransformation( QwtTransform * );
  99. QwtTransform *transformation() const;
  100. protected:
  101. bool contains( const QwtInterval &, double value ) const;
  102. QList<double> strip( const QList<double>&, const QwtInterval & ) const;
  103. double divideInterval( double intervalSize, int numSteps ) const;
  104. QwtInterval buildInterval( double value ) const;
  105. private:
  106. class PrivateData;
  107. PrivateData *d_data;
  108. };
  109. /*!
  110. \brief A scale engine for linear scales
  111. The step size will fit into the pattern
  112. \f$\left\{ 1,2,5\right\} \cdot 10^{n}\f$, where n is an integer.
  113. */
  114. class QWT_EXPORT QwtLinearScaleEngine: public QwtScaleEngine
  115. {
  116. public:
  117. QwtLinearScaleEngine( uint base = 10 );
  118. virtual ~QwtLinearScaleEngine();
  119. virtual void autoScale( int maxNumSteps,
  120. double &x1, double &x2, double &stepSize ) const;
  121. virtual QwtScaleDiv divideScale( double x1, double x2,
  122. int maxMajorSteps, int maxMinorSteps,
  123. double stepSize = 0.0 ) const;
  124. protected:
  125. QwtInterval align( const QwtInterval&, double stepSize ) const;
  126. void buildTicks(
  127. const QwtInterval &, double stepSize, int maxMinorSteps,
  128. QList<double> ticks[QwtScaleDiv::NTickTypes] ) const;
  129. QList<double> buildMajorTicks(
  130. const QwtInterval &interval, double stepSize ) const;
  131. void buildMinorTicks( const QList<double>& majorTicks,
  132. int maxMinorSteps, double stepSize,
  133. QList<double> &minorTicks, QList<double> &mediumTicks ) const;
  134. };
  135. /*!
  136. \brief A scale engine for logarithmic scales
  137. The step size is measured in *decades*
  138. and the major step size will be adjusted to fit the pattern
  139. \f$\left\{ 1,2,3,5\right\} \cdot 10^{n}\f$, where n is a natural number
  140. including zero.
  141. \warning the step size as well as the margins are measured in *decades*.
  142. */
  143. class QWT_EXPORT QwtLogScaleEngine: public QwtScaleEngine
  144. {
  145. public:
  146. QwtLogScaleEngine( uint base = 10 );
  147. virtual ~QwtLogScaleEngine();
  148. virtual void autoScale( int maxNumSteps,
  149. double &x1, double &x2, double &stepSize ) const;
  150. virtual QwtScaleDiv divideScale( double x1, double x2,
  151. int maxMajorSteps, int maxMinorSteps,
  152. double stepSize = 0.0 ) const;
  153. protected:
  154. QwtInterval align( const QwtInterval&, double stepSize ) const;
  155. void buildTicks(
  156. const QwtInterval &, double stepSize, int maxMinorSteps,
  157. QList<double> ticks[QwtScaleDiv::NTickTypes] ) const;
  158. QList<double> buildMajorTicks(
  159. const QwtInterval &interval, double stepSize ) const;
  160. void buildMinorTicks( const QList<double>& majorTicks,
  161. int maxMinorSteps, double stepSize,
  162. QList<double> &minorTicks, QList<double> &mediumTicks ) const;
  163. };
  164. Q_DECLARE_OPERATORS_FOR_FLAGS( QwtScaleEngine::Attributes )
  165. #endif