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

Provided by KDAB

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

source code of qtbase/src/printsupport/widgets/qprintpreviewwidget.cpp