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