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 | |
13 | QT_BEGIN_NAMESPACE |
14 | |
15 | QT_IMPL_METATYPE_EXTERN(QPageLayout) |
16 | QT_IMPL_METATYPE_EXTERN_TAGGED(QPageLayout::Unit, QPageLayout__Unit) |
17 | QT_IMPL_METATYPE_EXTERN_TAGGED(QPageLayout::Orientation, QPageLayout__Orientation) |
18 | |
19 | // Multiplier for converting units to points. |
20 | Q_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. |
40 | extern qreal qt_pixelMultiplier(int resolution); |
41 | |
42 | Q_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 | |
68 | class QPageLayoutPrivate : public QSharedData |
69 | { |
70 | public: |
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 | |
99 | private: |
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 | |
114 | QPageLayoutPrivate::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 | |
127 | QPageLayoutPrivate::~QPageLayoutPrivate() |
128 | { |
129 | } |
130 | |
131 | bool 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 | |
141 | bool 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 | |
149 | bool QPageLayoutPrivate::isValid() const |
150 | { |
151 | return m_pageSize.isValid(); |
152 | } |
153 | |
154 | QMarginsF 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 | |
162 | QMarginsF QPageLayoutPrivate::margins(QPageLayout::Unit units) const |
163 | { |
164 | return qt_convertMargins(margins: m_margins, fromUnits: m_units, toUnits: units); |
165 | } |
166 | |
167 | QMarginsF QPageLayoutPrivate::marginsPoints() const |
168 | { |
169 | return qt_convertMargins(margins: m_margins, fromUnits: m_units, toUnits: QPageLayout::Point); |
170 | } |
171 | |
172 | QMargins QPageLayoutPrivate::marginsPixels(int resolution) const |
173 | { |
174 | return QMarginsF(marginsPoints() / qt_pixelMultiplier(resolution)).toMargins(); |
175 | } |
176 | |
177 | void 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 | |
188 | QSizeF 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 | |
194 | QRectF QPageLayoutPrivate::fullRect() const |
195 | { |
196 | return QRectF(QPointF(0, 0), m_fullSize); |
197 | } |
198 | |
199 | QRectF QPageLayoutPrivate::fullRect(QPageLayout::Unit units) const |
200 | { |
201 | return units == m_units ? fullRect() : QRectF(QPointF(0, 0), fullSizeUnits(units)); |
202 | } |
203 | |
204 | QRect 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 | |
212 | QRect 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 | |
220 | QRectF 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 | |
318 | QPageLayout::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 | |
336 | QPageLayout::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 | |
347 | QPageLayout::QPageLayout(const QPageLayout &other) |
348 | : d(other.d) |
349 | { |
350 | } |
351 | |
352 | /*! |
353 | Destroys the page layout. |
354 | */ |
355 | |
356 | QPageLayout::~QPageLayout() |
357 | { |
358 | } |
359 | |
360 | /*! |
361 | Assignment operator, assigns \a other to this. |
362 | */ |
363 | |
364 | QPageLayout &QPageLayout::operator=(const QPageLayout &other) |
365 | { |
366 | d = other.d; |
367 | return *this; |
368 | } |
369 | |
370 | /*! |
371 | \fn void QPageLayout::swap(QPageLayout &other) |
372 | \memberswap{page layout} |
373 | */ |
374 | |
375 | /*! |
376 | \fn QPageLayout &QPageLayout::operator=(QPageLayout &&other) |
377 | |
378 | Move-assigns \a other to this QPageLayout instance, transferring the |
379 | ownership of the managed pointer to this instance. |
380 | */ |
381 | |
382 | /*! |
383 | \fn bool QPageLayout::operator==(const QPageLayout &lhs, const QPageLayout &rhs) |
384 | |
385 | Returns \c true if page layout \a lhs is equal to page layout \a rhs, |
386 | i.e. if all the attributes are exactly equal. |
387 | |
388 | Note that this is a strict equality, especially for page size where the |
389 | QPageSize ID, name and size must exactly match, and the margins where the |
390 | units must match. |
391 | |
392 | \sa QPageLayout::isEquivalentTo() |
393 | */ |
394 | |
395 | /*! |
396 | \fn bool QPageLayout::operator!=(const QPageLayout &lhs, const QPageLayout &rhs) |
397 | |
398 | Returns \c true if page layout \a lhs is not equal to page layout \a rhs, |
399 | i.e. if any of the attributes differ. |
400 | |
401 | Note that this is a strict equality, especially for page size where the |
402 | QPageSize ID, name and size must exactly match, and the margins where the |
403 | units must match. |
404 | |
405 | \sa QPageLayout::isEquivalentTo() |
406 | */ |
407 | |
408 | /*! |
409 | \internal |
410 | */ |
411 | bool QPageLayout::equals(const QPageLayout &other) const |
412 | { |
413 | return d == other.d || *d == *other.d; |
414 | } |
415 | |
416 | |
417 | /*! |
418 | Returns \c true if this page layout is equivalent to the \a other page layout, |
419 | i.e. if the page has the same size, margins and orientation. |
420 | */ |
421 | |
422 | bool QPageLayout::isEquivalentTo(const QPageLayout &other) const |
423 | { |
424 | return d && other.d && d->isEquivalentTo(other: *other.d); |
425 | } |
426 | |
427 | /*! |
428 | Returns \c true if this page layout is valid. |
429 | */ |
430 | |
431 | bool QPageLayout::isValid() const |
432 | { |
433 | return d->isValid(); |
434 | } |
435 | |
436 | /*! |
437 | Sets a page layout mode to \a mode. |
438 | */ |
439 | |
440 | void QPageLayout::setMode(Mode mode) |
441 | { |
442 | d.detach(); |
443 | d->m_mode = mode; |
444 | } |
445 | |
446 | /*! |
447 | Returns the page layout mode. |
448 | */ |
449 | |
450 | QPageLayout::Mode QPageLayout::mode() const |
451 | { |
452 | return d->m_mode; |
453 | } |
454 | |
455 | /*! |
456 | Sets the page size of the page layout to \a pageSize. |
457 | |
458 | Optionally define the minimum allowed margins \a minMargins, e.g. the minimum |
459 | margins able to be printed by a physical print device, otherwise the |
460 | minimum margins will default to 0. |
461 | |
462 | If StandardMode is set then the existing margins will be clamped |
463 | to the new minimum margins and the maximum margins allowed by the page size. |
464 | If FullPageMode is set then the existing margins will be unchanged. |
465 | */ |
466 | |
467 | void QPageLayout::setPageSize(const QPageSize &pageSize, const QMarginsF &minMargins) |
468 | { |
469 | if (!pageSize.isValid()) |
470 | return; |
471 | d.detach(); |
472 | d->m_pageSize = pageSize; |
473 | d->m_fullSize = d->fullSizeUnits(units: d->m_units); |
474 | d->setDefaultMargins(minMargins); |
475 | } |
476 | |
477 | /*! |
478 | Returns the page size of the page layout. |
479 | |
480 | Note that the QPageSize is always defined in a Portrait orientation. To |
481 | obtain a size that takes the set orientation into account you must use |
482 | fullRect(). |
483 | */ |
484 | |
485 | QPageSize QPageLayout::pageSize() const |
486 | { |
487 | return d->m_pageSize; |
488 | } |
489 | |
490 | /*! |
491 | Sets the page orientation of the page layout to \a orientation. |
492 | |
493 | Changing the orientation does not affect the current margins or |
494 | the minimum margins. |
495 | */ |
496 | |
497 | void QPageLayout::setOrientation(Orientation orientation) |
498 | { |
499 | if (orientation != d->m_orientation) { |
500 | d.detach(); |
501 | d->m_orientation = orientation; |
502 | d->m_fullSize = d->fullSizeUnits(units: d->m_units); |
503 | // Adust the max margins to reflect change in max page size |
504 | const qreal change = d->m_fullSize.width() - d->m_fullSize.height(); |
505 | d->m_maxMargins.setLeft(d->m_maxMargins.left() + change); |
506 | d->m_maxMargins.setRight(d->m_maxMargins.right() + change); |
507 | d->m_maxMargins.setTop(d->m_maxMargins.top() - change); |
508 | d->m_maxMargins.setBottom(d->m_maxMargins.bottom() - change); |
509 | } |
510 | } |
511 | |
512 | /*! |
513 | Returns the page orientation of the page layout. |
514 | */ |
515 | |
516 | QPageLayout::Orientation QPageLayout::orientation() const |
517 | { |
518 | return d->m_orientation; |
519 | } |
520 | |
521 | /*! |
522 | Sets the \a units used to define the page layout. |
523 | */ |
524 | |
525 | void QPageLayout::setUnits(Unit units) |
526 | { |
527 | if (units != d->m_units) { |
528 | d.detach(); |
529 | d->m_margins = qt_convertMargins(margins: d->m_margins, fromUnits: d->m_units, toUnits: units); |
530 | d->m_minMargins = qt_convertMargins(margins: d->m_minMargins, fromUnits: d->m_units, toUnits: units); |
531 | d->m_maxMargins = qt_convertMargins(margins: d->m_maxMargins, fromUnits: d->m_units, toUnits: units); |
532 | d->m_units = units; |
533 | d->m_fullSize = d->fullSizeUnits(units: d->m_units); |
534 | } |
535 | } |
536 | |
537 | /*! |
538 | Returns the units the page layout is currently defined in. |
539 | */ |
540 | |
541 | QPageLayout::Unit QPageLayout::units() const |
542 | { |
543 | return d->m_units; |
544 | } |
545 | |
546 | /*! |
547 | Sets the page margins of the page layout to \a margins. |
548 | Returns true if the margins were successfully set. |
549 | |
550 | The units used are those currently defined for the layout. To use different |
551 | units then call setUnits() first. |
552 | |
553 | Since Qt 6.8, the optional \a outOfBoundsPolicy can be used to specify how |
554 | margins that are out of bounds are handled. |
555 | |
556 | \sa margins(), units() |
557 | */ |
558 | |
559 | bool QPageLayout::setMargins(const QMarginsF &margins, OutOfBoundsPolicy outOfBoundsPolicy) |
560 | { |
561 | if (d->m_mode == FullPageMode) { |
562 | if (margins != d->m_margins) { |
563 | d.detach(); |
564 | d->m_margins = margins; |
565 | } |
566 | return true; |
567 | } |
568 | |
569 | if (outOfBoundsPolicy == OutOfBoundsPolicy::Clamp) { |
570 | const QMarginsF clampedMargins = d->clampMargins(margins); |
571 | if (clampedMargins != d->m_margins) { |
572 | d.detach(); |
573 | d->m_margins = clampedMargins; |
574 | } |
575 | return true; |
576 | } |
577 | |
578 | if (margins.left() >= d->m_minMargins.left() |
579 | && margins.right() >= d->m_minMargins.right() |
580 | && margins.top() >= d->m_minMargins.top() |
581 | && margins.bottom() >= d->m_minMargins.bottom() |
582 | && margins.left() <= d->m_maxMargins.left() |
583 | && margins.right() <= d->m_maxMargins.right() |
584 | && margins.top() <= d->m_maxMargins.top() |
585 | && margins.bottom() <= d->m_maxMargins.bottom()) { |
586 | if (margins != d->m_margins) { |
587 | d.detach(); |
588 | d->m_margins = margins; |
589 | } |
590 | return true; |
591 | } |
592 | |
593 | return false; |
594 | } |
595 | |
596 | /*! |
597 | Sets the left page margin of the page layout to \a leftMargin. |
598 | Returns true if the margin was successfully set. |
599 | |
600 | The units used are those currently defined for the layout. To use different |
601 | units call setUnits() first. |
602 | |
603 | Since Qt 6.8, the optional \a outOfBoundsPolicy can be used to specify how |
604 | margins that are out of bounds are handled. |
605 | |
606 | \sa setMargins(), margins() |
607 | */ |
608 | |
609 | bool QPageLayout::setLeftMargin(qreal leftMargin, OutOfBoundsPolicy outOfBoundsPolicy) |
610 | { |
611 | if (d->m_mode == StandardMode && outOfBoundsPolicy == OutOfBoundsPolicy::Clamp) |
612 | leftMargin = qBound(min: d->m_minMargins.left(), val: leftMargin, max: d->m_maxMargins.left()); |
613 | |
614 | if (qFuzzyCompare(p1: leftMargin, p2: d->m_margins.left())) |
615 | return true; |
616 | |
617 | if (d->m_mode == FullPageMode |
618 | || (leftMargin >= d->m_minMargins.left() && leftMargin <= d->m_maxMargins.left())) { |
619 | d.detach(); |
620 | d->m_margins.setLeft(leftMargin); |
621 | return true; |
622 | } |
623 | |
624 | return false; |
625 | } |
626 | |
627 | /*! |
628 | Sets the right page margin of the page layout to \a rightMargin. |
629 | Returns true if the margin was successfully set. |
630 | |
631 | The units used are those currently defined for the layout. To use different |
632 | units call setUnits() first. |
633 | |
634 | Since Qt 6.8, the optional \a outOfBoundsPolicy can be used to specify how |
635 | margins that are out of bounds are handled. |
636 | |
637 | \sa setMargins(), margins() |
638 | */ |
639 | |
640 | bool QPageLayout::setRightMargin(qreal rightMargin, OutOfBoundsPolicy outOfBoundsPolicy) |
641 | { |
642 | if (d->m_mode == StandardMode && outOfBoundsPolicy == OutOfBoundsPolicy::Clamp) |
643 | rightMargin = qBound(min: d->m_minMargins.right(), val: rightMargin, max: d->m_maxMargins.right()); |
644 | |
645 | if (qFuzzyCompare(p1: rightMargin, p2: d->m_margins.right())) |
646 | return true; |
647 | |
648 | if (d->m_mode == FullPageMode |
649 | || (rightMargin >= d->m_minMargins.right() && rightMargin <= d->m_maxMargins.right())) { |
650 | d.detach(); |
651 | d->m_margins.setRight(rightMargin); |
652 | return true; |
653 | } |
654 | |
655 | return false; |
656 | } |
657 | |
658 | /*! |
659 | Sets the top page margin of the page layout to \a topMargin. |
660 | Returns true if the margin was successfully set. |
661 | |
662 | The units used are those currently defined for the layout. To use different |
663 | units call setUnits() first. |
664 | |
665 | Since Qt 6.8, the optional \a outOfBoundsPolicy can be used to specify how |
666 | margins that are out of bounds are handled. |
667 | |
668 | \sa setMargins(), margins() |
669 | */ |
670 | |
671 | bool QPageLayout::setTopMargin(qreal topMargin, OutOfBoundsPolicy outOfBoundsPolicy) |
672 | { |
673 | if (d->m_mode == StandardMode && outOfBoundsPolicy == OutOfBoundsPolicy::Clamp) |
674 | topMargin = qBound(min: d->m_minMargins.top(), val: topMargin, max: d->m_maxMargins.top()); |
675 | |
676 | if (qFuzzyCompare(p1: topMargin, p2: d->m_margins.top())) |
677 | return true; |
678 | |
679 | if (d->m_mode == FullPageMode |
680 | || (topMargin >= d->m_minMargins.top() && topMargin <= d->m_maxMargins.top())) { |
681 | d.detach(); |
682 | d->m_margins.setTop(topMargin); |
683 | return true; |
684 | } |
685 | |
686 | return false; |
687 | } |
688 | |
689 | /*! |
690 | Sets the bottom page margin of the page layout to \a bottomMargin. |
691 | Returns true if the margin was successfully set. |
692 | |
693 | The units used are those currently defined for the layout. To use different |
694 | units call setUnits() first. |
695 | |
696 | Since Qt 6.8, the optional \a outOfBoundsPolicy can be used to specify how |
697 | margins that are out of bounds are handled. |
698 | |
699 | \sa setMargins(), margins() |
700 | */ |
701 | |
702 | bool QPageLayout::setBottomMargin(qreal bottomMargin, OutOfBoundsPolicy outOfBoundsPolicy) |
703 | { |
704 | if (d->m_mode == StandardMode && outOfBoundsPolicy == OutOfBoundsPolicy::Clamp) |
705 | bottomMargin = qBound(min: d->m_minMargins.bottom(), val: bottomMargin, max: d->m_maxMargins.bottom()); |
706 | |
707 | if (qFuzzyCompare(p1: bottomMargin, p2: d->m_margins.bottom())) |
708 | return true; |
709 | |
710 | if (d->m_mode == FullPageMode |
711 | || (bottomMargin >= d->m_minMargins.bottom() && bottomMargin <= d->m_maxMargins.bottom())) { |
712 | d.detach(); |
713 | d->m_margins.setBottom(bottomMargin); |
714 | return true; |
715 | } |
716 | |
717 | return false; |
718 | } |
719 | |
720 | /*! |
721 | Returns the margins of the page layout using the currently set units. |
722 | |
723 | \sa setMargins(), units() |
724 | */ |
725 | |
726 | QMarginsF QPageLayout::margins() const |
727 | { |
728 | return d->m_margins; |
729 | } |
730 | |
731 | /*! |
732 | Returns the margins of the page layout using the requested \a units. |
733 | |
734 | \sa setMargins(), margins() |
735 | */ |
736 | |
737 | QMarginsF QPageLayout::margins(Unit units) const |
738 | { |
739 | return d->margins(units); |
740 | } |
741 | |
742 | /*! |
743 | Returns the margins of the page layout in Postscript Points (1/72 of an inch). |
744 | |
745 | \sa setMargins(), margins() |
746 | */ |
747 | |
748 | QMargins QPageLayout::marginsPoints() const |
749 | { |
750 | return d->marginsPoints().toMargins(); |
751 | } |
752 | |
753 | /*! |
754 | Returns the margins of the page layout in device pixels for the given \a resolution. |
755 | |
756 | \sa setMargins() |
757 | */ |
758 | |
759 | QMargins QPageLayout::marginsPixels(int resolution) const |
760 | { |
761 | return d->marginsPixels(resolution); |
762 | } |
763 | |
764 | /*! |
765 | Sets the minimum page margins of the page layout to \a minMargins. |
766 | |
767 | It is not recommended to override the default values set for a page size |
768 | as this may be the minimum printable area for a physical print device. |
769 | |
770 | If the StandardMode mode is set then the existing margins will be clamped |
771 | to the new \a minMargins and the maximum allowed by the page size. If the |
772 | FullPageMode is set then the existing margins will be unchanged. |
773 | |
774 | \sa minimumMargins(), setMargins() |
775 | */ |
776 | |
777 | void QPageLayout::setMinimumMargins(const QMarginsF &minMargins) |
778 | { |
779 | d.detach(); |
780 | d->setDefaultMargins(minMargins); |
781 | } |
782 | |
783 | /*! |
784 | Returns the minimum margins of the page layout. |
785 | |
786 | \sa setMinimumMargins(), maximumMargins() |
787 | */ |
788 | |
789 | QMarginsF QPageLayout::minimumMargins() const |
790 | { |
791 | return d->m_minMargins; |
792 | } |
793 | |
794 | /*! |
795 | Returns the maximum margins that would be applied if the page layout was |
796 | in StandardMode. |
797 | |
798 | The maximum margins allowed are calculated as the full size of the page |
799 | minus the minimum margins set. For example, if the page width is 100 points |
800 | and the minimum right margin is 10 points, then the maximum left margin |
801 | will be 90 points. |
802 | |
803 | \sa setMinimumMargins(), minimumMargins() |
804 | */ |
805 | |
806 | QMarginsF QPageLayout::maximumMargins() const |
807 | { |
808 | return d->m_maxMargins; |
809 | } |
810 | |
811 | /*! |
812 | Returns the full page rectangle in the current layout units. |
813 | |
814 | The page rectangle takes into account the page size and page orientation, |
815 | but not the page margins. |
816 | |
817 | \sa paintRect(), units() |
818 | */ |
819 | |
820 | QRectF QPageLayout::fullRect() const |
821 | { |
822 | return isValid() ? d->fullRect() : QRect(); |
823 | } |
824 | |
825 | /*! |
826 | Returns the full page rectangle in the required \a units. |
827 | |
828 | The page rectangle takes into account the page size and page orientation, |
829 | but not the page margins. |
830 | |
831 | \sa paintRect() |
832 | */ |
833 | |
834 | QRectF QPageLayout::fullRect(Unit units) const |
835 | { |
836 | return isValid() ? d->fullRect(units) : QRect(); |
837 | } |
838 | |
839 | /*! |
840 | Returns the full page rectangle in Postscript Points (1/72 of an inch). |
841 | |
842 | The page rectangle takes into account the page size and page orientation, |
843 | but not the page margins. |
844 | |
845 | \sa paintRect() |
846 | */ |
847 | |
848 | QRect QPageLayout::fullRectPoints() const |
849 | { |
850 | return isValid() ? d->fullRectPoints() : QRect(); |
851 | } |
852 | |
853 | /*! |
854 | Returns the full page rectangle in device pixels for the given \a resolution. |
855 | |
856 | The page rectangle takes into account the page size and page orientation, |
857 | but not the page margins. |
858 | |
859 | \sa paintRect() |
860 | */ |
861 | |
862 | QRect QPageLayout::fullRectPixels(int resolution) const |
863 | { |
864 | return isValid() ? d->fullRectPixels(resolution) : QRect(); |
865 | } |
866 | |
867 | /*! |
868 | Returns the page rectangle in the current layout units. |
869 | |
870 | The paintable rectangle takes into account the page size, orientation |
871 | and margins. |
872 | |
873 | If the FullPageMode mode is set then the fullRect() is returned and |
874 | the margins must be manually managed. |
875 | */ |
876 | |
877 | QRectF QPageLayout::paintRect() const |
878 | { |
879 | return isValid() ? d->paintRect() : QRectF(); |
880 | } |
881 | |
882 | /*! |
883 | Returns the page rectangle in the required \a units. |
884 | |
885 | The paintable rectangle takes into account the page size, orientation |
886 | and margins. |
887 | |
888 | If the FullPageMode mode is set then the fullRect() is returned and |
889 | the margins must be manually managed. |
890 | */ |
891 | |
892 | QRectF QPageLayout::paintRect(Unit units) const |
893 | { |
894 | if (!isValid()) |
895 | return QRectF(); |
896 | if (units == d->m_units) |
897 | return d->paintRect(); |
898 | return d->m_mode == FullPageMode ? d->fullRect(units) |
899 | : d->fullRect(units) - d->margins(units); |
900 | } |
901 | |
902 | /*! |
903 | Returns the paintable rectangle in rounded Postscript Points (1/72 of an inch). |
904 | |
905 | The paintable rectangle takes into account the page size, orientation |
906 | and margins. |
907 | |
908 | If the FullPageMode mode is set then the fullRect() is returned and |
909 | the margins must be manually managed. |
910 | */ |
911 | |
912 | QRect QPageLayout::paintRectPoints() const |
913 | { |
914 | if (!isValid()) |
915 | return QRect(); |
916 | return d->m_mode == FullPageMode ? d->fullRectPoints() |
917 | : d->fullRectPoints() - d->marginsPoints().toMargins(); |
918 | } |
919 | |
920 | /*! |
921 | Returns the paintable rectangle in rounded device pixels for the given \a resolution. |
922 | |
923 | The paintable rectangle takes into account the page size, orientation |
924 | and margins. |
925 | |
926 | If the FullPageMode mode is set then the fullRect() is returned and |
927 | the margins must be manually managed. |
928 | */ |
929 | |
930 | QRect QPageLayout::paintRectPixels(int resolution) const |
931 | { |
932 | if (!isValid()) |
933 | return QRect(); |
934 | return d->m_mode == FullPageMode ? d->fullRectPixels(resolution) |
935 | : d->fullRectPixels(resolution) - d->marginsPixels(resolution); |
936 | } |
937 | |
938 | #ifndef QT_NO_DEBUG_STREAM |
939 | QDebug operator<<(QDebug dbg, const QPageLayout &layout) |
940 | { |
941 | QDebugStateSaver saver(dbg); |
942 | dbg.nospace(); |
943 | dbg.noquote(); |
944 | dbg << "QPageLayout("; |
945 | if (layout.isValid()) { |
946 | const QMarginsF margins = layout.margins(); |
947 | dbg << '"' << layout.pageSize().name() << "\", " |
948 | << (layout.orientation() == QPageLayout::Portrait ? "Portrait": "Landscape") |
949 | << ", l:"<< margins.left() << " r:"<< margins.right() << " t:" |
950 | << margins.top() << " b:"<< margins.bottom() << ' '; |
951 | switch (layout.units()) { |
952 | case QPageLayout::Millimeter: |
953 | dbg << "mm"; |
954 | break; |
955 | case QPageLayout::Point: |
956 | dbg << "pt"; |
957 | break; |
958 | case QPageLayout::Inch: |
959 | dbg << "in"; |
960 | break; |
961 | case QPageLayout::Pica: |
962 | dbg << "pc"; |
963 | break; |
964 | case QPageLayout::Didot: |
965 | dbg << "DD"; |
966 | break; |
967 | case QPageLayout::Cicero: |
968 | dbg << "CC"; |
969 | break; |
970 | } |
971 | } |
972 | dbg << ')'; |
973 | return dbg; |
974 | } |
975 | #endif |
976 | |
977 | QT_END_NAMESPACE |
978 |
Definitions
- qt_pointMultiplier
- qt_convertMargins
- QPageLayoutPrivate
- QPageLayoutPrivate
- ~QPageLayoutPrivate
- operator==
- isEquivalentTo
- isValid
- clampMargins
- margins
- marginsPoints
- marginsPixels
- setDefaultMargins
- fullSizeUnits
- fullRect
- fullRect
- fullRectPoints
- fullRectPixels
- paintRect
- QPageLayout
- QPageLayout
- QPageLayout
- ~QPageLayout
- operator=
- equals
- isEquivalentTo
- isValid
- setMode
- mode
- setPageSize
- pageSize
- setOrientation
- orientation
- setUnits
- units
- setMargins
- setLeftMargin
- setRightMargin
- setTopMargin
- setBottomMargin
- margins
- margins
- marginsPoints
- marginsPixels
- setMinimumMargins
- minimumMargins
- maximumMargins
- fullRect
- fullRect
- fullRectPoints
- fullRectPixels
- paintRect
- paintRect
- paintRectPoints
- paintRectPixels
Learn to use CMake with our Intro Training
Find out more