1 /****************************************************************************
3 ** This file is based on sources of the Qt GUI Toolkit, used under the terms
4 ** of the GNU General Public License version 2 (see the original copyright
6 ** All further contributions to this file are (and are required to be)
7 ** licensed under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 of the License, or
9 ** (at your option) any later version.
11 ** The original Qt license header follows:
14 ** Implementation of QSize class
18 ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
20 ** This file is part of the kernel module of the Qt GUI Toolkit.
22 ** This file may be distributed under the terms of the Q Public License
23 ** as defined by Trolltech AS of Norway and appearing in the file
24 ** LICENSE.QPL included in the packaging of this file.
26 ** This file may be distributed and/or modified under the terms of the
27 ** GNU General Public License version 2 as published by the Free Software
28 ** Foundation and appearing in the file LICENSE.GPL included in the
29 ** packaging of this file.
31 ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
32 ** licenses may use this file in accordance with the Qt Commercial License
33 ** Agreement provided with the Software.
35 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
36 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
38 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
39 ** information about Qt Commercial License Agreements.
40 ** See http://www.trolltech.com/qpl/ for QPL licensing information.
41 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
43 ** Contact info@trolltech.com if any conditions of this licensing are
46 **********************************************************************/
49 #include "qdatastream.h"
54 \brief The QSize class defines the size of a two-dimensional object.
59 A size is specified by a width and a height.
61 The coordinate type is QCOORD (defined in \c <qwindowdefs.h> as \c int).
62 The minimum value of QCOORD is QCOORD_MIN (-2147483648) and the maximum
63 value is QCOORD_MAX (2147483647).
65 The size can be set in the constructor and changed with setWidth()
66 and setHeight(), or using operator+=(), operator-=(), operator*=()
67 and operator/=(), etc. You can swap the width and height with
68 transpose(). You can get a size which holds the maximum height and
69 width of two sizes using expandedTo(), and the minimum height and
70 width of two sizes using boundedTo().
77 /*****************************************************************************
78 QSize member functions
79 *****************************************************************************/
83 Constructs a size with invalid (negative) width and height.
87 \fn QSize::QSize( int w, int h )
88 Constructs a size with width \a w and height \a h.
92 \fn bool QSize::isNull() const
93 Returns TRUE if the width is 0 and the height is 0; otherwise
98 \fn bool QSize::isEmpty() const
99 Returns TRUE if the width is less than or equal to 0, or the height is
100 less than or equal to 0; otherwise returns FALSE.
104 \fn bool QSize::isValid() const
105 Returns TRUE if the width is equal to or greater than 0 and the height is
106 equal to or greater than 0; otherwise returns FALSE.
110 \fn int QSize::width() const
116 \fn int QSize::height() const
122 \fn void QSize::setWidth( int w )
123 Sets the width to \a w.
124 \sa width(), setHeight()
128 \fn void QSize::setHeight( int h )
129 Sets the height to \a h.
130 \sa height(), setWidth()
134 Swaps the values of width and height.
137 void QSize::transpose()
144 /*! \enum QSize::ScaleMode
146 This enum type defines the different ways of scaling a size.
150 \value ScaleFree The size is scaled freely. The ratio is not preserved.
151 \value ScaleMin The size is scaled to a rectangle as large as possible
152 inside a given rectangle, preserving the aspect ratio.
153 \value ScaleMax The size is scaled to a rectangle as small as possible
154 outside a given rectangle, preserving the aspect ratio.
156 \sa QSize::scale(), QImage::scale(), QImage::smoothScale()
160 Scales the size to a rectangle of width \a w and height \a h according
161 to the ScaleMode \a mode.
164 \i If \a mode is \c ScaleFree, the size is set to (\a w, \a h).
165 \i If \a mode is \c ScaleMin, the current size is scaled to a rectangle
166 as large as possible inside (\a w, \a h), preserving the aspect ratio.
167 \i If \a mode is \c ScaleMax, the current size is scaled to a rectangle
168 as small as possible outside (\a w, \a h), preserving the aspect ratio.
174 t1.scale( 60, 60, QSize::ScaleFree );
178 t2.scale( 60, 60, QSize::ScaleMin );
182 t3.scale( 60, 60, QSize::ScaleMax );
186 void QSize::scale( int w
, int h
, ScaleMode mode
)
188 if ( mode
== ScaleFree
) {
192 bool useHeight
= true;
195 int rw
= h
* w0
/ h0
;
197 if ( mode
== ScaleMin
) {
198 useHeight
= ( rw
<= w
);
199 } else { // mode == ScaleMax
200 useHeight
= ( rw
>= w
);
208 ht
= (QCOORD
)( w
* h0
/ w0
);
216 Equivalent to scale(\a{s}.width(), \a{s}.height(), \a mode).
218 void QSize::scale( const QSize
&s
, ScaleMode mode
)
220 scale( s
.width(), s
.height(), mode
);
224 \fn QCOORD &QSize::rwidth()
225 Returns a reference to the width.
227 Using a reference makes it possible to directly manipulate the width.
232 s.rwidth() += 20; // s becomes (120,10)
239 \fn QCOORD &QSize::rheight()
240 Returns a reference to the height.
242 Using a reference makes it possible to directly manipulate the height.
247 s.rheight() += 5; // s becomes (100,15)
254 \fn QSize &QSize::operator+=( const QSize &s )
256 Adds \a s to the size and returns a reference to this size.
262 s += r; // s becomes (2,11)
267 \fn QSize &QSize::operator-=( const QSize &s )
269 Subtracts \a s from the size and returns a reference to this size.
275 s -= r; // s becomes (4,3)
280 \fn QSize &QSize::operator*=( int c )
281 Multiplies both the width and height by \a c and returns a reference to
286 \overload QSize &QSize::operator*=( double c )
288 Multiplies both the width and height by \a c and returns a reference to
291 Note that the result is truncated.
295 \fn bool operator==( const QSize &s1, const QSize &s2 )
297 Returns TRUE if \a s1 and \a s2 are equal; otherwise returns FALSE.
301 \fn bool operator!=( const QSize &s1, const QSize &s2 )
303 Returns TRUE if \a s1 and \a s2 are different; otherwise returns FALSE.
307 \fn const QSize operator+( const QSize &s1, const QSize &s2 )
309 Returns the sum of \a s1 and \a s2; each component is added separately.
313 \fn const QSize operator-( const QSize &s1, const QSize &s2 )
315 Returns \a s2 subtracted from \a s1; each component is
316 subtracted separately.
320 \fn const QSize operator*( const QSize &s, int c )
322 Multiplies \a s by \a c and returns the result.
326 \overload const QSize operator*( int c, const QSize &s )
328 Multiplies \a s by \a c and returns the result.
332 \overload const QSize operator*( const QSize &s, double c )
334 Multiplies \a s by \a c and returns the result.
338 \overload const QSize operator*( double c, const QSize &s )
340 Multiplies \a s by \a c and returns the result.
344 \fn QSize &QSize::operator/=( int c )
345 Divides both the width and height by \a c and returns a reference to the
350 \fn QSize &QSize::operator/=( double c )
352 Divides both the width and height by \a c and returns a reference to the
355 Note that the result is truncated.
359 \fn const QSize operator/( const QSize &s, int c )
361 Divides \a s by \a c and returns the result.
365 \fn const QSize operator/( const QSize &s, double c )
368 Divides \a s by \a c and returns the result.
370 Note that the result is truncated.
374 \fn QSize QSize::expandedTo( const QSize & otherSize ) const
376 Returns a size with the maximum width and height of this size and
381 \fn QSize QSize::boundedTo( const QSize & otherSize ) const
383 Returns a size with the minimum width and height of this size and
388 void QSize::warningDivByZero()
390 #if defined(QT_CHECK_MATH)
391 qWarning( "QSize: Division by zero error" );
396 /*****************************************************************************
397 QSize stream functions
398 *****************************************************************************/
399 #ifndef QT_NO_DATASTREAM
402 Writes the size \a sz to the stream \a s and returns a reference to
405 \sa \link datastreamformat.html Format of the QDataStream operators \endlink
408 QDataStream
&operator<<( QDataStream
&s
, const QSize
&sz
)
410 if ( s
.version() == 1 )
411 s
<< (Q_INT16
)sz
.width() << (Q_INT16
)sz
.height();
413 s
<< (Q_INT32
)sz
.width() << (Q_INT32
)sz
.height();
419 Reads the size from the stream \a s into size \a sz and returns a
420 reference to the stream.
422 \sa \link datastreamformat.html Format of the QDataStream operators \endlink
425 QDataStream
&operator>>( QDataStream
&s
, QSize
&sz
)
427 if ( s
.version() == 1 ) {
429 s
>> w
; sz
.rwidth() = w
;
430 s
>> h
; sz
.rheight() = h
;
434 s
>> w
; sz
.rwidth() = w
;
435 s
>> h
; sz
.rheight() = h
;
439 #endif // QT_NO_DATASTREAM