Qwt User's Guide 6.2.0

qwt_interval.cpp
1 /******************************************************************************
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_interval.h"
11
12 namespace
13 {
14 static const struct RegisterQwtInterval
15 {
16 inline RegisterQwtInterval() { qRegisterMetaType< QwtInterval >(); }
17
18 } qwtRegisterQwtInterval;
19 }
20
29 QwtInterval QwtInterval::normalized() const
30 {
31 if ( m_minValue > m_maxValue )
32 {
33 return inverted();
34 }
35 if ( m_minValue == m_maxValue && m_borderFlags == ExcludeMinimum )
36 {
37 return inverted();
38 }
39
40 return *this;
41 }
42
48 QwtInterval QwtInterval::inverted() const
49 {
50 BorderFlags borderFlags = IncludeBorders;
51
52 if ( m_borderFlags & ExcludeMinimum )
53 borderFlags |= ExcludeMaximum;
54
55 if ( m_borderFlags & ExcludeMaximum )
56 borderFlags |= ExcludeMinimum;
57
58 return QwtInterval( m_maxValue, m_minValue, borderFlags );
59 }
60
67 bool QwtInterval::contains( double value ) const
68 {
69 if ( !isValid() )
70 return false;
71
72 if ( ( value < m_minValue ) || ( value > m_maxValue ) )
73 return false;
74
75 if ( ( value == m_minValue ) && ( m_borderFlags & ExcludeMinimum ) )
76 return false;
77
78 if ( ( value == m_maxValue ) && ( m_borderFlags & ExcludeMaximum ) )
79 return false;
80
81 return true;
82 }
83
90 bool QwtInterval::contains( const QwtInterval& interval ) const
91 {
92 if ( !isValid() || !interval.isValid() )
93 return false;
94
95 if ( ( interval.m_minValue < m_minValue ) || ( interval.m_maxValue > m_maxValue ) )
96 return false;
97
98 if ( m_borderFlags )
99 {
100 if ( interval.m_minValue == m_minValue )
101 {
102 if ( ( m_borderFlags & ExcludeMinimum )
103 && !( interval.m_borderFlags & ExcludeMinimum ) )
104 {
105 return false;
106 }
107 }
108
109 if ( interval.m_maxValue == m_maxValue )
110 {
111 if ( ( m_borderFlags & ExcludeMaximum )
112 && !( interval.m_borderFlags & ExcludeMaximum ) )
113 {
114 return false;
115 }
116 }
117 }
118
119 return true;
120 }
121
123 QwtInterval QwtInterval::unite( const QwtInterval& other ) const
124 {
125 /*
126 If one of the intervals is invalid return the other one.
127 If both are invalid return an invalid default interval
128 */
129 if ( !isValid() )
130 {
131 if ( !other.isValid() )
132 return QwtInterval();
133 else
134 return other;
135 }
136 if ( !other.isValid() )
137 return *this;
138
139 QwtInterval united;
140 BorderFlags flags = IncludeBorders;
141
142 // minimum
143 if ( m_minValue < other.minValue() )
144 {
145 united.setMinValue( m_minValue );
146 flags &= m_borderFlags & ExcludeMinimum;
147 }
148 else if ( other.minValue() < m_minValue )
149 {
150 united.setMinValue( other.minValue() );
151 flags &= other.borderFlags() & ExcludeMinimum;
152 }
153 else // m_minValue == other.minValue()
154 {
155 united.setMinValue( m_minValue );
156 flags &= ( m_borderFlags & other.borderFlags() ) & ExcludeMinimum;
157 }
158
159 // maximum
160 if ( m_maxValue > other.maxValue() )
161 {
162 united.setMaxValue( m_maxValue );
163 flags &= m_borderFlags & ExcludeMaximum;
164 }
165 else if ( other.maxValue() > m_maxValue )
166 {
167 united.setMaxValue( other.maxValue() );
168 flags &= other.borderFlags() & ExcludeMaximum;
169 }
170 else // m_maxValue == other.maxValue() )
171 {
172 united.setMaxValue( m_maxValue );
173 flags &= m_borderFlags & other.borderFlags() & ExcludeMaximum;
174 }
175
176 united.setBorderFlags( flags );
177 return united;
178 }
179
186 QwtInterval QwtInterval::intersect( const QwtInterval& other ) const
187 {
188 if ( !other.isValid() || !isValid() )
189 return QwtInterval();
190
191 QwtInterval i1 = *this;
192 QwtInterval i2 = other;
193
194 // swap i1/i2, so that the minimum of i1
195 // is smaller then the minimum of i2
196
197 if ( i1.minValue() > i2.minValue() )
198 {
199 qSwap( i1, i2 );
200 }
201 else if ( i1.minValue() == i2.minValue() )
202 {
203 if ( i1.borderFlags() & ExcludeMinimum )
204 qSwap( i1, i2 );
205 }
206
207 if ( i1.maxValue() < i2.minValue() )
208 {
209 return QwtInterval();
210 }
211
212 if ( i1.maxValue() == i2.minValue() )
213 {
214 if ( i1.borderFlags() & ExcludeMaximum ||
215 i2.borderFlags() & ExcludeMinimum )
216 {
217 return QwtInterval();
218 }
219 }
220
221 QwtInterval intersected;
222 BorderFlags flags = IncludeBorders;
223
224 intersected.setMinValue( i2.minValue() );
225 flags |= i2.borderFlags() & ExcludeMinimum;
226
227 if ( i1.maxValue() < i2.maxValue() )
228 {
229 intersected.setMaxValue( i1.maxValue() );
230 flags |= i1.borderFlags() & ExcludeMaximum;
231 }
232 else if ( i2.maxValue() < i1.maxValue() )
233 {
234 intersected.setMaxValue( i2.maxValue() );
235 flags |= i2.borderFlags() & ExcludeMaximum;
236 }
237 else // i1.maxValue() == i2.maxValue()
238 {
239 intersected.setMaxValue( i1.maxValue() );
240 flags |= i1.borderFlags() & i2.borderFlags() & ExcludeMaximum;
241 }
242
243 intersected.setBorderFlags( flags );
244 return intersected;
245 }
246
253 QwtInterval& QwtInterval::operator|=( const QwtInterval& other )
254 {
255 *this = *this | other;
256 return *this;
257 }
258
265 QwtInterval& QwtInterval::operator&=( const QwtInterval& other )
266 {
267 *this = *this & other;
268 return *this;
269 }
270
277 bool QwtInterval::intersects( const QwtInterval& other ) const
278 {
279 if ( !isValid() || !other.isValid() )
280 return false;
281
282 QwtInterval i1 = *this;
283 QwtInterval i2 = other;
284
285 // swap i1/i2, so that the minimum of i1
286 // is smaller then the minimum of i2
287
288 if ( i1.minValue() > i2.minValue() )
289 {
290 qSwap( i1, i2 );
291 }
292 else if ( i1.minValue() == i2.minValue() &&
293 i1.borderFlags() & ExcludeMinimum )
294 {
295 qSwap( i1, i2 );
296 }
297
298 if ( i1.maxValue() > i2.minValue() )
299 {
300 return true;
301 }
302 if ( i1.maxValue() == i2.minValue() )
303 {
304 return !( ( i1.borderFlags() & ExcludeMaximum ) ||
305 ( i2.borderFlags() & ExcludeMinimum ) );
306 }
307 return false;
308 }
309
317 QwtInterval QwtInterval::symmetrize( double value ) const
318 {
319 if ( !isValid() )
320 return *this;
321
322 const double delta =
323 qMax( qAbs( value - m_maxValue ), qAbs( value - m_minValue ) );
324
325 return QwtInterval( value - delta, value + delta );
326 }
327
336 QwtInterval QwtInterval::limited( double lowerBound, double upperBound ) const
337 {
338 if ( !isValid() || lowerBound > upperBound )
339 return QwtInterval();
340
341 double minValue = qMax( m_minValue, lowerBound );
342 minValue = qMin( minValue, upperBound );
343
344 double maxValue = qMax( m_maxValue, lowerBound );
345 maxValue = qMin( maxValue, upperBound );
346
347 return QwtInterval( minValue, maxValue, m_borderFlags );
348 }
349
363 QwtInterval QwtInterval::extend( double value ) const
364 {
365 if ( !isValid() )
366 return *this;
367
368 return QwtInterval( qMin( value, m_minValue ),
369 qMax( value, m_maxValue ), m_borderFlags );
370 }
371
380 QwtInterval& QwtInterval::operator|=( double value )
381 {
382 *this = *this | value;
383 return *this;
384 }
385
386 #ifndef QT_NO_DEBUG_STREAM
387
388 #include <qdebug.h>
389
390 QDebug operator<<( QDebug debug, const QwtInterval& interval )
391 {
392 const int flags = interval.borderFlags();
393
394 debug.nospace() << "QwtInterval("
395 << ( ( flags& QwtInterval::ExcludeMinimum ) ? "]" : "[" )
396 << interval.minValue() << "," << interval.maxValue()
397 << ( ( flags& QwtInterval::ExcludeMaximum ) ? "[" : "]" )
398 << ")";
399
400 return debug.space();
401 }
402
403 #endif
QwtInterval
A class representing an interval.
Definition: qwt_interval.h:23
QwtInterval::minValue
double minValue() const
Definition: qwt_interval.h:192
QwtInterval::normalized
QwtInterval normalized() const
Normalize the limits of the interval.
Definition: qwt_interval.cpp:29
QwtInterval::operator&=
QwtInterval & operator&=(const QwtInterval &)
Intersect this interval with the given interval.
Definition: qwt_interval.cpp:265
QwtInterval::ExcludeMaximum
@ ExcludeMaximum
Max value is not included in the interval.
Definition: qwt_interval.h:38
QwtInterval::ExcludeMinimum
@ ExcludeMinimum
Min value is not included in the interval.
Definition: qwt_interval.h:35
QwtInterval::IncludeBorders
@ IncludeBorders
Min/Max values are inside the interval.
Definition: qwt_interval.h:32
QwtInterval::setMaxValue
void setMaxValue(double)
Definition: qwt_interval.h:186
QwtInterval::maxValue
double maxValue() const
Definition: qwt_interval.h:198
QwtInterval::unite
QwtInterval unite(const QwtInterval &) const
Unite 2 intervals.
Definition: qwt_interval.cpp:123
QwtInterval::intersects
bool intersects(const QwtInterval &) const
Test if two intervals overlap.
Definition: qwt_interval.cpp:277
QwtInterval::limited
QwtInterval limited(double lowerBound, double upperBound) const
Definition: qwt_interval.cpp:336
QwtInterval::operator|=
QwtInterval & operator|=(const QwtInterval &)
Unite this interval with the given interval.
Definition: qwt_interval.cpp:253
QwtInterval::extend
QwtInterval extend(double value) const
Extend the interval.
Definition: qwt_interval.cpp:363
QwtInterval::setMinValue
void setMinValue(double)
Definition: qwt_interval.h:176
QwtInterval::inverted
QwtInterval inverted() const
Definition: qwt_interval.cpp:48
QwtInterval::intersect
QwtInterval intersect(const QwtInterval &) const
Intersect 2 intervals.
Definition: qwt_interval.cpp:186
QwtInterval::borderFlags
BorderFlags borderFlags() const
Definition: qwt_interval.h:166
QwtInterval::QwtInterval
QwtInterval()
Default Constructor.
Definition: qwt_interval.h:112
QwtInterval::setBorderFlags
void setBorderFlags(BorderFlags)
Definition: qwt_interval.h:157
QwtInterval::isValid
bool isValid() const
Definition: qwt_interval.h:210
QwtInterval::contains
bool contains(double value) const
Definition: qwt_interval.cpp:67
QwtInterval::BorderFlags
QFlags< BorderFlag > BorderFlags
Border flags.
Definition: qwt_interval.h:45
QwtInterval::symmetrize
QwtInterval symmetrize(double value) const
Definition: qwt_interval.cpp:317