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 
45 QT_BEGIN_NAMESPACE
46 
47 /*****************************************************************************
48   QMargins class
49  *****************************************************************************/
50 
51 class QMargins
52 {
53 public:
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 
78 private:
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 
88 Q_DECLARE_TYPEINFO(QMargins, Q_MOVABLE_TYPE);
89 
90 /*****************************************************************************
91   QMargins stream functions
92  *****************************************************************************/
93 #ifndef QT_NO_DATASTREAM
94 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QMargins &);
95 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QMargins &);
96 #endif
97 
98 /*****************************************************************************
99   QMargins inline functions
100  *****************************************************************************/
101 
QMargins()102 Q_DECL_CONSTEXPR inline QMargins::QMargins() noexcept : m_left(0), m_top(0), m_right(0), m_bottom(0) {}
103 
QMargins(int aleft,int atop,int aright,int abottom)104 Q_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 
isNull()107 Q_DECL_CONSTEXPR inline bool QMargins::isNull() const noexcept
108 { return m_left==0 && m_top==0 && m_right==0 && m_bottom==0; }
109 
left()110 Q_DECL_CONSTEXPR inline int QMargins::left() const noexcept
111 { return m_left; }
112 
top()113 Q_DECL_CONSTEXPR inline int QMargins::top() const noexcept
114 { return m_top; }
115 
right()116 Q_DECL_CONSTEXPR inline int QMargins::right() const noexcept
117 { return m_right; }
118 
bottom()119 Q_DECL_CONSTEXPR inline int QMargins::bottom() const noexcept
120 { return m_bottom; }
121 
122 
setLeft(int aleft)123 Q_DECL_RELAXED_CONSTEXPR inline void QMargins::setLeft(int aleft) noexcept
124 { m_left = aleft; }
125 
setTop(int atop)126 Q_DECL_RELAXED_CONSTEXPR inline void QMargins::setTop(int atop) noexcept
127 { m_top = atop; }
128 
setRight(int aright)129 Q_DECL_RELAXED_CONSTEXPR inline void QMargins::setRight(int aright) noexcept
130 { m_right = aright; }
131 
setBottom(int abottom)132 Q_DECL_RELAXED_CONSTEXPR inline void QMargins::setBottom(int abottom) noexcept
133 { m_bottom = abottom; }
134 
135 Q_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 
144 Q_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 
153 Q_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 
159 Q_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 
165 Q_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 
171 Q_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 
177 Q_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 
183 Q_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 
189 Q_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 
195 Q_DECL_CONSTEXPR 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 
201 Q_DECL_CONSTEXPR 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 
207 Q_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 
213 Q_DECL_CONSTEXPR 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 
219 Q_DECL_RELAXED_CONSTEXPR inline QMargins &QMargins::operator+=(const QMargins &margins) noexcept
220 {
221     return *this = *this + margins;
222 }
223 
224 Q_DECL_RELAXED_CONSTEXPR inline QMargins &QMargins::operator-=(const QMargins &margins) noexcept
225 {
226     return *this = *this - margins;
227 }
228 
229 Q_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 
238 Q_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 
247 Q_DECL_RELAXED_CONSTEXPR inline QMargins &QMargins::operator*=(int factor) noexcept
248 {
249     return *this = *this * factor;
250 }
251 
252 Q_DECL_RELAXED_CONSTEXPR inline QMargins &QMargins::operator/=(int divisor)
253 {
254     return *this = *this / divisor;
255 }
256 
257 Q_DECL_RELAXED_CONSTEXPR inline QMargins &QMargins::operator*=(qreal factor) noexcept
258 {
259     return *this = *this * factor;
260 }
261 
262 Q_DECL_RELAXED_CONSTEXPR inline QMargins &QMargins::operator/=(qreal divisor)
263 {
264     return *this = *this / divisor;
265 }
266 
267 Q_DECL_CONSTEXPR inline QMargins operator+(const QMargins &margins) noexcept
268 {
269     return margins;
270 }
271 
272 Q_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
278 Q_CORE_EXPORT QDebug operator<<(QDebug, const QMargins &);
279 #endif
280 
281 /*****************************************************************************
282   QMarginsF class
283  *****************************************************************************/
284 
285 class QMarginsF
286 {
287 public:
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 
313 private:
314     qreal m_left;
315     qreal m_top;
316     qreal m_right;
317     qreal m_bottom;
318 };
319 
320 Q_DECLARE_TYPEINFO(QMarginsF, Q_MOVABLE_TYPE);
321 
322 /*****************************************************************************
323   QMarginsF stream functions
324  *****************************************************************************/
325 
326 #ifndef QT_NO_DATASTREAM
327 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QMarginsF &);
328 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QMarginsF &);
329 #endif
330 
331 /*****************************************************************************
332   QMarginsF inline functions
333  *****************************************************************************/
334 
QMarginsF()335 Q_DECL_CONSTEXPR inline QMarginsF::QMarginsF() noexcept
336     : m_left(0), m_top(0), m_right(0), m_bottom(0) {}
337 
QMarginsF(qreal aleft,qreal atop,qreal aright,qreal abottom)338 Q_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 
QMarginsF(const QMargins & margins)341 Q_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 
isNull()344 Q_DECL_CONSTEXPR inline bool QMarginsF::isNull() const noexcept
345 { return qFuzzyIsNull(m_left) && qFuzzyIsNull(m_top) && qFuzzyIsNull(m_right) && qFuzzyIsNull(m_bottom); }
346 
left()347 Q_DECL_CONSTEXPR inline qreal QMarginsF::left() const noexcept
348 { return m_left; }
349 
top()350 Q_DECL_CONSTEXPR inline qreal QMarginsF::top() const noexcept
351 { return m_top; }
352 
right()353 Q_DECL_CONSTEXPR inline qreal QMarginsF::right() const noexcept
354 { return m_right; }
355 
bottom()356 Q_DECL_CONSTEXPR inline qreal QMarginsF::bottom() const noexcept
357 { return m_bottom; }
358 
359 
setLeft(qreal aleft)360 Q_DECL_RELAXED_CONSTEXPR inline void QMarginsF::setLeft(qreal aleft) noexcept
361 { m_left = aleft; }
362 
setTop(qreal atop)363 Q_DECL_RELAXED_CONSTEXPR inline void QMarginsF::setTop(qreal atop) noexcept
364 { m_top = atop; }
365 
setRight(qreal aright)366 Q_DECL_RELAXED_CONSTEXPR inline void QMarginsF::setRight(qreal aright) noexcept
367 { m_right = aright; }
368 
setBottom(qreal abottom)369 Q_DECL_RELAXED_CONSTEXPR inline void QMarginsF::setBottom(qreal abottom) noexcept
370 { m_bottom = abottom; }
371 
372 Q_DECL_CONSTEXPR inline bool operator==(const QMarginsF &lhs, const QMarginsF &rhs) noexcept
373 {
374     return qFuzzyCompare(lhs.left(), rhs.left())
375            && qFuzzyCompare(lhs.top(), rhs.top())
376            && qFuzzyCompare(lhs.right(), rhs.right())
377            && qFuzzyCompare(lhs.bottom(), rhs.bottom());
378 }
379 
380 Q_DECL_CONSTEXPR inline bool operator!=(const QMarginsF &lhs, const QMarginsF &rhs) noexcept
381 {
382     return !operator==(lhs, rhs);
383 }
384 
385 Q_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 
391 Q_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 
397 Q_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 
403 Q_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 
409 Q_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 
415 Q_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 
421 Q_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 
427 Q_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 
433 Q_DECL_RELAXED_CONSTEXPR inline QMarginsF &QMarginsF::operator+=(const QMarginsF &margins) noexcept
434 {
435     return *this = *this + margins;
436 }
437 
438 Q_DECL_RELAXED_CONSTEXPR inline QMarginsF &QMarginsF::operator-=(const QMarginsF &margins) noexcept
439 {
440     return *this = *this - margins;
441 }
442 
443 Q_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 
452 Q_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 
461 Q_DECL_RELAXED_CONSTEXPR inline QMarginsF &QMarginsF::operator*=(qreal factor) noexcept
462 {
463     return *this = *this * factor;
464 }
465 
466 Q_DECL_RELAXED_CONSTEXPR inline QMarginsF &QMarginsF::operator/=(qreal divisor)
467 {
468     return *this = *this / divisor;
469 }
470 
471 Q_DECL_CONSTEXPR inline QMarginsF operator+(const QMarginsF &margins) noexcept
472 {
473     return margins;
474 }
475 
476 Q_DECL_CONSTEXPR inline QMarginsF operator-(const QMarginsF &margins) noexcept
477 {
478     return QMarginsF(-margins.left(), -margins.top(), -margins.right(), -margins.bottom());
479 }
480 
toMargins()481 Q_DECL_CONSTEXPR inline QMargins QMarginsF::toMargins() const noexcept
482 {
483     return QMargins(qRound(m_left), qRound(m_top), qRound(m_right), qRound(m_bottom));
484 }
485 
486 #ifndef QT_NO_DEBUG_STREAM
487 Q_CORE_EXPORT QDebug operator<<(QDebug, const QMarginsF &);
488 #endif
489 
490 QT_END_NAMESPACE
491 
492 #endif // QMARGINS_H
493