1 | /****************************************************************************
|
---|
2 | ** $Id: qsize.cpp 2 2005-11-16 15:49:26Z dmik $
|
---|
3 | **
|
---|
4 | ** Implementation of QSize class
|
---|
5 | **
|
---|
6 | ** Created : 931028
|
---|
7 | **
|
---|
8 | ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
|
---|
9 | **
|
---|
10 | ** This file is part of the kernel module of the Qt GUI Toolkit.
|
---|
11 | **
|
---|
12 | ** This file may be distributed under the terms of the Q Public License
|
---|
13 | ** as defined by Trolltech AS of Norway and appearing in the file
|
---|
14 | ** LICENSE.QPL included in the packaging of this file.
|
---|
15 | **
|
---|
16 | ** This file may be distributed and/or modified under the terms of the
|
---|
17 | ** GNU General Public License version 2 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
19 | ** packaging of this file.
|
---|
20 | **
|
---|
21 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
|
---|
22 | ** licenses may use this file in accordance with the Qt Commercial License
|
---|
23 | ** Agreement provided with the Software.
|
---|
24 | **
|
---|
25 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
---|
26 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
---|
27 | **
|
---|
28 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
---|
29 | ** information about Qt Commercial License Agreements.
|
---|
30 | ** See http://www.trolltech.com/qpl/ for QPL licensing information.
|
---|
31 | ** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
---|
32 | **
|
---|
33 | ** Contact info@trolltech.com if any conditions of this licensing are
|
---|
34 | ** not clear to you.
|
---|
35 | **
|
---|
36 | **********************************************************************/
|
---|
37 |
|
---|
38 | #include "qsize.h"
|
---|
39 | #include "qdatastream.h"
|
---|
40 |
|
---|
41 |
|
---|
42 | /*!
|
---|
43 | \class QSize
|
---|
44 | \brief The QSize class defines the size of a two-dimensional object.
|
---|
45 |
|
---|
46 | \ingroup images
|
---|
47 | \ingroup graphics
|
---|
48 |
|
---|
49 | A size is specified by a width and a height.
|
---|
50 |
|
---|
51 | The coordinate type is QCOORD (defined in \c <qwindowdefs.h> as \c int).
|
---|
52 | The minimum value of QCOORD is QCOORD_MIN (-2147483648) and the maximum
|
---|
53 | value is QCOORD_MAX (2147483647).
|
---|
54 |
|
---|
55 | The size can be set in the constructor and changed with setWidth()
|
---|
56 | and setHeight(), or using operator+=(), operator-=(), operator*=()
|
---|
57 | and operator/=(), etc. You can swap the width and height with
|
---|
58 | transpose(). You can get a size which holds the maximum height and
|
---|
59 | width of two sizes using expandedTo(), and the minimum height and
|
---|
60 | width of two sizes using boundedTo().
|
---|
61 |
|
---|
62 |
|
---|
63 | \sa QPoint, QRect
|
---|
64 | */
|
---|
65 |
|
---|
66 |
|
---|
67 | /*****************************************************************************
|
---|
68 | QSize member functions
|
---|
69 | *****************************************************************************/
|
---|
70 |
|
---|
71 | /*!
|
---|
72 | \fn QSize::QSize()
|
---|
73 | Constructs a size with invalid (negative) width and height.
|
---|
74 | */
|
---|
75 |
|
---|
76 | /*!
|
---|
77 | \fn QSize::QSize( int w, int h )
|
---|
78 | Constructs a size with width \a w and height \a h.
|
---|
79 | */
|
---|
80 |
|
---|
81 | /*!
|
---|
82 | \fn bool QSize::isNull() const
|
---|
83 | Returns TRUE if the width is 0 and the height is 0; otherwise
|
---|
84 | returns FALSE.
|
---|
85 | */
|
---|
86 |
|
---|
87 | /*!
|
---|
88 | \fn bool QSize::isEmpty() const
|
---|
89 | Returns TRUE if the width is less than or equal to 0, or the height is
|
---|
90 | less than or equal to 0; otherwise returns FALSE.
|
---|
91 | */
|
---|
92 |
|
---|
93 | /*!
|
---|
94 | \fn bool QSize::isValid() const
|
---|
95 | Returns TRUE if the width is equal to or greater than 0 and the height is
|
---|
96 | equal to or greater than 0; otherwise returns FALSE.
|
---|
97 | */
|
---|
98 |
|
---|
99 | /*!
|
---|
100 | \fn int QSize::width() const
|
---|
101 | Returns the width.
|
---|
102 | \sa height()
|
---|
103 | */
|
---|
104 |
|
---|
105 | /*!
|
---|
106 | \fn int QSize::height() const
|
---|
107 | Returns the height.
|
---|
108 | \sa width()
|
---|
109 | */
|
---|
110 |
|
---|
111 | /*!
|
---|
112 | \fn void QSize::setWidth( int w )
|
---|
113 | Sets the width to \a w.
|
---|
114 | \sa width(), setHeight()
|
---|
115 | */
|
---|
116 |
|
---|
117 | /*!
|
---|
118 | \fn void QSize::setHeight( int h )
|
---|
119 | Sets the height to \a h.
|
---|
120 | \sa height(), setWidth()
|
---|
121 | */
|
---|
122 |
|
---|
123 | /*!
|
---|
124 | Swaps the values of width and height.
|
---|
125 | */
|
---|
126 |
|
---|
127 | void QSize::transpose()
|
---|
128 | {
|
---|
129 | QCOORD tmp = wd;
|
---|
130 | wd = ht;
|
---|
131 | ht = tmp;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /*! \enum QSize::ScaleMode
|
---|
135 |
|
---|
136 | This enum type defines the different ways of scaling a size.
|
---|
137 |
|
---|
138 | \img scaling.png
|
---|
139 |
|
---|
140 | \value ScaleFree The size is scaled freely. The ratio is not preserved.
|
---|
141 | \value ScaleMin The size is scaled to a rectangle as large as possible
|
---|
142 | inside a given rectangle, preserving the aspect ratio.
|
---|
143 | \value ScaleMax The size is scaled to a rectangle as small as possible
|
---|
144 | outside a given rectangle, preserving the aspect ratio.
|
---|
145 |
|
---|
146 | \sa QSize::scale(), QImage::scale(), QImage::smoothScale()
|
---|
147 | */
|
---|
148 |
|
---|
149 | /*!
|
---|
150 | Scales the size to a rectangle of width \a w and height \a h according
|
---|
151 | to the ScaleMode \a mode.
|
---|
152 |
|
---|
153 | \list
|
---|
154 | \i If \a mode is \c ScaleFree, the size is set to (\a w, \a h).
|
---|
155 | \i If \a mode is \c ScaleMin, the current size is scaled to a rectangle
|
---|
156 | as large as possible inside (\a w, \a h), preserving the aspect ratio.
|
---|
157 | \i If \a mode is \c ScaleMax, the current size is scaled to a rectangle
|
---|
158 | as small as possible outside (\a w, \a h), preserving the aspect ratio.
|
---|
159 | \endlist
|
---|
160 |
|
---|
161 | Example:
|
---|
162 | \code
|
---|
163 | QSize t1( 10, 12 );
|
---|
164 | t1.scale( 60, 60, QSize::ScaleFree );
|
---|
165 | // t1 is (60, 60)
|
---|
166 |
|
---|
167 | QSize t2( 10, 12 );
|
---|
168 | t2.scale( 60, 60, QSize::ScaleMin );
|
---|
169 | // t2 is (50, 60)
|
---|
170 |
|
---|
171 | QSize t3( 10, 12 );
|
---|
172 | t3.scale( 60, 60, QSize::ScaleMax );
|
---|
173 | // t3 is (60, 72)
|
---|
174 | \endcode
|
---|
175 | */
|
---|
176 | void QSize::scale( int w, int h, ScaleMode mode )
|
---|
177 | {
|
---|
178 | if ( mode == ScaleFree ) {
|
---|
179 | wd = (QCOORD)w;
|
---|
180 | ht = (QCOORD)h;
|
---|
181 | } else {
|
---|
182 | bool useHeight = TRUE;
|
---|
183 | int w0 = width();
|
---|
184 | int h0 = height();
|
---|
185 | int rw = h * w0 / h0;
|
---|
186 |
|
---|
187 | if ( mode == ScaleMin ) {
|
---|
188 | useHeight = ( rw <= w );
|
---|
189 | } else { // mode == ScaleMax
|
---|
190 | useHeight = ( rw >= w );
|
---|
191 | }
|
---|
192 |
|
---|
193 | if ( useHeight ) {
|
---|
194 | wd = (QCOORD)rw;
|
---|
195 | ht = (QCOORD)h;
|
---|
196 | } else {
|
---|
197 | wd = (QCOORD)w;
|
---|
198 | ht = (QCOORD)( w * h0 / w0 );
|
---|
199 | }
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | /*!
|
---|
204 | \overload
|
---|
205 |
|
---|
206 | Equivalent to scale(\a{s}.width(), \a{s}.height(), \a mode).
|
---|
207 | */
|
---|
208 | void QSize::scale( const QSize &s, ScaleMode mode )
|
---|
209 | {
|
---|
210 | scale( s.width(), s.height(), mode );
|
---|
211 | }
|
---|
212 |
|
---|
213 | /*!
|
---|
214 | \fn QCOORD &QSize::rwidth()
|
---|
215 | Returns a reference to the width.
|
---|
216 |
|
---|
217 | Using a reference makes it possible to directly manipulate the width.
|
---|
218 |
|
---|
219 | Example:
|
---|
220 | \code
|
---|
221 | QSize s( 100, 10 );
|
---|
222 | s.rwidth() += 20; // s becomes (120,10)
|
---|
223 | \endcode
|
---|
224 |
|
---|
225 | \sa rheight()
|
---|
226 | */
|
---|
227 |
|
---|
228 | /*!
|
---|
229 | \fn QCOORD &QSize::rheight()
|
---|
230 | Returns a reference to the height.
|
---|
231 |
|
---|
232 | Using a reference makes it possible to directly manipulate the height.
|
---|
233 |
|
---|
234 | Example:
|
---|
235 | \code
|
---|
236 | QSize s( 100, 10 );
|
---|
237 | s.rheight() += 5; // s becomes (100,15)
|
---|
238 | \endcode
|
---|
239 |
|
---|
240 | \sa rwidth()
|
---|
241 | */
|
---|
242 |
|
---|
243 | /*!
|
---|
244 | \fn QSize &QSize::operator+=( const QSize &s )
|
---|
245 |
|
---|
246 | Adds \a s to the size and returns a reference to this size.
|
---|
247 |
|
---|
248 | Example:
|
---|
249 | \code
|
---|
250 | QSize s( 3, 7 );
|
---|
251 | QSize r( -1, 4 );
|
---|
252 | s += r; // s becomes (2,11)
|
---|
253 | \endcode
|
---|
254 | */
|
---|
255 |
|
---|
256 | /*!
|
---|
257 | \fn QSize &QSize::operator-=( const QSize &s )
|
---|
258 |
|
---|
259 | Subtracts \a s from the size and returns a reference to this size.
|
---|
260 |
|
---|
261 | Example:
|
---|
262 | \code
|
---|
263 | QSize s( 3, 7 );
|
---|
264 | QSize r( -1, 4 );
|
---|
265 | s -= r; // s becomes (4,3)
|
---|
266 | \endcode
|
---|
267 | */
|
---|
268 |
|
---|
269 | /*!
|
---|
270 | \fn QSize &QSize::operator*=( int c )
|
---|
271 | Multiplies both the width and height by \a c and returns a reference to
|
---|
272 | the size.
|
---|
273 | */
|
---|
274 |
|
---|
275 | /*!
|
---|
276 | \overload QSize &QSize::operator*=( double c )
|
---|
277 |
|
---|
278 | Multiplies both the width and height by \a c and returns a reference to
|
---|
279 | the size.
|
---|
280 |
|
---|
281 | Note that the result is truncated.
|
---|
282 | */
|
---|
283 |
|
---|
284 | /*!
|
---|
285 | \fn bool operator==( const QSize &s1, const QSize &s2 )
|
---|
286 | \relates QSize
|
---|
287 | Returns TRUE if \a s1 and \a s2 are equal; otherwise returns FALSE.
|
---|
288 | */
|
---|
289 |
|
---|
290 | /*!
|
---|
291 | \fn bool operator!=( const QSize &s1, const QSize &s2 )
|
---|
292 | \relates QSize
|
---|
293 | Returns TRUE if \a s1 and \a s2 are different; otherwise returns FALSE.
|
---|
294 | */
|
---|
295 |
|
---|
296 | /*!
|
---|
297 | \fn const QSize operator+( const QSize &s1, const QSize &s2 )
|
---|
298 | \relates QSize
|
---|
299 | Returns the sum of \a s1 and \a s2; each component is added separately.
|
---|
300 | */
|
---|
301 |
|
---|
302 | /*!
|
---|
303 | \fn const QSize operator-( const QSize &s1, const QSize &s2 )
|
---|
304 | \relates QSize
|
---|
305 | Returns \a s2 subtracted from \a s1; each component is
|
---|
306 | subtracted separately.
|
---|
307 | */
|
---|
308 |
|
---|
309 | /*!
|
---|
310 | \fn const QSize operator*( const QSize &s, int c )
|
---|
311 | \relates QSize
|
---|
312 | Multiplies \a s by \a c and returns the result.
|
---|
313 | */
|
---|
314 |
|
---|
315 | /*!
|
---|
316 | \overload const QSize operator*( int c, const QSize &s )
|
---|
317 | \relates QSize
|
---|
318 | Multiplies \a s by \a c and returns the result.
|
---|
319 | */
|
---|
320 |
|
---|
321 | /*!
|
---|
322 | \overload const QSize operator*( const QSize &s, double c )
|
---|
323 | \relates QSize
|
---|
324 | Multiplies \a s by \a c and returns the result.
|
---|
325 | */
|
---|
326 |
|
---|
327 | /*!
|
---|
328 | \overload const QSize operator*( double c, const QSize &s )
|
---|
329 | \relates QSize
|
---|
330 | Multiplies \a s by \a c and returns the result.
|
---|
331 | */
|
---|
332 |
|
---|
333 | /*!
|
---|
334 | \fn QSize &QSize::operator/=( int c )
|
---|
335 | Divides both the width and height by \a c and returns a reference to the
|
---|
336 | size.
|
---|
337 | */
|
---|
338 |
|
---|
339 | /*!
|
---|
340 | \fn QSize &QSize::operator/=( double c )
|
---|
341 | \overload
|
---|
342 | Divides both the width and height by \a c and returns a reference to the
|
---|
343 | size.
|
---|
344 |
|
---|
345 | Note that the result is truncated.
|
---|
346 | */
|
---|
347 |
|
---|
348 | /*!
|
---|
349 | \fn const QSize operator/( const QSize &s, int c )
|
---|
350 | \relates QSize
|
---|
351 | Divides \a s by \a c and returns the result.
|
---|
352 | */
|
---|
353 |
|
---|
354 | /*!
|
---|
355 | \fn const QSize operator/( const QSize &s, double c )
|
---|
356 | \relates QSize
|
---|
357 | \overload
|
---|
358 | Divides \a s by \a c and returns the result.
|
---|
359 |
|
---|
360 | Note that the result is truncated.
|
---|
361 | */
|
---|
362 |
|
---|
363 | /*!
|
---|
364 | \fn QSize QSize::expandedTo( const QSize & otherSize ) const
|
---|
365 |
|
---|
366 | Returns a size with the maximum width and height of this size and
|
---|
367 | \a otherSize.
|
---|
368 | */
|
---|
369 |
|
---|
370 | /*!
|
---|
371 | \fn QSize QSize::boundedTo( const QSize & otherSize ) const
|
---|
372 |
|
---|
373 | Returns a size with the minimum width and height of this size and
|
---|
374 | \a otherSize.
|
---|
375 | */
|
---|
376 |
|
---|
377 |
|
---|
378 | void QSize::warningDivByZero()
|
---|
379 | {
|
---|
380 | #if defined(QT_CHECK_MATH)
|
---|
381 | qWarning( "QSize: Division by zero error" );
|
---|
382 | #endif
|
---|
383 | }
|
---|
384 |
|
---|
385 |
|
---|
386 | /*****************************************************************************
|
---|
387 | QSize stream functions
|
---|
388 | *****************************************************************************/
|
---|
389 | #ifndef QT_NO_DATASTREAM
|
---|
390 | /*!
|
---|
391 | \relates QSize
|
---|
392 | Writes the size \a sz to the stream \a s and returns a reference to
|
---|
393 | the stream.
|
---|
394 |
|
---|
395 | \sa \link datastreamformat.html Format of the QDataStream operators \endlink
|
---|
396 | */
|
---|
397 |
|
---|
398 | QDataStream &operator<<( QDataStream &s, const QSize &sz )
|
---|
399 | {
|
---|
400 | if ( s.version() == 1 )
|
---|
401 | s << (Q_INT16)sz.width() << (Q_INT16)sz.height();
|
---|
402 | else
|
---|
403 | s << (Q_INT32)sz.width() << (Q_INT32)sz.height();
|
---|
404 | return s;
|
---|
405 | }
|
---|
406 |
|
---|
407 | /*!
|
---|
408 | \relates QSize
|
---|
409 | Reads the size from the stream \a s into size \a sz and returns a
|
---|
410 | reference to the stream.
|
---|
411 |
|
---|
412 | \sa \link datastreamformat.html Format of the QDataStream operators \endlink
|
---|
413 | */
|
---|
414 |
|
---|
415 | QDataStream &operator>>( QDataStream &s, QSize &sz )
|
---|
416 | {
|
---|
417 | if ( s.version() == 1 ) {
|
---|
418 | Q_INT16 w, h;
|
---|
419 | s >> w; sz.rwidth() = w;
|
---|
420 | s >> h; sz.rheight() = h;
|
---|
421 | }
|
---|
422 | else {
|
---|
423 | Q_INT32 w, h;
|
---|
424 | s >> w; sz.rwidth() = w;
|
---|
425 | s >> h; sz.rheight() = h;
|
---|
426 | }
|
---|
427 | return s;
|
---|
428 | }
|
---|
429 | #endif // QT_NO_DATASTREAM
|
---|