1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtCore module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #ifndef QSIZE_H |
41 | #define QSIZE_H |
42 | |
43 | #include <QtCore/qnamespace.h> |
44 | #include <QtCore/qhashfunctions.h> |
45 | #include <QtCore/qmargins.h> |
46 | |
47 | #if defined(Q_OS_DARWIN) || defined(Q_QDOC) |
48 | struct CGSize; |
49 | #endif |
50 | |
51 | QT_BEGIN_NAMESPACE |
52 | |
53 | |
54 | class Q_CORE_EXPORT QSize |
55 | { |
56 | public: |
57 | constexpr QSize() noexcept; |
58 | constexpr QSize(int w, int h) noexcept; |
59 | |
60 | constexpr inline bool isNull() const noexcept; |
61 | constexpr inline bool isEmpty() const noexcept; |
62 | constexpr inline bool isValid() const noexcept; |
63 | |
64 | constexpr inline int width() const noexcept; |
65 | constexpr inline int height() const noexcept; |
66 | constexpr inline void setWidth(int w) noexcept; |
67 | constexpr inline void setHeight(int h) noexcept; |
68 | void transpose() noexcept; |
69 | [[nodiscard]] constexpr inline QSize transposed() const noexcept; |
70 | |
71 | inline void scale(int w, int h, Qt::AspectRatioMode mode) noexcept; |
72 | inline void scale(const QSize &s, Qt::AspectRatioMode mode) noexcept; |
73 | [[nodiscard]] QSize scaled(int w, int h, Qt::AspectRatioMode mode) const noexcept; |
74 | [[nodiscard]] QSize scaled(const QSize &s, Qt::AspectRatioMode mode) const noexcept; |
75 | |
76 | [[nodiscard]] constexpr inline QSize expandedTo(const QSize &) const noexcept; |
77 | [[nodiscard]] constexpr inline QSize boundedTo(const QSize &) const noexcept; |
78 | |
79 | [[nodiscard]] constexpr QSize grownBy(QMargins m) const noexcept |
80 | { return {width() + m.left() + m.right(), height() + m.top() + m.bottom()}; } |
81 | [[nodiscard]] constexpr QSize shrunkBy(QMargins m) const noexcept |
82 | { return {width() - m.left() - m.right(), height() - m.top() - m.bottom()}; } |
83 | |
84 | constexpr inline int &rwidth() noexcept; |
85 | constexpr inline int &rheight() noexcept; |
86 | |
87 | constexpr inline QSize &operator+=(const QSize &) noexcept; |
88 | constexpr inline QSize &operator-=(const QSize &) noexcept; |
89 | constexpr inline QSize &operator*=(qreal c) noexcept; |
90 | inline QSize &operator/=(qreal c); |
91 | |
92 | friend inline constexpr bool operator==(const QSize &s1, const QSize &s2) noexcept |
93 | { return s1.wd == s2.wd && s1.ht == s2.ht; } |
94 | friend inline constexpr bool operator!=(const QSize &s1, const QSize &s2) noexcept |
95 | { return s1.wd != s2.wd || s1.ht != s2.ht; } |
96 | friend inline constexpr QSize operator+(const QSize &s1, const QSize &s2) noexcept |
97 | { return QSize(s1.wd + s2.wd, s1.ht + s2.ht); } |
98 | friend inline constexpr QSize operator-(const QSize &s1, const QSize &s2) noexcept |
99 | { return QSize(s1.wd - s2.wd, s1.ht - s2.ht); } |
100 | friend inline constexpr QSize operator*(const QSize &s, qreal c) noexcept |
101 | { return QSize(qRound(s.wd * c), qRound(s.ht * c)); } |
102 | friend inline constexpr QSize operator*(qreal c, const QSize &s) noexcept |
103 | { return s * c; } |
104 | friend inline QSize operator/(const QSize &s, qreal c) |
105 | { Q_ASSERT(!qFuzzyIsNull(c)); return QSize(qRound(s.wd / c), qRound(s.ht / c)); } |
106 | friend inline constexpr size_t qHash(const QSize &, size_t) noexcept; |
107 | |
108 | #if defined(Q_OS_DARWIN) || defined(Q_QDOC) |
109 | [[nodiscard]] CGSize toCGSize() const noexcept; |
110 | #endif |
111 | |
112 | private: |
113 | int wd; |
114 | int ht; |
115 | }; |
116 | Q_DECLARE_TYPEINFO(QSize, Q_MOVABLE_TYPE); |
117 | |
118 | /***************************************************************************** |
119 | QSize stream functions |
120 | *****************************************************************************/ |
121 | |
122 | #ifndef QT_NO_DATASTREAM |
123 | Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QSize &); |
124 | Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QSize &); |
125 | #endif |
126 | |
127 | |
128 | /***************************************************************************** |
129 | QSize inline functions |
130 | *****************************************************************************/ |
131 | |
132 | constexpr inline QSize::QSize() noexcept : wd(-1), ht(-1) {} |
133 | |
134 | constexpr inline QSize::QSize(int w, int h) noexcept : wd(w), ht(h) {} |
135 | |
136 | constexpr inline bool QSize::isNull() const noexcept |
137 | { return wd == 0 && ht == 0; } |
138 | |
139 | constexpr inline bool QSize::isEmpty() const noexcept |
140 | { return wd < 1 || ht < 1; } |
141 | |
142 | constexpr inline bool QSize::isValid() const noexcept |
143 | { return wd >= 0 && ht >= 0; } |
144 | |
145 | constexpr inline int QSize::width() const noexcept |
146 | { return wd; } |
147 | |
148 | constexpr inline int QSize::height() const noexcept |
149 | { return ht; } |
150 | |
151 | constexpr inline void QSize::setWidth(int w) noexcept |
152 | { wd = w; } |
153 | |
154 | constexpr inline void QSize::setHeight(int h) noexcept |
155 | { ht = h; } |
156 | |
157 | constexpr inline QSize QSize::transposed() const noexcept |
158 | { return QSize(ht, wd); } |
159 | |
160 | inline void QSize::scale(int w, int h, Qt::AspectRatioMode mode) noexcept |
161 | { scale(QSize(w, h), mode); } |
162 | |
163 | inline void QSize::scale(const QSize &s, Qt::AspectRatioMode mode) noexcept |
164 | { *this = scaled(s, mode); } |
165 | |
166 | inline QSize QSize::scaled(int w, int h, Qt::AspectRatioMode mode) const noexcept |
167 | { return scaled(QSize(w, h), mode); } |
168 | |
169 | constexpr inline int &QSize::rwidth() noexcept |
170 | { return wd; } |
171 | |
172 | constexpr inline int &QSize::rheight() noexcept |
173 | { return ht; } |
174 | |
175 | constexpr inline QSize &QSize::operator+=(const QSize &s) noexcept |
176 | { |
177 | wd += s.wd; |
178 | ht += s.ht; |
179 | return *this; |
180 | } |
181 | |
182 | constexpr inline QSize &QSize::operator-=(const QSize &s) noexcept |
183 | { |
184 | wd -= s.wd; |
185 | ht -= s.ht; |
186 | return *this; |
187 | } |
188 | |
189 | constexpr inline QSize &QSize::operator*=(qreal c) noexcept |
190 | { |
191 | wd = qRound(wd * c); |
192 | ht = qRound(ht * c); |
193 | return *this; |
194 | } |
195 | |
196 | constexpr inline size_t qHash(const QSize &s, size_t seed = 0) noexcept |
197 | { return qHashMulti(seed, s.wd, s.ht); } |
198 | |
199 | inline QSize &QSize::operator/=(qreal c) |
200 | { |
201 | Q_ASSERT(!qFuzzyIsNull(c)); |
202 | wd = qRound(wd / c); |
203 | ht = qRound(ht / c); |
204 | return *this; |
205 | } |
206 | |
207 | constexpr inline QSize QSize::expandedTo(const QSize & otherSize) const noexcept |
208 | { |
209 | return QSize(qMax(wd,otherSize.wd), qMax(ht,otherSize.ht)); |
210 | } |
211 | |
212 | constexpr inline QSize QSize::boundedTo(const QSize & otherSize) const noexcept |
213 | { |
214 | return QSize(qMin(wd,otherSize.wd), qMin(ht,otherSize.ht)); |
215 | } |
216 | |
217 | #ifndef QT_NO_DEBUG_STREAM |
218 | Q_CORE_EXPORT QDebug operator<<(QDebug, const QSize &); |
219 | #endif |
220 | |
221 | |
222 | class Q_CORE_EXPORT QSizeF |
223 | { |
224 | public: |
225 | constexpr QSizeF() noexcept; |
226 | constexpr QSizeF(const QSize &sz) noexcept; |
227 | constexpr QSizeF(qreal w, qreal h) noexcept; |
228 | |
229 | inline bool isNull() const noexcept; |
230 | constexpr inline bool isEmpty() const noexcept; |
231 | constexpr inline bool isValid() const noexcept; |
232 | |
233 | constexpr inline qreal width() const noexcept; |
234 | constexpr inline qreal height() const noexcept; |
235 | constexpr inline void setWidth(qreal w) noexcept; |
236 | constexpr inline void setHeight(qreal h) noexcept; |
237 | void transpose() noexcept; |
238 | [[nodiscard]] constexpr inline QSizeF transposed() const noexcept; |
239 | |
240 | inline void scale(qreal w, qreal h, Qt::AspectRatioMode mode) noexcept; |
241 | inline void scale(const QSizeF &s, Qt::AspectRatioMode mode) noexcept; |
242 | [[nodiscard]] QSizeF scaled(qreal w, qreal h, Qt::AspectRatioMode mode) const noexcept; |
243 | [[nodiscard]] QSizeF scaled(const QSizeF &s, Qt::AspectRatioMode mode) const noexcept; |
244 | |
245 | [[nodiscard]] constexpr inline QSizeF expandedTo(const QSizeF &) const noexcept; |
246 | [[nodiscard]] constexpr inline QSizeF boundedTo(const QSizeF &) const noexcept; |
247 | |
248 | [[nodiscard]] constexpr QSizeF grownBy(QMarginsF m) const noexcept |
249 | { return {width() + m.left() + m.right(), height() + m.top() + m.bottom()}; } |
250 | [[nodiscard]] constexpr QSizeF shrunkBy(QMarginsF m) const noexcept |
251 | { return {width() - m.left() - m.right(), height() - m.top() - m.bottom()}; } |
252 | |
253 | constexpr inline qreal &rwidth() noexcept; |
254 | constexpr inline qreal &rheight() noexcept; |
255 | |
256 | constexpr inline QSizeF &operator+=(const QSizeF &) noexcept; |
257 | constexpr inline QSizeF &operator-=(const QSizeF &) noexcept; |
258 | constexpr inline QSizeF &operator*=(qreal c) noexcept; |
259 | inline QSizeF &operator/=(qreal c); |
260 | |
261 | QT_WARNING_PUSH |
262 | QT_WARNING_DISABLE_FLOAT_COMPARE |
263 | friend constexpr inline bool operator==(const QSizeF &s1, const QSizeF &s2) |
264 | { |
265 | return ((!s1.wd || !s2.wd) ? qFuzzyIsNull(s1.wd - s2.wd) : qFuzzyCompare(s1.wd, s2.wd)) |
266 | && ((!s1.ht || !s2.ht) ? qFuzzyIsNull(s1.ht - s2.ht) : qFuzzyCompare(s1.ht, s2.ht)); |
267 | } |
268 | QT_WARNING_POP |
269 | friend constexpr inline bool operator!=(const QSizeF &s1, const QSizeF &s2) |
270 | { return !(s1 == s2); } |
271 | friend constexpr inline QSizeF operator+(const QSizeF &s1, const QSizeF &s2) noexcept |
272 | { return QSizeF(s1.wd + s2.wd, s1.ht + s2.ht); } |
273 | friend constexpr inline QSizeF operator-(const QSizeF &s1, const QSizeF &s2) noexcept |
274 | { return QSizeF(s1.wd - s2.wd, s1.ht - s2.ht); } |
275 | friend constexpr inline QSizeF operator*(const QSizeF &s, qreal c) noexcept |
276 | { return QSizeF(s.wd * c, s.ht * c); } |
277 | friend constexpr inline QSizeF operator*(qreal c, const QSizeF &s) noexcept |
278 | { return s * c; } |
279 | friend inline QSizeF operator/(const QSizeF &s, qreal c) |
280 | { Q_ASSERT(!qFuzzyIsNull(c)); return QSizeF(s.wd / c, s.ht / c); } |
281 | |
282 | constexpr inline QSize toSize() const noexcept; |
283 | |
284 | #if defined(Q_OS_DARWIN) || defined(Q_QDOC) |
285 | [[nodiscard]] static QSizeF fromCGSize(CGSize size) noexcept; |
286 | [[nodiscard]] CGSize toCGSize() const noexcept; |
287 | #endif |
288 | |
289 | private: |
290 | qreal wd; |
291 | qreal ht; |
292 | }; |
293 | Q_DECLARE_TYPEINFO(QSizeF, Q_MOVABLE_TYPE); |
294 | |
295 | |
296 | /***************************************************************************** |
297 | QSizeF stream functions |
298 | *****************************************************************************/ |
299 | |
300 | #ifndef QT_NO_DATASTREAM |
301 | Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QSizeF &); |
302 | Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QSizeF &); |
303 | #endif |
304 | |
305 | |
306 | /***************************************************************************** |
307 | QSizeF inline functions |
308 | *****************************************************************************/ |
309 | |
310 | constexpr inline QSizeF::QSizeF() noexcept : wd(-1.), ht(-1.) {} |
311 | |
312 | constexpr inline QSizeF::QSizeF(const QSize &sz) noexcept : wd(sz.width()), ht(sz.height()) {} |
313 | |
314 | constexpr inline QSizeF::QSizeF(qreal w, qreal h) noexcept : wd(w), ht(h) {} |
315 | |
316 | inline bool QSizeF::isNull() const noexcept |
317 | { return qIsNull(wd) && qIsNull(ht); } |
318 | |
319 | constexpr inline bool QSizeF::isEmpty() const noexcept |
320 | { return wd <= 0. || ht <= 0.; } |
321 | |
322 | constexpr inline bool QSizeF::isValid() const noexcept |
323 | { return wd >= 0. && ht >= 0.; } |
324 | |
325 | constexpr inline qreal QSizeF::width() const noexcept |
326 | { return wd; } |
327 | |
328 | constexpr inline qreal QSizeF::height() const noexcept |
329 | { return ht; } |
330 | |
331 | constexpr inline void QSizeF::setWidth(qreal w) noexcept |
332 | { wd = w; } |
333 | |
334 | constexpr inline void QSizeF::setHeight(qreal h) noexcept |
335 | { ht = h; } |
336 | |
337 | constexpr inline QSizeF QSizeF::transposed() const noexcept |
338 | { return QSizeF(ht, wd); } |
339 | |
340 | inline void QSizeF::scale(qreal w, qreal h, Qt::AspectRatioMode mode) noexcept |
341 | { scale(QSizeF(w, h), mode); } |
342 | |
343 | inline void QSizeF::scale(const QSizeF &s, Qt::AspectRatioMode mode) noexcept |
344 | { *this = scaled(s, mode); } |
345 | |
346 | inline QSizeF QSizeF::scaled(qreal w, qreal h, Qt::AspectRatioMode mode) const noexcept |
347 | { return scaled(QSizeF(w, h), mode); } |
348 | |
349 | constexpr inline qreal &QSizeF::rwidth() noexcept |
350 | { return wd; } |
351 | |
352 | constexpr inline qreal &QSizeF::rheight() noexcept |
353 | { return ht; } |
354 | |
355 | constexpr inline QSizeF &QSizeF::operator+=(const QSizeF &s) noexcept |
356 | { |
357 | wd += s.wd; |
358 | ht += s.ht; |
359 | return *this; |
360 | } |
361 | |
362 | constexpr inline QSizeF &QSizeF::operator-=(const QSizeF &s) noexcept |
363 | { |
364 | wd -= s.wd; |
365 | ht -= s.ht; |
366 | return *this; |
367 | } |
368 | |
369 | constexpr inline QSizeF &QSizeF::operator*=(qreal c) noexcept |
370 | { |
371 | wd *= c; |
372 | ht *= c; |
373 | return *this; |
374 | } |
375 | |
376 | inline QSizeF &QSizeF::operator/=(qreal c) |
377 | { |
378 | Q_ASSERT(!qFuzzyIsNull(c)); |
379 | wd = wd / c; |
380 | ht = ht / c; |
381 | return *this; |
382 | } |
383 | |
384 | constexpr inline QSizeF QSizeF::expandedTo(const QSizeF &otherSize) const noexcept |
385 | { |
386 | return QSizeF(qMax(wd, otherSize.wd), qMax(ht, otherSize.ht)); |
387 | } |
388 | |
389 | constexpr inline QSizeF QSizeF::boundedTo(const QSizeF &otherSize) const noexcept |
390 | { |
391 | return QSizeF(qMin(wd, otherSize.wd), qMin(ht, otherSize.ht)); |
392 | } |
393 | |
394 | constexpr inline QSize QSizeF::toSize() const noexcept |
395 | { |
396 | return QSize(qRound(wd), qRound(ht)); |
397 | } |
398 | |
399 | #ifndef QT_NO_DEBUG_STREAM |
400 | Q_CORE_EXPORT QDebug operator<<(QDebug, const QSizeF &); |
401 | #endif |
402 | |
403 | QT_END_NAMESPACE |
404 | |
405 | #endif // QSIZE_H |
406 | |