1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "qprintpreviewwidget.h" |
5 | #include "private/qwidget_p.h" |
6 | #include <private/qprinter_p.h> |
7 | |
8 | #include <QtCore/qmath.h> |
9 | #include <QtWidgets/qboxlayout.h> |
10 | #include <QtWidgets/qgraphicsitem.h> |
11 | #include <QtWidgets/qgraphicsview.h> |
12 | #include <QtWidgets/qscrollbar.h> |
13 | #include <QtWidgets/qstyleoption.h> |
14 | |
15 | QT_BEGIN_NAMESPACE |
16 | |
17 | namespace QtPrivate { |
18 | class PageItem : public QGraphicsItem |
19 | { |
20 | public: |
21 | PageItem(int _pageNum, const QPicture* _pagePicture, QSize _paperSize, QRect ) |
22 | : pageNum(_pageNum), pagePicture(_pagePicture), |
23 | paperSize(_paperSize), pageRect(_pageRect) |
24 | { |
25 | qreal border = qMax(a: paperSize.height(), b: paperSize.width()) / 25; |
26 | brect = QRectF(QPointF(-border, -border), |
27 | QSizeF(paperSize)+QSizeF(2*border, 2*border)); |
28 | setCacheMode(mode: DeviceCoordinateCache); |
29 | } |
30 | |
31 | QRectF boundingRect() const override |
32 | { return brect; } |
33 | |
34 | inline int pageNumber() const |
35 | { return pageNum; } |
36 | |
37 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget) override; |
38 | |
39 | private: |
40 | int pageNum; |
41 | const QPicture* pagePicture; |
42 | QSize paperSize; |
43 | QRect ; |
44 | QRectF brect; |
45 | }; |
46 | |
47 | void PageItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
48 | { |
49 | Q_UNUSED(widget); |
50 | |
51 | #if 0 |
52 | // Draw item bounding rect, for debugging |
53 | painter->save(); |
54 | painter->setPen(QPen(Qt::red, 0)); |
55 | painter->setBrush(Qt::NoBrush); |
56 | painter->drawRect(QRectF(-border()+1.0, -border()+1.0, boundingRect().width()-2, boundingRect().height()-2)); |
57 | painter->restore(); |
58 | #endif |
59 | |
60 | QRectF paperRect(0,0, paperSize.width(), paperSize.height()); |
61 | |
62 | // Draw shadow |
63 | painter->setClipRect(option->exposedRect); |
64 | qreal shWidth = paperRect.width()/100; |
65 | QRectF rshadow(paperRect.topRight() + QPointF(0, shWidth), |
66 | paperRect.bottomRight() + QPointF(shWidth, 0)); |
67 | QLinearGradient rgrad(rshadow.topLeft(), rshadow.topRight()); |
68 | rgrad.setColorAt(pos: 0.0, color: QColor(0,0,0,255)); |
69 | rgrad.setColorAt(pos: 1.0, color: QColor(0,0,0,0)); |
70 | painter->fillRect(rshadow, QBrush(rgrad)); |
71 | QRectF bshadow(paperRect.bottomLeft() + QPointF(shWidth, 0), |
72 | paperRect.bottomRight() + QPointF(0, shWidth)); |
73 | QLinearGradient bgrad(bshadow.topLeft(), bshadow.bottomLeft()); |
74 | bgrad.setColorAt(pos: 0.0, color: QColor(0,0,0,255)); |
75 | bgrad.setColorAt(pos: 1.0, color: QColor(0,0,0,0)); |
76 | painter->fillRect(bshadow, QBrush(bgrad)); |
77 | QRectF cshadow(paperRect.bottomRight(), |
78 | paperRect.bottomRight() + QPointF(shWidth, shWidth)); |
79 | QRadialGradient cgrad(cshadow.topLeft(), shWidth, cshadow.topLeft()); |
80 | cgrad.setColorAt(pos: 0.0, color: QColor(0,0,0,255)); |
81 | cgrad.setColorAt(pos: 1.0, color: QColor(0,0,0,0)); |
82 | painter->fillRect(cshadow, QBrush(cgrad)); |
83 | |
84 | painter->setClipRect(paperRect & option->exposedRect); |
85 | painter->fillRect(r: paperRect, c: Qt::white); |
86 | if (!pagePicture) |
87 | return; |
88 | painter->drawPicture(pt: pageRect.topLeft(), p: *pagePicture); |
89 | |
90 | // Effect: make anything drawn in the margins look washed out. |
91 | QPainterPath path; |
92 | path.addRect(rect: paperRect); |
93 | path.addRect(rect: pageRect); |
94 | painter->setPen(QPen(Qt::NoPen)); |
95 | painter->setBrush(QColor(255, 255, 255, 180)); |
96 | painter->drawPath(path); |
97 | |
98 | #if 0 |
99 | // Draw frame around paper. |
100 | painter->setPen(QPen(Qt::black, 0)); |
101 | painter->setBrush(Qt::NoBrush); |
102 | painter->drawRect(paperRect); |
103 | #endif |
104 | |
105 | // todo: drawtext "Page N" below paper |
106 | } |
107 | |
108 | class GraphicsView : public QGraphicsView |
109 | { |
110 | Q_OBJECT |
111 | public: |
112 | GraphicsView(QWidget* parent = nullptr) |
113 | : QGraphicsView(parent) |
114 | { |
115 | #ifdef Q_OS_MAC |
116 | setFrameStyle(QFrame::NoFrame); |
117 | #endif |
118 | } |
119 | signals: |
120 | void resized(); |
121 | |
122 | protected: |
123 | void resizeEvent(QResizeEvent* e) override |
124 | { |
125 | { |
126 | const QSignalBlocker blocker(verticalScrollBar()); // Don't change page, QTBUG-14517 |
127 | QGraphicsView::resizeEvent(event: e); |
128 | } |
129 | emit resized(); |
130 | } |
131 | |
132 | void showEvent(QShowEvent* e) override |
133 | { |
134 | QGraphicsView::showEvent(event: e); |
135 | emit resized(); |
136 | } |
137 | }; |
138 | |
139 | } // namespace QtPrivate |
140 | |
141 | using GraphicsView = QtPrivate::GraphicsView; |
142 | using PageItem = QtPrivate::PageItem; |
143 | |
144 | class QPrintPreviewWidgetPrivate : public QWidgetPrivate |
145 | { |
146 | Q_DECLARE_PUBLIC(QPrintPreviewWidget) |
147 | public: |
148 | QPrintPreviewWidgetPrivate() |
149 | : scene(nullptr), curPage(1), |
150 | viewMode(QPrintPreviewWidget::SinglePageView), |
151 | zoomMode(QPrintPreviewWidget::FitInView), |
152 | zoomFactor(1), initialized(false), fitting(true) |
153 | {} |
154 | |
155 | // private slots |
156 | void _q_fit(bool doFitting = false); |
157 | void _q_updateCurrentPage(); |
158 | |
159 | void init(); |
160 | void populateScene(); |
161 | void layoutPages(); |
162 | void generatePreview(); |
163 | void setCurrentPage(int pageNumber); |
164 | void zoom(qreal zoom); |
165 | void setZoomFactor(qreal zoomFactor); |
166 | int calcCurrentPage(); |
167 | |
168 | GraphicsView *graphicsView; |
169 | QGraphicsScene *scene; |
170 | |
171 | int curPage; |
172 | QList<const QPicture *> pictures; |
173 | QList<QGraphicsItem *> pages; |
174 | |
175 | QPrintPreviewWidget::ViewMode viewMode; |
176 | QPrintPreviewWidget::ZoomMode zoomMode; |
177 | qreal zoomFactor; |
178 | bool ownPrinter; |
179 | QPrinter* printer; |
180 | bool initialized; |
181 | bool fitting; |
182 | }; |
183 | |
184 | void QPrintPreviewWidgetPrivate::_q_fit(bool doFitting) |
185 | { |
186 | Q_Q(QPrintPreviewWidget); |
187 | |
188 | if (curPage < 1 || curPage > pages.size()) |
189 | return; |
190 | |
191 | if (!doFitting && !fitting) |
192 | return; |
193 | |
194 | if (doFitting && fitting) { |
195 | QRect viewRect = graphicsView->viewport()->rect(); |
196 | if (zoomMode == QPrintPreviewWidget::FitInView) { |
197 | const QList<QGraphicsItem*> containedItems = graphicsView->items(rect: viewRect, mode: Qt::ContainsItemBoundingRect); |
198 | for (QGraphicsItem* item : containedItems) { |
199 | PageItem* pg = static_cast<PageItem*>(item); |
200 | if (pg->pageNumber() == curPage) |
201 | return; |
202 | } |
203 | } |
204 | |
205 | int newPage = calcCurrentPage(); |
206 | if (newPage != curPage) |
207 | curPage = newPage; |
208 | } |
209 | |
210 | QRectF target = pages.at(i: curPage-1)->sceneBoundingRect(); |
211 | if (viewMode == QPrintPreviewWidget::FacingPagesView) { |
212 | // fit two pages |
213 | if (curPage % 2) |
214 | target.setLeft(target.left() - target.width()); |
215 | else |
216 | target.setRight(target.right() + target.width()); |
217 | } else if (viewMode == QPrintPreviewWidget::AllPagesView) { |
218 | target = scene->itemsBoundingRect(); |
219 | } |
220 | |
221 | if (zoomMode == QPrintPreviewWidget::FitToWidth) { |
222 | QTransform t; |
223 | qreal scale = graphicsView->viewport()->width() / target.width(); |
224 | t.scale(sx: scale, sy: scale); |
225 | graphicsView->setTransform(matrix: t); |
226 | if (doFitting && fitting) { |
227 | QRectF viewSceneRect = graphicsView->viewportTransform().mapRect(graphicsView->viewport()->rect()); |
228 | viewSceneRect.moveTop(pos: target.top()); |
229 | graphicsView->ensureVisible(rect: viewSceneRect); // Nah... |
230 | } |
231 | } else { |
232 | graphicsView->fitInView(rect: target, aspectRadioMode: Qt::KeepAspectRatio); |
233 | if (zoomMode == QPrintPreviewWidget::FitInView) { |
234 | const int step = qRound(d: graphicsView->transform().mapRect(target).height()); |
235 | graphicsView->verticalScrollBar()->setSingleStep(step); |
236 | graphicsView->verticalScrollBar()->setPageStep(step); |
237 | } |
238 | } |
239 | |
240 | zoomFactor = graphicsView->transform().m11() * (float(printer->logicalDpiY()) / q->logicalDpiY()); |
241 | emit q->previewChanged(); |
242 | } |
243 | |
244 | void QPrintPreviewWidgetPrivate::_q_updateCurrentPage() |
245 | { |
246 | Q_Q(QPrintPreviewWidget); |
247 | |
248 | if (viewMode == QPrintPreviewWidget::AllPagesView) |
249 | return; |
250 | |
251 | int newPage = calcCurrentPage(); |
252 | if (newPage != curPage) { |
253 | curPage = newPage; |
254 | emit q->previewChanged(); |
255 | } |
256 | } |
257 | |
258 | int QPrintPreviewWidgetPrivate::calcCurrentPage() |
259 | { |
260 | int maxArea = 0; |
261 | int newPage = curPage; |
262 | QRect viewRect = graphicsView->viewport()->rect(); |
263 | const QList<QGraphicsItem*> items = graphicsView->items(rect: viewRect); |
264 | for (auto *item : items) { |
265 | PageItem* pg = static_cast<PageItem*>(item); |
266 | QRect overlap = graphicsView->mapFromScene(rect: pg->sceneBoundingRect()).boundingRect() & viewRect; |
267 | int area = overlap.width() * overlap.height(); |
268 | if (area > maxArea) { |
269 | maxArea = area; |
270 | newPage = pg->pageNumber(); |
271 | } else if (area == maxArea && pg->pageNumber() < newPage) { |
272 | newPage = pg->pageNumber(); |
273 | } |
274 | } |
275 | return newPage; |
276 | } |
277 | |
278 | void QPrintPreviewWidgetPrivate::init() |
279 | { |
280 | Q_Q(QPrintPreviewWidget); |
281 | |
282 | graphicsView = new GraphicsView; |
283 | graphicsView->setInteractive(false); |
284 | graphicsView->setDragMode(QGraphicsView::ScrollHandDrag); |
285 | graphicsView->setViewportUpdateMode(QGraphicsView::SmartViewportUpdate); |
286 | QObject::connect(sender: graphicsView->verticalScrollBar(), SIGNAL(valueChanged(int)), |
287 | receiver: q, SLOT(_q_updateCurrentPage())); |
288 | QObject::connect(sender: graphicsView, SIGNAL(resized()), receiver: q, SLOT(_q_fit())); |
289 | |
290 | scene = new QGraphicsScene(graphicsView); |
291 | scene->setBackgroundBrush(Qt::gray); |
292 | graphicsView->setScene(scene); |
293 | |
294 | QVBoxLayout *layout = new QVBoxLayout(q); |
295 | layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0); |
296 | layout->addWidget(graphicsView); |
297 | } |
298 | |
299 | void QPrintPreviewWidgetPrivate::populateScene() |
300 | { |
301 | // remove old pages |
302 | for (auto *page : std::as_const(t&: pages)) |
303 | scene->removeItem(item: page); |
304 | qDeleteAll(c: pages); |
305 | pages.clear(); |
306 | |
307 | QSize paperSize = printer->pageLayout().fullRectPixels(resolution: printer->resolution()).size(); |
308 | QRect = printer->pageLayout().paintRectPixels(resolution: printer->resolution()); |
309 | |
310 | int page = 1; |
311 | for (auto *picture : std::as_const(t&: pictures)) { |
312 | PageItem* item = new PageItem(page++, picture, paperSize, pageRect); |
313 | scene->addItem(item); |
314 | pages.append(t: item); |
315 | } |
316 | } |
317 | |
318 | void QPrintPreviewWidgetPrivate::layoutPages() |
319 | { |
320 | int numPages = pages.size(); |
321 | if (numPages < 1) |
322 | return; |
323 | |
324 | int numPagePlaces = numPages; |
325 | int cols = 1; // singleMode and default |
326 | if (viewMode == QPrintPreviewWidget::AllPagesView) { |
327 | if (printer->pageLayout().orientation() == QPageLayout::Portrait) |
328 | cols = qCeil(v: qSqrt(v: (float) numPages)); |
329 | else |
330 | cols = qFloor(v: qSqrt(v: (float) numPages)); |
331 | cols += cols % 2; // Nicer with an even number of cols |
332 | } |
333 | else if (viewMode == QPrintPreviewWidget::FacingPagesView) { |
334 | cols = 2; |
335 | numPagePlaces += 1; |
336 | } |
337 | int rows = qCeil(v: qreal(numPagePlaces) / cols); |
338 | |
339 | qreal itemWidth = pages.at(i: 0)->boundingRect().width(); |
340 | qreal itemHeight = pages.at(i: 0)->boundingRect().height(); |
341 | int pageNum = 1; |
342 | for (int i = 0; i < rows && pageNum <= numPages; i++) { |
343 | for (int j = 0; j < cols && pageNum <= numPages; j++) { |
344 | if (!i && !j && viewMode == QPrintPreviewWidget::FacingPagesView) { |
345 | // Front page doesn't have a facing page |
346 | continue; |
347 | } else { |
348 | pages.at(i: pageNum-1)->setPos(QPointF(j*itemWidth, i*itemHeight)); |
349 | pageNum++; |
350 | } |
351 | } |
352 | } |
353 | scene->setSceneRect(scene->itemsBoundingRect()); |
354 | } |
355 | |
356 | void QPrintPreviewWidgetPrivate::generatePreview() |
357 | { |
358 | //### If QPrinter::setPreviewMode() becomes public, handle the |
359 | //### case that we have been constructed with a printer that |
360 | //### _already_ has been preview-painted to, so we should |
361 | //### initially just show the pages it already contains, and not |
362 | //### emit paintRequested() until the user changes some parameter |
363 | |
364 | Q_Q(QPrintPreviewWidget); |
365 | printer->d_func()->setPreviewMode(true); |
366 | emit q->paintRequested(printer); |
367 | printer->d_func()->setPreviewMode(false); |
368 | pictures = printer->d_func()->previewPages(); |
369 | populateScene(); // i.e. setPreviewPrintedPictures() e.l. |
370 | layoutPages(); |
371 | curPage = pages.size() > 0 ? qBound(min: 1, val: curPage, max: pages.size()) : 1; |
372 | if (fitting) |
373 | _q_fit(); |
374 | emit q->previewChanged(); |
375 | } |
376 | |
377 | void QPrintPreviewWidgetPrivate::setCurrentPage(int pageNumber) |
378 | { |
379 | if (pageNumber < 1 || pageNumber > pages.size()) |
380 | return; |
381 | |
382 | int lastPage = curPage; |
383 | curPage = pageNumber; |
384 | |
385 | if (lastPage != curPage && lastPage > 0 && lastPage <= pages.size()) { |
386 | if (zoomMode != QPrintPreviewWidget::FitInView) { |
387 | QScrollBar *hsc = graphicsView->horizontalScrollBar(); |
388 | QScrollBar *vsc = graphicsView->verticalScrollBar(); |
389 | QPointF pt = graphicsView->transform().map(p: pages.at(i: curPage-1)->pos()); |
390 | vsc->setValue(int(pt.y()) - 10); |
391 | hsc->setValue(int(pt.x()) - 10); |
392 | } else { |
393 | graphicsView->centerOn(item: pages.at(i: curPage-1)); |
394 | } |
395 | } |
396 | } |
397 | |
398 | void QPrintPreviewWidgetPrivate::zoom(qreal zoom) |
399 | { |
400 | zoomFactor *= zoom; |
401 | graphicsView->scale(sx: zoom, sy: zoom); |
402 | } |
403 | |
404 | void QPrintPreviewWidgetPrivate::setZoomFactor(qreal _zoomFactor) |
405 | { |
406 | Q_Q(QPrintPreviewWidget); |
407 | zoomFactor = _zoomFactor; |
408 | graphicsView->resetTransform(); |
409 | int dpi_y = q->logicalDpiY(); |
410 | int printer_dpi_y = printer->logicalDpiY(); |
411 | graphicsView->scale(sx: zoomFactor*(dpi_y/float(printer_dpi_y)), |
412 | sy: zoomFactor*(dpi_y/float(printer_dpi_y))); |
413 | } |
414 | |
415 | /////////////////////////////////////// |
416 | |
417 | /*! |
418 | \class QPrintPreviewWidget |
419 | \since 4.4 |
420 | |
421 | \brief The QPrintPreviewWidget class provides a widget for |
422 | previewing page layouts for printer output. |
423 | |
424 | \ingroup printing |
425 | \inmodule QtPrintSupport |
426 | |
427 | QPrintPreviewDialog uses a QPrintPreviewWidget internally, and the |
428 | purpose of QPrintPreviewWidget is to make it possible to embed the |
429 | preview into other widgets. It also makes it possible to build a different |
430 | user interface around it than the default one provided with QPrintPreviewDialog. |
431 | |
432 | Using QPrintPreviewWidget is straightforward: |
433 | |
434 | \list 1 |
435 | \li Create the QPrintPreviewWidget |
436 | |
437 | Construct the QPrintPreviewWidget either by passing in an |
438 | existing QPrinter object, or have QPrintPreviewWidget create a |
439 | default constructed QPrinter object for you. |
440 | |
441 | \li Connect the paintRequested() signal to a slot. |
442 | |
443 | When the widget needs to generate a set of preview pages, a |
444 | paintRequested() signal will be emitted from the widget. Connect a |
445 | slot to this signal, and draw onto the QPrinter passed in as a |
446 | signal parameter. Call QPrinter::newPage(), to start a new |
447 | page in the preview. |
448 | |
449 | \endlist |
450 | |
451 | \sa QPrinter, QPrintDialog, QPageSetupDialog, QPrintPreviewDialog |
452 | */ |
453 | |
454 | |
455 | /*! |
456 | \enum QPrintPreviewWidget::ViewMode |
457 | |
458 | This enum is used to describe the view mode of the preview widget. |
459 | |
460 | \value SinglePageView A mode where single pages in the preview |
461 | is viewed. |
462 | |
463 | \value FacingPagesView A mode where the facing pages in the preview |
464 | is viewed. |
465 | |
466 | \value AllPagesView A view mode where all the pages in the preview |
467 | is viewed. |
468 | */ |
469 | |
470 | /*! |
471 | \enum QPrintPreviewWidget::ZoomMode |
472 | |
473 | This enum is used to describe zoom mode of the preview widget. |
474 | |
475 | \value CustomZoom The zoom is set to a custom zoom value. |
476 | |
477 | \value FitToWidth This mode fits the current page to the width of the view. |
478 | |
479 | \value FitInView This mode fits the current page inside the view. |
480 | |
481 | */ |
482 | |
483 | /*! |
484 | Constructs a QPrintPreviewWidget based on \a printer and with \a |
485 | parent as the parent widget. The widget flags \a flags are passed on |
486 | to the QWidget constructor. |
487 | |
488 | \sa QWidget::setWindowFlags() |
489 | */ |
490 | QPrintPreviewWidget::QPrintPreviewWidget(QPrinter *printer, QWidget *parent, Qt::WindowFlags flags) |
491 | : QWidget(*new QPrintPreviewWidgetPrivate, parent, flags) |
492 | { |
493 | Q_D(QPrintPreviewWidget); |
494 | d->printer = printer; |
495 | d->ownPrinter = false; |
496 | d->init(); |
497 | } |
498 | |
499 | /*! |
500 | \overload |
501 | |
502 | This will cause QPrintPreviewWidget to create an internal, default |
503 | constructed QPrinter object, which will be used to generate the |
504 | preview. |
505 | */ |
506 | QPrintPreviewWidget::QPrintPreviewWidget(QWidget *parent, Qt::WindowFlags flags) |
507 | : QWidget(*new QPrintPreviewWidgetPrivate, parent, flags) |
508 | { |
509 | Q_D(QPrintPreviewWidget); |
510 | d->printer = new QPrinter; |
511 | d->ownPrinter = true; |
512 | d->init(); |
513 | } |
514 | |
515 | |
516 | /*! |
517 | Destroys the QPrintPreviewWidget. |
518 | */ |
519 | QPrintPreviewWidget::~QPrintPreviewWidget() |
520 | { |
521 | Q_D(QPrintPreviewWidget); |
522 | if (d->ownPrinter) |
523 | delete d->printer; |
524 | } |
525 | |
526 | /*! |
527 | Returns the current view mode. The default view mode is SinglePageView. |
528 | */ |
529 | QPrintPreviewWidget::ViewMode QPrintPreviewWidget::viewMode() const |
530 | { |
531 | Q_D(const QPrintPreviewWidget); |
532 | return d->viewMode; |
533 | } |
534 | |
535 | /*! |
536 | Sets the view mode to \a mode. The default view mode is |
537 | SinglePageView. |
538 | */ |
539 | void QPrintPreviewWidget::setViewMode(ViewMode mode) |
540 | { |
541 | Q_D(QPrintPreviewWidget); |
542 | d->viewMode = mode; |
543 | d->layoutPages(); |
544 | if (d->viewMode == AllPagesView) { |
545 | d->graphicsView->fitInView(rect: d->scene->itemsBoundingRect(), aspectRadioMode: Qt::KeepAspectRatio); |
546 | d->fitting = false; |
547 | d->zoomMode = QPrintPreviewWidget::CustomZoom; |
548 | d->zoomFactor = d->graphicsView->transform().m11() * (float(d->printer->logicalDpiY()) / logicalDpiY()); |
549 | emit previewChanged(); |
550 | } else { |
551 | d->fitting = true; |
552 | d->_q_fit(); |
553 | } |
554 | } |
555 | |
556 | /*! |
557 | Returns the current orientation of the preview. This value is |
558 | obtained from the QPrinter object associated with the preview. |
559 | */ |
560 | QPageLayout::Orientation QPrintPreviewWidget::orientation() const |
561 | { |
562 | Q_D(const QPrintPreviewWidget); |
563 | return d->printer->pageLayout().orientation(); |
564 | } |
565 | |
566 | /*! |
567 | Sets the current orientation to \a orientation. This value will be |
568 | set on the QPrinter object associated with the preview. |
569 | */ |
570 | void QPrintPreviewWidget::setOrientation(QPageLayout::Orientation orientation) |
571 | { |
572 | Q_D(QPrintPreviewWidget); |
573 | d->printer->setPageOrientation(orientation); |
574 | d->generatePreview(); |
575 | } |
576 | |
577 | /*! |
578 | Prints the preview to the printer associated with the preview. |
579 | */ |
580 | void QPrintPreviewWidget::print() |
581 | { |
582 | Q_D(QPrintPreviewWidget); |
583 | // ### make use of the generated pages |
584 | emit paintRequested(printer: d->printer); |
585 | } |
586 | |
587 | /*! |
588 | Zooms the current view in by \a factor. The default value for \a |
589 | factor is 1.1, which means the view will be scaled up by 10%. |
590 | */ |
591 | void QPrintPreviewWidget::zoomIn(qreal factor) |
592 | { |
593 | Q_D(QPrintPreviewWidget); |
594 | d->fitting = false; |
595 | d->zoomMode = QPrintPreviewWidget::CustomZoom; |
596 | d->zoom(zoom: factor); |
597 | } |
598 | |
599 | /*! |
600 | Zooms the current view out by \a factor. The default value for \a |
601 | factor is 1.1, which means the view will be scaled down by 10%. |
602 | */ |
603 | void QPrintPreviewWidget::zoomOut(qreal factor) |
604 | { |
605 | Q_D(QPrintPreviewWidget); |
606 | d->fitting = false; |
607 | d->zoomMode = QPrintPreviewWidget::CustomZoom; |
608 | d->zoom(zoom: 1/factor); |
609 | } |
610 | |
611 | /*! |
612 | Returns the zoom factor of the view. |
613 | */ |
614 | qreal QPrintPreviewWidget::zoomFactor() const |
615 | { |
616 | Q_D(const QPrintPreviewWidget); |
617 | return d->zoomFactor; |
618 | } |
619 | |
620 | /*! |
621 | Sets the zoom factor of the view to \a factor. For example, a |
622 | value of 1.0 indicates an unscaled view, which is approximately |
623 | the size the view will have on paper. A value of 0.5 will halve |
624 | the size of the view, while a value of 2.0 will double the size of |
625 | the view. |
626 | */ |
627 | void QPrintPreviewWidget::setZoomFactor(qreal factor) |
628 | { |
629 | Q_D(QPrintPreviewWidget); |
630 | d->fitting = false; |
631 | d->zoomMode = QPrintPreviewWidget::CustomZoom; |
632 | d->setZoomFactor(factor); |
633 | } |
634 | |
635 | /*! |
636 | \since 4.6 |
637 | Returns the number of pages in the preview. |
638 | */ |
639 | int QPrintPreviewWidget::pageCount() const |
640 | { |
641 | Q_D(const QPrintPreviewWidget); |
642 | return d->pages.size(); |
643 | } |
644 | |
645 | /*! |
646 | Returns the currently viewed page in the preview. |
647 | */ |
648 | int QPrintPreviewWidget::currentPage() const |
649 | { |
650 | Q_D(const QPrintPreviewWidget); |
651 | return d->curPage; |
652 | } |
653 | |
654 | /*! |
655 | Sets the current page in the preview. This will cause the view to |
656 | skip to the beginning of \a page. |
657 | */ |
658 | void QPrintPreviewWidget::setCurrentPage(int page) |
659 | { |
660 | Q_D(QPrintPreviewWidget); |
661 | d->setCurrentPage(page); |
662 | } |
663 | |
664 | /*! |
665 | This is a convenience function and is the same as calling \c |
666 | {setZoomMode(QPrintPreviewWidget::FitToWidth)}. |
667 | */ |
668 | void QPrintPreviewWidget::fitToWidth() |
669 | { |
670 | setZoomMode(FitToWidth); |
671 | } |
672 | |
673 | /*! |
674 | This is a convenience function and is the same as calling \c |
675 | {setZoomMode(QPrintPreviewWidget::FitInView)}. |
676 | */ |
677 | void QPrintPreviewWidget::fitInView() |
678 | { |
679 | setZoomMode(FitInView); |
680 | } |
681 | |
682 | /*! |
683 | Sets the zoom mode to \a zoomMode. The default zoom mode is FitInView. |
684 | |
685 | \sa zoomMode(), viewMode(), setViewMode() |
686 | */ |
687 | void QPrintPreviewWidget::setZoomMode(QPrintPreviewWidget::ZoomMode zoomMode) |
688 | { |
689 | Q_D(QPrintPreviewWidget); |
690 | d->zoomMode = zoomMode; |
691 | if (d->zoomMode == FitInView || d->zoomMode == FitToWidth) { |
692 | d->fitting = true; |
693 | d->_q_fit(doFitting: true); |
694 | } else { |
695 | d->fitting = false; |
696 | } |
697 | } |
698 | |
699 | /*! |
700 | Returns the current zoom mode. |
701 | |
702 | \sa setZoomMode(), viewMode(), setViewMode() |
703 | */ |
704 | QPrintPreviewWidget::ZoomMode QPrintPreviewWidget::zoomMode() const |
705 | { |
706 | Q_D(const QPrintPreviewWidget); |
707 | return d->zoomMode; |
708 | } |
709 | |
710 | /*! |
711 | This is a convenience function and is the same as calling \c |
712 | {setOrientation(QPageLayout::Landscape)}. |
713 | */ |
714 | void QPrintPreviewWidget::setLandscapeOrientation() |
715 | { |
716 | setOrientation(QPageLayout::Landscape); |
717 | } |
718 | |
719 | /*! |
720 | This is a convenience function and is the same as calling \c |
721 | {setOrientation(QPageLayout::Portrait)}. |
722 | */ |
723 | void QPrintPreviewWidget::setPortraitOrientation() |
724 | { |
725 | setOrientation(QPageLayout::Portrait); |
726 | } |
727 | |
728 | /*! |
729 | This is a convenience function and is the same as calling \c |
730 | {setViewMode(QPrintPreviewWidget::SinglePageView)}. |
731 | */ |
732 | void QPrintPreviewWidget::setSinglePageViewMode() |
733 | { |
734 | setViewMode(SinglePageView); |
735 | } |
736 | |
737 | /*! |
738 | This is a convenience function and is the same as calling \c |
739 | {setViewMode(QPrintPreviewWidget::FacingPagesView)}. |
740 | */ |
741 | void QPrintPreviewWidget::setFacingPagesViewMode() |
742 | { |
743 | setViewMode(FacingPagesView); |
744 | } |
745 | |
746 | /*! |
747 | This is a convenience function and is the same as calling \c |
748 | {setViewMode(QPrintPreviewWidget::AllPagesView)}. |
749 | */ |
750 | void QPrintPreviewWidget::setAllPagesViewMode() |
751 | { |
752 | setViewMode(AllPagesView); |
753 | } |
754 | |
755 | |
756 | /*! |
757 | This function updates the preview, which causes the |
758 | paintRequested() signal to be emitted. |
759 | */ |
760 | void QPrintPreviewWidget::updatePreview() |
761 | { |
762 | Q_D(QPrintPreviewWidget); |
763 | d->initialized = true; |
764 | d->generatePreview(); |
765 | d->graphicsView->updateGeometry(); |
766 | } |
767 | |
768 | /*! \reimp |
769 | */ |
770 | void QPrintPreviewWidget::setVisible(bool visible) |
771 | { |
772 | Q_D(QPrintPreviewWidget); |
773 | if (visible && !d->initialized) |
774 | updatePreview(); |
775 | QWidget::setVisible(visible); |
776 | } |
777 | |
778 | /*! |
779 | \fn void QPrintPreviewWidget::paintRequested(QPrinter *printer) |
780 | |
781 | This signal is emitted when the preview widget needs to generate a |
782 | set of preview pages. \a printer is the printer associated with |
783 | this preview widget. |
784 | */ |
785 | |
786 | /*! |
787 | \fn void QPrintPreviewWidget::previewChanged() |
788 | |
789 | This signal is emitted whenever the preview widget has changed |
790 | some internal state, such as the orientation. |
791 | */ |
792 | |
793 | |
794 | QT_END_NAMESPACE |
795 | |
796 | #include "moc_qprintpreviewwidget.cpp" |
797 | #include "qprintpreviewwidget.moc" |
798 | |