Qt 4.8
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 
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 };
96 
97 /*****************************************************************************
98  QSize stream functions
99  *****************************************************************************/
100 
101 #ifndef QT_NO_DATASTREAM
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 
147 inline QSize &QSize::operator+=(const QSize &s)
148 { wd+=s.wd; ht+=s.ht; return *this; }
149 
150 inline QSize &QSize::operator-=(const QSize &s)
151 { wd-=s.wd; ht-=s.ht; return *this; }
152 
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 
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
199 #endif
200 
201 
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 &);
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:
246 };
248 
249 
250 /*****************************************************************************
251  QSizeF stream functions
252  *****************************************************************************/
253 
254 #ifndef QT_NO_DATASTREAM
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 
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 
297 { scale(QSizeF(w, h), mode); }
298 
300 { return wd; }
301 
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
362 #endif
363 
365 
367 
368 #endif // QSIZE_H
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:62
bool isEmpty() const
Returns true if either of the width and height is less than or equal to 0; otherwise returns false...
Definition: qsize.h:278
QSize & operator*=(qreal c)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qsize.h:153
QSizeF()
Constructs an invalid size.
Definition: qsize.h:264
bool isNull() const
Returns true if both the width and height are +0.
Definition: qsize.h:275
double qreal
Definition: qglobal.h:1193
unsigned char c[8]
Definition: qnumeric_p.h:62
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
QSize()
Constructs a size with an invalid width and height (i.
Definition: qsize.h:111
friend const QSize operator*(const QSize &, qreal)
Definition: qsize.h:168
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
friend const QSize operator+(const QSize &, const QSize &)
Returns the sum of s1 and s2; each component is added separately.
Definition: qsize.h:162
#define QT_MODULE(x)
Definition: qglobal.h:2783
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
QSize toSize() const
Returns an integer based copy of this size.
Definition: qsize.h:355
friend const QSizeF operator-(const QSizeF &, const QSizeF &)
Returns s2 subtracted from s1; each component is subtracted separately.
Definition: qsize.h:323
friend bool operator!=(const QSizeF &, const QSizeF &)
Returns true if s1 and s2 are different; otherwise returns false.
Definition: qsize.h:317
qreal width() const
Returns the width.
Definition: qsize.h:284
qreal height() const
Returns the height.
Definition: qsize.h:287
QSize expandedTo(const QSize &) const
Returns a size holding the maximum width and height of this size and the given otherSize.
Definition: qsize.h:187
void scale(qreal w, qreal h, Qt::AspectRatioMode mode)
Scales the size to a rectangle with the given width and height, according to the specified mode...
Definition: qsize.h:296
friend bool operator==(const QSize &, const QSize &)
Returns true if s1 and s2 are equal; otherwise returns false.
Definition: qsize.h:156
static Q_DECL_CONSTEXPR bool qFuzzyCompare(double p1, double p2)
Definition: qglobal.h:2030
QSizeF & operator*=(qreal c)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qsize.h:311
void setHeight(int h)
Sets the height to the given height.
Definition: qsize.h:135
static bool qIsNull(double d)
Definition: qglobal.h:2061
bool isValid() const
Returns true if both the width and height is equal to or greater than 0; otherwise returns false...
Definition: qsize.h:281
void setWidth(qreal w)
Sets the width to the given width.
Definition: qsize.h:290
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
friend const QSize operator/(const QSize &, qreal)
Divides the given size by the given divisor, and returns the result rounded to the nearest integer...
Definition: qsize.h:181
QSize boundedTo(const QSize &) const
Returns a size holding the minimum width and height of this size and the given otherSize.
Definition: qsize.h:192
The QSizeF class defines the size of a two-dimensional object using floating point precision...
Definition: qsize.h:202
const QByteArray operator+(const QByteArray &a1, const QByteArray &a2)
Returns a byte array that is the result of concatenating byte array a1 and byte array a2...
Definition: qbytearray.h:564
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
friend const QSizeF operator+(const QSizeF &, const QSizeF &)
Returns the sum of s1 and s2; each component is added separately.
Definition: qsize.h:320
void setWidth(int w)
Sets the width to the given width.
Definition: qsize.h:132
friend const QSize operator-(const QSize &, const QSize &)
Returns s2 subtracted from s1; each component is subtracted separately.
Definition: qsize.h:165
int width() const
Returns the width.
Definition: qsize.h:126
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static bool isEmpty(const char *str)
QSize & operator+=(const QSize &)
Adds the given size to this size, and returns a reference to this size.
Definition: qsize.h:147
friend const QSizeF operator*(const QSizeF &, qreal)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qsize.h:326
void transpose()
Swaps the width and height values.
Definition: qsize.cpp:196
QSizeF & operator-=(const QSizeF &)
Subtracts the given size from this size and returns a reference to this size.
Definition: qsize.h:308
qreal wd
Definition: qsize.h:244
bool operator!=(const T *o, const QPointer< T > &p)
Definition: qpointer.h:122
friend bool operator!=(const QSize &, const QSize &)
Returns true if s1 and s2 are different; otherwise returns false.
Definition: qsize.h:159
Q_DECLARE_TYPEINFO(QSize, Q_MOVABLE_TYPE)
#define Q_CORE_EXPORT
Definition: qglobal.h:1449
QSizeF & operator/=(qreal c)
Divides both the width and height by the given divisor and returns a reference to the size...
Definition: qsize.h:332
int ht
Definition: qsize.h:93
qreal ht
Definition: qsize.h:245
QSizeF boundedTo(const QSizeF &) const
Returns a size holding the minimum width and height of this size and the given otherSize.
Definition: qsize.h:350
QSizeF expandedTo(const QSizeF &) const
Returns a size holding the maximum width and height of this size and the given otherSize.
Definition: qsize.h:345
QSize & operator/=(qreal c)
Divides both the width and height by the given divisor, and returns a reference to the size...
Definition: qsize.h:174
int & rheight()
Returns a reference to the height.
Definition: qsize.h:144
int height() const
Returns the height.
Definition: qsize.h:129
QGenericMatrix< N, M, T > operator/(const QGenericMatrix< N, M, T > &matrix, T divisor)
Returns the result of dividing all elements of matrix by divisor.
QSize & operator-=(const QSize &)
Subtracts the given size from this size, and returns a reference to this size.
Definition: qsize.h:150
bool isValid() const
Returns true if both the width and height is equal to or greater than 0; otherwise returns false...
Definition: qsize.h:123
QDataStream & operator<<(QDataStream &stream, const QSizeF &size)
Writes the given size to the given stream and returns a reference to the stream.
Definition: qsize.cpp:953
bool operator==(const T *o, const QPointer< T > &p)
Definition: qpointer.h:87
QSizeF & operator+=(const QSizeF &)
Adds the given size to this size and returns a reference to this size.
Definition: qsize.h:305
void scale(int w, int h, Qt::AspectRatioMode mode)
Scales the size to a rectangle with the given width and height, according to the specified mode: ...
Definition: qsize.h:138
void setHeight(qreal h)
Sets the height to the given height.
Definition: qsize.h:293
bool isNull() const
Returns true if both the width and height is 0; otherwise returns false.
Definition: qsize.h:117
static Q_DECL_CONSTEXPR bool qFuzzyIsNull(double d)
Definition: qglobal.h:2043
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
QGenericMatrix< M1, M2, T > operator*(const QGenericMatrix< N, M2, T > &m1, const QGenericMatrix< M1, N, T > &m2)
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
QDataStream & operator<<(QDataStream &stream, const QSize &size)
Writes the given size to the given stream, and returns a reference to the stream. ...
Definition: qsize.cpp:461
QDataStream & operator>>(QDataStream &stream, QSize &size)
Reads a size from the given stream into the given size, and returns a reference to the stream...
Definition: qsize.cpp:483
bool isEmpty() const
Returns true if either of the width and height is less than or equal to 0; otherwise returns false...
Definition: qsize.h:120
int wd
Definition: qsize.h:92
timeval & operator+=(timeval &t1, const timeval &t2)
Definition: qcore_unix_p.h:120
#define class
qreal & rheight()
Returns a reference to the height.
Definition: qsize.h:302
friend const QSizeF operator/(const QSizeF &, qreal)
Divides the given size by the given divisor and returns the result.
Definition: qsize.h:339
#define QT_END_HEADER
Definition: qglobal.h:137
friend bool operator==(const QSizeF &, const QSizeF &)
Returns true if s1 and s2 are equal; otherwise returns false.
Definition: qsize.h:314
QDataStream & operator<<(QDataStream &out, const QUrl &url)
Writes url url to the stream out and returns a reference to the stream.
Definition: qurl.cpp:6757
qreal & rwidth()
Returns a reference to the width.
Definition: qsize.h:299
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203
static bool isNull(const QVariant::Private *d)
Definition: qvariant.cpp:300
int & rwidth()
Returns a reference to the width.
Definition: qsize.h:141
QDataStream & operator>>(QDataStream &in, QUrl &url)
Reads a url into url from the stream in and returns a reference to the stream.
Definition: qurl.cpp:6774
AspectRatioMode
Definition: qnamespace.h:1317
QGenericMatrix< N, M, T > operator-(const QGenericMatrix< N, M, T > &m1, const QGenericMatrix< N, M, T > &m2)
Returns the difference of m1 and m2.