43 #include "private/qwidget_p.h" 44 #include <private/qprinter_p.h> 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> 53 #ifndef QT_NO_PRINTPREVIEWWIDGET 61 PageItem(
int _pageNum,
const QPicture* _pagePicture,
QSize _paperSize,
QRect _pageRect)
62 : pageNum(_pageNum), pagePicture(_pagePicture),
63 paperSize(_paperSize), pageRect(_pageRect)
74 inline int pageNumber()
const 104 qreal shWidth = paperRect.width()/100;
106 paperRect.bottomRight() +
QPointF(shWidth, 0));
109 rgrad.setColorAt(1.0,
QColor(0,0,0,0));
112 paperRect.bottomRight() +
QPointF(0, shWidth));
115 bgrad.setColorAt(1.0,
QColor(0,0,0,0));
117 QRectF cshadow(paperRect.bottomRight(),
118 paperRect.bottomRight() +
QPointF(shWidth, shWidth));
121 cgrad.setColorAt(1.0,
QColor(0,0,0,0));
128 painter->
drawPicture(pageRect.topLeft(), *pagePicture);
152 GraphicsView(
QWidget* parent = 0)
183 : scene(0), curPage(1),
186 zoomFactor(1), initialized(false), fitting(true)
190 void _q_fit(
bool doFitting =
false);
191 void _q_updateCurrentPage();
194 void populateScene();
196 void generatePreview();
197 void setCurrentPage(
int pageNumber);
198 void zoom(
qreal zoom);
199 void setZoomFactor(
qreal zoomFactor);
200 int calcCurrentPage();
222 if (curPage < 1 || curPage > pages.count())
225 if (!doFitting && !fitting)
228 if (doFitting && fitting) {
229 QRect viewRect = graphicsView->viewport()->rect();
233 PageItem* pg =
static_cast<PageItem*
>(item);
234 if (pg->pageNumber() == curPage)
239 int newPage = calcCurrentPage();
240 if (newPage != curPage)
244 QRectF target = pages.at(curPage-1)->sceneBoundingRect();
252 target = scene->itemsBoundingRect();
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());
263 graphicsView->ensureVisible(viewSceneRect);
268 int step =
qRound(graphicsView->matrix().mapRect(target).height());
269 graphicsView->verticalScrollBar()->setSingleStep(step);
270 graphicsView->verticalScrollBar()->setPageStep(step);
274 zoomFactor = graphicsView->transform().m11() * (float(printer->logicalDpiY()) / q->logicalDpiY());
275 emit q->previewChanged();
285 int newPage = calcCurrentPage();
286 if (newPage != curPage) {
288 emit q->previewChanged();
295 int newPage = curPage;
296 QRect viewRect = graphicsView->viewport()->rect();
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;
302 if (area > maxArea) {
304 newPage = pg->pageNumber();
305 }
else if (area == maxArea && pg->pageNumber() < newPage) {
306 newPage = pg->pageNumber();
316 graphicsView =
new GraphicsView;
317 graphicsView->setInteractive(
false);
321 q,
SLOT(_q_updateCurrentPage()));
325 scene->setBackgroundBrush(
Qt::gray);
326 graphicsView->setScene(scene);
329 q->setLayout(layout);
337 for (
int i = 0; i < pages.size(); i++)
338 scene->removeItem(pages.at(i));
342 int numPages = pictures.count();
344 QRect pageRect = printer->pageRect();
346 for (
int i = 0; i < numPages; i++) {
347 PageItem* item =
new PageItem(i+1, pictures.at(i),
paperSize, pageRect);
348 scene->addItem(item);
355 int numPages = pages.count();
359 int numPagePlaces = numPages;
372 int rows =
qCeil(
qreal(numPagePlaces) / cols);
374 qreal itemWidth = pages.at(0)->boundingRect().width();
375 qreal itemHeight = pages.at(0)->boundingRect().height();
377 for (
int i = 0; i < rows && pageNum <= numPages; i++) {
378 for (
int j = 0; j < cols && pageNum <= numPages; j++) {
383 pages.at(pageNum-1)->setPos(
QPointF(j*itemWidth, i*itemHeight));
388 scene->setSceneRect(scene->itemsBoundingRect());
400 printer->d_func()->setPreviewMode(
true);
401 emit q->paintRequested(printer);
402 printer->d_func()->setPreviewMode(
false);
403 pictures = printer->d_func()->previewPages();
406 curPage =
qBound(1, curPage, pages.count());
409 emit q->previewChanged();
414 if (pageNumber < 1 || pageNumber > pages.count())
417 int lastPage = curPage;
418 curPage = pageNumber;
420 if (lastPage != curPage && lastPage > 0 && lastPage <= pages.count()) {
422 QScrollBar *hsc = graphicsView->horizontalScrollBar();
423 QScrollBar *vsc = graphicsView->verticalScrollBar();
424 QPointF pt = graphicsView->transform().map(pages.at(curPage-1)->pos());
428 graphicsView->centerOn(pages.at(curPage-1));
436 graphicsView->scale(zoom, zoom);
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)));
537 d->printer = printer;
538 d->ownPrinter =
false;
557 d->ownPrinter =
true;
594 d->zoomFactor =
d->graphicsView->transform().m11() * (float(
d->printer->logicalDpiY()) /
logicalDpiY());
609 return d->printer->orientation();
619 d->printer->setOrientation(orientation);
620 d->generatePreview();
663 return d->zoomFactor;
678 d->setZoomFactor(factor);
692 return d->pages.size();
705 return d->pages.size();
724 d->setCurrentPage(page);
826 d->initialized =
true;
827 d->generatePreview();
828 d->graphicsView->updateGeometry();
836 if (visible && !
d->initialized)
862 #include "moc_qprintpreviewwidget.cpp" 863 #include "qprintpreviewwidget.moc" 865 #endif // QT_NO_PRINTPREVIEWWIDGET The QPainter class performs low-level painting on widgets and other paint devices.
The QColor class provides colors based on RGB, HSV or CMYK values.
void moveTop(qreal pos)
Moves the rectangle vertically, leaving the rectangle's top line at the given y coordinate.
The QGraphicsScene class provides a surface for managing a large number of 2D graphical items...
qreal right() const
Returns the x-coordinate of the rectangle's right edge.
void drawPath(const QPainterPath &path)
Draws the given painter path using the current pen for outline and the current brush for filling...
PaperSize paperSize(QPrinter::PaperSize paperSize)
void setColorAt(qreal pos, const QColor &color)
Creates a stop point at the given position with the given color.
#define QT_END_NAMESPACE
This macro expands to.
QPointer< QWidget > widget
void setLeft(qreal pos)
Sets the left edge of the rectangle to the given x coordinate.
The QPainterPath class provides a container for painting operations, enabling graphical shapes to be ...
virtual QRectF boundingRect() const =0
This pure virtual function defines the outer bounds of the item as a rectangle; all painting must be ...
void setClipRect(const QRectF &, Qt::ClipOperation op=Qt::ReplaceClip)
Enables clipping, and sets the clip region to the given rectangle using the given clip operation...
The QPointF class defines a point in the plane using floating point precision.
The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.
qreal left() const
Returns the x-coordinate of the rectangle's left edge.
void restore()
Restores the current painter state (pops a saved state off the stack).
Orientation
This enum type (not to be confused with Orientation) is used to specify each page's orientation...
int width() const
Returns the width of the rectangle.
int height() const
Returns the height of the rectangle.
The QRadialGradient class is used in combination with QBrush to specify a radial gradient brush...
void addWidget(QWidget *, int stretch=0, Qt::Alignment alignment=0)
Adds widget to the end of this box layout, with a stretch factor of stretch and alignment alignment...
The QPen class defines how a QPainter should draw lines and outlines of shapes.
The QSizeF class defines the size of a two-dimensional object using floating point precision...
void setRight(qreal pos)
Sets the right edge of the rectangle to the given x coordinate.
void save()
Saves the current painter state (pushes the state onto a stack).
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
qreal x() const
Returns the x-coordinate of this point.
static const QRectF boundingRect(const QPointF *points, int pointCount)
#define QT_BEGIN_NAMESPACE
This macro expands to.
The QRectF class defines a rectangle in the plane using floating point precision. ...
void drawPicture(const QPointF &p, const QPicture &picture)
Replays the given picture at the given point.
void setCacheMode(CacheMode mode, const QSize &cacheSize=QSize())
Sets the item's cache mode to mode.
void resizeEvent(QResizeEvent *event)
Reimplemented Function
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
The QPrinter class is a paint device that paints on a printer.
const T & at(int i) const
Returns the item at index position i in the list.
The QResizeEvent class contains event parameters for resize events.
void addRect(const QRectF &rect)
Adds the given rectangle to this path as a closed subpath.
qreal width() const
Returns the width of the rectangle.
void showEvent(QShowEvent *event)
Reimplemented Function
The QShowEvent class provides an event that is sent when a widget is shown.
The QBrush class defines the fill pattern of shapes drawn by QPainter.
#define Q_DECLARE_PUBLIC(Class)
The QLinearGradient class is used in combination with QBrush to specify a linear gradient brush...
QObject * parent() const
Returns a pointer to the parent object.
Q_DECL_CONSTEXPR const T & qBound(const T &min, const T &val, const T &max)
void setBrush(const QBrush &brush)
Sets the painter's brush to the given brush.
int size() const
Returns the number of items in the list.
void setPen(const QColor &color)
Sets the painter's pen to have style Qt::SolidLine, width 0 and the specified color.
The QRect class defines a rectangle in the plane using integer precision.
void drawRect(const QRectF &rect)
Draws the current rectangle with the current pen and brush.
qreal y() const
Returns the y-coordinate of this point.
void setContentsMargins(int left, int top, int right, int bottom)
Sets the left, top, right, and bottom margins to use around the layout.
The QGraphicsView class provides a widget for displaying the contents of a QGraphicsScene.
qreal top() const
Returns the y-coordinate of the rectangle's top edge.
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=0)=0
This function, which is usually called by QGraphicsView, paints the contents of an item in local coor...
The QSize class defines the size of a two-dimensional object using integer point precision.
The QVBoxLayout class lines up widgets vertically.
QRectF exposedRect
the exposed rectangle, in item coordinates
The QPicture class is a paint device that records and replays QPainter commands.
The QStyleOptionGraphicsItem class is used to describe the parameters needed to draw a QGraphicsItem...
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Q_OUTOFLINE_TEMPLATE void qDeleteAll(ForwardIterator begin, ForwardIterator end)
Q_DECL_CONSTEXPR int qRound(qreal d)
void fillRect(const QRectF &, const QBrush &)
Fills the given rectangle with the brush specified.
The QList class is a template class that provides lists.
static int area(const QSize &s)