1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the core module of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
30
#include "qdatastream.h"
35
\brief The QSize class defines the size of a two-dimensional object.
39
A size is specified by a width and a height.
41
The size can be set in the constructor and changed with setWidth(),
42
setHeight(), or scale(), or using arithmetic operators. You can swap the
43
width and height with transpose(). You can get a size which holds the
44
maximum height and width of two sizes using expandedTo(), and the minimum
45
height and width of two sizes using boundedTo().
48
\sa QPoint, QRect QSizeF
52
/*****************************************************************************
53
QSize member functions
54
*****************************************************************************/
59
Constructs a size with an invalid width and height.
61
\sa isValid() setWidth() setHeight()
65
\fn QSize::QSize(int w, int h)
67
Constructs a size with width \a w and height \a h.
71
\fn bool QSize::isNull() const
73
Returns true if the width is 0 and the height is 0; otherwise
76
\sa isValid() isEmpty() width() height()
80
\fn bool QSize::isEmpty() const
82
Returns true if the width is less than or equal to 0, or the height is
83
less than or equal to 0; otherwise returns false.
85
\sa isNull() isValid() width() height()
89
\fn bool QSize::isValid() const
91
Returns true if the width is equal to or greater than 0 and the height is
92
equal to or greater than 0; otherwise returns false.
94
\sa isNull() isEmpty() width() height()
98
\fn int QSize::width() const
102
\sa height() setWidth()
106
\fn int QSize::height() const
110
\sa width() setHeight()
114
\fn void QSize::setWidth(int w)
116
Sets the width to \a w.
118
\sa width() rwidth() setHeight() expandedTo() boundedTo() scale() transpose()
122
\fn void QSize::setHeight(int h)
124
Sets the height to \a h.
126
\sa height() rheight() setWidth() expandedTo() boundedTo() scale() transpose()
130
Swaps the width and height values.
132
\sa expandedTo() boundedTo() setWidth() setHeight()
135
void QSize::transpose()
143
\fn void QSize::scale(int w, int h, Qt::AspectRatioMode mode)
145
Scales the size to a rectangle of width \a w and height \a h according
146
to the Qt::AspectRatioMode \a mode.
149
\i If \a mode is \c Qt::IgnoreAspectRatio, the size is set to (\a w, \a h).
150
\i If \a mode is \c Qt::KeepAspectRatio, the current size is scaled to a rectangle
151
as large as possible inside (\a w, \a h), preserving the aspect ratio.
152
\i If \a mode is \c Qt::KeepAspectRatioByExpanding, the current size is scaled to a rectangle
153
as small as possible outside (\a w, \a h), preserving the aspect ratio.
159
t1.scale(60, 60, QSize::IgnoreAspectRatio);
163
t2.scale(60, 60, QSize::KeepAspectRatio);
167
t3.scale(60, 60, QSize::KeepAspectRatioByExpanding);
171
\sa boundedTo() expandedTo() setWidth() setHeight()
177
Equivalent to scale(\a{s}.width(), \a{s}.height(), \a mode).
179
void QSize::scale(const QSize &s, Qt::AspectRatioMode mode)
181
if (mode == Qt::IgnoreAspectRatio) {
186
int rw = s.ht * wd / ht;
188
if (mode == Qt::KeepAspectRatio) {
189
useHeight = (rw <= s.wd);
190
} else { // mode == Qt::KeepAspectRatioByExpanding
191
useHeight = (rw >= s.wd);
205
\fn int &QSize::rwidth()
207
Returns a reference to the width.
209
Using a reference makes it possible to directly manipulate the width.
214
s.rwidth() += 20; // s becomes (120,10)
217
\sa rheight() setWidth()
221
\fn int &QSize::rheight()
223
Returns a reference to the height.
225
Using a reference makes it possible to directly manipulate the height.
230
s.rheight() += 5; // s becomes (100,15)
233
\sa rwidth() setHeight()
237
\fn QSize &QSize::operator+=(const QSize &s)
239
Adds \a s to the size and returns a reference to this size.
245
s += r; // s becomes (2,11)
250
\fn QSize &QSize::operator-=(const QSize &s)
252
Subtracts \a s from the size and returns a reference to this size.
258
s -= r; // s becomes (4,3)
263
\fn QSize &QSize::operator*=(qreal coeff)
267
Multiplies both the width and height by \a coeff and returns a
268
reference to the size.
270
Note that the result is rounded to the nearest integer.
274
\fn bool operator==(const QSize &s1, const QSize &s2)
278
Returns true if \a s1 and \a s2 are equal; otherwise returns false.
282
\fn bool operator!=(const QSize &s1, const QSize &s2)
286
Returns true if \a s1 and \a s2 are different; otherwise returns false.
290
\fn const QSize operator+(const QSize &s1, const QSize &s2)
294
Returns the sum of \a s1 and \a s2; each component is added separately.
298
\fn const QSize operator-(const QSize &s1, const QSize &s2)
302
Returns \a s2 subtracted from \a s1; each component is subtracted
307
\fn const QSize operator*(const QSize &size, qreal coeff)
311
Multiplies \a size by \a coeff and returns the result rounded to the nearest integer.
317
\fn const QSize operator*(qreal coeff, const QSize &size)
322
Multiplies \a size by \a coeff and returns the result rounded to the nearest integer.
328
\fn QSize &QSize::operator/=(qreal coeff)
332
Divides both the width and height by \a coeff and returns a
333
reference to the size.
335
Note that the result is rounded to the nearest integer.
341
\fn const QSize operator/(const QSize &size, qreal divisor)
346
Divides \a size by \a divisor and returns the result.
348
Note that the result is rounded to the nearest integer.
354
\fn QSize QSize::expandedTo(const QSize & otherSize) const
356
Returns a size with the maximum width and height of this size and
359
\sa boundedTo() scale() setWidth() setHeight()
363
\fn QSize QSize::boundedTo(const QSize & otherSize) const
365
Returns a size with the minimum width and height of this size and
368
\sa expandedTo() scale() setWidth() setHeight()
373
/*****************************************************************************
374
QSize stream functions
375
*****************************************************************************/
376
#ifndef QT_NO_DATASTREAM
380
Writes the size \a sz to the stream \a s and returns a reference
383
\sa \link datastreamformat.html Format of the QDataStream
387
QDataStream &operator<<(QDataStream &s, const QSize &sz)
389
if (s.version() == 1)
390
s << (qint16)sz.width() << (qint16)sz.height();
392
s << (qint32)sz.width() << (qint32)sz.height();
399
Reads the size from the stream \a s into size \a sz and returns a
400
reference to the stream.
402
\sa \link datastreamformat.html Format of the QDataStream
406
QDataStream &operator>>(QDataStream &s, QSize &sz)
408
if (s.version() == 1) {
410
s >> w; sz.rwidth() = w;
411
s >> h; sz.rheight() = h;
415
s >> w; sz.rwidth() = w;
416
s >> h; sz.rheight() = h;
420
#endif // QT_NO_DATASTREAM
422
#ifndef QT_NO_DEBUG_STREAM
423
QDebug operator<<(QDebug dbg, const QSize &s) {
424
dbg.nospace() << "QSize(" << s.width() << ',' << s.height() << ')';
433
\brief The QSizeF class defines the size of a two-dimensional object
434
using floating point values for accuracy.
438
A size is specified by a width and a height.
440
The coordinate type is qreal.
442
The size can be set in the constructor and changed with setWidth(),
443
setHeight(), or scale(), or using arithmetic operators. You can swap the
444
width and height with transpose(). You can get a size which holds the
445
maximum height and width of two sizes using expandedTo(), and the minimum
446
height and width of two sizes using boundedTo().
448
\sa QSize QPointF QRectF
452
/*****************************************************************************
453
QSizeF member functions
454
*****************************************************************************/
459
Constructs an invalid size.
461
\sa isValid() setWidth() setHeight()
465
\fn QSizeF::QSizeF(const QSize &size)
467
Constructs a size with floating point accuracy from the given \a size.
471
\fn QSizeF::QSizeF(qreal width, qreal height)
473
Constructs a size with width \a width and height \a height.
477
\fn bool QSizeF::isNull() const
479
Returns true if the width is 0 and the height is 0; otherwise
482
\sa isValid() isEmpty() width() height()
486
\fn bool QSizeF::isEmpty() const
488
Returns true if the width is less than or equal to 0, or the height is
489
less than or equal to 0; otherwise returns false.
491
\sa isNull() isValid() width() height()
495
\fn bool QSizeF::isValid() const
497
Returns true if the width is equal to or greater than 0 and the height is
498
equal to or greater than 0; otherwise returns false.
500
\sa isNull() isEmpty() width() height()
504
\fn int QSizeF::width() const
508
\sa height() setWidth()
512
\fn int QSizeF::height() const
516
\sa width() setHeight()
520
\fn void QSizeF::setWidth(qreal width)
522
Sets the width to \a width.
524
\sa width() rwidth() setHeight() expandedTo() boundedTo() scale() transpose()
528
\fn void QSizeF::setHeight(qreal height)
530
Sets the height to \a height.
532
\sa height() setWidth() expandedTo() boundedTo() scale() transpose()
536
\fn QSize QSizeF::toSize() const
538
Returns a size with integer precision. Note that the coordinates in the
539
returned size will be rounded to the nearest integer.
543
Swaps the width and height values.
545
\sa expandedTo() boundedTo() setWidth() setHeight()
548
void QSizeF::transpose()
556
\fn void QSizeF::scale(qreal w, qreal h, Qt::AspectRatioMode mode)
558
Scales the size to a rectangle of width \a w and height \a h according
559
to the Qt::AspectRatioMode \a mode.
562
\i If \a mode is \c Qt::IgnoreAspectRatio, the size is set to (\a w, \a h).
563
\i If \a mode is \c Qt::KeepAspectRatio, the current size is scaled to a rectangle
564
as large as possible inside (\a w, \a h), preserving the aspect ratio.
565
\i If \a mode is \c Qt::KeepAspectRatioByExpanding, the current size is scaled to a rectangle
566
as small as possible outside (\a w, \a h), preserving the aspect ratio.
572
t1.scale(60, 60, QSizeF::IgnoreAspectRatio);
576
t2.scale(60, 60, QSizeF::KeepAspectRatio);
580
t3.scale(60, 60, QSizeF::KeepAspectRatioByExpanding);
584
\sa boundedTo() expandedTo() setWidth() setHeight()
590
Equivalent to scale(\a{s}.width(), \a{s}.height(), \a mode).
592
void QSizeF::scale(const QSizeF &s, Qt::AspectRatioMode mode)
594
if (mode == Qt::IgnoreAspectRatio) {
599
qreal rw = s.ht * wd / ht;
601
if (mode == Qt::KeepAspectRatio) {
602
useHeight = (rw <= s.wd);
603
} else { // mode == Qt::KeepAspectRatioByExpanding
604
useHeight = (rw >= s.wd);
618
\fn int &QSizeF::rwidth()
620
Returns a reference to the width.
622
Using a reference makes it possible to directly manipulate the width.
627
s.rwidth() += 20.5; // s becomes (120.8,10)
630
\sa rheight() setWidth()
634
\fn int &QSizeF::rheight()
636
Returns a reference to the height.
638
Using a reference makes it possible to directly manipulate the height.
643
s.rheight() += 5.5; // s becomes (100,15.7)
646
\sa rwidth() setHeight()
650
\fn QSizeF &QSizeF::operator+=(const QSizeF &s)
652
Adds \a s to the size and returns a reference to this size.
658
s += r; // s becomes (2,11)
663
\fn QSizeF &QSizeF::operator-=(const QSizeF &s)
665
Subtracts \a s from the size and returns a reference to this size.
671
s -= r; // s becomes (4,3)
676
\fn QSizeF &QSizeF::operator*=(qreal coeff)
680
Multiplies both the width and height by \a coeff and returns a
681
reference to the size.
685
\fn bool operator==(const QSizeF &s1, const QSizeF &s2)
689
Returns true if \a s1 and \a s2 are equal; otherwise returns false.
693
\fn bool operator!=(const QSizeF &s1, const QSizeF &s2)
697
Returns true if \a s1 and \a s2 are different; otherwise returns false.
701
\fn const QSizeF operator+(const QSizeF &s1, const QSizeF &s2)
705
Returns the sum of \a s1 and \a s2; each component is added separately.
709
\fn const QSizeF operator-(const QSizeF &s1, const QSizeF &s2)
713
Returns \a s2 subtracted from \a s1; each component is subtracted
718
\fn const QSizeF operator*(const QSizeF &size, qreal coeff)
723
Multiplies \a size by \a coeff and returns the result.
729
\fn const QSizeF operator*(qreal c, const QSizeF &s)
734
Multiplies \a s by \a c and returns the result.
740
\fn QSizeF &QSizeF::operator/=(qreal divisor)
744
Divides both the width and height by \a divisor and returns a reference to the
751
\fn const QSizeF operator/(const QSizeF &size, qreal divisor)
756
Divides \a size by \a divisor and returns the result.
762
\fn QSizeF QSizeF::expandedTo(const QSizeF & otherSize) const
764
Returns a size with the maximum width and height of this size and
767
\sa boundedTo() scale() setWidth() setHeight()
771
\fn QSizeF QSizeF::boundedTo(const QSizeF & otherSize) const
773
Returns a size with the minimum width and height of this size and
776
\sa expandedTo() scale() setWidth() setHeight()
781
/*****************************************************************************
782
QSizeF stream functions
783
*****************************************************************************/
784
#ifndef QT_NO_DATASTREAM
788
Writes the size \a sz to the stream \a s and returns a reference
791
\sa \link datastreamformat.html Format of the QDataStream
795
QDataStream &operator<<(QDataStream &s, const QSizeF &sz)
797
s << sz.width() << sz.height();
804
Reads the size from the stream \a s into size \a sz and returns a
805
reference to the stream.
807
\sa \link datastreamformat.html Format of the QDataStream
811
QDataStream &operator>>(QDataStream &s, QSizeF &sz)
820
#endif // QT_NO_DATASTREAM
822
#ifndef QT_NO_DEBUG_STREAM
823
QDebug operator<<(QDebug dbg, const QSizeF &s) {
824
dbg.nospace() << "QSizeF(" << s.width() << ',' << s.height() << ')';