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