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