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