1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QSIZE_H
5#define QSIZE_H
6
7#include <QtCore/qnamespace.h>
8#include <QtCore/qhashfunctions.h>
9#include <QtCore/qmargins.h>
10
11#include <QtCore/q20type_traits.h>
12#include <QtCore/q23utility.h>
13
14#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
15struct CGSize;
16#endif
17
18QT_BEGIN_NAMESPACE
19
20// QT_ENABLE_P0846_SEMANTICS_FOR(get) // from qmargins.h
21
22class QSizeF;
23
24class Q_CORE_EXPORT QSize
25{
26public:
27 constexpr QSize() noexcept;
28 constexpr QSize(int w, int h) noexcept;
29
30 constexpr inline bool isNull() const noexcept;
31 constexpr inline bool isEmpty() const noexcept;
32 constexpr inline bool isValid() const noexcept;
33
34 constexpr inline int width() const noexcept;
35 constexpr inline int height() const noexcept;
36 constexpr inline void setWidth(int w) noexcept;
37 constexpr inline void setHeight(int h) noexcept;
38 void transpose() noexcept;
39 [[nodiscard]] constexpr inline QSize transposed() const noexcept;
40
41 inline void scale(int w, int h, Qt::AspectRatioMode mode) noexcept;
42 inline void scale(const QSize &s, Qt::AspectRatioMode mode) noexcept;
43 [[nodiscard]] QSize scaled(int w, int h, Qt::AspectRatioMode mode) const noexcept;
44 [[nodiscard]] QSize scaled(const QSize &s, Qt::AspectRatioMode mode) const noexcept;
45
46 [[nodiscard]] constexpr inline QSize expandedTo(const QSize &) const noexcept;
47 [[nodiscard]] constexpr inline QSize boundedTo(const QSize &) const noexcept;
48
49 [[nodiscard]] constexpr QSize grownBy(QMargins m) const noexcept
50 { return {width() + m.left() + m.right(), height() + m.top() + m.bottom()}; }
51 [[nodiscard]] constexpr QSize shrunkBy(QMargins m) const noexcept
52 { return {width() - m.left() - m.right(), height() - m.top() - m.bottom()}; }
53
54 constexpr inline int &rwidth() noexcept;
55 constexpr inline int &rheight() noexcept;
56
57 constexpr inline QSize &operator+=(const QSize &) noexcept;
58 constexpr inline QSize &operator-=(const QSize &) noexcept;
59 constexpr inline QSize &operator*=(qreal c) noexcept;
60 inline QSize &operator/=(qreal c);
61
62 friend inline constexpr bool operator==(const QSize &s1, const QSize &s2) noexcept
63 { return s1.wd == s2.wd && s1.ht == s2.ht; }
64 friend inline constexpr bool operator!=(const QSize &s1, const QSize &s2) noexcept
65 { return s1.wd != s2.wd || s1.ht != s2.ht; }
66 friend inline constexpr QSize operator+(const QSize &s1, const QSize &s2) noexcept
67 { return QSize(s1.wd + s2.wd, s1.ht + s2.ht); }
68 friend inline constexpr QSize operator-(const QSize &s1, const QSize &s2) noexcept
69 { return QSize(s1.wd - s2.wd, s1.ht - s2.ht); }
70 friend inline constexpr QSize operator*(const QSize &s, qreal c) noexcept
71 { return QSize(qRound(d: s.wd * c), qRound(d: s.ht * c)); }
72 friend inline constexpr QSize operator*(qreal c, const QSize &s) noexcept
73 { return s * c; }
74 friend inline QSize operator/(const QSize &s, qreal c)
75 { Q_ASSERT(!qFuzzyIsNull(c)); return QSize(qRound(d: s.wd / c), qRound(d: s.ht / c)); }
76 friend inline constexpr size_t qHash(const QSize &, size_t) noexcept;
77
78#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
79 [[nodiscard]] CGSize toCGSize() const noexcept;
80#endif
81
82 [[nodiscard]] inline constexpr QSizeF toSizeF() const noexcept;
83
84private:
85 int wd;
86 int ht;
87
88 template <std::size_t I,
89 typename S,
90 std::enable_if_t<(I < 2), bool> = true,
91 std::enable_if_t<std::is_same_v<q20::remove_cvref_t<S>, QSize>, bool> = true>
92 friend constexpr decltype(auto) get(S &&s) noexcept
93 {
94 if constexpr (I == 0)
95 return q23::forward_like<S>(s.wd);
96 else if constexpr (I == 1)
97 return q23::forward_like<S>(s.ht);
98 }
99};
100Q_DECLARE_TYPEINFO(QSize, Q_RELOCATABLE_TYPE);
101
102/*****************************************************************************
103 QSize stream functions
104 *****************************************************************************/
105
106#ifndef QT_NO_DATASTREAM
107Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QSize &);
108Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QSize &);
109#endif
110
111
112/*****************************************************************************
113 QSize inline functions
114 *****************************************************************************/
115
116constexpr inline QSize::QSize() noexcept : wd(-1), ht(-1) {}
117
118constexpr inline QSize::QSize(int w, int h) noexcept : wd(w), ht(h) {}
119
120constexpr inline bool QSize::isNull() const noexcept
121{ return wd == 0 && ht == 0; }
122
123constexpr inline bool QSize::isEmpty() const noexcept
124{ return wd < 1 || ht < 1; }
125
126constexpr inline bool QSize::isValid() const noexcept
127{ return wd >= 0 && ht >= 0; }
128
129constexpr inline int QSize::width() const noexcept
130{ return wd; }
131
132constexpr inline int QSize::height() const noexcept
133{ return ht; }
134
135constexpr inline void QSize::setWidth(int w) noexcept
136{ wd = w; }
137
138constexpr inline void QSize::setHeight(int h) noexcept
139{ ht = h; }
140
141constexpr inline QSize QSize::transposed() const noexcept
142{ return QSize(ht, wd); }
143
144inline void QSize::scale(int w, int h, Qt::AspectRatioMode mode) noexcept
145{ scale(s: QSize(w, h), mode); }
146
147inline void QSize::scale(const QSize &s, Qt::AspectRatioMode mode) noexcept
148{ *this = scaled(s, mode); }
149
150inline QSize QSize::scaled(int w, int h, Qt::AspectRatioMode mode) const noexcept
151{ return scaled(s: QSize(w, h), mode); }
152
153constexpr inline int &QSize::rwidth() noexcept
154{ return wd; }
155
156constexpr inline int &QSize::rheight() noexcept
157{ return ht; }
158
159constexpr inline QSize &QSize::operator+=(const QSize &s) noexcept
160{
161 wd += s.wd;
162 ht += s.ht;
163 return *this;
164}
165
166constexpr inline QSize &QSize::operator-=(const QSize &s) noexcept
167{
168 wd -= s.wd;
169 ht -= s.ht;
170 return *this;
171}
172
173constexpr inline QSize &QSize::operator*=(qreal c) noexcept
174{
175 wd = qRound(d: wd * c);
176 ht = qRound(d: ht * c);
177 return *this;
178}
179
180constexpr inline size_t qHash(const QSize &s, size_t seed = 0) noexcept
181{ return qHashMulti(seed, args: s.wd, args: s.ht); }
182
183inline QSize &QSize::operator/=(qreal c)
184{
185 Q_ASSERT(!qFuzzyIsNull(c));
186 wd = qRound(d: wd / c);
187 ht = qRound(d: ht / c);
188 return *this;
189}
190
191constexpr inline QSize QSize::expandedTo(const QSize & otherSize) const noexcept
192{
193 return QSize(qMax(a: wd,b: otherSize.wd), qMax(a: ht,b: otherSize.ht));
194}
195
196constexpr inline QSize QSize::boundedTo(const QSize & otherSize) const noexcept
197{
198 return QSize(qMin(a: wd,b: otherSize.wd), qMin(a: ht,b: otherSize.ht));
199}
200
201#ifndef QT_NO_DEBUG_STREAM
202Q_CORE_EXPORT QDebug operator<<(QDebug, const QSize &);
203#endif
204
205
206class Q_CORE_EXPORT QSizeF
207{
208public:
209 constexpr QSizeF() noexcept;
210 constexpr QSizeF(const QSize &sz) noexcept;
211 constexpr QSizeF(qreal w, qreal h) noexcept;
212
213 inline bool isNull() const noexcept;
214 constexpr inline bool isEmpty() const noexcept;
215 constexpr inline bool isValid() const noexcept;
216
217 constexpr inline qreal width() const noexcept;
218 constexpr inline qreal height() const noexcept;
219 constexpr inline void setWidth(qreal w) noexcept;
220 constexpr inline void setHeight(qreal h) noexcept;
221 void transpose() noexcept;
222 [[nodiscard]] constexpr inline QSizeF transposed() const noexcept;
223
224 inline void scale(qreal w, qreal h, Qt::AspectRatioMode mode) noexcept;
225 inline void scale(const QSizeF &s, Qt::AspectRatioMode mode) noexcept;
226 [[nodiscard]] QSizeF scaled(qreal w, qreal h, Qt::AspectRatioMode mode) const noexcept;
227 [[nodiscard]] QSizeF scaled(const QSizeF &s, Qt::AspectRatioMode mode) const noexcept;
228
229 [[nodiscard]] constexpr inline QSizeF expandedTo(const QSizeF &) const noexcept;
230 [[nodiscard]] constexpr inline QSizeF boundedTo(const QSizeF &) const noexcept;
231
232 [[nodiscard]] constexpr QSizeF grownBy(QMarginsF m) const noexcept
233 { return {width() + m.left() + m.right(), height() + m.top() + m.bottom()}; }
234 [[nodiscard]] constexpr QSizeF shrunkBy(QMarginsF m) const noexcept
235 { return {width() - m.left() - m.right(), height() - m.top() - m.bottom()}; }
236
237 constexpr inline qreal &rwidth() noexcept;
238 constexpr inline qreal &rheight() noexcept;
239
240 constexpr inline QSizeF &operator+=(const QSizeF &) noexcept;
241 constexpr inline QSizeF &operator-=(const QSizeF &) noexcept;
242 constexpr inline QSizeF &operator*=(qreal c) noexcept;
243 inline QSizeF &operator/=(qreal c);
244
245 QT_WARNING_PUSH
246 QT_WARNING_DISABLE_FLOAT_COMPARE
247 friend constexpr inline bool operator==(const QSizeF &s1, const QSizeF &s2)
248 {
249 return ((!s1.wd || !s2.wd) ? qFuzzyIsNull(d: s1.wd - s2.wd) : qFuzzyCompare(p1: s1.wd, p2: s2.wd))
250 && ((!s1.ht || !s2.ht) ? qFuzzyIsNull(d: s1.ht - s2.ht) : qFuzzyCompare(p1: s1.ht, p2: s2.ht));
251 }
252 QT_WARNING_POP
253 friend constexpr inline bool operator!=(const QSizeF &s1, const QSizeF &s2)
254 { return !(s1 == s2); }
255 friend constexpr inline QSizeF operator+(const QSizeF &s1, const QSizeF &s2) noexcept
256 { return QSizeF(s1.wd + s2.wd, s1.ht + s2.ht); }
257 friend constexpr inline QSizeF operator-(const QSizeF &s1, const QSizeF &s2) noexcept
258 { return QSizeF(s1.wd - s2.wd, s1.ht - s2.ht); }
259 friend constexpr inline QSizeF operator*(const QSizeF &s, qreal c) noexcept
260 { return QSizeF(s.wd * c, s.ht * c); }
261 friend constexpr inline QSizeF operator*(qreal c, const QSizeF &s) noexcept
262 { return s * c; }
263 friend inline QSizeF operator/(const QSizeF &s, qreal c)
264 { Q_ASSERT(!qFuzzyIsNull(c)); return QSizeF(s.wd / c, s.ht / c); }
265
266 constexpr inline QSize toSize() const noexcept;
267
268#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
269 [[nodiscard]] static QSizeF fromCGSize(CGSize size) noexcept;
270 [[nodiscard]] CGSize toCGSize() const noexcept;
271#endif
272
273private:
274 qreal wd;
275 qreal ht;
276
277 template <std::size_t I,
278 typename S,
279 std::enable_if_t<(I < 2), bool> = true,
280 std::enable_if_t<std::is_same_v<q20::remove_cvref_t<S>, QSizeF>, bool> = true>
281 friend constexpr decltype(auto) get(S &&s) noexcept
282 {
283 if constexpr (I == 0)
284 return q23::forward_like<S>(s.wd);
285 else if constexpr (I == 1)
286 return q23::forward_like<S>(s.ht);
287 }
288};
289Q_DECLARE_TYPEINFO(QSizeF, Q_RELOCATABLE_TYPE);
290
291
292/*****************************************************************************
293 QSizeF stream functions
294 *****************************************************************************/
295
296#ifndef QT_NO_DATASTREAM
297Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QSizeF &);
298Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QSizeF &);
299#endif
300
301
302/*****************************************************************************
303 QSizeF inline functions
304 *****************************************************************************/
305
306constexpr inline QSizeF::QSizeF() noexcept : wd(-1.), ht(-1.) {}
307
308constexpr inline QSizeF::QSizeF(const QSize &sz) noexcept : wd(sz.width()), ht(sz.height()) {}
309
310constexpr inline QSizeF::QSizeF(qreal w, qreal h) noexcept : wd(w), ht(h) {}
311
312inline bool QSizeF::isNull() const noexcept
313{ return qIsNull(d: wd) && qIsNull(d: ht); }
314
315constexpr inline bool QSizeF::isEmpty() const noexcept
316{ return wd <= 0. || ht <= 0.; }
317
318constexpr inline bool QSizeF::isValid() const noexcept
319{ return wd >= 0. && ht >= 0.; }
320
321constexpr inline qreal QSizeF::width() const noexcept
322{ return wd; }
323
324constexpr inline qreal QSizeF::height() const noexcept
325{ return ht; }
326
327constexpr inline void QSizeF::setWidth(qreal w) noexcept
328{ wd = w; }
329
330constexpr inline void QSizeF::setHeight(qreal h) noexcept
331{ ht = h; }
332
333constexpr inline QSizeF QSizeF::transposed() const noexcept
334{ return QSizeF(ht, wd); }
335
336inline void QSizeF::scale(qreal w, qreal h, Qt::AspectRatioMode mode) noexcept
337{ scale(s: QSizeF(w, h), mode); }
338
339inline void QSizeF::scale(const QSizeF &s, Qt::AspectRatioMode mode) noexcept
340{ *this = scaled(s, mode); }
341
342inline QSizeF QSizeF::scaled(qreal w, qreal h, Qt::AspectRatioMode mode) const noexcept
343{ return scaled(s: QSizeF(w, h), mode); }
344
345constexpr inline qreal &QSizeF::rwidth() noexcept
346{ return wd; }
347
348constexpr inline qreal &QSizeF::rheight() noexcept
349{ return ht; }
350
351constexpr inline QSizeF &QSizeF::operator+=(const QSizeF &s) noexcept
352{
353 wd += s.wd;
354 ht += s.ht;
355 return *this;
356}
357
358constexpr inline QSizeF &QSizeF::operator-=(const QSizeF &s) noexcept
359{
360 wd -= s.wd;
361 ht -= s.ht;
362 return *this;
363}
364
365constexpr inline QSizeF &QSizeF::operator*=(qreal c) noexcept
366{
367 wd *= c;
368 ht *= c;
369 return *this;
370}
371
372inline QSizeF &QSizeF::operator/=(qreal c)
373{
374 Q_ASSERT(!qFuzzyIsNull(c) && qIsFinite(c));
375 wd = wd / c;
376 ht = ht / c;
377 return *this;
378}
379
380constexpr inline QSizeF QSizeF::expandedTo(const QSizeF &otherSize) const noexcept
381{
382 return QSizeF(qMax(a: wd, b: otherSize.wd), qMax(a: ht, b: otherSize.ht));
383}
384
385constexpr inline QSizeF QSizeF::boundedTo(const QSizeF &otherSize) const noexcept
386{
387 return QSizeF(qMin(a: wd, b: otherSize.wd), qMin(a: ht, b: otherSize.ht));
388}
389
390constexpr inline QSize QSizeF::toSize() const noexcept
391{
392 return QSize(qRound(d: wd), qRound(d: ht));
393}
394
395constexpr QSizeF QSize::toSizeF() const noexcept { return *this; }
396
397#ifndef QT_NO_DEBUG_STREAM
398Q_CORE_EXPORT QDebug operator<<(QDebug, const QSizeF &);
399#endif
400
401QT_END_NAMESPACE
402
403/*****************************************************************************
404 QSize/QSizeF tuple protocol
405 *****************************************************************************/
406
407namespace std {
408 template <>
409 class tuple_size<QT_PREPEND_NAMESPACE(QSize)> : public integral_constant<size_t, 2> {};
410 template <>
411 class tuple_element<0, QT_PREPEND_NAMESPACE(QSize)> { public: using type = int; };
412 template <>
413 class tuple_element<1, QT_PREPEND_NAMESPACE(QSize)> { public: using type = int; };
414
415 template <>
416 class tuple_size<QT_PREPEND_NAMESPACE(QSizeF)> : public integral_constant<size_t, 2> {};
417 template <>
418 class tuple_element<0, QT_PREPEND_NAMESPACE(QSizeF)> { public: using type = QT_PREPEND_NAMESPACE(qreal); };
419 template <>
420 class tuple_element<1, QT_PREPEND_NAMESPACE(QSizeF)> { public: using type = QT_PREPEND_NAMESPACE(qreal); };
421}
422
423#endif // QSIZE_H
424

source code of qtbase/src/corelib/tools/qsize.h