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