1 /****************************************************************************
2 ** $Id: fsize.cpp 20691 2016-01-17 14:41:01Z craig $
3 **
4 ** Implementation of FSize 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 "fsize.h"
39 //#include "qdatastream.h"
40 
41 
42 /*!
43   \class FSize
44   \brief The FSize 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 qreal (defined in \c <qwindowdefs.h> as \c qreal).
52   The minimum value of qreal is qreal_MIN (-2147483648) and the maximum
53   value is qreal_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   FSize member functions
69  *****************************************************************************/
70 
71 /*!
72   \fn FSize::FSize()
73   Constructs a size with invalid (negative) width and height.
74 */
75 
76 /*!
77   \fn FSize::FSize( qreal w, qreal h )
78   Constructs a size with width \a w and height \a h.
79 */
80 
81 /*!
82   \fn bool FSize::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 FSize::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 FSize::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 qreal FSize::width() const
101   Returns the width.
102   \sa height()
103 */
104 
105 /*!
106   \fn qreal FSize::height() const
107   Returns the height.
108   \sa width()
109 */
110 
111 /*!
112   \fn void FSize::setWidth( qreal w )
113   Sets the width to \a w.
114   \sa width(), setHeight()
115 */
116 
117 /*!
118   \fn void FSize::setHeight( qreal 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 
transpose()127 void FSize::transpose()
128 {
129     qreal tmp = m_wd;
130     m_wd = m_ht;
131     m_ht = tmp;
132 }
133 
134 /*! \enum FSize::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 FSize::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     FSize t1( 10, 12 );
164     t1.scale( 60, 60, FSize::ScaleFree );
165     // t1 is (60, 60)
166 
167     FSize t2( 10, 12 );
168     t2.scale( 60, 60, FSize::ScaleMin );
169     // t2 is (50, 60)
170 
171     FSize t3( 10, 12 );
172     t3.scale( 60, 60, FSize::ScaleMax );
173     // t3 is (60, 72)
174     \endcode
175 */
scale(qreal w,qreal h,Qt::AspectRatioMode mode)176 void FSize::scale( qreal w, qreal h, Qt::AspectRatioMode mode )
177 {
178     if ( mode == Qt::IgnoreAspectRatio ) {
179 	m_wd = (qreal)w;
180 	m_ht = (qreal)h;
181     } else {
182 	bool useHeight = true;
183 	qreal w0 = width();
184 	qreal h0 = height();
185 	qreal rw = h * w0 / h0;
186 
187 	if ( mode == Qt::KeepAspectRatio ) {
188 	    useHeight = ( rw <= w );
189 	} else { // mode == ScaleMax
190 	    useHeight = ( rw >= w );
191 	}
192 
193 	if ( useHeight ) {
194 	    m_wd = (qreal)rw;
195 	    m_ht = (qreal)h;
196 	} else {
197 	    m_wd = (qreal)w;
198 	    m_ht = (qreal)( w * h0 / w0 );
199 	}
200     }
201 }
202 
203 /*!
204     \overload
205 
206     Equivalent to scale(\a{s}.width(), \a{s}.height(), \a mode).
207 */
scale(const FSize & s,Qt::AspectRatioMode mode)208 void FSize::scale( const FSize &s, Qt::AspectRatioMode mode )
209 {
210     scale( s.width(), s.height(), mode );
211 }
212 
213 /*!
214   \fn qreal &FSize::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     FSize s( 100, 10 );
222     s.rwidth() += 20;		// s becomes (120,10)
223   \endcode
224 
225   \sa rheight()
226 */
227 
228 /*!
229   \fn qreal &FSize::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     FSize s( 100, 10 );
237     s.rheight() += 5;		// s becomes (100,15)
238   \endcode
239 
240   \sa rwidth()
241 */
242 
243 /*!
244   \fn FSize &FSize::operator+=( const FSize &s )
245 
246   Adds \a s to the size and returns a reference to this size.
247 
248   Example:
249   \code
250     FSize s(  3, 7 );
251     FSize r( -1, 4 );
252     s += r;			// s becomes (2,11)
253 \endcode
254 */
255 
256 /*!
257   \fn FSize &FSize::operator-=( const FSize &s )
258 
259   Subtracts \a s from the size and returns a reference to this size.
260 
261   Example:
262   \code
263     FSize s(  3, 7 );
264     FSize r( -1, 4 );
265     s -= r;			// s becomes (4,3)
266   \endcode
267 */
268 
269 /*!
270   \fn FSize &FSize::operator*=( qreal c )
271   Multiplies both the width and height by \a c and returns a reference to
272   the size.
273 */
274 
275 /*!
276   \overload FSize &FSize::operator*=( qreal 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 FSize &s1, const FSize &s2 )
286   \relates FSize
287   Returns TRUE if \a s1 and \a s2 are equal; otherwise returns FALSE.
288 */
289 
290 /*!
291   \fn bool operator!=( const FSize &s1, const FSize &s2 )
292   \relates FSize
293   Returns TRUE if \a s1 and \a s2 are different; otherwise returns FALSE.
294 */
295 
296 /*!
297   \fn const FSize operator+( const FSize &s1, const FSize &s2 )
298   \relates FSize
299   Returns the sum of \a s1 and \a s2; each component is added separately.
300 */
301 
302 /*!
303   \fn const FSize operator-( const FSize &s1, const FSize &s2 )
304   \relates FSize
305   Returns \a s2 subtracted from \a s1; each component is
306   subtracted separately.
307 */
308 
309 /*!
310   \fn const FSize operator*( const FSize &s, qreal c )
311   \relates FSize
312   Multiplies \a s by \a c and returns the result.
313 */
314 
315 /*!
316   \overload const FSize operator*( qreal c, const FSize &s )
317   \relates FSize
318   Multiplies \a s by \a c and returns the result.
319 */
320 
321 /*!
322   \overload const FSize operator*( const FSize &s, qreal c )
323   \relates FSize
324   Multiplies \a s by \a c and returns the result.
325 */
326 
327 /*!
328   \overload const FSize operator*( qreal c, const FSize &s )
329   \relates FSize
330   Multiplies \a s by \a c and returns the result.
331 */
332 
333 /*!
334   \fn FSize &FSize::operator/=( qreal c )
335   Divides both the width and height by \a c and returns a reference to the
336   size.
337 */
338 
339 /*!
340   \fn FSize &FSize::operator/=( qreal 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 FSize operator/( const FSize &s, qreal c )
350   \relates FSize
351   Divides \a s by \a c and returns the result.
352 */
353 
354 /*!
355   \fn const FSize operator/( const FSize &s, qreal c )
356   \relates FSize
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 FSize FSize::expandedTo( const FSize & 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 FSize FSize::boundedTo( const FSize & otherSize ) const
372 
373   Returns a size with the minimum width and height of this size and
374   \a otherSize.
375 */
376 
377 
warningDivByZero()378 void FSize::warningDivByZero()
379 {
380 #if defined(QT_CHECK_MATH)
381     qWarning( "FSize: Division by zero error" );
382 #endif
383 }
384 
385 
386 /*****************************************************************************
387   FSize stream functions
388  *****************************************************************************/
389 // #ifndef QT_NO_DATASTREAM
390 // /*!
391 //   \relates FSize
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 FSize &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 FSize
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, FSize &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
430