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