qwt_scale_map.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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_MAP_H
  10. #define QWT_SCALE_MAP_H
  11. #include "qwt_global.h"
  12. #include "qwt_math.h"
  13. #ifndef QT_NO_DEBUG_STREAM
  14. #include <qdebug.h>
  15. #endif
  16. class QRectF;
  17. /*!
  18. \brief Operations for linear or logarithmic (base 10) transformations
  19. */
  20. class QWT_EXPORT QwtScaleTransformation
  21. {
  22. public:
  23. /*!
  24. - Linear\n
  25. Transformation between 2 linear scales
  26. - Log10
  27. Transformation between a linear and a logarithmic ( base 10 ) scale
  28. - Other
  29. Any other type of transformation
  30. */
  31. enum Type
  32. {
  33. Linear,
  34. Log10,
  35. Other
  36. };
  37. QwtScaleTransformation( Type type );
  38. virtual ~QwtScaleTransformation();
  39. virtual double xForm( double x, double s1, double s2,
  40. double p1, double p2 ) const;
  41. virtual double invXForm( double x, double s1, double s2,
  42. double p1, double p2 ) const;
  43. Type type() const;
  44. virtual QwtScaleTransformation *copy() const;
  45. private:
  46. QwtScaleTransformation();
  47. QwtScaleTransformation &operator=( const QwtScaleTransformation );
  48. const Type d_type;
  49. };
  50. //! \return Transformation type
  51. inline QwtScaleTransformation::Type QwtScaleTransformation::type() const
  52. {
  53. return d_type;
  54. }
  55. /*!
  56. \brief A scale map
  57. QwtScaleMap offers transformations from a scale
  58. into a paint interval and vice versa.
  59. */
  60. class QWT_EXPORT QwtScaleMap
  61. {
  62. public:
  63. QwtScaleMap();
  64. QwtScaleMap( const QwtScaleMap& );
  65. ~QwtScaleMap();
  66. QwtScaleMap &operator=( const QwtScaleMap & );
  67. void setTransformation( QwtScaleTransformation * );
  68. const QwtScaleTransformation *transformation() const;
  69. void setPaintInterval( double p1, double p2 );
  70. void setScaleInterval( double s1, double s2 );
  71. double transform( double s ) const;
  72. double invTransform( double p ) const;
  73. double p1() const;
  74. double p2() const;
  75. double s1() const;
  76. double s2() const;
  77. double pDist() const;
  78. double sDist() const;
  79. QT_STATIC_CONST double LogMin;
  80. QT_STATIC_CONST double LogMax;
  81. static QRectF transform( const QwtScaleMap &,
  82. const QwtScaleMap &, const QRectF & );
  83. static QRectF invTransform( const QwtScaleMap &,
  84. const QwtScaleMap &, const QRectF & );
  85. bool isInverting() const;
  86. private:
  87. void newFactor();
  88. double d_s1, d_s2; // scale interval boundaries
  89. double d_p1, d_p2; // paint device interval boundaries
  90. double d_cnv; // conversion factor
  91. QwtScaleTransformation *d_transformation;
  92. };
  93. /*!
  94. \return First border of the scale interval
  95. */
  96. inline double QwtScaleMap::s1() const
  97. {
  98. return d_s1;
  99. }
  100. /*!
  101. \return Second border of the scale interval
  102. */
  103. inline double QwtScaleMap::s2() const
  104. {
  105. return d_s2;
  106. }
  107. /*!
  108. \return First border of the paint interval
  109. */
  110. inline double QwtScaleMap::p1() const
  111. {
  112. return d_p1;
  113. }
  114. /*!
  115. \return Second border of the paint interval
  116. */
  117. inline double QwtScaleMap::p2() const
  118. {
  119. return d_p2;
  120. }
  121. /*!
  122. \return qwtAbs(p2() - p1())
  123. */
  124. inline double QwtScaleMap::pDist() const
  125. {
  126. return qAbs( d_p2 - d_p1 );
  127. }
  128. /*!
  129. \return qwtAbs(s2() - s1())
  130. */
  131. inline double QwtScaleMap::sDist() const
  132. {
  133. return qAbs( d_s2 - d_s1 );
  134. }
  135. /*!
  136. Transform a point related to the scale interval into an point
  137. related to the interval of the paint device
  138. \param s Value relative to the coordinates of the scale
  139. */
  140. inline double QwtScaleMap::transform( double s ) const
  141. {
  142. // try to inline code from QwtScaleTransformation
  143. if ( d_transformation->type() == QwtScaleTransformation::Linear )
  144. return d_p1 + ( s - d_s1 ) * d_cnv;
  145. if ( d_transformation->type() == QwtScaleTransformation::Log10 )
  146. return d_p1 + log( s / d_s1 ) * d_cnv;
  147. return d_transformation->xForm( s, d_s1, d_s2, d_p1, d_p2 );
  148. }
  149. /*!
  150. Transform an paint device value into a value in the
  151. interval of the scale.
  152. \param p Value relative to the coordinates of the paint device
  153. \sa transform()
  154. */
  155. inline double QwtScaleMap::invTransform( double p ) const
  156. {
  157. return d_transformation->invXForm( p, d_p1, d_p2, d_s1, d_s2 );
  158. }
  159. //! \return True, when ( p1() < p2() ) != ( s1() < s2() )
  160. inline bool QwtScaleMap::isInverting() const
  161. {
  162. return ( ( d_p1 < d_p2 ) != ( d_s1 < d_s2 ) );
  163. }
  164. #ifndef QT_NO_DEBUG_STREAM
  165. QWT_EXPORT QDebug operator<<( QDebug, const QwtScaleMap & );
  166. #endif
  167. #endif