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
13QT_BEGIN_NAMESPACE
14
15QT_IMPL_METATYPE_EXTERN(QPageLayout)
16QT_IMPL_METATYPE_EXTERN_TAGGED(QPageLayout::Unit, QPageLayout__Unit)
17QT_IMPL_METATYPE_EXTERN_TAGGED(QPageLayout::Orientation, QPageLayout__Orientation)
18
19// Multiplier for converting units to points.
20Q_GUI_EXPORT qreal qt_pointMultiplier(QPageLayout::Unit unit)
21{
22 switch (unit) {
23 case QPageLayout::Millimeter:
24 return 2.83464566929;
25 case QPageLayout::Point:
26 return 1.0;
27 case QPageLayout::Inch:
28 return 72.0;
29 case QPageLayout::Pica:
30 return 12;
31 case QPageLayout::Didot:
32 return 1.065826771;
33 case QPageLayout::Cicero:
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
42Q_GUI_EXPORT QMarginsF qt_convertMargins(const QMarginsF &margins, QPageLayout::Unit fromUnits, QPageLayout::Unit toUnits)
43{
44 // If the margins have the same units, or are all 0, then don't need to convert
45 if (fromUnits == toUnits || margins.isNull())
46 return margins;
47
48 // If converting to points then convert and round up to 2 decimal places
49 if (toUnits == QPageLayout::Point) {
50 const qreal multiplierX100 = qt_pointMultiplier(unit: fromUnits) * 100;
51 return QMarginsF(qCeil(v: margins.left() * multiplierX100) / 100.0,
52 qCeil(v: margins.top() * multiplierX100) / 100.0,
53 qCeil(v: margins.right() * multiplierX100) / 100.0,
54 qCeil(v: margins.bottom() * multiplierX100) / 100.0);
55 }
56
57 // If converting to other units, need to convert to unrounded points first
58 QMarginsF pointMargins = fromUnits == QPageLayout::Point ? margins : margins * qt_pointMultiplier(unit: fromUnits);
59
60 // Then convert from points to required units rounded to 2 decimal places
61 const qreal multiplier = qt_pointMultiplier(unit: toUnits);
62 return QMarginsF(qRound(d: pointMargins.left() * 100 / multiplier) / 100.0,
63 qRound(d: pointMargins.top() * 100 / multiplier) / 100.0,
64 qRound(d: pointMargins.right() * 100 / multiplier) / 100.0,
65 qRound(d: pointMargins.bottom() * 100 / multiplier) / 100.0);
66}
67
68class QPageLayoutPrivate : public QSharedData
69{
70public:
71
72 QPageLayoutPrivate(const QPageSize &pageSize, QPageLayout::Orientation orientation,
73 const QMarginsF &margins, QPageLayout::Unit units,
74 const QMarginsF &minMargins);
75 ~QPageLayoutPrivate();
76
77 bool operator==(const QPageLayoutPrivate &other) const;
78 bool isEquivalentTo(const QPageLayoutPrivate &other) const;
79
80 bool isValid() const;
81
82 QMarginsF clampMargins(const QMarginsF &margins) const;
83
84 QMarginsF margins(QPageLayout::Unit units) const;
85 QMarginsF marginsPoints() const;
86 QMargins marginsPixels(int resolution) const;
87
88 void setDefaultMargins(const QMarginsF &minMargins);
89
90 QSizeF paintSize() const;
91
92 QRectF fullRect() const;
93 QRectF fullRect(QPageLayout::Unit units) const;
94 QRect fullRectPoints() const;
95 QRect fullRectPixels(int resolution) const;
96
97 QRectF paintRect() const;
98
99private:
100 friend class QPageLayout;
101
102 QSizeF fullSizeUnits(QPageLayout::Unit units) const;
103
104 QPageSize m_pageSize;
105 QPageLayout::Orientation m_orientation;
106 QPageLayout::Mode m_mode;
107 QPageLayout::Unit m_units;
108 QSizeF m_fullSize;
109 QMarginsF m_margins;
110 QMarginsF m_minMargins;
111 QMarginsF m_maxMargins;
112};
113
114QPageLayoutPrivate::QPageLayoutPrivate(const QPageSize &pageSize, QPageLayout::Orientation orientation,
115 const QMarginsF &margins, QPageLayout::Unit units,
116 const QMarginsF &minMargins)
117 : m_pageSize(pageSize),
118 m_orientation(orientation),
119 m_mode(QPageLayout::StandardMode),
120 m_units(units),
121 m_margins(margins)
122{
123 m_fullSize = fullSizeUnits(units: m_units);
124 setDefaultMargins(minMargins);
125}
126
127QPageLayoutPrivate::~QPageLayoutPrivate()
128{
129}
130
131bool QPageLayoutPrivate::operator==(const QPageLayoutPrivate &other) const
132{
133 return m_pageSize == other.m_pageSize
134 && m_orientation == other.m_orientation
135 && m_units == other.m_units
136 && m_margins == other.m_margins
137 && m_minMargins == other.m_minMargins
138 && m_maxMargins == other.m_maxMargins;
139}
140
141bool QPageLayoutPrivate::isEquivalentTo(const QPageLayoutPrivate &other) const
142{
143 return m_pageSize.isEquivalentTo(other: other.m_pageSize)
144 && m_orientation == other.m_orientation
145 && qt_convertMargins(margins: m_margins, fromUnits: m_units, toUnits: QPageLayout::Point)
146 == qt_convertMargins(margins: other.m_margins, fromUnits: other.m_units, toUnits: QPageLayout::Point);
147}
148
149bool QPageLayoutPrivate::isValid() const
150{
151 return m_pageSize.isValid();
152}
153
154QMarginsF QPageLayoutPrivate::clampMargins(const QMarginsF &margins) const
155{
156 return QMarginsF(qBound(min: m_minMargins.left(), val: margins.left(), max: m_maxMargins.left()),
157 qBound(min: m_minMargins.top(), val: margins.top(), max: m_maxMargins.top()),
158 qBound(min: m_minMargins.right(), val: margins.right(), max: m_maxMargins.right()),
159 qBound(min: m_minMargins.bottom(), val: margins.bottom(), max: m_maxMargins.bottom()));
160}
161
162QMarginsF QPageLayoutPrivate::margins(QPageLayout::Unit units) const
163{
164 return qt_convertMargins(margins: m_margins, fromUnits: m_units, toUnits: units);
165}
166
167QMarginsF QPageLayoutPrivate::marginsPoints() const
168{
169 return qt_convertMargins(margins: m_margins, fromUnits: m_units, toUnits: QPageLayout::Point);
170}
171
172QMargins QPageLayoutPrivate::marginsPixels(int resolution) const
173{
174 return QMarginsF(marginsPoints() / qt_pixelMultiplier(resolution)).toMargins();
175}
176
177void QPageLayoutPrivate::setDefaultMargins(const QMarginsF &minMargins)
178{
179 m_minMargins = minMargins;
180 m_maxMargins = QMarginsF(qMax(a: m_fullSize.width() - m_minMargins.right(), b: qreal(0)),
181 qMax(a: m_fullSize.height() - m_minMargins.bottom(), b: qreal(0)),
182 qMax(a: m_fullSize.width() - m_minMargins.left(), b: qreal(0)),
183 qMax(a: m_fullSize.height() - m_minMargins.top(), b: qreal(0)));
184 if (m_mode == QPageLayout::StandardMode)
185 m_margins = clampMargins(margins: m_margins);
186}
187
188QSizeF QPageLayoutPrivate::fullSizeUnits(QPageLayout::Unit units) const
189{
190 QSizeF fullPageSize = m_pageSize.size(units: QPageSize::Unit(units));
191 return m_orientation == QPageLayout::Landscape ? fullPageSize.transposed() : fullPageSize;
192}
193
194QRectF QPageLayoutPrivate::fullRect() const
195{
196 return QRectF(QPointF(0, 0), m_fullSize);
197}
198
199QRectF QPageLayoutPrivate::fullRect(QPageLayout::Unit units) const
200{
201 return units == m_units ? fullRect() : QRectF(QPointF(0, 0), fullSizeUnits(units));
202}
203
204QRect QPageLayoutPrivate::fullRectPoints() const
205{
206 if (m_orientation == QPageLayout::Landscape)
207 return QRect(QPoint(0, 0), m_pageSize.sizePoints().transposed());
208 else
209 return QRect(QPoint(0, 0), m_pageSize.sizePoints());
210}
211
212QRect QPageLayoutPrivate::fullRectPixels(int resolution) const
213{
214 if (m_orientation == QPageLayout::Landscape)
215 return QRect(QPoint(0, 0), m_pageSize.sizePixels(resolution).transposed());
216 else
217 return QRect(QPoint(0, 0), m_pageSize.sizePixels(resolution));
218}
219
220QRectF QPageLayoutPrivate::paintRect() const
221{
222 return m_mode == QPageLayout::FullPageMode ? fullRect() : fullRect() - m_margins;
223}
224
225
226/*!
227 \class QPageLayout
228 \inmodule QtGui
229 \since 5.3
230 \brief Describes the size, orientation and margins of a page.
231
232 The QPageLayout class defines the layout of a page in a paged document, with the
233 page size, orientation and margins able to be set and the full page and paintable
234 page rectangles defined by those attributes able to be queried in a variety of units.
235
236 The page size is defined by the QPageSize class which can be queried for page size
237 attributes. Note that the QPageSize itself is always defined in a Portrait
238 orientation.
239
240 The minimum margins can be defined for the layout but normally default to 0.
241 When used in conjunction with Qt's printing support the minimum margins
242 will reflect the minimum printable area defined by the printer.
243
244 In the default StandardMode the current margins and minimum margins are
245 always taken into account. The paintable rectangle is the full page
246 rectangle less the current margins, and the current margins can only be set
247 to values between the minimum margins and the maximum margins allowed by
248 the full page size.
249
250 In FullPageMode the current margins and minimum margins are not taken
251 into account. The paintable rectangle is the full page rectangle, and the
252 current margins can be set to any values regardless of the minimum margins
253 and page size.
254
255 \sa QPageSize
256*/
257
258/*!
259 \enum QPageLayout::Unit
260
261 This enum type is used to specify the measurement unit for page layout and margins.
262
263 \value Millimeter
264 \value Point 1/72th of an inch
265 \value Inch
266 \value Pica 1/72th of a foot, 1/6th of an inch, 12 Points
267 \value Didot 1/72th of a French inch, 0.375 mm
268 \value Cicero 1/6th of a French inch, 12 Didot, 4.5mm
269*/
270
271/*!
272 \enum QPageLayout::Orientation
273
274 This enum type defines the page orientation
275
276 \value Portrait The page size is used in its default orientation
277 \value Landscape The page size is rotated through 90 degrees
278
279 Note that some standard page sizes are defined with a width larger than
280 their height, hence the orientation is defined relative to the standard
281 page size and not using the relative page dimensions.
282*/
283
284/*!
285 \enum QPageLayout::Mode
286
287 Defines the page layout mode
288
289 \value StandardMode Paint Rect includes margins, margins must fall between the minimum and maximum.
290 \value FullPageMode Paint Rect excludes margins, margins can be any value and must be managed manually.
291
292 In StandardMode, when setting margins, use \l{QPageLayout::OutOfBoundsPolicy::}{Clamp} to
293 automatically clamp the margins to fall between the minimum and maximum
294 allowed values.
295
296 \sa OutOfBoundsPolicy
297*/
298
299/*!
300 \enum QPageLayout::OutOfBoundsPolicy
301 \since 6.8
302
303 Defines the policy for margins that are out of bounds
304
305 \value Reject The margins must fall within the minimum and maximum values,
306 otherwise they will be rejected.
307 \value Clamp The margins are clamped between the minimum and maximum
308 values to ensure they are valid.
309
310 \note The policy has no effect in \l{QPageLayout::Mode}{FullPageMode},
311 where all margins are accepted.
312*/
313
314/*!
315 Creates an invalid QPageLayout.
316*/
317
318QPageLayout::QPageLayout()
319 : QPageLayout(QPageSize(), QPageLayout::Landscape, QMarginsF())
320{
321}
322
323/*!
324 Creates a QPageLayout with the given \a pageSize, \a orientation and
325 \a margins in the given \a units.
326
327 Optionally define the minimum allowed margins \a minMargins, e.g. the minimum
328 margins able to be printed by a physical print device.
329
330 The constructed QPageLayout will be in StandardMode.
331
332 The \a margins given will be clamped to the minimum margins and the maximum
333 margins allowed by the page size.
334*/
335
336QPageLayout::QPageLayout(const QPageSize &pageSize, Orientation orientation,
337 const QMarginsF &margins, Unit units,
338 const QMarginsF &minMargins)
339 : d(new QPageLayoutPrivate(pageSize, orientation, margins, units, minMargins))
340{
341}
342
343/*!
344 Copy constructor, copies \a other to this.
345*/
346
347QPageLayout::QPageLayout(const QPageLayout &other)
348 : d(other.d)
349{
350}
351
352/*!
353 Destroys the page layout.
354*/
355
356QPageLayout::~QPageLayout()
357{
358}
359
360/*!
361 Assignment operator, assigns \a other to this.
362*/
363
364QPageLayout &QPageLayout::operator=(const QPageLayout &other)
365{
366 d = other.d;
367 return *this;
368}
369
370/*!
371 \fn void QPageLayout::swap(QPageLayout &other)
372
373 Swaps this page layout with \a other. This function is very fast and
374 never fails.
375*/
376
377/*!
378 \fn QPageLayout &QPageLayout::operator=(QPageLayout &&other)
379
380 Move-assigns \a other to this QPageLayout instance, transferring the
381 ownership of the managed pointer to this instance.
382*/
383
384/*!
385 \fn bool QPageLayout::operator==(const QPageLayout &lhs, const QPageLayout &rhs)
386
387 Returns \c true if page layout \a lhs is equal to page layout \a rhs,
388 i.e. if all the attributes are exactly equal.
389
390 Note that this is a strict equality, especially for page size where the
391 QPageSize ID, name and size must exactly match, and the margins where the
392 units must match.
393
394 \sa QPageLayout::isEquivalentTo()
395*/
396
397/*!
398 \fn bool QPageLayout::operator!=(const QPageLayout &lhs, const QPageLayout &rhs)
399
400 Returns \c true if page layout \a lhs is not equal to page layout \a rhs,
401 i.e. if any of the attributes differ.
402
403 Note that this is a strict equality, especially for page size where the
404 QPageSize ID, name and size must exactly match, and the margins where the
405 units must match.
406
407 \sa QPageLayout::isEquivalentTo()
408*/
409
410/*!
411 \internal
412*/
413bool QPageLayout::equals(const QPageLayout &other) const
414{
415 return d == other.d || *d == *other.d;
416}
417
418
419/*!
420 Returns \c true if this page layout is equivalent to the \a other page layout,
421 i.e. if the page has the same size, margins and orientation.
422*/
423
424bool QPageLayout::isEquivalentTo(const QPageLayout &other) const
425{
426 return d && other.d && d->isEquivalentTo(other: *other.d);
427}
428
429/*!
430 Returns \c true if this page layout is valid.
431*/
432
433bool QPageLayout::isValid() const
434{
435 return d->isValid();
436}
437
438/*!
439 Sets a page layout mode to \a mode.
440*/
441
442void QPageLayout::setMode(Mode mode)
443{
444 d.detach();
445 d->m_mode = mode;
446}
447
448/*!
449 Returns the page layout mode.
450*/
451
452QPageLayout::Mode QPageLayout::mode() const
453{
454 return d->m_mode;
455}
456
457/*!
458 Sets the page size of the page layout to \a pageSize.
459
460 Optionally define the minimum allowed margins \a minMargins, e.g. the minimum
461 margins able to be printed by a physical print device, otherwise the
462 minimum margins will default to 0.
463
464 If StandardMode is set then the existing margins will be clamped
465 to the new minimum margins and the maximum margins allowed by the page size.
466 If FullPageMode is set then the existing margins will be unchanged.
467*/
468
469void QPageLayout::setPageSize(const QPageSize &pageSize, const QMarginsF &minMargins)
470{
471 if (!pageSize.isValid())
472 return;
473 d.detach();
474 d->m_pageSize = pageSize;
475 d->m_fullSize = d->fullSizeUnits(units: d->m_units);
476 d->setDefaultMargins(minMargins);
477}
478
479/*!
480 Returns the page size of the page layout.
481
482 Note that the QPageSize is always defined in a Portrait orientation. To
483 obtain a size that takes the set orientation into account you must use
484 fullRect().
485*/
486
487QPageSize QPageLayout::pageSize() const
488{
489 return d->m_pageSize;
490}
491
492/*!
493 Sets the page orientation of the page layout to \a orientation.
494
495 Changing the orientation does not affect the current margins or
496 the minimum margins.
497*/
498
499void QPageLayout::setOrientation(Orientation orientation)
500{
501 if (orientation != d->m_orientation) {
502 d.detach();
503 d->m_orientation = orientation;
504 d->m_fullSize = d->fullSizeUnits(units: d->m_units);
505 // Adust the max margins to reflect change in max page size
506 const qreal change = d->m_fullSize.width() - d->m_fullSize.height();
507 d->m_maxMargins.setLeft(d->m_maxMargins.left() + change);
508 d->m_maxMargins.setRight(d->m_maxMargins.right() + change);
509 d->m_maxMargins.setTop(d->m_maxMargins.top() - change);
510 d->m_maxMargins.setBottom(d->m_maxMargins.bottom() - change);
511 }
512}
513
514/*!
515 Returns the page orientation of the page layout.
516*/
517
518QPageLayout::Orientation QPageLayout::orientation() const
519{
520 return d->m_orientation;
521}
522
523/*!
524 Sets the \a units used to define the page layout.
525*/
526
527void QPageLayout::setUnits(Unit units)
528{
529 if (units != d->m_units) {
530 d.detach();
531 d->m_margins = qt_convertMargins(margins: d->m_margins, fromUnits: d->m_units, toUnits: units);
532 d->m_minMargins = qt_convertMargins(margins: d->m_minMargins, fromUnits: d->m_units, toUnits: units);
533 d->m_maxMargins = qt_convertMargins(margins: d->m_maxMargins, fromUnits: d->m_units, toUnits: units);
534 d->m_units = units;
535 d->m_fullSize = d->fullSizeUnits(units: d->m_units);
536 }
537}
538
539/*!
540 Returns the units the page layout is currently defined in.
541*/
542
543QPageLayout::Unit QPageLayout::units() const
544{
545 return d->m_units;
546}
547
548/*!
549 Sets the page margins of the page layout to \a margins.
550 Returns true if the margins were successfully set.
551
552 The units used are those currently defined for the layout. To use different
553 units then call setUnits() first.
554
555 Since Qt 6.8, the optional \a outOfBoundsPolicy can be used to specify how
556 margins that are out of bounds are handled.
557
558 \sa margins(), units()
559*/
560
561bool QPageLayout::setMargins(const QMarginsF &margins, OutOfBoundsPolicy outOfBoundsPolicy)
562{
563 if (d->m_mode == FullPageMode) {
564 if (margins != d->m_margins) {
565 d.detach();
566 d->m_margins = margins;
567 }
568 return true;
569 }
570
571 if (outOfBoundsPolicy == OutOfBoundsPolicy::Clamp) {
572 const QMarginsF clampedMargins = d->clampMargins(margins);
573 if (clampedMargins != d->m_margins) {
574 d.detach();
575 d->m_margins = clampedMargins;
576 }
577 return true;
578 }
579
580 if (margins.left() >= d->m_minMargins.left()
581 && margins.right() >= d->m_minMargins.right()
582 && margins.top() >= d->m_minMargins.top()
583 && margins.bottom() >= d->m_minMargins.bottom()
584 && margins.left() <= d->m_maxMargins.left()
585 && margins.right() <= d->m_maxMargins.right()
586 && margins.top() <= d->m_maxMargins.top()
587 && margins.bottom() <= d->m_maxMargins.bottom()) {
588 if (margins != d->m_margins) {
589 d.detach();
590 d->m_margins = margins;
591 }
592 return true;
593 }
594
595 return false;
596}
597
598/*!
599 Sets the left page margin of the page layout to \a leftMargin.
600 Returns true if the margin was successfully set.
601
602 The units used are those currently defined for the layout. To use different
603 units call setUnits() first.
604
605 Since Qt 6.8, the optional \a outOfBoundsPolicy can be used to specify how
606 margins that are out of bounds are handled.
607
608 \sa setMargins(), margins()
609*/
610
611bool QPageLayout::setLeftMargin(qreal leftMargin, OutOfBoundsPolicy outOfBoundsPolicy)
612{
613 if (d->m_mode == StandardMode && outOfBoundsPolicy == OutOfBoundsPolicy::Clamp)
614 leftMargin = qBound(min: d->m_minMargins.left(), val: leftMargin, max: d->m_maxMargins.left());
615
616 if (qFuzzyCompare(p1: leftMargin, p2: d->m_margins.left()))
617 return true;
618
619 if (d->m_mode == FullPageMode
620 || (leftMargin >= d->m_minMargins.left() && leftMargin <= d->m_maxMargins.left())) {
621 d.detach();
622 d->m_margins.setLeft(leftMargin);
623 return true;
624 }
625
626 return false;
627}
628
629/*!
630 Sets the right page margin of the page layout to \a rightMargin.
631 Returns true if the margin was successfully set.
632
633 The units used are those currently defined for the layout. To use different
634 units call setUnits() first.
635
636 Since Qt 6.8, the optional \a outOfBoundsPolicy can be used to specify how
637 margins that are out of bounds are handled.
638
639 \sa setMargins(), margins()
640*/
641
642bool QPageLayout::setRightMargin(qreal rightMargin, OutOfBoundsPolicy outOfBoundsPolicy)
643{
644 if (d->m_mode == StandardMode && outOfBoundsPolicy == OutOfBoundsPolicy::Clamp)
645 rightMargin = qBound(min: d->m_minMargins.right(), val: rightMargin, max: d->m_maxMargins.right());
646
647 if (qFuzzyCompare(p1: rightMargin, p2: d->m_margins.right()))
648 return true;
649
650 if (d->m_mode == FullPageMode
651 || (rightMargin >= d->m_minMargins.right() && rightMargin <= d->m_maxMargins.right())) {
652 d.detach();
653 d->m_margins.setRight(rightMargin);
654 return true;
655 }
656
657 return false;
658}
659
660/*!
661 Sets the top page margin of the page layout to \a topMargin.
662 Returns true if the margin was successfully set.
663
664 The units used are those currently defined for the layout. To use different
665 units call setUnits() first.
666
667 Since Qt 6.8, the optional \a outOfBoundsPolicy can be used to specify how
668 margins that are out of bounds are handled.
669
670 \sa setMargins(), margins()
671*/
672
673bool QPageLayout::setTopMargin(qreal topMargin, OutOfBoundsPolicy outOfBoundsPolicy)
674{
675 if (d->m_mode == StandardMode && outOfBoundsPolicy == OutOfBoundsPolicy::Clamp)
676 topMargin = qBound(min: d->m_minMargins.top(), val: topMargin, max: d->m_maxMargins.top());
677
678 if (qFuzzyCompare(p1: topMargin, p2: d->m_margins.top()))
679 return true;
680
681 if (d->m_mode == FullPageMode
682 || (topMargin >= d->m_minMargins.top() && topMargin <= d->m_maxMargins.top())) {
683 d.detach();
684 d->m_margins.setTop(topMargin);
685 return true;
686 }
687
688 return false;
689}
690
691/*!
692 Sets the bottom page margin of the page layout to \a bottomMargin.
693 Returns true if the margin was successfully set.
694
695 The units used are those currently defined for the layout. To use different
696 units call setUnits() first.
697
698 Since Qt 6.8, the optional \a outOfBoundsPolicy can be used to specify how
699 margins that are out of bounds are handled.
700
701 \sa setMargins(), margins()
702*/
703
704bool QPageLayout::setBottomMargin(qreal bottomMargin, OutOfBoundsPolicy outOfBoundsPolicy)
705{
706 if (d->m_mode == StandardMode && outOfBoundsPolicy == OutOfBoundsPolicy::Clamp)
707 bottomMargin = qBound(min: d->m_minMargins.bottom(), val: bottomMargin, max: d->m_maxMargins.bottom());
708
709 if (qFuzzyCompare(p1: bottomMargin, p2: d->m_margins.bottom()))
710 return true;
711
712 if (d->m_mode == FullPageMode
713 || (bottomMargin >= d->m_minMargins.bottom() && bottomMargin <= d->m_maxMargins.bottom())) {
714 d.detach();
715 d->m_margins.setBottom(bottomMargin);
716 return true;
717 }
718
719 return false;
720}
721
722/*!
723 Returns the margins of the page layout using the currently set units.
724
725 \sa setMargins(), units()
726*/
727
728QMarginsF QPageLayout::margins() const
729{
730 return d->m_margins;
731}
732
733/*!
734 Returns the margins of the page layout using the requested \a units.
735
736 \sa setMargins(), margins()
737*/
738
739QMarginsF QPageLayout::margins(Unit units) const
740{
741 return d->margins(units);
742}
743
744/*!
745 Returns the margins of the page layout in Postscript Points (1/72 of an inch).
746
747 \sa setMargins(), margins()
748*/
749
750QMargins QPageLayout::marginsPoints() const
751{
752 return d->marginsPoints().toMargins();
753}
754
755/*!
756 Returns the margins of the page layout in device pixels for the given \a resolution.
757
758 \sa setMargins()
759*/
760
761QMargins QPageLayout::marginsPixels(int resolution) const
762{
763 return d->marginsPixels(resolution);
764}
765
766/*!
767 Sets the minimum page margins of the page layout to \a minMargins.
768
769 It is not recommended to override the default values set for a page size
770 as this may be the minimum printable area for a physical print device.
771
772 If the StandardMode mode is set then the existing margins will be clamped
773 to the new \a minMargins and the maximum allowed by the page size. If the
774 FullPageMode is set then the existing margins will be unchanged.
775
776 \sa minimumMargins(), setMargins()
777*/
778
779void QPageLayout::setMinimumMargins(const QMarginsF &minMargins)
780{
781 d.detach();
782 d->setDefaultMargins(minMargins);
783}
784
785/*!
786 Returns the minimum margins of the page layout.
787
788 \sa setMinimumMargins(), maximumMargins()
789*/
790
791QMarginsF QPageLayout::minimumMargins() const
792{
793 return d->m_minMargins;
794}
795
796/*!
797 Returns the maximum margins that would be applied if the page layout was
798 in StandardMode.
799
800 The maximum margins allowed are calculated as the full size of the page
801 minus the minimum margins set. For example, if the page width is 100 points
802 and the minimum right margin is 10 points, then the maximum left margin
803 will be 90 points.
804
805 \sa setMinimumMargins(), minimumMargins()
806*/
807
808QMarginsF QPageLayout::maximumMargins() const
809{
810 return d->m_maxMargins;
811}
812
813/*!
814 Returns the full page rectangle in the current layout units.
815
816 The page rectangle takes into account the page size and page orientation,
817 but not the page margins.
818
819 \sa paintRect(), units()
820*/
821
822QRectF QPageLayout::fullRect() const
823{
824 return isValid() ? d->fullRect() : QRect();
825}
826
827/*!
828 Returns the full page rectangle in the required \a units.
829
830 The page rectangle takes into account the page size and page orientation,
831 but not the page margins.
832
833 \sa paintRect()
834*/
835
836QRectF QPageLayout::fullRect(Unit units) const
837{
838 return isValid() ? d->fullRect(units) : QRect();
839}
840
841/*!
842 Returns the full page rectangle in Postscript Points (1/72 of an inch).
843
844 The page rectangle takes into account the page size and page orientation,
845 but not the page margins.
846
847 \sa paintRect()
848*/
849
850QRect QPageLayout::fullRectPoints() const
851{
852 return isValid() ? d->fullRectPoints() : QRect();
853}
854
855/*!
856 Returns the full page rectangle in device pixels for the given \a resolution.
857
858 The page rectangle takes into account the page size and page orientation,
859 but not the page margins.
860
861 \sa paintRect()
862*/
863
864QRect QPageLayout::fullRectPixels(int resolution) const
865{
866 return isValid() ? d->fullRectPixels(resolution) : QRect();
867}
868
869/*!
870 Returns the page rectangle in the current layout units.
871
872 The paintable rectangle takes into account the page size, orientation
873 and margins.
874
875 If the FullPageMode mode is set then the fullRect() is returned and
876 the margins must be manually managed.
877*/
878
879QRectF QPageLayout::paintRect() const
880{
881 return isValid() ? d->paintRect() : QRectF();
882}
883
884/*!
885 Returns the page rectangle in the required \a units.
886
887 The paintable rectangle takes into account the page size, orientation
888 and margins.
889
890 If the FullPageMode mode is set then the fullRect() is returned and
891 the margins must be manually managed.
892*/
893
894QRectF QPageLayout::paintRect(Unit units) const
895{
896 if (!isValid())
897 return QRectF();
898 if (units == d->m_units)
899 return d->paintRect();
900 return d->m_mode == FullPageMode ? d->fullRect(units)
901 : d->fullRect(units) - d->margins(units);
902}
903
904/*!
905 Returns the paintable rectangle in rounded Postscript Points (1/72 of an inch).
906
907 The paintable rectangle takes into account the page size, orientation
908 and margins.
909
910 If the FullPageMode mode is set then the fullRect() is returned and
911 the margins must be manually managed.
912*/
913
914QRect QPageLayout::paintRectPoints() const
915{
916 if (!isValid())
917 return QRect();
918 return d->m_mode == FullPageMode ? d->fullRectPoints()
919 : d->fullRectPoints() - d->marginsPoints().toMargins();
920}
921
922/*!
923 Returns the paintable rectangle in rounded device pixels for the given \a resolution.
924
925 The paintable rectangle takes into account the page size, orientation
926 and margins.
927
928 If the FullPageMode mode is set then the fullRect() is returned and
929 the margins must be manually managed.
930*/
931
932QRect QPageLayout::paintRectPixels(int resolution) const
933{
934 if (!isValid())
935 return QRect();
936 return d->m_mode == FullPageMode ? d->fullRectPixels(resolution)
937 : d->fullRectPixels(resolution) - d->marginsPixels(resolution);
938}
939
940#ifndef QT_NO_DEBUG_STREAM
941QDebug operator<<(QDebug dbg, const QPageLayout &layout)
942{
943 QDebugStateSaver saver(dbg);
944 dbg.nospace();
945 dbg.noquote();
946 dbg << "QPageLayout(";
947 if (layout.isValid()) {
948 const QMarginsF margins = layout.margins();
949 dbg << '"' << layout.pageSize().name() << "\", "
950 << (layout.orientation() == QPageLayout::Portrait ? "Portrait" : "Landscape")
951 << ", l:" << margins.left() << " r:" << margins.right() << " t:"
952 << margins.top() << " b:" << margins.bottom() << ' ';
953 switch (layout.units()) {
954 case QPageLayout::Millimeter:
955 dbg << "mm";
956 break;
957 case QPageLayout::Point:
958 dbg << "pt";
959 break;
960 case QPageLayout::Inch:
961 dbg << "in";
962 break;
963 case QPageLayout::Pica:
964 dbg << "pc";
965 break;
966 case QPageLayout::Didot:
967 dbg << "DD";
968 break;
969 case QPageLayout::Cicero:
970 dbg << "CC";
971 break;
972 }
973 }
974 dbg << ')';
975 return dbg;
976}
977#endif
978
979QT_END_NAMESPACE
980

Provided by KDAB

Privacy Policy
Start learning QML with our Intro Training
Find out more

source code of qtbase/src/gui/painting/qpagelayout.cpp