qwt_interval.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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_INTERVAL_H
  10. #define QWT_INTERVAL_H
  11. #include "qwt_global.h"
  12. #ifndef QT_NO_DEBUG_STREAM
  13. #include <qdebug.h>
  14. #endif
  15. /*!
  16. \brief A class representing an interval
  17. The interval is represented by 2 doubles, the lower and the upper limit.
  18. */
  19. class QWT_EXPORT QwtInterval
  20. {
  21. public:
  22. /*!
  23. Flag indicating if a border is included/excluded from an interval
  24. - IncludeBorders\n
  25. min/max values are inside the interval
  26. - ExcludeMinimum\n
  27. min value is not included in the interval
  28. - ExcludeMaximum\n
  29. max value is not included in the interval
  30. - ExcludeBorders\n
  31. min/max values are not included in the interval
  32. \sa setBorderMode(), testBorderMode()
  33. */
  34. enum BorderMode
  35. {
  36. IncludeBorders = 0,
  37. ExcludeMinimum = 1,
  38. ExcludeMaximum = 2,
  39. ExcludeBorders = ExcludeMinimum | ExcludeMaximum
  40. };
  41. QwtInterval();
  42. QwtInterval( double minValue, double maxValue,
  43. int borderFlags = IncludeBorders );
  44. void setInterval( double minValue, double maxValue,
  45. int borderFlags = IncludeBorders );
  46. QwtInterval normalized() const;
  47. QwtInterval inverted() const;
  48. QwtInterval limited( double minValue, double maxValue ) const;
  49. bool operator==( const QwtInterval & ) const;
  50. bool operator!=( const QwtInterval & ) const;
  51. void setBorderFlags( int );
  52. int borderFlags() const;
  53. double minValue() const;
  54. double maxValue() const;
  55. double width() const;
  56. void setMinValue( double );
  57. void setMaxValue( double );
  58. bool contains( double value ) const;
  59. bool intersects( const QwtInterval & ) const;
  60. QwtInterval intersect( const QwtInterval & ) const;
  61. QwtInterval unite( const QwtInterval & ) const;
  62. QwtInterval operator|( const QwtInterval & ) const;
  63. QwtInterval operator&( const QwtInterval & ) const;
  64. QwtInterval &operator|=( const QwtInterval & );
  65. QwtInterval &operator&=( const QwtInterval & );
  66. QwtInterval extend( double value ) const;
  67. QwtInterval operator|( double ) const;
  68. QwtInterval &operator|=( double );
  69. bool isValid() const;
  70. bool isNull() const;
  71. void invalidate();
  72. QwtInterval symmetrize( double value ) const;
  73. private:
  74. double d_minValue;
  75. double d_maxValue;
  76. int d_borderFlags;
  77. };
  78. /*!
  79. \brief Default Constructor
  80. Creates an invalid interval [0.0, -1.0]
  81. \sa setInterval(), isValid()
  82. */
  83. inline QwtInterval::QwtInterval():
  84. d_minValue( 0.0 ),
  85. d_maxValue( -1.0 ),
  86. d_borderFlags( IncludeBorders )
  87. {
  88. }
  89. /*!
  90. Constructor
  91. Build an interval with from min/max values
  92. \param minValue Minimum value
  93. \param maxValue Maximum value
  94. \param borderFlags Include/Exclude borders
  95. */
  96. inline QwtInterval::QwtInterval(
  97. double minValue, double maxValue, int borderFlags ):
  98. d_minValue( minValue ),
  99. d_maxValue( maxValue ),
  100. d_borderFlags( borderFlags )
  101. {
  102. }
  103. /*!
  104. Assign the limits of the interval
  105. \param minValue Minimum value
  106. \param maxValue Maximum value
  107. \param borderFlags Include/Exclude borders
  108. */
  109. inline void QwtInterval::setInterval(
  110. double minValue, double maxValue, int borderFlags )
  111. {
  112. d_minValue = minValue;
  113. d_maxValue = maxValue;
  114. d_borderFlags = borderFlags;
  115. }
  116. /*!
  117. Change the border flags
  118. \param borderFlags Or'd BorderMode flags
  119. \sa borderFlags()
  120. */
  121. inline void QwtInterval::setBorderFlags( int borderFlags )
  122. {
  123. d_borderFlags = borderFlags;
  124. }
  125. /*!
  126. \return Border flags
  127. \sa setBorderFlags()
  128. */
  129. inline int QwtInterval::borderFlags() const
  130. {
  131. return d_borderFlags;
  132. }
  133. /*!
  134. Assign the lower limit of the interval
  135. \param minValue Minimum value
  136. */
  137. inline void QwtInterval::setMinValue( double minValue )
  138. {
  139. d_minValue = minValue;
  140. }
  141. /*!
  142. Assign the upper limit of the interval
  143. \param maxValue Maximum value
  144. */
  145. inline void QwtInterval::setMaxValue( double maxValue )
  146. {
  147. d_maxValue = maxValue;
  148. }
  149. //! \return Lower limit of the interval
  150. inline double QwtInterval::minValue() const
  151. {
  152. return d_minValue;
  153. }
  154. //! \return Upper limit of the interval
  155. inline double QwtInterval::maxValue() const
  156. {
  157. return d_maxValue;
  158. }
  159. /*!
  160. Return the width of an interval
  161. The width of invalid intervals is 0.0, otherwise the result is
  162. maxValue() - minValue().
  163. \sa isValid()
  164. */
  165. inline double QwtInterval::width() const
  166. {
  167. return isValid() ? ( d_maxValue - d_minValue ) : 0.0;
  168. }
  169. /*!
  170. Intersection of two intervals
  171. \sa intersect()
  172. */
  173. inline QwtInterval QwtInterval::operator&(
  174. const QwtInterval &interval ) const
  175. {
  176. return intersect( interval );
  177. }
  178. /*!
  179. Union of two intervals
  180. \sa unite()
  181. */
  182. inline QwtInterval QwtInterval::operator|(
  183. const QwtInterval &interval ) const
  184. {
  185. return unite( interval );
  186. }
  187. //! Compare two intervals
  188. inline bool QwtInterval::operator==( const QwtInterval &other ) const
  189. {
  190. return ( d_minValue == other.d_minValue ) &&
  191. ( d_maxValue == other.d_maxValue ) &&
  192. ( d_borderFlags == other.d_borderFlags );
  193. }
  194. //! Compare two intervals
  195. inline bool QwtInterval::operator!=( const QwtInterval &other ) const
  196. {
  197. return ( !( *this == other ) );
  198. }
  199. /*!
  200. Extend an interval
  201. \param value Value
  202. \return Extended interval
  203. \sa extend()
  204. */
  205. inline QwtInterval QwtInterval::operator|( double value ) const
  206. {
  207. return extend( value );
  208. }
  209. //! \return true, if isValid() && (minValue() >= maxValue())
  210. inline bool QwtInterval::isNull() const
  211. {
  212. return isValid() && d_minValue >= d_maxValue;
  213. }
  214. /*!
  215. A interval is valid when minValue() <= maxValue().
  216. In case of QwtInterval::ExcludeBorders it is true
  217. when minValue() < maxValue()
  218. */
  219. inline bool QwtInterval::isValid() const
  220. {
  221. if ( ( d_borderFlags & ExcludeBorders ) == 0 )
  222. return d_minValue <= d_maxValue;
  223. else
  224. return d_minValue < d_maxValue;
  225. }
  226. /*!
  227. Invalidate the interval
  228. The limits are set to interval [0.0, -1.0]
  229. \sa isValid()
  230. */
  231. inline void QwtInterval::invalidate()
  232. {
  233. d_minValue = 0.0;
  234. d_maxValue = -1.0;
  235. }
  236. #ifndef QT_NO_DEBUG_STREAM
  237. QWT_EXPORT QDebug operator<<( QDebug, const QwtInterval & );
  238. #endif
  239. #endif