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 <gehadelrobey@gmail.com>
Signed-off-by: Lubomir I. Ivanov <neolit123@gmail.com>
This commit is contained in:
Gehad elrobey 2015-07-10 21:30:18 +02:00 committed by Lubomir I. Ivanov
parent 142fd950c8
commit 3a69638366
3 changed files with 17 additions and 4 deletions

View file

@ -6,11 +6,12 @@
#include <QWebElementCollection> #include <QWebElementCollection>
#include <QWebElement> #include <QWebElement>
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->paintDevice = paintDevice;
this->printOptions = printOptions; this->printOptions = printOptions;
this->templateOptions = templateOptions; this->templateOptions = templateOptions;
this->printMode = printMode;
dpi = 0; dpi = 0;
done = 0; done = 0;
webView = new QWebView(); webView = new QWebView();
@ -80,7 +81,7 @@ void Printer::render(int Pages = 0)
// rendering progress is 4/5 of total work // rendering progress is 4/5 of total work
emit(progessUpdated((i * 80.0 / Pages) + done)); emit(progessUpdated((i * 80.0 / Pages) + done));
if (i < Pages - 1) if (i < Pages - 1 && printMode == Printer::PRINT)
static_cast<QPrinter*>(paintDevice)->newPage(); static_cast<QPrinter*>(paintDevice)->newPage();
} }
painter.end(); painter.end();
@ -106,6 +107,11 @@ void Printer::templateProgessUpdated(int value)
void Printer::print() void Printer::print()
{ {
// we can only print if "PRINT" mode is selected
if (printMode != Printer::PRINT) {
return;
}
QPrinter *printerPtr; QPrinter *printerPtr;
printerPtr = static_cast<QPrinter*>(paintDevice); printerPtr = static_cast<QPrinter*>(paintDevice);

View file

@ -13,12 +13,19 @@
class Printer : public QObject { class Printer : public QObject {
Q_OBJECT Q_OBJECT
public:
enum PrintMode {
PRINT,
PREVIEW
};
private: private:
QPaintDevice *paintDevice; QPaintDevice *paintDevice;
QWebView *webView; QWebView *webView;
print_options *printOptions; print_options *printOptions;
template_options *templateOptions; template_options *templateOptions;
QSize pageSize; QSize pageSize;
PrintMode printMode;
int done; int done;
int dpi; int dpi;
void render(int Pages); void render(int Pages);
@ -28,7 +35,7 @@ private slots:
void templateProgessUpdated(int value); void templateProgessUpdated(int value);
public: public:
Printer(QPaintDevice *paintDevice, print_options *printOptions, template_options *templateOptions); Printer(QPaintDevice *paintDevice, print_options *printOptions, template_options *templateOptions, PrintMode printMode);
~Printer(); ~Printer();
void print(); void print();

View file

@ -45,7 +45,7 @@ PrintDialog::PrintDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f
optionsWidget = new PrintOptions(this, &printOptions, &templateOptions); optionsWidget = new PrintOptions(this, &printOptions, &templateOptions);
// create a new printer object // create a new printer object
printer = new Printer(&qprinter, &printOptions, &templateOptions); printer = new Printer(&qprinter, &printOptions, &templateOptions, Printer::PRINT);
QVBoxLayout *layout = new QVBoxLayout(this); QVBoxLayout *layout = new QVBoxLayout(this);
setLayout(layout); setLayout(layout);