[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the QtGui module of the Qt Toolkit.
|
---|
| 8 | **
|
---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
| 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
| 14 | ** a written agreement between you and Nokia.
|
---|
| 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 | **
|
---|
[561] | 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.
|
---|
[2] | 27 | **
|
---|
| 28 | ** GNU General Public License Usage
|
---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
| 32 | ** packaging of this file. Please review the following information to
|
---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
| 35 | **
|
---|
[561] | 36 | ** If you have questions regarding the use of this file, please contact
|
---|
| 37 | ** Nokia at [email protected].
|
---|
[2] | 38 | ** $QT_END_LICENSE$
|
---|
| 39 | **
|
---|
| 40 | ****************************************************************************/
|
---|
| 41 |
|
---|
| 42 | #include "qprintpreviewwidget.h"
|
---|
[561] | 43 | #include "private/qwidget_p.h"
|
---|
[2] | 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 |
|
---|
[561] | 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 |
|
---|
[2] | 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)
|
---|
| 154 | {}
|
---|
| 155 | signals:
|
---|
| 156 | void resized();
|
---|
| 157 |
|
---|
| 158 | protected:
|
---|
| 159 | void resizeEvent(QResizeEvent* e)
|
---|
| 160 | {
|
---|
| 161 | QGraphicsView::resizeEvent(e);
|
---|
| 162 | emit resized();
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | void showEvent(QShowEvent* e)
|
---|
| 166 | {
|
---|
| 167 | QGraphicsView::showEvent(e);
|
---|
| 168 | emit resized();
|
---|
| 169 | }
|
---|
| 170 | };
|
---|
| 171 |
|
---|
| 172 | } // anonymous namespace
|
---|
| 173 |
|
---|
[561] | 174 | class QPrintPreviewWidgetPrivate : public QWidgetPrivate
|
---|
[2] | 175 | {
|
---|
| 176 | Q_DECLARE_PUBLIC(QPrintPreviewWidget)
|
---|
| 177 | public:
|
---|
[561] | 178 | QPrintPreviewWidgetPrivate()
|
---|
| 179 | : scene(0), curPage(1),
|
---|
[2] | 180 | viewMode(QPrintPreviewWidget::SinglePageView),
|
---|
| 181 | zoomMode(QPrintPreviewWidget::FitInView),
|
---|
| 182 | zoomFactor(1), initialized(false), fitting(true)
|
---|
| 183 | {}
|
---|
| 184 |
|
---|
| 185 | // private slots
|
---|
| 186 | void _q_fit(bool doFitting = false);
|
---|
| 187 | void _q_updateCurrentPage();
|
---|
| 188 |
|
---|
| 189 | void init();
|
---|
| 190 | void populateScene();
|
---|
| 191 | void layoutPages();
|
---|
| 192 | void generatePreview();
|
---|
| 193 | void setCurrentPage(int pageNumber);
|
---|
| 194 | void zoom(qreal zoom);
|
---|
| 195 | void setZoomFactor(qreal zoomFactor);
|
---|
| 196 | int calcCurrentPage();
|
---|
| 197 |
|
---|
| 198 | GraphicsView *graphicsView;
|
---|
| 199 | QGraphicsScene *scene;
|
---|
| 200 |
|
---|
| 201 | int curPage;
|
---|
| 202 | QList<const QPicture *> pictures;
|
---|
| 203 | QList<QGraphicsItem *> pages;
|
---|
| 204 |
|
---|
| 205 | QPrintPreviewWidget::ViewMode viewMode;
|
---|
| 206 | QPrintPreviewWidget::ZoomMode zoomMode;
|
---|
| 207 | qreal zoomFactor;
|
---|
| 208 | bool ownPrinter;
|
---|
| 209 | QPrinter* printer;
|
---|
| 210 | bool initialized;
|
---|
| 211 | bool fitting;
|
---|
| 212 | };
|
---|
| 213 |
|
---|
| 214 | void QPrintPreviewWidgetPrivate::_q_fit(bool doFitting)
|
---|
| 215 | {
|
---|
| 216 | Q_Q(QPrintPreviewWidget);
|
---|
| 217 |
|
---|
| 218 | if (curPage < 1 || curPage > pages.count())
|
---|
| 219 | return;
|
---|
| 220 |
|
---|
| 221 | if (!doFitting && !fitting)
|
---|
| 222 | return;
|
---|
| 223 |
|
---|
| 224 | if (doFitting && fitting) {
|
---|
| 225 | QRect viewRect = graphicsView->viewport()->rect();
|
---|
| 226 | if (zoomMode == QPrintPreviewWidget::FitInView) {
|
---|
| 227 | QList<QGraphicsItem*> containedItems = graphicsView->items(viewRect, Qt::ContainsItemBoundingRect);
|
---|
| 228 | foreach(QGraphicsItem* item, containedItems) {
|
---|
| 229 | PageItem* pg = static_cast<PageItem*>(item);
|
---|
| 230 | if (pg->pageNumber() == curPage)
|
---|
| 231 | return;
|
---|
| 232 | }
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | int newPage = calcCurrentPage();
|
---|
| 236 | if (newPage != curPage)
|
---|
| 237 | curPage = newPage;
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | QRectF target = pages.at(curPage-1)->sceneBoundingRect();
|
---|
| 241 | if (viewMode == QPrintPreviewWidget::FacingPagesView) {
|
---|
| 242 | // fit two pages
|
---|
| 243 | if (curPage % 2)
|
---|
| 244 | target.setLeft(target.left() - target.width());
|
---|
| 245 | else
|
---|
| 246 | target.setRight(target.right() + target.width());
|
---|
| 247 | } else if (viewMode == QPrintPreviewWidget::AllPagesView) {
|
---|
| 248 | target = scene->itemsBoundingRect();
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | if (zoomMode == QPrintPreviewWidget::FitToWidth) {
|
---|
| 252 | QTransform t;
|
---|
| 253 | qreal scale = graphicsView->viewport()->width() / target.width();
|
---|
| 254 | t.scale(scale, scale);
|
---|
| 255 | graphicsView->setTransform(t);
|
---|
| 256 | if (doFitting && fitting) {
|
---|
| 257 | QRectF viewSceneRect = graphicsView->viewportTransform().mapRect(graphicsView->viewport()->rect());
|
---|
| 258 | viewSceneRect.moveTop(target.top());
|
---|
| 259 | graphicsView->ensureVisible(viewSceneRect); // Nah...
|
---|
| 260 | }
|
---|
| 261 | } else {
|
---|
| 262 | graphicsView->fitInView(target, Qt::KeepAspectRatio);
|
---|
| 263 | if (zoomMode == QPrintPreviewWidget::FitInView) {
|
---|
| 264 | int step = qRound(graphicsView->matrix().mapRect(target).height());
|
---|
| 265 | graphicsView->verticalScrollBar()->setSingleStep(step);
|
---|
| 266 | graphicsView->verticalScrollBar()->setPageStep(step);
|
---|
| 267 | }
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | zoomFactor = graphicsView->transform().m11() * (float(printer->logicalDpiY()) / q->logicalDpiY());
|
---|
| 271 | emit q->previewChanged();
|
---|
| 272 | }
|
---|
| 273 |
|
---|
| 274 | void QPrintPreviewWidgetPrivate::_q_updateCurrentPage()
|
---|
| 275 | {
|
---|
| 276 | Q_Q(QPrintPreviewWidget);
|
---|
| 277 |
|
---|
| 278 | if (viewMode == QPrintPreviewWidget::AllPagesView)
|
---|
| 279 | return;
|
---|
| 280 |
|
---|
| 281 | int newPage = calcCurrentPage();
|
---|
| 282 | if (newPage != curPage) {
|
---|
| 283 | curPage = newPage;
|
---|
| 284 | emit q->previewChanged();
|
---|
| 285 | }
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 | int QPrintPreviewWidgetPrivate::calcCurrentPage()
|
---|
| 289 | {
|
---|
| 290 | int maxArea = 0;
|
---|
| 291 | int newPage = curPage;
|
---|
| 292 | QRect viewRect = graphicsView->viewport()->rect();
|
---|
| 293 | QList<QGraphicsItem*> items = graphicsView->items(viewRect);
|
---|
| 294 | for (int i=0; i<items.size(); ++i) {
|
---|
| 295 | PageItem* pg = static_cast<PageItem*>(items.at(i));
|
---|
| 296 | QRect overlap = graphicsView->mapFromScene(pg->sceneBoundingRect()).boundingRect() & viewRect;
|
---|
| 297 | int area = overlap.width() * overlap.height();
|
---|
| 298 | if (area > maxArea) {
|
---|
| 299 | maxArea = area;
|
---|
| 300 | newPage = pg->pageNumber();
|
---|
| 301 | } else if (area == maxArea && pg->pageNumber() < newPage) {
|
---|
| 302 | newPage = pg->pageNumber();
|
---|
| 303 | }
|
---|
| 304 | }
|
---|
| 305 | return newPage;
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 | void QPrintPreviewWidgetPrivate::init()
|
---|
| 309 | {
|
---|
| 310 | Q_Q(QPrintPreviewWidget);
|
---|
| 311 |
|
---|
| 312 | graphicsView = new GraphicsView;
|
---|
| 313 | graphicsView->setInteractive(false);
|
---|
| 314 | graphicsView->setDragMode(QGraphicsView::ScrollHandDrag);
|
---|
| 315 | graphicsView->setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
|
---|
| 316 | QObject::connect(graphicsView->verticalScrollBar(), SIGNAL(valueChanged(int)),
|
---|
| 317 | q, SLOT(_q_updateCurrentPage()));
|
---|
| 318 | QObject::connect(graphicsView, SIGNAL(resized()), q, SLOT(_q_fit()));
|
---|
| 319 |
|
---|
| 320 | scene = new QGraphicsScene(graphicsView);
|
---|
| 321 | scene->setBackgroundBrush(Qt::gray);
|
---|
| 322 | graphicsView->setScene(scene);
|
---|
| 323 |
|
---|
| 324 | QVBoxLayout *layout = new QVBoxLayout;
|
---|
| 325 | q->setLayout(layout);
|
---|
| 326 | layout->setContentsMargins(0, 0, 0, 0);
|
---|
| 327 | layout->addWidget(graphicsView);
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | void QPrintPreviewWidgetPrivate::populateScene()
|
---|
| 331 | {
|
---|
| 332 | // remove old pages
|
---|
| 333 | for (int i = 0; i < pages.size(); i++)
|
---|
| 334 | scene->removeItem(pages.at(i));
|
---|
| 335 | qDeleteAll(pages);
|
---|
| 336 | pages.clear();
|
---|
| 337 |
|
---|
| 338 | int numPages = pictures.count();
|
---|
| 339 | QSize paperSize = printer->paperRect().size();
|
---|
| 340 | QRect pageRect = printer->pageRect();
|
---|
| 341 |
|
---|
| 342 | for (int i = 0; i < numPages; i++) {
|
---|
| 343 | PageItem* item = new PageItem(i+1, pictures.at(i), paperSize, pageRect);
|
---|
| 344 | scene->addItem(item);
|
---|
| 345 | pages.append(item);
|
---|
| 346 | }
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 | void QPrintPreviewWidgetPrivate::layoutPages()
|
---|
| 350 | {
|
---|
| 351 | int numPages = pages.count();
|
---|
| 352 | if (numPages < 1)
|
---|
| 353 | return;
|
---|
| 354 |
|
---|
| 355 | int numPagePlaces = numPages;
|
---|
| 356 | int cols = 1; // singleMode and default
|
---|
| 357 | if (viewMode == QPrintPreviewWidget::AllPagesView) {
|
---|
| 358 | if (printer->orientation() == QPrinter::Portrait)
|
---|
| 359 | cols = qCeil(qSqrt((float) numPages));
|
---|
| 360 | else
|
---|
| 361 | cols = qFloor(qSqrt((float) numPages));
|
---|
| 362 | cols += cols % 2; // Nicer with an even number of cols
|
---|
| 363 | }
|
---|
| 364 | else if (viewMode == QPrintPreviewWidget::FacingPagesView) {
|
---|
| 365 | cols = 2;
|
---|
| 366 | numPagePlaces += 1;
|
---|
| 367 | }
|
---|
| 368 | int rows = qCeil(qreal(numPagePlaces) / cols);
|
---|
| 369 |
|
---|
| 370 | qreal itemWidth = pages.at(0)->boundingRect().width();
|
---|
| 371 | qreal itemHeight = pages.at(0)->boundingRect().height();
|
---|
| 372 | int pageNum = 1;
|
---|
| 373 | for (int i = 0; i < rows && pageNum <= numPages; i++) {
|
---|
| 374 | for (int j = 0; j < cols && pageNum <= numPages; j++) {
|
---|
| 375 | if (!i && !j && viewMode == QPrintPreviewWidget::FacingPagesView) {
|
---|
| 376 | // Front page doesn't have a facing page
|
---|
| 377 | continue;
|
---|
| 378 | } else {
|
---|
| 379 | pages.at(pageNum-1)->setPos(QPointF(j*itemWidth, i*itemHeight));
|
---|
| 380 | pageNum++;
|
---|
| 381 | }
|
---|
| 382 | }
|
---|
| 383 | }
|
---|
| 384 | scene->setSceneRect(scene->itemsBoundingRect());
|
---|
| 385 | }
|
---|
| 386 |
|
---|
| 387 | void QPrintPreviewWidgetPrivate::generatePreview()
|
---|
| 388 | {
|
---|
| 389 | //### If QPrinter::setPreviewMode() becomes public, handle the
|
---|
| 390 | //### case that we have been constructed with a printer that
|
---|
| 391 | //### _already_ has been preview-painted to, so we should
|
---|
| 392 | //### initially just show the pages it already contains, and not
|
---|
| 393 | //### emit paintRequested() until the user changes some parameter
|
---|
| 394 |
|
---|
| 395 | Q_Q(QPrintPreviewWidget);
|
---|
| 396 | printer->d_func()->setPreviewMode(true);
|
---|
| 397 | emit q->paintRequested(printer);
|
---|
| 398 | printer->d_func()->setPreviewMode(false);
|
---|
| 399 | pictures = printer->d_func()->previewPages();
|
---|
| 400 | populateScene(); // i.e. setPreviewPrintedPictures() e.l.
|
---|
| 401 | layoutPages();
|
---|
| 402 | curPage = qBound(1, curPage, pages.count());
|
---|
| 403 | if (fitting)
|
---|
| 404 | _q_fit();
|
---|
| 405 | emit q->previewChanged();
|
---|
| 406 | }
|
---|
| 407 |
|
---|
| 408 | void QPrintPreviewWidgetPrivate::setCurrentPage(int pageNumber)
|
---|
| 409 | {
|
---|
| 410 | if (pageNumber < 1 || pageNumber > pages.count())
|
---|
| 411 | return;
|
---|
| 412 |
|
---|
| 413 | int lastPage = curPage;
|
---|
| 414 | curPage = pageNumber;
|
---|
| 415 |
|
---|
| 416 | if (lastPage != curPage && lastPage > 0 && lastPage <= pages.count()) {
|
---|
| 417 | if (zoomMode != QPrintPreviewWidget::FitInView) {
|
---|
| 418 | QScrollBar *hsc = graphicsView->horizontalScrollBar();
|
---|
| 419 | QScrollBar *vsc = graphicsView->verticalScrollBar();
|
---|
| 420 | QPointF pt = graphicsView->transform().map(pages.at(curPage-1)->pos());
|
---|
| 421 | vsc->setValue(int(pt.y()) - 10);
|
---|
| 422 | hsc->setValue(int(pt.x()) - 10);
|
---|
| 423 | } else {
|
---|
| 424 | graphicsView->centerOn(pages.at(curPage-1));
|
---|
| 425 | }
|
---|
| 426 | }
|
---|
| 427 | }
|
---|
| 428 |
|
---|
| 429 | void QPrintPreviewWidgetPrivate::zoom(qreal zoom)
|
---|
| 430 | {
|
---|
| 431 | zoomFactor *= zoom;
|
---|
| 432 | graphicsView->scale(zoom, zoom);
|
---|
| 433 | }
|
---|
| 434 |
|
---|
| 435 | void QPrintPreviewWidgetPrivate::setZoomFactor(qreal _zoomFactor)
|
---|
| 436 | {
|
---|
| 437 | Q_Q(QPrintPreviewWidget);
|
---|
| 438 | zoomFactor = _zoomFactor;
|
---|
| 439 | graphicsView->resetTransform();
|
---|
| 440 | int dpi_y = q->logicalDpiY();
|
---|
| 441 | int printer_dpi_y = printer->logicalDpiY();
|
---|
| 442 | graphicsView->scale(zoomFactor*(dpi_y/float(printer_dpi_y)),
|
---|
| 443 | zoomFactor*(dpi_y/float(printer_dpi_y)));
|
---|
| 444 | }
|
---|
| 445 |
|
---|
| 446 | ///////////////////////////////////////
|
---|
| 447 |
|
---|
| 448 | /*!
|
---|
| 449 | \class QPrintPreviewWidget
|
---|
| 450 | \since 4.4
|
---|
| 451 |
|
---|
| 452 | \brief The QPrintPreviewWidget class provides a widget for
|
---|
| 453 | previewing page layouts for printer output.
|
---|
| 454 |
|
---|
[561] | 455 | \ingroup printing
|
---|
[2] | 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 | \o Create the QPrintPreviewWidget
|
---|
| 466 |
|
---|
| 467 | Construct the QPrintPreviewWidget either by passing in an
|
---|
| 468 | exisiting QPrinter object, or have QPrintPreviewWidget create a
|
---|
| 469 | default constructed QPrinter object for you.
|
---|
| 470 |
|
---|
| 471 | \o 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)
|
---|
[561] | 521 | : QWidget(*new QPrintPreviewWidgetPrivate, parent, flags)
|
---|
[2] | 522 | {
|
---|
| 523 | Q_D(QPrintPreviewWidget);
|
---|
| 524 | d->printer = printer;
|
---|
| 525 | d->ownPrinter = false;
|
---|
| 526 | d->init();
|
---|
| 527 | }
|
---|
| 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)
|
---|
[561] | 537 | : QWidget(*new QPrintPreviewWidgetPrivate, parent, flags)
|
---|
[2] | 538 | {
|
---|
| 539 | Q_D(QPrintPreviewWidget);
|
---|
| 540 | d->printer = new QPrinter;
|
---|
| 541 | d->ownPrinter = true;
|
---|
| 542 | d->init();
|
---|
| 543 | }
|
---|
| 544 |
|
---|
| 545 |
|
---|
| 546 | /*!
|
---|
| 547 | Destroys the QPrintPreviewWidget.
|
---|
| 548 | */
|
---|
| 549 | QPrintPreviewWidget::~QPrintPreviewWidget()
|
---|
| 550 | {
|
---|
| 551 | Q_D(QPrintPreviewWidget);
|
---|
| 552 | if (d->ownPrinter)
|
---|
| 553 | delete d->printer;
|
---|
| 554 | }
|
---|
| 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;
|
---|
| 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) {
|
---|
| 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 {
|
---|
| 581 | d->fitting = true;
|
---|
| 582 | d->_q_fit();
|
---|
| 583 | }
|
---|
| 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();
|
---|
| 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 | }
|
---|
| 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 | }
|
---|
| 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 | }
|
---|
| 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 | }
|
---|
| 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;
|
---|
| 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 | }
|
---|
| 664 |
|
---|
| 665 | /*!
|
---|
[561] | 666 | \obsolete
|
---|
[2] | 667 | Returns the number of pages in the preview.
|
---|
[561] | 668 | \sa pageCount()
|
---|
[2] | 669 | */
|
---|
| 670 | int QPrintPreviewWidget::numPages() const
|
---|
| 671 | {
|
---|
| 672 | Q_D(const QPrintPreviewWidget);
|
---|
| 673 | return d->pages.size();
|
---|
| 674 | }
|
---|
| 675 |
|
---|
| 676 | /*!
|
---|
[561] | 677 | \since 4.6
|
---|
| 678 | Returns the number of pages in the preview.
|
---|
| 679 | */
|
---|
| 680 | int QPrintPreviewWidget::pageCount() const
|
---|
| 681 | {
|
---|
| 682 | Q_D(const QPrintPreviewWidget);
|
---|
| 683 | return d->pages.size();
|
---|
| 684 | }
|
---|
| 685 |
|
---|
| 686 | /*!
|
---|
[2] | 687 | Returns the currently viewed page in the preview.
|
---|
| 688 | */
|
---|
| 689 | int QPrintPreviewWidget::currentPage() const
|
---|
| 690 | {
|
---|
| 691 | Q_D(const QPrintPreviewWidget);
|
---|
| 692 | return d->curPage;
|
---|
| 693 | }
|
---|
| 694 |
|
---|
| 695 | /*!
|
---|
| 696 | Sets the current page in the preview. This will cause the view to
|
---|
| 697 | skip to the beginning of \a page.
|
---|
| 698 | */
|
---|
| 699 | void QPrintPreviewWidget::setCurrentPage(int page)
|
---|
| 700 | {
|
---|
| 701 | Q_D(QPrintPreviewWidget);
|
---|
| 702 | d->setCurrentPage(page);
|
---|
| 703 | }
|
---|
| 704 |
|
---|
| 705 | /*!
|
---|
| 706 | This is a convenience function and is the same as calling \c
|
---|
| 707 | {setZoomMode(QPrintPreviewWidget::FitToWidth)}.
|
---|
| 708 | */
|
---|
| 709 | void QPrintPreviewWidget::fitToWidth()
|
---|
| 710 | {
|
---|
| 711 | setZoomMode(FitToWidth);
|
---|
| 712 | }
|
---|
| 713 |
|
---|
| 714 | /*!
|
---|
| 715 | This is a convenience function and is the same as calling \c
|
---|
| 716 | {setZoomMode(QPrintPreviewWidget::FitInView)}.
|
---|
| 717 | */
|
---|
| 718 | void QPrintPreviewWidget::fitInView()
|
---|
| 719 | {
|
---|
| 720 | setZoomMode(FitInView);
|
---|
| 721 | }
|
---|
| 722 |
|
---|
| 723 | /*!
|
---|
| 724 | Sets the zoom mode to \a zoomMode. The default zoom mode is FitInView.
|
---|
| 725 |
|
---|
| 726 | \sa zoomMode(), viewMode(), setViewMode()
|
---|
| 727 | */
|
---|
| 728 | void QPrintPreviewWidget::setZoomMode(QPrintPreviewWidget::ZoomMode zoomMode)
|
---|
| 729 | {
|
---|
| 730 | Q_D(QPrintPreviewWidget);
|
---|
| 731 | d->zoomMode = zoomMode;
|
---|
| 732 | if (d->zoomMode == FitInView || d->zoomMode == FitToWidth) {
|
---|
| 733 | d->fitting = true;
|
---|
| 734 | d->_q_fit(true);
|
---|
| 735 | } else {
|
---|
| 736 | d->fitting = false;
|
---|
| 737 | }
|
---|
| 738 | }
|
---|
| 739 |
|
---|
| 740 | /*!
|
---|
| 741 | Returns the current zoom mode.
|
---|
| 742 |
|
---|
| 743 | \sa setZoomMode(), viewMode(), setViewMode()
|
---|
| 744 | */
|
---|
| 745 | QPrintPreviewWidget::ZoomMode QPrintPreviewWidget::zoomMode() const
|
---|
| 746 | {
|
---|
| 747 | Q_D(const QPrintPreviewWidget);
|
---|
| 748 | return d->zoomMode;
|
---|
| 749 | }
|
---|
| 750 |
|
---|
| 751 | /*!
|
---|
| 752 | This is a convenience function and is the same as calling \c
|
---|
| 753 | {setOrientation(QPrinter::Landscape)}.
|
---|
| 754 | */
|
---|
| 755 | void QPrintPreviewWidget::setLandscapeOrientation()
|
---|
| 756 | {
|
---|
| 757 | setOrientation(QPrinter::Landscape);
|
---|
| 758 | }
|
---|
| 759 |
|
---|
| 760 | /*!
|
---|
| 761 | This is a convenience function and is the same as calling \c
|
---|
| 762 | {setOrientation(QPrinter::Portrait)}.
|
---|
| 763 | */
|
---|
| 764 | void QPrintPreviewWidget::setPortraitOrientation()
|
---|
| 765 | {
|
---|
| 766 | setOrientation(QPrinter::Portrait);
|
---|
| 767 | }
|
---|
| 768 |
|
---|
| 769 | /*!
|
---|
| 770 | This is a convenience function and is the same as calling \c
|
---|
| 771 | {setViewMode(QPrintPreviewWidget::SinglePageView)}.
|
---|
| 772 | */
|
---|
| 773 | void QPrintPreviewWidget::setSinglePageViewMode()
|
---|
| 774 | {
|
---|
| 775 | setViewMode(SinglePageView);
|
---|
| 776 | }
|
---|
| 777 |
|
---|
| 778 | /*!
|
---|
| 779 | This is a convenience function and is the same as calling \c
|
---|
| 780 | {setViewMode(QPrintPreviewWidget::FacingPagesView)}.
|
---|
| 781 | */
|
---|
| 782 | void QPrintPreviewWidget::setFacingPagesViewMode()
|
---|
| 783 | {
|
---|
| 784 | setViewMode(FacingPagesView);
|
---|
| 785 | }
|
---|
| 786 |
|
---|
| 787 | /*!
|
---|
| 788 | This is a convenience function and is the same as calling \c
|
---|
| 789 | {setViewMode(QPrintPreviewWidget::AllPagesView)}.
|
---|
| 790 | */
|
---|
| 791 | void QPrintPreviewWidget::setAllPagesViewMode()
|
---|
| 792 | {
|
---|
| 793 | setViewMode(AllPagesView);
|
---|
| 794 | }
|
---|
| 795 |
|
---|
| 796 |
|
---|
| 797 | /*!
|
---|
| 798 | This function updates the preview, which causes the
|
---|
| 799 | paintRequested() signal to be emitted.
|
---|
| 800 | */
|
---|
| 801 | void QPrintPreviewWidget::updatePreview()
|
---|
| 802 | {
|
---|
| 803 | Q_D(QPrintPreviewWidget);
|
---|
| 804 | d->initialized = true;
|
---|
| 805 | d->generatePreview();
|
---|
| 806 | d->graphicsView->updateGeometry();
|
---|
| 807 | }
|
---|
| 808 |
|
---|
| 809 | /*! \reimp
|
---|
| 810 | */
|
---|
| 811 | void QPrintPreviewWidget::setVisible(bool visible)
|
---|
| 812 | {
|
---|
| 813 | Q_D(QPrintPreviewWidget);
|
---|
| 814 | if (visible && !d->initialized)
|
---|
| 815 | updatePreview();
|
---|
| 816 | QWidget::setVisible(visible);
|
---|
| 817 | }
|
---|
| 818 |
|
---|
| 819 | /*!
|
---|
| 820 | \fn void QPrintPreviewWidget::paintRequested(QPrinter *printer)
|
---|
| 821 |
|
---|
| 822 | This signal is emitted when the preview widget needs to generate a
|
---|
| 823 | set of preview pages. \a printer is the printer associated with
|
---|
| 824 | this preview widget.
|
---|
| 825 | */
|
---|
| 826 |
|
---|
| 827 | /*!
|
---|
| 828 | \fn void QPrintPreviewWidget::previewChanged()
|
---|
| 829 |
|
---|
| 830 | This signal is emitted whenever the preview widget has changed
|
---|
| 831 | some internal state, such as the orientation.
|
---|
| 832 | */
|
---|
| 833 |
|
---|
| 834 |
|
---|
| 835 | QT_END_NAMESPACE
|
---|
| 836 |
|
---|
| 837 | #include "moc_qprintpreviewwidget.cpp"
|
---|
| 838 | #include "qprintpreviewwidget.moc"
|
---|
| 839 |
|
---|
| 840 | #endif // QT_NO_PRINTPREVIEWWIDGET
|
---|