1 /****************************************************************************
3 ** This file is based on sources of the Qt GUI Toolkit, used under the terms
4 ** of the GNU General Public License version 2 (see the original copyright
6 ** All further contributions to this file are (and are required to be)
7 ** licensed under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 of the License, or
9 ** (at your option) any later version.
11 ** The original Qt license header follows:
14 ** Implementation of QRect class
18 ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
20 ** This file is part of the kernel module of the Qt GUI Toolkit.
22 ** This file may be distributed under the terms of the Q Public License
23 ** as defined by Trolltech AS of Norway and appearing in the file
24 ** LICENSE.QPL included in the packaging of this file.
26 ** This file may be distributed and/or modified under the terms of the
27 ** GNU General Public License version 2 as published by the Free Software
28 ** Foundation and appearing in the file LICENSE.GPL included in the
29 ** packaging of this file.
31 ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
32 ** licenses may use this file in accordance with the Qt Commercial License
33 ** Agreement provided with the Software.
35 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
36 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
38 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
39 ** information about Qt Commercial License Agreements.
40 ** See http://www.trolltech.com/qpl/ for QPL licensing information.
41 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
43 ** Contact info@trolltech.com if any conditions of this licensing are
46 **********************************************************************/
50 #include "qdatastream.h"
54 \brief The QRect class defines a rectangle in the plane.
60 A rectangle is internally represented as an upper-left corner and
61 a bottom-right corner, but it is normally expressed as an
62 upper-left corner and a size.
64 The coordinate type is QCOORD (defined in \c qwindowdefs.h as \c
65 int). The minimum value of QCOORD is QCOORD_MIN (-2147483648) and
66 the maximum value is QCOORD_MAX (2147483647).
68 Note that the size (width and height) of a rectangle might be
69 different from what you are used to. If the top-left corner and
70 the bottom-right corner are the same, the height and the width of
71 the rectangle will both be 1.
73 Generally, \e{width = right - left + 1} and \e{height = bottom -
74 top + 1}. We designed it this way to make it correspond to
75 rectangular spaces used by drawing functions in which the width
76 and height denote a number of pixels. For example, drawing a
77 rectangle with width and height 1 draws a single pixel.
79 The default coordinate system has origin (0, 0) in the top-left
80 corner. The positive direction of the y axis is down, and the
81 positive x axis is from left to right.
83 A QRect can be constructed with a set of left, top, width and
84 height integers, from two QPoints or from a QPoint and a QSize.
85 After creation the dimensions can be changed, e.g. with setLeft(),
86 setRight(), setTop() and setBottom(), or by setting sizes, e.g.
87 setWidth(), setHeight() and setSize(). The dimensions can also be
88 changed with the move functions, e.g. moveBy(), moveCenter(),
89 moveBottomRight(), etc. You can also add coordinates to a
90 rectangle with addCoords().
92 You can test to see if a QRect contains a specific point with
93 contains(). You can also test to see if two QRects intersect with
94 intersects() (see also intersect()). To get the bounding rectangle
95 of two QRects use unite().
101 /*****************************************************************************
102 QRect member functions
103 *****************************************************************************/
108 Constructs an invalid rectangle.
112 Constructs a rectangle with \a topLeft as the top-left corner and
113 \a bottomRight as the bottom-right corner.
116 QRect::QRect( const QPoint
&topLeft
, const QPoint
&bottomRight
)
118 x1
= (QCOORD
)topLeft
.x();
119 y1
= (QCOORD
)topLeft
.y();
120 x2
= (QCOORD
)bottomRight
.x();
121 y2
= (QCOORD
)bottomRight
.y();
125 Constructs a rectangle with \a topLeft as the top-left corner and
126 \a size as the rectangle size.
129 QRect::QRect( const QPoint
&topLeft
, const QSize
&size
)
131 x1
= (QCOORD
)topLeft
.x();
132 y1
= (QCOORD
)topLeft
.y();
133 x2
= (QCOORD
)(x1
+size
.width()-1);
134 y2
= (QCOORD
)(y1
+size
.height()-1);
138 \fn QRect::QRect( int left, int top, int width, int height )
140 Constructs a rectangle with the \a top, \a left corner and \a
143 Example (creates three identical rectangles):
145 QRect r1( QPoint(100,200), QPoint(110,215) );
146 QRect r2( QPoint(100,200), QSize(11,16) );
147 QRect r3( 100, 200, 11, 16 );
153 \fn bool QRect::isNull() const
155 Returns TRUE if the rectangle is a null rectangle; otherwise
158 A null rectangle has both the width and the height set to 0, that
159 is right() == left() - 1 and bottom() == top() - 1.
161 Note that if right() == left() and bottom() == top(), then the
162 rectangle has width 1 and height 1.
164 A null rectangle is also empty.
166 A null rectangle is not valid.
168 \sa isEmpty(), isValid()
172 \fn bool QRect::isEmpty() const
174 Returns TRUE if the rectangle is empty; otherwise returns FALSE.
176 An empty rectangle has a left() \> right() or top() \> bottom().
178 An empty rectangle is not valid. \c{isEmpty() == !isValid()}
180 \sa isNull(), isValid(), normalize()
184 \fn bool QRect::isValid() const
186 Returns TRUE if the rectangle is valid; otherwise returns FALSE.
188 A valid rectangle has a left() \<= right() and top() \<= bottom().
190 Note that non-trivial operations like intersections are not defined
191 for invalid rectangles.
193 \c{isValid() == !isEmpty()}
195 \sa isNull(), isEmpty(), normalize()
200 Returns a normalized rectangle, i.e. a rectangle that has a
201 non-negative width and height.
203 It swaps left and right if left() \> right(), and swaps top and
204 bottom if top() \> bottom().
209 QRect
QRect::normalize() const
212 if ( x2
< x1
) { // swap bad x values
219 if ( y2
< y1
) { // swap bad y values
231 \fn int QRect::left() const
233 Returns the left coordinate of the rectangle. Identical to x().
235 \sa setLeft(), right(), topLeft(), bottomLeft()
239 \fn int QRect::top() const
241 Returns the top coordinate of the rectangle. Identical to y().
243 \sa setTop(), bottom(), topLeft(), topRight()
247 \fn int QRect::right() const
249 Returns the right coordinate of the rectangle.
251 \sa setRight(), left(), topRight(), bottomRight()
255 \fn int QRect::bottom() const
257 Returns the bottom coordinate of the rectangle.
259 \sa setBottom(), top(), bottomLeft(), bottomRight()
263 \fn QCOORD &QRect::rLeft()
265 Returns a reference to the left coordinate of the rectangle.
267 \sa rTop(), rRight(), rBottom()
271 \fn QCOORD &QRect::rTop()
273 Returns a reference to the top coordinate of the rectangle.
275 \sa rLeft(), rRight(), rBottom()
279 \fn QCOORD &QRect::rRight()
281 Returns a reference to the right coordinate of the rectangle.
283 \sa rLeft(), rTop(), rBottom()
287 \fn QCOORD &QRect::rBottom()
289 Returns a reference to the bottom coordinate of the rectangle.
291 \sa rLeft(), rTop(), rRight()
295 \fn int QRect::x() const
297 Returns the left coordinate of the rectangle. Identical to left().
299 \sa left(), y(), setX()
303 \fn int QRect::y() const
305 Returns the top coordinate of the rectangle. Identical to top().
307 \sa top(), x(), setY()
311 \fn void QRect::setLeft( int pos )
313 Sets the left edge of the rectangle to \a pos. May change the
314 width, but will never change the right edge of the rectangle.
318 \sa left(), setTop(), setWidth()
322 \fn void QRect::setTop( int pos )
324 Sets the top edge of the rectangle to \a pos. May change the
325 height, but will never change the bottom edge of the rectangle.
329 \sa top(), setBottom(), setHeight()
333 \fn void QRect::setRight( int pos )
335 Sets the right edge of the rectangle to \a pos. May change the
336 width, but will never change the left edge of the rectangle.
338 \sa right(), setLeft(), setWidth()
342 \fn void QRect::setBottom( int pos )
344 Sets the bottom edge of the rectangle to \a pos. May change the
345 height, but will never change the top edge of the rectangle.
347 \sa bottom(), setTop(), setHeight()
351 \fn void QRect::setX( int x )
353 Sets the x position of the rectangle (its left end) to \a x. May
354 change the width, but will never change the right edge of the
357 Identical to setLeft().
363 \fn void QRect::setY( int y )
365 Sets the y position of the rectangle (its top) to \a y. May change
366 the height, but will never change the bottom edge of the
369 Identical to setTop().
375 Set the top-left corner of the rectangle to \a p. May change
376 the size, but will the never change the bottom-right corner of
379 \sa topLeft(), moveTopLeft(), setBottomRight(), setTopRight(), setBottomLeft()
381 void QRect::setTopLeft( const QPoint
&p
)
388 Set the bottom-right corner of the rectangle to \a p. May change
389 the size, but will the never change the top-left corner of
392 \sa bottomRight(), moveBottomRight(), setTopLeft(), setTopRight(), setBottomLeft()
394 void QRect::setBottomRight( const QPoint
&p
)
401 Set the top-right corner of the rectangle to \a p. May change
402 the size, but will the never change the bottom-left corner of
405 \sa topRight(), moveTopRight(), setTopLeft(), setBottomRight(), setBottomLeft()
407 void QRect::setTopRight( const QPoint
&p
)
414 Set the bottom-left corner of the rectangle to \a p. May change
415 the size, but will the never change the top-right corner of
418 \sa bottomLeft(), moveBottomLeft(), setTopLeft(), setBottomRight(), setTopRight()
420 void QRect::setBottomLeft( const QPoint
&p
)
427 \fn QPoint QRect::topLeft() const
429 Returns the top-left position of the rectangle.
431 \sa setTopLeft(), moveTopLeft(), bottomRight(), left(), top()
435 \fn QPoint QRect::bottomRight() const
437 Returns the bottom-right position of the rectangle.
439 \sa setBottomRight(), moveBottomRight(), topLeft(), right(), bottom()
443 \fn QPoint QRect::topRight() const
445 Returns the top-right position of the rectangle.
447 \sa setTopRight(), moveTopRight(), bottomLeft(), top(), right()
451 \fn QPoint QRect::bottomLeft() const
453 Returns the bottom-left position of the rectangle.
455 \sa setBottomLeft(), moveBottomLeft(), topRight(), bottom(), left()
459 \fn QPoint QRect::center() const
461 Returns the center point of the rectangle.
463 \sa moveCenter(), topLeft(), bottomRight(), topRight(), bottomLeft()
468 Extracts the rectangle parameters as the position \a *x, \a *y and
469 width \a *w and height \a *h.
471 \sa setRect(), coords()
474 void QRect::rect( int *x
, int *y
, int *w
, int *h
) const
483 Extracts the rectangle parameters as the top-left point \a *xp1,
484 \a *yp1 and the bottom-right point \a *xp2, \a *yp2.
486 \sa setCoords(), rect()
489 void QRect::coords( int *xp1
, int *yp1
, int *xp2
, int *yp2
) const
499 Sets the left position of the rectangle to \a pos, leaving the
502 \sa left(), setLeft(), moveTop(), moveRight(), moveBottom()
504 void QRect::moveLeft( int pos
)
506 x2
+= (QCOORD
)(pos
- x1
);
511 Sets the top position of the rectangle to \a pos, leaving the
514 \sa top(), setTop(), moveLeft(), moveRight(), moveBottom()
517 void QRect::moveTop( int pos
)
519 y2
+= (QCOORD
)(pos
- y1
);
524 Sets the right position of the rectangle to \a pos, leaving the
527 \sa right(), setRight(), moveLeft(), moveTop(), moveBottom()
530 void QRect::moveRight( int pos
)
532 x1
+= (QCOORD
)(pos
- x2
);
537 Sets the bottom position of the rectangle to \a pos, leaving the
540 \sa bottom(), setBottom(), moveLeft(), moveTop(), moveRight()
543 void QRect::moveBottom( int pos
)
545 y1
+= (QCOORD
)(pos
- y2
);
550 Sets the top-left position of the rectangle to \a p, leaving the
553 \sa topLeft(), setTopLeft(), moveBottomRight(), moveTopRight(), moveBottomLeft()
556 void QRect::moveTopLeft( const QPoint
&p
)
563 Sets the bottom-right position of the rectangle to \a p, leaving
566 \sa bottomRight(), setBottomRight(), moveTopLeft(), moveTopRight(), moveBottomLeft()
569 void QRect::moveBottomRight( const QPoint
&p
)
576 Sets the top-right position of the rectangle to \a p, leaving the
579 \sa topRight(), setTopRight(), moveTopLeft(), moveBottomRight(), moveBottomLeft()
582 void QRect::moveTopRight( const QPoint
&p
)
589 Sets the bottom-left position of the rectangle to \a p, leaving
592 \sa bottomLeft(), setBottomLeft(), moveTopLeft(), moveBottomRight(), moveTopRight()
595 void QRect::moveBottomLeft( const QPoint
&p
)
603 Sets the center point of the rectangle to \a p, leaving the size
606 \sa center(), moveTopLeft(), moveBottomRight(), moveTopRight(), moveBottomLeft()
609 void QRect::moveCenter( const QPoint
&p
)
613 x1
= (QCOORD
)(p
.x() - w
/2);
614 y1
= (QCOORD
)(p
.y() - h
/2);
621 Moves the rectangle \a dx along the x axis and \a dy along the y
622 axis, relative to the current position. Positive values move the
623 rectangle to the right and down.
628 void QRect::moveBy( int dx
, int dy
)
637 Sets the coordinates of the rectangle's top-left corner to \a (x,
638 y), and its size to \a (w, h).
640 \sa rect(), setCoords()
643 void QRect::setRect( int x
, int y
, int w
, int h
)
647 x2
= (QCOORD
)(x
+w
-1);
648 y2
= (QCOORD
)(y
+h
-1);
652 Sets the coordinates of the rectangle's top-left corner to \a
653 (xp1, yp1), and the coordinates of its bottom-right corner to \a
656 \sa coords(), setRect()
659 void QRect::setCoords( int xp1
, int yp1
, int xp2
, int yp2
)
668 Adds \a xp1, \a yp1, \a xp2 and \a yp2 respectively to the
669 existing coordinates of the rectangle.
672 void QRect::addCoords( int xp1
, int yp1
, int xp2
, int yp2
)
681 \fn QSize QRect::size() const
683 Returns the size of the rectangle.
685 \sa width(), height()
689 \fn int QRect::width() const
691 Returns the width of the rectangle. The width includes both the
692 left and right edges, i.e. width = right - left + 1.
694 \sa height(), size(), setHeight()
698 \fn int QRect::height() const
700 Returns the height of the rectangle. The height includes both the
701 top and bottom edges, i.e. height = bottom - top + 1.
703 \sa width(), size(), setHeight()
707 Sets the width of the rectangle to \a w. The right edge is
708 changed, but not the left edge.
710 \sa width(), setLeft(), setRight(), setSize()
713 void QRect::setWidth( int w
)
715 x2
= (QCOORD
)(x1
+ w
- 1);
719 Sets the height of the rectangle to \a h. The top edge is not
720 moved, but the bottom edge may be moved.
722 \sa height(), setTop(), setBottom(), setSize()
725 void QRect::setHeight( int h
)
727 y2
= (QCOORD
)(y1
+ h
- 1);
731 Sets the size of the rectangle to \a s. The top-left corner is not
734 \sa size(), setWidth(), setHeight()
737 void QRect::setSize( const QSize
&s
)
739 x2
= (QCOORD
)(s
.width() +x1
-1);
740 y2
= (QCOORD
)(s
.height()+y1
-1);
744 Returns TRUE if the point \a p is inside or on the edge of the
745 rectangle; otherwise returns FALSE.
747 If \a proper is TRUE, this function returns TRUE only if \a p is
748 inside (not on the edge).
751 bool QRect::contains( const QPoint
&p
, bool proper
) const
754 return p
.x() > x1
&& p
.x() < x2
&&
755 p
.y() > y1
&& p
.y() < y2
;
757 return p
.x() >= x1
&& p
.x() <= x2
&&
758 p
.y() >= y1
&& p
.y() <= y2
;
762 \overload bool QRect::contains( int x, int y, bool proper ) const
764 Returns TRUE if the point \a x, \a y is inside this rectangle;
765 otherwise returns FALSE.
767 If \a proper is TRUE, this function returns TRUE only if the point
768 is entirely inside (not on the edge).
772 \overload bool QRect::contains( int x, int y ) const
774 Returns TRUE if the point \a x, \a y is inside this rectangle;
775 otherwise returns FALSE.
781 Returns TRUE if the rectangle \a r is inside this rectangle;
782 otherwise returns FALSE.
784 If \a proper is TRUE, this function returns TRUE only if \a r is
785 entirely inside (not on the edge).
787 \sa unite(), intersect(), intersects()
790 bool QRect::contains( const QRect
&r
, bool proper
) const
793 return r
.x1
> x1
&& r
.x2
< x2
&& r
.y1
> y1
&& r
.y2
< y2
;
795 return r
.x1
>= x1
&& r
.x2
<= x2
&& r
.y1
>= y1
&& r
.y2
<= y2
;
799 Unites this rectangle with rectangle \a r.
801 QRect
& QRect::operator|=(const QRect
&r
)
808 Intersects this rectangle with rectangle \a r.
810 QRect
& QRect::operator&=(const QRect
&r
)
818 Returns the bounding rectangle of this rectangle and rectangle \a
821 The bounding rectangle of a nonempty rectangle and an empty or
822 invalid rectangle is defined to be the nonempty rectangle.
824 \sa operator|=(), operator&(), intersects(), contains()
827 QRect
QRect::operator|(const QRect
&r
) const
832 tmp
.setLeft( qMin( x1
, r
.x1
) );
833 tmp
.setRight( qMax( x2
, r
.x2
) );
834 tmp
.setTop( qMin( y1
, r
.y1
) );
835 tmp
.setBottom( qMax( y2
, r
.y2
) );
846 Returns the bounding rectangle of this rectangle and rectangle \a
847 r. \c{r.unite(s)} is equivalent to \c{r|s}.
849 QRect
QRect::unite( const QRect
&r
) const
856 Returns the intersection of this rectangle and rectangle \a r.
858 Returns an empty rectangle if there is no intersection.
860 \sa operator&=(), operator|(), isEmpty(), intersects(), contains()
863 QRect
QRect::operator&( const QRect
&r
) const
866 tmp
.x1
= qMax( x1
, r
.x1
);
867 tmp
.x2
= qMin( x2
, r
.x2
);
868 tmp
.y1
= qMax( y1
, r
.y1
);
869 tmp
.y2
= qMin( y2
, r
.y2
);
874 Returns the intersection of this rectangle and rectangle \a r.
875 \c{r.intersect(s)} is equivalent to \c{r&s}.
877 QRect
QRect::intersect( const QRect
&r
) const
883 Returns TRUE if this rectangle intersects with rectangle \a r
884 (there is at least one pixel that is within both rectangles);
885 otherwise returns FALSE.
887 \sa intersect(), contains()
890 bool QRect::intersects( const QRect
&r
) const
892 return ( qMax( x1
, r
.x1
) <= qMin( x2
, r
.x2
) &&
893 qMax( y1
, r
.y1
) <= qMin( y2
, r
.y2
) );
900 Returns TRUE if \a r1 and \a r2 are equal; otherwise returns FALSE.
903 bool operator==( const QRect
&r1
, const QRect
&r2
)
905 return r1
.x1
==r2
.x1
&& r1
.x2
==r2
.x2
&& r1
.y1
==r2
.y1
&& r1
.y2
==r2
.y2
;
911 Returns TRUE if \a r1 and \a r2 are different; otherwise returns FALSE.
914 bool operator!=( const QRect
&r1
, const QRect
&r2
)
916 return r1
.x1
!=r2
.x1
|| r1
.x2
!=r2
.x2
|| r1
.y1
!=r2
.y1
|| r1
.y2
!=r2
.y2
;
920 /*****************************************************************************
921 QRect stream functions
922 *****************************************************************************/
923 #ifndef QT_NO_DATASTREAM
927 Writes the QRect, \a r, to the stream \a s, and returns a
928 reference to the stream.
930 \sa \link datastreamformat.html Format of the QDataStream operators \endlink
933 QDataStream
&operator<<( QDataStream
&s
, const QRect
&r
)
935 if ( s
.version() == 1 )
936 s
<< (Q_INT16
)r
.left() << (Q_INT16
)r
.top()
937 << (Q_INT16
)r
.right() << (Q_INT16
)r
.bottom();
939 s
<< (Q_INT32
)r
.left() << (Q_INT32
)r
.top()
940 << (Q_INT32
)r
.right() << (Q_INT32
)r
.bottom();
947 Reads a QRect from the stream \a s into rect \a r and returns a
948 reference to the stream.
950 \sa \link datastreamformat.html Format of the QDataStream operators \endlink
953 QDataStream
&operator>>( QDataStream
&s
, QRect
&r
)
955 if ( s
.version() == 1 ) {
956 Q_INT16 x1
, y1
, x2
, y2
;
957 s
>> x1
; s
>> y1
; s
>> x2
; s
>> y2
;
958 r
.setCoords( x1
, y1
, x2
, y2
);
961 Q_INT32 x1
, y1
, x2
, y2
;
962 s
>> x1
; s
>> y1
; s
>> x2
; s
>> y2
;
963 r
.setCoords( x1
, y1
, x2
, y2
);
967 #endif // QT_NO_DATASTREAM