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 QMARGINS_H
41#define QMARGINS_H
42
43#include <QtCore/qnamespace.h>
44
45QT_BEGIN_NAMESPACE
46
47/*****************************************************************************
48 QMargins class
49 *****************************************************************************/
50
51class QMargins
52{
53public:
54 Q_DECL_CONSTEXPR QMargins() noexcept;
55 Q_DECL_CONSTEXPR QMargins(int left, int top, int right, int bottom) noexcept;
56
57 Q_DECL_CONSTEXPR bool isNull() const noexcept;
58
59 Q_DECL_CONSTEXPR int left() const noexcept;
60 Q_DECL_CONSTEXPR int top() const noexcept;
61 Q_DECL_CONSTEXPR int right() const noexcept;
62 Q_DECL_CONSTEXPR int bottom() const noexcept;
63
64 Q_DECL_RELAXED_CONSTEXPR void setLeft(int left) noexcept;
65 Q_DECL_RELAXED_CONSTEXPR void setTop(int top) noexcept;
66 Q_DECL_RELAXED_CONSTEXPR void setRight(int right) noexcept;
67 Q_DECL_RELAXED_CONSTEXPR void setBottom(int bottom) noexcept;
68
69 Q_DECL_RELAXED_CONSTEXPR QMargins &operator+=(const QMargins &margins) noexcept;
70 Q_DECL_RELAXED_CONSTEXPR QMargins &operator-=(const QMargins &margins) noexcept;
71 Q_DECL_RELAXED_CONSTEXPR QMargins &operator+=(int) noexcept;
72 Q_DECL_RELAXED_CONSTEXPR QMargins &operator-=(int) noexcept;
73 Q_DECL_RELAXED_CONSTEXPR QMargins &operator*=(int) noexcept;
74 Q_DECL_RELAXED_CONSTEXPR QMargins &operator/=(int);
75 Q_DECL_RELAXED_CONSTEXPR QMargins &operator*=(qreal) noexcept;
76 Q_DECL_RELAXED_CONSTEXPR QMargins &operator/=(qreal);
77
78private:
79 int m_left;
80 int m_top;
81 int m_right;
82 int m_bottom;
83
84 friend Q_DECL_CONSTEXPR inline bool operator==(const QMargins &, const QMargins &) noexcept;
85 friend Q_DECL_CONSTEXPR inline bool operator!=(const QMargins &, const QMargins &) noexcept;
86};
87
88Q_DECLARE_TYPEINFO(QMargins, Q_MOVABLE_TYPE);
89
90/*****************************************************************************
91 QMargins stream functions
92 *****************************************************************************/
93#ifndef QT_NO_DATASTREAM
94Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QMargins &);
95Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QMargins &);
96#endif
97
98/*****************************************************************************
99 QMargins inline functions
100 *****************************************************************************/
101
102Q_DECL_CONSTEXPR inline QMargins::QMargins() noexcept : m_left(0), m_top(0), m_right(0), m_bottom(0) {}
103
104Q_DECL_CONSTEXPR inline QMargins::QMargins(int aleft, int atop, int aright, int abottom) noexcept
105 : m_left(aleft), m_top(atop), m_right(aright), m_bottom(abottom) {}
106
107Q_DECL_CONSTEXPR inline bool QMargins::isNull() const noexcept
108{ return m_left==0 && m_top==0 && m_right==0 && m_bottom==0; }
109
110Q_DECL_CONSTEXPR inline int QMargins::left() const noexcept
111{ return m_left; }
112
113Q_DECL_CONSTEXPR inline int QMargins::top() const noexcept
114{ return m_top; }
115
116Q_DECL_CONSTEXPR inline int QMargins::right() const noexcept
117{ return m_right; }
118
119Q_DECL_CONSTEXPR inline int QMargins::bottom() const noexcept
120{ return m_bottom; }
121
122
123Q_DECL_RELAXED_CONSTEXPR inline void QMargins::setLeft(int aleft) noexcept
124{ m_left = aleft; }
125
126Q_DECL_RELAXED_CONSTEXPR inline void QMargins::setTop(int atop) noexcept
127{ m_top = atop; }
128
129Q_DECL_RELAXED_CONSTEXPR inline void QMargins::setRight(int aright) noexcept
130{ m_right = aright; }
131
132Q_DECL_RELAXED_CONSTEXPR inline void QMargins::setBottom(int abottom) noexcept
133{ m_bottom = abottom; }
134
135Q_DECL_CONSTEXPR inline bool operator==(const QMargins &m1, const QMargins &m2) noexcept
136{
137 return
138 m1.m_left == m2.m_left &&
139 m1.m_top == m2.m_top &&
140 m1.m_right == m2.m_right &&
141 m1.m_bottom == m2.m_bottom;
142}
143
144Q_DECL_CONSTEXPR inline bool operator!=(const QMargins &m1, const QMargins &m2) noexcept
145{
146 return
147 m1.m_left != m2.m_left ||
148 m1.m_top != m2.m_top ||
149 m1.m_right != m2.m_right ||
150 m1.m_bottom != m2.m_bottom;
151}
152
153Q_DECL_CONSTEXPR inline QMargins operator+(const QMargins &m1, const QMargins &m2) noexcept
154{
155 return QMargins(m1.left() + m2.left(), m1.top() + m2.top(),
156 m1.right() + m2.right(), m1.bottom() + m2.bottom());
157}
158
159Q_DECL_CONSTEXPR inline QMargins operator-(const QMargins &m1, const QMargins &m2) noexcept
160{
161 return QMargins(m1.left() - m2.left(), m1.top() - m2.top(),
162 m1.right() - m2.right(), m1.bottom() - m2.bottom());
163}
164
165Q_DECL_CONSTEXPR inline QMargins operator+(const QMargins &lhs, int rhs) noexcept
166{
167 return QMargins(lhs.left() + rhs, lhs.top() + rhs,
168 lhs.right() + rhs, lhs.bottom() + rhs);
169}
170
171Q_DECL_CONSTEXPR inline QMargins operator+(int lhs, const QMargins &rhs) noexcept
172{
173 return QMargins(rhs.left() + lhs, rhs.top() + lhs,
174 rhs.right() + lhs, rhs.bottom() + lhs);
175}
176
177Q_DECL_CONSTEXPR inline QMargins operator-(const QMargins &lhs, int rhs) noexcept
178{
179 return QMargins(lhs.left() - rhs, lhs.top() - rhs,
180 lhs.right() - rhs, lhs.bottom() - rhs);
181}
182
183Q_DECL_CONSTEXPR inline QMargins operator*(const QMargins &margins, int factor) noexcept
184{
185 return QMargins(margins.left() * factor, margins.top() * factor,
186 margins.right() * factor, margins.bottom() * factor);
187}
188
189Q_DECL_CONSTEXPR inline QMargins operator*(int factor, const QMargins &margins) noexcept
190{
191 return QMargins(margins.left() * factor, margins.top() * factor,
192 margins.right() * factor, margins.bottom() * factor);
193}
194
195Q_DECL_CONSTEXPR inline QMargins operator*(const QMargins &margins, qreal factor) noexcept
196{
197 return QMargins(qRound(d: margins.left() * factor), qRound(d: margins.top() * factor),
198 qRound(d: margins.right() * factor), qRound(d: margins.bottom() * factor));
199}
200
201Q_DECL_CONSTEXPR inline QMargins operator*(qreal factor, const QMargins &margins) noexcept
202{
203 return QMargins(qRound(d: margins.left() * factor), qRound(d: margins.top() * factor),
204 qRound(d: margins.right() * factor), qRound(d: margins.bottom() * factor));
205}
206
207Q_DECL_CONSTEXPR inline QMargins operator/(const QMargins &margins, int divisor)
208{
209 return QMargins(margins.left() / divisor, margins.top() / divisor,
210 margins.right() / divisor, margins.bottom() / divisor);
211}
212
213Q_DECL_CONSTEXPR inline QMargins operator/(const QMargins &margins, qreal divisor)
214{
215 return QMargins(qRound(d: margins.left() / divisor), qRound(d: margins.top() / divisor),
216 qRound(d: margins.right() / divisor), qRound(d: margins.bottom() / divisor));
217}
218
219Q_DECL_RELAXED_CONSTEXPR inline QMargins &QMargins::operator+=(const QMargins &margins) noexcept
220{
221 return *this = *this + margins;
222}
223
224Q_DECL_RELAXED_CONSTEXPR inline QMargins &QMargins::operator-=(const QMargins &margins) noexcept
225{
226 return *this = *this - margins;
227}
228
229Q_DECL_RELAXED_CONSTEXPR inline QMargins &QMargins::operator+=(int margin) noexcept
230{
231 m_left += margin;
232 m_top += margin;
233 m_right += margin;
234 m_bottom += margin;
235 return *this;
236}
237
238Q_DECL_RELAXED_CONSTEXPR inline QMargins &QMargins::operator-=(int margin) noexcept
239{
240 m_left -= margin;
241 m_top -= margin;
242 m_right -= margin;
243 m_bottom -= margin;
244 return *this;
245}
246
247Q_DECL_RELAXED_CONSTEXPR inline QMargins &QMargins::operator*=(int factor) noexcept
248{
249 return *this = *this * factor;
250}
251
252Q_DECL_RELAXED_CONSTEXPR inline QMargins &QMargins::operator/=(int divisor)
253{
254 return *this = *this / divisor;
255}
256
257Q_DECL_RELAXED_CONSTEXPR inline QMargins &QMargins::operator*=(qreal factor) noexcept
258{
259 return *this = *this * factor;
260}
261
262Q_DECL_RELAXED_CONSTEXPR inline QMargins &QMargins::operator/=(qreal divisor)
263{
264 return *this = *this / divisor;
265}
266
267Q_DECL_CONSTEXPR inline QMargins operator+(const QMargins &margins) noexcept
268{
269 return margins;
270}
271
272Q_DECL_CONSTEXPR inline QMargins operator-(const QMargins &margins) noexcept
273{
274 return QMargins(-margins.left(), -margins.top(), -margins.right(), -margins.bottom());
275}
276
277#ifndef QT_NO_DEBUG_STREAM
278Q_CORE_EXPORT QDebug operator<<(QDebug, const QMargins &);
279#endif
280
281/*****************************************************************************
282 QMarginsF class
283 *****************************************************************************/
284
285class QMarginsF
286{
287public:
288 Q_DECL_CONSTEXPR QMarginsF() noexcept;
289 Q_DECL_CONSTEXPR QMarginsF(qreal left, qreal top, qreal right, qreal bottom) noexcept;
290 Q_DECL_CONSTEXPR QMarginsF(const QMargins &margins) noexcept;
291
292 Q_DECL_CONSTEXPR bool isNull() const noexcept;
293
294 Q_DECL_CONSTEXPR qreal left() const noexcept;
295 Q_DECL_CONSTEXPR qreal top() const noexcept;
296 Q_DECL_CONSTEXPR qreal right() const noexcept;
297 Q_DECL_CONSTEXPR qreal bottom() const noexcept;
298
299 Q_DECL_RELAXED_CONSTEXPR void setLeft(qreal left) noexcept;
300 Q_DECL_RELAXED_CONSTEXPR void setTop(qreal top) noexcept;
301 Q_DECL_RELAXED_CONSTEXPR void setRight(qreal right) noexcept;
302 Q_DECL_RELAXED_CONSTEXPR void setBottom(qreal bottom) noexcept;
303
304 Q_DECL_RELAXED_CONSTEXPR QMarginsF &operator+=(const QMarginsF &margins) noexcept;
305 Q_DECL_RELAXED_CONSTEXPR QMarginsF &operator-=(const QMarginsF &margins) noexcept;
306 Q_DECL_RELAXED_CONSTEXPR QMarginsF &operator+=(qreal addend) noexcept;
307 Q_DECL_RELAXED_CONSTEXPR QMarginsF &operator-=(qreal subtrahend) noexcept;
308 Q_DECL_RELAXED_CONSTEXPR QMarginsF &operator*=(qreal factor) noexcept;
309 Q_DECL_RELAXED_CONSTEXPR QMarginsF &operator/=(qreal divisor);
310
311 Q_DECL_CONSTEXPR inline QMargins toMargins() const noexcept;
312
313private:
314 qreal m_left;
315 qreal m_top;
316 qreal m_right;
317 qreal m_bottom;
318};
319
320Q_DECLARE_TYPEINFO(QMarginsF, Q_MOVABLE_TYPE);
321
322/*****************************************************************************
323 QMarginsF stream functions
324 *****************************************************************************/
325
326#ifndef QT_NO_DATASTREAM
327Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QMarginsF &);
328Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QMarginsF &);
329#endif
330
331/*****************************************************************************
332 QMarginsF inline functions
333 *****************************************************************************/
334
335Q_DECL_CONSTEXPR inline QMarginsF::QMarginsF() noexcept
336 : m_left(0), m_top(0), m_right(0), m_bottom(0) {}
337
338Q_DECL_CONSTEXPR inline QMarginsF::QMarginsF(qreal aleft, qreal atop, qreal aright, qreal abottom) noexcept
339 : m_left(aleft), m_top(atop), m_right(aright), m_bottom(abottom) {}
340
341Q_DECL_CONSTEXPR inline QMarginsF::QMarginsF(const QMargins &margins) noexcept
342 : m_left(margins.left()), m_top(margins.top()), m_right(margins.right()), m_bottom(margins.bottom()) {}
343
344Q_DECL_CONSTEXPR inline bool QMarginsF::isNull() const noexcept
345{ return qFuzzyIsNull(d: m_left) && qFuzzyIsNull(d: m_top) && qFuzzyIsNull(d: m_right) && qFuzzyIsNull(d: m_bottom); }
346
347Q_DECL_CONSTEXPR inline qreal QMarginsF::left() const noexcept
348{ return m_left; }
349
350Q_DECL_CONSTEXPR inline qreal QMarginsF::top() const noexcept
351{ return m_top; }
352
353Q_DECL_CONSTEXPR inline qreal QMarginsF::right() const noexcept
354{ return m_right; }
355
356Q_DECL_CONSTEXPR inline qreal QMarginsF::bottom() const noexcept
357{ return m_bottom; }
358
359
360Q_DECL_RELAXED_CONSTEXPR inline void QMarginsF::setLeft(qreal aleft) noexcept
361{ m_left = aleft; }
362
363Q_DECL_RELAXED_CONSTEXPR inline void QMarginsF::setTop(qreal atop) noexcept
364{ m_top = atop; }
365
366Q_DECL_RELAXED_CONSTEXPR inline void QMarginsF::setRight(qreal aright) noexcept
367{ m_right = aright; }
368
369Q_DECL_RELAXED_CONSTEXPR inline void QMarginsF::setBottom(qreal abottom) noexcept
370{ m_bottom = abottom; }
371
372Q_DECL_CONSTEXPR inline bool operator==(const QMarginsF &lhs, const QMarginsF &rhs) noexcept
373{
374 return qFuzzyCompare(p1: lhs.left(), p2: rhs.left())
375 && qFuzzyCompare(p1: lhs.top(), p2: rhs.top())
376 && qFuzzyCompare(p1: lhs.right(), p2: rhs.right())
377 && qFuzzyCompare(p1: lhs.bottom(), p2: rhs.bottom());
378}
379
380Q_DECL_CONSTEXPR inline bool operator!=(const QMarginsF &lhs, const QMarginsF &rhs) noexcept
381{
382 return !operator==(lhs, rhs);
383}
384
385Q_DECL_CONSTEXPR inline QMarginsF operator+(const QMarginsF &lhs, const QMarginsF &rhs) noexcept
386{
387 return QMarginsF(lhs.left() + rhs.left(), lhs.top() + rhs.top(),
388 lhs.right() + rhs.right(), lhs.bottom() + rhs.bottom());
389}
390
391Q_DECL_CONSTEXPR inline QMarginsF operator-(const QMarginsF &lhs, const QMarginsF &rhs) noexcept
392{
393 return QMarginsF(lhs.left() - rhs.left(), lhs.top() - rhs.top(),
394 lhs.right() - rhs.right(), lhs.bottom() - rhs.bottom());
395}
396
397Q_DECL_CONSTEXPR inline QMarginsF operator+(const QMarginsF &lhs, qreal rhs) noexcept
398{
399 return QMarginsF(lhs.left() + rhs, lhs.top() + rhs,
400 lhs.right() + rhs, lhs.bottom() + rhs);
401}
402
403Q_DECL_CONSTEXPR inline QMarginsF operator+(qreal lhs, const QMarginsF &rhs) noexcept
404{
405 return QMarginsF(rhs.left() + lhs, rhs.top() + lhs,
406 rhs.right() + lhs, rhs.bottom() + lhs);
407}
408
409Q_DECL_CONSTEXPR inline QMarginsF operator-(const QMarginsF &lhs, qreal rhs) noexcept
410{
411 return QMarginsF(lhs.left() - rhs, lhs.top() - rhs,
412 lhs.right() - rhs, lhs.bottom() - rhs);
413}
414
415Q_DECL_CONSTEXPR inline QMarginsF operator*(const QMarginsF &lhs, qreal rhs) noexcept
416{
417 return QMarginsF(lhs.left() * rhs, lhs.top() * rhs,
418 lhs.right() * rhs, lhs.bottom() * rhs);
419}
420
421Q_DECL_CONSTEXPR inline QMarginsF operator*(qreal lhs, const QMarginsF &rhs) noexcept
422{
423 return QMarginsF(rhs.left() * lhs, rhs.top() * lhs,
424 rhs.right() * lhs, rhs.bottom() * lhs);
425}
426
427Q_DECL_CONSTEXPR inline QMarginsF operator/(const QMarginsF &lhs, qreal divisor)
428{
429 return QMarginsF(lhs.left() / divisor, lhs.top() / divisor,
430 lhs.right() / divisor, lhs.bottom() / divisor);
431}
432
433Q_DECL_RELAXED_CONSTEXPR inline QMarginsF &QMarginsF::operator+=(const QMarginsF &margins) noexcept
434{
435 return *this = *this + margins;
436}
437
438Q_DECL_RELAXED_CONSTEXPR inline QMarginsF &QMarginsF::operator-=(const QMarginsF &margins) noexcept
439{
440 return *this = *this - margins;
441}
442
443Q_DECL_RELAXED_CONSTEXPR inline QMarginsF &QMarginsF::operator+=(qreal addend) noexcept
444{
445 m_left += addend;
446 m_top += addend;
447 m_right += addend;
448 m_bottom += addend;
449 return *this;
450}
451
452Q_DECL_RELAXED_CONSTEXPR inline QMarginsF &QMarginsF::operator-=(qreal subtrahend) noexcept
453{
454 m_left -= subtrahend;
455 m_top -= subtrahend;
456 m_right -= subtrahend;
457 m_bottom -= subtrahend;
458 return *this;
459}
460
461Q_DECL_RELAXED_CONSTEXPR inline QMarginsF &QMarginsF::operator*=(qreal factor) noexcept
462{
463 return *this = *this * factor;
464}
465
466Q_DECL_RELAXED_CONSTEXPR inline QMarginsF &QMarginsF::operator/=(qreal divisor)
467{
468 return *this = *this / divisor;
469}
470
471Q_DECL_CONSTEXPR inline QMarginsF operator+(const QMarginsF &margins) noexcept
472{
473 return margins;
474}
475
476Q_DECL_CONSTEXPR inline QMarginsF operator-(const QMarginsF &margins) noexcept
477{
478 return QMarginsF(-margins.left(), -margins.top(), -margins.right(), -margins.bottom());
479}
480
481Q_DECL_CONSTEXPR inline QMargins QMarginsF::toMargins() const noexcept
482{
483 return QMargins(qRound(d: m_left), qRound(d: m_top), qRound(d: m_right), qRound(d: m_bottom));
484}
485
486#ifndef QT_NO_DEBUG_STREAM
487Q_CORE_EXPORT QDebug operator<<(QDebug, const QMarginsF &);
488#endif
489
490QT_END_NAMESPACE
491
492#endif // QMARGINS_H
493

source code of qtbase/src/corelib/tools/qmargins.h