qwt_interval.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. #include <qmetatype.h>
  13. #ifndef QT_NO_DEBUG_STREAM
  14. #include <qdebug.h>
  15. #endif
  16. /*!
  17. \brief A class representing an interval
  18. The interval is represented by 2 doubles, the lower and the upper limit.
  19. */
  20. class QWT_EXPORT QwtInterval
  21. {
  22. public:
  23. /*!
  24. Flag indicating if a border is included or excluded
  25. \sa setBorderFlags(), borderFlags()
  26. */
  27. enum BorderFlag
  28. {
  29. //! Min/Max values are inside the interval
  30. IncludeBorders = 0x00,
  31. //! Min value is not included in the interval
  32. ExcludeMinimum = 0x01,
  33. //! Max value is not included in the interval
  34. ExcludeMaximum = 0x02,
  35. //! Min/Max values are not included in the interval
  36. ExcludeBorders = ExcludeMinimum | ExcludeMaximum
  37. };
  38. //! Border flags
  39. typedef QFlags<BorderFlag> BorderFlags;
  40. QwtInterval();
  41. QwtInterval( double minValue, double maxValue,
  42. BorderFlags = IncludeBorders );
  43. void setInterval( double minValue, double maxValue,
  44. BorderFlags = IncludeBorders );
  45. QwtInterval normalized() const;
  46. QwtInterval inverted() const;
  47. QwtInterval limited( double lowerBound, double upperBound ) const;
  48. bool operator==( const QwtInterval & ) const;
  49. bool operator!=( const QwtInterval & ) const;
  50. void setBorderFlags( BorderFlags );
  51. BorderFlags borderFlags() const;
  52. double minValue() const;
  53. double maxValue() const;
  54. double width() const;
  55. void setMinValue( double );
  56. void setMaxValue( double );
  57. bool contains( double value ) const;
  58. bool intersects( const QwtInterval & ) const;
  59. QwtInterval intersect( const QwtInterval & ) const;
  60. QwtInterval unite( const QwtInterval & ) const;
  61. QwtInterval operator|( const QwtInterval & ) const;
  62. QwtInterval operator&( const QwtInterval & ) const;
  63. QwtInterval &operator|=( const QwtInterval & );
  64. QwtInterval &operator&=( const QwtInterval & );
  65. QwtInterval extend( double value ) const;
  66. QwtInterval operator|( double ) const;
  67. QwtInterval &operator|=( double );
  68. bool isValid() const;
  69. bool isNull() const;
  70. void invalidate();
  71. QwtInterval symmetrize( double value ) const;
  72. private:
  73. double d_minValue;
  74. double d_maxValue;
  75. BorderFlags d_borderFlags;
  76. };
  77. Q_DECLARE_TYPEINFO(QwtInterval, Q_MOVABLE_TYPE);
  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, BorderFlags 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, BorderFlags 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( BorderFlags borderFlags )
  122. {
  123. d_borderFlags = borderFlags;
  124. }
  125. /*!
  126. \return Border flags
  127. \sa setBorderFlags()
  128. */
  129. inline QwtInterval::BorderFlags 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. A interval is valid when minValue() <= maxValue().
  161. In case of QwtInterval::ExcludeBorders it is true
  162. when minValue() < maxValue()
  163. \return True, when the interval is valid
  164. */
  165. inline bool QwtInterval::isValid() const
  166. {
  167. if ( ( d_borderFlags & ExcludeBorders ) == 0 )
  168. return d_minValue <= d_maxValue;
  169. else
  170. return d_minValue < d_maxValue;
  171. }
  172. /*!
  173. \brief Return the width of an interval
  174. The width of invalid intervals is 0.0, otherwise the result is
  175. maxValue() - minValue().
  176. \return Interval width
  177. \sa isValid()
  178. */
  179. inline double QwtInterval::width() const
  180. {
  181. return isValid() ? ( d_maxValue - d_minValue ) : 0.0;
  182. }
  183. /*!
  184. \brief Intersection of two intervals
  185. \param other Interval to intersect with
  186. \return Intersection of this and other
  187. \sa intersect()
  188. */
  189. inline QwtInterval QwtInterval::operator&(
  190. const QwtInterval &other ) const
  191. {
  192. return intersect( other );
  193. }
  194. /*!
  195. Union of two intervals
  196. \param other Interval to unite with
  197. \return Union of this and other
  198. \sa unite()
  199. */
  200. inline QwtInterval QwtInterval::operator|(
  201. const QwtInterval &other ) const
  202. {
  203. return unite( other );
  204. }
  205. /*!
  206. \brief Compare two intervals
  207. \param other Interval to compare with
  208. \return True, when this and other are equal
  209. */
  210. inline bool QwtInterval::operator==( const QwtInterval &other ) const
  211. {
  212. return ( d_minValue == other.d_minValue ) &&
  213. ( d_maxValue == other.d_maxValue ) &&
  214. ( d_borderFlags == other.d_borderFlags );
  215. }
  216. /*!
  217. \brief Compare two intervals
  218. \param other Interval to compare with
  219. \return True, when this and other are not equal
  220. */
  221. inline bool QwtInterval::operator!=( const QwtInterval &other ) const
  222. {
  223. return ( !( *this == other ) );
  224. }
  225. /*!
  226. Extend an interval
  227. \param value Value
  228. \return Extended interval
  229. \sa extend()
  230. */
  231. inline QwtInterval QwtInterval::operator|( double value ) const
  232. {
  233. return extend( value );
  234. }
  235. //! \return true, if isValid() && (minValue() >= maxValue())
  236. inline bool QwtInterval::isNull() const
  237. {
  238. return isValid() && d_minValue >= d_maxValue;
  239. }
  240. /*!
  241. Invalidate the interval
  242. The limits are set to interval [0.0, -1.0]
  243. \sa isValid()
  244. */
  245. inline void QwtInterval::invalidate()
  246. {
  247. d_minValue = 0.0;
  248. d_maxValue = -1.0;
  249. }
  250. Q_DECLARE_OPERATORS_FOR_FLAGS( QwtInterval::BorderFlags )
  251. Q_DECLARE_METATYPE( QwtInterval )
  252. #ifndef QT_NO_DEBUG_STREAM
  253. QWT_EXPORT QDebug operator<<( QDebug, const QwtInterval & );
  254. #endif
  255. #endif