qwt_scale_div.cpp 7.4 KB
Newer Older
pixhawk's avatar
pixhawk committed
1
2
3
4
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
5
 *
pixhawk's avatar
pixhawk committed
6
7
8
9
10
11
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#include "qwt_scale_div.h"
#include "qwt_math.h"
Bryant's avatar
Bryant committed
12
#include <qalgorithms.h>
pixhawk's avatar
pixhawk committed
13

Bryant's avatar
Bryant committed
14
15
16
17
18
19
20
21
22
23
24
/*!
  Construct a division without ticks

  \param lowerBound First boundary
  \param upperBound Second boundary

  \note lowerBound might be greater than upperBound for inverted scales
 */
QwtScaleDiv::QwtScaleDiv( double lowerBound, double upperBound ):
    d_lowerBound( lowerBound ),
    d_upperBound( upperBound )
pixhawk's avatar
pixhawk committed
25
26
27
{
}

28
/*!
Bryant's avatar
Bryant committed
29
  Construct a scale division
pixhawk's avatar
pixhawk committed
30
31
32
33

  \param interval Interval
  \param ticks List of major, medium and minor ticks
*/
Bryant's avatar
Bryant committed
34
35
36
37
QwtScaleDiv::QwtScaleDiv( const QwtInterval &interval,
        QList<double> ticks[NTickTypes] ):
    d_lowerBound( interval.minValue() ),
    d_upperBound( interval.maxValue() )
pixhawk's avatar
pixhawk committed
38
39
40
41
42
{
    for ( int i = 0; i < NTickTypes; i++ )
        d_ticks[i] = ticks[i];
}

43
/*!
Bryant's avatar
Bryant committed
44
  Construct a scale division
pixhawk's avatar
pixhawk committed
45

Bryant's avatar
Bryant committed
46
47
  \param lowerBound First boundary
  \param upperBound Second boundary
pixhawk's avatar
pixhawk committed
48
  \param ticks List of major, medium and minor ticks
Bryant's avatar
Bryant committed
49
50

  \note lowerBound might be greater than upperBound for inverted scales
pixhawk's avatar
pixhawk committed
51
*/
Bryant's avatar
Bryant committed
52
53
54
55
QwtScaleDiv::QwtScaleDiv( double lowerBound, double upperBound,
        QList<double> ticks[NTickTypes] ):
    d_lowerBound( lowerBound ),
    d_upperBound( upperBound )
pixhawk's avatar
pixhawk committed
56
57
58
59
60
{
    for ( int i = 0; i < NTickTypes; i++ )
        d_ticks[i] = ticks[i];
}

Bryant's avatar
Bryant committed
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
/*!
  Construct a scale division

  \param lowerBound First boundary
  \param upperBound Second boundary
  \param minorTicks List of minor ticks
  \param mediumTicks List medium ticks
  \param majorTicks List of major ticks

  \note lowerBound might be greater than upperBound for inverted scales
*/
QwtScaleDiv::QwtScaleDiv( double lowerBound, double upperBound,
        const QList<double> &minorTicks, 
        const QList<double> &mediumTicks,
        const QList<double> &majorTicks ):
    d_lowerBound( lowerBound ),
    d_upperBound( upperBound )
{
    d_ticks[ MinorTick ] = minorTicks;
    d_ticks[ MediumTick ] = mediumTicks;
    d_ticks[ MajorTick ] = majorTicks;
}

/*!
  Change the interval

  \param lowerBound First boundary
  \param upperBound Second boundary

  \note lowerBound might be greater than upperBound for inverted scales
*/
void QwtScaleDiv::setInterval( double lowerBound, double upperBound )
{
    d_lowerBound = lowerBound;
    d_upperBound = upperBound;
}

pixhawk's avatar
pixhawk committed
98
99
/*!
   Change the interval
Bryant's avatar
Bryant committed
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

   \param interval Interval
*/
void QwtScaleDiv::setInterval( const QwtInterval &interval )
{
    d_lowerBound = interval.minValue();
    d_upperBound = interval.maxValue();
}

/*!
  \return lowerBound -> upperBound
*/
QwtInterval QwtScaleDiv::interval() const
{
    return QwtInterval( d_lowerBound, d_upperBound );
}

/*!
  Set the first boundary

  \param lowerBound First boundary
  \sa lowerBiound(), setUpperBound()
 */
void QwtScaleDiv::setLowerBound( double lowerBound  )
{
    d_lowerBound = lowerBound;
}

/*!
  \return First boundary
  \sa upperBound()
*/
double QwtScaleDiv::lowerBound() const
{
    return d_lowerBound;
}   

/*!
  Set the second boundary

  \param upperBound Second boundary
  \sa upperBound(), setLowerBound()
 */
void QwtScaleDiv::setUpperBound( double upperBound  )
{
    d_upperBound = upperBound;
} 

/*!
  \return upper bound
  \sa lowerBound()
*/
double QwtScaleDiv::upperBound() const
{
    return d_upperBound;
}

/*!
  \return upperBound() - lowerBound()
pixhawk's avatar
pixhawk committed
159
*/
Bryant's avatar
Bryant committed
160
double QwtScaleDiv::range() const
pixhawk's avatar
pixhawk committed
161
{
Bryant's avatar
Bryant committed
162
    return d_upperBound - d_lowerBound;
pixhawk's avatar
pixhawk committed
163
164
165
166
167
168
}

/*!
  \brief Equality operator
  \return true if this instance is equal to other
*/
Bryant's avatar
Bryant committed
169
bool QwtScaleDiv::operator==( const QwtScaleDiv &other ) const
pixhawk's avatar
pixhawk committed
170
{
Bryant's avatar
Bryant committed
171
172
173
    if ( d_lowerBound != other.d_lowerBound ||
        d_upperBound != other.d_upperBound )
    {
pixhawk's avatar
pixhawk committed
174
175
176
        return false;
    }

Bryant's avatar
Bryant committed
177
178
    for ( int i = 0; i < NTickTypes; i++ )
    {
pixhawk's avatar
pixhawk committed
179
180
181
182
183
184
185
186
187
        if ( d_ticks[i] != other.d_ticks[i] )
            return false;
    }

    return true;
}

/*!
  \brief Inequality
Bryant's avatar
Bryant committed
188
  \return true if this instance is not equal to other
pixhawk's avatar
pixhawk committed
189
*/
Bryant's avatar
Bryant committed
190
bool QwtScaleDiv::operator!=( const QwtScaleDiv &other ) const
pixhawk's avatar
pixhawk committed
191
{
Bryant's avatar
Bryant committed
192
    return ( !( *this == other ) );
pixhawk's avatar
pixhawk committed
193
194
}

Bryant's avatar
Bryant committed
195
196
//! Check if the scale division is empty( lowerBound() == upperBound() )
bool QwtScaleDiv::isEmpty() const
pixhawk's avatar
pixhawk committed
197
{
Bryant's avatar
Bryant committed
198
    return ( d_lowerBound == d_upperBound );
pixhawk's avatar
pixhawk committed
199
200
}

Bryant's avatar
Bryant committed
201
202
//! Check if the scale division is increasing( lowerBound() <= upperBound() )
bool QwtScaleDiv::isIncreasing() const
pixhawk's avatar
pixhawk committed
203
{
Bryant's avatar
Bryant committed
204
    return d_lowerBound <= d_upperBound;
pixhawk's avatar
pixhawk committed
205
206
}

Bryant's avatar
Bryant committed
207
208
/*!
  Return if a value is between lowerBound() and upperBound()
pixhawk's avatar
pixhawk committed
209

Bryant's avatar
Bryant committed
210
211
212
213
214
215
216
  \param value Value
  \return true/false
*/
bool QwtScaleDiv::contains( double value ) const
{
    const double min = qMin( d_lowerBound, d_upperBound );
    const double max = qMax( d_lowerBound, d_upperBound );
pixhawk's avatar
pixhawk committed
217

Bryant's avatar
Bryant committed
218
    return value >= min && value <= max;
pixhawk's avatar
pixhawk committed
219
220
}

Bryant's avatar
Bryant committed
221
222
223
224
/*! 
   Invert the scale division
   \sa inverted()
 */
pixhawk's avatar
pixhawk committed
225
226
void QwtScaleDiv::invert()
{
Bryant's avatar
Bryant committed
227
    qSwap( d_lowerBound, d_upperBound );
pixhawk's avatar
pixhawk committed
228

Bryant's avatar
Bryant committed
229
230
231
    for ( int i = 0; i < NTickTypes; i++ )
    {
        QList<double>& ticks = d_ticks[i];
pixhawk's avatar
pixhawk committed
232
233
234

        const int size = ticks.count();
        const int size2 = size / 2;
235

Bryant's avatar
Bryant committed
236
237
        for ( int j = 0; j < size2; j++ )
            qSwap( ticks[j], ticks[size - 1 - j] );
pixhawk's avatar
pixhawk committed
238
239
240
    }
}

Bryant's avatar
Bryant committed
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
/*! 
  \return A scale division with inverted boundaries and ticks
  \sa invert()
 */
QwtScaleDiv QwtScaleDiv::inverted() const
{
    QwtScaleDiv other = *this;
    other.invert();

    return other;
}

/*! 
   Return a scale division with an interval [lowerBound, upperBound]
   where all ticks outside this interval are removed

   \param lowerBound Lower bound
   \param upperBound Upper bound

   \return Scale division with all ticks inside of the given interval

   \note lowerBound might be greater than upperBound for inverted scales
*/
QwtScaleDiv QwtScaleDiv::bounded( 
    double lowerBound, double upperBound ) const
{
    const double min = qMin( lowerBound, upperBound );
    const double max = qMax( lowerBound, upperBound );

    QwtScaleDiv sd;
    sd.setInterval( lowerBound, upperBound );

    for ( int tickType = 0; tickType < QwtScaleDiv::NTickTypes; tickType++ )
    {
        const QList<double> &ticks = d_ticks[ tickType ];

        QList<double> boundedTicks;
        for ( int i = 0; i < ticks.size(); i++ )
        {
            const double tick = ticks[i];
            if ( tick >= min && tick <= max )
                boundedTicks += tick;
        }

        sd.setTicks( tickType, boundedTicks );
    }

    return sd;

}

pixhawk's avatar
pixhawk committed
292
293
294
295
296
297
/*!
    Assign ticks

   \param type MinorTick, MediumTick or MajorTick
   \param ticks Values of the tick positions
*/
Bryant's avatar
Bryant committed
298
void QwtScaleDiv::setTicks( int type, const QList<double> &ticks )
pixhawk's avatar
pixhawk committed
299
{
Bryant's avatar
Bryant committed
300
    if ( type >= 0 && type < NTickTypes )
pixhawk's avatar
pixhawk committed
301
302
303
304
305
306
307
        d_ticks[type] = ticks;
}

/*!
   Return a list of ticks

   \param type MinorTick, MediumTick or MajorTick
Bryant's avatar
Bryant committed
308
   \return Tick list
pixhawk's avatar
pixhawk committed
309
*/
Bryant's avatar
Bryant committed
310
QList<double> QwtScaleDiv::ticks( int type ) const
pixhawk's avatar
pixhawk committed
311
{
Bryant's avatar
Bryant committed
312
    if ( type >= 0 && type < NTickTypes )
pixhawk's avatar
pixhawk committed
313
314
        return d_ticks[type];

Bryant's avatar
Bryant committed
315
316
317
318
319
320
321
322
323
324
325
326
327
    return QList<double>();
}

#ifndef QT_NO_DEBUG_STREAM

QDebug operator<<( QDebug debug, const QwtScaleDiv &scaleDiv )
{
    debug << scaleDiv.lowerBound() << "<->" << scaleDiv.upperBound();
    debug << "Major: " << scaleDiv.ticks( QwtScaleDiv::MajorTick );
    debug << "Medium: " << scaleDiv.ticks( QwtScaleDiv::MediumTick );
    debug << "Minor: " << scaleDiv.ticks( QwtScaleDiv::MinorTick );

    return debug;
pixhawk's avatar
pixhawk committed
328
}
Bryant's avatar
Bryant committed
329
330
331

#endif