1 | /****************************************************************************
|
---|
2 | ** $Id: qrect.cpp 2 2005-11-16 15:49:26Z dmik $
|
---|
3 | **
|
---|
4 | ** Implementation of QRect class
|
---|
5 | **
|
---|
6 | ** Created : 931028
|
---|
7 | **
|
---|
8 | ** Copyright (C) 1992-2000 Trolltech AS. 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 AS 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 | #define QRECT_C
|
---|
39 | #include "qrect.h"
|
---|
40 | #include "qdatastream.h"
|
---|
41 |
|
---|
42 | /*!
|
---|
43 | \class QRect
|
---|
44 | \brief The QRect class defines a rectangle in the plane.
|
---|
45 |
|
---|
46 | \ingroup images
|
---|
47 | \ingroup graphics
|
---|
48 | \mainclass
|
---|
49 |
|
---|
50 | A rectangle is internally represented as an upper-left corner and
|
---|
51 | a bottom-right corner, but it is normally expressed as an
|
---|
52 | upper-left corner and a size.
|
---|
53 |
|
---|
54 | The coordinate type is QCOORD (defined in \c qwindowdefs.h as \c
|
---|
55 | int). The minimum value of QCOORD is QCOORD_MIN (-2147483648) and
|
---|
56 | the maximum value is QCOORD_MAX (2147483647).
|
---|
57 |
|
---|
58 | Note that the size (width and height) of a rectangle might be
|
---|
59 | different from what you are used to. If the top-left corner and
|
---|
60 | the bottom-right corner are the same, the height and the width of
|
---|
61 | the rectangle will both be 1.
|
---|
62 |
|
---|
63 | Generally, \e{width = right - left + 1} and \e{height = bottom -
|
---|
64 | top + 1}. We designed it this way to make it correspond to
|
---|
65 | rectangular spaces used by drawing functions in which the width
|
---|
66 | and height denote a number of pixels. For example, drawing a
|
---|
67 | rectangle with width and height 1 draws a single pixel.
|
---|
68 |
|
---|
69 | The default coordinate system has origin (0, 0) in the top-left
|
---|
70 | corner. The positive direction of the y axis is down, and the
|
---|
71 | positive x axis is from left to right.
|
---|
72 |
|
---|
73 | A QRect can be constructed with a set of left, top, width and
|
---|
74 | height integers, from two QPoints or from a QPoint and a QSize.
|
---|
75 | After creation the dimensions can be changed, e.g. with setLeft(),
|
---|
76 | setRight(), setTop() and setBottom(), or by setting sizes, e.g.
|
---|
77 | setWidth(), setHeight() and setSize(). The dimensions can also be
|
---|
78 | changed with the move functions, e.g. moveBy(), moveCenter(),
|
---|
79 | moveBottomRight(), etc. You can also add coordinates to a
|
---|
80 | rectangle with addCoords().
|
---|
81 |
|
---|
82 | You can test to see if a QRect contains a specific point with
|
---|
83 | contains(). You can also test to see if two QRects intersect with
|
---|
84 | intersects() (see also intersect()). To get the bounding rectangle
|
---|
85 | of two QRects use unite().
|
---|
86 |
|
---|
87 | \sa QPoint, QSize
|
---|
88 | */
|
---|
89 |
|
---|
90 |
|
---|
91 | /*****************************************************************************
|
---|
92 | QRect member functions
|
---|
93 | *****************************************************************************/
|
---|
94 |
|
---|
95 | /*!
|
---|
96 | \fn QRect::QRect()
|
---|
97 |
|
---|
98 | Constructs an invalid rectangle.
|
---|
99 | */
|
---|
100 |
|
---|
101 | /*!
|
---|
102 | Constructs a rectangle with \a topLeft as the top-left corner and
|
---|
103 | \a bottomRight as the bottom-right corner.
|
---|
104 | */
|
---|
105 |
|
---|
106 | QRect::QRect( const QPoint &topLeft, const QPoint &bottomRight )
|
---|
107 | {
|
---|
108 | x1 = (QCOORD)topLeft.x();
|
---|
109 | y1 = (QCOORD)topLeft.y();
|
---|
110 | x2 = (QCOORD)bottomRight.x();
|
---|
111 | y2 = (QCOORD)bottomRight.y();
|
---|
112 | }
|
---|
113 |
|
---|
114 | /*!
|
---|
115 | Constructs a rectangle with \a topLeft as the top-left corner and
|
---|
116 | \a size as the rectangle size.
|
---|
117 | */
|
---|
118 |
|
---|
119 | QRect::QRect( const QPoint &topLeft, const QSize &size )
|
---|
120 | {
|
---|
121 | x1 = (QCOORD)topLeft.x();
|
---|
122 | y1 = (QCOORD)topLeft.y();
|
---|
123 | x2 = (QCOORD)(x1+size.width()-1);
|
---|
124 | y2 = (QCOORD)(y1+size.height()-1);
|
---|
125 | }
|
---|
126 |
|
---|
127 | /*!
|
---|
128 | \fn QRect::QRect( int left, int top, int width, int height )
|
---|
129 |
|
---|
130 | Constructs a rectangle with the \a top, \a left corner and \a
|
---|
131 | width and \a height.
|
---|
132 |
|
---|
133 | Example (creates three identical rectangles):
|
---|
134 | \code
|
---|
135 | QRect r1( QPoint(100,200), QPoint(110,215) );
|
---|
136 | QRect r2( QPoint(100,200), QSize(11,16) );
|
---|
137 | QRect r3( 100, 200, 11, 16 );
|
---|
138 | \endcode
|
---|
139 | */
|
---|
140 |
|
---|
141 |
|
---|
142 | /*!
|
---|
143 | \fn bool QRect::isNull() const
|
---|
144 |
|
---|
145 | Returns TRUE if the rectangle is a null rectangle; otherwise
|
---|
146 | returns FALSE.
|
---|
147 |
|
---|
148 | A null rectangle has both the width and the height set to 0, that
|
---|
149 | is right() == left() - 1 and bottom() == top() - 1.
|
---|
150 |
|
---|
151 | Note that if right() == left() and bottom() == top(), then the
|
---|
152 | rectangle has width 1 and height 1.
|
---|
153 |
|
---|
154 | A null rectangle is also empty.
|
---|
155 |
|
---|
156 | A null rectangle is not valid.
|
---|
157 |
|
---|
158 | \sa isEmpty(), isValid()
|
---|
159 | */
|
---|
160 |
|
---|
161 | /*!
|
---|
162 | \fn bool QRect::isEmpty() const
|
---|
163 |
|
---|
164 | Returns TRUE if the rectangle is empty; otherwise returns FALSE.
|
---|
165 |
|
---|
166 | An empty rectangle has a left() \> right() or top() \> bottom().
|
---|
167 |
|
---|
168 | An empty rectangle is not valid. \c{isEmpty() == !isValid()}
|
---|
169 |
|
---|
170 | \sa isNull(), isValid(), normalize()
|
---|
171 | */
|
---|
172 |
|
---|
173 | /*!
|
---|
174 | \fn bool QRect::isValid() const
|
---|
175 |
|
---|
176 | Returns TRUE if the rectangle is valid; otherwise returns FALSE.
|
---|
177 |
|
---|
178 | A valid rectangle has a left() \<= right() and top() \<= bottom().
|
---|
179 |
|
---|
180 | Note that non-trivial operations like intersections are not defined
|
---|
181 | for invalid rectangles.
|
---|
182 |
|
---|
183 | \c{isValid() == !isEmpty()}
|
---|
184 |
|
---|
185 | \sa isNull(), isEmpty(), normalize()
|
---|
186 | */
|
---|
187 |
|
---|
188 |
|
---|
189 | /*!
|
---|
190 | Returns a normalized rectangle, i.e. a rectangle that has a
|
---|
191 | non-negative width and height.
|
---|
192 |
|
---|
193 | It swaps left and right if left() \> right(), and swaps top and
|
---|
194 | bottom if top() \> bottom().
|
---|
195 |
|
---|
196 | \sa isValid()
|
---|
197 | */
|
---|
198 |
|
---|
199 | QRect QRect::normalize() const
|
---|
200 | {
|
---|
201 | QRect r;
|
---|
202 | if ( x2 < x1 ) { // swap bad x values
|
---|
203 | r.x1 = x2;
|
---|
204 | r.x2 = x1;
|
---|
205 | } else {
|
---|
206 | r.x1 = x1;
|
---|
207 | r.x2 = x2;
|
---|
208 | }
|
---|
209 | if ( y2 < y1 ) { // swap bad y values
|
---|
210 | r.y1 = y2;
|
---|
211 | r.y2 = y1;
|
---|
212 | } else {
|
---|
213 | r.y1 = y1;
|
---|
214 | r.y2 = y2;
|
---|
215 | }
|
---|
216 | return r;
|
---|
217 | }
|
---|
218 |
|
---|
219 |
|
---|
220 | /*!
|
---|
221 | \fn int QRect::left() const
|
---|
222 |
|
---|
223 | Returns the left coordinate of the rectangle. Identical to x().
|
---|
224 |
|
---|
225 | \sa setLeft(), right(), topLeft(), bottomLeft()
|
---|
226 | */
|
---|
227 |
|
---|
228 | /*!
|
---|
229 | \fn int QRect::top() const
|
---|
230 |
|
---|
231 | Returns the top coordinate of the rectangle. Identical to y().
|
---|
232 |
|
---|
233 | \sa setTop(), bottom(), topLeft(), topRight()
|
---|
234 | */
|
---|
235 |
|
---|
236 | /*!
|
---|
237 | \fn int QRect::right() const
|
---|
238 |
|
---|
239 | Returns the right coordinate of the rectangle.
|
---|
240 |
|
---|
241 | \sa setRight(), left(), topRight(), bottomRight()
|
---|
242 | */
|
---|
243 |
|
---|
244 | /*!
|
---|
245 | \fn int QRect::bottom() const
|
---|
246 |
|
---|
247 | Returns the bottom coordinate of the rectangle.
|
---|
248 |
|
---|
249 | \sa setBottom(), top(), bottomLeft(), bottomRight()
|
---|
250 | */
|
---|
251 |
|
---|
252 | /*!
|
---|
253 | \fn QCOORD &QRect::rLeft()
|
---|
254 |
|
---|
255 | Returns a reference to the left coordinate of the rectangle.
|
---|
256 |
|
---|
257 | \sa rTop(), rRight(), rBottom()
|
---|
258 | */
|
---|
259 |
|
---|
260 | /*!
|
---|
261 | \fn QCOORD &QRect::rTop()
|
---|
262 |
|
---|
263 | Returns a reference to the top coordinate of the rectangle.
|
---|
264 |
|
---|
265 | \sa rLeft(), rRight(), rBottom()
|
---|
266 | */
|
---|
267 |
|
---|
268 | /*!
|
---|
269 | \fn QCOORD &QRect::rRight()
|
---|
270 |
|
---|
271 | Returns a reference to the right coordinate of the rectangle.
|
---|
272 |
|
---|
273 | \sa rLeft(), rTop(), rBottom()
|
---|
274 | */
|
---|
275 |
|
---|
276 | /*!
|
---|
277 | \fn QCOORD &QRect::rBottom()
|
---|
278 |
|
---|
279 | Returns a reference to the bottom coordinate of the rectangle.
|
---|
280 |
|
---|
281 | \sa rLeft(), rTop(), rRight()
|
---|
282 | */
|
---|
283 |
|
---|
284 | /*!
|
---|
285 | \fn int QRect::x() const
|
---|
286 |
|
---|
287 | Returns the left coordinate of the rectangle. Identical to left().
|
---|
288 |
|
---|
289 | \sa left(), y(), setX()
|
---|
290 | */
|
---|
291 |
|
---|
292 | /*!
|
---|
293 | \fn int QRect::y() const
|
---|
294 |
|
---|
295 | Returns the top coordinate of the rectangle. Identical to top().
|
---|
296 |
|
---|
297 | \sa top(), x(), setY()
|
---|
298 | */
|
---|
299 |
|
---|
300 | /*!
|
---|
301 | \fn void QRect::setLeft( int pos )
|
---|
302 |
|
---|
303 | Sets the left edge of the rectangle to \a pos. May change the
|
---|
304 | width, but will never change the right edge of the rectangle.
|
---|
305 |
|
---|
306 | Identical to setX().
|
---|
307 |
|
---|
308 | \sa left(), setTop(), setWidth()
|
---|
309 | */
|
---|
310 |
|
---|
311 | /*!
|
---|
312 | \fn void QRect::setTop( int pos )
|
---|
313 |
|
---|
314 | Sets the top edge of the rectangle to \a pos. May change the
|
---|
315 | height, but will never change the bottom edge of the rectangle.
|
---|
316 |
|
---|
317 | Identical to setY().
|
---|
318 |
|
---|
319 | \sa top(), setBottom(), setHeight()
|
---|
320 | */
|
---|
321 |
|
---|
322 | /*!
|
---|
323 | \fn void QRect::setRight( int pos )
|
---|
324 |
|
---|
325 | Sets the right edge of the rectangle to \a pos. May change the
|
---|
326 | width, but will never change the left edge of the rectangle.
|
---|
327 |
|
---|
328 | \sa right(), setLeft(), setWidth()
|
---|
329 | */
|
---|
330 |
|
---|
331 | /*!
|
---|
332 | \fn void QRect::setBottom( int pos )
|
---|
333 |
|
---|
334 | Sets the bottom edge of the rectangle to \a pos. May change the
|
---|
335 | height, but will never change the top edge of the rectangle.
|
---|
336 |
|
---|
337 | \sa bottom(), setTop(), setHeight()
|
---|
338 | */
|
---|
339 |
|
---|
340 | /*!
|
---|
341 | \fn void QRect::setX( int x )
|
---|
342 |
|
---|
343 | Sets the x position of the rectangle (its left end) to \a x. May
|
---|
344 | change the width, but will never change the right edge of the
|
---|
345 | rectangle.
|
---|
346 |
|
---|
347 | Identical to setLeft().
|
---|
348 |
|
---|
349 | \sa x(), setY()
|
---|
350 | */
|
---|
351 |
|
---|
352 | /*!
|
---|
353 | \fn void QRect::setY( int y )
|
---|
354 |
|
---|
355 | Sets the y position of the rectangle (its top) to \a y. May change
|
---|
356 | the height, but will never change the bottom edge of the
|
---|
357 | rectangle.
|
---|
358 |
|
---|
359 | Identical to setTop().
|
---|
360 |
|
---|
361 | \sa y(), setX()
|
---|
362 | */
|
---|
363 |
|
---|
364 | /*!
|
---|
365 | Set the top-left corner of the rectangle to \a p. May change
|
---|
366 | the size, but will the never change the bottom-right corner of
|
---|
367 | the rectangle.
|
---|
368 |
|
---|
369 | \sa topLeft(), moveTopLeft(), setBottomRight(), setTopRight(), setBottomLeft()
|
---|
370 | */
|
---|
371 | void QRect::setTopLeft( const QPoint &p )
|
---|
372 | {
|
---|
373 | setLeft( p.x() );
|
---|
374 | setTop( p.y() );
|
---|
375 | }
|
---|
376 |
|
---|
377 | /*!
|
---|
378 | Set the bottom-right corner of the rectangle to \a p. May change
|
---|
379 | the size, but will the never change the top-left corner of
|
---|
380 | the rectangle.
|
---|
381 |
|
---|
382 | \sa bottomRight(), moveBottomRight(), setTopLeft(), setTopRight(), setBottomLeft()
|
---|
383 | */
|
---|
384 | void QRect::setBottomRight( const QPoint &p )
|
---|
385 | {
|
---|
386 | setRight( p.x() );
|
---|
387 | setBottom( p.y() );
|
---|
388 | }
|
---|
389 |
|
---|
390 | /*!
|
---|
391 | Set the top-right corner of the rectangle to \a p. May change
|
---|
392 | the size, but will the never change the bottom-left corner of
|
---|
393 | the rectangle.
|
---|
394 |
|
---|
395 | \sa topRight(), moveTopRight(), setTopLeft(), setBottomRight(), setBottomLeft()
|
---|
396 | */
|
---|
397 | void QRect::setTopRight( const QPoint &p )
|
---|
398 | {
|
---|
399 | setRight( p.x() );
|
---|
400 | setTop( p.y() );
|
---|
401 | }
|
---|
402 |
|
---|
403 | /*!
|
---|
404 | Set the bottom-left corner of the rectangle to \a p. May change
|
---|
405 | the size, but will the never change the top-right corner of
|
---|
406 | the rectangle.
|
---|
407 |
|
---|
408 | \sa bottomLeft(), moveBottomLeft(), setTopLeft(), setBottomRight(), setTopRight()
|
---|
409 | */
|
---|
410 | void QRect::setBottomLeft( const QPoint &p )
|
---|
411 | {
|
---|
412 | setLeft( p.x() );
|
---|
413 | setBottom( p.y() );
|
---|
414 | }
|
---|
415 |
|
---|
416 | /*!
|
---|
417 | \fn QPoint QRect::topLeft() const
|
---|
418 |
|
---|
419 | Returns the top-left position of the rectangle.
|
---|
420 |
|
---|
421 | \sa setTopLeft(), moveTopLeft(), bottomRight(), left(), top()
|
---|
422 | */
|
---|
423 |
|
---|
424 | /*!
|
---|
425 | \fn QPoint QRect::bottomRight() const
|
---|
426 |
|
---|
427 | Returns the bottom-right position of the rectangle.
|
---|
428 |
|
---|
429 | \sa setBottomRight(), moveBottomRight(), topLeft(), right(), bottom()
|
---|
430 | */
|
---|
431 |
|
---|
432 | /*!
|
---|
433 | \fn QPoint QRect::topRight() const
|
---|
434 |
|
---|
435 | Returns the top-right position of the rectangle.
|
---|
436 |
|
---|
437 | \sa setTopRight(), moveTopRight(), bottomLeft(), top(), right()
|
---|
438 | */
|
---|
439 |
|
---|
440 | /*!
|
---|
441 | \fn QPoint QRect::bottomLeft() const
|
---|
442 |
|
---|
443 | Returns the bottom-left position of the rectangle.
|
---|
444 |
|
---|
445 | \sa setBottomLeft(), moveBottomLeft(), topRight(), bottom(), left()
|
---|
446 | */
|
---|
447 |
|
---|
448 | /*!
|
---|
449 | \fn QPoint QRect::center() const
|
---|
450 |
|
---|
451 | Returns the center point of the rectangle.
|
---|
452 |
|
---|
453 | \sa moveCenter(), topLeft(), bottomRight(), topRight(), bottomLeft()
|
---|
454 | */
|
---|
455 |
|
---|
456 |
|
---|
457 | /*!
|
---|
458 | Extracts the rectangle parameters as the position \a *x, \a *y and
|
---|
459 | width \a *w and height \a *h.
|
---|
460 |
|
---|
461 | \sa setRect(), coords()
|
---|
462 | */
|
---|
463 |
|
---|
464 | void QRect::rect( int *x, int *y, int *w, int *h ) const
|
---|
465 | {
|
---|
466 | *x = x1;
|
---|
467 | *y = y1;
|
---|
468 | *w = x2-x1+1;
|
---|
469 | *h = y2-y1+1;
|
---|
470 | }
|
---|
471 |
|
---|
472 | /*!
|
---|
473 | Extracts the rectangle parameters as the top-left point \a *xp1,
|
---|
474 | \a *yp1 and the bottom-right point \a *xp2, \a *yp2.
|
---|
475 |
|
---|
476 | \sa setCoords(), rect()
|
---|
477 | */
|
---|
478 |
|
---|
479 | void QRect::coords( int *xp1, int *yp1, int *xp2, int *yp2 ) const
|
---|
480 | {
|
---|
481 | *xp1 = x1;
|
---|
482 | *yp1 = y1;
|
---|
483 | *xp2 = x2;
|
---|
484 | *yp2 = y2;
|
---|
485 | }
|
---|
486 |
|
---|
487 |
|
---|
488 | /*!
|
---|
489 | Sets the left position of the rectangle to \a pos, leaving the
|
---|
490 | size unchanged.
|
---|
491 |
|
---|
492 | \sa left(), setLeft(), moveTop(), moveRight(), moveBottom()
|
---|
493 | */
|
---|
494 | void QRect::moveLeft( int pos )
|
---|
495 | {
|
---|
496 | x2 += (QCOORD)(pos - x1);
|
---|
497 | x1 = (QCOORD)pos;
|
---|
498 | }
|
---|
499 |
|
---|
500 | /*!
|
---|
501 | Sets the top position of the rectangle to \a pos, leaving the
|
---|
502 | size unchanged.
|
---|
503 |
|
---|
504 | \sa top(), setTop(), moveLeft(), moveRight(), moveBottom()
|
---|
505 | */
|
---|
506 |
|
---|
507 | void QRect::moveTop( int pos )
|
---|
508 | {
|
---|
509 | y2 += (QCOORD)(pos - y1);
|
---|
510 | y1 = (QCOORD)pos;
|
---|
511 | }
|
---|
512 |
|
---|
513 | /*!
|
---|
514 | Sets the right position of the rectangle to \a pos, leaving the
|
---|
515 | size unchanged.
|
---|
516 |
|
---|
517 | \sa right(), setRight(), moveLeft(), moveTop(), moveBottom()
|
---|
518 | */
|
---|
519 |
|
---|
520 | void QRect::moveRight( int pos )
|
---|
521 | {
|
---|
522 | x1 += (QCOORD)(pos - x2);
|
---|
523 | x2 = (QCOORD)pos;
|
---|
524 | }
|
---|
525 |
|
---|
526 | /*!
|
---|
527 | Sets the bottom position of the rectangle to \a pos, leaving the
|
---|
528 | size unchanged.
|
---|
529 |
|
---|
530 | \sa bottom(), setBottom(), moveLeft(), moveTop(), moveRight()
|
---|
531 | */
|
---|
532 |
|
---|
533 | void QRect::moveBottom( int pos )
|
---|
534 | {
|
---|
535 | y1 += (QCOORD)(pos - y2);
|
---|
536 | y2 = (QCOORD)pos;
|
---|
537 | }
|
---|
538 |
|
---|
539 | /*!
|
---|
540 | Sets the top-left position of the rectangle to \a p, leaving the
|
---|
541 | size unchanged.
|
---|
542 |
|
---|
543 | \sa topLeft(), setTopLeft(), moveBottomRight(), moveTopRight(), moveBottomLeft()
|
---|
544 | */
|
---|
545 |
|
---|
546 | void QRect::moveTopLeft( const QPoint &p )
|
---|
547 | {
|
---|
548 | moveLeft( p.x() );
|
---|
549 | moveTop( p.y() );
|
---|
550 | }
|
---|
551 |
|
---|
552 | /*!
|
---|
553 | Sets the bottom-right position of the rectangle to \a p, leaving
|
---|
554 | the size unchanged.
|
---|
555 |
|
---|
556 | \sa bottomRight(), setBottomRight(), moveTopLeft(), moveTopRight(), moveBottomLeft()
|
---|
557 | */
|
---|
558 |
|
---|
559 | void QRect::moveBottomRight( const QPoint &p )
|
---|
560 | {
|
---|
561 | moveRight( p.x() );
|
---|
562 | moveBottom( p.y() );
|
---|
563 | }
|
---|
564 |
|
---|
565 | /*!
|
---|
566 | Sets the top-right position of the rectangle to \a p, leaving the
|
---|
567 | size unchanged.
|
---|
568 |
|
---|
569 | \sa topRight(), setTopRight(), moveTopLeft(), moveBottomRight(), moveBottomLeft()
|
---|
570 | */
|
---|
571 |
|
---|
572 | void QRect::moveTopRight( const QPoint &p )
|
---|
573 | {
|
---|
574 | moveRight( p.x() );
|
---|
575 | moveTop( p.y() );
|
---|
576 | }
|
---|
577 |
|
---|
578 | /*!
|
---|
579 | Sets the bottom-left position of the rectangle to \a p, leaving
|
---|
580 | the size unchanged.
|
---|
581 |
|
---|
582 | \sa bottomLeft(), setBottomLeft(), moveTopLeft(), moveBottomRight(), moveTopRight()
|
---|
583 | */
|
---|
584 |
|
---|
585 | void QRect::moveBottomLeft( const QPoint &p )
|
---|
586 | {
|
---|
587 | moveLeft( p.x() );
|
---|
588 | moveBottom( p.y() );
|
---|
589 | }
|
---|
590 |
|
---|
591 |
|
---|
592 | /*!
|
---|
593 | Sets the center point of the rectangle to \a p, leaving the size
|
---|
594 | unchanged.
|
---|
595 |
|
---|
596 | \sa center(), moveTopLeft(), moveBottomRight(), moveTopRight(), moveBottomLeft()
|
---|
597 | */
|
---|
598 |
|
---|
599 | void QRect::moveCenter( const QPoint &p )
|
---|
600 | {
|
---|
601 | QCOORD w = x2 - x1;
|
---|
602 | QCOORD h = y2 - y1;
|
---|
603 | x1 = (QCOORD)(p.x() - w/2);
|
---|
604 | y1 = (QCOORD)(p.y() - h/2);
|
---|
605 | x2 = x1 + w;
|
---|
606 | y2 = y1 + h;
|
---|
607 | }
|
---|
608 |
|
---|
609 |
|
---|
610 | /*!
|
---|
611 | Moves the rectangle \a dx along the x axis and \a dy along the y
|
---|
612 | axis, relative to the current position. Positive values move the
|
---|
613 | rectangle to the right and down.
|
---|
614 |
|
---|
615 | \sa moveTopLeft()
|
---|
616 | */
|
---|
617 |
|
---|
618 | void QRect::moveBy( int dx, int dy )
|
---|
619 | {
|
---|
620 | x1 += (QCOORD)dx;
|
---|
621 | y1 += (QCOORD)dy;
|
---|
622 | x2 += (QCOORD)dx;
|
---|
623 | y2 += (QCOORD)dy;
|
---|
624 | }
|
---|
625 |
|
---|
626 | /*!
|
---|
627 | Sets the coordinates of the rectangle's top-left corner to \a (x,
|
---|
628 | y), and its size to \a (w, h).
|
---|
629 |
|
---|
630 | \sa rect(), setCoords()
|
---|
631 | */
|
---|
632 |
|
---|
633 | void QRect::setRect( int x, int y, int w, int h )
|
---|
634 | {
|
---|
635 | x1 = (QCOORD)x;
|
---|
636 | y1 = (QCOORD)y;
|
---|
637 | x2 = (QCOORD)(x+w-1);
|
---|
638 | y2 = (QCOORD)(y+h-1);
|
---|
639 | }
|
---|
640 |
|
---|
641 | /*!
|
---|
642 | Sets the coordinates of the rectangle's top-left corner to \a
|
---|
643 | (xp1, yp1), and the coordinates of its bottom-right corner to \a
|
---|
644 | (xp2, yp2).
|
---|
645 |
|
---|
646 | \sa coords(), setRect()
|
---|
647 | */
|
---|
648 |
|
---|
649 | void QRect::setCoords( int xp1, int yp1, int xp2, int yp2 )
|
---|
650 | {
|
---|
651 | x1 = (QCOORD)xp1;
|
---|
652 | y1 = (QCOORD)yp1;
|
---|
653 | x2 = (QCOORD)xp2;
|
---|
654 | y2 = (QCOORD)yp2;
|
---|
655 | }
|
---|
656 |
|
---|
657 | /*!
|
---|
658 | Adds \a xp1, \a yp1, \a xp2 and \a yp2 respectively to the
|
---|
659 | existing coordinates of the rectangle.
|
---|
660 | */
|
---|
661 |
|
---|
662 | void QRect::addCoords( int xp1, int yp1, int xp2, int yp2 )
|
---|
663 | {
|
---|
664 | x1 += (QCOORD)xp1;
|
---|
665 | y1 += (QCOORD)yp1;
|
---|
666 | x2 += (QCOORD)xp2;
|
---|
667 | y2 += (QCOORD)yp2;
|
---|
668 | }
|
---|
669 |
|
---|
670 | /*!
|
---|
671 | \fn QSize QRect::size() const
|
---|
672 |
|
---|
673 | Returns the size of the rectangle.
|
---|
674 |
|
---|
675 | \sa width(), height()
|
---|
676 | */
|
---|
677 |
|
---|
678 | /*!
|
---|
679 | \fn int QRect::width() const
|
---|
680 |
|
---|
681 | Returns the width of the rectangle. The width includes both the
|
---|
682 | left and right edges, i.e. width = right - left + 1.
|
---|
683 |
|
---|
684 | \sa height(), size(), setHeight()
|
---|
685 | */
|
---|
686 |
|
---|
687 | /*!
|
---|
688 | \fn int QRect::height() const
|
---|
689 |
|
---|
690 | Returns the height of the rectangle. The height includes both the
|
---|
691 | top and bottom edges, i.e. height = bottom - top + 1.
|
---|
692 |
|
---|
693 | \sa width(), size(), setHeight()
|
---|
694 | */
|
---|
695 |
|
---|
696 | /*!
|
---|
697 | Sets the width of the rectangle to \a w. The right edge is
|
---|
698 | changed, but not the left edge.
|
---|
699 |
|
---|
700 | \sa width(), setLeft(), setRight(), setSize()
|
---|
701 | */
|
---|
702 |
|
---|
703 | void QRect::setWidth( int w )
|
---|
704 | {
|
---|
705 | x2 = (QCOORD)(x1 + w - 1);
|
---|
706 | }
|
---|
707 |
|
---|
708 | /*!
|
---|
709 | Sets the height of the rectangle to \a h. The top edge is not
|
---|
710 | moved, but the bottom edge may be moved.
|
---|
711 |
|
---|
712 | \sa height(), setTop(), setBottom(), setSize()
|
---|
713 | */
|
---|
714 |
|
---|
715 | void QRect::setHeight( int h )
|
---|
716 | {
|
---|
717 | y2 = (QCOORD)(y1 + h - 1);
|
---|
718 | }
|
---|
719 |
|
---|
720 | /*!
|
---|
721 | Sets the size of the rectangle to \a s. The top-left corner is not
|
---|
722 | moved.
|
---|
723 |
|
---|
724 | \sa size(), setWidth(), setHeight()
|
---|
725 | */
|
---|
726 |
|
---|
727 | void QRect::setSize( const QSize &s )
|
---|
728 | {
|
---|
729 | x2 = (QCOORD)(s.width() +x1-1);
|
---|
730 | y2 = (QCOORD)(s.height()+y1-1);
|
---|
731 | }
|
---|
732 |
|
---|
733 | /*!
|
---|
734 | Returns TRUE if the point \a p is inside or on the edge of the
|
---|
735 | rectangle; otherwise returns FALSE.
|
---|
736 |
|
---|
737 | If \a proper is TRUE, this function returns TRUE only if \a p is
|
---|
738 | inside (not on the edge).
|
---|
739 | */
|
---|
740 |
|
---|
741 | bool QRect::contains( const QPoint &p, bool proper ) const
|
---|
742 | {
|
---|
743 | if ( proper )
|
---|
744 | return p.x() > x1 && p.x() < x2 &&
|
---|
745 | p.y() > y1 && p.y() < y2;
|
---|
746 | else
|
---|
747 | return p.x() >= x1 && p.x() <= x2 &&
|
---|
748 | p.y() >= y1 && p.y() <= y2;
|
---|
749 | }
|
---|
750 |
|
---|
751 | /*!
|
---|
752 | \overload bool QRect::contains( int x, int y, bool proper ) const
|
---|
753 |
|
---|
754 | Returns TRUE if the point \a x, \a y is inside this rectangle;
|
---|
755 | otherwise returns FALSE.
|
---|
756 |
|
---|
757 | If \a proper is TRUE, this function returns TRUE only if the point
|
---|
758 | is entirely inside (not on the edge).
|
---|
759 | */
|
---|
760 |
|
---|
761 | /*!
|
---|
762 | \overload bool QRect::contains( int x, int y ) const
|
---|
763 |
|
---|
764 | Returns TRUE if the point \a x, \a y is inside this rectangle;
|
---|
765 | otherwise returns FALSE.
|
---|
766 | */
|
---|
767 |
|
---|
768 | /*!
|
---|
769 | \overload
|
---|
770 |
|
---|
771 | Returns TRUE if the rectangle \a r is inside this rectangle;
|
---|
772 | otherwise returns FALSE.
|
---|
773 |
|
---|
774 | If \a proper is TRUE, this function returns TRUE only if \a r is
|
---|
775 | entirely inside (not on the edge).
|
---|
776 |
|
---|
777 | \sa unite(), intersect(), intersects()
|
---|
778 | */
|
---|
779 |
|
---|
780 | bool QRect::contains( const QRect &r, bool proper ) const
|
---|
781 | {
|
---|
782 | if ( proper )
|
---|
783 | return r.x1 > x1 && r.x2 < x2 && r.y1 > y1 && r.y2 < y2;
|
---|
784 | else
|
---|
785 | return r.x1 >= x1 && r.x2 <= x2 && r.y1 >= y1 && r.y2 <= y2;
|
---|
786 | }
|
---|
787 |
|
---|
788 | /*!
|
---|
789 | Unites this rectangle with rectangle \a r.
|
---|
790 | */
|
---|
791 | QRect& QRect::operator|=(const QRect &r)
|
---|
792 | {
|
---|
793 | *this = *this | r;
|
---|
794 | return *this;
|
---|
795 | }
|
---|
796 |
|
---|
797 | /*!
|
---|
798 | Intersects this rectangle with rectangle \a r.
|
---|
799 | */
|
---|
800 | QRect& QRect::operator&=(const QRect &r)
|
---|
801 | {
|
---|
802 | *this = *this & r;
|
---|
803 | return *this;
|
---|
804 | }
|
---|
805 |
|
---|
806 |
|
---|
807 | /*!
|
---|
808 | Returns the bounding rectangle of this rectangle and rectangle \a
|
---|
809 | r.
|
---|
810 |
|
---|
811 | The bounding rectangle of a nonempty rectangle and an empty or
|
---|
812 | invalid rectangle is defined to be the nonempty rectangle.
|
---|
813 |
|
---|
814 | \sa operator|=(), operator&(), intersects(), contains()
|
---|
815 | */
|
---|
816 |
|
---|
817 | QRect QRect::operator|(const QRect &r) const
|
---|
818 | {
|
---|
819 | if ( isValid() ) {
|
---|
820 | if ( r.isValid() ) {
|
---|
821 | QRect tmp;
|
---|
822 | tmp.setLeft( QMIN( x1, r.x1 ) );
|
---|
823 | tmp.setRight( QMAX( x2, r.x2 ) );
|
---|
824 | tmp.setTop( QMIN( y1, r.y1 ) );
|
---|
825 | tmp.setBottom( QMAX( y2, r.y2 ) );
|
---|
826 | return tmp;
|
---|
827 | } else {
|
---|
828 | return *this;
|
---|
829 | }
|
---|
830 | } else {
|
---|
831 | return r;
|
---|
832 | }
|
---|
833 | }
|
---|
834 |
|
---|
835 | /*!
|
---|
836 | Returns the bounding rectangle of this rectangle and rectangle \a
|
---|
837 | r. \c{r.unite(s)} is equivalent to \c{r|s}.
|
---|
838 | */
|
---|
839 | QRect QRect::unite( const QRect &r ) const
|
---|
840 | {
|
---|
841 | return *this | r;
|
---|
842 | }
|
---|
843 |
|
---|
844 |
|
---|
845 | /*!
|
---|
846 | Returns the intersection of this rectangle and rectangle \a r.
|
---|
847 |
|
---|
848 | Returns an empty rectangle if there is no intersection.
|
---|
849 |
|
---|
850 | \sa operator&=(), operator|(), isEmpty(), intersects(), contains()
|
---|
851 | */
|
---|
852 |
|
---|
853 | QRect QRect::operator&( const QRect &r ) const
|
---|
854 | {
|
---|
855 | QRect tmp;
|
---|
856 | tmp.x1 = QMAX( x1, r.x1 );
|
---|
857 | tmp.x2 = QMIN( x2, r.x2 );
|
---|
858 | tmp.y1 = QMAX( y1, r.y1 );
|
---|
859 | tmp.y2 = QMIN( y2, r.y2 );
|
---|
860 | return tmp;
|
---|
861 | }
|
---|
862 |
|
---|
863 | /*!
|
---|
864 | Returns the intersection of this rectangle and rectangle \a r.
|
---|
865 | \c{r.intersect(s)} is equivalent to \c{r&s}.
|
---|
866 | */
|
---|
867 | QRect QRect::intersect( const QRect &r ) const
|
---|
868 | {
|
---|
869 | return *this & r;
|
---|
870 | }
|
---|
871 |
|
---|
872 | /*!
|
---|
873 | Returns TRUE if this rectangle intersects with rectangle \a r
|
---|
874 | (there is at least one pixel that is within both rectangles);
|
---|
875 | otherwise returns FALSE.
|
---|
876 |
|
---|
877 | \sa intersect(), contains()
|
---|
878 | */
|
---|
879 |
|
---|
880 | bool QRect::intersects( const QRect &r ) const
|
---|
881 | {
|
---|
882 | return ( QMAX( x1, r.x1 ) <= QMIN( x2, r.x2 ) &&
|
---|
883 | QMAX( y1, r.y1 ) <= QMIN( y2, r.y2 ) );
|
---|
884 | }
|
---|
885 |
|
---|
886 |
|
---|
887 | /*!
|
---|
888 | \relates QRect
|
---|
889 |
|
---|
890 | Returns TRUE if \a r1 and \a r2 are equal; otherwise returns FALSE.
|
---|
891 | */
|
---|
892 |
|
---|
893 | bool operator==( const QRect &r1, const QRect &r2 )
|
---|
894 | {
|
---|
895 | return r1.x1==r2.x1 && r1.x2==r2.x2 && r1.y1==r2.y1 && r1.y2==r2.y2;
|
---|
896 | }
|
---|
897 |
|
---|
898 | /*!
|
---|
899 | \relates QRect
|
---|
900 |
|
---|
901 | Returns TRUE if \a r1 and \a r2 are different; otherwise returns FALSE.
|
---|
902 | */
|
---|
903 |
|
---|
904 | bool operator!=( const QRect &r1, const QRect &r2 )
|
---|
905 | {
|
---|
906 | return r1.x1!=r2.x1 || r1.x2!=r2.x2 || r1.y1!=r2.y1 || r1.y2!=r2.y2;
|
---|
907 | }
|
---|
908 |
|
---|
909 |
|
---|
910 | /*****************************************************************************
|
---|
911 | QRect stream functions
|
---|
912 | *****************************************************************************/
|
---|
913 | #ifndef QT_NO_DATASTREAM
|
---|
914 | /*!
|
---|
915 | \relates QRect
|
---|
916 |
|
---|
917 | Writes the QRect, \a r, to the stream \a s, and returns a
|
---|
918 | reference to the stream.
|
---|
919 |
|
---|
920 | \sa \link datastreamformat.html Format of the QDataStream operators \endlink
|
---|
921 | */
|
---|
922 |
|
---|
923 | QDataStream &operator<<( QDataStream &s, const QRect &r )
|
---|
924 | {
|
---|
925 | if ( s.version() == 1 )
|
---|
926 | s << (Q_INT16)r.left() << (Q_INT16)r.top()
|
---|
927 | << (Q_INT16)r.right() << (Q_INT16)r.bottom();
|
---|
928 | else
|
---|
929 | s << (Q_INT32)r.left() << (Q_INT32)r.top()
|
---|
930 | << (Q_INT32)r.right() << (Q_INT32)r.bottom();
|
---|
931 | return s;
|
---|
932 | }
|
---|
933 |
|
---|
934 | /*!
|
---|
935 | \relates QRect
|
---|
936 |
|
---|
937 | Reads a QRect from the stream \a s into rect \a r and returns a
|
---|
938 | reference to the stream.
|
---|
939 |
|
---|
940 | \sa \link datastreamformat.html Format of the QDataStream operators \endlink
|
---|
941 | */
|
---|
942 |
|
---|
943 | QDataStream &operator>>( QDataStream &s, QRect &r )
|
---|
944 | {
|
---|
945 | if ( s.version() == 1 ) {
|
---|
946 | Q_INT16 x1, y1, x2, y2;
|
---|
947 | s >> x1; s >> y1; s >> x2; s >> y2;
|
---|
948 | r.setCoords( x1, y1, x2, y2 );
|
---|
949 | }
|
---|
950 | else {
|
---|
951 | Q_INT32 x1, y1, x2, y2;
|
---|
952 | s >> x1; s >> y1; s >> x2; s >> y2;
|
---|
953 | r.setCoords( x1, y1, x2, y2 );
|
---|
954 | }
|
---|
955 | return s;
|
---|
956 | }
|
---|
957 | #endif // QT_NO_DATASTREAM
|
---|