1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qmargins.h"
5#include "qdatastream.h"
6
7#include <private/qdebug_p.h>
8
9QT_BEGIN_NAMESPACE
10
11/*!
12 \class QMargins
13 \inmodule QtCore
14 \ingroup painting
15 \since 4.6
16
17 \brief The QMargins class defines the four margins of a rectangle.
18
19 QMargin defines a set of four margins; left, top, right, and bottom,
20 that describe the size of the borders surrounding a rectangle.
21
22 The isNull() function returns \c true only if all margins are set to zero.
23
24 QMargin objects can be streamed as well as compared.
25*/
26
27
28/*****************************************************************************
29 QMargins member functions
30 *****************************************************************************/
31
32/*!
33 \fn QMargins::QMargins()
34
35 Constructs a margins object with all margins set to 0.
36
37 \sa isNull()
38*/
39
40/*!
41 \fn QMargins::QMargins(int left, int top, int right, int bottom)
42
43 Constructs margins with the given \a left, \a top, \a right, and \a bottom
44
45 \sa setLeft(), setRight(), setTop(), setBottom()
46*/
47
48/*!
49 \fn bool QMargins::isNull() const
50
51 Returns \c true if all margins are is 0; otherwise returns
52 false.
53*/
54
55
56/*!
57 \fn int QMargins::left() const
58
59 Returns the left margin.
60
61 \sa setLeft()
62*/
63
64/*!
65 \fn int QMargins::top() const
66
67 Returns the top margin.
68
69 \sa setTop()
70*/
71
72/*!
73 \fn int QMargins::right() const
74
75 Returns the right margin.
76*/
77
78/*!
79 \fn int QMargins::bottom() const
80
81 Returns the bottom margin.
82*/
83
84
85/*!
86 \fn void QMargins::setLeft(int left)
87
88 Sets the left margin to \a left.
89*/
90
91/*!
92 \fn void QMargins::setTop(int Top)
93
94 Sets the Top margin to \a Top.
95*/
96
97/*!
98 \fn void QMargins::setRight(int right)
99
100 Sets the right margin to \a right.
101*/
102
103/*!
104 \fn void QMargins::setBottom(int bottom)
105
106 Sets the bottom margin to \a bottom.
107*/
108
109/*!
110 \fn bool QMargins::operator==(const QMargins &m1, const QMargins &m2)
111
112 Returns \c true if \a m1 and \a m2 are equal; otherwise returns \c false.
113*/
114
115/*!
116 \fn bool QMargins::operator!=(const QMargins &m1, const QMargins &m2)
117
118 Returns \c true if \a m1 and \a m2 are different; otherwise returns \c false.
119*/
120
121/*!
122 \fn QMargins operator+(const QMargins &m1, const QMargins &m2)
123 \relates QMargins
124
125 Returns a QMargins object that is the sum of the given margins, \a m1
126 and \a m2; each component is added separately.
127
128 \sa QMargins::operator+=(), QMargins::operator-=()
129
130 \since 5.1
131*/
132
133/*!
134 \fn QMargins operator-(const QMargins &m1, const QMargins &m2)
135 \relates QMargins
136
137 Returns a QMargins object that is formed by subtracting \a m2 from
138 \a m1; each component is subtracted separately.
139
140 \sa QMargins::operator+=(), QMargins::operator-=()
141
142 \since 5.1
143*/
144
145/*!
146 \fn QMargins operator+(const QMargins &lhs, int rhs)
147 \relates QMargins
148
149 Returns a QMargins object that is formed by adding \a rhs to
150 \a lhs.
151
152 \sa QMargins::operator+=(), QMargins::operator-=()
153
154 \since 5.3
155*/
156
157/*!
158 \fn QMargins operator+(int lhs, const QMargins &rhs)
159 \relates QMargins
160
161 Returns a QMargins object that is formed by adding \a lhs to
162 \a rhs.
163
164 \sa QMargins::operator+=(), QMargins::operator-=()
165
166 \since 5.3
167*/
168
169/*!
170 \fn QMargins operator-(const QMargins &lhs, int rhs)
171 \relates QMargins
172
173 Returns a QMargins object that is formed by subtracting \a rhs from
174 \a lhs.
175
176 \sa QMargins::operator+=(), QMargins::operator-=()
177
178 \since 5.3
179*/
180
181/*!
182 \fn QMargins operator*(const QMargins &margins, int factor)
183 \relates QMargins
184
185 Returns a QMargins object that is formed by multiplying each component
186 of the given \a margins by \a factor.
187
188 \sa QMargins::operator*=(), QMargins::operator/=()
189
190 \since 5.1
191*/
192
193/*!
194 \fn QMargins operator*(int factor, const QMargins &margins)
195 \relates QMargins
196 \overload
197
198 Returns a QMargins object that is formed by multiplying each component
199 of the given \a margins by \a factor.
200
201 \sa QMargins::operator*=(), QMargins::operator/=()
202
203 \since 5.1
204*/
205
206/*!
207 \fn QMargins operator*(const QMargins &margins, qreal factor)
208 \relates QMargins
209 \overload
210
211 Returns a QMargins object that is formed by multiplying each component
212 of the given \a margins by \a factor.
213
214 \sa QMargins::operator*=(), QMargins::operator/=()
215
216 \since 5.1
217*/
218
219/*!
220 \fn QMargins operator*(qreal factor, const QMargins &margins)
221 \relates QMargins
222 \overload
223
224 Returns a QMargins object that is formed by multiplying each component
225 of the given \a margins by \a factor.
226
227 \sa QMargins::operator*=(), QMargins::operator/=()
228
229 \since 5.1
230*/
231
232/*!
233 \fn QMargins operator/(const QMargins &margins, int divisor)
234 \relates QMargins
235
236 Returns a QMargins object that is formed by dividing the components of
237 the given \a margins by the given \a divisor.
238
239 \sa QMargins::operator*=(), QMargins::operator/=()
240
241 \since 5.1
242*/
243
244/*!
245 \fn QMargins operator/(const QMargins &, qreal)
246 \relates QMargins
247 \overload
248
249 Returns a QMargins object that is formed by dividing the components of
250 the given \a margins by the given \a divisor.
251
252 \sa QMargins::operator*=(), QMargins::operator/=()
253
254 \since 5.1
255*/
256
257/*!
258 \fn QMargins operator+(const QMargins &margins)
259 \relates QMargins
260
261 Returns a QMargin object that is formed from all components of \a margins.
262
263 \since 5.3
264*/
265
266/*!
267 \fn QMargins operator-(const QMargins &margins)
268 \relates QMargins
269
270 Returns a QMargin object that is formed by negating all components of \a margins.
271
272 \since 5.1
273*/
274
275/*!
276 \fn QMargins operator|(const QMargins &m1, const QMargins &m2)
277 \relates QMargins
278
279 Returns a QMargins object that is formed from the maximum of each
280 component of \a m2 and \a m1.
281
282 \sa QMargins::operator+=(), QMargins::operator-=()
283
284 \since 6.0
285*/
286
287/*!
288 \fn QMargins &QMargins::operator+=(const QMargins &margins)
289
290 Add each component of \a margins to the respective component of this object
291 and returns a reference to it.
292
293 \sa operator-=()
294
295 \since 5.1
296*/
297
298/*!
299 \fn QMargins &QMargins::operator-=(const QMargins &margins)
300
301 Subtract each component of \a margins from the respective component of this object
302 and returns a reference to it.
303
304 \sa operator+=()
305
306 \since 5.1
307*/
308
309/*!
310 \fn QMargins &QMargins::operator+=(int addend)
311 \overload
312
313 Adds the \a addend to each component of this object
314 and returns a reference to it.
315
316 \sa operator-=()
317*/
318
319/*!
320 \fn QMargins &QMargins::operator-=(int subtrahend)
321 \overload
322
323 Subtracts the \a subtrahend from each component of this object
324 and returns a reference to it.
325
326 \sa operator+=()
327*/
328
329/*!
330 \fn QMargins &QMargins::operator*=(int factor)
331
332 Multiplies each component of this object by \a factor
333 and returns a reference to it.
334
335 \sa operator/=()
336
337 \since 5.1
338*/
339
340/*!
341 \fn QMargins &QMargins::operator*=(qreal factor)
342 \overload
343
344 Multiplies each component of this object by \a factor
345 and returns a reference to it.
346
347 \sa operator/=()
348
349 \since 5.1
350*/
351
352/*!
353 \fn QMargins &QMargins::operator/=(int divisor)
354
355 Divides each component of this object by \a divisor
356 and returns a reference to it.
357
358 \sa operator*=()
359
360 \since 5.1
361*/
362
363/*!
364 \fn QMargins &QMargins::operator/=(qreal divisor)
365
366 \overload
367
368 \sa operator*=()
369
370 \since 5.1
371*/
372
373/*!
374 \fn QMargins::toMarginsF() const
375 \since 6.4
376
377 Returns these margins as margins with floating point accuracy.
378
379 \sa QMarginsF::toMargins()
380*/
381
382/*****************************************************************************
383 QMargins stream functions
384 *****************************************************************************/
385#ifndef QT_NO_DATASTREAM
386/*!
387 \fn QDataStream &operator<<(QDataStream &stream, const QMargins &m)
388 \relates QMargins
389
390 Writes margin \a m to the given \a stream and returns a
391 reference to the stream.
392
393 \sa {Serializing Qt Data Types}
394*/
395
396QDataStream &operator<<(QDataStream &s, const QMargins &m)
397{
398 s << m.left() << m.top() << m.right() << m.bottom();
399 return s;
400}
401
402/*!
403 \fn QDataStream &operator>>(QDataStream &stream, QMargins &m)
404 \relates QMargins
405
406 Reads a margin from the given \a stream into margin \a m
407 and returns a reference to the stream.
408
409 \sa {Serializing Qt Data Types}
410*/
411
412QDataStream &operator>>(QDataStream &s, QMargins &m)
413{
414 int left, top, right, bottom;
415 s >> left; m.setLeft(left);
416 s >> top; m.setTop(top);
417 s >> right; m.setRight(right);
418 s >> bottom; m.setBottom(bottom);
419 return s;
420}
421#endif // QT_NO_DATASTREAM
422
423#ifndef QT_NO_DEBUG_STREAM
424QDebug operator<<(QDebug dbg, const QMargins &m)
425{
426 QDebugStateSaver saver(dbg);
427 dbg.nospace();
428 dbg << "QMargins" << '(';
429 QtDebugUtils::formatQMargins(debug&: dbg, margins: m);
430 dbg << ')';
431 return dbg;
432}
433#endif
434
435/*!
436 \class QMarginsF
437 \inmodule QtCore
438 \ingroup painting
439 \since 5.3
440
441 \brief The QMarginsF class defines the four margins of a rectangle.
442
443 QMarginsF defines a set of four margins; left, top, right, and bottom,
444 that describe the finite size of the borders surrounding a rectangle.
445
446 The isNull() function returns \c true only if all margins are very close to zero.
447
448 QMarginsF objects can be streamed as well as compared.
449*/
450
451
452/*****************************************************************************
453 QMarginsF member functions
454 *****************************************************************************/
455
456/*!
457 \fn QMarginsF::QMarginsF()
458
459 Constructs a margins object with all margins set to 0.
460
461 \sa isNull()
462*/
463
464/*!
465 \fn QMarginsF::QMarginsF(qreal left, qreal top, qreal right, qreal bottom)
466
467 Constructs margins with the given \a left, \a top, \a right, and \a bottom.
468 All parameters must be finite.
469
470 \sa setLeft(), setRight(), setTop(), setBottom()
471*/
472
473/*!
474 \fn QMarginsF::QMarginsF(const QMargins &margins)
475
476 Constructs margins copied from the given \a margins.
477
478 \sa QMargins::toMarginsF()
479*/
480
481/*!
482 \fn bool QMarginsF::isNull() const
483
484 Returns \c true if all margins are very close to 0; otherwise returns
485 false.
486
487 \sa {<QtNumeric>::}{qFuzzyIsNull()}
488*/
489
490
491/*!
492 \fn qreal QMarginsF::left() const
493
494 Returns the left margin.
495
496 \sa setLeft()
497*/
498
499/*!
500 \fn qreal QMarginsF::top() const
501
502 Returns the top margin.
503
504 \sa setTop()
505*/
506
507/*!
508 \fn qreal QMarginsF::right() const
509
510 Returns the right margin.
511*/
512
513/*!
514 \fn qreal QMarginsF::bottom() const
515
516 Returns the bottom margin.
517*/
518
519
520/*!
521 \fn void QMarginsF::setLeft(qreal aleft)
522
523 Sets the left margin to \a aleft (which must be finite).
524*/
525
526/*!
527 \fn void QMarginsF::setTop(qreal atop)
528
529 Sets the top margin to \a atop (which must be finite).
530*/
531
532/*!
533 \fn void QMarginsF::setRight(qreal aright)
534
535 Sets the right margin to \a aright (which must be finite).
536*/
537
538/*!
539 \fn void QMarginsF::setBottom(qreal abottom)
540
541 Sets the bottom margin to \a abottom (which must be finite).
542*/
543
544/*!
545 \fn bool QMarginsF::operator==(const QMarginsF &lhs, const QMarginsF &rhs)
546
547 Returns \c true if \a lhs and \a rhs are approximately equal; otherwise
548 returns false.
549
550 \warning This function does not check for strict equality; instead,
551 it uses a fuzzy comparison to compare the margins.
552
553 \sa qFuzzyCompare
554*/
555
556/*!
557 \fn bool QMarginsF::operator!=(const QMarginsF &lhs, const QMarginsF &rhs)
558
559 Returns \c true if \a lhs and \a rhs are sufficiently different; otherwise
560 returns \c false.
561
562 \warning This function does not check for strict inequality; instead,
563 it uses a fuzzy comparison to compare the margins.
564
565 \sa qFuzzyCompare
566*/
567
568/*!
569 \fn const QMarginsF operator+(const QMarginsF &lhs, const QMarginsF &rhs)
570 \relates QMarginsF
571
572 Returns a QMarginsF object that is the sum of the given margins, \a lhs
573 and \a rhs; each component is added separately.
574
575 \sa QMarginsF::operator+=(), QMarginsF::operator-=()
576*/
577
578/*!
579 \fn const QMarginsF operator-(const QMarginsF &lhs, const QMarginsF &rhs)
580 \relates QMarginsF
581
582 Returns a QMarginsF object that is formed by subtracting \a rhs from
583 \a lhs; each component is subtracted separately.
584
585 \sa QMarginsF::operator+=(), QMarginsF::operator-=()
586*/
587
588/*!
589 \fn const QMarginsF operator+(const QMarginsF &lhs, qreal rhs)
590 \relates QMarginsF
591
592 Returns a QMarginsF object that is formed by adding \a rhs (which must be
593 finite) to each component of \a lhs.
594
595 \sa QMarginsF::operator+=(), QMarginsF::operator-=()
596*/
597
598/*!
599 \fn const QMarginsF operator+(qreal lhs, const QMarginsF &rhs)
600 \relates QMarginsF
601
602 Returns a QMarginsF object that is formed by adding \a lhs (which must be
603 finite) to each component of \a rhs.
604
605 \sa QMarginsF::operator+=(), QMarginsF::operator-=()
606*/
607
608/*!
609 \fn const QMarginsF operator-(const QMarginsF &lhs, qreal rhs)
610 \relates QMarginsF
611
612 Returns a QMarginsF object that is formed by subtracting \a rhs (which must
613 be finite) from each component of \a lhs.
614
615 \sa QMarginsF::operator+=(), QMarginsF::operator-=()
616*/
617
618/*!
619 \fn const QMarginsF operator*(const QMarginsF &lhs, qreal rhs)
620 \relates QMarginsF
621 \overload
622
623 Returns a QMarginsF object that is formed by multiplying each component
624 of the given \a lhs margins by finite factor \a rhs.
625
626 \sa QMarginsF::operator*=(), QMarginsF::operator/=()
627*/
628
629/*!
630 \fn const QMarginsF operator*(qreal lhs, const QMarginsF &rhs)
631 \relates QMarginsF
632 \overload
633
634 Returns a QMarginsF object that is formed by multiplying each component
635 of the given \a lhs margins by finite factor \a rhs.
636
637 \sa QMarginsF::operator*=(), QMarginsF::operator/=()
638*/
639
640/*!
641 \fn const QMarginsF operator/(const QMarginsF &lhs, qreal rhs)
642 \relates QMarginsF
643 \overload
644
645 Returns a QMarginsF object that is formed by dividing the components of
646 the given \a lhs margins by the given \a rhs divisor.
647
648 The divisor must not be either zero or NaN.
649
650 \sa QMarginsF::operator*=(), QMarginsF::operator/=()
651*/
652
653/*!
654 \fn QMarginsF operator|(const QMarginsF &m1, const QMarginsF &m2)
655 \relates QMarginsF
656 \overload
657
658 Returns a QMarginsF object that is formed from the maximum of each
659 component of \a m2 and \a m1.
660
661 \sa QMarginsF::operator+=(), QMarginsF::operator-=()
662
663 \since 6.0
664*/
665
666/*!
667 \fn QMarginsF operator+(const QMarginsF &margins)
668 \relates QMarginsF
669
670 Returns a QMargin object that is formed from all components of \a margins.
671*/
672
673/*!
674 \fn QMarginsF operator-(const QMarginsF &margins)
675 \relates QMarginsF
676
677 Returns a QMargin object that is formed by negating all components of \a margins.
678*/
679
680/*!
681 \fn QMarginsF &QMarginsF::operator+=(const QMarginsF &margins)
682
683 Add each component of \a margins to the respective component of this object
684 and returns a reference to it.
685
686 \sa operator-=()
687*/
688
689/*!
690 \fn QMarginsF &QMarginsF::operator-=(const QMarginsF &margins)
691
692 Subtract each component of \a margins from the respective component of this object
693 and returns a reference to it.
694
695 \sa operator+=()
696*/
697
698/*!
699 \fn QMarginsF &QMarginsF::operator+=(qreal addend)
700 \overload
701
702 Adds the given finite \a addend to each component of this object
703 and returns a reference to it.
704
705 \sa operator-=()
706*/
707
708/*!
709 \fn QMarginsF &QMarginsF::operator-=(qreal subtrahend)
710 \overload
711
712 Subtracts the given finite \a subtrahend from each component of this object
713 and returns a reference to it.
714
715 \sa operator+=()
716*/
717
718/*!
719 \fn QMarginsF &QMarginsF::operator*=(qreal factor)
720
721 Multiplies each component of this object by the given finite \a factor
722 and returns a reference to this object.
723
724 \sa operator/=()
725*/
726
727/*!
728 \fn QMarginsF &QMarginsF::operator/=(qreal divisor)
729
730 Divides each component of this object by \a divisor and returns a reference
731 to this object.
732
733 The \a divisor must not be either zero or NaN.
734
735 \sa operator*=()
736*/
737
738/*!
739 \fn QMargins QMarginsF::toMargins() const
740
741 Returns an integer-based copy of this margins object.
742
743 Note that the components in the returned margins will be rounded to
744 the nearest integer.
745
746 \sa QMarginsF(), QMargins::toMarginsF()
747*/
748
749/*****************************************************************************
750 QMarginsF stream functions
751 *****************************************************************************/
752#ifndef QT_NO_DATASTREAM
753/*!
754 \fn QDataStream &operator<<(QDataStream &stream, const QMarginsF &m)
755 \relates QMarginsF
756
757 Writes margin \a m to the given \a stream and returns a
758 reference to the stream.
759
760 \sa {Serializing Qt Data Types}
761*/
762
763QDataStream &operator<<(QDataStream &s, const QMarginsF &m)
764{
765 s << double(m.left()) << double(m.top()) << double(m.right()) << double(m.bottom());
766 return s;
767}
768
769/*!
770 \fn QDataStream &operator>>(QDataStream &stream, QMarginsF &m)
771 \relates QMarginsF
772
773 Reads a margin from the given \a stream into margin \a m
774 and returns a reference to the stream.
775
776 \sa {Serializing Qt Data Types}
777*/
778
779QDataStream &operator>>(QDataStream &s, QMarginsF &m)
780{
781 double left, top, right, bottom;
782 s >> left;
783 s >> top;
784 s >> right;
785 s >> bottom;
786 m = QMarginsF(qreal(left), qreal(top), qreal(right), qreal(bottom));
787 return s;
788}
789#endif // QT_NO_DATASTREAM
790
791#ifndef QT_NO_DEBUG_STREAM
792QDebug operator<<(QDebug dbg, const QMarginsF &m)
793{
794 QDebugStateSaver saver(dbg);
795 dbg.nospace();
796 dbg << "QMarginsF" << '(';
797 QtDebugUtils::formatQMargins(debug&: dbg, margins: m);
798 dbg << ')';
799 return dbg;
800}
801#endif
802
803QT_END_NAMESPACE
804

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