QtCore/qsize.h Source File

qsize.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QSIZE_H
43 #define QSIZE_H
44 
45 #include <QtCore/qnamespace.h>
46 
48 
50 
51 QT_MODULE(Core)
52 
53 class Q_CORE_EXPORT QSize
54 {
55 public:
56  QSize();
57  QSize(int w, int h);
58 
59  bool isNull() const;
60  bool isEmpty() const;
61  bool isValid() const;
62 
63  int width() const;
64  int height() const;
65  void setWidth(int w);
66  void setHeight(int h);
67  void transpose();
68 
69  void scale(int w, int h, Qt::AspectRatioMode mode);
70  void scale(const QSize &s, Qt::AspectRatioMode mode);
71 
72  QSize expandedTo(const QSize &) const;
73  QSize boundedTo(const QSize &) const;
74 
75  int &rwidth();
76  int &rheight();
77 
78  QSize &operator+=(const QSize &);
79  QSize &operator-=(const QSize &);
80  QSize &operator*=(qreal c);
81  QSize &operator/=(qreal c);
82 
83  friend inline bool operator==(const QSize &, const QSize &);
84  friend inline bool operator!=(const QSize &, const QSize &);
85  friend inline const QSize operator+(const QSize &, const QSize &);
86  friend inline const QSize operator-(const QSize &, const QSize &);
87  friend inline const QSize operator*(const QSize &, qreal);
88  friend inline const QSize operator*(qreal, const QSize &);
89  friend inline const QSize operator/(const QSize &, qreal);
90 
91 private:
92  int wd;
93  int ht;
94 };
95 Q_DECLARE_TYPEINFO(QSize, Q_MOVABLE_TYPE);
96 
97 /*****************************************************************************
98  QSize stream functions
99  *****************************************************************************/
100 
101 #ifndef QT_NO_DATASTREAM
102 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QSize &);
103 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QSize &);
104 #endif
105 
106 
107 /*****************************************************************************
108  QSize inline functions
109  *****************************************************************************/
110 
111 inline QSize::QSize()
112 { wd = ht = -1; }
113 
114 inline QSize::QSize(int w, int h)
115 { wd = w; ht = h; }
116 
117 inline bool QSize::isNull() const
118 { return wd==0 && ht==0; }
119 
120 inline bool QSize::isEmpty() const
121 { return wd<1 || ht<1; }
122 
123 inline bool QSize::isValid() const
124 { return wd>=0 && ht>=0; }
125 
126 inline int QSize::width() const
127 { return wd; }
128 
129 inline int QSize::height() const
130 { return ht; }
131 
132 inline void QSize::setWidth(int w)
133 { wd = w; }
134 
135 inline void QSize::setHeight(int h)
136 { ht = h; }
137 
138 inline void QSize::scale(int w, int h, Qt::AspectRatioMode mode)
139 { scale(QSize(w, h), mode); }
140 
141 inline int &QSize::rwidth()
142 { return wd; }
143 
144 inline int &QSize::rheight()
145 { return ht; }
146 
148 { wd+=s.wd; ht+=s.ht; return *this; }
149 
151 { wd-=s.wd; ht-=s.ht; return *this; }
152 
153 inline QSize &QSize::operator*=(qreal c)
154 { wd = qRound(wd*c); ht = qRound(ht*c); return *this; }
155 
156 inline bool operator==(const QSize &s1, const QSize &s2)
157 { return s1.wd == s2.wd && s1.ht == s2.ht; }
158 
159 inline bool operator!=(const QSize &s1, const QSize &s2)
160 { return s1.wd != s2.wd || s1.ht != s2.ht; }
161 
162 inline const QSize operator+(const QSize & s1, const QSize & s2)
163 { return QSize(s1.wd+s2.wd, s1.ht+s2.ht); }
164 
165 inline const QSize operator-(const QSize &s1, const QSize &s2)
166 { return QSize(s1.wd-s2.wd, s1.ht-s2.ht); }
167 
168 inline const QSize operator*(const QSize &s, qreal c)
169 { return QSize(qRound(s.wd*c), qRound(s.ht*c)); }
170 
171 inline const QSize operator*(qreal c, const QSize &s)
172 { return QSize(qRound(s.wd*c), qRound(s.ht*c)); }
173 
174 inline QSize &QSize::operator/=(qreal c)
175 {
176  Q_ASSERT(!qFuzzyIsNull(c));
177  wd = qRound(wd/c); ht = qRound(ht/c);
178  return *this;
179 }
180 
181 inline const QSize operator/(const QSize &s, qreal c)
182 {
183  Q_ASSERT(!qFuzzyIsNull(c));
184  return QSize(qRound(s.wd/c), qRound(s.ht/c));
185 }
186 
187 inline QSize QSize::expandedTo(const QSize & otherSize) const
188 {
189  return QSize(qMax(wd,otherSize.wd), qMax(ht,otherSize.ht));
190 }
191 
192 inline QSize QSize::boundedTo(const QSize & otherSize) const
193 {
194  return QSize(qMin(wd,otherSize.wd), qMin(ht,otherSize.ht));
195 }
196 
197 #ifndef QT_NO_DEBUG_STREAM
198 Q_CORE_EXPORT QDebug operator<<(QDebug, const QSize &);
199 #endif
200 
201 
202 class Q_CORE_EXPORT QSizeF
203 {
204 public:
205  QSizeF();
206  QSizeF(const QSize &sz);
207  QSizeF(qreal w, qreal h);
208 
209  bool isNull() const;
210  bool isEmpty() const;
211  bool isValid() const;
212 
213  qreal width() const;
214  qreal height() const;
215  void setWidth(qreal w);
216  void setHeight(qreal h);
217  void transpose();
218 
219  void scale(qreal w, qreal h, Qt::AspectRatioMode mode);
220  void scale(const QSizeF &s, Qt::AspectRatioMode mode);
221 
222  QSizeF expandedTo(const QSizeF &) const;
223  QSizeF boundedTo(const QSizeF &) const;
224 
225  qreal &rwidth();
226  qreal &rheight();
227 
228  QSizeF &operator+=(const QSizeF &);
229  QSizeF &operator-=(const QSizeF &);
230  QSizeF &operator*=(qreal c);
231  QSizeF &operator/=(qreal c);
232 
233  friend inline bool operator==(const QSizeF &, const QSizeF &);
234  friend inline bool operator!=(const QSizeF &, const QSizeF &);
235  friend inline const QSizeF operator+(const QSizeF &, const QSizeF &);
236  friend inline const QSizeF operator-(const QSizeF &, const QSizeF &);
237  friend inline const QSizeF operator*(const QSizeF &, qreal);
238  friend inline const QSizeF operator*(qreal, const QSizeF &);
239  friend inline const QSizeF operator/(const QSizeF &, qreal);
240 
241  inline QSize toSize() const;
242 
243 private:
244  qreal wd;
245  qreal ht;
246 };
247 Q_DECLARE_TYPEINFO(QSizeF, Q_MOVABLE_TYPE);
248 
249 
250 /*****************************************************************************
251  QSizeF stream functions
252  *****************************************************************************/
253 
254 #ifndef QT_NO_DATASTREAM
255 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QSizeF &);
256 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QSizeF &);
257 #endif
258 
259 
260 /*****************************************************************************
261  QSizeF inline functions
262  *****************************************************************************/
263 
265 { wd = ht = -1.; }
266 
267 inline QSizeF::QSizeF(const QSize &sz)
268  : wd(sz.width()), ht(sz.height())
269 {
270 }
271 
272 inline QSizeF::QSizeF(qreal w, qreal h)
273 { wd = w; ht = h; }
274 
275 inline bool QSizeF::isNull() const
276 { return qIsNull(wd) && qIsNull(ht); }
277 
278 inline bool QSizeF::isEmpty() const
279 { return wd <= 0. || ht <= 0.; }
280 
281 inline bool QSizeF::isValid() const
282 { return wd >= 0. && ht >= 0.; }
283 
284 inline qreal QSizeF::width() const
285 { return wd; }
286 
287 inline qreal QSizeF::height() const
288 { return ht; }
289 
290 inline void QSizeF::setWidth(qreal w)
291 { wd = w; }
292 
293 inline void QSizeF::setHeight(qreal h)
294 { ht = h; }
295 
296 inline void QSizeF::scale(qreal w, qreal h, Qt::AspectRatioMode mode)
297 { scale(QSizeF(w, h), mode); }
298 
299 inline qreal &QSizeF::rwidth()
300 { return wd; }
301 
302 inline qreal &QSizeF::rheight()
303 { return ht; }
304 
306 { wd += s.wd; ht += s.ht; return *this; }
307 
309 { wd -= s.wd; ht -= s.ht; return *this; }
310 
312 { wd *= c; ht *= c; return *this; }
313 
314 inline bool operator==(const QSizeF &s1, const QSizeF &s2)
315 { return qFuzzyCompare(s1.wd, s2.wd) && qFuzzyCompare(s1.ht, s2.ht); }
316 
317 inline bool operator!=(const QSizeF &s1, const QSizeF &s2)
318 { return !qFuzzyCompare(s1.wd, s2.wd) || !qFuzzyCompare(s1.ht, s2.ht); }
319 
320 inline const QSizeF operator+(const QSizeF & s1, const QSizeF & s2)
321 { return QSizeF(s1.wd+s2.wd, s1.ht+s2.ht); }
322 
323 inline const QSizeF operator-(const QSizeF &s1, const QSizeF &s2)
324 { return QSizeF(s1.wd-s2.wd, s1.ht-s2.ht); }
325 
326 inline const QSizeF operator*(const QSizeF &s, qreal c)
327 { return QSizeF(s.wd*c, s.ht*c); }
328 
329 inline const QSizeF operator*(qreal c, const QSizeF &s)
330 { return QSizeF(s.wd*c, s.ht*c); }
331 
333 {
334  Q_ASSERT(!qFuzzyIsNull(c));
335  wd = wd/c; ht = ht/c;
336  return *this;
337 }
338 
339 inline const QSizeF operator/(const QSizeF &s, qreal c)
340 {
341  Q_ASSERT(!qFuzzyIsNull(c));
342  return QSizeF(s.wd/c, s.ht/c);
343 }
344 
345 inline QSizeF QSizeF::expandedTo(const QSizeF & otherSize) const
346 {
347  return QSizeF(qMax(wd,otherSize.wd), qMax(ht,otherSize.ht));
348 }
349 
350 inline QSizeF QSizeF::boundedTo(const QSizeF & otherSize) const
351 {
352  return QSizeF(qMin(wd,otherSize.wd), qMin(ht,otherSize.ht));
353 }
354 
355 inline QSize QSizeF::toSize() const
356 {
357  return QSize(qRound(wd), qRound(ht));
358 }
359 
360 #ifndef QT_NO_DEBUG_STREAM
361 Q_CORE_EXPORT QDebug operator<<(QDebug, const QSizeF &);
362 #endif
363 
365 
367 
368 #endif // QSIZE_H
QSizeF boundedTo(const QSizeF &) const
Definition: qsize.h:350
qreal & rheight()
Definition: qsize.h:302
void setHeight(qreal h)
Definition: qsize.h:293
GLint mode
Definition: GLee.h:4479
const QSize operator/(const QSize &s, qreal c)
Definition: qsize.h:181
QSizeF & operator*=(qreal c)
Definition: qsize.h:311
qreal & rwidth()
Definition: qsize.h:299
#define QT_END_NAMESPACE
Definition: qglobal.h:128
int height() const
Definition: qsize.h:129
QByteArray & operator+=(QByteArray &a, const QStringBuilder< A, B > &b)
#define QT_BEGIN_HEADER
Definition: qglobal.h:141
bool isEmpty() const
Definition: qsize.h:278
int & rheight()
Definition: qsize.h:144
bool isNull() const
Definition: qsize.h:275
QSizeF & operator-=(const QSizeF &)
Definition: qsize.h:308
GLenum GLsizei width
Definition: GLee.h:873
QSize & operator+=(const QSize &)
Definition: qsize.h:147
void setWidth(qreal w)
Definition: qsize.h:290
QSizeF & operator/=(qreal c)
Definition: qsize.h:332
QSizeF expandedTo(const QSizeF &) const
Definition: qsize.h:345
QSize & operator-=(const QSize &)
Definition: qsize.h:150
Definition: qdebug.h:62
bool operator!=(const QSize &s1, const QSize &s2)
Definition: qsize.h:159
#define QT_BEGIN_NAMESPACE
Definition: qglobal.h:127
QSize & operator/=(qreal c)
Definition: qsize.h:174
QSize boundedTo(const QSize &) const
Definition: qsize.h:192
int width() const
Definition: qsize.h:126
GLenum GLsizei GLsizei height
Definition: GLee.h:883
QSizeF & operator+=(const QSizeF &)
Definition: qsize.h:305
bool isEmpty() const
Definition: qsize.h:120
QSize & operator*=(qreal c)
Definition: qsize.h:153
Definition: qsize.h:202
const QSize operator-(const QSize &s1, const QSize &s2)
Definition: qsize.h:165
qreal height() const
Definition: qsize.h:287
Q_DECLARE_TYPEINFO(QSize, Q_MOVABLE_TYPE)
bool isValid() const
Definition: qsize.h:123
QSize()
Definition: qsize.h:111
const GLubyte * c
Definition: GLee.h:5419
int int int int int int h
Definition: GLee.h:10534
bool operator==(const QSize &s1, const QSize &s2)
Definition: qsize.h:156
Definition: qsize.h:53
AspectRatioMode
Definition: qnamespace.h:1317
Q_CORE_EXPORT QDataStream & operator>>(QDataStream &, QSize &)
QSize toSize() const
Definition: qsize.h:355
void setHeight(int h)
Definition: qsize.h:135
qreal width() const
Definition: qsize.h:284
bool qFuzzyCompare(const QMatrix &m1, const QMatrix &m2)
Definition: qmatrix.h:172
bool isValid() const
Definition: qsize.h:281
QSizeF()
Definition: qsize.h:264
void scale(int w, int h, Qt::AspectRatioMode mode)
Definition: qsize.h:138
QSize expandedTo(const QSize &) const
Definition: qsize.h:187
void setWidth(int w)
Definition: qsize.h:132
GLubyte GLubyte GLubyte GLubyte w
Definition: GLee.h:1775
const QSize operator+(const QSize &s1, const QSize &s2)
Definition: qsize.h:162
const QSize operator*(const QSize &s, qreal c)
Definition: qsize.h:168
GLenum GLenum GLenum GLenum GLenum scale
Definition: GLee.h:5777
GLdouble s
Definition: GLee.h:1173
#define QT_END_HEADER
Definition: qglobal.h:142
bool isNull() const
Definition: qsize.h:117
Q_CORE_EXPORT QDataStream & operator<<(QDataStream &, const QSize &)
void scale(qreal w, qreal h, Qt::AspectRatioMode mode)
Definition: qsize.h:296
int & rwidth()
Definition: qsize.h:141
GLsizei GLboolean transpose
Definition: GLee.h:1750