1 /****************************************************************************
2 ** $Id: qt/qrect.h   3.3.8   edited Jan 11 14:38 $
3 **
4 ** Definition of QRect class
5 **
6 ** Created : 931028
7 **
8 ** Copyright (C) 1992-2007 Trolltech ASA.  All rights reserved.
9 **
10 ** This file is part of the kernel module of the Qt GUI Toolkit.
11 **
12 ** This file may be distributed under the terms of the Q Public License
13 ** as defined by Trolltech ASA of Norway and appearing in the file
14 ** LICENSE.QPL included in the packaging of this file.
15 **
16 ** This file may be distributed and/or modified under the terms of the
17 ** GNU General Public License version 2 as published by the Free Software
18 ** Foundation and appearing in the file LICENSE.GPL included in the
19 ** packaging of this file.
20 **
21 ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
22 ** licenses may use this file in accordance with the Qt Commercial License
23 ** Agreement provided with the Software.
24 **
25 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
26 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27 **
28 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
29 **   information about Qt Commercial License Agreements.
30 ** See http://www.trolltech.com/qpl/ for QPL licensing information.
31 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
32 **
33 ** Contact info@trolltech.com if any conditions of this licensing are
34 ** not clear to you.
35 **
36 **********************************************************************/
37 
38 #ifndef QRECT_H
39 #define QRECT_H
40 
41 #ifndef QT_H
42 #include "qsize.h"
43 #endif // QT_H
44 
45 #if defined(topLeft)
46 #error "Macro definition of topLeft conflicts with QRect"
47 // don't just silently undo people's defines: #undef topLeft
48 #endif
49 
50 class Q_EXPORT QRect					// rectangle class
51 {
52 public:
QRect()53     QRect()	{ x1 = y1 = 0; x2 = y2 = -1; }
54     QRect( const QPoint &topleft, const QPoint &bottomright );
55     QRect( const QPoint &topleft, const QSize &size );
56     QRect( int left, int top, int width, int height );
57 
58     bool   isNull()	const;
59     bool   isEmpty()	const;
60     bool   isValid()	const;
61     QRect  normalize()	const;
62 
63     int	   left()	const;
64     int	   top()	const;
65     int	   right()	const;
66     int	   bottom()	const;
67 
68     QCOORD &rLeft();
69     QCOORD &rTop();
70     QCOORD &rRight();
71     QCOORD &rBottom();
72 
73     int	   x()		const;
74     int	   y()		const;
75     void   setLeft( int pos );
76     void   setTop( int pos );
77     void   setRight( int pos );
78     void   setBottom( int pos );
79     void   setX( int x );
80     void   setY( int y );
81 
82     void   setTopLeft( const QPoint &p );
83     void   setBottomRight( const QPoint &p );
84     void   setTopRight( const QPoint &p );
85     void   setBottomLeft( const QPoint &p );
86 
87     QPoint topLeft()	 const;
88     QPoint bottomRight() const;
89     QPoint topRight()	 const;
90     QPoint bottomLeft()	 const;
91     QPoint center()	 const;
92 
93     void   rect( int *x, int *y, int *w, int *h ) const;
94     void   coords( int *x1, int *y1, int *x2, int *y2 ) const;
95 
96     void   moveLeft( int pos );
97     void   moveTop( int pos );
98     void   moveRight( int pos );
99     void   moveBottom( int pos );
100     void   moveTopLeft( const QPoint &p );
101     void   moveBottomRight( const QPoint &p );
102     void   moveTopRight( const QPoint &p );
103     void   moveBottomLeft( const QPoint &p );
104     void   moveCenter( const QPoint &p );
105     void   moveBy( int dx, int dy );
106 
107     void   setRect( int x, int y, int w, int h );
108     void   setCoords( int x1, int y1, int x2, int y2 );
109     void   addCoords( int x1, int y1, int x2, int y2 );
110 
111     QSize  size()	const;
112     int	   width()	const;
113     int	   height()	const;
114     void   setWidth( int w );
115     void   setHeight( int h );
116     void   setSize( const QSize &s );
117 
118     QRect  operator|(const QRect &r) const;
119     QRect  operator&(const QRect &r) const;
120     QRect&  operator|=(const QRect &r);
121     QRect&  operator&=(const QRect &r);
122 
123     bool   contains( const QPoint &p, bool proper=FALSE ) const;
124     bool   contains( int x, int y ) const; // inline methods, _don't_ merge these
125     bool   contains( int x, int y, bool proper ) const;
126     bool   contains( const QRect &r, bool proper=FALSE ) const;
127     QRect  unite( const QRect &r ) const;
128     QRect  intersect( const QRect &r ) const;
129     bool   intersects( const QRect &r ) const;
130 
131     friend Q_EXPORT bool operator==( const QRect &, const QRect & );
132     friend Q_EXPORT bool operator!=( const QRect &, const QRect & );
133 
134 private:
135 #if defined(Q_WS_X11) || defined(Q_OS_TEMP)
136     friend void qt_setCoords( QRect *r, int xp1, int yp1, int xp2, int yp2 );
137 #endif
138 #if defined(Q_OS_MAC)
139     QCOORD y1;
140     QCOORD x1;
141     QCOORD y2;
142     QCOORD x2;
143 #else
144     QCOORD x1;
145     QCOORD y1;
146     QCOORD x2;
147     QCOORD y2;
148 #endif
149 };
150 
151 Q_EXPORT bool operator==( const QRect &, const QRect & );
152 Q_EXPORT bool operator!=( const QRect &, const QRect & );
153 
154 
155 /*****************************************************************************
156   QRect stream functions
157  *****************************************************************************/
158 #ifndef QT_NO_DATASTREAM
159 Q_EXPORT QDataStream &operator<<( QDataStream &, const QRect & );
160 Q_EXPORT QDataStream &operator>>( QDataStream &, QRect & );
161 #endif
162 
163 /*****************************************************************************
164   QRect inline member functions
165  *****************************************************************************/
166 
QRect(int left,int top,int width,int height)167 inline QRect::QRect( int left, int top, int width, int height )
168 {
169     x1 = (QCOORD)left;
170     y1 = (QCOORD)top;
171     x2 = (QCOORD)(left+width-1);
172     y2 = (QCOORD)(top+height-1);
173 }
174 
isNull()175 inline bool QRect::isNull() const
176 { return x2 == x1-1 && y2 == y1-1; }
177 
isEmpty()178 inline bool QRect::isEmpty() const
179 { return x1 > x2 || y1 > y2; }
180 
isValid()181 inline bool QRect::isValid() const
182 { return x1 <= x2 && y1 <= y2; }
183 
left()184 inline int QRect::left() const
185 { return x1; }
186 
top()187 inline int QRect::top() const
188 { return y1; }
189 
right()190 inline int QRect::right() const
191 { return x2; }
192 
bottom()193 inline int QRect::bottom() const
194 { return y2; }
195 
rLeft()196 inline QCOORD &QRect::rLeft()
197 { return x1; }
198 
rTop()199 inline QCOORD & QRect::rTop()
200 { return y1; }
201 
rRight()202 inline QCOORD & QRect::rRight()
203 { return x2; }
204 
rBottom()205 inline QCOORD & QRect::rBottom()
206 { return y2; }
207 
x()208 inline int QRect::x() const
209 { return x1; }
210 
y()211 inline int QRect::y() const
212 { return y1; }
213 
setLeft(int pos)214 inline void QRect::setLeft( int pos )
215 { x1 = (QCOORD)pos; }
216 
setTop(int pos)217 inline void QRect::setTop( int pos )
218 { y1 = (QCOORD)pos; }
219 
setRight(int pos)220 inline void QRect::setRight( int pos )
221 { x2 = (QCOORD)pos; }
222 
setBottom(int pos)223 inline void QRect::setBottom( int pos )
224 { y2 = (QCOORD)pos; }
225 
setX(int x)226 inline void QRect::setX( int x )
227 { x1 = (QCOORD)x; }
228 
setY(int y)229 inline void QRect::setY( int y )
230 { y1 = (QCOORD)y; }
231 
topLeft()232 inline QPoint QRect::topLeft() const
233 { return QPoint(x1, y1); }
234 
bottomRight()235 inline QPoint QRect::bottomRight() const
236 { return QPoint(x2, y2); }
237 
topRight()238 inline QPoint QRect::topRight() const
239 { return QPoint(x2, y1); }
240 
bottomLeft()241 inline QPoint QRect::bottomLeft() const
242 { return QPoint(x1, y2); }
243 
center()244 inline QPoint QRect::center() const
245 { return QPoint((x1+x2)/2, (y1+y2)/2); }
246 
width()247 inline int QRect::width() const
248 { return  x2 - x1 + 1; }
249 
height()250 inline int QRect::height() const
251 { return  y2 - y1 + 1; }
252 
size()253 inline QSize QRect::size() const
254 { return QSize(x2-x1+1, y2-y1+1); }
255 
contains(int x,int y,bool proper)256 inline bool QRect::contains( int x, int y, bool proper ) const
257 {
258     if ( proper )
259         return x > x1 && x < x2 &&
260                y > y1 && y < y2;
261     else
262         return x >= x1 && x <= x2 &&
263                y >= y1 && y <= y2;
264 }
265 
contains(int x,int y)266 inline bool QRect::contains( int x, int y ) const
267 {
268     return x >= x1 && x <= x2 &&
269 	   y >= y1 && y <= y2;
270 }
271 #define Q_DEFINED_QRECT
272 #include "qwinexport.h"
273 #endif // QRECT_H
274