Qt 6.x
The Qt SDK
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
175/*****************************************************************************
176 QRect member functions
177 *****************************************************************************/
178
273QRect QRect::normalized() const noexcept
274{
275 QRect r(*this);
276 if (x2 < x1) { // swap bad x values
277 r.x1 = x2 + 1;
278 r.x2 = x1 - 1;
279 }
280 if (y2 < y1) { // swap bad y values
281 r.y1 = y2 + 1;
282 r.y2 = y1 - 1;
283 }
284 return r;
285}
286
287
787bool QRect::contains(const QPoint &p, bool proper) const noexcept
788{
789 int l, r;
790 if (x2 < x1 - 1) {
791 l = x2 + 1;
792 r = x1 - 1;
793 } else {
794 l = x1;
795 r = x2;
796 }
797 if (proper) {
798 if (p.x() <= l || p.x() >= r)
799 return false;
800 } else {
801 if (p.x() < l || p.x() > r)
802 return false;
803 }
804 int t, b;
805 if (y2 < y1 - 1) {
806 t = y2 + 1;
807 b = y1 - 1;
808 } else {
809 t = y1;
810 b = y2;
811 }
812 if (proper) {
813 if (p.y() <= t || p.y() >= b)
814 return false;
815 } else {
816 if (p.y() < t || p.y() > b)
817 return false;
818 }
819 return true;
820}
821
822
851bool QRect::contains(const QRect &r, bool proper) const noexcept
852{
853 if (isNull() || r.isNull())
854 return false;
855
856 int l1 = x1;
857 int r1 = x1 - 1;
858 if (x2 < x1 - 1)
859 l1 = x2 + 1;
860 else
861 r1 = x2;
862
863 int l2 = r.x1;
864 int r2 = r.x1 - 1;
865 if (r.x2 < r.x1 - 1)
866 l2 = r.x2 + 1;
867 else
868 r2 = r.x2;
869
870 if (proper) {
871 if (l2 <= l1 || r2 >= r1)
872 return false;
873 } else {
874 if (l2 < l1 || r2 > r1)
875 return false;
876 }
877
878 int t1 = y1;
879 int b1 = y1 - 1;
880 if (y2 < y1 - 1)
881 t1 = y2 + 1;
882 else
883 b1 = y2;
884
885 int t2 = r.y1;
886 int b2 = r.y1 - 1;
887 if (r.y2 < r.y1 - 1)
888 t2 = r.y2 + 1;
889 else
890 b2 = r.y2;
891
892 if (proper) {
893 if (t2 <= t1 || b2 >= b1)
894 return false;
895 } else {
896 if (t2 < t1 || b2 > b1)
897 return false;
898 }
899
900 return true;
901}
902
929QRect QRect::operator|(const QRect &r) const noexcept
930{
931 if (isNull())
932 return r;
933 if (r.isNull())
934 return *this;
935
936 int l1 = x1;
937 int r1 = x1 - 1;
938 if (x2 < x1 - 1)
939 l1 = x2 + 1;
940 else
941 r1 = x2;
942
943 int l2 = r.x1;
944 int r2 = r.x1 - 1;
945 if (r.x2 < r.x1 - 1)
946 l2 = r.x2 + 1;
947 else
948 r2 = r.x2;
949
950 int t1 = y1;
951 int b1 = y1 - 1;
952 if (y2 < y1 - 1)
953 t1 = y2 + 1;
954 else
955 b1 = y2;
956
957 int t2 = r.y1;
958 int b2 = r.y1 - 1;
959 if (r.y2 < r.y1 - 1)
960 t2 = r.y2 + 1;
961 else
962 b2 = r.y2;
963
964 QRect tmp;
965 tmp.x1 = qMin(l1, l2);
966 tmp.x2 = qMax(r1, r2);
967 tmp.y1 = qMin(t1, t2);
968 tmp.y2 = qMax(b1, b2);
969 return tmp;
970}
971
993QRect QRect::operator&(const QRect &r) const noexcept
994{
995 if (isNull() || r.isNull())
996 return QRect();
997
998 int l1 = x1;
999 int r1 = x2;
1000 if (x2 < x1 - 1) {
1001 l1 = x2 + 1;
1002 r1 = x1 - 1;
1003 }
1004
1005 int l2 = r.x1;
1006 int r2 = r.x2;
1007 if (r.x2 < r.x1 - 1) {
1008 l2 = r.x2 + 1;
1009 r2 = r.x1 - 1;
1010 }
1011
1012 if (l1 > r2 || l2 > r1)
1013 return QRect();
1014
1015 int t1 = y1;
1016 int b1 = y2;
1017 if (y2 < y1 - 1) {
1018 t1 = y2 + 1;
1019 b1 = y1 - 1;
1020 }
1021
1022 int t2 = r.y1;
1023 int b2 = r.y2;
1024 if (r.y2 < r.y1 - 1) {
1025 t2 = r.y2 + 1;
1026 b2 = r.y1 - 1;
1027 }
1028
1029 if (t1 > b2 || t2 > b1)
1030 return QRect();
1031
1032 QRect tmp;
1033 tmp.x1 = qMax(l1, l2);
1034 tmp.x2 = qMin(r1, r2);
1035 tmp.y1 = qMax(t1, t2);
1036 tmp.y2 = qMin(b1, b2);
1037 return tmp;
1038}
1039
1065bool QRect::intersects(const QRect &r) const noexcept
1066{
1067 if (isNull() || r.isNull())
1068 return false;
1069
1070 int l1 = x1;
1071 int r1 = x2;
1072 if (x2 < x1 - 1) {
1073 l1 = x2 + 1;
1074 r1 = x1 - 1;
1075 }
1076
1077 int l2 = r.x1;
1078 int r2 = r.x2;
1079 if (r.x2 < r.x1 - 1) {
1080 l2 = r.x2 + 1;
1081 r2 = r.x1 - 1;
1082 }
1083
1084 if (l1 > r2 || l2 > r1)
1085 return false;
1086
1087 int t1 = y1;
1088 int b1 = y2;
1089 if (y2 < y1 - 1) {
1090 t1 = y2 + 1;
1091 b1 = y1 - 1;
1092 }
1093
1094 int t2 = r.y1;
1095 int b2 = r.y2;
1096 if (r.y2 < r.y1 - 1) {
1097 t2 = r.y2 + 1;
1098 b2 = r.y1 - 1;
1099 }
1100
1101 if (t1 > b2 || t2 > b1)
1102 return false;
1103
1104 return true;
1105}
1106
1211/*****************************************************************************
1212 QRect stream functions
1213 *****************************************************************************/
1214#ifndef QT_NO_DATASTREAM
1226{
1227 if (s.version() == 1)
1228 s << (qint16)r.left() << (qint16)r.top()
1229 << (qint16)r.right() << (qint16)r.bottom();
1230 else
1231 s << (qint32)r.left() << (qint32)r.top()
1232 << (qint32)r.right() << (qint32)r.bottom();
1233 return s;
1234}
1235
1247{
1248 if (s.version() == 1) {
1249 qint16 x1, y1, x2, y2;
1250 s >> x1; s >> y1; s >> x2; s >> y2;
1251 r.setCoords(x1, y1, x2, y2);
1252 }
1253 else {
1254 qint32 x1, y1, x2, y2;
1255 s >> x1; s >> y1; s >> x2; s >> y2;
1256 r.setCoords(x1, y1, x2, y2);
1257 }
1258 return s;
1259}
1260
1261#endif // QT_NO_DATASTREAM
1262
1263
1264#ifndef QT_NO_DEBUG_STREAM
1266{
1267 QDebugStateSaver saver(dbg);
1268 dbg.nospace();
1269 dbg << "QRect" << '(';
1271 dbg << ')';
1272 return dbg;
1273}
1274#endif
1275
1416/*****************************************************************************
1417 QRectF member functions
1418 *****************************************************************************/
1419
1515{
1516 QRectF r = *this;
1517 if (r.w < 0) {
1518 r.xp += r.w;
1519 r.w = -r.w;
1520 }
1521 if (r.h < 0) {
1522 r.yp += r.h;
1523 r.h = -r.h;
1524 }
1525 return r;
1526}
1527
1939bool QRectF::contains(const QPointF &p) const noexcept
1940{
1941 qreal l = xp;
1942 qreal r = xp;
1943 if (w < 0)
1944 l += w;
1945 else
1946 r += w;
1947 if (l == r) // null rect
1948 return false;
1949
1950 if (p.x() < l || p.x() > r)
1951 return false;
1952
1953 qreal t = yp;
1954 qreal b = yp;
1955 if (h < 0)
1956 t += h;
1957 else
1958 b += h;
1959 if (t == b) // null rect
1960 return false;
1961
1962 if (p.y() < t || p.y() > b)
1963 return false;
1964
1965 return true;
1966}
1967
1968
1985bool QRectF::contains(const QRectF &r) const noexcept
1986{
1987 qreal l1 = xp;
1988 qreal r1 = xp;
1989 if (w < 0)
1990 l1 += w;
1991 else
1992 r1 += w;
1993 if (l1 == r1) // null rect
1994 return false;
1995
1996 qreal l2 = r.xp;
1997 qreal r2 = r.xp;
1998 if (r.w < 0)
1999 l2 += r.w;
2000 else
2001 r2 += r.w;
2002 if (l2 == r2) // null rect
2003 return false;
2004
2005 if (l2 < l1 || r2 > r1)
2006 return false;
2007
2008 qreal t1 = yp;
2009 qreal b1 = yp;
2010 if (h < 0)
2011 t1 += h;
2012 else
2013 b1 += h;
2014 if (t1 == b1) // null rect
2015 return false;
2016
2017 qreal t2 = r.yp;
2018 qreal b2 = r.yp;
2019 if (r.h < 0)
2020 t2 += r.h;
2021 else
2022 b2 += r.h;
2023 if (t2 == b2) // null rect
2024 return false;
2025
2026 if (t2 < t1 || b2 > b1)
2027 return false;
2028
2029 return true;
2030}
2031
2123QRectF QRectF::operator|(const QRectF &r) const noexcept
2124{
2125 if (isNull())
2126 return r;
2127 if (r.isNull())
2128 return *this;
2129
2130 qreal left = xp;
2131 qreal right = xp;
2132 if (w < 0)
2133 left += w;
2134 else
2135 right += w;
2136
2137 if (r.w < 0) {
2138 left = qMin(left, r.xp + r.w);
2139 right = qMax(right, r.xp);
2140 } else {
2141 left = qMin(left, r.xp);
2142 right = qMax(right, r.xp + r.w);
2143 }
2144
2145 qreal top = yp;
2146 qreal bottom = yp;
2147 if (h < 0)
2148 top += h;
2149 else
2150 bottom += h;
2151
2152 if (r.h < 0) {
2153 top = qMin(top, r.yp + r.h);
2154 bottom = qMax(bottom, r.yp);
2155 } else {
2156 top = qMin(top, r.yp);
2157 bottom = qMax(bottom, r.yp + r.h);
2158 }
2159
2160 return QRectF(left, top, right - left, bottom - top);
2161}
2162
2185QRectF QRectF::operator&(const QRectF &r) const noexcept
2186{
2187 qreal l1 = xp;
2188 qreal r1 = xp;
2189 if (w < 0)
2190 l1 += w;
2191 else
2192 r1 += w;
2193 if (l1 == r1) // null rect
2194 return QRectF();
2195
2196 qreal l2 = r.xp;
2197 qreal r2 = r.xp;
2198 if (r.w < 0)
2199 l2 += r.w;
2200 else
2201 r2 += r.w;
2202 if (l2 == r2) // null rect
2203 return QRectF();
2204
2205 if (l1 >= r2 || l2 >= r1)
2206 return QRectF();
2207
2208 qreal t1 = yp;
2209 qreal b1 = yp;
2210 if (h < 0)
2211 t1 += h;
2212 else
2213 b1 += h;
2214 if (t1 == b1) // null rect
2215 return QRectF();
2216
2217 qreal t2 = r.yp;
2218 qreal b2 = r.yp;
2219 if (r.h < 0)
2220 t2 += r.h;
2221 else
2222 b2 += r.h;
2223 if (t2 == b2) // null rect
2224 return QRectF();
2225
2226 if (t1 >= b2 || t2 >= b1)
2227 return QRectF();
2228
2229 QRectF tmp;
2230 tmp.xp = qMax(l1, l2);
2231 tmp.yp = qMax(t1, t2);
2232 tmp.w = qMin(r1, r2) - tmp.xp;
2233 tmp.h = qMin(b1, b2) - tmp.yp;
2234 return tmp;
2235}
2236
2263bool QRectF::intersects(const QRectF &r) const noexcept
2264{
2265 qreal l1 = xp;
2266 qreal r1 = xp;
2267 if (w < 0)
2268 l1 += w;
2269 else
2270 r1 += w;
2271 if (l1 == r1) // null rect
2272 return false;
2273
2274 qreal l2 = r.xp;
2275 qreal r2 = r.xp;
2276 if (r.w < 0)
2277 l2 += r.w;
2278 else
2279 r2 += r.w;
2280 if (l2 == r2) // null rect
2281 return false;
2282
2283 if (l1 >= r2 || l2 >= r1)
2284 return false;
2285
2286 qreal t1 = yp;
2287 qreal b1 = yp;
2288 if (h < 0)
2289 t1 += h;
2290 else
2291 b1 += h;
2292 if (t1 == b1) // null rect
2293 return false;
2294
2295 qreal t2 = r.yp;
2296 qreal b2 = r.yp;
2297 if (r.h < 0)
2298 t2 += r.h;
2299 else
2300 b2 += r.h;
2301 if (t2 == b2) // null rect
2302 return false;
2303
2304 if (t1 >= b2 || t2 >= b1)
2305 return false;
2306
2307 return true;
2308}
2309
2331{
2332 int xmin = int(qFloor(xp));
2333 int xmax = int(qCeil(xp + w));
2334 int ymin = int(qFloor(yp));
2335 int ymax = int(qCeil(yp + h));
2336 return QRect(xmin, ymin, xmax - xmin, ymax - ymin);
2337}
2338
2432/*****************************************************************************
2433 QRectF stream functions
2434 *****************************************************************************/
2435#ifndef QT_NO_DATASTREAM
2448{
2449 s << double(r.x()) << double(r.y()) << double(r.width()) << double(r.height());
2450 return s;
2451}
2452
2465{
2466 double x, y, w, h;
2467 s >> x;
2468 s >> y;
2469 s >> w;
2470 s >> h;
2471 r.setRect(qreal(x), qreal(y), qreal(w), qreal(h));
2472 return s;
2473}
2474
2475#endif // QT_NO_DATASTREAM
2476
2477
2478#ifndef QT_NO_DEBUG_STREAM
2480{
2481 QDebugStateSaver saver(dbg);
2482 dbg.nospace();
2483 dbg << "QRectF" << '(';
2485 dbg << ')';
2486 return dbg;
2487}
2488#endif
2489
\inmodule QtCore\reentrant
Definition qdatastream.h:30
\inmodule QtCore
\inmodule QtCore
\inmodule QtCore\reentrant
Definition qpoint.h:214
\inmodule QtCore\reentrant
Definition qpoint.h:23
\inmodule QtCore\reentrant
Definition qrect.h:483
QRectF operator|(const QRectF &r) const noexcept
Returns the bounding rectangle of this rectangle and the given rectangle.
Definition qrect.cpp:2123
QRect toAlignedRect() const noexcept
Definition qrect.cpp:2330
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:1985
bool intersects(const QRectF &r) const noexcept
Returns true if this rectangle intersects with the given rectangle (i.e.
Definition qrect.cpp:2263
QRectF normalized() const noexcept
Returns a normalized rectangle; i.e., a rectangle that has a non-negative width and height.
Definition qrect.cpp:1514
QRectF operator&(const QRectF &r) const noexcept
Returns the intersection of this rectangle and the given rectangle.
Definition qrect.cpp:2185
\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:1065
QRect normalized() const noexcept
Returns a normalized rectangle; i.e., a rectangle that has a non-negative width and height.
Definition qrect.cpp:273
QRect operator&(const QRect &r) const noexcept
Returns the intersection of this rectangle and the given rectangle.
Definition qrect.cpp:993
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:851
QRect operator|(const QRect &r) const noexcept
Returns the bounding rectangle of this rectangle and the given rectangle.
Definition qrect.cpp:929
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
GLfixed GLfixed GLfixed y2
GLfixed GLfixed x2
GLdouble GLdouble t
Definition qopenglext.h:243
GLdouble s
[6]
Definition qopenglext.h:235
GLfloat GLfloat p
[1]
QDataStream & operator<<(QDataStream &s, const QRect &r)
Definition qrect.cpp:1225
QDataStream & operator>>(QDataStream &s, QRect &r)
Definition qrect.cpp:1246
#define t2
short qint16
Definition qtypes.h:42
int qint32
Definition qtypes.h:44
double qreal
Definition qtypes.h:92
QRect r1(100, 200, 11, 16)
[0]
QRect r2(QPoint(100, 200), QSize(11, 16))