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