2015-04-21 16:05:20 +00:00
|
|
|
#include "printer.h"
|
|
|
|
#include "templatelayout.h"
|
|
|
|
|
|
|
|
#include <QtWebKitWidgets>
|
|
|
|
#include <QPainter>
|
2015-06-10 17:31:19 +00:00
|
|
|
#include <QWebElementCollection>
|
|
|
|
#include <QWebElement>
|
2015-04-21 16:05:20 +00:00
|
|
|
|
2015-06-14 04:25:35 +00:00
|
|
|
Printer::Printer(QPrinter *printer, print_options *printOptions)
|
2015-04-21 16:05:20 +00:00
|
|
|
{
|
|
|
|
this->printer = printer;
|
2015-06-14 04:25:35 +00:00
|
|
|
this->printOptions = printOptions;
|
2015-06-22 13:42:02 +00:00
|
|
|
dpi = 0;
|
2015-05-30 13:32:15 +00:00
|
|
|
done = 0;
|
2015-04-21 16:05:20 +00:00
|
|
|
}
|
|
|
|
|
2015-06-10 17:31:19 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-04-21 16:05:20 +00:00
|
|
|
void Printer::render()
|
|
|
|
{
|
2015-06-22 22:10:32 +00:00
|
|
|
// apply user settings
|
|
|
|
int divesPerPage;
|
|
|
|
if (printOptions->color_selected && printer->colorMode()) {
|
|
|
|
printer->setColorMode(QPrinter::Color);
|
|
|
|
} else {
|
|
|
|
printer->setColorMode(QPrinter::GrayScale);
|
|
|
|
}
|
|
|
|
switch (printOptions->p_template) {
|
|
|
|
case print_options::ONE_DIVE:
|
|
|
|
divesPerPage = 1;
|
|
|
|
break;
|
|
|
|
case print_options::TWO_DIVE:
|
|
|
|
divesPerPage = 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
int Pages = ceil(getTotalWork(printOptions) / (float)divesPerPage);
|
2015-06-10 17:31:19 +00:00
|
|
|
|
|
|
|
// keep original preferences
|
2015-06-22 22:10:32 +00:00
|
|
|
QPointer<ProfileWidget2> profile = MainWindow::instance()->graphics();
|
2015-06-10 17:31:19 +00:00
|
|
|
int profileFrameStyle = profile->frameStyle();
|
|
|
|
int animationOriginal = prefs.animation_speed;
|
|
|
|
double fontScale = profile->getFontPrintScale();
|
|
|
|
|
|
|
|
// apply printing settings to profile
|
|
|
|
profile->setFrameStyle(QFrame::NoFrame);
|
2015-06-14 04:34:58 +00:00
|
|
|
profile->setPrintMode(true, !printOptions->color_selected);
|
2015-06-22 20:46:39 +00:00
|
|
|
profile->setFontPrintScale(printer->pageLayout().paintRect(QPageLayout::Inch).width() * dpi * 0.001);
|
2015-06-10 17:31:19 +00:00
|
|
|
profile->setToolTipVisibile(false);
|
|
|
|
prefs.animation_speed = 0;
|
|
|
|
|
|
|
|
// render the Qwebview
|
2015-04-21 16:05:20 +00:00
|
|
|
QPainter painter;
|
2015-06-17 15:05:14 +00:00
|
|
|
QRect viewPort(0, 0, pageSize.width(), pageSize.height());
|
2015-04-21 16:05:20 +00:00
|
|
|
painter.begin(printer);
|
|
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
|
|
|
painter.setRenderHint(QPainter::SmoothPixmapTransform);
|
2015-06-14 04:34:58 +00:00
|
|
|
|
2015-06-10 17:31:19 +00:00
|
|
|
// get all refereces to diveprofile class in the Html template
|
|
|
|
QWebElementCollection collection = webView->page()->mainFrame()->findAllElements(".diveprofile");
|
|
|
|
|
|
|
|
QSize originalSize = profile->size();
|
|
|
|
if (collection.count() > 0) {
|
|
|
|
profile->resize(collection.at(0).geometry().size());
|
|
|
|
}
|
|
|
|
|
|
|
|
int elemNo = 0;
|
2015-04-21 16:05:20 +00:00
|
|
|
for (int i = 0; i < Pages; i++) {
|
2015-06-10 17:31:19 +00:00
|
|
|
// render the base Html template
|
2015-04-21 16:05:20 +00:00
|
|
|
webView->page()->mainFrame()->render(&painter, QWebFrame::ContentsLayer);
|
2015-06-10 17:31:19 +00:00
|
|
|
|
|
|
|
// 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
|
2015-06-17 15:05:14 +00:00
|
|
|
webView->page()->mainFrame()->scroll(0, pageSize.height());
|
|
|
|
viewPort.adjust(0, pageSize.height(), 0, pageSize.height());
|
2015-06-10 17:31:19 +00:00
|
|
|
|
|
|
|
// rendering progress is 4/5 of total work
|
2015-05-30 13:32:15 +00:00
|
|
|
emit(progessUpdated((i * 80.0 / Pages) + done));
|
2015-04-21 16:05:20 +00:00
|
|
|
if (i < Pages - 1)
|
|
|
|
printer->newPage();
|
|
|
|
}
|
|
|
|
painter.end();
|
2015-06-10 17:31:19 +00:00
|
|
|
|
|
|
|
// 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);
|
2015-04-21 16:05:20 +00:00
|
|
|
}
|
|
|
|
|
2015-05-30 13:32:15 +00:00
|
|
|
//value: ranges from 0 : 100 and shows the progress of the templating engine
|
|
|
|
void Printer::templateProgessUpdated(int value)
|
|
|
|
{
|
|
|
|
done = value / 5; //template progess if 1/5 of total work
|
|
|
|
emit progessUpdated(done);
|
|
|
|
}
|
|
|
|
|
2015-04-21 16:05:20 +00:00
|
|
|
void Printer::print()
|
|
|
|
{
|
2015-06-14 04:25:35 +00:00
|
|
|
TemplateLayout t(printOptions);
|
2015-04-21 16:05:20 +00:00
|
|
|
webView = new QWebView();
|
2015-06-17 15:05:14 +00:00
|
|
|
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);
|
2015-04-21 16:05:20 +00:00
|
|
|
webView->setHtml(t.generate());
|
|
|
|
render();
|
|
|
|
}
|