~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to ksplash/ksplashx/qsize.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.
 
10
**
 
11
** The original Qt license header follows:
 
12
** 
 
13
**
 
14
** Implementation of QSize class
 
15
**
 
16
** Created : 931028
 
17
**
 
18
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
 
19
**
 
20
** This file is part of the kernel module of the Qt GUI Toolkit.
 
21
**
 
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.
 
25
**
 
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.
 
30
**
 
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.
 
34
**
 
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.
 
37
**
 
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.
 
42
**
 
43
** Contact info@trolltech.com if any conditions of this licensing are
 
44
** not clear to you.
 
45
**
 
46
**********************************************************************/
 
47
 
 
48
#include "qsize.h"
 
49
#include "qdatastream.h"
 
50
 
 
51
 
 
52
/*!
 
53
  \class QSize
 
54
  \brief The QSize class defines the size of a two-dimensional object.
 
55
 
 
56
  \ingroup images
 
57
  \ingroup graphics
 
58
 
 
59
  A size is specified by a width and a height.
 
60
 
 
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).
 
64
 
 
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().
 
71
 
 
72
 
 
73
  \sa QPoint, QRect
 
74
*/
 
75
 
 
76
 
 
77
/*****************************************************************************
 
78
  QSize member functions
 
79
 *****************************************************************************/
 
80
 
 
81
/*!
 
82
  \fn QSize::QSize()
 
83
  Constructs a size with invalid (negative) width and height.
 
84
*/
 
85
 
 
86
/*!
 
87
  \fn QSize::QSize( int w, int h )
 
88
  Constructs a size with width \a w and height \a h.
 
89
*/
 
90
 
 
91
/*!
 
92
  \fn bool QSize::isNull() const
 
93
  Returns TRUE if the width is 0 and the height is 0; otherwise
 
94
  returns FALSE.
 
95
*/
 
96
 
 
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.
 
101
*/
 
102
 
 
103
/*!
 
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.
 
107
*/
 
108
 
 
109
/*!
 
110
  \fn int QSize::width() const
 
111
  Returns the width.
 
112
  \sa height()
 
113
*/
 
114
 
 
115
/*!
 
116
  \fn int QSize::height() const
 
117
  Returns the height.
 
118
  \sa width()
 
119
*/
 
120
 
 
121
/*!
 
122
  \fn void QSize::setWidth( int w )
 
123
  Sets the width to \a w.
 
124
  \sa width(), setHeight()
 
125
*/
 
126
 
 
127
/*!
 
128
  \fn void QSize::setHeight( int h )
 
129
  Sets the height to \a h.
 
130
  \sa height(), setWidth()
 
131
*/
 
132
 
 
133
/*!
 
134
  Swaps the values of width and height.
 
135
*/
 
136
 
 
137
void QSize::transpose()
 
138
{
 
139
    QCOORD tmp = wd;
 
140
    wd = ht;
 
141
    ht = tmp;
 
142
}
 
143
 
 
144
/*! \enum QSize::ScaleMode
 
145
 
 
146
    This enum type defines the different ways of scaling a size.
 
147
 
 
148
    \img scaling.png
 
149
 
 
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.
 
155
 
 
156
    \sa QSize::scale(), QImage::scale(), QImage::smoothScale()
 
157
*/
 
158
 
 
159
/*!
 
160
    Scales the size to a rectangle of width \a w and height \a h according
 
161
    to the ScaleMode \a mode.
 
162
 
 
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
 
170
 
 
171
    Example:
 
172
    \code
 
173
    QSize t1( 10, 12 );
 
174
    t1.scale( 60, 60, QSize::ScaleFree );
 
175
    // t1 is (60, 60)
 
176
 
 
177
    QSize t2( 10, 12 );
 
178
    t2.scale( 60, 60, QSize::ScaleMin );
 
179
    // t2 is (50, 60)
 
180
 
 
181
    QSize t3( 10, 12 );
 
182
    t3.scale( 60, 60, QSize::ScaleMax );
 
183
    // t3 is (60, 72)
 
184
    \endcode
 
185
*/
 
186
void QSize::scale( int w, int h, ScaleMode mode )
 
187
{
 
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;
 
196
 
 
197
        if ( mode == ScaleMin ) {
 
198
            useHeight = ( rw <= w );
 
199
        } else { // mode == ScaleMax
 
200
            useHeight = ( rw >= w );
 
201
        }
 
202
 
 
203
        if ( useHeight ) {
 
204
            wd = (QCOORD)rw;
 
205
            ht = (QCOORD)h;
 
206
        } else {
 
207
            wd = (QCOORD)w;
 
208
            ht = (QCOORD)( w * h0 / w0 );
 
209
        }
 
210
    }
 
211
}
 
212
 
 
213
/*!
 
214
    \overload
 
215
 
 
216
    Equivalent to scale(\a{s}.width(), \a{s}.height(), \a mode).
 
217
*/
 
218
void QSize::scale( const QSize &s, ScaleMode mode )
 
219
{
 
220
    scale( s.width(), s.height(), mode );
 
221
}
 
222
 
 
223
/*!
 
224
  \fn QCOORD &QSize::rwidth()
 
225
  Returns a reference to the width.
 
226
 
 
227
  Using a reference makes it possible to directly manipulate the width.
 
228
 
 
229
  Example:
 
230
  \code
 
231
    QSize s( 100, 10 );
 
232
    s.rwidth() += 20;           // s becomes (120,10)
 
233
  \endcode
 
234
 
 
235
  \sa rheight()
 
236
*/
 
237
 
 
238
/*!
 
239
  \fn QCOORD &QSize::rheight()
 
240
  Returns a reference to the height.
 
241
 
 
242
  Using a reference makes it possible to directly manipulate the height.
 
243
 
 
244
  Example:
 
245
  \code
 
246
    QSize s( 100, 10 );
 
247
    s.rheight() += 5;           // s becomes (100,15)
 
248
  \endcode
 
249
 
 
250
  \sa rwidth()
 
251
*/
 
252
 
 
253
/*!
 
254
  \fn QSize &QSize::operator+=( const QSize &s )
 
255
 
 
256
  Adds \a s to the size and returns a reference to this size.
 
257
 
 
258
  Example:
 
259
  \code
 
260
    QSize s(  3, 7 );
 
261
    QSize r( -1, 4 );
 
262
    s += r;                     // s becomes (2,11)
 
263
\endcode
 
264
*/
 
265
 
 
266
/*!
 
267
  \fn QSize &QSize::operator-=( const QSize &s )
 
268
 
 
269
  Subtracts \a s from the size and returns a reference to this size.
 
270
 
 
271
  Example:
 
272
  \code
 
273
    QSize s(  3, 7 );
 
274
    QSize r( -1, 4 );
 
275
    s -= r;                     // s becomes (4,3)
 
276
  \endcode
 
277
*/
 
278
 
 
279
/*!
 
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.
 
283
*/
 
284
 
 
285
/*!
 
286
  \overload QSize &QSize::operator*=( double c )
 
287
 
 
288
  Multiplies both the width and height by \a c and returns a reference to
 
289
  the size.
 
290
 
 
291
  Note that the result is truncated.
 
292
*/
 
293
 
 
294
/*!
 
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.
 
298
*/
 
299
 
 
300
/*!
 
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.
 
304
*/
 
305
 
 
306
/*!
 
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.
 
310
*/
 
311
 
 
312
/*!
 
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.
 
317
*/
 
318
 
 
319
/*!
 
320
  \fn const QSize operator*( const QSize &s, int c )
 
321
  \relates QSize
 
322
  Multiplies \a s by \a c and returns the result.
 
323
*/
 
324
 
 
325
/*!
 
326
  \overload const QSize operator*( int c, const QSize &s )
 
327
  \relates QSize
 
328
  Multiplies \a s by \a c and returns the result.
 
329
*/
 
330
 
 
331
/*!
 
332
  \overload const QSize operator*( const QSize &s, double c )
 
333
  \relates QSize
 
334
  Multiplies \a s by \a c and returns the result.
 
335
*/
 
336
 
 
337
/*!
 
338
  \overload const QSize operator*( double c, const QSize &s )
 
339
  \relates QSize
 
340
  Multiplies \a s by \a c and returns the result.
 
341
*/
 
342
 
 
343
/*!
 
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.
 
347
*/
 
348
 
 
349
/*!
 
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.
 
354
 
 
355
  Note that the result is truncated.
 
356
*/
 
357
 
 
358
/*!
 
359
  \fn const QSize operator/( const QSize &s, int c )
 
360
  \relates QSize
 
361
  Divides \a s by \a c and returns the result.
 
362
*/
 
363
 
 
364
/*!
 
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.
 
369
 
 
370
  Note that the result is truncated.
 
371
*/
 
372
 
 
373
/*!
 
374
  \fn QSize QSize::expandedTo( const QSize & otherSize ) const
 
375
 
 
376
  Returns a size with the maximum width and height of this size and
 
377
  \a otherSize.
 
378
*/
 
379
 
 
380
/*!
 
381
  \fn QSize QSize::boundedTo( const QSize & otherSize ) const
 
382
 
 
383
  Returns a size with the minimum width and height of this size and
 
384
  \a otherSize.
 
385
*/
 
386
 
 
387
 
 
388
void QSize::warningDivByZero()
 
389
{
 
390
#if defined(QT_CHECK_MATH)
 
391
    qWarning( "QSize: Division by zero error" );
 
392
#endif
 
393
}
 
394
 
 
395
 
 
396
/*****************************************************************************
 
397
  QSize stream functions
 
398
 *****************************************************************************/
 
399
#ifndef QT_NO_DATASTREAM
 
400
/*!
 
401
  \relates QSize
 
402
  Writes the size \a sz to the stream \a s and returns a reference to
 
403
  the stream.
 
404
 
 
405
  \sa \link datastreamformat.html Format of the QDataStream operators \endlink
 
406
*/
 
407
 
 
408
QDataStream &operator<<( QDataStream &s, const QSize &sz )
 
409
{
 
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;
 
415
}
 
416
 
 
417
/*!
 
418
  \relates QSize
 
419
  Reads the size from the stream \a s into size \a sz and returns a
 
420
  reference to the stream.
 
421
 
 
422
  \sa \link datastreamformat.html Format of the QDataStream operators \endlink
 
423
*/
 
424
 
 
425
QDataStream &operator>>( QDataStream &s, QSize &sz )
 
426
{
 
427
    if ( s.version() == 1 ) {
 
428
        Q_INT16 w, h;
 
429
        s >> w;  sz.rwidth() = w;
 
430
        s >> h;  sz.rheight() = h;
 
431
    }
 
432
    else {
 
433
        Q_INT32 w, h;
 
434
        s >> w;  sz.rwidth() = w;
 
435
        s >> h;  sz.rheight() = h;
 
436
    }
 
437
    return s;
 
438
}
 
439
#endif // QT_NO_DATASTREAM