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