Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / workspace / ksplash / ksplashx / qsize.cpp
blob350fb1beee6bfa538972d87b4cf8bdbfe6c6d934
1 /****************************************************************************
2 **
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
5 ** notice below).
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:
12 **
14 ** Implementation of QSize class
16 ** Created : 931028
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
44 ** not clear to you.
46 **********************************************************************/
48 #include "qsize.h"
49 #include "qdatastream.h"
52 /*!
53 \class QSize
54 \brief The QSize class defines the size of a two-dimensional object.
56 \ingroup images
57 \ingroup graphics
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().
73 \sa QPoint, QRect
77 /*****************************************************************************
78 QSize member functions
79 *****************************************************************************/
81 /*!
82 \fn QSize::QSize()
83 Constructs a size with invalid (negative) width and height.
86 /*!
87 \fn QSize::QSize( int w, int h )
88 Constructs a size with width \a w and height \a h.
91 /*!
92 \fn bool QSize::isNull() const
93 Returns TRUE if the width is 0 and the height is 0; otherwise
94 returns FALSE.
97 /*!
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
111 Returns the width.
112 \sa height()
116 \fn int QSize::height() const
117 Returns the height.
118 \sa width()
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()
139 QCOORD tmp = wd;
140 wd = ht;
141 ht = tmp;
144 /*! \enum QSize::ScaleMode
146 This enum type defines the different ways of scaling a size.
148 \img scaling.png
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.
163 \list
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.
169 \endlist
171 Example:
172 \code
173 QSize t1( 10, 12 );
174 t1.scale( 60, 60, QSize::ScaleFree );
175 // t1 is (60, 60)
177 QSize t2( 10, 12 );
178 t2.scale( 60, 60, QSize::ScaleMin );
179 // t2 is (50, 60)
181 QSize t3( 10, 12 );
182 t3.scale( 60, 60, QSize::ScaleMax );
183 // t3 is (60, 72)
184 \endcode
186 void QSize::scale( int w, int h, ScaleMode mode )
188 if ( mode == ScaleFree ) {
189 wd = (QCOORD)w;
190 ht = (QCOORD)h;
191 } else {
192 bool useHeight = true;
193 int w0 = width();
194 int h0 = height();
195 int rw = h * w0 / h0;
197 if ( mode == ScaleMin ) {
198 useHeight = ( rw <= w );
199 } else { // mode == ScaleMax
200 useHeight = ( rw >= w );
203 if ( useHeight ) {
204 wd = (QCOORD)rw;
205 ht = (QCOORD)h;
206 } else {
207 wd = (QCOORD)w;
208 ht = (QCOORD)( w * h0 / w0 );
214 \overload
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.
229 Example:
230 \code
231 QSize s( 100, 10 );
232 s.rwidth() += 20; // s becomes (120,10)
233 \endcode
235 \sa rheight()
239 \fn QCOORD &QSize::rheight()
240 Returns a reference to the height.
242 Using a reference makes it possible to directly manipulate the height.
244 Example:
245 \code
246 QSize s( 100, 10 );
247 s.rheight() += 5; // s becomes (100,15)
248 \endcode
250 \sa rwidth()
254 \fn QSize &QSize::operator+=( const QSize &s )
256 Adds \a s to the size and returns a reference to this size.
258 Example:
259 \code
260 QSize s( 3, 7 );
261 QSize r( -1, 4 );
262 s += r; // s becomes (2,11)
263 \endcode
267 \fn QSize &QSize::operator-=( const QSize &s )
269 Subtracts \a s from the size and returns a reference to this size.
271 Example:
272 \code
273 QSize s( 3, 7 );
274 QSize r( -1, 4 );
275 s -= r; // s becomes (4,3)
276 \endcode
280 \fn QSize &QSize::operator*=( int c )
281 Multiplies both the width and height by \a c and returns a reference to
282 the size.
286 \overload QSize &QSize::operator*=( double c )
288 Multiplies both the width and height by \a c and returns a reference to
289 the size.
291 Note that the result is truncated.
295 \fn bool operator==( const QSize &s1, const QSize &s2 )
296 \relates QSize
297 Returns TRUE if \a s1 and \a s2 are equal; otherwise returns FALSE.
301 \fn bool operator!=( const QSize &s1, const QSize &s2 )
302 \relates QSize
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 )
308 \relates QSize
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 )
314 \relates QSize
315 Returns \a s2 subtracted from \a s1; each component is
316 subtracted separately.
320 \fn const QSize operator*( const QSize &s, int c )
321 \relates QSize
322 Multiplies \a s by \a c and returns the result.
326 \overload const QSize operator*( int c, const QSize &s )
327 \relates QSize
328 Multiplies \a s by \a c and returns the result.
332 \overload const QSize operator*( const QSize &s, double c )
333 \relates QSize
334 Multiplies \a s by \a c and returns the result.
338 \overload const QSize operator*( double c, const QSize &s )
339 \relates QSize
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
346 size.
350 \fn QSize &QSize::operator/=( double c )
351 \overload
352 Divides both the width and height by \a c and returns a reference to the
353 size.
355 Note that the result is truncated.
359 \fn const QSize operator/( const QSize &s, int c )
360 \relates QSize
361 Divides \a s by \a c and returns the result.
365 \fn const QSize operator/( const QSize &s, double c )
366 \relates QSize
367 \overload
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
377 \a otherSize.
381 \fn QSize QSize::boundedTo( const QSize & otherSize ) const
383 Returns a size with the minimum width and height of this size and
384 \a otherSize.
388 void QSize::warningDivByZero()
390 #if defined(QT_CHECK_MATH)
391 qWarning( "QSize: Division by zero error" );
392 #endif
396 /*****************************************************************************
397 QSize stream functions
398 *****************************************************************************/
399 #ifndef QT_NO_DATASTREAM
401 \relates QSize
402 Writes the size \a sz to the stream \a s and returns a reference to
403 the stream.
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();
412 else
413 s << (Q_INT32)sz.width() << (Q_INT32)sz.height();
414 return s;
418 \relates QSize
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 ) {
428 Q_INT16 w, h;
429 s >> w; sz.rwidth() = w;
430 s >> h; sz.rheight() = h;
432 else {
433 Q_INT32 w, h;
434 s >> w; sz.rwidth() = w;
435 s >> h; sz.rheight() = h;
437 return s;
439 #endif // QT_NO_DATASTREAM