[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 | #include "qwt_scale_div.h"
|
---|
| 11 | #include "qwt_math.h"
|
---|
| 12 | #include <qalgorithms.h>
|
---|
| 13 |
|
---|
[8127] | 14 | /*!
|
---|
| 15 | Construct a division without ticks
|
---|
| 16 |
|
---|
| 17 | \param lowerBound First boundary
|
---|
| 18 | \param upperBound Second boundary
|
---|
| 19 |
|
---|
| 20 | \note lowerBound might be greater than upperBound for inverted scales
|
---|
| 21 | */
|
---|
| 22 | QwtScaleDiv::QwtScaleDiv( double lowerBound, double upperBound ):
|
---|
| 23 | d_lowerBound( lowerBound ),
|
---|
| 24 | d_upperBound( upperBound )
|
---|
[4271] | 25 | {
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | /*!
|
---|
[8127] | 29 | Construct a scale division
|
---|
[4271] | 30 |
|
---|
| 31 | \param interval Interval
|
---|
| 32 | \param ticks List of major, medium and minor ticks
|
---|
| 33 | */
|
---|
| 34 | QwtScaleDiv::QwtScaleDiv( const QwtInterval &interval,
|
---|
| 35 | QList<double> ticks[NTickTypes] ):
|
---|
| 36 | d_lowerBound( interval.minValue() ),
|
---|
[8127] | 37 | d_upperBound( interval.maxValue() )
|
---|
[4271] | 38 | {
|
---|
| 39 | for ( int i = 0; i < NTickTypes; i++ )
|
---|
| 40 | d_ticks[i] = ticks[i];
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | /*!
|
---|
[8127] | 44 | Construct a scale division
|
---|
[4271] | 45 |
|
---|
[8127] | 46 | \param lowerBound First boundary
|
---|
| 47 | \param upperBound Second boundary
|
---|
[4271] | 48 | \param ticks List of major, medium and minor ticks
|
---|
[8127] | 49 |
|
---|
| 50 | \note lowerBound might be greater than upperBound for inverted scales
|
---|
[4271] | 51 | */
|
---|
[8127] | 52 | QwtScaleDiv::QwtScaleDiv( double lowerBound, double upperBound,
|
---|
[4271] | 53 | QList<double> ticks[NTickTypes] ):
|
---|
| 54 | d_lowerBound( lowerBound ),
|
---|
[8127] | 55 | d_upperBound( upperBound )
|
---|
[4271] | 56 | {
|
---|
| 57 | for ( int i = 0; i < NTickTypes; i++ )
|
---|
| 58 | d_ticks[i] = ticks[i];
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | /*!
|
---|
[8127] | 62 | Construct a scale division
|
---|
| 63 |
|
---|
| 64 | \param lowerBound First boundary
|
---|
| 65 | \param upperBound Second boundary
|
---|
| 66 | \param minorTicks List of minor ticks
|
---|
| 67 | \param mediumTicks List medium ticks
|
---|
| 68 | \param majorTicks List of major ticks
|
---|
| 69 |
|
---|
| 70 | \note lowerBound might be greater than upperBound for inverted scales
|
---|
| 71 | */
|
---|
| 72 | QwtScaleDiv::QwtScaleDiv( double lowerBound, double upperBound,
|
---|
| 73 | const QList<double> &minorTicks,
|
---|
| 74 | const QList<double> &mediumTicks,
|
---|
| 75 | const QList<double> &majorTicks ):
|
---|
| 76 | d_lowerBound( lowerBound ),
|
---|
| 77 | d_upperBound( upperBound )
|
---|
| 78 | {
|
---|
| 79 | d_ticks[ MinorTick ] = minorTicks;
|
---|
| 80 | d_ticks[ MediumTick ] = mediumTicks;
|
---|
| 81 | d_ticks[ MajorTick ] = majorTicks;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | /*!
|
---|
| 85 | Change the interval
|
---|
| 86 |
|
---|
| 87 | \param lowerBound First boundary
|
---|
| 88 | \param upperBound Second boundary
|
---|
| 89 |
|
---|
| 90 | \note lowerBound might be greater than upperBound for inverted scales
|
---|
| 91 | */
|
---|
| 92 | void QwtScaleDiv::setInterval( double lowerBound, double upperBound )
|
---|
| 93 | {
|
---|
| 94 | d_lowerBound = lowerBound;
|
---|
| 95 | d_upperBound = upperBound;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | /*!
|
---|
[4271] | 99 | Change the interval
|
---|
[8127] | 100 |
|
---|
[4271] | 101 | \param interval Interval
|
---|
| 102 | */
|
---|
| 103 | void QwtScaleDiv::setInterval( const QwtInterval &interval )
|
---|
| 104 | {
|
---|
[8127] | 105 | d_lowerBound = interval.minValue();
|
---|
| 106 | d_upperBound = interval.maxValue();
|
---|
[4271] | 107 | }
|
---|
| 108 |
|
---|
| 109 | /*!
|
---|
[8127] | 110 | \return lowerBound -> upperBound
|
---|
| 111 | */
|
---|
| 112 | QwtInterval QwtScaleDiv::interval() const
|
---|
| 113 | {
|
---|
| 114 | return QwtInterval( d_lowerBound, d_upperBound );
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | /*!
|
---|
| 118 | Set the first boundary
|
---|
| 119 |
|
---|
| 120 | \param lowerBound First boundary
|
---|
| 121 | \sa lowerBiound(), setUpperBound()
|
---|
| 122 | */
|
---|
| 123 | void QwtScaleDiv::setLowerBound( double lowerBound )
|
---|
| 124 | {
|
---|
| 125 | d_lowerBound = lowerBound;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | /*!
|
---|
| 129 | \return First boundary
|
---|
| 130 | \sa upperBound()
|
---|
| 131 | */
|
---|
| 132 | double QwtScaleDiv::lowerBound() const
|
---|
| 133 | {
|
---|
| 134 | return d_lowerBound;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | /*!
|
---|
| 138 | Set the second boundary
|
---|
| 139 |
|
---|
| 140 | \param upperBound Second boundary
|
---|
| 141 | \sa upperBound(), setLowerBound()
|
---|
| 142 | */
|
---|
| 143 | void QwtScaleDiv::setUpperBound( double upperBound )
|
---|
| 144 | {
|
---|
| 145 | d_upperBound = upperBound;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | /*!
|
---|
| 149 | \return upper bound
|
---|
| 150 | \sa lowerBound()
|
---|
| 151 | */
|
---|
| 152 | double QwtScaleDiv::upperBound() const
|
---|
| 153 | {
|
---|
| 154 | return d_upperBound;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | /*!
|
---|
| 158 | \return upperBound() - lowerBound()
|
---|
| 159 | */
|
---|
| 160 | double QwtScaleDiv::range() const
|
---|
| 161 | {
|
---|
| 162 | return d_upperBound - d_lowerBound;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | /*!
|
---|
[4271] | 166 | \brief Equality operator
|
---|
| 167 | \return true if this instance is equal to other
|
---|
| 168 | */
|
---|
| 169 | bool QwtScaleDiv::operator==( const QwtScaleDiv &other ) const
|
---|
| 170 | {
|
---|
| 171 | if ( d_lowerBound != other.d_lowerBound ||
|
---|
[8127] | 172 | d_upperBound != other.d_upperBound )
|
---|
[4271] | 173 | {
|
---|
| 174 | return false;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | for ( int i = 0; i < NTickTypes; i++ )
|
---|
| 178 | {
|
---|
| 179 | if ( d_ticks[i] != other.d_ticks[i] )
|
---|
| 180 | return false;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | return true;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | /*!
|
---|
| 187 | \brief Inequality
|
---|
[8127] | 188 | \return true if this instance is not equal to other
|
---|
[4271] | 189 | */
|
---|
[8127] | 190 | bool QwtScaleDiv::operator!=( const QwtScaleDiv &other ) const
|
---|
[4271] | 191 | {
|
---|
[8127] | 192 | return ( !( *this == other ) );
|
---|
[4271] | 193 | }
|
---|
| 194 |
|
---|
[8127] | 195 | //! Check if the scale division is empty( lowerBound() == upperBound() )
|
---|
| 196 | bool QwtScaleDiv::isEmpty() const
|
---|
[4271] | 197 | {
|
---|
[8127] | 198 | return ( d_lowerBound == d_upperBound );
|
---|
[4271] | 199 | }
|
---|
| 200 |
|
---|
[8127] | 201 | //! Check if the scale division is increasing( lowerBound() <= upperBound() )
|
---|
| 202 | bool QwtScaleDiv::isIncreasing() const
|
---|
[4271] | 203 | {
|
---|
[8127] | 204 | return d_lowerBound <= d_upperBound;
|
---|
[4271] | 205 | }
|
---|
| 206 |
|
---|
| 207 | /*!
|
---|
| 208 | Return if a value is between lowerBound() and upperBound()
|
---|
| 209 |
|
---|
| 210 | \param value Value
|
---|
| 211 | \return true/false
|
---|
| 212 | */
|
---|
| 213 | bool QwtScaleDiv::contains( double value ) const
|
---|
| 214 | {
|
---|
| 215 | const double min = qMin( d_lowerBound, d_upperBound );
|
---|
| 216 | const double max = qMax( d_lowerBound, d_upperBound );
|
---|
| 217 |
|
---|
| 218 | return value >= min && value <= max;
|
---|
| 219 | }
|
---|
| 220 |
|
---|
[8127] | 221 | /*!
|
---|
| 222 | Invert the scale division
|
---|
| 223 | \sa inverted()
|
---|
| 224 | */
|
---|
[4271] | 225 | void QwtScaleDiv::invert()
|
---|
| 226 | {
|
---|
| 227 | qSwap( d_lowerBound, d_upperBound );
|
---|
| 228 |
|
---|
| 229 | for ( int i = 0; i < NTickTypes; i++ )
|
---|
| 230 | {
|
---|
| 231 | QList<double>& ticks = d_ticks[i];
|
---|
| 232 |
|
---|
| 233 | const int size = ticks.count();
|
---|
| 234 | const int size2 = size / 2;
|
---|
| 235 |
|
---|
[8127] | 236 | for ( int j = 0; j < size2; j++ )
|
---|
| 237 | qSwap( ticks[j], ticks[size - 1 - j] );
|
---|
[4271] | 238 | }
|
---|
| 239 | }
|
---|
| 240 |
|
---|
[8127] | 241 | /*!
|
---|
| 242 | \return A scale division with inverted boundaries and ticks
|
---|
| 243 | \sa invert()
|
---|
| 244 | */
|
---|
| 245 | QwtScaleDiv QwtScaleDiv::inverted() const
|
---|
| 246 | {
|
---|
| 247 | QwtScaleDiv other = *this;
|
---|
| 248 | other.invert();
|
---|
| 249 |
|
---|
| 250 | return other;
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | /*!
|
---|
| 254 | Return a scale division with an interval [lowerBound, upperBound]
|
---|
| 255 | where all ticks outside this interval are removed
|
---|
| 256 |
|
---|
| 257 | \param lowerBound Lower bound
|
---|
| 258 | \param upperBound Upper bound
|
---|
| 259 |
|
---|
| 260 | \return Scale division with all ticks inside of the given interval
|
---|
| 261 |
|
---|
| 262 | \note lowerBound might be greater than upperBound for inverted scales
|
---|
| 263 | */
|
---|
| 264 | QwtScaleDiv QwtScaleDiv::bounded(
|
---|
| 265 | double lowerBound, double upperBound ) const
|
---|
| 266 | {
|
---|
| 267 | const double min = qMin( lowerBound, upperBound );
|
---|
| 268 | const double max = qMax( lowerBound, upperBound );
|
---|
| 269 |
|
---|
| 270 | QwtScaleDiv sd;
|
---|
| 271 | sd.setInterval( lowerBound, upperBound );
|
---|
| 272 |
|
---|
| 273 | for ( int tickType = 0; tickType < QwtScaleDiv::NTickTypes; tickType++ )
|
---|
| 274 | {
|
---|
| 275 | const QList<double> &ticks = d_ticks[ tickType ];
|
---|
| 276 |
|
---|
| 277 | QList<double> boundedTicks;
|
---|
| 278 | for ( int i = 0; i < ticks.size(); i++ )
|
---|
| 279 | {
|
---|
| 280 | const double tick = ticks[i];
|
---|
| 281 | if ( tick >= min && tick <= max )
|
---|
| 282 | boundedTicks += tick;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | sd.setTicks( tickType, boundedTicks );
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 | return sd;
|
---|
| 289 |
|
---|
| 290 | }
|
---|
| 291 |
|
---|
[4271] | 292 | /*!
|
---|
| 293 | Assign ticks
|
---|
| 294 |
|
---|
| 295 | \param type MinorTick, MediumTick or MajorTick
|
---|
| 296 | \param ticks Values of the tick positions
|
---|
| 297 | */
|
---|
| 298 | void QwtScaleDiv::setTicks( int type, const QList<double> &ticks )
|
---|
| 299 | {
|
---|
[8127] | 300 | if ( type >= 0 && type < NTickTypes )
|
---|
[4271] | 301 | d_ticks[type] = ticks;
|
---|
| 302 | }
|
---|
| 303 |
|
---|
| 304 | /*!
|
---|
| 305 | Return a list of ticks
|
---|
| 306 |
|
---|
| 307 | \param type MinorTick, MediumTick or MajorTick
|
---|
[8127] | 308 | \return Tick list
|
---|
[4271] | 309 | */
|
---|
[8127] | 310 | QList<double> QwtScaleDiv::ticks( int type ) const
|
---|
[4271] | 311 | {
|
---|
[8127] | 312 | if ( type >= 0 && type < NTickTypes )
|
---|
[4271] | 313 | return d_ticks[type];
|
---|
| 314 |
|
---|
[8127] | 315 | return QList<double>();
|
---|
[4271] | 316 | }
|
---|
[8127] | 317 |
|
---|
| 318 | #ifndef QT_NO_DEBUG_STREAM
|
---|
| 319 |
|
---|
| 320 | QDebug operator<<( QDebug debug, const QwtScaleDiv &scaleDiv )
|
---|
| 321 | {
|
---|
| 322 | debug << scaleDiv.lowerBound() << "<->" << scaleDiv.upperBound();
|
---|
| 323 | debug << "Major: " << scaleDiv.ticks( QwtScaleDiv::MajorTick );
|
---|
| 324 | debug << "Medium: " << scaleDiv.ticks( QwtScaleDiv::MediumTick );
|
---|
| 325 | debug << "Minor: " << scaleDiv.ticks( QwtScaleDiv::MinorTick );
|
---|
| 326 |
|
---|
| 327 | return debug;
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | #endif
|
---|
| 331 |
|
---|