aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Gehad elrobey <gehadelrobey@gmail.com>2015-04-21 18:05:20 +0200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2015-06-04 10:07:35 -0700
commit6de5cfb7a400897e6644668ffa9d0d4926b74590 (patch)
treeb11159f2375dc2d91e44e38863b7aa28a31885a5
parente2ad38189eb81eda213f775302efb44d2ed70c85 (diff)
downloadsubsurface-6de5cfb7a400897e6644668ffa9d0d4926b74590.tar.gz
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> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
-rw-r--r--CMakeLists.txt1
-rw-r--r--printer.cpp48
-rw-r--r--printer.h20
-rw-r--r--subsurface.pro2
4 files changed, 71 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index a93ccbab5..34fc57659 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -261,6 +261,7 @@ set(SUBSURFACE_CORE_LIB_SRCS
configuredivecomputerthreads.cpp
divesitehelpers.cpp
templatelayout.cpp
+ printer.cpp
${PLATFORM_SRC}
)
source_group("Subsurface Core" FILES ${SUBSURFACE_CORE_LIB_SRCS})
diff --git a/printer.cpp b/printer.cpp
new file mode 100644
index 000000000..01c1f5546
--- /dev/null
+++ b/printer.cpp
@@ -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();
+}
diff --git a/printer.h b/printer.h
new file mode 100644
index 000000000..3b574d126
--- /dev/null
+++ b/printer.h
@@ -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
diff --git a/subsurface.pro b/subsurface.pro
index 509feb7bc..fd4665cb3 100644
--- a/subsurface.pro
+++ b/subsurface.pro
@@ -68,6 +68,7 @@ HEADERS = \
units.h \
divecomputer.h \
templatelayout.h \
+ printer.h \
qt-ui/about.h \
qt-ui/completionmodels.h \
qt-ui/divecomputermanagementdialog.h \
@@ -162,6 +163,7 @@ SOURCES = \
gaspressures.c \
divecomputer.cpp \
templatelayout.cpp \
+ printer.cpp \
worldmap-save.c \
save-html.c \
qt-gui.cpp \