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 constexpr QMargins() noexcept;
55 constexpr QMargins(int left, int top, int right, int bottom) noexcept;
56
57 constexpr bool isNull() const noexcept;
58
59 constexpr int left() const noexcept;
60 constexpr int top() const noexcept;
61 constexpr int right() const noexcept;
62 constexpr int bottom() const noexcept;
63
64 constexpr void setLeft(int left) noexcept;
65 constexpr void setTop(int top) noexcept;
66 constexpr void setRight(int right) noexcept;
67 constexpr void setBottom(int bottom) noexcept;
68
69 constexpr QMargins &operator+=(const QMargins &margins) noexcept;
70 constexpr QMargins &operator-=(const QMargins &margins) noexcept;
71 constexpr QMargins &operator+=(int) noexcept;
72 constexpr QMargins &operator-=(int) noexcept;
73 constexpr QMargins &operator*=(int) noexcept;
74 constexpr QMargins &operator/=(int);
75 constexpr QMargins &operator*=(qreal) noexcept;
76 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 constexpr inline bool operator==(const QMargins &, const QMargins &) noexcept;
85 friend 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
102constexpr inline QMargins::QMargins() noexcept : m_left(0), m_top(0), m_right(0), m_bottom(0) {}
103
104constexpr 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
107constexpr inline bool QMargins::isNull() const noexcept
108{ return m_left==0 && m_top==0 && m_right==0 && m_bottom==0; }
109
110constexpr inline int QMargins::left() const noexcept
111{ return m_left; }
112
113constexpr inline int QMargins::top() const noexcept
114{ return m_top; }
115
116constexpr inline int QMargins::right() const noexcept
117{ return m_right; }
118
119constexpr inline int QMargins::bottom() const noexcept
120{ return m_bottom; }
121
122
123constexpr inline void QMargins::setLeft(int aleft) noexcept
124{ m_left = aleft; }
125
126constexpr inline void QMargins::setTop(int atop) noexcept
127{ m_top = atop; }
128
129constexpr inline void QMargins::setRight(int aright) noexcept
130{ m_right = aright; }
131
132constexpr inline void QMargins::setBottom(int abottom) noexcept
133{ m_bottom = abottom; }
134
135constexpr 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
144constexpr 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
153constexpr 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
159constexpr 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
165constexpr 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
171constexpr 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
177constexpr 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
183constexpr 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
189constexpr 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
195constexpr inline QMargins operator*(const QMargins &margins, qreal factor) noexcept
196{
197 return QMargins(qRound(margins.left() * factor), qRound(margins.top() * factor),
198 qRound(margins.right() * factor), qRound(margins.bottom() * factor));
199}
200
201constexpr inline QMargins operator*(qreal factor, const QMargins &margins) noexcept
202{
203 return QMargins(qRound(margins.left() * factor), qRound(margins.top() * factor),
204 qRound(margins.right() * factor), qRound(margins.bottom() * factor));
205}
206
207constexpr 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
213constexpr inline QMargins operator/(const QMargins &margins, qreal divisor)
214{
215 return QMargins(qRound(margins.left() / divisor), qRound(margins.top() / divisor),
216 qRound(margins.right() / divisor), qRound(margins.bottom() / divisor));
217}
218
219constexpr inline QMargins operator|(const QMargins &m1, const QMargins &m2) noexcept
220{
221 return QMargins(qMax(m1.left(), m2.left()), qMax(m1.top(), m2.top()),
222 qMax(m1.right(), m2.right()), qMax(m1.bottom(), m2.bottom()));
223}
224
225constexpr inline QMargins &QMargins::operator+=(const QMargins &margins) noexcept
226{
227 return *this = *this + margins;
228}
229
230constexpr inline QMargins &QMargins::operator-=(const QMargins &margins) noexcept
231{
232 return *this = *this - margins;
233}
234
235constexpr inline QMargins &QMargins::operator+=(int margin) noexcept
236{
237 m_left += margin;
238 m_top += margin;
239 m_right += margin;
240 m_bottom += margin;
241 return *this;
242}
243
244constexpr inline QMargins &QMargins::operator-=(int margin) noexcept
245{
246 m_left -= margin;
247 m_top -= margin;
248 m_right -= margin;
249 m_bottom -= margin;
250 return *this;
251}
252
253constexpr inline QMargins &QMargins::operator*=(int factor) noexcept
254{
255 return *this = *this * factor;
256}
257
258constexpr inline QMargins &QMargins::operator/=(int divisor)
259{
260 return *this = *this / divisor;
261}
262
263constexpr inline QMargins &QMargins::operator*=(qreal factor) noexcept
264{
265 return *this = *this * factor;
266}
267
268constexpr inline QMargins &QMargins::operator/=(qreal divisor)
269{
270 return *this = *this / divisor;
271}
272
273constexpr inline QMargins operator+(const QMargins &margins) noexcept
274{
275 return margins;
276}
277
278constexpr inline QMargins operator-(const QMargins &margins) noexcept
279{
280 return QMargins(-margins.left(), -margins.top(), -margins.right(), -margins.bottom());
281}
282
283#ifndef QT_NO_DEBUG_STREAM
284Q_CORE_EXPORT QDebug operator<<(QDebug, const QMargins &);
285#endif
286
287/*****************************************************************************
288 QMarginsF class
289 *****************************************************************************/
290
291class QMarginsF
292{
293public:
294 constexpr QMarginsF() noexcept;
295 constexpr QMarginsF(qreal left, qreal top, qreal right, qreal bottom) noexcept;
296 constexpr QMarginsF(const QMargins &margins) noexcept;
297
298 constexpr bool isNull() const noexcept;
299
300 constexpr qreal left() const noexcept;
301 constexpr qreal top() const noexcept;
302 constexpr qreal right() const noexcept;
303 constexpr qreal bottom() const noexcept;
304
305 constexpr void setLeft(qreal left) noexcept;
306 constexpr void setTop(qreal top) noexcept;
307 constexpr void setRight(qreal right) noexcept;
308 constexpr void setBottom(qreal bottom) noexcept;
309
310 constexpr QMarginsF &operator+=(const QMarginsF &margins) noexcept;
311 constexpr QMarginsF &operator-=(const QMarginsF &margins) noexcept;
312 constexpr QMarginsF &operator+=(qreal addend) noexcept;
313 constexpr QMarginsF &operator-=(qreal subtrahend) noexcept;
314 constexpr QMarginsF &operator*=(qreal factor) noexcept;
315 constexpr QMarginsF &operator/=(qreal divisor);
316
317 constexpr inline QMargins toMargins() const noexcept;
318
319private:
320 qreal m_left;
321 qreal m_top;
322 qreal m_right;
323 qreal m_bottom;
324};
325
326Q_DECLARE_TYPEINFO(QMarginsF, Q_MOVABLE_TYPE);
327
328/*****************************************************************************
329 QMarginsF stream functions
330 *****************************************************************************/
331
332#ifndef QT_NO_DATASTREAM
333Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QMarginsF &);
334Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QMarginsF &);
335#endif
336
337/*****************************************************************************
338 QMarginsF inline functions
339 *****************************************************************************/
340
341constexpr inline QMarginsF::QMarginsF() noexcept
342 : m_left(0), m_top(0), m_right(0), m_bottom(0) {}
343
344constexpr inline QMarginsF::QMarginsF(qreal aleft, qreal atop, qreal aright, qreal abottom) noexcept
345 : m_left(aleft), m_top(atop), m_right(aright), m_bottom(abottom) {}
346
347constexpr inline QMarginsF::QMarginsF(const QMargins &margins) noexcept
348 : m_left(margins.left()), m_top(margins.top()), m_right(margins.right()), m_bottom(margins.bottom()) {}
349
350constexpr inline bool QMarginsF::isNull() const noexcept
351{ return qFuzzyIsNull(m_left) && qFuzzyIsNull(m_top) && qFuzzyIsNull(m_right) && qFuzzyIsNull(m_bottom); }
352
353constexpr inline qreal QMarginsF::left() const noexcept
354{ return m_left; }
355
356constexpr inline qreal QMarginsF::top() const noexcept
357{ return m_top; }
358
359constexpr inline qreal QMarginsF::right() const noexcept
360{ return m_right; }
361
362constexpr inline qreal QMarginsF::bottom() const noexcept
363{ return m_bottom; }
364
365
366constexpr inline void QMarginsF::setLeft(qreal aleft) noexcept
367{ m_left = aleft; }
368
369constexpr inline void QMarginsF::setTop(qreal atop) noexcept
370{ m_top = atop; }
371
372constexpr inline void QMarginsF::setRight(qreal aright) noexcept
373{ m_right = aright; }
374
375constexpr inline void QMarginsF::setBottom(qreal abottom) noexcept
376{ m_bottom = abottom; }
377
378constexpr inline bool operator==(const QMarginsF &lhs, const QMarginsF &rhs) noexcept
379{
380 return qFuzzyCompare(lhs.left(), rhs.left())
381 && qFuzzyCompare(lhs.top(), rhs.top())
382 && qFuzzyCompare(lhs.right(), rhs.right())
383 && qFuzzyCompare(lhs.bottom(), rhs.bottom());
384}
385
386constexpr inline bool operator!=(const QMarginsF &lhs, const QMarginsF &rhs) noexcept
387{
388 return !operator==(lhs, rhs);
389}
390
391constexpr 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
397constexpr inline QMarginsF operator-(const QMarginsF &lhs, const QMarginsF &rhs) noexcept
398{
399 return QMarginsF(lhs.left() - rhs.left(), lhs.top() - rhs.top(),
400 lhs.right() - rhs.right(), lhs.bottom() - rhs.bottom());
401}
402
403constexpr inline QMarginsF operator+(const QMarginsF &lhs, qreal rhs) noexcept
404{
405 return QMarginsF(lhs.left() + rhs, lhs.top() + rhs,
406 lhs.right() + rhs, lhs.bottom() + rhs);
407}
408
409constexpr inline QMarginsF operator+(qreal lhs, const QMarginsF &rhs) noexcept
410{
411 return QMarginsF(rhs.left() + lhs, rhs.top() + lhs,
412 rhs.right() + lhs, rhs.bottom() + lhs);
413}
414
415constexpr 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
421constexpr inline QMarginsF operator*(const QMarginsF &lhs, qreal rhs) noexcept
422{
423 return QMarginsF(lhs.left() * rhs, lhs.top() * rhs,
424 lhs.right() * rhs, lhs.bottom() * rhs);
425}
426
427constexpr inline QMarginsF operator*(qreal lhs, const QMarginsF &rhs) noexcept
428{
429 return QMarginsF(rhs.left() * lhs, rhs.top() * lhs,
430 rhs.right() * lhs, rhs.bottom() * lhs);
431}
432
433constexpr inline QMarginsF operator/(const QMarginsF &lhs, qreal divisor)
434{
435 return QMarginsF(lhs.left() / divisor, lhs.top() / divisor,
436 lhs.right() / divisor, lhs.bottom() / divisor);
437}
438
439constexpr inline QMarginsF operator|(const QMarginsF &m1, const QMarginsF &m2) noexcept
440{
441 return QMarginsF(qMax(m1.left(), m2.left()), qMax(m1.top(), m2.top()),
442 qMax(m1.right(), m2.right()), qMax(m1.bottom(), m2.bottom()));
443}
444
445constexpr inline QMarginsF &QMarginsF::operator+=(const QMarginsF &margins) noexcept
446{
447 return *this = *this + margins;
448}
449
450constexpr inline QMarginsF &QMarginsF::operator-=(const QMarginsF &margins) noexcept
451{
452 return *this = *this - margins;
453}
454
455constexpr inline QMarginsF &QMarginsF::operator+=(qreal addend) noexcept
456{
457 m_left += addend;
458 m_top += addend;
459 m_right += addend;
460 m_bottom += addend;
461 return *this;
462}
463
464constexpr inline QMarginsF &QMarginsF::operator-=(qreal subtrahend) noexcept
465{
466 m_left -= subtrahend;
467 m_top -= subtrahend;
468 m_right -= subtrahend;
469 m_bottom -= subtrahend;
470 return *this;
471}
472
473constexpr inline QMarginsF &QMarginsF::operator*=(qreal factor) noexcept
474{
475 return *this = *this * factor;
476}
477
478constexpr inline QMarginsF &QMarginsF::operator/=(qreal divisor)
479{
480 return *this = *this / divisor;
481}
482
483constexpr inline QMarginsF operator+(const QMarginsF &margins) noexcept
484{
485 return margins;
486}
487
488constexpr inline QMarginsF operator-(const QMarginsF &margins) noexcept
489{
490 return QMarginsF(-margins.left(), -margins.top(), -margins.right(), -margins.bottom());
491}
492
493constexpr inline QMargins QMarginsF::toMargins() const noexcept
494{
495 return QMargins(qRound(m_left), qRound(m_top), qRound(m_right), qRound(m_bottom));
496}
497
498#ifndef QT_NO_DEBUG_STREAM
499Q_CORE_EXPORT QDebug operator<<(QDebug, const QMarginsF &);
500#endif
501
502QT_END_NAMESPACE
503
504#endif // QMARGINS_H
505