From a32c6a9fd54fd400db0b729e826bc9a06297a3d7 Mon Sep 17 00:00:00 2001 From: ZackaryWelch Date: Tue, 26 Dec 2017 18:20:40 -0500 Subject: Began creating custom QPrintPreviewWidget for lumina-pdf --- src-qt5/desktop-utils/lumina-pdf/PrintWidget.h | 114 +++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 src-qt5/desktop-utils/lumina-pdf/PrintWidget.h (limited to 'src-qt5/desktop-utils/lumina-pdf/PrintWidget.h') diff --git a/src-qt5/desktop-utils/lumina-pdf/PrintWidget.h b/src-qt5/desktop-utils/lumina-pdf/PrintWidget.h new file mode 100644 index 00000000..061aef49 --- /dev/null +++ b/src-qt5/desktop-utils/lumina-pdf/PrintWidget.h @@ -0,0 +1,114 @@ +//=========================================== +// Lumina Desktop source code +// Copyright (c) 2017, Ken Moore +// Available under the 3-clause BSD license +// See the LICENSE file for full details +//=========================================== +// Simple subclass of QPrintPreviewWidget to provide +// notification when a context menu is requested +//=========================================== +#ifndef _PRINT_PREVIEW_WIDGET_H +#define _PRINT_PREVIEW_WIDGET_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +class GraphicsView : public QGraphicsView +{ + Q_OBJECT +public: + GraphicsView(QWidget *parent = 0) : QGraphicsView(parent) { } +signals: + void resized(); +protected: + void resizeEvent(QResizeEvent* e) Q_DECL_OVERRIDE { + /*{ + const QSignalBlocker blocker(verticalScrollBar()); // Don't change page, QTBUG-14517 + QGraphicsView::resizeEvent(e); + }*/ + QGraphicsView::resizeEvent(e); + emit resized(); + } + + void showEvent(QShowEvent* e) Q_DECL_OVERRIDE { + QGraphicsView::showEvent(e); + emit resized(); + } +}; + +class PrintWidget: public QWidget{ + Q_OBJECT +public: + enum ViewMode { + SinglePageView, + FacingPagesView, + AllPagesView + }; + + enum ZoomMode { + CustomZoom, + FitToWidth, + FitInView + }; + + PrintWidget(QPrinter *printer = 0, QWidget *parent = 0); + ~PrintWidget(); + + int currentPage() const { return curPage; }; + double getZoomFactor() const { return this->zoomFactor; }; + QPrinter::Orientation orientation() const { return printer->orientation(); }; + ZoomMode getZoomMode() const { return this->zoomMode; }; + void setVisible(bool); + +public slots: + void zoomIn(double factor=1.2); + void zoomOut(double factor=1.2); + void setOrientation(QPrinter::Orientation); + void setCurrentPage(int); + + void fitInView(); + void fitToWidget(); + void setAllPagesViewMode(); + void setSinglePageViewMode(); + void setFacingPagesViewMode(); + + void updatePreview(); + +private slots: + void updateCurrentPage(); + int calcCurrentPage(); + void fit(bool doFitting=false); + +signals: + void customContextMenuRequested(const QPoint&); + void paintRequested(QPrinter*); + void previewChanged(); + +private: + void generatePreview(); + void layoutPages(); + void populateScene(); + void setViewMode(ViewMode viewMode); + void setZoomMode(ZoomMode viewMode); + + QPrinter *printer; + GraphicsView *graphicsView; + QGraphicsScene *scene; + int curPage; + QList pictures; + QList pages; + + ViewMode viewMode; + ZoomMode zoomMode; + double zoomFactor; + bool initialized, fitting; +}; + +#endif -- cgit From 7e7f6f168a0528a19af6663e3c2b6fe985836a36 Mon Sep 17 00:00:00 2001 From: ZackaryWelch Date: Wed, 27 Dec 2017 15:59:26 -0500 Subject: Shifted from a custom QWidget to a subclass of QGraphicalView and connected it to the Main UI --- src-qt5/desktop-utils/lumina-pdf/PrintWidget.h | 157 +++++++++++++++++-------- 1 file changed, 107 insertions(+), 50 deletions(-) (limited to 'src-qt5/desktop-utils/lumina-pdf/PrintWidget.h') diff --git a/src-qt5/desktop-utils/lumina-pdf/PrintWidget.h b/src-qt5/desktop-utils/lumina-pdf/PrintWidget.h index 061aef49..81e4e794 100644 --- a/src-qt5/desktop-utils/lumina-pdf/PrintWidget.h +++ b/src-qt5/desktop-utils/lumina-pdf/PrintWidget.h @@ -7,8 +7,8 @@ // Simple subclass of QPrintPreviewWidget to provide // notification when a context menu is requested //=========================================== -#ifndef _PRINT_PREVIEW_WIDGET_H -#define _PRINT_PREVIEW_WIDGET_H +#ifndef _PRINT_GRAPHICS_H +#define _PRINT_GRAPHICS_H #include #include @@ -19,33 +19,85 @@ #include #include #include - -class GraphicsView : public QGraphicsView +namespace { +class PageItem : public QGraphicsItem { - Q_OBJECT public: - GraphicsView(QWidget *parent = 0) : QGraphicsView(parent) { } -signals: - void resized(); -protected: - void resizeEvent(QResizeEvent* e) Q_DECL_OVERRIDE { - /*{ - const QSignalBlocker blocker(verticalScrollBar()); // Don't change page, QTBUG-14517 - QGraphicsView::resizeEvent(e); - }*/ - QGraphicsView::resizeEvent(e); - emit resized(); - } + PageItem(int _pageNum, const QImage _pagePicture, QSize _paperSize, QRect _pageRect) + : pageNum(_pageNum), pagePicture(_pagePicture), + paperSize(_paperSize), pageRect(_pageRect) + { + qreal border = qMax(paperSize.height(), paperSize.width()) / 25; + brect = QRectF(QPointF(-border, -border), + QSizeF(paperSize)+QSizeF(2*border, 2*border)); + setCacheMode(DeviceCoordinateCache); + } - void showEvent(QShowEvent* e) Q_DECL_OVERRIDE { - QGraphicsView::showEvent(e); - emit resized(); - } + QRectF boundingRect() const Q_DECL_OVERRIDE + { return brect; } + + inline int pageNumber() const + { return pageNum; } + + void paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget) Q_DECL_OVERRIDE; + +private: + int pageNum; + const QImage pagePicture; + QSize paperSize; + QRect pageRect; + QRectF brect; }; -class PrintWidget: public QWidget{ +void PageItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + Q_UNUSED(widget); + QRectF paperRect(0,0, paperSize.width(), paperSize.height()); + + // Draw shadow + painter->setClipRect(option->exposedRect); + qreal shWidth = paperRect.width()/100; + QRectF rshadow(paperRect.topRight() + QPointF(0, shWidth), + paperRect.bottomRight() + QPointF(shWidth, 0)); + QLinearGradient rgrad(rshadow.topLeft(), rshadow.topRight()); + rgrad.setColorAt(0.0, QColor(0,0,0,255)); + rgrad.setColorAt(1.0, QColor(0,0,0,0)); + painter->fillRect(rshadow, QBrush(rgrad)); + QRectF bshadow(paperRect.bottomLeft() + QPointF(shWidth, 0), + paperRect.bottomRight() + QPointF(0, shWidth)); + QLinearGradient bgrad(bshadow.topLeft(), bshadow.bottomLeft()); + bgrad.setColorAt(0.0, QColor(0,0,0,255)); + bgrad.setColorAt(1.0, QColor(0,0,0,0)); + painter->fillRect(bshadow, QBrush(bgrad)); + QRectF cshadow(paperRect.bottomRight(), + paperRect.bottomRight() + QPointF(shWidth, shWidth)); + QRadialGradient cgrad(cshadow.topLeft(), shWidth, cshadow.topLeft()); + cgrad.setColorAt(0.0, QColor(0,0,0,255)); + cgrad.setColorAt(1.0, QColor(0,0,0,0)); + painter->fillRect(cshadow, QBrush(cgrad)); + + painter->setClipRect(paperRect & option->exposedRect); + painter->fillRect(paperRect, Qt::white); + if (pagePicture.isNull()) + return; + painter->drawImage(pageRect.topLeft(), pagePicture); + + // Effect: make anything drawn in the margins look washed out. + QPainterPath path; + path.addRect(paperRect); + path.addRect(pageRect); + painter->setPen(QPen(Qt::NoPen)); + painter->setBrush(QColor(255, 255, 255, 180)); + painter->drawPath(path); +} +} + +class PrintWidget : public QGraphicsView +{ Q_OBJECT public: + PrintWidget(QPrinter *printer, QWidget *parent = 0); + ~PrintWidget(); enum ViewMode { SinglePageView, FacingPagesView, @@ -58,57 +110,62 @@ public: FitInView }; - PrintWidget(QPrinter *printer = 0, QWidget *parent = 0); - ~PrintWidget(); - - int currentPage() const { return curPage; }; double getZoomFactor() const { return this->zoomFactor; }; - QPrinter::Orientation orientation() const { return printer->orientation(); }; ZoomMode getZoomMode() const { return this->zoomMode; }; - void setVisible(bool); + int currentPage() const { return curPage; }; + void setPictures(QHash*); +signals: + void resized(); + void customContextMenuRequested(const QPoint&); + void paintRequested(QPrinter*); public slots: - void zoomIn(double factor=1.2); + void zoomIn(double factor=1.2); void zoomOut(double factor=1.2); - void setOrientation(QPrinter::Orientation); void setCurrentPage(int); + void setVisible(bool) Q_DECL_OVERRIDE; - void fitInView(); - void fitToWidget(); + void fitView(); + void fitToWidth(); void setAllPagesViewMode(); void setSinglePageViewMode(); void setFacingPagesViewMode(); - void updatePreview(); - -private slots: - void updateCurrentPage(); + void updateCurrentPage(); int calcCurrentPage(); void fit(bool doFitting=false); + void updatePreview(); +protected: + void resizeEvent(QResizeEvent* e) Q_DECL_OVERRIDE { + /*{ + const QSignalBlocker blocker(verticalScrollBar()); // Don't change page, QTBUG-14517 + QGraphicsView::resizeEvent(e); + }*/ + QGraphicsView::resizeEvent(e); + emit resized(); + } -signals: - void customContextMenuRequested(const QPoint&); - void paintRequested(QPrinter*); - void previewChanged(); - + void showEvent(QShowEvent* e) Q_DECL_OVERRIDE { + QGraphicsView::showEvent(e); + emit resized(); + } private: void generatePreview(); void layoutPages(); void populateScene(); - void setViewMode(ViewMode viewMode); - void setZoomMode(ZoomMode viewMode); - - QPrinter *printer; - GraphicsView *graphicsView; + void setViewMode(ViewMode); + void setZoomMode(ZoomMode); QGraphicsScene *scene; - int curPage; - QList pictures; - QList pages; + QPrinter *printer; + QPreviewPaintEngine *previewEngine; + QPdfPrintEngine *pdfEngine; + int curPage; ViewMode viewMode; ZoomMode zoomMode; double zoomFactor; bool initialized, fitting; + QList pages; + QHash *pictures; }; - #endif -- cgit