From 3a6963836682aa0e0e2825db4eaac7ea48a9939b Mon Sep 17 00:00:00 2001 From: Gehad elrobey Date: Fri, 10 Jul 2015 21:30:18 +0200 Subject: Printing: check for different printing modes Add PRINT/PREVIEW print modes, check for printing modes before casting. We must pass a QPaintDevice with type QPixmap for previewing and with type QPrinter for actual printing. Signed-off-by: Gehad elrobey Signed-off-by: Lubomir I. Ivanov --- printer.cpp | 10 ++++++++-- printer.h | 9 ++++++++- qt-ui/printdialog.cpp | 2 +- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/printer.cpp b/printer.cpp index da5d5142d..c5867450f 100644 --- a/printer.cpp +++ b/printer.cpp @@ -6,11 +6,12 @@ #include #include -Printer::Printer(QPaintDevice *paintDevice, print_options *printOptions, template_options *templateOptions) +Printer::Printer(QPaintDevice *paintDevice, print_options *printOptions, template_options *templateOptions, PrintMode printMode) { this->paintDevice = paintDevice; this->printOptions = printOptions; this->templateOptions = templateOptions; + this->printMode = printMode; dpi = 0; done = 0; webView = new QWebView(); @@ -80,7 +81,7 @@ void Printer::render(int Pages = 0) // rendering progress is 4/5 of total work emit(progessUpdated((i * 80.0 / Pages) + done)); - if (i < Pages - 1) + if (i < Pages - 1 && printMode == Printer::PRINT) static_cast(paintDevice)->newPage(); } painter.end(); @@ -106,6 +107,11 @@ void Printer::templateProgessUpdated(int value) void Printer::print() { + // we can only print if "PRINT" mode is selected + if (printMode != Printer::PRINT) { + return; + } + QPrinter *printerPtr; printerPtr = static_cast(paintDevice); diff --git a/printer.h b/printer.h index caeb14a1f..dee9ba980 100644 --- a/printer.h +++ b/printer.h @@ -13,12 +13,19 @@ class Printer : public QObject { Q_OBJECT +public: + enum PrintMode { + PRINT, + PREVIEW + }; + private: QPaintDevice *paintDevice; QWebView *webView; print_options *printOptions; template_options *templateOptions; QSize pageSize; + PrintMode printMode; int done; int dpi; void render(int Pages); @@ -28,7 +35,7 @@ private slots: void templateProgessUpdated(int value); public: - Printer(QPaintDevice *paintDevice, print_options *printOptions, template_options *templateOptions); + Printer(QPaintDevice *paintDevice, print_options *printOptions, template_options *templateOptions, PrintMode printMode); ~Printer(); void print(); diff --git a/qt-ui/printdialog.cpp b/qt-ui/printdialog.cpp index 9ff29e67b..0a2c7898f 100644 --- a/qt-ui/printdialog.cpp +++ b/qt-ui/printdialog.cpp @@ -45,7 +45,7 @@ PrintDialog::PrintDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f optionsWidget = new PrintOptions(this, &printOptions, &templateOptions); // create a new printer object - printer = new Printer(&qprinter, &printOptions, &templateOptions); + printer = new Printer(&qprinter, &printOptions, &templateOptions, Printer::PRINT); QVBoxLayout *layout = new QVBoxLayout(this); setLayout(layout); -- cgit v1.2.3-70-g09d2