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