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/qmargins.h>
45
46#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
47struct CGSize;
48#endif
49
50QT_BEGIN_NAMESPACE
51
52
53class Q_CORE_EXPORT QSize
54{
55public:
56 Q_DECL_CONSTEXPR QSize() noexcept;
57 Q_DECL_CONSTEXPR QSize(int w, int h) noexcept;
58
59 Q_DECL_CONSTEXPR inline bool isNull() const noexcept;
60 Q_DECL_CONSTEXPR inline bool isEmpty() const noexcept;
61 Q_DECL_CONSTEXPR inline bool isValid() const noexcept;
62
63 Q_DECL_CONSTEXPR inline int width() const noexcept;
64 Q_DECL_CONSTEXPR inline int height() const noexcept;
65 Q_DECL_RELAXED_CONSTEXPR inline void setWidth(int w) noexcept;
66 Q_DECL_RELAXED_CONSTEXPR inline void setHeight(int h) noexcept;
67 void transpose() noexcept;
68 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR inline QSize transposed() const noexcept;
69
70 inline void scale(int w, int h, Qt::AspectRatioMode mode) noexcept;
71 inline void scale(const QSize &s, Qt::AspectRatioMode mode) noexcept;
72 Q_REQUIRED_RESULT QSize scaled(int w, int h, Qt::AspectRatioMode mode) const noexcept;
73 Q_REQUIRED_RESULT QSize scaled(const QSize &s, Qt::AspectRatioMode mode) const noexcept;
74
75 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR inline QSize expandedTo(const QSize &) const noexcept;
76 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR inline QSize boundedTo(const QSize &) const noexcept;
77
78 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QSize grownBy(QMargins m) const noexcept
79 { return {width() + m.left() + m.right(), height() + m.top() + m.bottom()}; }
80 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QSize shrunkBy(QMargins m) const noexcept
81 { return {width() - m.left() - m.right(), height() - m.top() - m.bottom()}; }
82
83 Q_DECL_RELAXED_CONSTEXPR inline int &rwidth() noexcept;
84 Q_DECL_RELAXED_CONSTEXPR inline int &rheight() noexcept;
85
86 Q_DECL_RELAXED_CONSTEXPR inline QSize &operator+=(const QSize &) noexcept;
87 Q_DECL_RELAXED_CONSTEXPR inline QSize &operator-=(const QSize &) noexcept;
88 Q_DECL_RELAXED_CONSTEXPR inline QSize &operator*=(qreal c) noexcept;
89 inline QSize &operator/=(qreal c);
90
91 friend inline Q_DECL_CONSTEXPR bool operator==(const QSize &, const QSize &) noexcept;
92 friend inline Q_DECL_CONSTEXPR bool operator!=(const QSize &, const QSize &) noexcept;
93 friend inline Q_DECL_CONSTEXPR const QSize operator+(const QSize &, const QSize &) noexcept;
94 friend inline Q_DECL_CONSTEXPR const QSize operator-(const QSize &, const QSize &) noexcept;
95 friend inline Q_DECL_CONSTEXPR const QSize operator*(const QSize &, qreal) noexcept;
96 friend inline Q_DECL_CONSTEXPR const QSize operator*(qreal, const QSize &) noexcept;
97 friend inline const QSize operator/(const QSize &, qreal);
98
99#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
100 Q_REQUIRED_RESULT CGSize toCGSize() const noexcept;
101#endif
102
103private:
104 int wd;
105 int ht;
106};
107Q_DECLARE_TYPEINFO(QSize, Q_MOVABLE_TYPE);
108
109/*****************************************************************************
110 QSize stream functions
111 *****************************************************************************/
112
113#ifndef QT_NO_DATASTREAM
114Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QSize &);
115Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QSize &);
116#endif
117
118
119/*****************************************************************************
120 QSize inline functions
121 *****************************************************************************/
122
123Q_DECL_CONSTEXPR inline QSize::QSize() noexcept : wd(-1), ht(-1) {}
124
125Q_DECL_CONSTEXPR inline QSize::QSize(int w, int h) noexcept : wd(w), ht(h) {}
126
127Q_DECL_CONSTEXPR inline bool QSize::isNull() const noexcept
128{ return wd==0 && ht==0; }
129
130Q_DECL_CONSTEXPR inline bool QSize::isEmpty() const noexcept
131{ return wd<1 || ht<1; }
132
133Q_DECL_CONSTEXPR inline bool QSize::isValid() const noexcept
134{ return wd>=0 && ht>=0; }
135
136Q_DECL_CONSTEXPR inline int QSize::width() const noexcept
137{ return wd; }
138
139Q_DECL_CONSTEXPR inline int QSize::height() const noexcept
140{ return ht; }
141
142Q_DECL_RELAXED_CONSTEXPR inline void QSize::setWidth(int w) noexcept
143{ wd = w; }
144
145Q_DECL_RELAXED_CONSTEXPR inline void QSize::setHeight(int h) noexcept
146{ ht = h; }
147
148Q_DECL_CONSTEXPR inline QSize QSize::transposed() const noexcept
149{ return QSize(ht, wd); }
150
151inline void QSize::scale(int w, int h, Qt::AspectRatioMode mode) noexcept
152{ scale(QSize(w, h), mode); }
153
154inline void QSize::scale(const QSize &s, Qt::AspectRatioMode mode) noexcept
155{ *this = scaled(s, mode); }
156
157inline QSize QSize::scaled(int w, int h, Qt::AspectRatioMode mode) const noexcept
158{ return scaled(QSize(w, h), mode); }
159
160Q_DECL_RELAXED_CONSTEXPR inline int &QSize::rwidth() noexcept
161{ return wd; }
162
163Q_DECL_RELAXED_CONSTEXPR inline int &QSize::rheight() noexcept
164{ return ht; }
165
166Q_DECL_RELAXED_CONSTEXPR inline QSize &QSize::operator+=(const QSize &s) noexcept
167{ wd+=s.wd; ht+=s.ht; return *this; }
168
169Q_DECL_RELAXED_CONSTEXPR inline QSize &QSize::operator-=(const QSize &s) noexcept
170{ wd-=s.wd; ht-=s.ht; return *this; }
171
172Q_DECL_RELAXED_CONSTEXPR inline QSize &QSize::operator*=(qreal c) noexcept
173{ wd = qRound(wd*c); ht = qRound(ht*c); return *this; }
174
175Q_DECL_CONSTEXPR inline bool operator==(const QSize &s1, const QSize &s2) noexcept
176{ return s1.wd == s2.wd && s1.ht == s2.ht; }
177
178Q_DECL_CONSTEXPR inline bool operator!=(const QSize &s1, const QSize &s2) noexcept
179{ return s1.wd != s2.wd || s1.ht != s2.ht; }
180
181Q_DECL_CONSTEXPR inline const QSize operator+(const QSize & s1, const QSize & s2) noexcept
182{ return QSize(s1.wd+s2.wd, s1.ht+s2.ht); }
183
184Q_DECL_CONSTEXPR inline const QSize operator-(const QSize &s1, const QSize &s2) noexcept
185{ return QSize(s1.wd-s2.wd, s1.ht-s2.ht); }
186
187Q_DECL_CONSTEXPR inline const QSize operator*(const QSize &s, qreal c) noexcept
188{ return QSize(qRound(s.wd*c), qRound(s.ht*c)); }
189
190Q_DECL_CONSTEXPR inline const QSize operator*(qreal c, const QSize &s) noexcept
191{ return QSize(qRound(s.wd*c), qRound(s.ht*c)); }
192
193inline QSize &QSize::operator/=(qreal c)
194{
195 Q_ASSERT(!qFuzzyIsNull(c));
196 wd = qRound(wd/c); ht = qRound(ht/c);
197 return *this;
198}
199
200inline const QSize operator/(const QSize &s, qreal c)
201{
202 Q_ASSERT(!qFuzzyIsNull(c));
203 return QSize(qRound(s.wd/c), qRound(s.ht/c));
204}
205
206Q_DECL_CONSTEXPR inline QSize QSize::expandedTo(const QSize & otherSize) const noexcept
207{
208 return QSize(qMax(wd,otherSize.wd), qMax(ht,otherSize.ht));
209}
210
211Q_DECL_CONSTEXPR inline QSize QSize::boundedTo(const QSize & otherSize) const noexcept
212{
213 return QSize(qMin(wd,otherSize.wd), qMin(ht,otherSize.ht));
214}
215
216#ifndef QT_NO_DEBUG_STREAM
217Q_CORE_EXPORT QDebug operator<<(QDebug, const QSize &);
218#endif
219
220
221class Q_CORE_EXPORT QSizeF
222{
223public:
224 Q_DECL_CONSTEXPR QSizeF() noexcept;
225 Q_DECL_CONSTEXPR QSizeF(const QSize &sz) noexcept;
226 Q_DECL_CONSTEXPR QSizeF(qreal w, qreal h) noexcept;
227
228 inline bool isNull() const noexcept;
229 Q_DECL_CONSTEXPR inline bool isEmpty() const noexcept;
230 Q_DECL_CONSTEXPR inline bool isValid() const noexcept;
231
232 Q_DECL_CONSTEXPR inline qreal width() const noexcept;
233 Q_DECL_CONSTEXPR inline qreal height() const noexcept;
234 Q_DECL_RELAXED_CONSTEXPR inline void setWidth(qreal w) noexcept;
235 Q_DECL_RELAXED_CONSTEXPR inline void setHeight(qreal h) noexcept;
236 void transpose() noexcept;
237 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR inline QSizeF transposed() const noexcept;
238
239 inline void scale(qreal w, qreal h, Qt::AspectRatioMode mode) noexcept;
240 inline void scale(const QSizeF &s, Qt::AspectRatioMode mode) noexcept;
241 Q_REQUIRED_RESULT QSizeF scaled(qreal w, qreal h, Qt::AspectRatioMode mode) const noexcept;
242 Q_REQUIRED_RESULT QSizeF scaled(const QSizeF &s, Qt::AspectRatioMode mode) const noexcept;
243
244 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR inline QSizeF expandedTo(const QSizeF &) const noexcept;
245 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR inline QSizeF boundedTo(const QSizeF &) const noexcept;
246
247 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QSizeF grownBy(QMarginsF m) const noexcept
248 { return {width() + m.left() + m.right(), height() + m.top() + m.bottom()}; }
249 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QSizeF shrunkBy(QMarginsF m) const noexcept
250 { return {width() - m.left() - m.right(), height() - m.top() - m.bottom()}; }
251
252 Q_DECL_RELAXED_CONSTEXPR inline qreal &rwidth() noexcept;
253 Q_DECL_RELAXED_CONSTEXPR inline qreal &rheight() noexcept;
254
255 Q_DECL_RELAXED_CONSTEXPR inline QSizeF &operator+=(const QSizeF &) noexcept;
256 Q_DECL_RELAXED_CONSTEXPR inline QSizeF &operator-=(const QSizeF &) noexcept;
257 Q_DECL_RELAXED_CONSTEXPR inline QSizeF &operator*=(qreal c) noexcept;
258 inline QSizeF &operator/=(qreal c);
259
260 friend Q_DECL_CONSTEXPR inline bool operator==(const QSizeF &, const QSizeF &) noexcept;
261 friend Q_DECL_CONSTEXPR inline bool operator!=(const QSizeF &, const QSizeF &) noexcept;
262 friend Q_DECL_CONSTEXPR inline const QSizeF operator+(const QSizeF &, const QSizeF &) noexcept;
263 friend Q_DECL_CONSTEXPR inline const QSizeF operator-(const QSizeF &, const QSizeF &) noexcept;
264 friend Q_DECL_CONSTEXPR inline const QSizeF operator*(const QSizeF &, qreal) noexcept;
265 friend Q_DECL_CONSTEXPR inline const QSizeF operator*(qreal, const QSizeF &) noexcept;
266 friend inline const QSizeF operator/(const QSizeF &, qreal);
267
268 Q_DECL_CONSTEXPR inline QSize toSize() const noexcept;
269
270#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
271 Q_REQUIRED_RESULT static QSizeF fromCGSize(CGSize size) noexcept;
272 Q_REQUIRED_RESULT CGSize toCGSize() const noexcept;
273#endif
274
275private:
276 qreal wd;
277 qreal ht;
278};
279Q_DECLARE_TYPEINFO(QSizeF, Q_MOVABLE_TYPE);
280
281
282/*****************************************************************************
283 QSizeF stream functions
284 *****************************************************************************/
285
286#ifndef QT_NO_DATASTREAM
287Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QSizeF &);
288Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QSizeF &);
289#endif
290
291
292/*****************************************************************************
293 QSizeF inline functions
294 *****************************************************************************/
295
296Q_DECL_CONSTEXPR inline QSizeF::QSizeF() noexcept : wd(-1.), ht(-1.) {}
297
298Q_DECL_CONSTEXPR inline QSizeF::QSizeF(const QSize &sz) noexcept : wd(sz.width()), ht(sz.height()) {}
299
300Q_DECL_CONSTEXPR inline QSizeF::QSizeF(qreal w, qreal h) noexcept : wd(w), ht(h) {}
301
302inline bool QSizeF::isNull() const noexcept
303{ return qIsNull(wd) && qIsNull(ht); }
304
305Q_DECL_CONSTEXPR inline bool QSizeF::isEmpty() const noexcept
306{ return wd <= 0. || ht <= 0.; }
307
308Q_DECL_CONSTEXPR inline bool QSizeF::isValid() const noexcept
309{ return wd >= 0. && ht >= 0.; }
310
311Q_DECL_CONSTEXPR inline qreal QSizeF::width() const noexcept
312{ return wd; }
313
314Q_DECL_CONSTEXPR inline qreal QSizeF::height() const noexcept
315{ return ht; }
316
317Q_DECL_RELAXED_CONSTEXPR inline void QSizeF::setWidth(qreal w) noexcept
318{ wd = w; }
319
320Q_DECL_RELAXED_CONSTEXPR inline void QSizeF::setHeight(qreal h) noexcept
321{ ht = h; }
322
323Q_DECL_CONSTEXPR inline QSizeF QSizeF::transposed() const noexcept
324{ return QSizeF(ht, wd); }
325
326inline void QSizeF::scale(qreal w, qreal h, Qt::AspectRatioMode mode) noexcept
327{ scale(QSizeF(w, h), mode); }
328
329inline void QSizeF::scale(const QSizeF &s, Qt::AspectRatioMode mode) noexcept
330{ *this = scaled(s, mode); }
331
332inline QSizeF QSizeF::scaled(qreal w, qreal h, Qt::AspectRatioMode mode) const noexcept
333{ return scaled(QSizeF(w, h), mode); }
334
335Q_DECL_RELAXED_CONSTEXPR inline qreal &QSizeF::rwidth() noexcept
336{ return wd; }
337
338Q_DECL_RELAXED_CONSTEXPR inline qreal &QSizeF::rheight() noexcept
339{ return ht; }
340
341Q_DECL_RELAXED_CONSTEXPR inline QSizeF &QSizeF::operator+=(const QSizeF &s) noexcept
342{ wd += s.wd; ht += s.ht; return *this; }
343
344Q_DECL_RELAXED_CONSTEXPR inline QSizeF &QSizeF::operator-=(const QSizeF &s) noexcept
345{ wd -= s.wd; ht -= s.ht; return *this; }
346
347Q_DECL_RELAXED_CONSTEXPR inline QSizeF &QSizeF::operator*=(qreal c) noexcept
348{ wd *= c; ht *= c; return *this; }
349
350Q_DECL_CONSTEXPR inline bool operator==(const QSizeF &s1, const QSizeF &s2) noexcept
351{ return qFuzzyCompare(s1.wd, s2.wd) && qFuzzyCompare(s1.ht, s2.ht); }
352
353Q_DECL_CONSTEXPR inline bool operator!=(const QSizeF &s1, const QSizeF &s2) noexcept
354{ return !qFuzzyCompare(s1.wd, s2.wd) || !qFuzzyCompare(s1.ht, s2.ht); }
355
356Q_DECL_CONSTEXPR inline const QSizeF operator+(const QSizeF & s1, const QSizeF & s2) noexcept
357{ return QSizeF(s1.wd+s2.wd, s1.ht+s2.ht); }
358
359Q_DECL_CONSTEXPR inline const QSizeF operator-(const QSizeF &s1, const QSizeF &s2) noexcept
360{ return QSizeF(s1.wd-s2.wd, s1.ht-s2.ht); }
361
362Q_DECL_CONSTEXPR inline const QSizeF operator*(const QSizeF &s, qreal c) noexcept
363{ return QSizeF(s.wd*c, s.ht*c); }
364
365Q_DECL_CONSTEXPR inline const QSizeF operator*(qreal c, const QSizeF &s) noexcept
366{ return QSizeF(s.wd*c, s.ht*c); }
367
368inline QSizeF &QSizeF::operator/=(qreal c)
369{
370 Q_ASSERT(!qFuzzyIsNull(c));
371 wd = wd/c; ht = ht/c;
372 return *this;
373}
374
375inline const QSizeF operator/(const QSizeF &s, qreal c)
376{
377 Q_ASSERT(!qFuzzyIsNull(c));
378 return QSizeF(s.wd/c, s.ht/c);
379}
380
381Q_DECL_CONSTEXPR inline QSizeF QSizeF::expandedTo(const QSizeF & otherSize) const noexcept
382{
383 return QSizeF(qMax(wd,otherSize.wd), qMax(ht,otherSize.ht));
384}
385
386Q_DECL_CONSTEXPR inline QSizeF QSizeF::boundedTo(const QSizeF & otherSize) const noexcept
387{
388 return QSizeF(qMin(wd,otherSize.wd), qMin(ht,otherSize.ht));
389}
390
391Q_DECL_CONSTEXPR inline QSize QSizeF::toSize() const noexcept
392{
393 return QSize(qRound(wd), qRound(ht));
394}
395
396#ifndef QT_NO_DEBUG_STREAM
397Q_CORE_EXPORT QDebug operator<<(QDebug, const QSizeF &);
398#endif
399
400QT_END_NAMESPACE
401
402#endif // QSIZE_H
403