1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
|
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qsize.h"
#include "qdatastream.h"
#include "qdebug.h"
QT_BEGIN_NAMESPACE
/*!
\class QSize
\ingroup painting
\brief The QSize class defines the size of a two-dimensional
object using integer point precision.
A size is specified by a width() and a height(). It can be set in
the constructor and changed using the setWidth(), setHeight(), or
scale() functions, or using arithmetic operators. A size can also
be manipulated directly by retrieving references to the width and
height using the rwidth() and rheight() functions. Finally, the
width and height can be swapped using the transpose() function.
The isValid() function determines if a size is valid (a valid size
has both width and height greater than zero). The isEmpty()
function returns true if either of the width and height is less
than, or equal to, zero, while the isNull() function returns true
only if both the width and the height is zero.
Use the expandedTo() function to retrieve a size which holds the
maximum height and width of \e this size and a given
size. Similarly, the boundedTo() function returns a size which
holds the minimum height and width of \e this size and a given
size.
QSize objects can be streamed as well as compared.
\sa QSizeF, QPoint, QRect
*/
/*****************************************************************************
QSize member functions
*****************************************************************************/
/*!
\fn QSize::QSize()
Constructs a size with an invalid width and height (i.e., isValid()
returns false).
\sa isValid()
*/
/*!
\fn QSize::QSize(int width, int height)
Constructs a size with the given \a width and \a height.
\sa setWidth(), setHeight()
*/
/*!
\fn bool QSize::isNull() const
Returns true if both the width and height is 0; otherwise returns
false.
\sa isValid(), isEmpty()
*/
/*!
\fn bool QSize::isEmpty() const
Returns true if either of the width and height is less than or
equal to 0; otherwise returns false.
\sa isNull(), isValid()
*/
/*!
\fn bool QSize::isValid() const
Returns true if both the width and height is equal to or greater
than 0; otherwise returns false.
\sa isNull(), isEmpty()
*/
/*!
\fn int QSize::width() const
Returns the width.
\sa height(), setWidth()
*/
/*!
\fn int QSize::height() const
Returns the height.
\sa width(), setHeight()
*/
/*!
\fn void QSize::setWidth(int width)
Sets the width to the given \a width.
\sa rwidth(), width(), setHeight()
*/
/*!
\fn void QSize::setHeight(int height)
Sets the height to the given \a height.
\sa rheight(), height(), setWidth()
*/
/*!
Swaps the width and height values.
\sa setWidth(), setHeight()
*/
void QSize::transpose()
{
int tmp = wd;
wd = ht;
ht = tmp;
}
/*!
\fn void QSize::scale(int width, int height, Qt::AspectRatioMode mode)
Scales the size to a rectangle with the given \a width and \a
height, according to the specified \a mode:
\list
\i If \a mode is Qt::IgnoreAspectRatio, the size is set to (\a width, \a height).
\i If \a mode is Qt::KeepAspectRatio, the current size is scaled to a rectangle
as large as possible inside (\a width, \a height), preserving the aspect ratio.
\i If \a mode is Qt::KeepAspectRatioByExpanding, the current size is scaled to a rectangle
as small as possible outside (\a width, \a height), preserving the aspect ratio.
\endlist
Example:
\snippet doc/src/snippets/code/src_corelib_tools_qsize.cpp 0
\sa setWidth(), setHeight()
*/
/*!
\fn void QSize::scale(const QSize &size, Qt::AspectRatioMode mode)
\overload
Scales the size to a rectangle with the given \a size, according to
the specified \a mode.
*/
void QSize::scale(const QSize &s, Qt::AspectRatioMode mode)
{
if (mode == Qt::IgnoreAspectRatio || wd == 0 || ht == 0) {
wd = s.wd;
ht = s.ht;
} else {
bool useHeight;
qint64 rw = qint64(s.ht) * qint64(wd) / qint64(ht);
if (mode == Qt::KeepAspectRatio) {
useHeight = (rw <= s.wd);
} else { // mode == Qt::KeepAspectRatioByExpanding
useHeight = (rw >= s.wd);
}
if (useHeight) {
wd = rw;
ht = s.ht;
} else {
ht = qint32(qint64(s.wd) * qint64(ht) / qint64(wd));
wd = s.wd;
}
}
}
/*!
\fn int &QSize::rwidth()
Returns a reference to the width.
Using a reference makes it possible to manipulate the width
directly. For example:
\snippet doc/src/snippets/code/src_corelib_tools_qsize.cpp 1
\sa rheight(), setWidth()
*/
/*!
\fn int &QSize::rheight()
Returns a reference to the height.
Using a reference makes it possible to manipulate the height
directly. For example:
\snippet doc/src/snippets/code/src_corelib_tools_qsize.cpp 2
\sa rwidth(), setHeight()
*/
/*!
\fn QSize &QSize::operator+=(const QSize &size)
Adds the given \a size to \e this size, and returns a reference to
this size. For example:
\snippet doc/src/snippets/code/src_corelib_tools_qsize.cpp 3
*/
/*!
\fn QSize &QSize::operator-=(const QSize &size)
Subtracts the given \a size from \e this size, and returns a
reference to this size. For example:
\snippet doc/src/snippets/code/src_corelib_tools_qsize.cpp 4
*/
/*!
\fn QSize &QSize::operator*=(qreal factor)
\overload
Multiplies both the width and height by the given \a factor, and
returns a reference to the size.
Note that the result is rounded to the nearest integer.
\sa scale()
*/
/*!
\fn bool operator==(const QSize &s1, const QSize &s2)
\relates QSize
Returns true if \a s1 and \a s2 are equal; otherwise returns false.
*/
/*!
\fn bool operator!=(const QSize &s1, const QSize &s2)
\relates QSize
Returns true if \a s1 and \a s2 are different; otherwise returns false.
*/
/*!
\fn const QSize operator+(const QSize &s1, const QSize &s2)
\relates QSize
Returns the sum of \a s1 and \a s2; each component is added separately.
*/
/*!
\fn const QSize operator-(const QSize &s1, const QSize &s2)
\relates QSize
Returns \a s2 subtracted from \a s1; each component is subtracted
separately.
*/
/*!
\fn const QSize operator*(const QSize &size, qreal factor)
\relates QSize
Multiplies the given \a size by the given \a factor, and returns
the result rounded to the nearest integer.
\sa QSize::scale()
*/
/*!
\fn const QSize operator*(qreal factor, const QSize &size)
\overload
\relates QSize
Multiplies the given \a size by the given \a factor, and returns
the result rounded to the nearest integer.
*/
/*!
\fn QSize &QSize::operator/=(qreal divisor)
\overload
Divides both the width and height by the given \a divisor, and
returns a reference to the size.
Note that the result is rounded to the nearest integer.
\sa QSize::scale()
*/
/*!
\fn const QSize operator/(const QSize &size, qreal divisor)
\relates QSize
\overload
Divides the given \a size by the given \a divisor, and returns the
result rounded to the nearest integer.
\sa QSize::scale()
*/
/*!
\fn QSize QSize::expandedTo(const QSize & otherSize) const
Returns a size holding the maximum width and height of this size
and the given \a otherSize.
\sa boundedTo(), scale()
*/
/*!
\fn QSize QSize::boundedTo(const QSize & otherSize) const
Returns a size holding the minimum width and height of this size
and the given \a otherSize.
\sa expandedTo(), scale()
*/
/*****************************************************************************
QSize stream functions
*****************************************************************************/
#ifndef QT_NO_DATASTREAM
/*!
\fn QDataStream &operator<<(QDataStream &stream, const QSize &size)
\relates QSize
Writes the given \a size to the given \a stream, and returns a
reference to the stream.
\sa {Format of the QDataStream Operators}
*/
QDataStream &operator<<(QDataStream &s, const QSize &sz)
{
if (s.version() == 1)
s << (qint16)sz.width() << (qint16)sz.height();
else
s << (qint32)sz.width() << (qint32)sz.height();
return s;
}
/*!
\fn QDataStream &operator>>(QDataStream &stream, QSize &size)
\relates QSize
Reads a size from the given \a stream into the given \a size, and
returns a reference to the stream.
\sa {Format of the QDataStream Operators}
*/
QDataStream &operator>>(QDataStream &s, QSize &sz)
{
if (s.version() == 1) {
qint16 w, h;
s >> w; sz.rwidth() = w;
s >> h; sz.rheight() = h;
}
else {
qint32 w, h;
s >> w; sz.rwidth() = w;
s >> h; sz.rheight() = h;
}
return s;
}
#endif // QT_NO_DATASTREAM
#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, const QSize &s) {
dbg.nospace() << "QSize(" << s.width() << ", " << s.height() << ')';
return dbg.space();
}
#endif
/*!
\class QSizeF
\brief The QSizeF class defines the size of a two-dimensional object
using floating point precision.
\ingroup painting
A size is specified by a width() and a height(). It can be set in
the constructor and changed using the setWidth(), setHeight(), or
scale() functions, or using arithmetic operators. A size can also
be manipulated directly by retrieving references to the width and
height using the rwidth() and rheight() functions. Finally, the
width and height can be swapped using the transpose() function.
The isValid() function determines if a size is valid. A valid size
has both width and height greater than or equal to zero. The
isEmpty() function returns true if either of the width and height
is \e less than (or equal to) zero, while the isNull() function
returns true only if both the width and the height is zero.
Use the expandedTo() function to retrieve a size which holds the
maximum height and width of this size and a given
size. Similarly, the boundedTo() function returns a size which
holds the minimum height and width of this size and a given size.
The QSizeF class also provides the toSize() function returning a
QSize copy of this size, constructed by rounding the width and
height to the nearest integers.
QSizeF objects can be streamed as well as compared.
\sa QSize, QPointF, QRectF
*/
/*****************************************************************************
QSizeF member functions
*****************************************************************************/
/*!
\fn QSizeF::QSizeF()
Constructs an invalid size.
\sa isValid()
*/
/*!
\fn QSizeF::QSizeF(const QSize &size)
Constructs a size with floating point accuracy from the given \a
size.
\sa toSize()
*/
/*!
\fn QSizeF::QSizeF(qreal width, qreal height)
Constructs a size with the given \a width and \a height.
*/
/*!
\fn bool QSizeF::isNull() const
Returns true if both the width and height is 0; otherwise returns
false.
\sa isValid(), isEmpty()
*/
/*!
\fn bool QSizeF::isEmpty() const
Returns true if either of the width and height is less than or
equal to 0; otherwise returns false.
\sa isNull(), isValid()
*/
/*!
\fn bool QSizeF::isValid() const
Returns true if both the width and height is equal to or greater
than 0; otherwise returns false.
\sa isNull(), isEmpty()
*/
/*!
\fn int QSizeF::width() const
Returns the width.
\sa height(), setWidth()
*/
/*!
\fn int QSizeF::height() const
Returns the height.
\sa width(), setHeight()
*/
/*!
\fn void QSizeF::setWidth(qreal width)
Sets the width to the given \a width.
\sa width(), rwidth(), setHeight()
*/
/*!
\fn void QSizeF::setHeight(qreal height)
Sets the height to the given \a height.
\sa height(), rheight(), setWidth()
*/
/*!
\fn QSize QSizeF::toSize() const
Returns an integer based copy of this size.
Note that the coordinates in the returned size will be rounded to
the nearest integer.
\sa QSizeF()
*/
/*!
Swaps the width and height values.
\sa setWidth(), setHeight()
*/
void QSizeF::transpose()
{
qreal tmp = wd;
wd = ht;
ht = tmp;
}
/*!
\fn void QSizeF::scale(qreal width, qreal height, Qt::AspectRatioMode mode)
Scales the size to a rectangle with the given \a width and \a
height, according to the specified \a mode.
\list
\i If \a mode is Qt::IgnoreAspectRatio, the size is set to (\a width, \a height).
\i If \a mode is Qt::KeepAspectRatio, the current size is scaled to a rectangle
as large as possible inside (\a width, \a height), preserving the aspect ratio.
\i If \a mode is Qt::KeepAspectRatioByExpanding, the current size is scaled to a rectangle
as small as possible outside (\a width, \a height), preserving the aspect ratio.
\endlist
Example:
\snippet doc/src/snippets/code/src_corelib_tools_qsize.cpp 5
\sa setWidth(), setHeight()
*/
/*!
\fn void QSizeF::scale(const QSizeF &size, Qt::AspectRatioMode mode)
\overload
Scales the size to a rectangle with the given \a size, according to
the specified \a mode.
*/
void QSizeF::scale(const QSizeF &s, Qt::AspectRatioMode mode)
{
if (mode == Qt::IgnoreAspectRatio || qIsNull(wd) || qIsNull(ht)) {
wd = s.wd;
ht = s.ht;
} else {
bool useHeight;
qreal rw = s.ht * wd / ht;
if (mode == Qt::KeepAspectRatio) {
useHeight = (rw <= s.wd);
} else { // mode == Qt::KeepAspectRatioByExpanding
useHeight = (rw >= s.wd);
}
if (useHeight) {
wd = rw;
ht = s.ht;
} else {
ht = s.wd * ht / wd;
wd = s.wd;
}
}
}
/*!
\fn int &QSizeF::rwidth()
Returns a reference to the width.
Using a reference makes it possible to manipulate the width
directly. For example:
\snippet doc/src/snippets/code/src_corelib_tools_qsize.cpp 6
\sa rheight(), setWidth()
*/
/*!
\fn int &QSizeF::rheight()
Returns a reference to the height.
Using a reference makes it possible to manipulate the height
directly. For example:
\snippet doc/src/snippets/code/src_corelib_tools_qsize.cpp 7
\sa rwidth(), setHeight()
*/
/*!
\fn QSizeF &QSizeF::operator+=(const QSizeF &size)
Adds the given \a size to this size and returns a reference to
this size. For example:
\snippet doc/src/snippets/code/src_corelib_tools_qsize.cpp 8
*/
/*!
\fn QSizeF &QSizeF::operator-=(const QSizeF &size)
Subtracts the given \a size from this size and returns a reference
to this size. For example:
\snippet doc/src/snippets/code/src_corelib_tools_qsize.cpp 9
*/
/*!
\fn QSizeF &QSizeF::operator*=(qreal factor)
\overload
Multiplies both the width and height by the given \a factor and
returns a reference to the size.
\sa scale()
*/
/*!
\fn bool operator==(const QSizeF &s1, const QSizeF &s2)
\relates QSizeF
Returns true if \a s1 and \a s2 are equal; otherwise returns
false.
*/
/*!
\fn bool operator!=(const QSizeF &s1, const QSizeF &s2)
\relates QSizeF
Returns true if \a s1 and \a s2 are different; otherwise returns false.
*/
/*!
\fn const QSizeF operator+(const QSizeF &s1, const QSizeF &s2)
\relates QSizeF
Returns the sum of \a s1 and \a s2; each component is added separately.
*/
/*!
\fn const QSizeF operator-(const QSizeF &s1, const QSizeF &s2)
\relates QSizeF
Returns \a s2 subtracted from \a s1; each component is subtracted
separately.
*/
/*!
\fn const QSizeF operator*(const QSizeF &size, qreal factor)
\overload
\relates QSizeF
Multiplies the given \a size by the given \a factor and returns
the result.
\sa QSizeF::scale()
*/
/*!
\fn const QSizeF operator*(qreal factor, const QSizeF &size)
\overload
\relates QSizeF
Multiplies the given \a size by the given \a factor and returns
the result.
*/
/*!
\fn QSizeF &QSizeF::operator/=(qreal divisor)
\overload
Divides both the width and height by the given \a divisor and
returns a reference to the size.
\sa scale()
*/
/*!
\fn const QSizeF operator/(const QSizeF &size, qreal divisor)
\relates QSizeF
\overload
Divides the given \a size by the given \a divisor and returns the
result.
\sa QSizeF::scale()
*/
/*!
\fn QSizeF QSizeF::expandedTo(const QSizeF & otherSize) const
Returns a size holding the maximum width and height of this size
and the given \a otherSize.
\sa boundedTo(), scale()
*/
/*!
\fn QSizeF QSizeF::boundedTo(const QSizeF & otherSize) const
Returns a size holding the minimum width and height of this size
and the given \a otherSize.
\sa expandedTo(), scale()
*/
/*****************************************************************************
QSizeF stream functions
*****************************************************************************/
#ifndef QT_NO_DATASTREAM
/*!
\fn QDataStream &operator<<(QDataStream &stream, const QSizeF &size)
\relates QSizeF
Writes the given \a size to the given \a stream and returns a
reference to the stream.
\sa {Format of the QDataStream Operators}
*/
QDataStream &operator<<(QDataStream &s, const QSizeF &sz)
{
s << double(sz.width()) << double(sz.height());
return s;
}
/*!
\fn QDataStream &operator>>(QDataStream &stream, QSizeF &size)
\relates QSizeF
Reads a size from the given \a stream into the given \a size and
returns a reference to the stream.
\sa {Format of the QDataStream Operators}
*/
QDataStream &operator>>(QDataStream &s, QSizeF &sz)
{
double w, h;
s >> w;
s >> h;
sz.setWidth(qreal(w));
sz.setHeight(qreal(h));
return s;
}
#endif // QT_NO_DATASTREAM
#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, const QSizeF &s) {
dbg.nospace() << "QSizeF(" << s.width() << ", " << s.height() << ')';
return dbg.space();
}
#endif
QT_END_NAMESPACE
|