1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qrect.h"
5#include "qdatastream.h"
6#include "qmath.h"
7
8#include <private/qdebug_p.h>
9
10QT_BEGIN_NAMESPACE
11
12/*!
13 \class QRect
14 \inmodule QtCore
15 \ingroup painting
16 \reentrant
17
18 \brief The QRect class defines a rectangle in the plane using
19 integer precision.
20
21 A rectangle is normally expressed as a top-left corner and a
22 size. The size (width and height) of a QRect is always equivalent
23 to the mathematical rectangle that forms the basis for its
24 rendering.
25
26 A QRect can be constructed with a set of left, top, width and
27 height integers, or from a QPoint and a QSize. The following code
28 creates two identical rectangles.
29
30 \snippet code/src_corelib_tools_qrect.cpp 0
31
32 There is a third constructor that creates a QRect using the
33 top-left and bottom-right coordinates, but we recommend that you
34 avoid using it. The rationale is that for historical reasons the
35 values returned by the bottom() and right() functions deviate from
36 the true bottom-right corner of the rectangle.
37
38 The QRect class provides a collection of functions that return the
39 various rectangle coordinates, and enable manipulation of
40 these. QRect also provides functions to move the rectangle relative
41 to the various coordinates. In addition there is a moveTo()
42 function that moves the rectangle, leaving its top left corner at
43 the given coordinates. Alternatively, the translate() function
44 moves the rectangle the given offset relative to the current
45 position, and the translated() function returns a translated copy
46 of this rectangle.
47
48 The size() function returns the rectangle's dimensions as a
49 QSize. The dimensions can also be retrieved separately using the
50 width() and height() functions. To manipulate the dimensions use
51 the setSize(), setWidth() or setHeight() functions. Alternatively,
52 the size can be changed by applying either of the functions
53 setting the rectangle coordinates, for example, setBottom() or
54 setRight().
55
56 The contains() function tells whether a given point is inside the
57 rectangle or not, and the intersects() function returns \c true if
58 this rectangle intersects with a given rectangle. The QRect class
59 also provides the intersected() function which returns the
60 intersection rectangle, and the united() function which returns the
61 rectangle that encloses the given rectangle and this:
62
63 \table
64 \row
65 \li \inlineimage qrect-intersect.png
66 \li \inlineimage qrect-unite.png
67 \row
68 \li intersected()
69 \li united()
70 \endtable
71
72 The isEmpty() function returns \c true if left() > right() or top() >
73 bottom(). Note that an empty rectangle is not valid: The isValid()
74 function returns \c true if left() <= right() \e and top() <=
75 bottom(). A null rectangle (isNull() == true) on the other hand,
76 has both width and height set to 0.
77
78 Note that due to the way QRect and QRectF are defined, an
79 empty QRect is defined in essentially the same way as QRectF.
80
81 Finally, QRect objects can be streamed as well as compared.
82
83 \tableofcontents
84
85 \section1 Rendering
86
87 When using an \l {QPainter::Antialiasing}{anti-aliased} painter,
88 the boundary line of a QRect will be rendered symmetrically on
89 both sides of the mathematical rectangle's boundary line. But when
90 using an aliased painter (the default) other rules apply.
91
92 Then, when rendering with a one pixel wide pen the QRect's boundary
93 line will be rendered to the right and below the mathematical
94 rectangle's boundary line.
95
96 When rendering with a two pixels wide pen the boundary line will
97 be split in the middle by the mathematical rectangle. This will be
98 the case whenever the pen is set to an even number of pixels,
99 while rendering with a pen with an odd number of pixels, the spare
100 pixel will be rendered to the right and below the mathematical
101 rectangle as in the one pixel case.
102
103 \table
104 \row
105 \li \inlineimage qrect-diagram-zero.png
106 \li \inlineimage qrect-diagram-one.png
107 \row
108 \li Logical representation
109 \li One pixel wide pen
110 \row
111 \li \inlineimage qrect-diagram-two.png
112 \li \inlineimage qrect-diagram-three.png
113 \row
114 \li Two pixel wide pen
115 \li Three pixel wide pen
116 \endtable
117
118 \section1 Coordinates
119
120 The QRect class provides a collection of functions that return the
121 various rectangle coordinates, and enable manipulation of
122 these. QRect also provides functions to move the rectangle relative
123 to the various coordinates.
124
125 For example the left(), setLeft() and moveLeft() functions as an
126 example: left() returns the x-coordinate of the rectangle's left
127 edge, setLeft() sets the left edge of the rectangle to the given x
128 coordinate (it may change the width, but will never change the
129 rectangle's right edge) and moveLeft() moves the entire rectangle
130 horizontally, leaving the rectangle's left edge at the given x
131 coordinate and its size unchanged.
132
133 \image qrect-coordinates.png
134
135 Note that for historical reasons the values returned by the
136 bottom() and right() functions deviate from the true bottom-right
137 corner of the rectangle: The right() function returns \e { left()
138 + width() - 1} and the bottom() function returns \e {top() +
139 height() - 1}. The same is the case for the point returned by the
140 bottomRight() convenience function. In addition, the x and y
141 coordinate of the topRight() and bottomLeft() functions,
142 respectively, contain the same deviation from the true right and
143 bottom edges.
144
145 We recommend that you use x() + width() and y() + height() to find
146 the true bottom-right corner, and avoid right() and
147 bottom(). Another solution is to use QRectF: The QRectF class
148 defines a rectangle in the plane using floating point accuracy for
149 coordinates, and the QRectF::right() and QRectF::bottom()
150 functions \e do return the right and bottom coordinates.
151
152 It is also possible to add offsets to this rectangle's coordinates
153 using the adjust() function, as well as retrieve a new rectangle
154 based on adjustments of the original one using the adjusted()
155 function. If either of the width and height is negative, use the
156 normalized() function to retrieve a rectangle where the corners
157 are swapped.
158
159 In addition, QRect provides the getCoords() function which extracts
160 the position of the rectangle's top-left and bottom-right corner,
161 and the getRect() function which extracts the rectangle's top-left
162 corner, width and height. Use the setCoords() and setRect()
163 function to manipulate the rectangle's coordinates and dimensions
164 in one go.
165
166 \section1 Constraints
167
168 QRect is limited to the minimum and maximum values for the \c int type.
169 Operations on a QRect that could potentially result in values outside this
170 range will result in undefined behavior.
171
172 \sa QRectF, QRegion
173*/
174
175/*****************************************************************************
176 QRect member functions
177 *****************************************************************************/
178
179/*!
180 \fn QRect::QRect()
181
182 Constructs a null rectangle.
183
184 \sa isNull()
185*/
186
187/*!
188 \fn QRect::QRect(const QPoint &topLeft, const QPoint &bottomRight)
189
190 Constructs a rectangle with the given \a topLeft and \a bottomRight corners, both included.
191
192 If \a bottomRight is to higher and to the left of \a topLeft, the rectangle defined
193 is instead non-inclusive of the corners.
194
195 \note To ensure both points are included regardless of relative order, use span().
196
197 \sa setTopLeft(), setBottomRight(), span()
198*/
199
200
201/*!
202 \fn QRect::QRect(const QPoint &topLeft, const QSize &size)
203
204 Constructs a rectangle with the given \a topLeft corner and the
205 given \a size.
206
207 \sa setTopLeft(), setSize()
208*/
209
210
211/*!
212 \fn QRect::QRect(int x, int y, int width, int height)
213
214 Constructs a rectangle with (\a x, \a y) as its top-left corner
215 and the given \a width and \a height.
216
217 \sa setRect()
218*/
219
220
221/*!
222 \fn bool QRect::isNull() const
223
224 Returns \c true if the rectangle is a null rectangle, otherwise
225 returns \c false.
226
227 A null rectangle has both the width and the height set to 0 (i.e.,
228 right() == left() - 1 and bottom() == top() - 1). A null rectangle
229 is also empty, and hence is not valid.
230
231 \sa isEmpty(), isValid()
232*/
233
234/*!
235 \fn bool QRect::isEmpty() const
236
237 Returns \c true if the rectangle is empty, otherwise returns \c false.
238
239 An empty rectangle has a left() > right() or top() > bottom(). An
240 empty rectangle is not valid (i.e., isEmpty() == !isValid()).
241
242 Use the normalized() function to retrieve a rectangle where the
243 corners are swapped.
244
245 \sa isNull(), isValid(), normalized()
246*/
247
248/*!
249 \fn bool QRect::isValid() const
250
251 Returns \c true if the rectangle is valid, otherwise returns \c false.
252
253 A valid rectangle has a left() <= right() and top() <=
254 bottom(). Note that non-trivial operations like intersections are
255 not defined for invalid rectangles. A valid rectangle is not empty
256 (i.e., isValid() == !isEmpty()).
257
258 \sa isNull(), isEmpty(), normalized()
259*/
260
261
262/*!
263 Returns a normalized rectangle; i.e., a rectangle that has a
264 non-negative width and height.
265
266 If width() < 0 the function swaps the left and right corners, and
267 it swaps the top and bottom corners if height() < 0. The corners
268 are at the same time changed from being non-inclusive to inclusive.
269
270 \sa isValid(), isEmpty()
271*/
272
273QRect QRect::normalized() const noexcept
274{
275 QRect r(*this);
276 if (x2 < x1) { // swap bad x values
277 r.x1 = x2 + 1;
278 r.x2 = x1 - 1;
279 }
280 if (y2 < y1) { // swap bad y values
281 r.y1 = y2 + 1;
282 r.y2 = y1 - 1;
283 }
284 return r;
285}
286
287
288/*!
289 \fn int QRect::left() const
290
291 Returns the x-coordinate of the rectangle's left edge. Equivalent
292 to x().
293
294 \sa setLeft(), topLeft(), bottomLeft()
295*/
296
297/*!
298 \fn int QRect::top() const
299
300 Returns the y-coordinate of the rectangle's top edge.
301 Equivalent to y().
302
303 \sa setTop(), topLeft(), topRight()
304*/
305
306/*!
307 \fn int QRect::right() const
308
309 Returns the x-coordinate of the rectangle's right edge.
310
311 Note that for historical reasons this function returns left() +
312 width() - 1; use x() + width() to retrieve the true x-coordinate.
313
314 \sa setRight(), topRight(), bottomRight()
315*/
316
317/*!
318 \fn int QRect::bottom() const
319
320 Returns the y-coordinate of the rectangle's bottom edge.
321
322 Note that for historical reasons this function returns top() +
323 height() - 1; use y() + height() to retrieve the true y-coordinate.
324
325 \sa setBottom(), bottomLeft(), bottomRight()
326*/
327
328/*!
329 \fn int QRect::x() const
330
331 Returns the x-coordinate of the rectangle's left edge. Equivalent to left().
332
333 \sa setX(), y(), topLeft()
334*/
335
336/*!
337 \fn int QRect::y() const
338
339 Returns the y-coordinate of the rectangle's top edge. Equivalent to top().
340
341 \sa setY(), x(), topLeft()
342*/
343
344/*!
345 \fn void QRect::setLeft(int x)
346
347 Sets the left edge of the rectangle to the given \a x
348 coordinate. May change the width, but will never change the right
349 edge of the rectangle.
350
351 Equivalent to setX().
352
353 \sa left(), moveLeft()
354*/
355
356/*!
357 \fn void QRect::setTop(int y)
358
359 Sets the top edge of the rectangle to the given \a y
360 coordinate. May change the height, but will never change the
361 bottom edge of the rectangle.
362
363 Equivalent to setY().
364
365 \sa top(), moveTop()
366*/
367
368/*!
369 \fn void QRect::setRight(int x)
370
371 Sets the right edge of the rectangle to the given \a x
372 coordinate. May change the width, but will never change the left
373 edge of the rectangle.
374
375 \sa right(), moveRight()
376*/
377
378/*!
379 \fn void QRect::setBottom(int y)
380
381 Sets the bottom edge of the rectangle to the given \a y
382 coordinate. May change the height, but will never change the top
383 edge of the rectangle.
384
385 \sa bottom(), moveBottom(),
386*/
387
388/*!
389 \fn void QRect::setX(int x)
390
391 Sets the left edge of the rectangle to the given \a x
392 coordinate. May change the width, but will never change the right
393 edge of the rectangle.
394
395 Equivalent to setLeft().
396
397 \sa x(), setY(), setTopLeft()
398*/
399
400/*!
401 \fn void QRect::setY(int y)
402
403 Sets the top edge of the rectangle to the given \a y
404 coordinate. May change the height, but will never change the
405 bottom edge of the rectangle.
406
407 Equivalent to setTop().
408
409 \sa y(), setX(), setTopLeft()
410*/
411
412/*!
413 \fn void QRect::setTopLeft(const QPoint &position)
414
415 Set the top-left corner of the rectangle to the given \a
416 position. May change the size, but will never change the
417 bottom-right corner of the rectangle.
418
419 \sa topLeft(), moveTopLeft()
420*/
421
422/*!
423 \fn void QRect::setBottomRight(const QPoint &position)
424
425 Set the bottom-right corner of the rectangle to the given \a
426 position. May change the size, but will never change the
427 top-left corner of the rectangle.
428
429 \sa bottomRight(), moveBottomRight()
430*/
431
432/*!
433 \fn void QRect::setTopRight(const QPoint &position)
434
435 Set the top-right corner of the rectangle to the given \a
436 position. May change the size, but will never change the
437 bottom-left corner of the rectangle.
438
439 \sa topRight(), moveTopRight()
440*/
441
442/*!
443 \fn void QRect::setBottomLeft(const QPoint &position)
444
445 Set the bottom-left corner of the rectangle to the given \a
446 position. May change the size, but will never change the
447 top-right corner of the rectangle.
448
449 \sa bottomLeft(), moveBottomLeft()
450*/
451
452/*!
453 \fn QPoint QRect::topLeft() const
454
455 Returns the position of the rectangle's top-left corner.
456
457 \sa setTopLeft(), top(), left()
458*/
459
460/*!
461 \fn QPoint QRect::bottomRight() const
462
463 Returns the position of the rectangle's bottom-right corner.
464
465 Note that for historical reasons this function returns
466 QPoint(left() + width() -1, top() + height() - 1).
467
468 \sa setBottomRight(), bottom(), right()
469*/
470
471/*!
472 \fn QPoint QRect::topRight() const
473
474 Returns the position of the rectangle's top-right corner.
475
476 Note that for historical reasons this function returns
477 QPoint(left() + width() -1, top()).
478
479 \sa setTopRight(), top(), right()
480*/
481
482/*!
483 \fn QPoint QRect::bottomLeft() const
484
485 Returns the position of the rectangle's bottom-left corner. Note
486 that for historical reasons this function returns QPoint(left(),
487 top() + height() - 1).
488
489 \sa setBottomLeft(), bottom(), left()
490*/
491
492/*!
493 \fn QPoint QRect::center() const
494
495 Returns the center point of the rectangle.
496
497 \sa moveCenter()
498*/
499
500
501/*!
502 \fn void QRect::getRect(int *x, int *y, int *width, int *height) const
503
504 Extracts the position of the rectangle's top-left corner to *\a x
505 and *\a y, and its dimensions to *\a width and *\a height.
506
507 \sa setRect(), getCoords()
508*/
509
510
511/*!
512 \fn void QRect::getCoords(int *x1, int *y1, int *x2, int *y2) const
513
514 Extracts the position of the rectangle's top-left corner to *\a x1
515 and *\a y1, and the position of the bottom-right corner to *\a x2
516 and *\a y2.
517
518 \sa setCoords(), getRect()
519*/
520
521/*!
522 \fn void QRect::moveLeft(int x)
523
524 Moves the rectangle horizontally, leaving the rectangle's left
525 edge at the given \a x coordinate. The rectangle's size is
526 unchanged.
527
528 \sa left(), setLeft(), moveRight()
529*/
530
531/*!
532 \fn void QRect::moveTop(int y)
533
534 Moves the rectangle vertically, leaving the rectangle's top edge
535 at the given \a y coordinate. The rectangle's size is unchanged.
536
537 \sa top(), setTop(), moveBottom()
538*/
539
540
541/*!
542 \fn void QRect::moveRight(int x)
543
544 Moves the rectangle horizontally, leaving the rectangle's right
545 edge at the given \a x coordinate. The rectangle's size is
546 unchanged.
547
548 \sa right(), setRight(), moveLeft()
549*/
550
551
552/*!
553 \fn void QRect::moveBottom(int y)
554
555 Moves the rectangle vertically, leaving the rectangle's bottom
556 edge at the given \a y coordinate. The rectangle's size is
557 unchanged.
558
559 \sa bottom(), setBottom(), moveTop()
560*/
561
562
563/*!
564 \fn void QRect::moveTopLeft(const QPoint &position)
565
566 Moves the rectangle, leaving the top-left corner at the given \a
567 position. The rectangle's size is unchanged.
568
569 \sa setTopLeft(), moveTop(), moveLeft()
570*/
571
572
573/*!
574 \fn void QRect::moveBottomRight(const QPoint &position)
575
576 Moves the rectangle, leaving the bottom-right corner at the given
577 \a position. The rectangle's size is unchanged.
578
579 \sa setBottomRight(), moveRight(), moveBottom()
580*/
581
582
583/*!
584 \fn void QRect::moveTopRight(const QPoint &position)
585
586 Moves the rectangle, leaving the top-right corner at the given \a
587 position. The rectangle's size is unchanged.
588
589 \sa setTopRight(), moveTop(), moveRight()
590*/
591
592
593/*!
594 \fn void QRect::moveBottomLeft(const QPoint &position)
595
596 Moves the rectangle, leaving the bottom-left corner at the given
597 \a position. The rectangle's size is unchanged.
598
599 \sa setBottomLeft(), moveBottom(), moveLeft()
600*/
601
602
603/*!
604 \fn void QRect::moveCenter(const QPoint &position)
605
606 Moves the rectangle, leaving the center point at the given \a
607 position. The rectangle's size is unchanged.
608
609 \sa center()
610*/
611
612/*!
613 \fn void QRect::moveTo(int x, int y)
614
615 Moves the rectangle, leaving the top-left corner at the given
616 position (\a x, \a y). The rectangle's size is unchanged.
617
618 \sa translate(), moveTopLeft()
619*/
620
621/*!
622 \fn void QRect::moveTo(const QPoint &position)
623
624 Moves the rectangle, leaving the top-left corner at the given \a
625 position.
626*/
627
628/*!
629 \fn void QRect::translate(int dx, int dy)
630
631 Moves the rectangle \a dx along the x axis and \a dy along the y
632 axis, relative to the current position. Positive values move the
633 rectangle to the right and down.
634
635 \sa moveTopLeft(), moveTo(), translated()
636*/
637
638
639/*!
640 \fn void QRect::translate(const QPoint &offset)
641 \overload
642
643 Moves the rectangle \a{offset}.\l{QPoint::x()}{x()} along the x
644 axis and \a{offset}.\l{QPoint::y()}{y()} along the y axis,
645 relative to the current position.
646*/
647
648
649/*!
650 \fn QRect QRect::translated(int dx, int dy) const
651
652 Returns a copy of the rectangle that is translated \a dx along the
653 x axis and \a dy along the y axis, relative to the current
654 position. Positive values move the rectangle to the right and
655 down.
656
657 \sa translate()
658
659*/
660
661
662/*!
663 \fn QRect QRect::translated(const QPoint &offset) const
664
665 \overload
666
667 Returns a copy of the rectangle that is translated
668 \a{offset}.\l{QPoint::x()}{x()} along the x axis and
669 \a{offset}.\l{QPoint::y()}{y()} along the y axis, relative to the
670 current position.
671*/
672
673/*!
674 \fn QRect QRect::transposed() const
675 \since 5.7
676
677 Returns a copy of the rectangle that has its width and height
678 exchanged:
679
680 \snippet code/src_corelib_tools_qrect.cpp 2
681
682 \sa QSize::transposed()
683*/
684
685/*!
686 \fn void QRect::setRect(int x, int y, int width, int height)
687
688 Sets the coordinates of the rectangle's top-left corner to (\a{x},
689 \a{y}), and its size to the given \a width and \a height.
690
691 \sa getRect(), setCoords()
692*/
693
694
695/*!
696 \fn void QRect::setCoords(int x1, int y1, int x2, int y2)
697
698 Sets the coordinates of the rectangle's top-left corner to (\a x1,
699 \a y1), and the coordinates of its bottom-right corner to (\a x2,
700 \a y2).
701
702 \sa getCoords(), setRect()
703*/
704
705
706/*! \fn QRect QRect::adjusted(int dx1, int dy1, int dx2, int dy2) const
707
708 Returns a new rectangle with \a dx1, \a dy1, \a dx2 and \a dy2
709 added respectively to the existing coordinates of this rectangle.
710
711 \sa adjust()
712*/
713
714/*! \fn void QRect::adjust(int dx1, int dy1, int dx2, int dy2)
715
716 Adds \a dx1, \a dy1, \a dx2 and \a dy2 respectively to the
717 existing coordinates of the rectangle.
718
719 \sa adjusted(), setRect()
720*/
721
722/*!
723 \fn QSize QRect::size() const
724
725 Returns the size of the rectangle.
726
727 \sa setSize(), width(), height()
728*/
729
730/*!
731 \fn int QRect::width() const
732
733 Returns the width of the rectangle.
734
735 \sa setWidth(), height(), size()
736*/
737
738/*!
739 \fn int QRect::height() const
740
741 Returns the height of the rectangle.
742
743 \sa setHeight(), width(), size()
744*/
745
746/*!
747 \fn void QRect::setWidth(int width)
748
749 Sets the width of the rectangle to the given \a width. The right
750 edge is changed, but not the left one.
751
752 \sa width(), setSize()
753*/
754
755
756/*!
757 \fn void QRect::setHeight(int height)
758
759 Sets the height of the rectangle to the given \a height. The bottom
760 edge is changed, but not the top one.
761
762 \sa height(), setSize()
763*/
764
765
766/*!
767 \fn void QRect::setSize(const QSize &size)
768
769 Sets the size of the rectangle to the given \a size. The top-left
770 corner is not moved.
771
772 \sa size(), setWidth(), setHeight()
773*/
774
775
776/*!
777 \fn bool QRect::contains(const QPoint &point, bool proper) const
778
779 Returns \c true if the given \a point is inside or on the edge of
780 the rectangle, otherwise returns \c false. If \a proper is true, this
781 function only returns \c true if the given \a point is \e inside the
782 rectangle (i.e., not on the edge).
783
784 \sa intersects()
785*/
786
787bool QRect::contains(const QPoint &p, bool proper) const noexcept
788{
789 int l, r;
790 if (x2 < x1 - 1) {
791 l = x2 + 1;
792 r = x1 - 1;
793 } else {
794 l = x1;
795 r = x2;
796 }
797 if (proper) {
798 if (p.x() <= l || p.x() >= r)
799 return false;
800 } else {
801 if (p.x() < l || p.x() > r)
802 return false;
803 }
804 int t, b;
805 if (y2 < y1 - 1) {
806 t = y2 + 1;
807 b = y1 - 1;
808 } else {
809 t = y1;
810 b = y2;
811 }
812 if (proper) {
813 if (p.y() <= t || p.y() >= b)
814 return false;
815 } else {
816 if (p.y() < t || p.y() > b)
817 return false;
818 }
819 return true;
820}
821
822
823/*!
824 \fn bool QRect::contains(int x, int y, bool proper) const
825 \overload
826
827 Returns \c true if the point (\a x, \a y) is inside or on the edge of
828 the rectangle, otherwise returns \c false. If \a proper is true, this
829 function only returns \c true if the point is entirely inside the
830 rectangle(not on the edge).
831*/
832
833/*!
834 \fn bool QRect::contains(int x, int y) const
835 \overload
836
837 Returns \c true if the point (\a x, \a y) is inside this rectangle,
838 otherwise returns \c false.
839*/
840
841/*!
842 \fn bool QRect::contains(const QRect &rectangle, bool proper) const
843 \overload
844
845 Returns \c true if the given \a rectangle is inside this rectangle.
846 otherwise returns \c false. If \a proper is true, this function only
847 returns \c true if the \a rectangle is entirely inside this
848 rectangle (not on the edge).
849*/
850
851bool QRect::contains(const QRect &r, bool proper) const noexcept
852{
853 if (isNull() || r.isNull())
854 return false;
855
856 int l1 = x1;
857 int r1 = x1 - 1;
858 if (x2 < x1 - 1)
859 l1 = x2 + 1;
860 else
861 r1 = x2;
862
863 int l2 = r.x1;
864 int r2 = r.x1 - 1;
865 if (r.x2 < r.x1 - 1)
866 l2 = r.x2 + 1;
867 else
868 r2 = r.x2;
869
870 if (proper) {
871 if (l2 <= l1 || r2 >= r1)
872 return false;
873 } else {
874 if (l2 < l1 || r2 > r1)
875 return false;
876 }
877
878 int t1 = y1;
879 int b1 = y1 - 1;
880 if (y2 < y1 - 1)
881 t1 = y2 + 1;
882 else
883 b1 = y2;
884
885 int t2 = r.y1;
886 int b2 = r.y1 - 1;
887 if (r.y2 < r.y1 - 1)
888 t2 = r.y2 + 1;
889 else
890 b2 = r.y2;
891
892 if (proper) {
893 if (t2 <= t1 || b2 >= b1)
894 return false;
895 } else {
896 if (t2 < t1 || b2 > b1)
897 return false;
898 }
899
900 return true;
901}
902
903/*!
904 \fn QRect& QRect::operator|=(const QRect &rectangle)
905
906 Unites this rectangle with the given \a rectangle.
907
908 \sa united(), operator|()
909*/
910
911/*!
912 \fn QRect& QRect::operator&=(const QRect &rectangle)
913
914 Intersects this rectangle with the given \a rectangle.
915
916 \sa intersected(), operator&()
917*/
918
919
920/*!
921 \fn QRect QRect::operator|(const QRect &rectangle) const
922
923 Returns the bounding rectangle of this rectangle and the given \a
924 rectangle.
925
926 \sa operator|=(), united()
927*/
928
929QRect QRect::operator|(const QRect &r) const noexcept
930{
931 if (isNull())
932 return r;
933 if (r.isNull())
934 return *this;
935
936 int l1 = x1;
937 int r1 = x1 - 1;
938 if (x2 < x1 - 1)
939 l1 = x2 + 1;
940 else
941 r1 = x2;
942
943 int l2 = r.x1;
944 int r2 = r.x1 - 1;
945 if (r.x2 < r.x1 - 1)
946 l2 = r.x2 + 1;
947 else
948 r2 = r.x2;
949
950 int t1 = y1;
951 int b1 = y1 - 1;
952 if (y2 < y1 - 1)
953 t1 = y2 + 1;
954 else
955 b1 = y2;
956
957 int t2 = r.y1;
958 int b2 = r.y1 - 1;
959 if (r.y2 < r.y1 - 1)
960 t2 = r.y2 + 1;
961 else
962 b2 = r.y2;
963
964 QRect tmp;
965 tmp.x1 = qMin(a: l1, b: l2);
966 tmp.x2 = qMax(a: r1, b: r2);
967 tmp.y1 = qMin(a: t1, b: t2);
968 tmp.y2 = qMax(a: b1, b: b2);
969 return tmp;
970}
971
972/*!
973 \fn QRect QRect::united(const QRect &rectangle) const
974 \since 4.2
975
976 Returns the bounding rectangle of this rectangle and the given \a rectangle.
977
978 \image qrect-unite.png
979
980 \sa intersected()
981*/
982
983
984/*!
985 \fn QRect QRect::operator&(const QRect &rectangle) const
986
987 Returns the intersection of this rectangle and the given \a
988 rectangle. Returns an empty rectangle if there is no intersection.
989
990 \sa operator&=(), intersected()
991*/
992
993QRect QRect::operator&(const QRect &r) const noexcept
994{
995 if (isNull() || r.isNull())
996 return QRect();
997
998 int l1 = x1;
999 int r1 = x2;
1000 if (x2 < x1 - 1) {
1001 l1 = x2 + 1;
1002 r1 = x1 - 1;
1003 }
1004
1005 int l2 = r.x1;
1006 int r2 = r.x2;
1007 if (r.x2 < r.x1 - 1) {
1008 l2 = r.x2 + 1;
1009 r2 = r.x1 - 1;
1010 }
1011
1012 if (l1 > r2 || l2 > r1)
1013 return QRect();
1014
1015 int t1 = y1;
1016 int b1 = y2;
1017 if (y2 < y1 - 1) {
1018 t1 = y2 + 1;
1019 b1 = y1 - 1;
1020 }
1021
1022 int t2 = r.y1;
1023 int b2 = r.y2;
1024 if (r.y2 < r.y1 - 1) {
1025 t2 = r.y2 + 1;
1026 b2 = r.y1 - 1;
1027 }
1028
1029 if (t1 > b2 || t2 > b1)
1030 return QRect();
1031
1032 QRect tmp;
1033 tmp.x1 = qMax(a: l1, b: l2);
1034 tmp.x2 = qMin(a: r1, b: r2);
1035 tmp.y1 = qMax(a: t1, b: t2);
1036 tmp.y2 = qMin(a: b1, b: b2);
1037 return tmp;
1038}
1039
1040/*!
1041 \fn QRect QRect::intersected(const QRect &rectangle) const
1042 \since 4.2
1043
1044 Returns the intersection of this rectangle and the given \a
1045 rectangle. Note that \c{r.intersected(s)} is equivalent to \c{r & s}.
1046
1047 \image qrect-intersect.png
1048
1049 \sa intersects(), united(), operator&=()
1050*/
1051
1052/*!
1053 \fn bool QRect::intersects(const QRect &rectangle) const
1054
1055 Returns \c true if this rectangle intersects with the given \a
1056 rectangle (i.e., there is at least one pixel that is within both
1057 rectangles), otherwise returns \c false.
1058
1059 The intersection rectangle can be retrieved using the intersected()
1060 function.
1061
1062 \sa contains()
1063*/
1064
1065bool QRect::intersects(const QRect &r) const noexcept
1066{
1067 if (isNull() || r.isNull())
1068 return false;
1069
1070 int l1 = x1;
1071 int r1 = x2;
1072 if (x2 < x1 - 1) {
1073 l1 = x2 + 1;
1074 r1 = x1 - 1;
1075 }
1076
1077 int l2 = r.x1;
1078 int r2 = r.x2;
1079 if (r.x2 < r.x1 - 1) {
1080 l2 = r.x2 + 1;
1081 r2 = r.x1 - 1;
1082 }
1083
1084 if (l1 > r2 || l2 > r1)
1085 return false;
1086
1087 int t1 = y1;
1088 int b1 = y2;
1089 if (y2 < y1 - 1) {
1090 t1 = y2 + 1;
1091 b1 = y1 - 1;
1092 }
1093
1094 int t2 = r.y1;
1095 int b2 = r.y2;
1096 if (r.y2 < r.y1 - 1) {
1097 t2 = r.y2 + 1;
1098 b2 = r.y1 - 1;
1099 }
1100
1101 if (t1 > b2 || t2 > b1)
1102 return false;
1103
1104 return true;
1105}
1106
1107/*!
1108 \fn bool QRect::operator==(const QRect &r1, const QRect &r2)
1109
1110 Returns \c true if the rectangles \a r1 and \a r2 are equal,
1111 otherwise returns \c false.
1112*/
1113
1114
1115/*!
1116 \fn bool QRect::operator!=(const QRect &r1, const QRect &r2)
1117
1118 Returns \c true if the rectangles \a r1 and \a r2 are different, otherwise
1119 returns \c false.
1120*/
1121
1122/*!
1123 \fn QRect operator+(const QRect &rectangle, const QMargins &margins)
1124 \relates QRect
1125
1126 Returns the \a rectangle grown by the \a margins.
1127
1128 \since 5.1
1129*/
1130
1131/*!
1132 \fn QRect operator+(const QMargins &margins, const QRect &rectangle)
1133 \relates QRect
1134 \overload
1135
1136 Returns the \a rectangle grown by the \a margins.
1137
1138 \since 5.1
1139*/
1140
1141/*!
1142 \fn QRect operator-(const QRect &lhs, const QMargins &rhs)
1143 \relates QRect
1144
1145 Returns the \a lhs rectangle shrunk by the \a rhs margins.
1146
1147 \since 5.3
1148*/
1149
1150/*!
1151 \fn QRect QRect::marginsAdded(const QMargins &margins) const
1152
1153 Returns a rectangle grown by the \a margins.
1154
1155 \sa operator+=(), marginsRemoved(), operator-=()
1156
1157 \since 5.1
1158*/
1159
1160/*!
1161 \fn QRect QRect::operator+=(const QMargins &margins)
1162
1163 Adds the \a margins to the rectangle, growing it.
1164
1165 \sa marginsAdded(), marginsRemoved(), operator-=()
1166
1167 \since 5.1
1168*/
1169
1170/*!
1171 \fn QRect QRect::marginsRemoved(const QMargins &margins) const
1172
1173 Removes the \a margins from the rectangle, shrinking it.
1174
1175 \sa marginsAdded(), operator+=(), operator-=()
1176
1177 \since 5.1
1178*/
1179
1180/*!
1181 \fn QRect QRect::operator -=(const QMargins &margins)
1182
1183 Returns a rectangle shrunk by the \a margins.
1184
1185 \sa marginsRemoved(), operator+=(), marginsAdded()
1186
1187 \since 5.1
1188*/
1189
1190/*!
1191 \fn static QRect QRect::span(const QPoint &p1, const QPoint &p2)
1192
1193 Returns a rectangle spanning the two points \a p1 and \a p2, including both and
1194 everything in between.
1195
1196 \since 6.0
1197*/
1198
1199/*!
1200 \fn QRect::toRectF() const
1201 \since 6.4
1202
1203 Returns this rectangle as a rectangle with floating point accuracy.
1204
1205 \note This function, like the QRectF(QRect) constructor, preserves the
1206 size() of the rectangle, not its bottomRight() corner.
1207
1208 \sa QRectF::toRect()
1209*/
1210
1211/*****************************************************************************
1212 QRect stream functions
1213 *****************************************************************************/
1214#ifndef QT_NO_DATASTREAM
1215/*!
1216 \fn QDataStream &operator<<(QDataStream &stream, const QRect &rectangle)
1217 \relates QRect
1218
1219 Writes the given \a rectangle to the given \a stream, and returns
1220 a reference to the stream.
1221
1222 \sa {Serializing Qt Data Types}
1223*/
1224
1225QDataStream &operator<<(QDataStream &s, const QRect &r)
1226{
1227 if (s.version() == 1)
1228 s << (qint16)r.left() << (qint16)r.top()
1229 << (qint16)r.right() << (qint16)r.bottom();
1230 else
1231 s << (qint32)r.left() << (qint32)r.top()
1232 << (qint32)r.right() << (qint32)r.bottom();
1233 return s;
1234}
1235
1236/*!
1237 \fn QDataStream &operator>>(QDataStream &stream, QRect &rectangle)
1238 \relates QRect
1239
1240 Reads a rectangle from the given \a stream into the given \a
1241 rectangle, and returns a reference to the stream.
1242
1243 \sa {Serializing Qt Data Types}
1244*/
1245
1246QDataStream &operator>>(QDataStream &s, QRect &r)
1247{
1248 if (s.version() == 1) {
1249 qint16 x1, y1, x2, y2;
1250 s >> x1; s >> y1; s >> x2; s >> y2;
1251 r.setCoords(xp1: x1, yp1: y1, xp2: x2, yp2: y2);
1252 }
1253 else {
1254 qint32 x1, y1, x2, y2;
1255 s >> x1; s >> y1; s >> x2; s >> y2;
1256 r.setCoords(xp1: x1, yp1: y1, xp2: x2, yp2: y2);
1257 }
1258 return s;
1259}
1260
1261#endif // QT_NO_DATASTREAM
1262
1263
1264#ifndef QT_NO_DEBUG_STREAM
1265QDebug operator<<(QDebug dbg, const QRect &r)
1266{
1267 QDebugStateSaver saver(dbg);
1268 dbg.nospace();
1269 dbg << "QRect" << '(';
1270 QtDebugUtils::formatQRect(debug&: dbg, rect: r);
1271 dbg << ')';
1272 return dbg;
1273}
1274#endif
1275
1276/*!
1277 \class QRectF
1278 \inmodule QtCore
1279 \ingroup painting
1280 \reentrant
1281
1282 \brief The QRectF class defines a finite rectangle in the plane using
1283 floating point precision.
1284
1285 A rectangle is normally expressed as a top-left corner and a
1286 size. The size (width and height) of a QRectF is always equivalent
1287 to the mathematical rectangle that forms the basis for its
1288 rendering.
1289
1290 A QRectF can be constructed with a set of left, top, width and
1291 height coordinates, or from a QPointF and a QSizeF. The following
1292 code creates two identical rectangles.
1293
1294 \snippet code/src_corelib_tools_qrect.cpp 1
1295
1296 There is also a third constructor creating a QRectF from a QRect,
1297 and a corresponding toRect() function that returns a QRect object
1298 based on the values of this rectangle (note that the coordinates
1299 in the returned rectangle are rounded to the nearest integer).
1300
1301 The QRectF class provides a collection of functions that return
1302 the various rectangle coordinates, and enable manipulation of
1303 these. QRectF also provides functions to move the rectangle
1304 relative to the various coordinates. In addition there is a
1305 moveTo() function that moves the rectangle, leaving its top left
1306 corner at the given coordinates. Alternatively, the translate()
1307 function moves the rectangle the given offset relative to the
1308 current position, and the translated() function returns a
1309 translated copy of this rectangle.
1310
1311 The size() function returns the rectangle's dimensions as a
1312 QSizeF. The dimensions can also be retrieved separately using the
1313 width() and height() functions. To manipulate the dimensions use
1314 the setSize(), setWidth() or setHeight() functions. Alternatively,
1315 the size can be changed by applying either of the functions
1316 setting the rectangle coordinates, for example, setBottom() or
1317 setRight().
1318
1319 The contains() function tells whether a given point is inside the
1320 rectangle or not, and the intersects() function returns \c true if
1321 this rectangle intersects with a given rectangle (otherwise
1322 false). The QRectF class also provides the intersected() function
1323 which returns the intersection rectangle, and the united() function
1324 which returns the rectangle that encloses the given rectangle and
1325 this:
1326
1327 \table
1328 \row
1329 \li \inlineimage qrect-intersect.png
1330 \li \inlineimage qrect-unite.png
1331 \row
1332 \li intersected()
1333 \li united()
1334 \endtable
1335
1336 The isEmpty() function returns \c true if the rectangle's width or
1337 height is less than, or equal to, 0. Note that an empty rectangle
1338 is not valid: The isValid() function returns \c true if both width
1339 and height is larger than 0. A null rectangle (isNull() == true)
1340 on the other hand, has both width and height set to 0.
1341
1342 Note that due to the way QRect and QRectF are defined, an
1343 empty QRectF is defined in essentially the same way as QRect.
1344
1345 Finally, QRectF objects can be streamed as well as compared.
1346
1347 \tableofcontents
1348
1349 \section1 Rendering
1350
1351 When using an \l {QPainter::Antialiasing}{anti-aliased} painter,
1352 the boundary line of a QRectF will be rendered symmetrically on both
1353 sides of the mathematical rectangle's boundary line. But when
1354 using an aliased painter (the default) other rules apply.
1355
1356 Then, when rendering with a one pixel wide pen the QRectF's boundary
1357 line will be rendered to the right and below the mathematical
1358 rectangle's boundary line.
1359
1360 When rendering with a two pixels wide pen the boundary line will
1361 be split in the middle by the mathematical rectangle. This will be
1362 the case whenever the pen is set to an even number of pixels,
1363 while rendering with a pen with an odd number of pixels, the spare
1364 pixel will be rendered to the right and below the mathematical
1365 rectangle as in the one pixel case.
1366
1367 \table
1368 \row
1369 \li \inlineimage qrect-diagram-zero.png
1370 \li \inlineimage qrectf-diagram-one.png
1371 \row
1372 \li Logical representation
1373 \li One pixel wide pen
1374 \row
1375 \li \inlineimage qrectf-diagram-two.png
1376 \li \inlineimage qrectf-diagram-three.png
1377 \row
1378 \li Two pixel wide pen
1379 \li Three pixel wide pen
1380 \endtable
1381
1382 \section1 Coordinates
1383
1384 The QRectF class provides a collection of functions that return
1385 the various rectangle coordinates, and enable manipulation of
1386 these. QRectF also provides functions to move the rectangle
1387 relative to the various coordinates.
1388
1389 For example: the bottom(), setBottom() and moveBottom() functions:
1390 bottom() returns the y-coordinate of the rectangle's bottom edge,
1391 setBottom() sets the bottom edge of the rectangle to the given y
1392 coordinate (it may change the height, but will never change the
1393 rectangle's top edge) and moveBottom() moves the entire rectangle
1394 vertically, leaving the rectangle's bottom edge at the given y
1395 coordinate and its size unchanged.
1396
1397 \image qrectf-coordinates.png
1398
1399 It is also possible to add offsets to this rectangle's coordinates
1400 using the adjust() function, as well as retrieve a new rectangle
1401 based on adjustments of the original one using the adjusted()
1402 function. If either of the width and height is negative, use the
1403 normalized() function to retrieve a rectangle where the corners
1404 are swapped.
1405
1406 In addition, QRectF provides the getCoords() function which extracts
1407 the position of the rectangle's top-left and bottom-right corner,
1408 and the getRect() function which extracts the rectangle's top-left
1409 corner, width and height. Use the setCoords() and setRect()
1410 function to manipulate the rectangle's coordinates and dimensions
1411 in one go.
1412
1413 \sa QRect, QRegion
1414*/
1415
1416/*****************************************************************************
1417 QRectF member functions
1418 *****************************************************************************/
1419
1420/*!
1421 \fn QRectF::QRectF()
1422
1423 Constructs a null rectangle.
1424
1425 \sa isNull()
1426*/
1427
1428/*!
1429 \fn QRectF::QRectF(const QPointF &topLeft, const QSizeF &size)
1430
1431 Constructs a rectangle with the given \a topLeft corner and the given \a size.
1432
1433 \sa setTopLeft(), setSize()
1434*/
1435
1436/*!
1437 \fn QRectF::QRectF(const QPointF &topLeft, const QPointF &bottomRight)
1438 \since 4.3
1439
1440 Constructs a rectangle with the given \a topLeft and \a bottomRight corners.
1441
1442 \sa setTopLeft(), setBottomRight()
1443*/
1444
1445/*!
1446 \fn QRectF::QRectF(qreal x, qreal y, qreal width, qreal height)
1447
1448 Constructs a rectangle with (\a x, \a y) as its top-left corner and the
1449 given \a width and \a height. All parameters must be finite.
1450
1451 \sa setRect()
1452*/
1453
1454/*!
1455 \fn QRectF::QRectF(const QRect &rectangle)
1456
1457 Constructs a QRectF rectangle from the given QRect \a rectangle.
1458
1459 \note This function, like QRect::toRectF(), preserves the size() of
1460 \a rectangle, not its bottomRight() corner.
1461
1462 \sa toRect(), QRect::toRectF()
1463*/
1464
1465/*!
1466 \fn bool QRectF::isNull() const
1467
1468 Returns \c true if the rectangle is a null rectangle, otherwise returns \c false.
1469
1470 A null rectangle has both the width and the height set to 0. A
1471 null rectangle is also empty, and hence not valid.
1472
1473 \sa isEmpty(), isValid()
1474*/
1475
1476/*!
1477 \fn bool QRectF::isEmpty() const
1478
1479 Returns \c true if the rectangle is empty, otherwise returns \c false.
1480
1481 An empty rectangle has width() <= 0 or height() <= 0. An empty
1482 rectangle is not valid (i.e., isEmpty() == !isValid()).
1483
1484 Use the normalized() function to retrieve a rectangle where the
1485 corners are swapped.
1486
1487 \sa isNull(), isValid(), normalized()
1488*/
1489
1490/*!
1491 \fn bool QRectF::isValid() const
1492
1493 Returns \c true if the rectangle is valid, otherwise returns \c false.
1494
1495 A valid rectangle has a width() > 0 and height() > 0. Note that
1496 non-trivial operations like intersections are not defined for
1497 invalid rectangles. A valid rectangle is not empty (i.e., isValid()
1498 == !isEmpty()).
1499
1500 \sa isNull(), isEmpty(), normalized()
1501*/
1502
1503
1504/*!
1505 Returns a normalized rectangle; i.e., a rectangle that has a
1506 non-negative width and height.
1507
1508 If width() < 0 the function swaps the left and right corners, and
1509 it swaps the top and bottom corners if height() < 0.
1510
1511 \sa isValid(), isEmpty()
1512*/
1513
1514QRectF QRectF::normalized() const noexcept
1515{
1516 QRectF r = *this;
1517 if (r.w < 0) {
1518 r.xp += r.w;
1519 r.w = -r.w;
1520 }
1521 if (r.h < 0) {
1522 r.yp += r.h;
1523 r.h = -r.h;
1524 }
1525 return r;
1526}
1527
1528/*!
1529 \fn qreal QRectF::x() const
1530
1531 Returns the x-coordinate of the rectangle's left edge. Equivalent
1532 to left().
1533
1534
1535 \sa setX(), y(), topLeft()
1536*/
1537
1538/*!
1539 \fn qreal QRectF::y() const
1540
1541 Returns the y-coordinate of the rectangle's top edge. Equivalent
1542 to top().
1543
1544 \sa setY(), x(), topLeft()
1545*/
1546
1547
1548/*!
1549 \fn void QRectF::setLeft(qreal x)
1550
1551 Sets the left edge of the rectangle to the given finite \a x
1552 coordinate. May change the width, but will never change the right
1553 edge of the rectangle.
1554
1555 Equivalent to setX().
1556
1557 \sa left(), moveLeft()
1558*/
1559
1560/*!
1561 \fn void QRectF::setTop(qreal y)
1562
1563 Sets the top edge of the rectangle to the given finite \a y coordinate. May
1564 change the height, but will never change the bottom edge of the
1565 rectangle.
1566
1567 Equivalent to setY().
1568
1569 \sa top(), moveTop()
1570*/
1571
1572/*!
1573 \fn void QRectF::setRight(qreal x)
1574
1575 Sets the right edge of the rectangle to the given finite \a x
1576 coordinate. May change the width, but will never change the left
1577 edge of the rectangle.
1578
1579 \sa right(), moveRight()
1580*/
1581
1582/*!
1583 \fn void QRectF::setBottom(qreal y)
1584
1585 Sets the bottom edge of the rectangle to the given finite \a y
1586 coordinate. May change the height, but will never change the top
1587 edge of the rectangle.
1588
1589 \sa bottom(), moveBottom()
1590*/
1591
1592/*!
1593 \fn void QRectF::setX(qreal x)
1594
1595 Sets the left edge of the rectangle to the given finite \a x
1596 coordinate. May change the width, but will never change the right
1597 edge of the rectangle.
1598
1599 Equivalent to setLeft().
1600
1601 \sa x(), setY(), setTopLeft()
1602*/
1603
1604/*!
1605 \fn void QRectF::setY(qreal y)
1606
1607 Sets the top edge of the rectangle to the given finite \a y
1608 coordinate. May change the height, but will never change the
1609 bottom edge of the rectangle.
1610
1611 Equivalent to setTop().
1612
1613 \sa y(), setX(), setTopLeft()
1614*/
1615
1616/*!
1617 \fn void QRectF::setTopLeft(const QPointF &position)
1618
1619 Set the top-left corner of the rectangle to the given \a
1620 position. May change the size, but will never change the
1621 bottom-right corner of the rectangle.
1622
1623 \sa topLeft(), moveTopLeft()
1624*/
1625
1626/*!
1627 \fn void QRectF::setBottomRight(const QPointF &position)
1628
1629 Set the bottom-right corner of the rectangle to the given \a
1630 position. May change the size, but will never change the
1631 top-left corner of the rectangle.
1632
1633 \sa bottomRight(), moveBottomRight()
1634*/
1635
1636/*!
1637 \fn void QRectF::setTopRight(const QPointF &position)
1638
1639 Set the top-right corner of the rectangle to the given \a
1640 position. May change the size, but will never change the
1641 bottom-left corner of the rectangle.
1642
1643 \sa topRight(), moveTopRight()
1644*/
1645
1646/*!
1647 \fn void QRectF::setBottomLeft(const QPointF &position)
1648
1649 Set the bottom-left corner of the rectangle to the given \a
1650 position. May change the size, but will never change the
1651 top-right corner of the rectangle.
1652
1653 \sa bottomLeft(), moveBottomLeft()
1654*/
1655
1656/*!
1657 \fn QPointF QRectF::center() const
1658
1659 Returns the center point of the rectangle.
1660
1661 \sa moveCenter()
1662*/
1663
1664
1665/*!
1666 \fn void QRectF::getRect(qreal *x, qreal *y, qreal *width, qreal *height) const
1667
1668 Extracts the position of the rectangle's top-left corner to *\a x and
1669 *\a y, and its dimensions to *\a width and *\a height.
1670
1671 \sa setRect(), getCoords()
1672*/
1673
1674
1675/*!
1676 \fn void QRectF::getCoords(qreal *x1, qreal *y1, qreal *x2, qreal *y2) const
1677
1678 Extracts the position of the rectangle's top-left corner to *\a x1
1679 and *\a y1, and the position of the bottom-right corner to *\a x2 and
1680 *\a y2.
1681
1682 \sa setCoords(), getRect()
1683*/
1684
1685/*!
1686 \fn void QRectF::moveLeft(qreal x)
1687
1688 Moves the rectangle horizontally, leaving the rectangle's left
1689 edge at the given finite \a x coordinate. The rectangle's size is
1690 unchanged.
1691
1692 \sa left(), setLeft(), moveRight()
1693*/
1694
1695/*!
1696 \fn void QRectF::moveTop(qreal y)
1697
1698 Moves the rectangle vertically, leaving the rectangle's top line
1699 at the given finite \a y coordinate. The rectangle's size is unchanged.
1700
1701 \sa top(), setTop(), moveBottom()
1702*/
1703
1704
1705/*!
1706 \fn void QRectF::moveRight(qreal x)
1707
1708 Moves the rectangle horizontally, leaving the rectangle's right
1709 edge at the given finite \a x coordinate. The rectangle's size is
1710 unchanged.
1711
1712 \sa right(), setRight(), moveLeft()
1713*/
1714
1715
1716/*!
1717 \fn void QRectF::moveBottom(qreal y)
1718
1719 Moves the rectangle vertically, leaving the rectangle's bottom
1720 edge at the given finite \a y coordinate. The rectangle's size is
1721 unchanged.
1722
1723 \sa bottom(), setBottom(), moveTop()
1724*/
1725
1726
1727/*!
1728 \fn void QRectF::moveTopLeft(const QPointF &position)
1729
1730 Moves the rectangle, leaving the top-left corner at the given \a
1731 position. The rectangle's size is unchanged.
1732
1733 \sa setTopLeft(), moveTop(), moveLeft()
1734*/
1735
1736
1737/*!
1738 \fn void QRectF::moveBottomRight(const QPointF &position)
1739
1740 Moves the rectangle, leaving the bottom-right corner at the given
1741 \a position. The rectangle's size is unchanged.
1742
1743 \sa setBottomRight(), moveBottom(), moveRight()
1744*/
1745
1746
1747/*!
1748 \fn void QRectF::moveTopRight(const QPointF &position)
1749
1750 Moves the rectangle, leaving the top-right corner at the given
1751 \a position. The rectangle's size is unchanged.
1752
1753 \sa setTopRight(), moveTop(), moveRight()
1754*/
1755
1756
1757/*!
1758 \fn void QRectF::moveBottomLeft(const QPointF &position)
1759
1760 Moves the rectangle, leaving the bottom-left corner at the given
1761 \a position. The rectangle's size is unchanged.
1762
1763 \sa setBottomLeft(), moveBottom(), moveLeft()
1764*/
1765
1766
1767/*!
1768 \fn void QRectF::moveTo(qreal x, qreal y)
1769
1770 Moves the rectangle, leaving the top-left corner at the given position (\a
1771 x, \a y). The rectangle's size is unchanged. Both parameters must be finite.
1772
1773 \sa translate(), moveTopLeft()
1774*/
1775
1776/*!
1777 \fn void QRectF::moveTo(const QPointF &position)
1778 \overload
1779
1780 Moves the rectangle, leaving the top-left corner at the given \a
1781 position.
1782*/
1783
1784/*!
1785 \fn void QRectF::translate(qreal dx, qreal dy)
1786
1787 Moves the rectangle \a dx along the x-axis and \a dy along the y-axis,
1788 relative to the current position. Positive values move the rectangle to the
1789 right and downwards. Both parameters must be finite.
1790
1791 \sa moveTopLeft(), moveTo(), translated()
1792*/
1793
1794
1795/*!
1796 \fn void QRectF::translate(const QPointF &offset)
1797 \overload
1798
1799 Moves the rectangle \a{offset}.\l{QPointF::x()}{x()} along the x
1800 axis and \a{offset}.\l{QPointF::y()}{y()} along the y axis,
1801 relative to the current position.
1802*/
1803
1804
1805/*!
1806 \fn QRectF QRectF::translated(qreal dx, qreal dy) const
1807
1808 Returns a copy of the rectangle that is translated \a dx along the
1809 x axis and \a dy along the y axis, relative to the current
1810 position. Positive values move the rectangle to the right and
1811 down. Both parameters must be finite.
1812
1813 \sa translate()
1814*/
1815
1816
1817/*!
1818 \fn QRectF QRectF::translated(const QPointF &offset) const
1819 \overload
1820
1821 Returns a copy of the rectangle that is translated
1822 \a{offset}.\l{QPointF::x()}{x()} along the x axis and
1823 \a{offset}.\l{QPointF::y()}{y()} along the y axis, relative to the
1824 current position.
1825*/
1826
1827/*!
1828 \fn QRectF QRectF::transposed() const
1829 \since 5.7
1830
1831 Returns a copy of the rectangle that has its width and height
1832 exchanged:
1833
1834 \snippet code/src_corelib_tools_qrect.cpp 3
1835
1836 \sa QSizeF::transposed()
1837*/
1838
1839/*!
1840 \fn void QRectF::setRect(qreal x, qreal y, qreal width, qreal height)
1841
1842 Sets the coordinates of the rectangle's top-left corner to (\a x, \a y), and
1843 its size to the given \a width and \a height. All parameters must be finite.
1844
1845 \sa getRect(), setCoords()
1846*/
1847
1848
1849/*!
1850 \fn void QRectF::setCoords(qreal x1, qreal y1, qreal x2, qreal y2)
1851
1852 Sets the coordinates of the rectangle's top-left corner to (\a x1,
1853 \a y1), and the coordinates of its bottom-right corner to (\a x2,
1854 \a y2). All parameters must be finite.
1855
1856 \sa getCoords(), setRect()
1857*/
1858
1859/*!
1860 \fn QRectF QRectF::adjusted(qreal dx1, qreal dy1, qreal dx2, qreal dy2) const
1861
1862 Returns a new rectangle with \a dx1, \a dy1, \a dx2 and \a dy2
1863 added respectively to the existing coordinates of this rectangle.
1864 All parameters must be finite.
1865
1866 \sa adjust()
1867*/
1868
1869/*! \fn void QRectF::adjust(qreal dx1, qreal dy1, qreal dx2, qreal dy2)
1870
1871 Adds \a dx1, \a dy1, \a dx2 and \a dy2 respectively to the
1872 existing coordinates of the rectangle. All parameters must be finite.
1873
1874 \sa adjusted(), setRect()
1875*/
1876/*!
1877 \fn QSizeF QRectF::size() const
1878
1879 Returns the size of the rectangle.
1880
1881 \sa setSize(), width(), height()
1882*/
1883
1884/*!
1885 \fn qreal QRectF::width() const
1886
1887 Returns the width of the rectangle.
1888
1889 \sa setWidth(), height(), size()
1890*/
1891
1892/*!
1893 \fn qreal QRectF::height() const
1894
1895 Returns the height of the rectangle.
1896
1897 \sa setHeight(), width(), size()
1898*/
1899
1900/*!
1901 \fn void QRectF::setWidth(qreal width)
1902
1903 Sets the width of the rectangle to the given finite \a width. The right
1904 edge is changed, but not the left one.
1905
1906 \sa width(), setSize()
1907*/
1908
1909
1910/*!
1911 \fn void QRectF::setHeight(qreal height)
1912
1913 Sets the height of the rectangle to the given finite \a height. The bottom
1914 edge is changed, but not the top one.
1915
1916 \sa height(), setSize()
1917*/
1918
1919
1920/*!
1921 \fn void QRectF::setSize(const QSizeF &size)
1922
1923 Sets the size of the rectangle to the given finite \a size. The top-left
1924 corner is not moved.
1925
1926 \sa size(), setWidth(), setHeight()
1927*/
1928
1929
1930/*!
1931 \fn bool QRectF::contains(const QPointF &point) const
1932
1933 Returns \c true if the given \a point is inside or on the edge of the
1934 rectangle; otherwise returns \c false.
1935
1936 \sa intersects()
1937*/
1938
1939bool QRectF::contains(const QPointF &p) const noexcept
1940{
1941 qreal l = xp;
1942 qreal r = xp;
1943 if (w < 0)
1944 l += w;
1945 else
1946 r += w;
1947 if (l == r) // null rect
1948 return false;
1949
1950 if (p.x() < l || p.x() > r)
1951 return false;
1952
1953 qreal t = yp;
1954 qreal b = yp;
1955 if (h < 0)
1956 t += h;
1957 else
1958 b += h;
1959 if (t == b) // null rect
1960 return false;
1961
1962 if (p.y() < t || p.y() > b)
1963 return false;
1964
1965 return true;
1966}
1967
1968
1969/*!
1970 \fn bool QRectF::contains(qreal x, qreal y) const
1971 \overload
1972
1973 Returns \c true if the point (\a x, \a y) is inside or on the edge of
1974 the rectangle; otherwise returns \c false.
1975*/
1976
1977/*!
1978 \fn bool QRectF::contains(const QRectF &rectangle) const
1979 \overload
1980
1981 Returns \c true if the given \a rectangle is inside this rectangle;
1982 otherwise returns \c false.
1983*/
1984
1985bool QRectF::contains(const QRectF &r) const noexcept
1986{
1987 qreal l1 = xp;
1988 qreal r1 = xp;
1989 if (w < 0)
1990 l1 += w;
1991 else
1992 r1 += w;
1993 if (l1 == r1) // null rect
1994 return false;
1995
1996 qreal l2 = r.xp;
1997 qreal r2 = r.xp;
1998 if (r.w < 0)
1999 l2 += r.w;
2000 else
2001 r2 += r.w;
2002 if (l2 == r2) // null rect
2003 return false;
2004
2005 if (l2 < l1 || r2 > r1)
2006 return false;
2007
2008 qreal t1 = yp;
2009 qreal b1 = yp;
2010 if (h < 0)
2011 t1 += h;
2012 else
2013 b1 += h;
2014 if (t1 == b1) // null rect
2015 return false;
2016
2017 qreal t2 = r.yp;
2018 qreal b2 = r.yp;
2019 if (r.h < 0)
2020 t2 += r.h;
2021 else
2022 b2 += r.h;
2023 if (t2 == b2) // null rect
2024 return false;
2025
2026 if (t2 < t1 || b2 > b1)
2027 return false;
2028
2029 return true;
2030}
2031
2032/*!
2033 \fn qreal QRectF::left() const
2034
2035 Returns the x-coordinate of the rectangle's left edge. Equivalent
2036 to x().
2037
2038 \sa setLeft(), topLeft(), bottomLeft()
2039*/
2040
2041/*!
2042 \fn qreal QRectF::top() const
2043
2044 Returns the y-coordinate of the rectangle's top edge. Equivalent
2045 to y().
2046
2047 \sa setTop(), topLeft(), topRight()
2048*/
2049
2050/*!
2051 \fn qreal QRectF::right() const
2052
2053 Returns the x-coordinate of the rectangle's right edge.
2054
2055 \sa setRight(), topRight(), bottomRight()
2056*/
2057
2058/*!
2059 \fn qreal QRectF::bottom() const
2060
2061 Returns the y-coordinate of the rectangle's bottom edge.
2062
2063 \sa setBottom(), bottomLeft(), bottomRight()
2064*/
2065
2066/*!
2067 \fn QPointF QRectF::topLeft() const
2068
2069 Returns the position of the rectangle's top-left corner.
2070
2071 \sa setTopLeft(), top(), left()
2072*/
2073
2074/*!
2075 \fn QPointF QRectF::bottomRight() const
2076
2077 Returns the position of the rectangle's bottom-right corner.
2078
2079 \sa setBottomRight(), bottom(), right()
2080*/
2081
2082/*!
2083 \fn QPointF QRectF::topRight() const
2084
2085 Returns the position of the rectangle's top-right corner.
2086
2087 \sa setTopRight(), top(), right()
2088*/
2089
2090/*!
2091 \fn QPointF QRectF::bottomLeft() const
2092
2093 Returns the position of the rectangle's bottom-left corner.
2094
2095 \sa setBottomLeft(), bottom(), left()
2096*/
2097
2098/*!
2099 \fn QRectF& QRectF::operator|=(const QRectF &rectangle)
2100
2101 Unites this rectangle with the given \a rectangle.
2102
2103 \sa united(), operator|()
2104*/
2105
2106/*!
2107 \fn QRectF& QRectF::operator&=(const QRectF &rectangle)
2108
2109 Intersects this rectangle with the given \a rectangle.
2110
2111 \sa intersected(), operator&()
2112*/
2113
2114
2115/*!
2116 \fn QRectF QRectF::operator|(const QRectF &rectangle) const
2117
2118 Returns the bounding rectangle of this rectangle and the given \a rectangle.
2119
2120 \sa united(), operator|=()
2121*/
2122
2123QRectF QRectF::operator|(const QRectF &r) const noexcept
2124{
2125 if (isNull())
2126 return r;
2127 if (r.isNull())
2128 return *this;
2129
2130 qreal left = xp;
2131 qreal right = xp;
2132 if (w < 0)
2133 left += w;
2134 else
2135 right += w;
2136
2137 if (r.w < 0) {
2138 left = qMin(a: left, b: r.xp + r.w);
2139 right = qMax(a: right, b: r.xp);
2140 } else {
2141 left = qMin(a: left, b: r.xp);
2142 right = qMax(a: right, b: r.xp + r.w);
2143 }
2144
2145 qreal top = yp;
2146 qreal bottom = yp;
2147 if (h < 0)
2148 top += h;
2149 else
2150 bottom += h;
2151
2152 if (r.h < 0) {
2153 top = qMin(a: top, b: r.yp + r.h);
2154 bottom = qMax(a: bottom, b: r.yp);
2155 } else {
2156 top = qMin(a: top, b: r.yp);
2157 bottom = qMax(a: bottom, b: r.yp + r.h);
2158 }
2159
2160 return QRectF(left, top, right - left, bottom - top);
2161}
2162
2163/*!
2164 \fn QRectF QRectF::united(const QRectF &rectangle) const
2165 \since 4.2
2166
2167 Returns the bounding rectangle of this rectangle and the given \a
2168 rectangle.
2169
2170 \image qrect-unite.png
2171
2172 \sa intersected()
2173*/
2174
2175
2176/*!
2177 \fn QRectF QRectF::operator &(const QRectF &rectangle) const
2178
2179 Returns the intersection of this rectangle and the given \a
2180 rectangle. Returns an empty rectangle if there is no intersection.
2181
2182 \sa operator&=(), intersected()
2183*/
2184
2185QRectF QRectF::operator&(const QRectF &r) const noexcept
2186{
2187 qreal l1 = xp;
2188 qreal r1 = xp;
2189 if (w < 0)
2190 l1 += w;
2191 else
2192 r1 += w;
2193 if (l1 == r1) // null rect
2194 return QRectF();
2195
2196 qreal l2 = r.xp;
2197 qreal r2 = r.xp;
2198 if (r.w < 0)
2199 l2 += r.w;
2200 else
2201 r2 += r.w;
2202 if (l2 == r2) // null rect
2203 return QRectF();
2204
2205 if (l1 >= r2 || l2 >= r1)
2206 return QRectF();
2207
2208 qreal t1 = yp;
2209 qreal b1 = yp;
2210 if (h < 0)
2211 t1 += h;
2212 else
2213 b1 += h;
2214 if (t1 == b1) // null rect
2215 return QRectF();
2216
2217 qreal t2 = r.yp;
2218 qreal b2 = r.yp;
2219 if (r.h < 0)
2220 t2 += r.h;
2221 else
2222 b2 += r.h;
2223 if (t2 == b2) // null rect
2224 return QRectF();
2225
2226 if (t1 >= b2 || t2 >= b1)
2227 return QRectF();
2228
2229 QRectF tmp;
2230 tmp.xp = qMax(a: l1, b: l2);
2231 tmp.yp = qMax(a: t1, b: t2);
2232 tmp.w = qMin(a: r1, b: r2) - tmp.xp;
2233 tmp.h = qMin(a: b1, b: b2) - tmp.yp;
2234 return tmp;
2235}
2236
2237/*!
2238 \fn QRectF QRectF::intersected(const QRectF &rectangle) const
2239 \since 4.2
2240
2241 Returns the intersection of this rectangle and the given \a
2242 rectangle. Note that \c {r.intersected(s)} is equivalent to \c
2243 {r & s}.
2244
2245 \image qrect-intersect.png
2246
2247 \sa intersects(), united(), operator&=()
2248*/
2249
2250/*!
2251 \fn bool QRectF::intersects(const QRectF &rectangle) const
2252
2253 Returns \c true if this rectangle intersects with the given \a
2254 rectangle (i.e. there is a non-empty area of overlap between
2255 them), otherwise returns \c false.
2256
2257 The intersection rectangle can be retrieved using the intersected()
2258 function.
2259
2260 \sa contains()
2261*/
2262
2263bool QRectF::intersects(const QRectF &r) const noexcept
2264{
2265 qreal l1 = xp;
2266 qreal r1 = xp;
2267 if (w < 0)
2268 l1 += w;
2269 else
2270 r1 += w;
2271 if (l1 == r1) // null rect
2272 return false;
2273
2274 qreal l2 = r.xp;
2275 qreal r2 = r.xp;
2276 if (r.w < 0)
2277 l2 += r.w;
2278 else
2279 r2 += r.w;
2280 if (l2 == r2) // null rect
2281 return false;
2282
2283 if (l1 >= r2 || l2 >= r1)
2284 return false;
2285
2286 qreal t1 = yp;
2287 qreal b1 = yp;
2288 if (h < 0)
2289 t1 += h;
2290 else
2291 b1 += h;
2292 if (t1 == b1) // null rect
2293 return false;
2294
2295 qreal t2 = r.yp;
2296 qreal b2 = r.yp;
2297 if (r.h < 0)
2298 t2 += r.h;
2299 else
2300 b2 += r.h;
2301 if (t2 == b2) // null rect
2302 return false;
2303
2304 if (t1 >= b2 || t2 >= b1)
2305 return false;
2306
2307 return true;
2308}
2309
2310/*!
2311 \fn QRect QRectF::toRect() const
2312
2313 Returns a QRect based on the values of this rectangle. Note that the
2314 coordinates in the returned rectangle are rounded to the nearest integer.
2315
2316 \sa QRectF(), toAlignedRect(), QRect::toRectF()
2317*/
2318
2319/*!
2320 \fn QRect QRectF::toAlignedRect() const
2321 \since 4.3
2322
2323 Returns a QRect based on the values of this rectangle that is the
2324 smallest possible integer rectangle that completely contains this
2325 rectangle.
2326
2327 \sa toRect()
2328*/
2329
2330QRect QRectF::toAlignedRect() const noexcept
2331{
2332 int xmin = int(qFloor(v: xp));
2333 int xmax = int(qCeil(v: xp + w));
2334 int ymin = int(qFloor(v: yp));
2335 int ymax = int(qCeil(v: yp + h));
2336 return QRect(xmin, ymin, xmax - xmin, ymax - ymin);
2337}
2338
2339/*!
2340 \fn void QRectF::moveCenter(const QPointF &position)
2341
2342 Moves the rectangle, leaving the center point at the given \a
2343 position. The rectangle's size is unchanged.
2344
2345 \sa center()
2346*/
2347
2348/*!
2349 \fn bool QRectF::operator==(const QRectF &r1, const QRectF &r2)
2350
2351 Returns \c true if the rectangles \a r1 and \a r2 are \b approximately equal,
2352 otherwise returns \c false.
2353
2354 \warning This function does not check for strict equality; instead,
2355 it uses a fuzzy comparison to compare the rectangles' coordinates.
2356
2357 \sa qFuzzyCompare
2358*/
2359
2360
2361/*!
2362 \fn bool QRectF::operator!=(const QRectF &r1, const QRectF &r2)
2363
2364 Returns \c true if the rectangles \a r1 and \a r2 are sufficiently
2365 different, otherwise returns \c false.
2366
2367 \warning This function does not check for strict inequality; instead,
2368 it uses a fuzzy comparison to compare the rectangles' coordinates.
2369*/
2370
2371/*!
2372 \fn QRectF operator+(const QRectF &lhs, const QMarginsF &rhs)
2373 \relates QRectF
2374 \since 5.3
2375
2376 Returns the \a lhs rectangle grown by the \a rhs margins.
2377*/
2378
2379/*!
2380 \fn QRectF operator-(const QRectF &lhs, const QMarginsF &rhs)
2381 \relates QRectF
2382 \since 5.3
2383
2384 Returns the \a lhs rectangle shrunk by the \a rhs margins.
2385*/
2386
2387/*!
2388 \fn QRectF operator+(const QMarginsF &lhs, const QRectF &rhs)
2389 \relates QRectF
2390 \overload
2391 \since 5.3
2392
2393 Returns the \a lhs rectangle grown by the \a rhs margins.
2394*/
2395
2396/*!
2397 \fn QRectF QRectF::marginsAdded(const QMarginsF &margins) const
2398 \since 5.3
2399
2400 Returns a rectangle grown by the \a margins.
2401
2402 \sa operator+=(), marginsRemoved(), operator-=()
2403*/
2404
2405/*!
2406 \fn QRectF QRectF::marginsRemoved(const QMarginsF &margins) const
2407 \since 5.3
2408
2409 Removes the \a margins from the rectangle, shrinking it.
2410
2411 \sa marginsAdded(), operator+=(), operator-=()
2412*/
2413
2414/*!
2415 \fn QRectF QRectF::operator+=(const QMarginsF &margins)
2416 \since 5.3
2417
2418 Adds the \a margins to the rectangle, growing it.
2419
2420 \sa marginsAdded(), marginsRemoved(), operator-=()
2421*/
2422
2423/*!
2424 \fn QRectF QRectF::operator-=(const QMarginsF &margins)
2425 \since 5.3
2426
2427 Returns a rectangle shrunk by the \a margins.
2428
2429 \sa marginsRemoved(), operator+=(), marginsAdded()
2430*/
2431
2432/*****************************************************************************
2433 QRectF stream functions
2434 *****************************************************************************/
2435#ifndef QT_NO_DATASTREAM
2436/*!
2437 \fn QDataStream &operator<<(QDataStream &stream, const QRectF &rectangle)
2438
2439 \relates QRectF
2440
2441 Writes the \a rectangle to the \a stream, and returns a reference to the
2442 stream.
2443
2444 \sa {Serializing Qt Data Types}
2445*/
2446
2447QDataStream &operator<<(QDataStream &s, const QRectF &r)
2448{
2449 s << double(r.x()) << double(r.y()) << double(r.width()) << double(r.height());
2450 return s;
2451}
2452
2453/*!
2454 \fn QDataStream &operator>>(QDataStream &stream, QRectF &rectangle)
2455
2456 \relates QRectF
2457
2458 Reads a \a rectangle from the \a stream, and returns a reference to the
2459 stream.
2460
2461 \sa {Serializing Qt Data Types}
2462*/
2463
2464QDataStream &operator>>(QDataStream &s, QRectF &r)
2465{
2466 double x, y, w, h;
2467 s >> x;
2468 s >> y;
2469 s >> w;
2470 s >> h;
2471 r.setRect(ax: qreal(x), ay: qreal(y), aaw: qreal(w), aah: qreal(h));
2472 return s;
2473}
2474
2475#endif // QT_NO_DATASTREAM
2476
2477
2478#ifndef QT_NO_DEBUG_STREAM
2479QDebug operator<<(QDebug dbg, const QRectF &r)
2480{
2481 QDebugStateSaver saver(dbg);
2482 dbg.nospace();
2483 dbg << "QRectF" << '(';
2484 QtDebugUtils::formatQRect(debug&: dbg, rect: r);
2485 dbg << ')';
2486 return dbg;
2487}
2488#endif
2489
2490QT_END_NAMESPACE
2491

source code of qtbase/src/corelib/tools/qrect.cpp