Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qrect.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qrect.h"
5#include "qdatastream.h"
6#include "qmath.h"
7
8#include <private/qdebug_p.h>
9
11
179/*****************************************************************************
180 QRect member functions
181 *****************************************************************************/
182
277QRect QRect::normalized() const noexcept
278{
279 QRect r(*this);
280 if (x2 < x1) { // swap bad x values
281 r.x1 = x2 + 1;
282 r.x2 = x1 - 1;
283 }
284 if (y2 < y1) { // swap bad y values
285 r.y1 = y2 + 1;
286 r.y2 = y1 - 1;
287 }
288 return r;
289}
290
291
791bool QRect::contains(const QPoint &p, bool proper) const noexcept
792{
793 int l, r;
794 if (x2 < x1 - 1) {
795 l = x2 + 1;
796 r = x1 - 1;
797 } else {
798 l = x1;
799 r = x2;
800 }
801 if (proper) {
802 if (p.x() <= l || p.x() >= r)
803 return false;
804 } else {
805 if (p.x() < l || p.x() > r)
806 return false;
807 }
808 int t, b;
809 if (y2 < y1 - 1) {
810 t = y2 + 1;
811 b = y1 - 1;
812 } else {
813 t = y1;
814 b = y2;
815 }
816 if (proper) {
817 if (p.y() <= t || p.y() >= b)
818 return false;
819 } else {
820 if (p.y() < t || p.y() > b)
821 return false;
822 }
823 return true;
824}
825
826
855bool QRect::contains(const QRect &r, bool proper) const noexcept
856{
857 if (isNull() || r.isNull())
858 return false;
859
860 int l1 = x1;
861 int r1 = x1 - 1;
862 if (x2 < x1 - 1)
863 l1 = x2 + 1;
864 else
865 r1 = x2;
866
867 int l2 = r.x1;
868 int r2 = r.x1 - 1;
869 if (r.x2 < r.x1 - 1)
870 l2 = r.x2 + 1;
871 else
872 r2 = r.x2;
873
874 if (proper) {
875 if (l2 <= l1 || r2 >= r1)
876 return false;
877 } else {
878 if (l2 < l1 || r2 > r1)
879 return false;
880 }
881
882 int t1 = y1;
883 int b1 = y1 - 1;
884 if (y2 < y1 - 1)
885 t1 = y2 + 1;
886 else
887 b1 = y2;
888
889 int t2 = r.y1;
890 int b2 = r.y1 - 1;
891 if (r.y2 < r.y1 - 1)
892 t2 = r.y2 + 1;
893 else
894 b2 = r.y2;
895
896 if (proper) {
897 if (t2 <= t1 || b2 >= b1)
898 return false;
899 } else {
900 if (t2 < t1 || b2 > b1)
901 return false;
902 }
903
904 return true;
905}
906
933QRect QRect::operator|(const QRect &r) const noexcept
934{
935 if (isNull())
936 return r;
937 if (r.isNull())
938 return *this;
939
940 int l1 = x1;
941 int r1 = x1 - 1;
942 if (x2 < x1 - 1)
943 l1 = x2 + 1;
944 else
945 r1 = x2;
946
947 int l2 = r.x1;
948 int r2 = r.x1 - 1;
949 if (r.x2 < r.x1 - 1)
950 l2 = r.x2 + 1;
951 else
952 r2 = r.x2;
953
954 int t1 = y1;
955 int b1 = y1 - 1;
956 if (y2 < y1 - 1)
957 t1 = y2 + 1;
958 else
959 b1 = y2;
960
961 int t2 = r.y1;
962 int b2 = r.y1 - 1;
963 if (r.y2 < r.y1 - 1)
964 t2 = r.y2 + 1;
965 else
966 b2 = r.y2;
967
968 QRect tmp;
969 tmp.x1 = qMin(l1, l2);
970 tmp.x2 = qMax(r1, r2);
971 tmp.y1 = qMin(t1, t2);
972 tmp.y2 = qMax(b1, b2);
973 return tmp;
974}
975
997QRect QRect::operator&(const QRect &r) const noexcept
998{
999 if (isNull() || r.isNull())
1000 return QRect();
1001
1002 int l1 = x1;
1003 int r1 = x2;
1004 if (x2 < x1 - 1) {
1005 l1 = x2 + 1;
1006 r1 = x1 - 1;
1007 }
1008
1009 int l2 = r.x1;
1010 int r2 = r.x2;
1011 if (r.x2 < r.x1 - 1) {
1012 l2 = r.x2 + 1;
1013 r2 = r.x1 - 1;
1014 }
1015
1016 if (l1 > r2 || l2 > r1)
1017 return QRect();
1018
1019 int t1 = y1;
1020 int b1 = y2;
1021 if (y2 < y1 - 1) {
1022 t1 = y2 + 1;
1023 b1 = y1 - 1;
1024 }
1025
1026 int t2 = r.y1;
1027 int b2 = r.y2;
1028 if (r.y2 < r.y1 - 1) {
1029 t2 = r.y2 + 1;
1030 b2 = r.y1 - 1;
1031 }
1032
1033 if (t1 > b2 || t2 > b1)
1034 return QRect();
1035
1036 QRect tmp;
1037 tmp.x1 = qMax(l1, l2);
1038 tmp.x2 = qMin(r1, r2);
1039 tmp.y1 = qMax(t1, t2);
1040 tmp.y2 = qMin(b1, b2);
1041 return tmp;
1042}
1043
1069bool QRect::intersects(const QRect &r) const noexcept
1070{
1071 if (isNull() || r.isNull())
1072 return false;
1073
1074 int l1 = x1;
1075 int r1 = x2;
1076 if (x2 < x1 - 1) {
1077 l1 = x2 + 1;
1078 r1 = x1 - 1;
1079 }
1080
1081 int l2 = r.x1;
1082 int r2 = r.x2;
1083 if (r.x2 < r.x1 - 1) {
1084 l2 = r.x2 + 1;
1085 r2 = r.x1 - 1;
1086 }
1087
1088 if (l1 > r2 || l2 > r1)
1089 return false;
1090
1091 int t1 = y1;
1092 int b1 = y2;
1093 if (y2 < y1 - 1) {
1094 t1 = y2 + 1;
1095 b1 = y1 - 1;
1096 }
1097
1098 int t2 = r.y1;
1099 int b2 = r.y2;
1100 if (r.y2 < r.y1 - 1) {
1101 t2 = r.y2 + 1;
1102 b2 = r.y1 - 1;
1103 }
1104
1105 if (t1 > b2 || t2 > b1)
1106 return false;
1107
1108 return true;
1109}
1110
1215/*****************************************************************************
1216 QRect stream functions
1217 *****************************************************************************/
1218#ifndef QT_NO_DATASTREAM
1230{
1231 if (s.version() == 1)
1232 s << (qint16)r.left() << (qint16)r.top()
1233 << (qint16)r.right() << (qint16)r.bottom();
1234 else
1235 s << (qint32)r.left() << (qint32)r.top()
1236 << (qint32)r.right() << (qint32)r.bottom();
1237 return s;
1238}
1239
1251{
1252 if (s.version() == 1) {
1253 qint16 x1, y1, x2, y2;
1254 s >> x1; s >> y1; s >> x2; s >> y2;
1255 r.setCoords(x1, y1, x2, y2);
1256 }
1257 else {
1258 qint32 x1, y1, x2, y2;
1259 s >> x1; s >> y1; s >> x2; s >> y2;
1260 r.setCoords(x1, y1, x2, y2);
1261 }
1262 return s;
1263}
1264
1265#endif // QT_NO_DATASTREAM
1266
1267
1268#ifndef QT_NO_DEBUG_STREAM
1270{
1271 QDebugStateSaver saver(dbg);
1272 dbg.nospace();
1273 dbg << "QRect" << '(';
1275 dbg << ')';
1276 return dbg;
1277}
1278#endif
1279
1424/*****************************************************************************
1425 QRectF member functions
1426 *****************************************************************************/
1427
1523{
1524 QRectF r = *this;
1525 if (r.w < 0) {
1526 r.xp += r.w;
1527 r.w = -r.w;
1528 }
1529 if (r.h < 0) {
1530 r.yp += r.h;
1531 r.h = -r.h;
1532 }
1533 return r;
1534}
1535
1947bool QRectF::contains(const QPointF &p) const noexcept
1948{
1949 qreal l = xp;
1950 qreal r = xp;
1951 if (w < 0)
1952 l += w;
1953 else
1954 r += w;
1955 if (l == r) // null rect
1956 return false;
1957
1958 if (p.x() < l || p.x() > r)
1959 return false;
1960
1961 qreal t = yp;
1962 qreal b = yp;
1963 if (h < 0)
1964 t += h;
1965 else
1966 b += h;
1967 if (t == b) // null rect
1968 return false;
1969
1970 if (p.y() < t || p.y() > b)
1971 return false;
1972
1973 return true;
1974}
1975
1976
1993bool QRectF::contains(const QRectF &r) const noexcept
1994{
1995 qreal l1 = xp;
1996 qreal r1 = xp;
1997 if (w < 0)
1998 l1 += w;
1999 else
2000 r1 += w;
2001 if (l1 == r1) // null rect
2002 return false;
2003
2004 qreal l2 = r.xp;
2005 qreal r2 = r.xp;
2006 if (r.w < 0)
2007 l2 += r.w;
2008 else
2009 r2 += r.w;
2010 if (l2 == r2) // null rect
2011 return false;
2012
2013 if (l2 < l1 || r2 > r1)
2014 return false;
2015
2016 qreal t1 = yp;
2017 qreal b1 = yp;
2018 if (h < 0)
2019 t1 += h;
2020 else
2021 b1 += h;
2022 if (t1 == b1) // null rect
2023 return false;
2024
2025 qreal t2 = r.yp;
2026 qreal b2 = r.yp;
2027 if (r.h < 0)
2028 t2 += r.h;
2029 else
2030 b2 += r.h;
2031 if (t2 == b2) // null rect
2032 return false;
2033
2034 if (t2 < t1 || b2 > b1)
2035 return false;
2036
2037 return true;
2038}
2039
2131QRectF QRectF::operator|(const QRectF &r) const noexcept
2132{
2133 if (isNull())
2134 return r;
2135 if (r.isNull())
2136 return *this;
2137
2138 qreal left = xp;
2139 qreal right = xp;
2140 if (w < 0)
2141 left += w;
2142 else
2143 right += w;
2144
2145 if (r.w < 0) {
2146 left = qMin(left, r.xp + r.w);
2147 right = qMax(right, r.xp);
2148 } else {
2149 left = qMin(left, r.xp);
2150 right = qMax(right, r.xp + r.w);
2151 }
2152
2153 qreal top = yp;
2154 qreal bottom = yp;
2155 if (h < 0)
2156 top += h;
2157 else
2158 bottom += h;
2159
2160 if (r.h < 0) {
2161 top = qMin(top, r.yp + r.h);
2162 bottom = qMax(bottom, r.yp);
2163 } else {
2164 top = qMin(top, r.yp);
2165 bottom = qMax(bottom, r.yp + r.h);
2166 }
2167
2168 return QRectF(left, top, right - left, bottom - top);
2169}
2170
2193QRectF QRectF::operator&(const QRectF &r) const noexcept
2194{
2195 qreal l1 = xp;
2196 qreal r1 = xp;
2197 if (w < 0)
2198 l1 += w;
2199 else
2200 r1 += w;
2201 if (l1 == r1) // null rect
2202 return QRectF();
2203
2204 qreal l2 = r.xp;
2205 qreal r2 = r.xp;
2206 if (r.w < 0)
2207 l2 += r.w;
2208 else
2209 r2 += r.w;
2210 if (l2 == r2) // null rect
2211 return QRectF();
2212
2213 if (l1 >= r2 || l2 >= r1)
2214 return QRectF();
2215
2216 qreal t1 = yp;
2217 qreal b1 = yp;
2218 if (h < 0)
2219 t1 += h;
2220 else
2221 b1 += h;
2222 if (t1 == b1) // null rect
2223 return QRectF();
2224
2225 qreal t2 = r.yp;
2226 qreal b2 = r.yp;
2227 if (r.h < 0)
2228 t2 += r.h;
2229 else
2230 b2 += r.h;
2231 if (t2 == b2) // null rect
2232 return QRectF();
2233
2234 if (t1 >= b2 || t2 >= b1)
2235 return QRectF();
2236
2237 QRectF tmp;
2238 tmp.xp = qMax(l1, l2);
2239 tmp.yp = qMax(t1, t2);
2240 tmp.w = qMin(r1, r2) - tmp.xp;
2241 tmp.h = qMin(b1, b2) - tmp.yp;
2242 return tmp;
2243}
2244
2271bool QRectF::intersects(const QRectF &r) const noexcept
2272{
2273 qreal l1 = xp;
2274 qreal r1 = xp;
2275 if (w < 0)
2276 l1 += w;
2277 else
2278 r1 += w;
2279 if (l1 == r1) // null rect
2280 return false;
2281
2282 qreal l2 = r.xp;
2283 qreal r2 = r.xp;
2284 if (r.w < 0)
2285 l2 += r.w;
2286 else
2287 r2 += r.w;
2288 if (l2 == r2) // null rect
2289 return false;
2290
2291 if (l1 >= r2 || l2 >= r1)
2292 return false;
2293
2294 qreal t1 = yp;
2295 qreal b1 = yp;
2296 if (h < 0)
2297 t1 += h;
2298 else
2299 b1 += h;
2300 if (t1 == b1) // null rect
2301 return false;
2302
2303 qreal t2 = r.yp;
2304 qreal b2 = r.yp;
2305 if (r.h < 0)
2306 t2 += r.h;
2307 else
2308 b2 += r.h;
2309 if (t2 == b2) // null rect
2310 return false;
2311
2312 if (t1 >= b2 || t2 >= b1)
2313 return false;
2314
2315 return true;
2316}
2317
2339{
2340 int xmin = int(qFloor(xp));
2341 int xmax = int(qCeil(xp + w));
2342 int ymin = int(qFloor(yp));
2343 int ymax = int(qCeil(yp + h));
2344 return QRect(xmin, ymin, xmax - xmin, ymax - ymin);
2345}
2346
2456/*****************************************************************************
2457 QRectF stream functions
2458 *****************************************************************************/
2459#ifndef QT_NO_DATASTREAM
2472{
2473 s << double(r.x()) << double(r.y()) << double(r.width()) << double(r.height());
2474 return s;
2475}
2476
2489{
2490 double x, y, w, h;
2491 s >> x;
2492 s >> y;
2493 s >> w;
2494 s >> h;
2495 r.setRect(qreal(x), qreal(y), qreal(w), qreal(h));
2496 return s;
2497}
2498
2499#endif // QT_NO_DATASTREAM
2500
2501
2502#ifndef QT_NO_DEBUG_STREAM
2504{
2505 QDebugStateSaver saver(dbg);
2506 dbg.nospace();
2507 dbg << "QRectF" << '(';
2509 dbg << ')';
2510 return dbg;
2511}
2512#endif
2513
\inmodule QtCore\reentrant
Definition qdatastream.h:46
\inmodule QtCore
\inmodule QtCore
\inmodule QtCore\reentrant
Definition qpoint.h:217
\inmodule QtCore\reentrant
Definition qpoint.h:25
\inmodule QtCore\reentrant
Definition qrect.h:484
QRectF operator|(const QRectF &r) const noexcept
Returns the bounding rectangle of this rectangle and the given rectangle.
Definition qrect.cpp:2131
QRect toAlignedRect() const noexcept
Definition qrect.cpp:2338
bool contains(const QRectF &r) const noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qrect.cpp:1993
bool intersects(const QRectF &r) const noexcept
Returns true if this rectangle intersects with the given rectangle (i.e.
Definition qrect.cpp:2271
QRectF normalized() const noexcept
Returns a normalized rectangle; i.e., a rectangle that has a non-negative width and height.
Definition qrect.cpp:1522
QRectF operator&(const QRectF &r) const noexcept
Returns the intersection of this rectangle and the given rectangle.
Definition qrect.cpp:2193
\inmodule QtCore\reentrant
Definition qrect.h:30
bool intersects(const QRect &r) const noexcept
Returns true if this rectangle intersects with the given rectangle (i.e., there is at least one pixel...
Definition qrect.cpp:1069
QRect normalized() const noexcept
Returns a normalized rectangle; i.e., a rectangle that has a non-negative width and height.
Definition qrect.cpp:277
QRect operator&(const QRect &r) const noexcept
Returns the intersection of this rectangle and the given rectangle.
Definition qrect.cpp:997
bool contains(const QRect &r, bool proper=false) const noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qrect.cpp:855
QRect operator|(const QRect &r) const noexcept
Returns the bounding rectangle of this rectangle and the given rectangle.
Definition qrect.cpp:933
Combined button and popup list for selecting options.
static void formatQRect(QDebug &debug, const Rect &rect)
Definition qdebug_p.h:45
int qFloor(T v)
Definition qmath.h:42
int qCeil(T v)
Definition qmath.h:36
constexpr const T & qMin(const T &a, const T &b)
Definition qminmax.h:40
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
GLboolean GLboolean GLboolean b
GLint GLint GLint GLint GLint x
[0]
GLfloat GLfloat GLfloat w
[0]
GLuint GLfloat GLfloat GLfloat GLfloat y1
GLboolean r
[2]
GLuint GLfloat GLfloat GLfloat x1
GLdouble GLdouble GLdouble GLdouble top
GLdouble GLdouble right
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t1
[4]
GLint left
GLint GLint bottom
GLint y
GLfloat GLfloat GLfloat GLfloat h
GLdouble s
[6]
Definition qopenglext.h:235
GLfixed GLfixed GLfixed y2
GLfixed GLfixed x2
GLdouble GLdouble t
Definition qopenglext.h:243
GLfloat GLfloat p
[1]
QDataStream & operator<<(QDataStream &s, const QRect &r)
Definition qrect.cpp:1229
QDataStream & operator>>(QDataStream &s, QRect &r)
Definition qrect.cpp:1250
#define t2
short qint16
Definition qtypes.h:47
int qint32
Definition qtypes.h:49
double qreal
Definition qtypes.h:187
QRect r1(100, 200, 11, 16)
[0]
QRect r2(QPoint(100, 200), QSize(11, 16))