summaryrefslogtreecommitdiffstats
path: root/printer.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'printer.cpp')
-rw-r--r--printer.cpp101
1 files changed, 83 insertions, 18 deletions
diff --git a/printer.cpp b/printer.cpp
index 5d340055e..9e5a34b57 100644
--- a/printer.cpp
+++ b/printer.cpp
@@ -3,43 +3,102 @@
#include <QtWebKitWidgets>
#include <QPainter>
+#include <QWebElementCollection>
+#include <QWebElement>
-#define A4_300DPI_WIDTH 2480
-#define A4_300DPI_HIGHT 3508
-
-Printer::Printer(QPrinter *printer)
+Printer::Printer(QPrinter *printer, print_options *printOptions)
{
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);
+ this->printOptions = printOptions;
done = 0;
}
+void Printer::putProfileImage(QRect profilePlaceholder, QRect viewPort, QPainter *painter, struct dive *dive, QPointer<ProfileWidget2> profile)
+{
+ int x = profilePlaceholder.x() - viewPort.x();
+ int y = profilePlaceholder.y() - viewPort.y();
+ // use the placeHolder and the viewPort position to calculate the relative position of the dive profile.
+ QRect pos(x, y, profilePlaceholder.width(), profilePlaceholder.height());
+ profile->plotDive(dive, true);
+ profile->render(painter, pos);
+}
+
void Printer::render()
{
+ QPointer<ProfileWidget2> profile = MainWindow::instance()->graphics();
+
+ // keep original preferences
+ int profileFrameStyle = profile->frameStyle();
+ int animationOriginal = prefs.animation_speed;
+ double fontScale = profile->getFontPrintScale();
+
+ // apply printing settings to profile
+ profile->setFrameStyle(QFrame::NoFrame);
+ profile->setPrintMode(true, !printOptions->color_selected);
+ profile->setFontPrintScale(0.6);
+ profile->setToolTipVisibile(false);
+ prefs.animation_speed = 0;
+
+ // render the Qwebview
QPainter painter;
- QSize size(A4_300DPI_WIDTH, A4_300DPI_HIGHT);
+ QRect viewPort(0, 0, pageSize.width(), pageSize.height());
painter.begin(printer);
painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::SmoothPixmapTransform);
- webView->page()->setViewportSize(size);
+ int divesPerPage;
+ switch (printOptions->p_template) {
+ case print_options::ONE_DIVE:
+ divesPerPage = 1;
+ break;
+ case print_options::TWO_DIVE:
+ divesPerPage = 2;
+ break;
+ }
+ int Pages = ceil(getTotalWork() / (float)divesPerPage);
+
+ // get all refereces to diveprofile class in the Html template
+ QWebElementCollection collection = webView->page()->mainFrame()->findAllElements(".diveprofile");
- int Pages = ceil((float)webView->page()->mainFrame()->contentsSize().rheight() / A4_300DPI_HIGHT);
+ QSize originalSize = profile->size();
+ if (collection.count() > 0) {
+ profile->resize(collection.at(0).geometry().size());
+ }
+
+ int elemNo = 0;
for (int i = 0; i < Pages; i++) {
+ // render the base Html template
webView->page()->mainFrame()->render(&painter, QWebFrame::ContentsLayer);
- webView->page()->mainFrame()->scroll(0, A4_300DPI_HIGHT);
- //rendering progress is 4/5 of total work
+
+ // render all the dive profiles in the current page
+ while (elemNo < collection.count() && collection.at(elemNo).geometry().y() < viewPort.y() + viewPort.height()) {
+ // dive id field should be dive_{{dive_no}} se we remove the first 5 characters
+ int diveNo = collection.at(elemNo).attribute("id").remove(0, 5).toInt(0, 10);
+ putProfileImage(collection.at(elemNo).geometry(), viewPort, &painter, get_dive(diveNo - 1), profile);
+ elemNo++;
+ }
+
+ // scroll the webview to the next page
+ webView->page()->mainFrame()->scroll(0, pageSize.height());
+ viewPort.adjust(0, pageSize.height(), 0, pageSize.height());
+
+ // rendering progress is 4/5 of total work
emit(progessUpdated((i * 80.0 / Pages) + done));
if (i < Pages - 1)
printer->newPage();
}
painter.end();
+
+ // return profle settings
+ profile->setFrameStyle(profileFrameStyle);
+ profile->setPrintMode(false);
+ profile->setFontPrintScale(fontScale);
+ profile->setToolTipVisibile(true);
+ profile->resize(originalSize);
+ prefs.animation_speed = animationOriginal;
+
+ //replot the dive after returning the settings
+ profile->plotDive(0, true);
}
//value: ranges from 0 : 100 and shows the progress of the templating engine
@@ -51,9 +110,15 @@ void Printer::templateProgessUpdated(int value)
void Printer::print()
{
- TemplateLayout t;
- connect(&t, SIGNAL(progressUpdated(int)), this, SLOT(templateProgessUpdated(int)));
+ TemplateLayout t(printOptions);
webView = new QWebView();
+ connect(&t, SIGNAL(progressUpdated(int)), this, SLOT(templateProgessUpdated(int)));
+
+ dpi = printer->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);
+ webView->page()->setViewportSize(pageSize);
webView->setHtml(t.generate());
render();
}