Skip to content

Commit

Permalink
Printing: change QPrinter to parent class QPaintDevice
Browse files Browse the repository at this point in the history
Use general class QPaintDevice to be used for printing and
previewing instances, printing uses a QPrinter object while
previewing uses a QPixmap instance. We use static_cast to use the
needed object.

Signed-off-by: Gehad elrobey <gehadelrobey@gmail.com>
  • Loading branch information
Gehadelrobey committed Jul 13, 2015
1 parent ab4d4d5 commit 1fd0cf1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
24 changes: 13 additions & 11 deletions printer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
#include <QWebElementCollection>
#include <QWebElement>

Printer::Printer(QPrinter *printer, print_options *printOptions, template_options *templateOptions)
Printer::Printer(QPaintDevice *paintDevice, print_options *printOptions, template_options *templateOptions)
{
this->printer = printer;
this->paintDevice = paintDevice;
this->printOptions = printOptions;
this->templateOptions = templateOptions;
dpi = 0;
Expand Down Expand Up @@ -49,7 +49,7 @@ void Printer::render(int Pages = 0)
// render the Qwebview
QPainter painter;
QRect viewPort(0, 0, pageSize.width(), pageSize.height());
painter.begin(printer);
painter.begin(paintDevice);
painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::SmoothPixmapTransform);

Expand Down Expand Up @@ -81,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)
printer->newPage();
static_cast<QPrinter*>(paintDevice)->newPage();
}
painter.end();

Expand All @@ -106,19 +106,21 @@ void Printer::templateProgessUpdated(int value)

void Printer::print()
{
QPrinter *printerPtr;
printerPtr = static_cast<QPrinter*>(paintDevice);

TemplateLayout t(printOptions, templateOptions);
connect(&t, SIGNAL(progressUpdated(int)), this, SLOT(templateProgessUpdated(int)));

dpi = printer->resolution();
dpi = printerPtr->resolution();
//rendering resolution = selected paper size in inchs * printer dpi
pageSize.setHeight(printer->pageLayout().paintRect(QPageLayout::Inch).height() * dpi);
pageSize.setWidth(printer->pageLayout().paintRect(QPageLayout::Inch).width() * dpi);
pageSize.setHeight(printerPtr->pageLayout().paintRect(QPageLayout::Inch).height() * dpi);
pageSize.setWidth(printerPtr->pageLayout().paintRect(QPageLayout::Inch).width() * dpi);
webView->page()->setViewportSize(pageSize);
webView->setHtml(t.generate());
if (printOptions->color_selected && printer->colorMode()) {
printer->setColorMode(QPrinter::Color);
if (printOptions->color_selected && printerPtr->colorMode()) {
printerPtr->setColorMode(QPrinter::Color);
} else {
printer->setColorMode(QPrinter::GrayScale);
printerPtr->setColorMode(QPrinter::GrayScale);
}
// apply user settings
int divesPerPage;
Expand Down
4 changes: 2 additions & 2 deletions printer.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Printer : public QObject {
Q_OBJECT

private:
QPrinter *printer;
QPaintDevice *paintDevice;
QWebView *webView;
print_options *printOptions;
template_options *templateOptions;
Expand All @@ -28,7 +28,7 @@ private slots:
void templateProgessUpdated(int value);

public:
Printer(QPrinter *printer, print_options *printOptions, template_options *templateOptions);
Printer(QPaintDevice *paintDevice, print_options *printOptions, template_options *templateOptions);
~Printer();
void print();

Expand Down

0 comments on commit 1fd0cf1

Please sign in to comment.