Printing: render the dive profile on top of QWebView

We render the dive profile over the QWebView to the QPainter. This helps
us not to save the SVG images to disk and then render them again to the
webview.

Signed-off-by: Gehad elrobey <gehadelrobey@gmail.com>
Signed-off-by: Lubomir I. Ivanov <neolit123@gmail.com>
This commit is contained in:
Gehad elrobey 2015-06-10 19:31:19 +02:00 committed by Lubomir I. Ivanov
parent 1040f0d4e5
commit cd30e11672
3 changed files with 70 additions and 5 deletions

View file

@ -3,6 +3,8 @@
#include <QtWebKitWidgets> #include <QtWebKitWidgets>
#include <QPainter> #include <QPainter>
#include <QWebElementCollection>
#include <QWebElement>
#define A4_300DPI_WIDTH 2480 #define A4_300DPI_WIDTH 2480
#define A4_300DPI_HIGHT 3508 #define A4_300DPI_HIGHT 3508
@ -20,26 +22,85 @@ Printer::Printer(QPrinter *printer)
done = 0; 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() 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);
profile->setFontPrintScale(0.6);
profile->setToolTipVisibile(false);
prefs.animation_speed = 0;
// render the Qwebview
QPainter painter; QPainter painter;
QSize size(A4_300DPI_WIDTH, A4_300DPI_HIGHT); QSize size(A4_300DPI_WIDTH, A4_300DPI_HIGHT);
QRect viewPort(0, 0, size.width(), size.height());
painter.begin(printer); painter.begin(printer);
painter.setRenderHint(QPainter::Antialiasing); painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::SmoothPixmapTransform); painter.setRenderHint(QPainter::SmoothPixmapTransform);
webView->page()->setViewportSize(size); webView->page()->setViewportSize(size);
int Pages = ceil(getTotalWork() / 2.0); int Pages = ceil(getTotalWork() / 2.0);
// 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;
for (int i = 0; i < Pages; i++) { for (int i = 0; i < Pages; i++) {
// render the base Html template
webView->page()->mainFrame()->render(&painter, QWebFrame::ContentsLayer); 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, size.height());
viewPort.adjust(0, size.height(), 0, size.height());
// rendering progress is 4/5 of total work
emit(progessUpdated((i * 80.0 / Pages) + done)); emit(progessUpdated((i * 80.0 / Pages) + done));
if (i < Pages - 1) if (i < Pages - 1)
printer->newPage(); printer->newPage();
} }
painter.end(); 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 //value: ranges from 0 : 100 and shows the progress of the templating engine

View file

@ -3,6 +3,10 @@
#include <QPrinter> #include <QPrinter>
#include <QWebView> #include <QWebView>
#include <QRect>
#include <QPainter>
#include "profile/profilewidget2.h"
class Printer : public QObject { class Printer : public QObject {
Q_OBJECT Q_OBJECT
@ -11,6 +15,7 @@ private:
QPrinter *printer; QPrinter *printer;
QWebView *webView; QWebView *webView;
void render(); void render();
void putProfileImage(QRect box, QRect viewPort, QPainter *painter, struct dive *dive, QPointer<ProfileWidget2> profile);
int done; int done;
private slots: private slots:

View file

@ -180,8 +180,7 @@
</td> </td>
</tr> </tr>
</tbody></table> </tbody></table>
<div class="diveProfile"> <div class="diveProfile" id="dive_{{ dive.number }}">
<h1> Dive profile area </h1>
</div> </div>
</div> </div>
<div class="notesPart"> <div class="notesPart">