Qt 4.8
qrect.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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 
50 
209 /*****************************************************************************
210  QRect member functions
211  *****************************************************************************/
212 
323 {
324  QRect r;
325  if (x2 < x1 - 1) { // swap bad x values
326  r.x1 = x2;
327  r.x2 = x1;
328  } else {
329  r.x1 = x1;
330  r.x2 = x2;
331  }
332  if (y2 < y1 - 1) { // swap bad y values
333  r.y1 = y2;
334  r.y2 = y1;
335  } else {
336  r.y1 = y1;
337  r.y2 = y2;
338  }
339  return r;
340 }
341 
342 
840 void QRect::moveCenter(const QPoint &p)
841 {
842  int w = x2 - x1;
843  int h = y2 - y1;
844  x1 = p.x() - w/2;
845  y1 = p.y() - h/2;
846  x2 = x1 + w;
847  y2 = y1 + h;
848 }
849 
1101 bool QRect::contains(const QPoint &p, bool proper) const
1102 {
1103  int l, r;
1104  if (x2 < x1 - 1) {
1105  l = x2;
1106  r = x1;
1107  } else {
1108  l = x1;
1109  r = x2;
1110  }
1111  if (proper) {
1112  if (p.x() <= l || p.x() >= r)
1113  return false;
1114  } else {
1115  if (p.x() < l || p.x() > r)
1116  return false;
1117  }
1118  int t, b;
1119  if (y2 < y1 - 1) {
1120  t = y2;
1121  b = y1;
1122  } else {
1123  t = y1;
1124  b = y2;
1125  }
1126  if (proper) {
1127  if (p.y() <= t || p.y() >= b)
1128  return false;
1129  } else {
1130  if (p.y() < t || p.y() > b)
1131  return false;
1132  }
1133  return true;
1134 }
1135 
1136 
1174 bool QRect::contains(const QRect &r, bool proper) const
1175 {
1176  if (isNull() || r.isNull())
1177  return false;
1178 
1179  int l1 = x1;
1180  int r1 = x1;
1181  if (x2 - x1 + 1 < 0)
1182  l1 = x2;
1183  else
1184  r1 = x2;
1185 
1186  int l2 = r.x1;
1187  int r2 = r.x1;
1188  if (r.x2 - r.x1 + 1 < 0)
1189  l2 = r.x2;
1190  else
1191  r2 = r.x2;
1192 
1193  if (proper) {
1194  if (l2 <= l1 || r2 >= r1)
1195  return false;
1196  } else {
1197  if (l2 < l1 || r2 > r1)
1198  return false;
1199  }
1200 
1201  int t1 = y1;
1202  int b1 = y1;
1203  if (y2 - y1 + 1 < 0)
1204  t1 = y2;
1205  else
1206  b1 = y2;
1207 
1208  int t2 = r.y1;
1209  int b2 = r.y1;
1210  if (r.y2 - r.y1 + 1 < 0)
1211  t2 = r.y2;
1212  else
1213  b2 = r.y2;
1214 
1215  if (proper) {
1216  if (t2 <= t1 || b2 >= b1)
1217  return false;
1218  } else {
1219  if (t2 < t1 || b2 > b1)
1220  return false;
1221  }
1222 
1223  return true;
1224 }
1225 
1262 {
1263  if (isNull())
1264  return r;
1265  if (r.isNull())
1266  return *this;
1267 
1268  int l1 = x1;
1269  int r1 = x1;
1270  if (x2 - x1 + 1 < 0)
1271  l1 = x2;
1272  else
1273  r1 = x2;
1274 
1275  int l2 = r.x1;
1276  int r2 = r.x1;
1277  if (r.x2 - r.x1 + 1 < 0)
1278  l2 = r.x2;
1279  else
1280  r2 = r.x2;
1281 
1282  int t1 = y1;
1283  int b1 = y1;
1284  if (y2 - y1 + 1 < 0)
1285  t1 = y2;
1286  else
1287  b1 = y2;
1288 
1289  int t2 = r.y1;
1290  int b2 = r.y1;
1291  if (r.y2 - r.y1 + 1 < 0)
1292  t2 = r.y2;
1293  else
1294  b2 = r.y2;
1295 
1296  QRect tmp;
1297  tmp.x1 = qMin(l1, l2);
1298  tmp.x2 = qMax(r1, r2);
1299  tmp.y1 = qMin(t1, t2);
1300  tmp.y2 = qMax(b1, b2);
1301  return tmp;
1302 }
1303 
1341 QRect QRect::operator&(const QRect &r) const
1342 {
1343  if (isNull() || r.isNull())
1344  return QRect();
1345 
1346  int l1 = x1;
1347  int r1 = x1;
1348  if (x2 - x1 + 1 < 0)
1349  l1 = x2;
1350  else
1351  r1 = x2;
1352 
1353  int l2 = r.x1;
1354  int r2 = r.x1;
1355  if (r.x2 - r.x1 + 1 < 0)
1356  l2 = r.x2;
1357  else
1358  r2 = r.x2;
1359 
1360  if (l1 > r2 || l2 > r1)
1361  return QRect();
1362 
1363  int t1 = y1;
1364  int b1 = y1;
1365  if (y2 - y1 + 1 < 0)
1366  t1 = y2;
1367  else
1368  b1 = y2;
1369 
1370  int t2 = r.y1;
1371  int b2 = r.y1;
1372  if (r.y2 - r.y1 + 1 < 0)
1373  t2 = r.y2;
1374  else
1375  b2 = r.y2;
1376 
1377  if (t1 > b2 || t2 > b1)
1378  return QRect();
1379 
1380  QRect tmp;
1381  tmp.x1 = qMax(l1, l2);
1382  tmp.x2 = qMin(r1, r2);
1383  tmp.y1 = qMax(t1, t2);
1384  tmp.y2 = qMin(b1, b2);
1385  return tmp;
1386 }
1387 
1429 bool QRect::intersects(const QRect &r) const
1430 {
1431  if (isNull() || r.isNull())
1432  return false;
1433 
1434  int l1 = x1;
1435  int r1 = x1;
1436  if (x2 - x1 + 1 < 0)
1437  l1 = x2;
1438  else
1439  r1 = x2;
1440 
1441  int l2 = r.x1;
1442  int r2 = r.x1;
1443  if (r.x2 - r.x1 + 1 < 0)
1444  l2 = r.x2;
1445  else
1446  r2 = r.x2;
1447 
1448  if (l1 > r2 || l2 > r1)
1449  return false;
1450 
1451  int t1 = y1;
1452  int b1 = y1;
1453  if (y2 - y1 + 1 < 0)
1454  t1 = y2;
1455  else
1456  b1 = y2;
1457 
1458  int t2 = r.y1;
1459  int b2 = r.y1;
1460  if (r.y2 - r.y1 + 1 < 0)
1461  t2 = r.y2;
1462  else
1463  b2 = r.y2;
1464 
1465  if (t1 > b2 || t2 > b1)
1466  return false;
1467 
1468  return true;
1469 }
1470 
1495 /*****************************************************************************
1496  QRect stream functions
1497  *****************************************************************************/
1498 #ifndef QT_NO_DATASTREAM
1499 
1513 {
1514  if (s.version() == 1)
1515  s << (qint16)r.left() << (qint16)r.top()
1516  << (qint16)r.right() << (qint16)r.bottom();
1517  else
1518  s << (qint32)r.left() << (qint32)r.top()
1519  << (qint32)r.right() << (qint32)r.bottom();
1520  return s;
1521 }
1522 
1537 {
1538  if (s.version() == 1) {
1539  qint16 x1, y1, x2, y2;
1540  s >> x1; s >> y1; s >> x2; s >> y2;
1541  r.setCoords(x1, y1, x2, y2);
1542  }
1543  else {
1544  qint32 x1, y1, x2, y2;
1545  s >> x1; s >> y1; s >> x2; s >> y2;
1546  r.setCoords(x1, y1, x2, y2);
1547  }
1548  return s;
1549 }
1550 
1551 #endif // QT_NO_DATASTREAM
1552 
1553 
1554 #ifndef QT_NO_DEBUG_STREAM
1555 QDebug operator<<(QDebug dbg, const QRect &r) {
1556  dbg.nospace() << "QRect(" << r.x() << ',' << r.y() << ' '
1557  << r.width() << 'x' << r.height() << ')';
1558  return dbg.space();
1559 }
1560 #endif
1561 
1703 /*****************************************************************************
1704  QRectF member functions
1705  *****************************************************************************/
1706 
1823 {
1824  QRectF r = *this;
1825  if (r.w < 0) {
1826  r.xp += r.w;
1827  r.w = -r.w;
1828  }
1829  if (r.h < 0) {
1830  r.yp += r.h;
1831  r.h = -r.h;
1832  }
1833  return r;
1834 }
1835 
2349 bool QRectF::contains(const QPointF &p) const
2350 {
2351  qreal l = xp;
2352  qreal r = xp;
2353  if (w < 0)
2354  l += w;
2355  else
2356  r += w;
2357  if (l == r) // null rect
2358  return false;
2359 
2360  if (p.x() < l || p.x() > r)
2361  return false;
2362 
2363  qreal t = yp;
2364  qreal b = yp;
2365  if (h < 0)
2366  t += h;
2367  else
2368  b += h;
2369  if (t == b) // null rect
2370  return false;
2371 
2372  if (p.y() < t || p.y() > b)
2373  return false;
2374 
2375  return true;
2376 }
2377 
2378 
2401 bool QRectF::contains(const QRectF &r) const
2402 {
2403  qreal l1 = xp;
2404  qreal r1 = xp;
2405  if (w < 0)
2406  l1 += w;
2407  else
2408  r1 += w;
2409  if (l1 == r1) // null rect
2410  return false;
2411 
2412  qreal l2 = r.xp;
2413  qreal r2 = r.xp;
2414  if (r.w < 0)
2415  l2 += r.w;
2416  else
2417  r2 += r.w;
2418  if (l2 == r2) // null rect
2419  return false;
2420 
2421  if (l2 < l1 || r2 > r1)
2422  return false;
2423 
2424  qreal t1 = yp;
2425  qreal b1 = yp;
2426  if (h < 0)
2427  t1 += h;
2428  else
2429  b1 += h;
2430  if (t1 == b1) // null rect
2431  return false;
2432 
2433  qreal t2 = r.yp;
2434  qreal b2 = r.yp;
2435  if (r.h < 0)
2436  t2 += r.h;
2437  else
2438  b2 += r.h;
2439  if (t2 == b2) // null rect
2440  return false;
2441 
2442  if (t2 < t1 || b2 > b1)
2443  return false;
2444 
2445  return true;
2446 }
2447 
2573 {
2574  if (isNull())
2575  return r;
2576  if (r.isNull())
2577  return *this;
2578 
2579  qreal left = xp;
2580  qreal right = xp;
2581  if (w < 0)
2582  left += w;
2583  else
2584  right += w;
2585 
2586  if (r.w < 0) {
2587  left = qMin(left, r.xp + r.w);
2588  right = qMax(right, r.xp);
2589  } else {
2590  left = qMin(left, r.xp);
2591  right = qMax(right, r.xp + r.w);
2592  }
2593 
2594  qreal top = yp;
2595  qreal bottom = yp;
2596  if (h < 0)
2597  top += h;
2598  else
2599  bottom += h;
2600 
2601  if (r.h < 0) {
2602  top = qMin(top, r.yp + r.h);
2603  bottom = qMax(bottom, r.yp);
2604  } else {
2605  top = qMin(top, r.yp);
2606  bottom = qMax(bottom, r.yp + r.h);
2607  }
2608 
2609  return QRectF(left, top, right - left, bottom - top);
2610 }
2611 
2650 QRectF QRectF::operator&(const QRectF &r) const
2651 {
2652  qreal l1 = xp;
2653  qreal r1 = xp;
2654  if (w < 0)
2655  l1 += w;
2656  else
2657  r1 += w;
2658  if (l1 == r1) // null rect
2659  return QRectF();
2660 
2661  qreal l2 = r.xp;
2662  qreal r2 = r.xp;
2663  if (r.w < 0)
2664  l2 += r.w;
2665  else
2666  r2 += r.w;
2667  if (l2 == r2) // null rect
2668  return QRectF();
2669 
2670  if (l1 >= r2 || l2 >= r1)
2671  return QRectF();
2672 
2673  qreal t1 = yp;
2674  qreal b1 = yp;
2675  if (h < 0)
2676  t1 += h;
2677  else
2678  b1 += h;
2679  if (t1 == b1) // null rect
2680  return QRectF();
2681 
2682  qreal t2 = r.yp;
2683  qreal b2 = r.yp;
2684  if (r.h < 0)
2685  t2 += r.h;
2686  else
2687  b2 += r.h;
2688  if (t2 == b2) // null rect
2689  return QRectF();
2690 
2691  if (t1 >= b2 || t2 >= b1)
2692  return QRectF();
2693 
2694  QRectF tmp;
2695  tmp.xp = qMax(l1, l2);
2696  tmp.yp = qMax(t1, t2);
2697  tmp.w = qMin(r1, r2) - tmp.xp;
2698  tmp.h = qMin(b1, b2) - tmp.yp;
2699  return tmp;
2700 }
2701 
2744 bool QRectF::intersects(const QRectF &r) const
2745 {
2746  qreal l1 = xp;
2747  qreal r1 = xp;
2748  if (w < 0)
2749  l1 += w;
2750  else
2751  r1 += w;
2752  if (l1 == r1) // null rect
2753  return false;
2754 
2755  qreal l2 = r.xp;
2756  qreal r2 = r.xp;
2757  if (r.w < 0)
2758  l2 += r.w;
2759  else
2760  r2 += r.w;
2761  if (l2 == r2) // null rect
2762  return false;
2763 
2764  if (l1 >= r2 || l2 >= r1)
2765  return false;
2766 
2767  qreal t1 = yp;
2768  qreal b1 = yp;
2769  if (h < 0)
2770  t1 += h;
2771  else
2772  b1 += h;
2773  if (t1 == b1) // null rect
2774  return false;
2775 
2776  qreal t2 = r.yp;
2777  qreal b2 = r.yp;
2778  if (r.h < 0)
2779  t2 += r.h;
2780  else
2781  b2 += r.h;
2782  if (t2 == b2) // null rect
2783  return false;
2784 
2785  if (t1 >= b2 || t2 >= b1)
2786  return false;
2787 
2788  return true;
2789 }
2790 
2818 {
2819  int xmin = int(qFloor(xp));
2820  int xmax = int(qCeil(xp + w));
2821  int ymin = int(qFloor(yp));
2822  int ymax = int(qCeil(yp + h));
2823  return QRect(xmin, ymin, xmax - xmin, ymax - ymin);
2824 }
2825 
2861 /*****************************************************************************
2862  QRectF stream functions
2863  *****************************************************************************/
2864 #ifndef QT_NO_DATASTREAM
2865 
2880 {
2881  s << double(r.x()) << double(r.y()) << double(r.width()) << double(r.height());
2882  return s;
2883 }
2884 
2900 {
2901  double x, y, w, h;
2902  s >> x;
2903  s >> y;
2904  s >> w;
2905  s >> h;
2906  r.setRect(qreal(x), qreal(y), qreal(w), qreal(h));
2907  return s;
2908 }
2909 
2910 #endif // QT_NO_DATASTREAM
2911 
2912 
2913 #ifndef QT_NO_DEBUG_STREAM
2914 QDebug operator<<(QDebug dbg, const QRectF &r) {
2915  dbg.nospace() << "QRectF(" << r.x() << ',' << r.y() << ' '
2916  << r.width() << 'x' << r.height() << ')';
2917  return dbg.space();
2918 }
2919 #endif
2920 
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:62
QRect toAlignedRect() const
Returns a QRect based on the values of this rectangle that is the smallest possible integer rectangle...
Definition: qrect.cpp:2817
qreal y() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:667
bool isNull() const
Returns true if the rectangle is a null rectangle, otherwise returns false.
Definition: qrect.h:231
double qreal
Definition: qglobal.h:1193
QRectF operator|(const QRectF &r) const
Returns the bounding rectangle of this rectangle and the given rectangle.
Definition: qrect.cpp:2572
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
int qint32
Definition: qglobal.h:937
QRect operator &(const QRect &r) const
int y1
Definition: qrect.h:177
int qCeil(qreal v)
Definition: qmath.h:63
QDebug & nospace()
Clears the stream&#39;s internal flag that records whether the last character was a space and returns a r...
Definition: qdebug.h:92
int qFloor(qreal v)
Definition: qmath.h:73
int y2
Definition: qrect.h:179
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:214
qreal xp
Definition: qrect.h:606
QRect normalized() const
Returns a normalized rectangle; i.e., a rectangle that has a non-negative width and height...
Definition: qrect.cpp:322
int left() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:240
int width() const
Returns the width of the rectangle.
Definition: qrect.h:303
qreal h
Definition: qrect.h:609
int height() const
Returns the height of the rectangle.
Definition: qrect.h:306
bool intersects(const QRectF &r) const
Returns true if this rectangle intersects with the given rectangle (i.
Definition: qrect.cpp:2744
int bottom() const
Returns the y-coordinate of the rectangle&#39;s bottom edge.
Definition: qrect.h:249
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
qreal x() const
Returns the x-coordinate of this point.
Definition: qpoint.h:282
bool contains(const QPointF &p) const
Returns true if the given point is inside or on the edge of the rectangle; otherwise returns false...
Definition: qrect.cpp:2349
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
qreal w
Definition: qrect.h:608
QRect()
Constructs a null rectangle.
Definition: qrect.h:61
qreal height() const
Returns the height of the rectangle.
Definition: qrect.h:710
short qint16
Definition: qglobal.h:935
qreal width() const
Returns the width of the rectangle.
Definition: qrect.h:707
void setRect(qreal x, qreal y, qreal w, qreal h)
Sets the coordinates of the rectangle&#39;s top-left corner to (x, y), and its size to the given width an...
Definition: qrect.h:754
void setCoords(int x1, int y1, int x2, int y2)
Sets the coordinates of the rectangle&#39;s top-left corner to (x1, y1), and the coordinates of its botto...
Definition: qrect.h:416
QRectF operator &(const QRectF &r) const
Returns the intersection of this rectangle and the given rectangle.
bool contains(const QPoint &p, bool proper=false) const
Returns true if the given point is inside or on the edge of the rectangle, otherwise returns false...
Definition: qrect.cpp:1101
void moveCenter(const QPoint &p)
Moves the rectangle, leaving the center point at the given position.
Definition: qrect.cpp:840
QDataStream & operator>>(QDataStream &stream, QRect &rectangle)
Reads a rectangle from the given stream into the given rectangle, and returns a reference to the stre...
Definition: qrect.cpp:1536
int version() const
Returns the version number of the data serialization format.
Definition: qdatastream.h:212
int top() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:243
int right() const
Returns the x-coordinate of the rectangle&#39;s right edge.
Definition: qrect.h:246
int y() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:255
qreal x() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:664
int x() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:252
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
QFactoryLoader * l
int y() const
Returns the y coordinate of this point.
Definition: qpoint.h:131
qreal y() const
Returns the y-coordinate of this point.
Definition: qpoint.h:287
int x2
Definition: qrect.h:180
qreal yp
Definition: qrect.h:607
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
bool intersects(const QRect &r) const
Returns true if this rectangle intersects with the given rectangle (i.
Definition: qrect.cpp:1429
int x() const
Returns the x coordinate of this point.
Definition: qpoint.h:128
QRect operator|(const QRect &r) const
Returns the bounding rectangle of this rectangle and the given rectangle.
Definition: qrect.cpp:1261
int x1
Definition: qrect.h:178
QRectF normalized() const
Returns a normalized rectangle; i.e., a rectangle that has a non-negative width and height...
Definition: qrect.cpp:1822
QDebug & space()
Writes a space character to the debug stream and returns a reference to the stream.
Definition: qdebug.h:91
bool isNull() const
Returns true if the rectangle is a null rectangle, otherwise returns false.
Definition: qrect.h:655
QDataStream & operator<<(QDataStream &stream, const QRect &rectangle)
Writes the given rectangle to the given stream, and returns a reference to the stream.
Definition: qrect.cpp:1512