Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qpagelayout.cpp
Go to the documentation of this file.
1// Copyright (C) 2014 John Layt <jlayt@kde.org>
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4
5#include "qpagelayout.h"
6
7#include <QtCore/qpoint.h>
8#include <QtCore/qrect.h>
9#include <QtCore/qsize.h>
10
11#include <qdebug.h>
12
14
18
19// Multiplier for converting units to points.
20Q_GUI_EXPORT qreal qt_pointMultiplier(QPageLayout::Unit unit)
21{
22 switch (unit) {
24 return 2.83464566929;
26 return 1.0;
28 return 72.0;
30 return 12;
32 return 1.065826771;
34 return 12.789921252;
35 }
36 return 1.0;
37}
38
39// Multiplier for converting pixels to points.
40extern qreal qt_pixelMultiplier(int resolution);
41
43{
44 // If the size have the same units, or are all 0, then don't need to convert
45 if (fromUnits == toUnits || xy.isNull())
46 return xy;
47
48 // If converting to points then convert and round to 0 decimal places
49 if (toUnits == QPageLayout::Point) {
50 const qreal multiplier = qt_pointMultiplier(fromUnits);
51 return QPointF(qRound(xy.x() * multiplier),
52 qRound(xy.y() * multiplier));
53 }
54
55 // If converting to other units, need to convert to unrounded points first
56 QPointF pointXy = (fromUnits == QPageLayout::Point) ? xy : xy * qt_pointMultiplier(fromUnits);
57
58 // Then convert from points to required units rounded to 2 decimal places
59 const qreal multiplier = qt_pointMultiplier(toUnits);
60 return QPointF(qRound(pointXy.x() * 100 / multiplier) / 100.0,
61 qRound(pointXy.y() * 100 / multiplier) / 100.0);
62}
63
64Q_GUI_EXPORT QMarginsF qt_convertMargins(const QMarginsF &margins, QPageLayout::Unit fromUnits, QPageLayout::Unit toUnits)
65{
66 // If the margins have the same units, or are all 0, then don't need to convert
67 if (fromUnits == toUnits || margins.isNull())
68 return margins;
69
70 // If converting to points then convert and round to 0 decimal places
71 if (toUnits == QPageLayout::Point) {
72 const qreal multiplier = qt_pointMultiplier(fromUnits);
73 return QMarginsF(qRound(margins.left() * multiplier),
74 qRound(margins.top() * multiplier),
75 qRound(margins.right() * multiplier),
76 qRound(margins.bottom() * multiplier));
77 }
78
79 // If converting to other units, need to convert to unrounded points first
80 QMarginsF pointMargins = fromUnits == QPageLayout::Point ? margins : margins * qt_pointMultiplier(fromUnits);
81
82 // Then convert from points to required units rounded to 2 decimal places
83 const qreal multiplier = qt_pointMultiplier(toUnits);
84 return QMarginsF(qRound(pointMargins.left() * 100 / multiplier) / 100.0,
85 qRound(pointMargins.top() * 100 / multiplier) / 100.0,
86 qRound(pointMargins.right() * 100 / multiplier) / 100.0,
87 qRound(pointMargins.bottom() * 100 / multiplier) / 100.0);
88}
89
91{
92public:
93
94 QPageLayoutPrivate(const QPageSize &pageSize, QPageLayout::Orientation orientation,
96 const QMarginsF &minMargins);
98
99 bool operator==(const QPageLayoutPrivate &other) const;
100 bool isEquivalentTo(const QPageLayoutPrivate &other) const;
101
102 bool isValid() const;
103
104 void clampMargins(const QMarginsF &margins);
105
107 QMargins marginsPoints() const;
108 QMargins marginsPixels(int resolution) const;
109
110 void setDefaultMargins(const QMarginsF &minMargins);
111
113
114 QRectF fullRect() const;
116 QRect fullRectPoints() const;
117 QRect fullRectPixels(int resolution) const;
118
119 QRectF paintRect() const;
120
121private:
122 friend class QPageLayout;
123
124 QSizeF fullSizeUnits(QPageLayout::Unit units) const;
125
126 QPageSize m_pageSize;
127 QPageLayout::Orientation m_orientation;
128 QPageLayout::Mode m_mode;
129 QPageLayout::Unit m_units;
130 QSizeF m_fullSize;
131 QMarginsF m_margins;
132 QMarginsF m_minMargins;
133 QMarginsF m_maxMargins;
134};
135
138 const QMarginsF &minMargins)
139 : m_pageSize(pageSize),
140 m_orientation(orientation),
141 m_mode(QPageLayout::StandardMode),
142 m_units(units),
143 m_margins(margins)
144{
145 m_fullSize = fullSizeUnits(m_units);
146 setDefaultMargins(minMargins);
147}
148
150{
151}
152
154{
155 return m_pageSize == other.m_pageSize
156 && m_orientation == other.m_orientation
157 && m_units == other.m_units
158 && m_margins == other.m_margins
159 && m_minMargins == other.m_minMargins
160 && m_maxMargins == other.m_maxMargins;
161}
162
164{
165 return m_pageSize.isEquivalentTo(other.m_pageSize)
166 && m_orientation == other.m_orientation
167 && qt_convertMargins(m_margins, m_units, QPageLayout::Point)
168 == qt_convertMargins(other.m_margins, other.m_units, QPageLayout::Point);
169}
170
172{
173 return m_pageSize.isValid();
174}
175
177{
178 m_margins = QMarginsF(qBound(m_minMargins.left(), margins.left(), m_maxMargins.left()),
179 qBound(m_minMargins.top(), margins.top(), m_maxMargins.top()),
180 qBound(m_minMargins.right(), margins.right(), m_maxMargins.right()),
181 qBound(m_minMargins.bottom(), margins.bottom(), m_maxMargins.bottom()));
182}
183
185{
186 return qt_convertMargins(m_margins, m_units, units);
187}
188
190{
191 return qt_convertMargins(m_margins, m_units, QPageLayout::Point).toMargins();
192}
193
195{
196 return marginsPoints() / qt_pixelMultiplier(resolution);
197}
198
200{
201 m_minMargins = minMargins;
202 m_maxMargins = QMarginsF(qMax(m_fullSize.width() - m_minMargins.right(), qreal(0)),
203 qMax(m_fullSize.height() - m_minMargins.bottom(), qreal(0)),
204 qMax(m_fullSize.width() - m_minMargins.left(), qreal(0)),
205 qMax(m_fullSize.height() - m_minMargins.top(), qreal(0)));
206 if (m_mode == QPageLayout::StandardMode)
207 clampMargins(m_margins);
208}
209
210QSizeF QPageLayoutPrivate::fullSizeUnits(QPageLayout::Unit units) const
211{
212 QSizeF fullPageSize = m_pageSize.size(QPageSize::Unit(units));
213 return m_orientation == QPageLayout::Landscape ? fullPageSize.transposed() : fullPageSize;
214}
215
217{
218 return QRectF(QPointF(0, 0), m_fullSize);
219}
220
222{
223 return units == m_units ? fullRect() : QRectF(QPointF(0, 0), fullSizeUnits(units));
224}
225
227{
228 if (m_orientation == QPageLayout::Landscape)
229 return QRect(QPoint(0, 0), m_pageSize.sizePoints().transposed());
230 else
231 return QRect(QPoint(0, 0), m_pageSize.sizePoints());
232}
233
235{
236 if (m_orientation == QPageLayout::Landscape)
237 return QRect(QPoint(0, 0), m_pageSize.sizePixels(resolution).transposed());
238 else
239 return QRect(QPoint(0, 0), m_pageSize.sizePixels(resolution));
240}
241
243{
244 return m_mode == QPageLayout::FullPageMode ? fullRect() : fullRect() - m_margins;
245}
246
247
321{
322}
323
338 const QMarginsF &margins, Unit units,
339 const QMarginsF &minMargins)
341{
342}
343
349 : d(other.d)
350{
351}
352
358{
359}
360
366{
367 d = other.d;
368 return *this;
369}
370
414bool QPageLayout::equals(const QPageLayout &other) const
415{
416 return d == other.d || *d == *other.d;
417}
418
419
426{
427 return d && other.d && d->isEquivalentTo(*other.d);
428}
429
435{
436 return d->isValid();
437}
438
444{
445 d.detach();
446 d->m_mode = mode;
447}
448
454{
455 return d->m_mode;
456}
457
471{
472 if (!pageSize.isValid())
473 return;
474 d.detach();
475 d->m_pageSize = pageSize;
476 d->m_fullSize = d->fullSizeUnits(d->m_units);
477 d->setDefaultMargins(minMargins);
478}
479
489{
490 return d->m_pageSize;
491}
492
501{
502 if (orientation != d->m_orientation) {
503 d.detach();
504 d->m_orientation = orientation;
505 d->m_fullSize = d->fullSizeUnits(d->m_units);
506 // Adust the max margins to reflect change in max page size
507 const qreal change = d->m_fullSize.width() - d->m_fullSize.height();
508 d->m_maxMargins.setLeft(d->m_maxMargins.left() + change);
509 d->m_maxMargins.setRight(d->m_maxMargins.right() + change);
510 d->m_maxMargins.setTop(d->m_maxMargins.top() - change);
511 d->m_maxMargins.setBottom(d->m_maxMargins.bottom() - change);
512 }
513}
514
520{
521 return d->m_orientation;
522}
523
529{
530 if (units != d->m_units) {
531 d.detach();
532 d->m_margins = qt_convertMargins(d->m_margins, d->m_units, units);
533 d->m_minMargins = qt_convertMargins(d->m_minMargins, d->m_units, units);
534 d->m_maxMargins = qt_convertMargins(d->m_maxMargins, d->m_units, units);
535 d->m_units = units;
536 d->m_fullSize = d->fullSizeUnits(d->m_units);
537 }
538}
539
545{
546 return d->m_units;
547}
548
566{
567 if (d->m_mode == FullPageMode) {
568 d.detach();
569 d->m_margins = margins;
570 return true;
571 } else if (margins.left() >= d->m_minMargins.left()
572 && margins.right() >= d->m_minMargins.right()
573 && margins.top() >= d->m_minMargins.top()
574 && margins.bottom() >= d->m_minMargins.bottom()
575 && margins.left() <= d->m_maxMargins.left()
576 && margins.right() <= d->m_maxMargins.right()
577 && margins.top() <= d->m_maxMargins.top()
578 && margins.bottom() <= d->m_maxMargins.bottom()) {
579 d.detach();
580 d->m_margins = margins;
581 return true;
582 }
583 return false;
584}
585
603{
604 if (d->m_mode == FullPageMode
605 || (leftMargin >= d->m_minMargins.left() && leftMargin <= d->m_maxMargins.left())) {
606 d.detach();
607 d->m_margins.setLeft(leftMargin);
608 return true;
609 }
610 return false;
611}
612
630{
631 if (d->m_mode == FullPageMode
632 || (rightMargin >= d->m_minMargins.right() && rightMargin <= d->m_maxMargins.right())) {
633 d.detach();
634 d->m_margins.setRight(rightMargin);
635 return true;
636 }
637 return false;
638}
639
657{
658 if (d->m_mode == FullPageMode
659 || (topMargin >= d->m_minMargins.top() && topMargin <= d->m_maxMargins.top())) {
660 d.detach();
661 d->m_margins.setTop(topMargin);
662 return true;
663 }
664 return false;
665}
666
684{
685 if (d->m_mode == FullPageMode
686 || (bottomMargin >= d->m_minMargins.bottom() && bottomMargin <= d->m_maxMargins.bottom())) {
687 d.detach();
688 d->m_margins.setBottom(bottomMargin);
689 return true;
690 }
691 return false;
692}
693
701{
702 return d->m_margins;
703}
704
712{
713 return d->margins(units);
714}
715
723{
724 return d->marginsPoints();
725}
726
734{
735 return d->marginsPixels(resolution);
736}
737
752{
753 d.detach();
754 d->setDefaultMargins(minMargins);
755}
756
764{
765 return d->m_minMargins;
766}
767
781{
782 return d->m_maxMargins;
783}
784
795{
796 return isValid() ? d->fullRect() : QRect();
797}
798
809{
810 return isValid() ? d->fullRect(units) : QRect();
811}
812
823{
824 return isValid() ? d->fullRectPoints() : QRect();
825}
826
837{
838 return isValid() ? d->fullRectPixels(resolution) : QRect();
839}
840
852{
853 return isValid() ? d->paintRect() : QRectF();
854}
855
867{
868 if (!isValid())
869 return QRectF();
870 if (units == d->m_units)
871 return d->paintRect();
872 return d->m_mode == FullPageMode ? d->fullRect(units)
873 : d->fullRect(units) - d->margins(units);
874}
875
887{
888 if (!isValid())
889 return QRect();
890 return d->m_mode == FullPageMode ? d->fullRectPoints()
891 : d->fullRectPoints() - d->marginsPoints();
892}
893
905{
906 if (!isValid())
907 return QRect();
908 return d->m_mode == FullPageMode ? d->fullRectPixels(resolution)
909 : d->fullRectPixels(resolution) - d->marginsPixels(resolution);
910}
911
912#ifndef QT_NO_DEBUG_STREAM
914{
915 QDebugStateSaver saver(dbg);
916 dbg.nospace();
917 dbg.noquote();
918 dbg << "QPageLayout(";
919 if (layout.isValid()) {
920 const QMarginsF margins = layout.margins();
921 dbg << '"' << layout.pageSize().name() << "\", "
922 << (layout.orientation() == QPageLayout::Portrait ? "Portrait" : "Landscape")
923 << ", l:" << margins.left() << " r:" << margins.right() << " t:"
924 << margins.top() << " b:" << margins.bottom() << ' ';
925 switch (layout.units()) {
927 dbg << "mm";
928 break;
930 dbg << "pt";
931 break;
933 dbg << "in";
934 break;
936 dbg << "pc";
937 break;
939 dbg << "DD";
940 break;
942 dbg << "CC";
943 break;
944 }
945 }
946 dbg << ')';
947 return dbg;
948}
949#endif
950
\inmodule QtCore
\inmodule QtCore
void detach()
If the shared data object's reference count is greater than 1, this function creates a deep copy of t...
\inmodule QtCore
Definition qmargins.h:274
constexpr qreal right() const noexcept
Returns the right margin.
Definition qmargins.h:370
constexpr qreal left() const noexcept
Returns the left margin.
Definition qmargins.h:364
constexpr qreal top() const noexcept
Returns the top margin.
Definition qmargins.h:367
constexpr void setLeft(qreal aleft) noexcept
Sets the left margin to aleft (which must be finite).
Definition qmargins.h:377
constexpr bool isNull() const noexcept
Returns true if all margins are very close to 0; otherwise returns false.
Definition qmargins.h:361
constexpr QMargins toMargins() const noexcept
Returns an integer-based copy of this margins object.
Definition qmargins.h:494
constexpr void setRight(qreal aright) noexcept
Sets the right margin to aright (which must be finite).
Definition qmargins.h:383
constexpr void setBottom(qreal abottom) noexcept
Sets the bottom margin to abottom (which must be finite).
Definition qmargins.h:386
constexpr void setTop(qreal atop) noexcept
Sets the top margin to atop (which must be finite).
Definition qmargins.h:380
constexpr qreal bottom() const noexcept
Returns the bottom margin.
Definition qmargins.h:373
\inmodule QtCore
Definition qmargins.h:23
bool isEquivalentTo(const QPageLayoutPrivate &other) const
bool operator==(const QPageLayoutPrivate &other) const
void setDefaultMargins(const QMarginsF &minMargins)
QMargins marginsPixels(int resolution) const
QRect fullRectPixels(int resolution) const
QRect fullRectPoints() const
QMarginsF margins(QPageLayout::Unit units) const
QRectF paintRect() const
bool isValid() const
QPageLayoutPrivate(const QPageSize &pageSize, QPageLayout::Orientation orientation, const QMarginsF &margins, QPageLayout::Unit units, const QMarginsF &minMargins)
void clampMargins(const QMarginsF &margins)
QRectF fullRect() const
QMargins marginsPoints() const
QSizeF paintSize() const
\inmodule QtGui
Definition qpagelayout.h:20
QRect paintRectPoints() const
Returns the paintable rectangle in rounded Postscript Points (1/72 of an inch).
QMargins marginsPoints() const
Returns the margins of the page layout in Postscript Points (1/72 of an inch).
bool setLeftMargin(qreal leftMargin)
Sets the left page margin of the page layout to leftMargin.
QMarginsF minimumMargins() const
Returns the minimum margins of the page layout.
void setOrientation(Orientation orientation)
Sets the page orientation of the page layout to orientation.
QRect fullRectPoints() const
Returns the full page rectangle in Postscript Points (1/72 of an inch).
bool setBottomMargin(qreal bottomMargin)
Sets the bottom page margin of the page layout to bottomMargin.
Mode mode() const
Returns the page layout mode.
QRectF fullRect() const
Returns the full page rectangle in the current layout units.
bool isEquivalentTo(const QPageLayout &other) const
Returns true if this page layout is equivalent to the other page layout, i.e.
void setMinimumMargins(const QMarginsF &minMargins)
Sets the minimum page margins of the page layout to minMargins.
void setUnits(Unit units)
Sets the units used to define the page layout.
bool setMargins(const QMarginsF &margins)
Sets the page margins of the page layout to margins Returns true if the margins were successfully set...
Unit units() const
Returns the units the page layout is currently defined in.
bool isValid() const
Returns true if this page layout is valid.
QMargins marginsPixels(int resolution) const
Returns the margins of the page layout in device pixels for the given resolution.
bool setRightMargin(qreal rightMargin)
Sets the right page margin of the page layout to rightMargin.
QRectF paintRect() const
Returns the page rectangle in the current layout units.
Unit
This enum type is used to specify the measurement unit for page layout and margins.
Definition qpagelayout.h:24
bool setTopMargin(qreal topMargin)
Sets the top page margin of the page layout to topMargin.
QPageLayout()
Creates an invalid QPageLayout.
QMarginsF margins() const
Returns the margins of the page layout using the currently set units.
Mode
Defines the page layout mode.
Definition qpagelayout.h:38
QPageLayout & operator=(const QPageLayout &other)
Assignment operator, assigns other to this.
Orientation orientation() const
Returns the page orientation of the page layout.
~QPageLayout()
Destroys the page layout.
QMarginsF maximumMargins() const
Returns the maximum margins that would be applied if the page layout was in StandardMode.
QRect paintRectPixels(int resolution) const
Returns the paintable rectangle in rounded device pixels for the given resolution.
void setPageSize(const QPageSize &pageSize, const QMarginsF &minMargins=QMarginsF(0, 0, 0, 0))
Sets the page size of the page layout to pageSize.
Orientation
This enum type defines the page orientation.
Definition qpagelayout.h:33
QRect fullRectPixels(int resolution) const
Returns the full page rectangle in device pixels for the given resolution.
void setMode(Mode mode)
Sets a page layout mode to mode.
QPageSize pageSize() const
Returns the page size of the page layout.
\inmodule QtGui
Definition qpagesize.h:22
bool isValid() const
Returns true if this page size is valid.
Unit
This enum type is used to specify the measurement unit for page sizes.
Definition qpagesize.h:175
QSizeF size(Unit units) const
Returns the size of the page in the required units.
QSize sizePoints() const
Returns the size of the page in Postscript Points (1/72 of an inch).
QSize sizePixels(int resolution) const
Returns the size of the page in Device Pixels at the given resolution.
bool isEquivalentTo(const QPageSize &other) const
Returns true if this page is equivalent to the other page, i.e.
\inmodule QtCore\reentrant
Definition qpoint.h:214
constexpr qreal x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:333
constexpr qreal y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:338
bool isNull() const noexcept
Returns true if both the x and y coordinates are set to 0.0 (ignoring the sign); otherwise returns fa...
Definition qpoint.h:328
\inmodule QtCore\reentrant
Definition qpoint.h:23
\inmodule QtCore\reentrant
Definition qrect.h:483
\inmodule QtCore\reentrant
Definition qrect.h:30
\inmodule QtCore
Definition qshareddata.h:19
\inmodule QtCore
Definition qsize.h:207
constexpr qreal width() const noexcept
Returns the width.
Definition qsize.h:321
constexpr QSizeF transposed() const noexcept
Definition qsize.h:333
constexpr qreal height() const noexcept
Returns the height.
Definition qsize.h:324
constexpr QSize transposed() const noexcept
Definition qsize.h:141
Combined button and popup list for selecting options.
int qRound(qfloat16 d) noexcept
Definition qfloat16.h:281
#define QT_IMPL_METATYPE_EXTERN_TAGGED(TYPE, TAG)
Definition qmetatype.h:1363
#define QT_IMPL_METATYPE_EXTERN(TYPE)
Definition qmetatype.h:1369
constexpr const T & qBound(const T &min, const T &val, const T &max)
Definition qminmax.h:44
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
GLenum mode
GLfloat units
QPointF qt_convertPoint(const QPointF &xy, QPageLayout::Unit fromUnits, QPageLayout::Unit toUnits)
Q_GUI_EXPORT QMarginsF qt_convertMargins(const QMarginsF &margins, QPageLayout::Unit fromUnits, QPageLayout::Unit toUnits)
qreal qt_pixelMultiplier(int resolution)
QDebug operator<<(QDebug dbg, const QPageLayout &layout)
QT_BEGIN_NAMESPACE Q_GUI_EXPORT qreal qt_pointMultiplier(QPageLayout::Unit unit)
double qreal
Definition qtypes.h:92
QVBoxLayout * layout
QSharedPointer< T > other(t)
[5]