| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the QtGui module of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 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 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include "qprintpreviewwidget.h"
|
|---|
| 43 | #include <private/qprinter_p.h>
|
|---|
| 44 |
|
|---|
| 45 | #include <QtCore/qmath.h>
|
|---|
| 46 | #include <QtGui/qboxlayout.h>
|
|---|
| 47 | #include <QtGui/qgraphicsitem.h>
|
|---|
| 48 | #include <QtGui/qgraphicsview.h>
|
|---|
| 49 | #include <QtGui/qscrollbar.h>
|
|---|
| 50 | #include <QtGui/qstyleoption.h>
|
|---|
| 51 |
|
|---|
| 52 | #ifndef QT_NO_PRINTPREVIEWWIDGET
|
|---|
| 53 |
|
|---|
| 54 | QT_BEGIN_NAMESPACE
|
|---|
| 55 |
|
|---|
| 56 | namespace {
|
|---|
| 57 | class PageItem : public QGraphicsItem
|
|---|
| 58 | {
|
|---|
| 59 | public:
|
|---|
| 60 | PageItem(int _pageNum, const QPicture* _pagePicture, QSize _paperSize, QRect _pageRect)
|
|---|
| 61 | : pageNum(_pageNum), pagePicture(_pagePicture),
|
|---|
| 62 | paperSize(_paperSize), pageRect(_pageRect)
|
|---|
| 63 | {
|
|---|
| 64 | qreal border = qMax(paperSize.height(), paperSize.width()) / 25;
|
|---|
| 65 | brect = QRectF(QPointF(-border, -border),
|
|---|
| 66 | QSizeF(paperSize)+QSizeF(2*border, 2*border));
|
|---|
| 67 | setCacheMode(DeviceCoordinateCache);
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | inline QRectF boundingRect() const
|
|---|
| 71 | { return brect; }
|
|---|
| 72 |
|
|---|
| 73 | inline int pageNumber() const
|
|---|
| 74 | { return pageNum; }
|
|---|
| 75 |
|
|---|
| 76 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget);
|
|---|
| 77 |
|
|---|
| 78 | private:
|
|---|
| 79 | int pageNum;
|
|---|
| 80 | const QPicture* pagePicture;
|
|---|
| 81 | QSize paperSize;
|
|---|
| 82 | QRect pageRect;
|
|---|
| 83 | QRectF brect;
|
|---|
| 84 | };
|
|---|
| 85 |
|
|---|
| 86 | void PageItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|---|
| 87 | {
|
|---|
| 88 | Q_UNUSED(widget);
|
|---|
| 89 |
|
|---|
| 90 | #if 0
|
|---|
| 91 | // Draw item bounding rect, for debugging
|
|---|
| 92 | painter->save();
|
|---|
| 93 | painter->setPen(QPen(Qt::red, 0));
|
|---|
| 94 | painter->setBrush(Qt::NoBrush);
|
|---|
| 95 | painter->drawRect(QRectF(-border()+1.0, -border()+1.0, boundingRect().width()-2, boundingRect().height()-2));
|
|---|
| 96 | painter->restore();
|
|---|
| 97 | #endif
|
|---|
| 98 |
|
|---|
| 99 | QRectF paperRect(0,0, paperSize.width(), paperSize.height());
|
|---|
| 100 |
|
|---|
| 101 | painter->setClipRect(paperRect & option->exposedRect);
|
|---|
| 102 | painter->fillRect(paperRect, Qt::white);
|
|---|
| 103 | if (!pagePicture)
|
|---|
| 104 | return;
|
|---|
| 105 | painter->drawPicture(pageRect.topLeft(), *pagePicture);
|
|---|
| 106 |
|
|---|
| 107 | // Effect: make anything drawn in the margins look washed out.
|
|---|
| 108 | QPainterPath path;
|
|---|
| 109 | path.addRect(paperRect);
|
|---|
| 110 | path.addRect(pageRect);
|
|---|
| 111 | painter->setPen(QPen(Qt::NoPen));
|
|---|
| 112 | painter->setBrush(QColor(255, 255, 255, 180));
|
|---|
| 113 | painter->drawPath(path);
|
|---|
| 114 |
|
|---|
| 115 | painter->setClipRect(option->exposedRect);
|
|---|
| 116 | #if 0
|
|---|
| 117 | // Draw frame around paper.
|
|---|
| 118 | painter->setPen(QPen(Qt::black, 0));
|
|---|
| 119 | painter->setBrush(Qt::NoBrush);
|
|---|
| 120 | painter->drawRect(paperRect);
|
|---|
| 121 | #endif
|
|---|
| 122 |
|
|---|
| 123 | // Draw shadow
|
|---|
| 124 | qreal shWidth = paperRect.width()/100;
|
|---|
| 125 | QRectF rshadow(paperRect.topRight() + QPointF(0, shWidth),
|
|---|
| 126 | paperRect.bottomRight() + QPointF(shWidth, 0));
|
|---|
| 127 | QLinearGradient rgrad(rshadow.topLeft(), rshadow.topRight());
|
|---|
| 128 | rgrad.setColorAt(0.0, QColor(0,0,0,255));
|
|---|
| 129 | rgrad.setColorAt(1.0, QColor(0,0,0,0));
|
|---|
| 130 | painter->fillRect(rshadow, QBrush(rgrad));
|
|---|
| 131 | QRectF bshadow(paperRect.bottomLeft() + QPointF(shWidth, 0),
|
|---|
| 132 | paperRect.bottomRight() + QPointF(0, shWidth));
|
|---|
| 133 | QLinearGradient bgrad(bshadow.topLeft(), bshadow.bottomLeft());
|
|---|
| 134 | bgrad.setColorAt(0.0, QColor(0,0,0,255));
|
|---|
| 135 | bgrad.setColorAt(1.0, QColor(0,0,0,0));
|
|---|
| 136 | painter->fillRect(bshadow, QBrush(bgrad));
|
|---|
| 137 | QRectF cshadow(paperRect.bottomRight(),
|
|---|
| 138 | paperRect.bottomRight() + QPointF(shWidth, shWidth));
|
|---|
| 139 | QRadialGradient cgrad(cshadow.topLeft(), shWidth, cshadow.topLeft());
|
|---|
| 140 | cgrad.setColorAt(0.0, QColor(0,0,0,255));
|
|---|
| 141 | cgrad.setColorAt(1.0, QColor(0,0,0,0));
|
|---|
| 142 | painter->fillRect(cshadow, QBrush(cgrad));
|
|---|
| 143 |
|
|---|
| 144 | // todo: drawtext "Page N" below paper
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | class GraphicsView : public QGraphicsView
|
|---|
| 148 | {
|
|---|
| 149 | Q_OBJECT
|
|---|
| 150 | public:
|
|---|
| 151 | GraphicsView(QWidget* parent = 0)
|
|---|
| 152 | : QGraphicsView(parent)
|
|---|
| 153 | {}
|
|---|
| 154 | signals:
|
|---|
| 155 | void resized();
|
|---|
| 156 |
|
|---|
| 157 | protected:
|
|---|
| 158 | void resizeEvent(QResizeEvent* e)
|
|---|
| 159 | {
|
|---|
| 160 | QGraphicsView::resizeEvent(e);
|
|---|
| 161 | emit resized();
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | void showEvent(QShowEvent* e)
|
|---|
| 165 | {
|
|---|
| 166 | QGraphicsView::showEvent(e);
|
|---|
| 167 | emit resized();
|
|---|
| 168 | }
|
|---|
| 169 | };
|
|---|
| 170 |
|
|---|
| 171 | } // anonymous namespace
|
|---|
| 172 |
|
|---|
| 173 | class QPrintPreviewWidgetPrivate
|
|---|
| 174 | {
|
|---|
| 175 | Q_DECLARE_PUBLIC(QPrintPreviewWidget)
|
|---|
| 176 | public:
|
|---|
| 177 | QPrintPreviewWidgetPrivate(QPrintPreviewWidget *q)
|
|---|
| 178 | : q_ptr(q), scene(0), curPage(1),
|
|---|
| 179 | viewMode(QPrintPreviewWidget::SinglePageView),
|
|---|
| 180 | zoomMode(QPrintPreviewWidget::FitInView),
|
|---|
| 181 | zoomFactor(1), initialized(false), fitting(true)
|
|---|
| 182 | {}
|
|---|
| 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 | QPrintPreviewWidget *q_ptr;
|
|---|
| 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 |
|
|---|
| 455 | \ingroup multimedia
|
|---|
| 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)
|
|---|
| 521 | : QWidget(parent, flags), d_ptr(new QPrintPreviewWidgetPrivate(this))
|
|---|
| 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)
|
|---|
| 537 | : QWidget(parent, flags), d_ptr(new QPrintPreviewWidgetPrivate(this))
|
|---|
| 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 | delete d_ptr;
|
|---|
| 555 | }
|
|---|
| 556 |
|
|---|
| 557 | /*!
|
|---|
| 558 | Returns the current view mode. The default view mode is SinglePageView.
|
|---|
| 559 | */
|
|---|
| 560 | QPrintPreviewWidget::ViewMode QPrintPreviewWidget::viewMode() const
|
|---|
| 561 | {
|
|---|
| 562 | Q_D(const QPrintPreviewWidget);
|
|---|
| 563 | return d->viewMode;
|
|---|
| 564 | }
|
|---|
| 565 |
|
|---|
| 566 | /*!
|
|---|
| 567 | Sets the view mode to \a mode. The default view mode is
|
|---|
| 568 | SinglePageView.
|
|---|
| 569 | */
|
|---|
| 570 | void QPrintPreviewWidget::setViewMode(ViewMode mode)
|
|---|
| 571 | {
|
|---|
| 572 | Q_D(QPrintPreviewWidget);
|
|---|
| 573 | d->viewMode = mode;
|
|---|
| 574 | d->layoutPages();
|
|---|
| 575 | if (d->viewMode == AllPagesView) {
|
|---|
| 576 | d->graphicsView->fitInView(d->scene->itemsBoundingRect(), Qt::KeepAspectRatio);
|
|---|
| 577 | d->fitting = false;
|
|---|
| 578 | d->zoomMode = QPrintPreviewWidget::CustomZoom;
|
|---|
| 579 | d->zoomFactor = d->graphicsView->transform().m11() * (float(d->printer->logicalDpiY()) / logicalDpiY());
|
|---|
| 580 | emit previewChanged();
|
|---|
| 581 | } else {
|
|---|
| 582 | d->fitting = true;
|
|---|
| 583 | d->_q_fit();
|
|---|
| 584 | }
|
|---|
| 585 | }
|
|---|
| 586 |
|
|---|
| 587 | /*!
|
|---|
| 588 | Returns the current orientation of the preview. This value is
|
|---|
| 589 | obtained from the QPrinter object associated with the preview.
|
|---|
| 590 | */
|
|---|
| 591 | QPrinter::Orientation QPrintPreviewWidget::orientation() const
|
|---|
| 592 | {
|
|---|
| 593 | Q_D(const QPrintPreviewWidget);
|
|---|
| 594 | return d->printer->orientation();
|
|---|
| 595 | }
|
|---|
| 596 |
|
|---|
| 597 | /*!
|
|---|
| 598 | Sets the current orientation to \a orientation. This value will be
|
|---|
| 599 | set on the QPrinter object associated with the preview.
|
|---|
| 600 | */
|
|---|
| 601 | void QPrintPreviewWidget::setOrientation(QPrinter::Orientation orientation)
|
|---|
| 602 | {
|
|---|
| 603 | Q_D(QPrintPreviewWidget);
|
|---|
| 604 | d->printer->setOrientation(orientation);
|
|---|
| 605 | d->generatePreview();
|
|---|
| 606 | }
|
|---|
| 607 |
|
|---|
| 608 | /*!
|
|---|
| 609 | Prints the preview to the printer associated with the preview.
|
|---|
| 610 | */
|
|---|
| 611 | void QPrintPreviewWidget::print()
|
|---|
| 612 | {
|
|---|
| 613 | Q_D(QPrintPreviewWidget);
|
|---|
| 614 | // ### make use of the generated pages
|
|---|
| 615 | emit paintRequested(d->printer);
|
|---|
| 616 | }
|
|---|
| 617 |
|
|---|
| 618 | /*!
|
|---|
| 619 | Zooms the current view in by \a factor. The default value for \a
|
|---|
| 620 | factor is 1.1, which means the view will be scaled up by 10%.
|
|---|
| 621 | */
|
|---|
| 622 | void QPrintPreviewWidget::zoomIn(qreal factor)
|
|---|
| 623 | {
|
|---|
| 624 | Q_D(QPrintPreviewWidget);
|
|---|
| 625 | d->fitting = false;
|
|---|
| 626 | d->zoomMode = QPrintPreviewWidget::CustomZoom;
|
|---|
| 627 | d->zoom(factor);
|
|---|
| 628 | }
|
|---|
| 629 |
|
|---|
| 630 | /*!
|
|---|
| 631 | Zooms the current view out by \a factor. The default value for \a
|
|---|
| 632 | factor is 1.1, which means the view will be scaled down by 10%.
|
|---|
| 633 | */
|
|---|
| 634 | void QPrintPreviewWidget::zoomOut(qreal factor)
|
|---|
| 635 | {
|
|---|
| 636 | Q_D(QPrintPreviewWidget);
|
|---|
| 637 | d->fitting = false;
|
|---|
| 638 | d->zoomMode = QPrintPreviewWidget::CustomZoom;
|
|---|
| 639 | d->zoom(1/factor);
|
|---|
| 640 | }
|
|---|
| 641 |
|
|---|
| 642 | /*!
|
|---|
| 643 | Returns the zoom factor of the view.
|
|---|
| 644 | */
|
|---|
| 645 | qreal QPrintPreviewWidget::zoomFactor() const
|
|---|
| 646 | {
|
|---|
| 647 | Q_D(const QPrintPreviewWidget);
|
|---|
| 648 | return d->zoomFactor;
|
|---|
| 649 | }
|
|---|
| 650 |
|
|---|
| 651 | /*!
|
|---|
| 652 | Sets the zoom factor of the view to \a factor. For example, a
|
|---|
| 653 | value of 1.0 indicates an unscaled view, which is approximately
|
|---|
| 654 | the size the view will have on paper. A value of 0.5 will halve
|
|---|
| 655 | the size of the view, while a value of 2.0 will double the size of
|
|---|
| 656 | the view.
|
|---|
| 657 | */
|
|---|
| 658 | void QPrintPreviewWidget::setZoomFactor(qreal factor)
|
|---|
| 659 | {
|
|---|
| 660 | Q_D(QPrintPreviewWidget);
|
|---|
| 661 | d->fitting = false;
|
|---|
| 662 | d->zoomMode = QPrintPreviewWidget::CustomZoom;
|
|---|
| 663 | d->setZoomFactor(factor);
|
|---|
| 664 | }
|
|---|
| 665 |
|
|---|
| 666 | /*!
|
|---|
| 667 | Returns the number of pages in the preview.
|
|---|
| 668 | */
|
|---|
| 669 | int QPrintPreviewWidget::numPages() const
|
|---|
| 670 | {
|
|---|
| 671 | Q_D(const QPrintPreviewWidget);
|
|---|
| 672 | return d->pages.size();
|
|---|
| 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;
|
|---|
| 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 | }
|
|---|
| 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 | }
|
|---|
| 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 | }
|
|---|
| 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) {
|
|---|
| 722 | d->fitting = true;
|
|---|
| 723 | d->_q_fit(true);
|
|---|
| 724 | } else {
|
|---|
| 725 | d->fitting = false;
|
|---|
| 726 | }
|
|---|
| 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;
|
|---|
| 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 | }
|
|---|
| 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 | }
|
|---|
| 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 | }
|
|---|
| 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 | }
|
|---|
| 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 | }
|
|---|
| 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 | }
|
|---|
| 797 |
|
|---|
| 798 | /*! \reimp
|
|---|
| 799 | */
|
|---|
| 800 | void QPrintPreviewWidget::setVisible(bool visible)
|
|---|
| 801 | {
|
|---|
| 802 | Q_D(QPrintPreviewWidget);
|
|---|
| 803 | if (visible && !d->initialized)
|
|---|
| 804 | updatePreview();
|
|---|
| 805 | QWidget::setVisible(visible);
|
|---|
| 806 | }
|
|---|
| 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
|
|---|