Planner: support profile in planner print

The "Print" button in the planner dumps the QTextEdit to
a QPrinter via ::print(). This patch renders the Profile
to a Pixmap which is inserted as Base64 stream in an <img>
tag and fed on top of the QTextEdit HTML contents.

This route preserves the planner notes as text in PDF prints.
The quick alternative is to render the QTextDocument to
a QPixmap as well, but that will not preserve the text
and pagination becomes manual.

Possibly the QTextDocument can be rendered as a QPicture
but pagination is still an issue, while so far there is exactly
one user requesting this feature!

Related small change in ProfileWidget2:
Explicitly hide the tooltip when printMode is true.

Signed-off-by: Lubomir I. Ivanov <neolit123@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Lubomir I. Ivanov 2015-11-15 23:02:02 +02:00 committed by Dirk Hohndel
parent 1d4075f368
commit f5dbc3b44d
2 changed files with 30 additions and 0 deletions

View file

@ -28,6 +28,7 @@
#include "diveplanner.h"
#ifndef NO_PRINTING
#include <QPrintDialog>
#include <QBuffer>
#include "printdialog.h"
#endif
#include "tankinfomodel.h"
@ -768,6 +769,34 @@ void MainWindow::printPlan()
if (dialog->exec() != QDialog::Accepted)
return;
/* render the profile as a pixmap that is inserted as base64 data into a HTML <img> tag
* make it fit a page width defined by 2 cm margins via QTextDocument->print() (cannot be changed?)
* the height of the profile is 40% of the page height.
*/
QSizeF renderSize = printer.pageRect(QPrinter::Inch).size();
const qreal marginsInch = 1.57480315; // = (2 x 2cm) / 2.45cm/inch
renderSize.setWidth((renderSize.width() - marginsInch) * printer.resolution());
renderSize.setHeight(((renderSize.height() - marginsInch) * printer.resolution()) / 2.5);
QPixmap pixmap(renderSize.toSize());
QPainter painter(&pixmap);
painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::SmoothPixmapTransform);
ProfileWidget2 *profile = graphics();
QSize origSize = profile->size();
profile->resize(renderSize.toSize());
profile->setPrintMode(true);
profile->render(&painter);
profile->resize(origSize);
profile->setPrintMode(false);
QByteArray byteArray;
QBuffer buffer(&byteArray);
pixmap.save(&buffer, "PNG");
QString profileImage = QString("<img src=\"data:image/png;base64,") + byteArray.toBase64() + "\"/><br><br>";
withDisclaimer = profileImage + withDisclaimer;
plannerDetails()->divePlanOutput()->setHtml(withDisclaimer);
plannerDetails()->divePlanOutput()->print(&printer);
plannerDetails()->divePlanOutput()->setHtml(diveplan);

View file

@ -1527,6 +1527,7 @@ void ProfileWidget2::setPrintMode(bool mode, bool grayscale)
isGrayscale = mode ? grayscale : false;
mouseFollowerHorizontal->setVisible(!mode);
mouseFollowerVertical->setVisible(!mode);
toolTipItem->setVisible(!mode);
}
void ProfileWidget2::setFontPrintScale(double scale)