QSkinny 0.8.0
C++/Qt UI toolkit
Loading...
Searching...
No Matches
QskIntervalF.h
1/******************************************************************************
2 * QSkinny - Copyright (C) The authors
3 * SPDX-License-Identifier: BSD-3-Clause
4 *****************************************************************************/
5
6#ifndef QSK_INTERVAL_H
7#define QSK_INTERVAL_H
8
9#include "QskGlobal.h"
10#include <qglobal.h>
11#include <qmetatype.h>
12
13class QVariant;
14class QRectF;
15
16class QSK_EXPORT QskIntervalF
17{
18 Q_GADGET
19
20 Q_PROPERTY( qreal lowerBound READ lowerBound WRITE setLowerBound )
21 Q_PROPERTY( qreal upperBound READ upperBound WRITE setUpperBound )
22 Q_PROPERTY( qreal length READ length )
23
24 public:
25 constexpr QskIntervalF() noexcept = default;
26 constexpr QskIntervalF( qreal lowerBound, qreal upperBound ) noexcept;
27
28 void setInterval( qreal lowerBound, qreal upperBound ) noexcept;
29
30 constexpr bool operator==( const QskIntervalF& ) const noexcept;
31 constexpr bool operator!=( const QskIntervalF& ) const noexcept;
32
33 constexpr qreal lowerBound() const noexcept;
34 void setLowerBound( qreal ) noexcept;
35
36 constexpr qreal upperBound() const noexcept;
37 void setUpperBound( qreal ) noexcept;
38
39 void spanFromLowerBound( qreal ) noexcept;
40 void spanFromUpperBound( qreal ) noexcept;
41
42 constexpr qreal length() const noexcept;
43 void setLength( qreal ) noexcept;
44
45 constexpr bool contains( qreal value ) const noexcept;
46 constexpr bool contains( const QskIntervalF& ) const noexcept;
47
48 constexpr bool isBoundary( qreal value ) const noexcept;
49
50 bool fuzzyContains( qreal value ) const;
51 bool fuzzyContains( const QskIntervalF& ) const;
52
53 bool fuzzyIsBoundary( qreal value ) const;
54
55 static constexpr QskIntervalF normalized( qreal value1, qreal value2 ) noexcept;
56
57 void translate( qreal offset ) noexcept;
58 constexpr QskIntervalF translated( qreal offset ) const noexcept;
59
60 bool intersects( const QskIntervalF& ) const noexcept;
61 QskIntervalF intersected( const QskIntervalF& ) const noexcept;
62
63 void unite( const QskIntervalF& ) noexcept;
64 QskIntervalF united( const QskIntervalF& ) const noexcept;
65
66 void extend( qreal value ) noexcept;
67 QskIntervalF extended( qreal value ) const noexcept;
68
69 QskIntervalF operator|( const QskIntervalF& ) const noexcept;
70 QskIntervalF operator&( const QskIntervalF& ) const noexcept;
71
72 QskIntervalF& operator|=( const QskIntervalF& ) noexcept;
73 QskIntervalF& operator&=( const QskIntervalF& ) noexcept;
74
75 QskIntervalF operator|( qreal ) const noexcept;
76 QskIntervalF& operator|=( qreal ) noexcept;
77
78 QskIntervalF fuzzyAligned( qreal stepSize ) const;
79
80 constexpr bool isValid() const noexcept;
81 constexpr bool isNull() const noexcept;
82 constexpr bool isEmpty() const noexcept;
83
84 void invalidate() noexcept;
85
86 QskIntervalF interpolated( const QskIntervalF&, qreal progress ) const noexcept;
87
88 static QRectF toRect( const QskIntervalF&, const QskIntervalF& ) noexcept;
89
90 static QVariant interpolate( const QskIntervalF&,
91 const QskIntervalF&, qreal progress ) noexcept;
92
93 private:
94 qreal m_lowerBound = 0.0;
95 qreal m_upperBound = -1.0;
96};
97
98Q_DECLARE_TYPEINFO( QskIntervalF, Q_MOVABLE_TYPE );
99Q_DECLARE_METATYPE( QskIntervalF )
100
101QT_WARNING_PUSH
102QT_WARNING_DISABLE_CLANG("-Wfloat-equal")
103QT_WARNING_DISABLE_GCC("-Wfloat-equal")
104QT_WARNING_DISABLE_INTEL(1572)
105
106inline constexpr QskIntervalF::QskIntervalF( qreal lowerBound, qreal upperBound ) noexcept
107 : m_lowerBound( lowerBound )
108 , m_upperBound( upperBound )
109{
110}
111
112inline void QskIntervalF::setInterval( qreal lowerBound, qreal upperBound ) noexcept
113{
114 m_lowerBound = lowerBound;
115 m_upperBound = upperBound;
116}
117
118inline void QskIntervalF::setLowerBound( qreal value ) noexcept
119{
120 m_lowerBound = value;
121}
122
123inline void QskIntervalF::setUpperBound( qreal value ) noexcept
124{
125 m_upperBound = value;
126}
127
128inline constexpr qreal QskIntervalF::lowerBound() const noexcept
129{
130 return m_lowerBound;
131}
132
133inline constexpr qreal QskIntervalF::upperBound() const noexcept
134{
135 return m_upperBound;
136}
137
138inline constexpr qreal QskIntervalF::length() const noexcept
139{
140 return ( m_upperBound > m_lowerBound ) ? ( m_upperBound - m_lowerBound ) : 0.0;
141}
142
143inline void QskIntervalF::setLength( qreal length ) noexcept
144{
145 m_upperBound = m_lowerBound + length;
146}
147
148inline constexpr bool QskIntervalF::isValid() const noexcept
149{
150 return m_lowerBound <= m_upperBound;
151}
152
153inline constexpr bool QskIntervalF::isNull() const noexcept
154{
155 return m_lowerBound == m_upperBound;
156}
157
158inline constexpr bool QskIntervalF::isEmpty() const noexcept
159{
160 return m_upperBound <= m_lowerBound;
161}
162
163inline void QskIntervalF::invalidate() noexcept
164{
165 m_lowerBound = 0.0;
166 m_upperBound = -1.0;
167}
168
169inline constexpr bool QskIntervalF::contains( qreal value ) const noexcept
170{
171 return ( value >= m_lowerBound ) && ( value <= m_upperBound );
172}
173
174inline constexpr bool QskIntervalF::contains( const QskIntervalF& interval ) const noexcept
175{
176 return interval.isValid() && contains( interval.m_lowerBound )
177 && contains( interval.m_upperBound );
178}
179
180inline constexpr bool QskIntervalF::isBoundary( qreal value ) const noexcept
181{
182 return ( value == m_lowerBound ) || ( value == m_upperBound );
183}
184
185inline void QskIntervalF::translate( qreal offset ) noexcept
186{
187 m_lowerBound += offset;
188 m_upperBound += offset;
189}
190
191inline constexpr QskIntervalF QskIntervalF::translated( qreal offset ) const noexcept
192{
193 return QskIntervalF( m_lowerBound + offset, m_upperBound + offset );
194}
195
196inline constexpr QskIntervalF QskIntervalF::normalized( qreal value1, qreal value2 ) noexcept
197{
198 return ( value1 < value2 ) ? QskIntervalF( value1, value2 ) : QskIntervalF( value2, value1 );
199}
200
201inline QskIntervalF QskIntervalF::operator&( const QskIntervalF& other ) const noexcept
202{
203 return intersected( other );
204}
205
206inline QskIntervalF QskIntervalF::operator|( const QskIntervalF& other ) const noexcept
207{
208 return united( other );
209}
210
211inline QskIntervalF& QskIntervalF::operator|=( const QskIntervalF& other ) noexcept
212{
213 unite( other );
214 return *this;
215}
216
217inline QskIntervalF& QskIntervalF::operator&=( const QskIntervalF& other ) noexcept
218{
219 *this = intersected( other );
220 return *this;
221}
222
223inline constexpr bool QskIntervalF::operator==( const QskIntervalF& other ) const noexcept
224{
225 return qFuzzyCompare( m_lowerBound, other.m_lowerBound ) &&
226 qFuzzyCompare( m_upperBound, other.m_upperBound );
227}
228
229inline constexpr bool QskIntervalF::operator!=( const QskIntervalF& other ) const noexcept
230{
231 return ( !( *this == other ) );
232}
233
234inline QskIntervalF QskIntervalF::operator|( qreal value ) const noexcept
235{
236 return extended( value );
237}
238
239inline QskIntervalF& QskIntervalF::operator|=( qreal value ) noexcept
240{
241 *this = *this | value;
242 return *this;
243}
244
245QT_WARNING_POP
246
247#ifndef QT_NO_DEBUG_STREAM
248
249class QDebug;
250QSK_EXPORT QDebug operator<<( QDebug, const QskIntervalF& );
251#endif
252
253#endif