[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 | #ifndef QWT_INTERVAL_H
|
---|
| 11 | #define QWT_INTERVAL_H
|
---|
| 12 |
|
---|
| 13 | #include "qwt_global.h"
|
---|
[8127] | 14 | #include <qmetatype.h>
|
---|
| 15 |
|
---|
[4271] | 16 | #ifndef QT_NO_DEBUG_STREAM
|
---|
| 17 | #include <qdebug.h>
|
---|
| 18 | #endif
|
---|
| 19 |
|
---|
| 20 | /*!
|
---|
| 21 | \brief A class representing an interval
|
---|
| 22 |
|
---|
| 23 | The interval is represented by 2 doubles, the lower and the upper limit.
|
---|
| 24 | */
|
---|
| 25 |
|
---|
| 26 | class QWT_EXPORT QwtInterval
|
---|
| 27 | {
|
---|
| 28 | public:
|
---|
| 29 | /*!
|
---|
| 30 | Flag indicating if a border is included or excluded
|
---|
| 31 | \sa setBorderFlags(), borderFlags()
|
---|
| 32 | */
|
---|
| 33 | enum BorderFlag
|
---|
| 34 | {
|
---|
| 35 | //! Min/Max values are inside the interval
|
---|
| 36 | IncludeBorders = 0x00,
|
---|
| 37 |
|
---|
| 38 | //! Min value is not included in the interval
|
---|
| 39 | ExcludeMinimum = 0x01,
|
---|
| 40 |
|
---|
| 41 | //! Max value is not included in the interval
|
---|
| 42 | ExcludeMaximum = 0x02,
|
---|
| 43 |
|
---|
| 44 | //! Min/Max values are not included in the interval
|
---|
| 45 | ExcludeBorders = ExcludeMinimum | ExcludeMaximum
|
---|
| 46 | };
|
---|
| 47 |
|
---|
| 48 | //! Border flags
|
---|
| 49 | typedef QFlags<BorderFlag> BorderFlags;
|
---|
| 50 |
|
---|
| 51 | QwtInterval();
|
---|
| 52 | QwtInterval( double minValue, double maxValue,
|
---|
| 53 | BorderFlags = IncludeBorders );
|
---|
| 54 |
|
---|
| 55 | void setInterval( double minValue, double maxValue,
|
---|
| 56 | BorderFlags = IncludeBorders );
|
---|
| 57 |
|
---|
| 58 | QwtInterval normalized() const;
|
---|
| 59 | QwtInterval inverted() const;
|
---|
| 60 | QwtInterval limited( double minValue, double maxValue ) const;
|
---|
| 61 |
|
---|
| 62 | bool operator==( const QwtInterval & ) const;
|
---|
| 63 | bool operator!=( const QwtInterval & ) const;
|
---|
| 64 |
|
---|
| 65 | void setBorderFlags( BorderFlags );
|
---|
| 66 | BorderFlags borderFlags() const;
|
---|
| 67 |
|
---|
| 68 | double minValue() const;
|
---|
| 69 | double maxValue() const;
|
---|
| 70 |
|
---|
| 71 | double width() const;
|
---|
| 72 |
|
---|
| 73 | void setMinValue( double );
|
---|
| 74 | void setMaxValue( double );
|
---|
| 75 |
|
---|
| 76 | bool contains( double value ) const;
|
---|
| 77 |
|
---|
| 78 | bool intersects( const QwtInterval & ) const;
|
---|
| 79 | QwtInterval intersect( const QwtInterval & ) const;
|
---|
| 80 | QwtInterval unite( const QwtInterval & ) const;
|
---|
| 81 |
|
---|
| 82 | QwtInterval operator|( const QwtInterval & ) const;
|
---|
| 83 | QwtInterval operator&( const QwtInterval & ) const;
|
---|
| 84 |
|
---|
| 85 | QwtInterval &operator|=( const QwtInterval & );
|
---|
| 86 | QwtInterval &operator&=( const QwtInterval & );
|
---|
| 87 |
|
---|
| 88 | QwtInterval extend( double value ) const;
|
---|
| 89 | QwtInterval operator|( double ) const;
|
---|
| 90 | QwtInterval &operator|=( double );
|
---|
| 91 |
|
---|
| 92 | bool isValid() const;
|
---|
| 93 | bool isNull() const;
|
---|
| 94 | void invalidate();
|
---|
| 95 |
|
---|
| 96 | QwtInterval symmetrize( double value ) const;
|
---|
| 97 |
|
---|
| 98 | private:
|
---|
| 99 | double d_minValue;
|
---|
| 100 | double d_maxValue;
|
---|
| 101 | BorderFlags d_borderFlags;
|
---|
| 102 | };
|
---|
| 103 |
|
---|
| 104 | Q_DECLARE_TYPEINFO(QwtInterval, Q_MOVABLE_TYPE);
|
---|
| 105 |
|
---|
| 106 | /*!
|
---|
| 107 | \brief Default Constructor
|
---|
| 108 |
|
---|
| 109 | Creates an invalid interval [0.0, -1.0]
|
---|
| 110 | \sa setInterval(), isValid()
|
---|
| 111 | */
|
---|
| 112 | inline QwtInterval::QwtInterval():
|
---|
| 113 | d_minValue( 0.0 ),
|
---|
| 114 | d_maxValue( -1.0 ),
|
---|
| 115 | d_borderFlags( IncludeBorders )
|
---|
| 116 | {
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | /*!
|
---|
| 120 | Constructor
|
---|
| 121 |
|
---|
| 122 | Build an interval with from min/max values
|
---|
| 123 |
|
---|
| 124 | \param minValue Minimum value
|
---|
| 125 | \param maxValue Maximum value
|
---|
| 126 | \param borderFlags Include/Exclude borders
|
---|
| 127 | */
|
---|
| 128 | inline QwtInterval::QwtInterval(
|
---|
| 129 | double minValue, double maxValue, BorderFlags borderFlags ):
|
---|
| 130 | d_minValue( minValue ),
|
---|
| 131 | d_maxValue( maxValue ),
|
---|
| 132 | d_borderFlags( borderFlags )
|
---|
| 133 | {
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | /*!
|
---|
| 137 | Assign the limits of the interval
|
---|
| 138 |
|
---|
| 139 | \param minValue Minimum value
|
---|
| 140 | \param maxValue Maximum value
|
---|
| 141 | \param borderFlags Include/Exclude borders
|
---|
| 142 | */
|
---|
| 143 | inline void QwtInterval::setInterval(
|
---|
| 144 | double minValue, double maxValue, BorderFlags borderFlags )
|
---|
| 145 | {
|
---|
| 146 | d_minValue = minValue;
|
---|
| 147 | d_maxValue = maxValue;
|
---|
| 148 | d_borderFlags = borderFlags;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | /*!
|
---|
| 152 | Change the border flags
|
---|
| 153 |
|
---|
| 154 | \param borderFlags Or'd BorderMode flags
|
---|
| 155 | \sa borderFlags()
|
---|
| 156 | */
|
---|
| 157 | inline void QwtInterval::setBorderFlags( BorderFlags borderFlags )
|
---|
| 158 | {
|
---|
| 159 | d_borderFlags = borderFlags;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | /*!
|
---|
| 163 | \return Border flags
|
---|
| 164 | \sa setBorderFlags()
|
---|
| 165 | */
|
---|
| 166 | inline QwtInterval::BorderFlags QwtInterval::borderFlags() const
|
---|
| 167 | {
|
---|
| 168 | return d_borderFlags;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | /*!
|
---|
| 172 | Assign the lower limit of the interval
|
---|
| 173 |
|
---|
| 174 | \param minValue Minimum value
|
---|
| 175 | */
|
---|
| 176 | inline void QwtInterval::setMinValue( double minValue )
|
---|
| 177 | {
|
---|
| 178 | d_minValue = minValue;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | /*!
|
---|
| 182 | Assign the upper limit of the interval
|
---|
| 183 |
|
---|
| 184 | \param maxValue Maximum value
|
---|
| 185 | */
|
---|
| 186 | inline void QwtInterval::setMaxValue( double maxValue )
|
---|
| 187 | {
|
---|
| 188 | d_maxValue = maxValue;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | //! \return Lower limit of the interval
|
---|
| 192 | inline double QwtInterval::minValue() const
|
---|
| 193 | {
|
---|
| 194 | return d_minValue;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | //! \return Upper limit of the interval
|
---|
| 198 | inline double QwtInterval::maxValue() const
|
---|
| 199 | {
|
---|
| 200 | return d_maxValue;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | /*!
|
---|
[8127] | 204 | A interval is valid when minValue() <= maxValue().
|
---|
| 205 | In case of QwtInterval::ExcludeBorders it is true
|
---|
| 206 | when minValue() < maxValue()
|
---|
| 207 |
|
---|
| 208 | \return True, when the interval is valid
|
---|
| 209 | */
|
---|
| 210 | inline bool QwtInterval::isValid() const
|
---|
| 211 | {
|
---|
| 212 | if ( ( d_borderFlags & ExcludeBorders ) == 0 )
|
---|
| 213 | return d_minValue <= d_maxValue;
|
---|
| 214 | else
|
---|
| 215 | return d_minValue < d_maxValue;
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | /*!
|
---|
| 219 | \brief Return the width of an interval
|
---|
| 220 |
|
---|
[4271] | 221 | The width of invalid intervals is 0.0, otherwise the result is
|
---|
| 222 | maxValue() - minValue().
|
---|
| 223 |
|
---|
[8127] | 224 | \return Interval width
|
---|
[4271] | 225 | \sa isValid()
|
---|
| 226 | */
|
---|
| 227 | inline double QwtInterval::width() const
|
---|
| 228 | {
|
---|
| 229 | return isValid() ? ( d_maxValue - d_minValue ) : 0.0;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | /*!
|
---|
[8127] | 233 | \brief Intersection of two intervals
|
---|
| 234 |
|
---|
| 235 | \param other Interval to intersect with
|
---|
| 236 | \return Intersection of this and other
|
---|
| 237 |
|
---|
[4271] | 238 | \sa intersect()
|
---|
| 239 | */
|
---|
| 240 | inline QwtInterval QwtInterval::operator&(
|
---|
[8127] | 241 | const QwtInterval &other ) const
|
---|
[4271] | 242 | {
|
---|
[8127] | 243 | return intersect( other );
|
---|
[4271] | 244 | }
|
---|
| 245 |
|
---|
| 246 | /*!
|
---|
| 247 | Union of two intervals
|
---|
[8127] | 248 |
|
---|
| 249 | \param other Interval to unite with
|
---|
| 250 | \return Union of this and other
|
---|
| 251 |
|
---|
[4271] | 252 | \sa unite()
|
---|
| 253 | */
|
---|
| 254 | inline QwtInterval QwtInterval::operator|(
|
---|
[8127] | 255 | const QwtInterval &other ) const
|
---|
[4271] | 256 | {
|
---|
[8127] | 257 | return unite( other );
|
---|
[4271] | 258 | }
|
---|
| 259 |
|
---|
[8127] | 260 | /*!
|
---|
| 261 | \brief Compare two intervals
|
---|
| 262 |
|
---|
| 263 | \param other Interval to compare with
|
---|
| 264 | \return True, when this and other are equal
|
---|
| 265 | */
|
---|
[4271] | 266 | inline bool QwtInterval::operator==( const QwtInterval &other ) const
|
---|
| 267 | {
|
---|
| 268 | return ( d_minValue == other.d_minValue ) &&
|
---|
| 269 | ( d_maxValue == other.d_maxValue ) &&
|
---|
| 270 | ( d_borderFlags == other.d_borderFlags );
|
---|
| 271 | }
|
---|
[8127] | 272 | /*!
|
---|
| 273 | \brief Compare two intervals
|
---|
[4271] | 274 |
|
---|
[8127] | 275 | \param other Interval to compare with
|
---|
| 276 | \return True, when this and other are not equal
|
---|
| 277 | */
|
---|
[4271] | 278 | inline bool QwtInterval::operator!=( const QwtInterval &other ) const
|
---|
| 279 | {
|
---|
| 280 | return ( !( *this == other ) );
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | /*!
|
---|
| 284 | Extend an interval
|
---|
| 285 |
|
---|
| 286 | \param value Value
|
---|
| 287 | \return Extended interval
|
---|
| 288 | \sa extend()
|
---|
| 289 | */
|
---|
| 290 | inline QwtInterval QwtInterval::operator|( double value ) const
|
---|
| 291 | {
|
---|
| 292 | return extend( value );
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | //! \return true, if isValid() && (minValue() >= maxValue())
|
---|
| 296 | inline bool QwtInterval::isNull() const
|
---|
| 297 | {
|
---|
| 298 | return isValid() && d_minValue >= d_maxValue;
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | /*!
|
---|
| 302 | Invalidate the interval
|
---|
| 303 |
|
---|
| 304 | The limits are set to interval [0.0, -1.0]
|
---|
| 305 | \sa isValid()
|
---|
| 306 | */
|
---|
| 307 | inline void QwtInterval::invalidate()
|
---|
| 308 | {
|
---|
| 309 | d_minValue = 0.0;
|
---|
| 310 | d_maxValue = -1.0;
|
---|
| 311 | }
|
---|
| 312 |
|
---|
| 313 | Q_DECLARE_OPERATORS_FOR_FLAGS( QwtInterval::BorderFlags )
|
---|
[8127] | 314 | Q_DECLARE_METATYPE( QwtInterval )
|
---|
[4271] | 315 |
|
---|
| 316 | #ifndef QT_NO_DEBUG_STREAM
|
---|
| 317 | QWT_EXPORT QDebug operator<<( QDebug, const QwtInterval & );
|
---|
| 318 | #endif
|
---|
| 319 |
|
---|
| 320 | #endif
|
---|