qprintpreviewwidget.cpp

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

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9