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