forked from subsurface/subsurface
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Printing: Add Printer class that holds the rendering logic.
Render Html pages into a QWebView then print it using QPainter. the Printer::print() is called that prepare the HTML file to be rendered by the QWebView. Printer::render() will do the rendering task. Signed-off-by: Gehad elrobey <gehadelrobey@gmail.com> Signed-off-by: Lubomir I. Ivanov <neolit123@gmail.com>
- Loading branch information
1 parent
5e2d3d6
commit 3f3937f
Showing
4 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include "printer.h" | ||
#include "templatelayout.h" | ||
|
||
#include <QtWebKitWidgets> | ||
#include <QPainter> | ||
|
||
#define A4_300DPI_WIDTH 2480 | ||
#define A4_300DPI_HIGHT 3508 | ||
|
||
Printer::Printer(QPrinter *printer) | ||
{ | ||
this->printer = printer; | ||
|
||
//override these settings for now. | ||
printer->setFullPage(true); | ||
printer->setOrientation(QPrinter::Portrait); | ||
printer->setPaperSize(QPrinter::A4); | ||
printer->setPrintRange(QPrinter::AllPages); | ||
printer->setResolution(300); | ||
} | ||
|
||
void Printer::render() | ||
{ | ||
QPainter painter; | ||
QSize size(A4_300DPI_WIDTH, A4_300DPI_HIGHT); | ||
painter.begin(printer); | ||
painter.setRenderHint(QPainter::Antialiasing); | ||
painter.setRenderHint(QPainter::SmoothPixmapTransform); | ||
|
||
webView->page()->setViewportSize(size); | ||
|
||
int Pages = ceil((float)webView->page()->mainFrame()->contentsSize().rheight() / A4_300DPI_HIGHT); | ||
for (int i = 0; i < Pages; i++) { | ||
webView->page()->mainFrame()->render(&painter, QWebFrame::ContentsLayer); | ||
webView->page()->mainFrame()->scroll(0, A4_300DPI_HIGHT); | ||
if (i < Pages - 1) | ||
printer->newPage(); | ||
} | ||
painter.end(); | ||
} | ||
|
||
void Printer::print() | ||
{ | ||
TemplateLayout t; | ||
webView = new QWebView(); | ||
webView->setHtml(t.generate()); | ||
render(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#ifndef PRINTER_H | ||
#define PRINTER_H | ||
|
||
#include <QPrinter> | ||
#include <QWebView> | ||
|
||
class Printer : public QObject { | ||
Q_OBJECT | ||
|
||
private: | ||
QPrinter *printer; | ||
QWebView *webView; | ||
void render(); | ||
|
||
public: | ||
Printer(QPrinter *printer); | ||
void print(); | ||
}; | ||
|
||
#endif //PRINTER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters