| 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 | #ifndef QRECT_H |
| 5 | #define QRECT_H |
| 6 | |
| 7 | #include <QtCore/qhashfunctions.h> |
| 8 | #include <QtCore/qmargins.h> |
| 9 | #include <QtCore/qsize.h> |
| 10 | #include <QtCore/qpoint.h> |
| 11 | |
| 12 | #ifdef topLeft |
| 13 | #error qrect.h must be included before any header file that defines topLeft |
| 14 | #endif |
| 15 | |
| 16 | #if defined(Q_OS_DARWIN) || defined(Q_QDOC) |
| 17 | struct CGRect; |
| 18 | #endif |
| 19 | #if defined(Q_OS_WASM) || defined(Q_QDOC) |
| 20 | namespace emscripten { |
| 21 | class val; |
| 22 | } |
| 23 | #endif |
| 24 | |
| 25 | QT_BEGIN_NAMESPACE |
| 26 | |
| 27 | class QRectF; |
| 28 | |
| 29 | class Q_CORE_EXPORT QRect |
| 30 | { |
| 31 | public: |
| 32 | constexpr QRect() noexcept : x1(0), y1(0), x2(-1), y2(-1) {} |
| 33 | constexpr QRect(const QPoint &topleft, const QPoint &bottomright) noexcept; |
| 34 | constexpr QRect(const QPoint &topleft, const QSize &size) noexcept; |
| 35 | constexpr QRect(int left, int top, int width, int height) noexcept; |
| 36 | |
| 37 | constexpr inline bool isNull() const noexcept; |
| 38 | constexpr inline bool isEmpty() const noexcept; |
| 39 | constexpr inline bool isValid() const noexcept; |
| 40 | |
| 41 | constexpr inline int left() const noexcept; |
| 42 | constexpr inline int top() const noexcept; |
| 43 | constexpr inline int right() const noexcept; |
| 44 | constexpr inline int bottom() const noexcept; |
| 45 | [[nodiscard]] QRect normalized() const noexcept; |
| 46 | |
| 47 | constexpr inline int x() const noexcept; |
| 48 | constexpr inline int y() const noexcept; |
| 49 | constexpr inline void setLeft(int pos) noexcept; |
| 50 | constexpr inline void setTop(int pos) noexcept; |
| 51 | constexpr inline void setRight(int pos) noexcept; |
| 52 | constexpr inline void setBottom(int pos) noexcept; |
| 53 | constexpr inline void setX(int x) noexcept; |
| 54 | constexpr inline void setY(int y) noexcept; |
| 55 | |
| 56 | constexpr inline void setTopLeft(const QPoint &p) noexcept; |
| 57 | constexpr inline void setBottomRight(const QPoint &p) noexcept; |
| 58 | constexpr inline void setTopRight(const QPoint &p) noexcept; |
| 59 | constexpr inline void setBottomLeft(const QPoint &p) noexcept; |
| 60 | |
| 61 | constexpr inline QPoint topLeft() const noexcept; |
| 62 | constexpr inline QPoint bottomRight() const noexcept; |
| 63 | constexpr inline QPoint topRight() const noexcept; |
| 64 | constexpr inline QPoint bottomLeft() const noexcept; |
| 65 | constexpr inline QPoint center() const noexcept; |
| 66 | |
| 67 | constexpr inline void moveLeft(int pos) noexcept; |
| 68 | constexpr inline void moveTop(int pos) noexcept; |
| 69 | constexpr inline void moveRight(int pos) noexcept; |
| 70 | constexpr inline void moveBottom(int pos) noexcept; |
| 71 | constexpr inline void moveTopLeft(const QPoint &p) noexcept; |
| 72 | constexpr inline void moveBottomRight(const QPoint &p) noexcept; |
| 73 | constexpr inline void moveTopRight(const QPoint &p) noexcept; |
| 74 | constexpr inline void moveBottomLeft(const QPoint &p) noexcept; |
| 75 | constexpr inline void moveCenter(const QPoint &p) noexcept; |
| 76 | |
| 77 | constexpr inline void translate(int dx, int dy) noexcept; |
| 78 | constexpr inline void translate(const QPoint &p) noexcept; |
| 79 | [[nodiscard]] constexpr inline QRect translated(int dx, int dy) const noexcept; |
| 80 | [[nodiscard]] constexpr inline QRect translated(const QPoint &p) const noexcept; |
| 81 | [[nodiscard]] constexpr inline QRect transposed() const noexcept; |
| 82 | |
| 83 | constexpr inline void moveTo(int x, int t) noexcept; |
| 84 | constexpr inline void moveTo(const QPoint &p) noexcept; |
| 85 | |
| 86 | constexpr inline void setRect(int x, int y, int w, int h) noexcept; |
| 87 | constexpr inline void getRect(int *x, int *y, int *w, int *h) const; |
| 88 | |
| 89 | constexpr inline void setCoords(int x1, int y1, int x2, int y2) noexcept; |
| 90 | constexpr inline void getCoords(int *x1, int *y1, int *x2, int *y2) const; |
| 91 | |
| 92 | constexpr inline void adjust(int x1, int y1, int x2, int y2) noexcept; |
| 93 | [[nodiscard]] constexpr inline QRect adjusted(int x1, int y1, int x2, int y2) const noexcept; |
| 94 | |
| 95 | constexpr inline QSize size() const noexcept; |
| 96 | constexpr inline int width() const noexcept; |
| 97 | constexpr inline int height() const noexcept; |
| 98 | constexpr inline void setWidth(int w) noexcept; |
| 99 | constexpr inline void setHeight(int h) noexcept; |
| 100 | constexpr inline void setSize(const QSize &s) noexcept; |
| 101 | |
| 102 | QRect operator|(const QRect &r) const noexcept; |
| 103 | QRect operator&(const QRect &r) const noexcept; |
| 104 | inline QRect &operator|=(const QRect &r) noexcept; |
| 105 | inline QRect &operator&=(const QRect &r) noexcept; |
| 106 | |
| 107 | bool contains(const QRect &r, bool proper = false) const noexcept; |
| 108 | bool contains(const QPoint &p, bool proper = false) const noexcept; |
| 109 | inline bool contains(int x, int y) const noexcept; |
| 110 | inline bool contains(int x, int y, bool proper) const noexcept; |
| 111 | [[nodiscard]] inline QRect united(const QRect &other) const noexcept; |
| 112 | [[nodiscard]] inline QRect intersected(const QRect &other) const noexcept; |
| 113 | bool intersects(const QRect &r) const noexcept; |
| 114 | |
| 115 | constexpr inline QRect marginsAdded(const QMargins &margins) const noexcept; |
| 116 | constexpr inline QRect marginsRemoved(const QMargins &margins) const noexcept; |
| 117 | constexpr inline QRect &operator+=(const QMargins &margins) noexcept; |
| 118 | constexpr inline QRect &operator-=(const QMargins &margins) noexcept; |
| 119 | |
| 120 | [[nodiscard]] static constexpr inline QRect span(const QPoint &p1, const QPoint &p2) noexcept; |
| 121 | |
| 122 | friend constexpr inline bool operator==(const QRect &r1, const QRect &r2) noexcept |
| 123 | { return r1.x1==r2.x1 && r1.x2==r2.x2 && r1.y1==r2.y1 && r1.y2==r2.y2; } |
| 124 | friend constexpr inline bool operator!=(const QRect &r1, const QRect &r2) noexcept |
| 125 | { return r1.x1!=r2.x1 || r1.x2!=r2.x2 || r1.y1!=r2.y1 || r1.y2!=r2.y2; } |
| 126 | friend constexpr inline size_t qHash(const QRect &, size_t) noexcept; |
| 127 | |
| 128 | #if defined(Q_OS_DARWIN) || defined(Q_QDOC) |
| 129 | [[nodiscard]] CGRect toCGRect() const noexcept; |
| 130 | #endif |
| 131 | [[nodiscard]] constexpr inline QRectF toRectF() const noexcept; |
| 132 | |
| 133 | private: |
| 134 | int x1; |
| 135 | int y1; |
| 136 | int x2; |
| 137 | int y2; |
| 138 | }; |
| 139 | Q_DECLARE_TYPEINFO(QRect, Q_RELOCATABLE_TYPE); |
| 140 | |
| 141 | |
| 142 | /***************************************************************************** |
| 143 | QRect stream functions |
| 144 | *****************************************************************************/ |
| 145 | #ifndef QT_NO_DATASTREAM |
| 146 | Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QRect &); |
| 147 | Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QRect &); |
| 148 | #endif |
| 149 | |
| 150 | /***************************************************************************** |
| 151 | QRect inline member functions |
| 152 | *****************************************************************************/ |
| 153 | |
| 154 | constexpr inline QRect::QRect(int aleft, int atop, int awidth, int aheight) noexcept |
| 155 | : x1(aleft), y1(atop), x2(aleft + awidth - 1), y2(atop + aheight - 1) {} |
| 156 | |
| 157 | constexpr inline QRect::QRect(const QPoint &atopLeft, const QPoint &abottomRight) noexcept |
| 158 | : x1(atopLeft.x()), y1(atopLeft.y()), x2(abottomRight.x()), y2(abottomRight.y()) {} |
| 159 | |
| 160 | constexpr inline QRect::QRect(const QPoint &atopLeft, const QSize &asize) noexcept |
| 161 | : x1(atopLeft.x()), y1(atopLeft.y()), x2(atopLeft.x()+asize.width() - 1), y2(atopLeft.y()+asize.height() - 1) {} |
| 162 | |
| 163 | constexpr inline bool QRect::isNull() const noexcept |
| 164 | { return x2 == x1 - 1 && y2 == y1 - 1; } |
| 165 | |
| 166 | constexpr inline bool QRect::isEmpty() const noexcept |
| 167 | { return x1 > x2 || y1 > y2; } |
| 168 | |
| 169 | constexpr inline bool QRect::isValid() const noexcept |
| 170 | { return x1 <= x2 && y1 <= y2; } |
| 171 | |
| 172 | constexpr inline int QRect::left() const noexcept |
| 173 | { return x1; } |
| 174 | |
| 175 | constexpr inline int QRect::top() const noexcept |
| 176 | { return y1; } |
| 177 | |
| 178 | constexpr inline int QRect::right() const noexcept |
| 179 | { return x2; } |
| 180 | |
| 181 | constexpr inline int QRect::bottom() const noexcept |
| 182 | { return y2; } |
| 183 | |
| 184 | constexpr inline int QRect::x() const noexcept |
| 185 | { return x1; } |
| 186 | |
| 187 | constexpr inline int QRect::y() const noexcept |
| 188 | { return y1; } |
| 189 | |
| 190 | constexpr inline void QRect::setLeft(int pos) noexcept |
| 191 | { x1 = pos; } |
| 192 | |
| 193 | constexpr inline void QRect::setTop(int pos) noexcept |
| 194 | { y1 = pos; } |
| 195 | |
| 196 | constexpr inline void QRect::setRight(int pos) noexcept |
| 197 | { x2 = pos; } |
| 198 | |
| 199 | constexpr inline void QRect::setBottom(int pos) noexcept |
| 200 | { y2 = pos; } |
| 201 | |
| 202 | constexpr inline void QRect::setTopLeft(const QPoint &p) noexcept |
| 203 | { x1 = p.x(); y1 = p.y(); } |
| 204 | |
| 205 | constexpr inline void QRect::setBottomRight(const QPoint &p) noexcept |
| 206 | { x2 = p.x(); y2 = p.y(); } |
| 207 | |
| 208 | constexpr inline void QRect::setTopRight(const QPoint &p) noexcept |
| 209 | { x2 = p.x(); y1 = p.y(); } |
| 210 | |
| 211 | constexpr inline void QRect::setBottomLeft(const QPoint &p) noexcept |
| 212 | { x1 = p.x(); y2 = p.y(); } |
| 213 | |
| 214 | constexpr inline void QRect::setX(int ax) noexcept |
| 215 | { x1 = ax; } |
| 216 | |
| 217 | constexpr inline void QRect::setY(int ay) noexcept |
| 218 | { y1 = ay; } |
| 219 | |
| 220 | constexpr inline QPoint QRect::topLeft() const noexcept |
| 221 | { return QPoint(x1, y1); } |
| 222 | |
| 223 | constexpr inline QPoint QRect::bottomRight() const noexcept |
| 224 | { return QPoint(x2, y2); } |
| 225 | |
| 226 | constexpr inline QPoint QRect::topRight() const noexcept |
| 227 | { return QPoint(x2, y1); } |
| 228 | |
| 229 | constexpr inline QPoint QRect::bottomLeft() const noexcept |
| 230 | { return QPoint(x1, y2); } |
| 231 | |
| 232 | constexpr inline QPoint QRect::center() const noexcept |
| 233 | { return QPoint(int((qint64(x1)+x2)/2), int((qint64(y1)+y2)/2)); } // cast avoids overflow on addition |
| 234 | |
| 235 | constexpr inline int QRect::width() const noexcept |
| 236 | { return x2 - x1 + 1; } |
| 237 | |
| 238 | constexpr inline int QRect::height() const noexcept |
| 239 | { return y2 - y1 + 1; } |
| 240 | |
| 241 | constexpr inline QSize QRect::size() const noexcept |
| 242 | { return QSize(width(), height()); } |
| 243 | |
| 244 | constexpr inline void QRect::translate(int dx, int dy) noexcept |
| 245 | { |
| 246 | x1 += dx; |
| 247 | y1 += dy; |
| 248 | x2 += dx; |
| 249 | y2 += dy; |
| 250 | } |
| 251 | |
| 252 | constexpr inline void QRect::translate(const QPoint &p) noexcept |
| 253 | { |
| 254 | x1 += p.x(); |
| 255 | y1 += p.y(); |
| 256 | x2 += p.x(); |
| 257 | y2 += p.y(); |
| 258 | } |
| 259 | |
| 260 | constexpr inline QRect QRect::translated(int dx, int dy) const noexcept |
| 261 | { return QRect(QPoint(x1 + dx, y1 + dy), QPoint(x2 + dx, y2 + dy)); } |
| 262 | |
| 263 | constexpr inline QRect QRect::translated(const QPoint &p) const noexcept |
| 264 | { return QRect(QPoint(x1 + p.x(), y1 + p.y()), QPoint(x2 + p.x(), y2 + p.y())); } |
| 265 | |
| 266 | constexpr inline QRect QRect::transposed() const noexcept |
| 267 | { return QRect(topLeft(), size().transposed()); } |
| 268 | |
| 269 | constexpr inline void QRect::moveTo(int ax, int ay) noexcept |
| 270 | { |
| 271 | x2 += ax - x1; |
| 272 | y2 += ay - y1; |
| 273 | x1 = ax; |
| 274 | y1 = ay; |
| 275 | } |
| 276 | |
| 277 | constexpr inline void QRect::moveTo(const QPoint &p) noexcept |
| 278 | { |
| 279 | x2 += p.x() - x1; |
| 280 | y2 += p.y() - y1; |
| 281 | x1 = p.x(); |
| 282 | y1 = p.y(); |
| 283 | } |
| 284 | |
| 285 | constexpr inline void QRect::moveLeft(int pos) noexcept |
| 286 | { x2 += (pos - x1); x1 = pos; } |
| 287 | |
| 288 | constexpr inline void QRect::moveTop(int pos) noexcept |
| 289 | { y2 += (pos - y1); y1 = pos; } |
| 290 | |
| 291 | constexpr inline void QRect::moveRight(int pos) noexcept |
| 292 | { |
| 293 | x1 += (pos - x2); |
| 294 | x2 = pos; |
| 295 | } |
| 296 | |
| 297 | constexpr inline void QRect::moveBottom(int pos) noexcept |
| 298 | { |
| 299 | y1 += (pos - y2); |
| 300 | y2 = pos; |
| 301 | } |
| 302 | |
| 303 | constexpr inline void QRect::moveTopLeft(const QPoint &p) noexcept |
| 304 | { |
| 305 | moveLeft(pos: p.x()); |
| 306 | moveTop(pos: p.y()); |
| 307 | } |
| 308 | |
| 309 | constexpr inline void QRect::moveBottomRight(const QPoint &p) noexcept |
| 310 | { |
| 311 | moveRight(pos: p.x()); |
| 312 | moveBottom(pos: p.y()); |
| 313 | } |
| 314 | |
| 315 | constexpr inline void QRect::moveTopRight(const QPoint &p) noexcept |
| 316 | { |
| 317 | moveRight(pos: p.x()); |
| 318 | moveTop(pos: p.y()); |
| 319 | } |
| 320 | |
| 321 | constexpr inline void QRect::moveBottomLeft(const QPoint &p) noexcept |
| 322 | { |
| 323 | moveLeft(pos: p.x()); |
| 324 | moveBottom(pos: p.y()); |
| 325 | } |
| 326 | |
| 327 | constexpr inline void QRect::moveCenter(const QPoint &p) noexcept |
| 328 | { |
| 329 | int w = x2 - x1; |
| 330 | int h = y2 - y1; |
| 331 | x1 = p.x() - w/2; |
| 332 | y1 = p.y() - h/2; |
| 333 | x2 = x1 + w; |
| 334 | y2 = y1 + h; |
| 335 | } |
| 336 | |
| 337 | constexpr inline void QRect::getRect(int *ax, int *ay, int *aw, int *ah) const |
| 338 | { |
| 339 | *ax = x1; |
| 340 | *ay = y1; |
| 341 | *aw = x2 - x1 + 1; |
| 342 | *ah = y2 - y1 + 1; |
| 343 | } |
| 344 | |
| 345 | constexpr inline void QRect::setRect(int ax, int ay, int aw, int ah) noexcept |
| 346 | { |
| 347 | x1 = ax; |
| 348 | y1 = ay; |
| 349 | x2 = (ax + aw - 1); |
| 350 | y2 = (ay + ah - 1); |
| 351 | } |
| 352 | |
| 353 | constexpr inline void QRect::getCoords(int *xp1, int *yp1, int *xp2, int *yp2) const |
| 354 | { |
| 355 | *xp1 = x1; |
| 356 | *yp1 = y1; |
| 357 | *xp2 = x2; |
| 358 | *yp2 = y2; |
| 359 | } |
| 360 | |
| 361 | constexpr inline void QRect::setCoords(int xp1, int yp1, int xp2, int yp2) noexcept |
| 362 | { |
| 363 | x1 = xp1; |
| 364 | y1 = yp1; |
| 365 | x2 = xp2; |
| 366 | y2 = yp2; |
| 367 | } |
| 368 | |
| 369 | constexpr inline QRect QRect::adjusted(int xp1, int yp1, int xp2, int yp2) const noexcept |
| 370 | { return QRect(QPoint(x1 + xp1, y1 + yp1), QPoint(x2 + xp2, y2 + yp2)); } |
| 371 | |
| 372 | constexpr inline void QRect::adjust(int dx1, int dy1, int dx2, int dy2) noexcept |
| 373 | { |
| 374 | x1 += dx1; |
| 375 | y1 += dy1; |
| 376 | x2 += dx2; |
| 377 | y2 += dy2; |
| 378 | } |
| 379 | |
| 380 | constexpr inline void QRect::setWidth(int w) noexcept |
| 381 | { x2 = (x1 + w - 1); } |
| 382 | |
| 383 | constexpr inline void QRect::setHeight(int h) noexcept |
| 384 | { y2 = (y1 + h - 1); } |
| 385 | |
| 386 | constexpr inline void QRect::setSize(const QSize &s) noexcept |
| 387 | { |
| 388 | x2 = (s.width() + x1 - 1); |
| 389 | y2 = (s.height() + y1 - 1); |
| 390 | } |
| 391 | |
| 392 | inline bool QRect::contains(int ax, int ay, bool aproper) const noexcept |
| 393 | { |
| 394 | return contains(p: QPoint(ax, ay), proper: aproper); |
| 395 | } |
| 396 | |
| 397 | inline bool QRect::contains(int ax, int ay) const noexcept |
| 398 | { |
| 399 | return contains(p: QPoint(ax, ay), proper: false); |
| 400 | } |
| 401 | |
| 402 | inline QRect &QRect::operator|=(const QRect &r) noexcept |
| 403 | { |
| 404 | *this = *this | r; |
| 405 | return *this; |
| 406 | } |
| 407 | |
| 408 | inline QRect &QRect::operator&=(const QRect &r) noexcept |
| 409 | { |
| 410 | *this = *this & r; |
| 411 | return *this; |
| 412 | } |
| 413 | |
| 414 | inline QRect QRect::intersected(const QRect &other) const noexcept |
| 415 | { |
| 416 | return *this & other; |
| 417 | } |
| 418 | |
| 419 | inline QRect QRect::united(const QRect &r) const noexcept |
| 420 | { |
| 421 | return *this | r; |
| 422 | } |
| 423 | |
| 424 | constexpr inline size_t qHash(const QRect &r, size_t seed = 0) noexcept |
| 425 | { |
| 426 | return qHashMulti(seed, args: r.x1, args: r.x2, args: r.y1, args: r.y2); |
| 427 | } |
| 428 | |
| 429 | constexpr inline QRect operator+(const QRect &rectangle, const QMargins &margins) noexcept |
| 430 | { |
| 431 | return QRect(QPoint(rectangle.left() - margins.left(), rectangle.top() - margins.top()), |
| 432 | QPoint(rectangle.right() + margins.right(), rectangle.bottom() + margins.bottom())); |
| 433 | } |
| 434 | |
| 435 | constexpr inline QRect operator+(const QMargins &margins, const QRect &rectangle) noexcept |
| 436 | { |
| 437 | return QRect(QPoint(rectangle.left() - margins.left(), rectangle.top() - margins.top()), |
| 438 | QPoint(rectangle.right() + margins.right(), rectangle.bottom() + margins.bottom())); |
| 439 | } |
| 440 | |
| 441 | constexpr inline QRect operator-(const QRect &lhs, const QMargins &rhs) noexcept |
| 442 | { |
| 443 | return QRect(QPoint(lhs.left() + rhs.left(), lhs.top() + rhs.top()), |
| 444 | QPoint(lhs.right() - rhs.right(), lhs.bottom() - rhs.bottom())); |
| 445 | } |
| 446 | |
| 447 | constexpr inline QRect QRect::marginsAdded(const QMargins &margins) const noexcept |
| 448 | { |
| 449 | return QRect(QPoint(x1 - margins.left(), y1 - margins.top()), |
| 450 | QPoint(x2 + margins.right(), y2 + margins.bottom())); |
| 451 | } |
| 452 | |
| 453 | constexpr inline QRect QRect::marginsRemoved(const QMargins &margins) const noexcept |
| 454 | { |
| 455 | return QRect(QPoint(x1 + margins.left(), y1 + margins.top()), |
| 456 | QPoint(x2 - margins.right(), y2 - margins.bottom())); |
| 457 | } |
| 458 | |
| 459 | constexpr inline QRect &QRect::operator+=(const QMargins &margins) noexcept |
| 460 | { |
| 461 | *this = marginsAdded(margins); |
| 462 | return *this; |
| 463 | } |
| 464 | |
| 465 | constexpr inline QRect &QRect::operator-=(const QMargins &margins) noexcept |
| 466 | { |
| 467 | *this = marginsRemoved(margins); |
| 468 | return *this; |
| 469 | } |
| 470 | |
| 471 | constexpr QRect QRect::span(const QPoint &p1, const QPoint &p2) noexcept |
| 472 | { |
| 473 | return QRect(QPoint(qMin(a: p1.x(), b: p2.x()), qMin(a: p1.y(), b: p2.y())), |
| 474 | QPoint(qMax(a: p1.x(), b: p2.x()), qMax(a: p1.y(), b: p2.y()))); |
| 475 | } |
| 476 | |
| 477 | #ifndef QT_NO_DEBUG_STREAM |
| 478 | Q_CORE_EXPORT QDebug operator<<(QDebug, const QRect &); |
| 479 | #endif |
| 480 | |
| 481 | |
| 482 | class Q_CORE_EXPORT QRectF |
| 483 | { |
| 484 | public: |
| 485 | constexpr QRectF() noexcept : xp(0.), yp(0.), w(0.), h(0.) {} |
| 486 | constexpr QRectF(const QPointF &topleft, const QSizeF &size) noexcept; |
| 487 | constexpr QRectF(const QPointF &topleft, const QPointF &bottomRight) noexcept; |
| 488 | constexpr QRectF(qreal left, qreal top, qreal width, qreal height) noexcept; |
| 489 | constexpr QRectF(const QRect &rect) noexcept; |
| 490 | |
| 491 | constexpr inline bool isNull() const noexcept; |
| 492 | constexpr inline bool isEmpty() const noexcept; |
| 493 | constexpr inline bool isValid() const noexcept; |
| 494 | [[nodiscard]] QRectF normalized() const noexcept; |
| 495 | |
| 496 | constexpr inline qreal left() const noexcept { return xp; } |
| 497 | constexpr inline qreal top() const noexcept { return yp; } |
| 498 | constexpr inline qreal right() const noexcept { return xp + w; } |
| 499 | constexpr inline qreal bottom() const noexcept { return yp + h; } |
| 500 | |
| 501 | constexpr inline qreal x() const noexcept; |
| 502 | constexpr inline qreal y() const noexcept; |
| 503 | constexpr inline void setLeft(qreal pos) noexcept; |
| 504 | constexpr inline void setTop(qreal pos) noexcept; |
| 505 | constexpr inline void setRight(qreal pos) noexcept; |
| 506 | constexpr inline void setBottom(qreal pos) noexcept; |
| 507 | constexpr inline void setX(qreal pos) noexcept { setLeft(pos); } |
| 508 | constexpr inline void setY(qreal pos) noexcept { setTop(pos); } |
| 509 | |
| 510 | constexpr inline QPointF topLeft() const noexcept { return QPointF(xp, yp); } |
| 511 | constexpr inline QPointF bottomRight() const noexcept { return QPointF(xp+w, yp+h); } |
| 512 | constexpr inline QPointF topRight() const noexcept { return QPointF(xp+w, yp); } |
| 513 | constexpr inline QPointF bottomLeft() const noexcept { return QPointF(xp, yp+h); } |
| 514 | constexpr inline QPointF center() const noexcept; |
| 515 | |
| 516 | constexpr inline void setTopLeft(const QPointF &p) noexcept; |
| 517 | constexpr inline void setBottomRight(const QPointF &p) noexcept; |
| 518 | constexpr inline void setTopRight(const QPointF &p) noexcept; |
| 519 | constexpr inline void setBottomLeft(const QPointF &p) noexcept; |
| 520 | |
| 521 | constexpr inline void moveLeft(qreal pos) noexcept; |
| 522 | constexpr inline void moveTop(qreal pos) noexcept; |
| 523 | constexpr inline void moveRight(qreal pos) noexcept; |
| 524 | constexpr inline void moveBottom(qreal pos) noexcept; |
| 525 | constexpr inline void moveTopLeft(const QPointF &p) noexcept; |
| 526 | constexpr inline void moveBottomRight(const QPointF &p) noexcept; |
| 527 | constexpr inline void moveTopRight(const QPointF &p) noexcept; |
| 528 | constexpr inline void moveBottomLeft(const QPointF &p) noexcept; |
| 529 | constexpr inline void moveCenter(const QPointF &p) noexcept; |
| 530 | |
| 531 | constexpr inline void translate(qreal dx, qreal dy) noexcept; |
| 532 | constexpr inline void translate(const QPointF &p) noexcept; |
| 533 | |
| 534 | [[nodiscard]] constexpr inline QRectF translated(qreal dx, qreal dy) const noexcept; |
| 535 | [[nodiscard]] constexpr inline QRectF translated(const QPointF &p) const noexcept; |
| 536 | |
| 537 | [[nodiscard]] constexpr inline QRectF transposed() const noexcept; |
| 538 | |
| 539 | constexpr inline void moveTo(qreal x, qreal y) noexcept; |
| 540 | constexpr inline void moveTo(const QPointF &p) noexcept; |
| 541 | |
| 542 | constexpr inline void setRect(qreal x, qreal y, qreal w, qreal h) noexcept; |
| 543 | constexpr inline void getRect(qreal *x, qreal *y, qreal *w, qreal *h) const; |
| 544 | |
| 545 | constexpr inline void setCoords(qreal x1, qreal y1, qreal x2, qreal y2) noexcept; |
| 546 | constexpr inline void getCoords(qreal *x1, qreal *y1, qreal *x2, qreal *y2) const; |
| 547 | |
| 548 | constexpr inline void adjust(qreal x1, qreal y1, qreal x2, qreal y2) noexcept; |
| 549 | [[nodiscard]] constexpr inline QRectF adjusted(qreal x1, qreal y1, qreal x2, qreal y2) const noexcept; |
| 550 | |
| 551 | constexpr inline QSizeF size() const noexcept; |
| 552 | constexpr inline qreal width() const noexcept; |
| 553 | constexpr inline qreal height() const noexcept; |
| 554 | constexpr inline void setWidth(qreal w) noexcept; |
| 555 | constexpr inline void setHeight(qreal h) noexcept; |
| 556 | constexpr inline void setSize(const QSizeF &s) noexcept; |
| 557 | |
| 558 | QRectF operator|(const QRectF &r) const noexcept; |
| 559 | QRectF operator&(const QRectF &r) const noexcept; |
| 560 | inline QRectF &operator|=(const QRectF &r) noexcept; |
| 561 | inline QRectF &operator&=(const QRectF &r) noexcept; |
| 562 | |
| 563 | bool contains(const QRectF &r) const noexcept; |
| 564 | bool contains(const QPointF &p) const noexcept; |
| 565 | inline bool contains(qreal x, qreal y) const noexcept; |
| 566 | [[nodiscard]] inline QRectF united(const QRectF &other) const noexcept; |
| 567 | [[nodiscard]] inline QRectF intersected(const QRectF &other) const noexcept; |
| 568 | bool intersects(const QRectF &r) const noexcept; |
| 569 | |
| 570 | constexpr inline QRectF marginsAdded(const QMarginsF &margins) const noexcept; |
| 571 | constexpr inline QRectF marginsRemoved(const QMarginsF &margins) const noexcept; |
| 572 | constexpr inline QRectF &operator+=(const QMarginsF &margins) noexcept; |
| 573 | constexpr inline QRectF &operator-=(const QMarginsF &margins) noexcept; |
| 574 | |
| 575 | friend constexpr inline bool operator==(const QRectF &r1, const QRectF &r2) noexcept |
| 576 | { |
| 577 | return r1.topLeft() == r2.topLeft() |
| 578 | && r1.size() == r2.size(); |
| 579 | } |
| 580 | friend constexpr inline bool operator!=(const QRectF &r1, const QRectF &r2) noexcept |
| 581 | { |
| 582 | return r1.topLeft() != r2.topLeft() |
| 583 | || r1.size() != r2.size(); |
| 584 | } |
| 585 | |
| 586 | [[nodiscard]] constexpr inline QRect toRect() const noexcept; |
| 587 | [[nodiscard]] QRect toAlignedRect() const noexcept; |
| 588 | |
| 589 | #if defined(Q_OS_DARWIN) || defined(Q_QDOC) |
| 590 | [[nodiscard]] static QRectF fromCGRect(CGRect rect) noexcept; |
| 591 | [[nodiscard]] CGRect toCGRect() const noexcept; |
| 592 | #endif |
| 593 | |
| 594 | #if defined(Q_OS_WASM) || defined(Q_QDOC) |
| 595 | [[nodiscard]] static QRectF fromDOMRect(emscripten::val domRect); |
| 596 | [[nodiscard]] emscripten::val toDOMRect() const; |
| 597 | #endif |
| 598 | |
| 599 | private: |
| 600 | qreal xp; |
| 601 | qreal yp; |
| 602 | qreal w; |
| 603 | qreal h; |
| 604 | }; |
| 605 | Q_DECLARE_TYPEINFO(QRectF, Q_RELOCATABLE_TYPE); |
| 606 | |
| 607 | |
| 608 | /***************************************************************************** |
| 609 | QRectF stream functions |
| 610 | *****************************************************************************/ |
| 611 | #ifndef QT_NO_DATASTREAM |
| 612 | Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QRectF &); |
| 613 | Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QRectF &); |
| 614 | #endif |
| 615 | |
| 616 | /***************************************************************************** |
| 617 | QRectF inline member functions |
| 618 | *****************************************************************************/ |
| 619 | |
| 620 | constexpr inline QRectF::QRectF(qreal aleft, qreal atop, qreal awidth, qreal aheight) noexcept |
| 621 | : xp(aleft), yp(atop), w(awidth), h(aheight) |
| 622 | { |
| 623 | } |
| 624 | |
| 625 | constexpr inline QRectF::QRectF(const QPointF &atopLeft, const QSizeF &asize) noexcept |
| 626 | : xp(atopLeft.x()), yp(atopLeft.y()), w(asize.width()), h(asize.height()) |
| 627 | { |
| 628 | } |
| 629 | |
| 630 | |
| 631 | constexpr inline QRectF::QRectF(const QPointF &atopLeft, const QPointF &abottomRight) noexcept |
| 632 | : xp(atopLeft.x()), yp(atopLeft.y()), w(abottomRight.x() - atopLeft.x()), h(abottomRight.y() - atopLeft.y()) |
| 633 | { |
| 634 | } |
| 635 | |
| 636 | constexpr inline QRectF::QRectF(const QRect &r) noexcept |
| 637 | : xp(r.x()), yp(r.y()), w(r.width()), h(r.height()) |
| 638 | { |
| 639 | } |
| 640 | |
| 641 | QT_WARNING_PUSH |
| 642 | QT_WARNING_DISABLE_FLOAT_COMPARE |
| 643 | |
| 644 | constexpr inline bool QRectF::isNull() const noexcept |
| 645 | { return w == 0. && h == 0.; } |
| 646 | |
| 647 | constexpr inline bool QRectF::isEmpty() const noexcept |
| 648 | { return w <= 0. || h <= 0.; } |
| 649 | |
| 650 | QT_WARNING_POP |
| 651 | |
| 652 | constexpr inline bool QRectF::isValid() const noexcept |
| 653 | { return w > 0. && h > 0.; } |
| 654 | |
| 655 | constexpr inline qreal QRectF::x() const noexcept |
| 656 | { return xp; } |
| 657 | |
| 658 | constexpr inline qreal QRectF::y() const noexcept |
| 659 | { return yp; } |
| 660 | |
| 661 | constexpr inline void QRectF::setLeft(qreal pos) noexcept |
| 662 | { qreal diff = pos - xp; xp += diff; w -= diff; } |
| 663 | |
| 664 | constexpr inline void QRectF::setRight(qreal pos) noexcept |
| 665 | { w = pos - xp; } |
| 666 | |
| 667 | constexpr inline void QRectF::setTop(qreal pos) noexcept |
| 668 | { qreal diff = pos - yp; yp += diff; h -= diff; } |
| 669 | |
| 670 | constexpr inline void QRectF::setBottom(qreal pos) noexcept |
| 671 | { h = pos - yp; } |
| 672 | |
| 673 | constexpr inline void QRectF::setTopLeft(const QPointF &p) noexcept |
| 674 | { setLeft(p.x()); setTop(p.y()); } |
| 675 | |
| 676 | constexpr inline void QRectF::setTopRight(const QPointF &p) noexcept |
| 677 | { setRight(p.x()); setTop(p.y()); } |
| 678 | |
| 679 | constexpr inline void QRectF::setBottomLeft(const QPointF &p) noexcept |
| 680 | { setLeft(p.x()); setBottom(p.y()); } |
| 681 | |
| 682 | constexpr inline void QRectF::setBottomRight(const QPointF &p) noexcept |
| 683 | { setRight(p.x()); setBottom(p.y()); } |
| 684 | |
| 685 | constexpr inline QPointF QRectF::center() const noexcept |
| 686 | { return QPointF(xp + w/2, yp + h/2); } |
| 687 | |
| 688 | constexpr inline void QRectF::moveLeft(qreal pos) noexcept |
| 689 | { xp = pos; } |
| 690 | |
| 691 | constexpr inline void QRectF::moveTop(qreal pos) noexcept |
| 692 | { yp = pos; } |
| 693 | |
| 694 | constexpr inline void QRectF::moveRight(qreal pos) noexcept |
| 695 | { xp = pos - w; } |
| 696 | |
| 697 | constexpr inline void QRectF::moveBottom(qreal pos) noexcept |
| 698 | { yp = pos - h; } |
| 699 | |
| 700 | constexpr inline void QRectF::moveTopLeft(const QPointF &p) noexcept |
| 701 | { moveLeft(pos: p.x()); moveTop(pos: p.y()); } |
| 702 | |
| 703 | constexpr inline void QRectF::moveTopRight(const QPointF &p) noexcept |
| 704 | { moveRight(pos: p.x()); moveTop(pos: p.y()); } |
| 705 | |
| 706 | constexpr inline void QRectF::moveBottomLeft(const QPointF &p) noexcept |
| 707 | { moveLeft(pos: p.x()); moveBottom(pos: p.y()); } |
| 708 | |
| 709 | constexpr inline void QRectF::moveBottomRight(const QPointF &p) noexcept |
| 710 | { moveRight(pos: p.x()); moveBottom(pos: p.y()); } |
| 711 | |
| 712 | constexpr inline void QRectF::moveCenter(const QPointF &p) noexcept |
| 713 | { xp = p.x() - w/2; yp = p.y() - h/2; } |
| 714 | |
| 715 | constexpr inline qreal QRectF::width() const noexcept |
| 716 | { return w; } |
| 717 | |
| 718 | constexpr inline qreal QRectF::height() const noexcept |
| 719 | { return h; } |
| 720 | |
| 721 | constexpr inline QSizeF QRectF::size() const noexcept |
| 722 | { return QSizeF(w, h); } |
| 723 | |
| 724 | constexpr inline void QRectF::translate(qreal dx, qreal dy) noexcept |
| 725 | { |
| 726 | xp += dx; |
| 727 | yp += dy; |
| 728 | } |
| 729 | |
| 730 | constexpr inline void QRectF::translate(const QPointF &p) noexcept |
| 731 | { |
| 732 | xp += p.x(); |
| 733 | yp += p.y(); |
| 734 | } |
| 735 | |
| 736 | constexpr inline void QRectF::moveTo(qreal ax, qreal ay) noexcept |
| 737 | { |
| 738 | xp = ax; |
| 739 | yp = ay; |
| 740 | } |
| 741 | |
| 742 | constexpr inline void QRectF::moveTo(const QPointF &p) noexcept |
| 743 | { |
| 744 | xp = p.x(); |
| 745 | yp = p.y(); |
| 746 | } |
| 747 | |
| 748 | constexpr inline QRectF QRectF::translated(qreal dx, qreal dy) const noexcept |
| 749 | { |
| 750 | return QRectF(xp + dx, yp + dy, w, h); |
| 751 | } |
| 752 | |
| 753 | constexpr inline QRectF QRectF::translated(const QPointF &p) const noexcept |
| 754 | { return QRectF(xp + p.x(), yp + p.y(), w, h); } |
| 755 | |
| 756 | constexpr inline QRectF QRectF::transposed() const noexcept |
| 757 | { return QRectF(topLeft(), size().transposed()); } |
| 758 | |
| 759 | constexpr inline void QRectF::getRect(qreal *ax, qreal *ay, qreal *aaw, qreal *aah) const |
| 760 | { |
| 761 | *ax = this->xp; |
| 762 | *ay = this->yp; |
| 763 | *aaw = this->w; |
| 764 | *aah = this->h; |
| 765 | } |
| 766 | |
| 767 | constexpr inline void QRectF::setRect(qreal ax, qreal ay, qreal aaw, qreal aah) noexcept |
| 768 | { |
| 769 | this->xp = ax; |
| 770 | this->yp = ay; |
| 771 | this->w = aaw; |
| 772 | this->h = aah; |
| 773 | } |
| 774 | |
| 775 | constexpr inline void QRectF::getCoords(qreal *xp1, qreal *yp1, qreal *xp2, qreal *yp2) const |
| 776 | { |
| 777 | *xp1 = xp; |
| 778 | *yp1 = yp; |
| 779 | *xp2 = xp + w; |
| 780 | *yp2 = yp + h; |
| 781 | } |
| 782 | |
| 783 | constexpr inline void QRectF::setCoords(qreal xp1, qreal yp1, qreal xp2, qreal yp2) noexcept |
| 784 | { |
| 785 | xp = xp1; |
| 786 | yp = yp1; |
| 787 | w = xp2 - xp1; |
| 788 | h = yp2 - yp1; |
| 789 | } |
| 790 | |
| 791 | constexpr inline void QRectF::adjust(qreal xp1, qreal yp1, qreal xp2, qreal yp2) noexcept |
| 792 | { |
| 793 | xp += xp1; |
| 794 | yp += yp1; |
| 795 | w += xp2 - xp1; |
| 796 | h += yp2 - yp1; |
| 797 | } |
| 798 | |
| 799 | constexpr inline QRectF QRectF::adjusted(qreal xp1, qreal yp1, qreal xp2, qreal yp2) const noexcept |
| 800 | { |
| 801 | return QRectF(xp + xp1, yp + yp1, w + xp2 - xp1, h + yp2 - yp1); |
| 802 | } |
| 803 | |
| 804 | constexpr inline void QRectF::setWidth(qreal aw) noexcept |
| 805 | { this->w = aw; } |
| 806 | |
| 807 | constexpr inline void QRectF::setHeight(qreal ah) noexcept |
| 808 | { this->h = ah; } |
| 809 | |
| 810 | constexpr inline void QRectF::setSize(const QSizeF &s) noexcept |
| 811 | { |
| 812 | w = s.width(); |
| 813 | h = s.height(); |
| 814 | } |
| 815 | |
| 816 | inline bool QRectF::contains(qreal ax, qreal ay) const noexcept |
| 817 | { |
| 818 | return contains(p: QPointF(ax, ay)); |
| 819 | } |
| 820 | |
| 821 | inline QRectF &QRectF::operator|=(const QRectF &r) noexcept |
| 822 | { |
| 823 | *this = *this | r; |
| 824 | return *this; |
| 825 | } |
| 826 | |
| 827 | inline QRectF &QRectF::operator&=(const QRectF &r) noexcept |
| 828 | { |
| 829 | *this = *this & r; |
| 830 | return *this; |
| 831 | } |
| 832 | |
| 833 | inline QRectF QRectF::intersected(const QRectF &r) const noexcept |
| 834 | { |
| 835 | return *this & r; |
| 836 | } |
| 837 | |
| 838 | inline QRectF QRectF::united(const QRectF &r) const noexcept |
| 839 | { |
| 840 | return *this | r; |
| 841 | } |
| 842 | |
| 843 | constexpr QRectF QRect::toRectF() const noexcept { return *this; } |
| 844 | |
| 845 | constexpr inline QRect QRectF::toRect() const noexcept |
| 846 | { |
| 847 | // This rounding is designed to minimize the maximum possible difference |
| 848 | // in topLeft(), bottomRight(), and size() after rounding. |
| 849 | // All dimensions are at most off by 0.75, and topLeft by at most 0.5. |
| 850 | const int nxp = qRound(d: xp); |
| 851 | const int nyp = qRound(d: yp); |
| 852 | const int nw = qRound(d: w + (xp - nxp) / 2); |
| 853 | const int nh = qRound(d: h + (yp - nyp) / 2); |
| 854 | return QRect(nxp, nyp, nw, nh); |
| 855 | } |
| 856 | |
| 857 | constexpr inline QRectF operator+(const QRectF &lhs, const QMarginsF &rhs) noexcept |
| 858 | { |
| 859 | return QRectF(QPointF(lhs.left() - rhs.left(), lhs.top() - rhs.top()), |
| 860 | QSizeF(lhs.width() + rhs.left() + rhs.right(), lhs.height() + rhs.top() + rhs.bottom())); |
| 861 | } |
| 862 | |
| 863 | constexpr inline QRectF operator+(const QMarginsF &lhs, const QRectF &rhs) noexcept |
| 864 | { |
| 865 | return QRectF(QPointF(rhs.left() - lhs.left(), rhs.top() - lhs.top()), |
| 866 | QSizeF(rhs.width() + lhs.left() + lhs.right(), rhs.height() + lhs.top() + lhs.bottom())); |
| 867 | } |
| 868 | |
| 869 | constexpr inline QRectF operator-(const QRectF &lhs, const QMarginsF &rhs) noexcept |
| 870 | { |
| 871 | return QRectF(QPointF(lhs.left() + rhs.left(), lhs.top() + rhs.top()), |
| 872 | QSizeF(lhs.width() - rhs.left() - rhs.right(), lhs.height() - rhs.top() - rhs.bottom())); |
| 873 | } |
| 874 | |
| 875 | constexpr inline QRectF QRectF::marginsAdded(const QMarginsF &margins) const noexcept |
| 876 | { |
| 877 | return QRectF(QPointF(xp - margins.left(), yp - margins.top()), |
| 878 | QSizeF(w + margins.left() + margins.right(), h + margins.top() + margins.bottom())); |
| 879 | } |
| 880 | |
| 881 | constexpr inline QRectF QRectF::marginsRemoved(const QMarginsF &margins) const noexcept |
| 882 | { |
| 883 | return QRectF(QPointF(xp + margins.left(), yp + margins.top()), |
| 884 | QSizeF(w - margins.left() - margins.right(), h - margins.top() - margins.bottom())); |
| 885 | } |
| 886 | |
| 887 | constexpr inline QRectF &QRectF::operator+=(const QMarginsF &margins) noexcept |
| 888 | { |
| 889 | *this = marginsAdded(margins); |
| 890 | return *this; |
| 891 | } |
| 892 | |
| 893 | constexpr inline QRectF &QRectF::operator-=(const QMarginsF &margins) noexcept |
| 894 | { |
| 895 | *this = marginsRemoved(margins); |
| 896 | return *this; |
| 897 | } |
| 898 | |
| 899 | #ifndef QT_NO_DEBUG_STREAM |
| 900 | Q_CORE_EXPORT QDebug operator<<(QDebug, const QRectF &); |
| 901 | #endif |
| 902 | |
| 903 | QT_END_NAMESPACE |
| 904 | |
| 905 | #endif // QRECT_H |
| 906 | |