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