qwt_interval.h 7 KB
Newer Older
Bryant's avatar
Bryant committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#ifndef QWT_INTERVAL_H
#define QWT_INTERVAL_H

#include "qwt_global.h"
#include <qmetatype.h>

#ifndef QT_NO_DEBUG_STREAM
#include <qdebug.h>
#endif

/*!
  \brief A class representing an interval

  The interval is represented by 2 doubles, the lower and the upper limit.
*/

class QWT_EXPORT QwtInterval
{
public:
    /*!
      Flag indicating if a border is included or excluded 
      \sa setBorderFlags(), borderFlags()
    */
    enum BorderFlag
    {
        //! Min/Max values are inside the interval
        IncludeBorders = 0x00,

        //! Min value is not included in the interval
        ExcludeMinimum = 0x01,

        //! Max value is not included in the interval
        ExcludeMaximum = 0x02,

        //! Min/Max values are not included in the interval
        ExcludeBorders = ExcludeMinimum | ExcludeMaximum
    };

    //! Border flags
    typedef QFlags<BorderFlag> BorderFlags;

    QwtInterval();
    QwtInterval( double minValue, double maxValue,
        BorderFlags = IncludeBorders );

    void setInterval( double minValue, double maxValue,
        BorderFlags = IncludeBorders );

    QwtInterval normalized() const;
    QwtInterval inverted() const;
    QwtInterval limited( double minValue, double maxValue ) const;

    bool operator==( const QwtInterval & ) const;
    bool operator!=( const QwtInterval & ) const;

    void setBorderFlags( BorderFlags );
    BorderFlags borderFlags() const;

    double minValue() const;
    double maxValue() const;

    double width() const;

    void setMinValue( double );
    void setMaxValue( double );

    bool contains( double value ) const;

    bool intersects( const QwtInterval & ) const;
    QwtInterval intersect( const QwtInterval & ) const;
    QwtInterval unite( const QwtInterval & ) const;

    QwtInterval operator|( const QwtInterval & ) const;
    QwtInterval operator&( const QwtInterval & ) const;

    QwtInterval &operator|=( const QwtInterval & );
    QwtInterval &operator&=( const QwtInterval & );

    QwtInterval extend( double value ) const;
    QwtInterval operator|( double ) const;
    QwtInterval &operator|=( double );

    bool isValid() const;
    bool isNull() const;
    void invalidate();

    QwtInterval symmetrize( double value ) const;

private:
    double d_minValue;
    double d_maxValue;
    BorderFlags d_borderFlags;
};

Q_DECLARE_TYPEINFO(QwtInterval, Q_MOVABLE_TYPE);

/*!
  \brief Default Constructor

  Creates an invalid interval [0.0, -1.0]
  \sa setInterval(), isValid()
*/
inline QwtInterval::QwtInterval():
    d_minValue( 0.0 ),
    d_maxValue( -1.0 ),
    d_borderFlags( IncludeBorders )
{
}

/*!
   Constructor

   Build an interval with from min/max values

   \param minValue Minimum value
   \param maxValue Maximum value
   \param borderFlags Include/Exclude borders
*/
inline QwtInterval::QwtInterval(
        double minValue, double maxValue, BorderFlags borderFlags ):
    d_minValue( minValue ),
    d_maxValue( maxValue ),
    d_borderFlags( borderFlags )
{
}

/*!
   Assign the limits of the interval

   \param minValue Minimum value
   \param maxValue Maximum value
   \param borderFlags Include/Exclude borders
*/
inline void QwtInterval::setInterval(
    double minValue, double maxValue, BorderFlags borderFlags )
{
    d_minValue = minValue;
    d_maxValue = maxValue;
    d_borderFlags = borderFlags;
}

/*!
   Change the border flags

   \param borderFlags Or'd BorderMode flags
   \sa borderFlags()
*/
inline void QwtInterval::setBorderFlags( BorderFlags borderFlags )
{
    d_borderFlags = borderFlags;
}

/*!
   \return Border flags
   \sa setBorderFlags()
*/
inline QwtInterval::BorderFlags QwtInterval::borderFlags() const
{
    return d_borderFlags;
}

/*!
   Assign the lower limit of the interval

   \param minValue Minimum value
*/
inline void QwtInterval::setMinValue( double minValue )
{
    d_minValue = minValue;
}

/*!
   Assign the upper limit of the interval

   \param maxValue Maximum value
*/
inline void QwtInterval::setMaxValue( double maxValue )
{
    d_maxValue = maxValue;
}

//! \return Lower limit of the interval
inline double QwtInterval::minValue() const
{
    return d_minValue;
}

//! \return Upper limit of the interval
inline double QwtInterval::maxValue() const
{
    return d_maxValue;
}

/*!
   A interval is valid when minValue() <= maxValue().
   In case of QwtInterval::ExcludeBorders it is true
   when minValue() < maxValue()

   \return True, when the interval is valid
*/
inline bool QwtInterval::isValid() const
{
    if ( ( d_borderFlags & ExcludeBorders ) == 0 )
        return d_minValue <= d_maxValue;
    else
        return d_minValue < d_maxValue;
}

/*!
   \brief Return the width of an interval

   The width of invalid intervals is 0.0, otherwise the result is
   maxValue() - minValue().

   \return Interval width
   \sa isValid()
*/
inline double QwtInterval::width() const
{
    return isValid() ? ( d_maxValue - d_minValue ) : 0.0;
}

/*!
   \brief Intersection of two intervals
 
   \param other Interval to intersect with
   \return Intersection of this and other

   \sa intersect()
*/
inline QwtInterval QwtInterval::operator&(
    const QwtInterval &other ) const
{
    return intersect( other );
}

/*!
   Union of two intervals

   \param other Interval to unite with
   \return Union of this and other

   \sa unite()
*/
inline QwtInterval QwtInterval::operator|(
    const QwtInterval &other ) const
{
    return unite( other );
}

/*! 
   \brief Compare two intervals

   \param other Interval to compare with
   \return True, when this and other are equal
*/
inline bool QwtInterval::operator==( const QwtInterval &other ) const
{
    return ( d_minValue == other.d_minValue ) &&
           ( d_maxValue == other.d_maxValue ) &&
           ( d_borderFlags == other.d_borderFlags );
}
/*! 
   \brief Compare two intervals

   \param other Interval to compare with
   \return True, when this and other are not equal
*/
inline bool QwtInterval::operator!=( const QwtInterval &other ) const
{
    return ( !( *this == other ) );
}

/*!
   Extend an interval

   \param value Value
   \return Extended interval
   \sa extend()
*/
inline QwtInterval QwtInterval::operator|( double value ) const
{
    return extend( value );
}

//! \return true, if isValid() && (minValue() >= maxValue())
inline bool QwtInterval::isNull() const
{
    return isValid() && d_minValue >= d_maxValue;
}

/*!
  Invalidate the interval

  The limits are set to interval [0.0, -1.0]
  \sa isValid()
*/
inline void QwtInterval::invalidate()
{
    d_minValue = 0.0;
    d_maxValue = -1.0;
}

Q_DECLARE_OPERATORS_FOR_FLAGS( QwtInterval::BorderFlags )
Q_DECLARE_METATYPE( QwtInterval )

#ifndef QT_NO_DEBUG_STREAM
QWT_EXPORT QDebug operator<<( QDebug, const QwtInterval & );
#endif

#endif