| 
									
										
										
										
											2017-04-27 20:26:05 +02:00
										 |  |  | // SPDX-License-Identifier: GPL-2.0
 | 
					
						
							| 
									
										
										
										
											2015-04-21 18:05:20 +02:00
										 |  |  | #include "printer.h"
 | 
					
						
							|  |  |  | #include "templatelayout.h"
 | 
					
						
							| 
									
										
										
										
											2016-04-05 09:40:03 -07:00
										 |  |  | #include "core/statistics.h"
 | 
					
						
							| 
									
										
										
										
											2018-06-03 22:15:19 +02:00
										 |  |  | #include "core/qthelper.h"
 | 
					
						
							| 
									
										
										
										
											2018-08-20 11:51:48 +02:00
										 |  |  | #include "core/settings/qPrefDisplay.h"
 | 
					
						
							| 
									
										
										
										
											2015-04-21 18:05:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-11 22:50:27 +02:00
										 |  |  | #include <algorithm>
 | 
					
						
							| 
									
										
										
										
											2015-04-21 18:05:20 +02:00
										 |  |  | #include <QPainter>
 | 
					
						
							| 
									
										
										
										
											2016-08-09 00:12:12 +02:00
										 |  |  | #include <QtWebKitWidgets>
 | 
					
						
							| 
									
										
										
										
											2015-06-10 19:31:19 +02:00
										 |  |  | #include <QWebElementCollection>
 | 
					
						
							|  |  |  | #include <QWebElement>
 | 
					
						
							| 
									
										
										
										
											2015-09-03 15:56:37 -03:00
										 |  |  | #include "profile-widget/profilewidget2.h"
 | 
					
						
							| 
									
										
										
										
											2015-04-21 18:05:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-10 21:30:18 +02:00
										 |  |  | Printer::Printer(QPaintDevice *paintDevice, print_options *printOptions, template_options *templateOptions,  PrintMode printMode) | 
					
						
							| 
									
										
										
										
											2015-04-21 18:05:20 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-07-10 20:34:25 +02:00
										 |  |  | 	this->paintDevice = paintDevice; | 
					
						
							| 
									
										
										
										
											2015-06-14 06:25:35 +02:00
										 |  |  | 	this->printOptions = printOptions; | 
					
						
							| 
									
										
										
										
											2015-07-02 22:26:31 +02:00
										 |  |  | 	this->templateOptions = templateOptions; | 
					
						
							| 
									
										
										
										
											2015-07-10 21:30:18 +02:00
										 |  |  | 	this->printMode = printMode; | 
					
						
							| 
									
										
										
										
											2015-06-22 06:42:02 -07:00
										 |  |  | 	dpi = 0; | 
					
						
							| 
									
										
										
										
											2015-05-30 15:32:15 +02:00
										 |  |  | 	done = 0; | 
					
						
							| 
									
										
										
										
											2015-07-10 14:59:56 +02:00
										 |  |  | 	webView = new QWebView(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Printer::~Printer() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	delete webView; | 
					
						
							| 
									
										
										
										
											2015-04-21 18:05:20 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-10 19:31:19 +02: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()); | 
					
						
							| 
									
										
										
										
											2018-05-16 15:25:57 +02:00
										 |  |  | 	profile->plotDive(dive, true, true); | 
					
						
							| 
									
										
										
										
											2015-07-19 07:12:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (!printOptions->color_selected) { | 
					
						
							|  |  |  | 		QImage image(pos.width(), pos.height(), QImage::Format_ARGB32); | 
					
						
							|  |  |  | 		QPainter imgPainter(&image); | 
					
						
							|  |  |  | 		imgPainter.setRenderHint(QPainter::Antialiasing); | 
					
						
							|  |  |  | 		imgPainter.setRenderHint(QPainter::SmoothPixmapTransform); | 
					
						
							|  |  |  | 		profile->render(&imgPainter, QRect(0, 0, pos.width(), pos.height())); | 
					
						
							|  |  |  | 		imgPainter.end(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// convert QImage to grayscale before rendering
 | 
					
						
							|  |  |  | 		for (int i = 0; i < image.height(); i++) { | 
					
						
							|  |  |  | 			QRgb *pixel = reinterpret_cast<QRgb *>(image.scanLine(i)); | 
					
						
							|  |  |  | 			QRgb *end = pixel + image.width(); | 
					
						
							|  |  |  | 			for (; pixel != end; pixel++) { | 
					
						
							|  |  |  | 				int gray_val = qGray(*pixel); | 
					
						
							|  |  |  | 				*pixel = QColor(gray_val, gray_val, gray_val).rgb(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		painter->drawImage(pos, image); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		profile->render(painter, pos); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-06-10 19:31:19 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-13 23:23:07 +02:00
										 |  |  | void Printer::flowRender() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-08-19 23:02:58 +02:00
										 |  |  | 	// add extra padding at the bottom to pages with height not divisible by view port
 | 
					
						
							|  |  |  | 	int paddingBottom = pageSize.height() - (webView->page()->mainFrame()->contentsSize().height() % pageSize.height()); | 
					
						
							|  |  |  | 	QString styleString = QString::fromUtf8("padding-bottom: ") + QString::number(paddingBottom) + "px;"; | 
					
						
							|  |  |  | 	webView->page()->mainFrame()->findFirstElement("body").setAttribute("style", styleString); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-13 23:23:07 +02:00
										 |  |  | 	// render the Qwebview
 | 
					
						
							|  |  |  | 	QPainter painter; | 
					
						
							|  |  |  | 	QRect viewPort(0, 0, 0, 0); | 
					
						
							|  |  |  | 	painter.begin(paintDevice); | 
					
						
							|  |  |  | 	painter.setRenderHint(QPainter::Antialiasing); | 
					
						
							|  |  |  | 	painter.setRenderHint(QPainter::SmoothPixmapTransform); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// get all references to dontbreak divs
 | 
					
						
							|  |  |  | 	int start = 0, end = 0; | 
					
						
							|  |  |  | 	int fullPageResolution = webView->page()->mainFrame()->contentsSize().height(); | 
					
						
							| 
									
										
										
										
											2019-04-01 22:15:19 +02:00
										 |  |  | 	const QWebElementCollection dontbreak = webView->page()->mainFrame()->findAllElements(".dontbreak"); | 
					
						
							|  |  |  | 	for (QWebElement dontbreakElement: dontbreak) { | 
					
						
							| 
									
										
										
										
											2015-08-13 23:23:07 +02:00
										 |  |  | 		if ((dontbreakElement.geometry().y() + dontbreakElement.geometry().height()) - start < pageSize.height()) { | 
					
						
							|  |  |  | 			// One more element can be placed
 | 
					
						
							|  |  |  | 			end = dontbreakElement.geometry().y() + dontbreakElement.geometry().height(); | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2015-08-14 21:37:12 +02:00
										 |  |  | 			// fill the page with background color
 | 
					
						
							|  |  |  | 			QRect fullPage(0, 0, pageSize.width(), pageSize.height()); | 
					
						
							|  |  |  | 			QBrush fillBrush(templateOptions->color_palette.color1); | 
					
						
							|  |  |  | 			painter.fillRect(fullPage, fillBrush); | 
					
						
							| 
									
										
										
										
											2015-08-13 23:23:07 +02:00
										 |  |  | 			QRegion reigon(0, 0, pageSize.width(), end - start); | 
					
						
							|  |  |  | 			viewPort.setRect(0, start, pageSize.width(), end - start); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// render the base Html template
 | 
					
						
							|  |  |  | 			webView->page()->mainFrame()->render(&painter, QWebFrame::ContentsLayer, reigon); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// scroll the webview to the next page
 | 
					
						
							|  |  |  | 			webView->page()->mainFrame()->scroll(0, dontbreakElement.geometry().y() - start); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// rendering progress is 4/5 of total work
 | 
					
						
							| 
									
										
										
										
											2017-03-23 08:13:49 +07:00
										 |  |  | 			emit(progessUpdated(lrint((end * 80.0 / fullPageResolution) + done))); | 
					
						
							| 
									
										
										
										
											2015-08-21 11:02:10 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			// add new pages only in print mode, while previewing we don't add new pages
 | 
					
						
							| 
									
										
										
										
											2019-05-15 07:42:14 -07:00
										 |  |  | 			if (printMode == Printer::PRINT) { | 
					
						
							| 
									
										
										
										
											2015-08-21 11:02:10 +02:00
										 |  |  | 				static_cast<QPrinter*>(paintDevice)->newPage(); | 
					
						
							| 
									
										
										
										
											2019-05-15 07:42:14 -07:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2015-08-21 11:02:10 +02:00
										 |  |  | 				painter.end(); | 
					
						
							|  |  |  | 				return; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-08-13 23:23:07 +02:00
										 |  |  | 			start = dontbreakElement.geometry().y(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// render the remianing page
 | 
					
						
							| 
									
										
										
										
											2015-08-14 21:37:12 +02:00
										 |  |  | 	QRect fullPage(0, 0, pageSize.width(), pageSize.height()); | 
					
						
							|  |  |  | 	QBrush fillBrush(templateOptions->color_palette.color1); | 
					
						
							|  |  |  | 	painter.fillRect(fullPage, fillBrush); | 
					
						
							| 
									
										
										
										
											2015-08-13 23:23:07 +02:00
										 |  |  | 	QRegion reigon(0, 0, pageSize.width(), end - start); | 
					
						
							|  |  |  | 	webView->page()->mainFrame()->render(&painter, QWebFrame::ContentsLayer, reigon); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	painter.end(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-10 16:20:14 +02:00
										 |  |  | void Printer::render(int Pages = 0) | 
					
						
							| 
									
										
										
										
											2015-04-21 18:05:20 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-06-10 19:31:19 +02:00
										 |  |  | 	// keep original preferences
 | 
					
						
							| 
									
										
										
										
											2018-10-12 15:07:40 +02:00
										 |  |  | 	QPointer<ProfileWidget2> profile = MainWindow::instance()->graphics; | 
					
						
							| 
									
										
										
										
											2015-06-10 19:31:19 +02:00
										 |  |  | 	int profileFrameStyle = profile->frameStyle(); | 
					
						
							| 
									
										
										
										
											2018-08-20 11:51:48 +02:00
										 |  |  | 	int animationOriginal = qPrefDisplay::animation_speed(); | 
					
						
							| 
									
										
										
										
											2015-06-10 19:31:19 +02:00
										 |  |  | 	double fontScale = profile->getFontPrintScale(); | 
					
						
							| 
									
										
										
										
											2015-07-29 12:24:18 -07:00
										 |  |  | 	double printFontScale = 1.0; | 
					
						
							| 
									
										
										
										
											2015-06-10 19:31:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// apply printing settings to profile
 | 
					
						
							|  |  |  | 	profile->setFrameStyle(QFrame::NoFrame); | 
					
						
							| 
									
										
										
										
											2015-06-14 06:34:58 +02:00
										 |  |  | 	profile->setPrintMode(true, !printOptions->color_selected); | 
					
						
							| 
									
										
										
										
											2015-06-10 19:31:19 +02:00
										 |  |  | 	profile->setToolTipVisibile(false); | 
					
						
							| 
									
										
										
										
											2018-08-20 11:51:48 +02:00
										 |  |  | 	qPrefDisplay::set_animation_speed(0); | 
					
						
							| 
									
										
										
										
											2015-06-10 19:31:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// render the Qwebview
 | 
					
						
							| 
									
										
										
										
											2015-04-21 18:05:20 +02:00
										 |  |  | 	QPainter painter; | 
					
						
							| 
									
										
										
										
											2015-06-17 17:05:14 +02:00
										 |  |  | 	QRect viewPort(0, 0, pageSize.width(), pageSize.height()); | 
					
						
							| 
									
										
										
										
											2015-07-10 20:34:25 +02:00
										 |  |  | 	painter.begin(paintDevice); | 
					
						
							| 
									
										
										
										
											2015-04-21 18:05:20 +02:00
										 |  |  | 	painter.setRenderHint(QPainter::Antialiasing); | 
					
						
							|  |  |  | 	painter.setRenderHint(QPainter::SmoothPixmapTransform); | 
					
						
							| 
									
										
										
										
											2015-06-14 06:34:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-10 19:31:19 +02: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) { | 
					
						
							| 
									
										
										
										
											2015-07-29 12:24:18 -07:00
										 |  |  | 		printFontScale = (double)collection.at(0).geometry().size().height() / (double)profile->size().height(); | 
					
						
							| 
									
										
										
										
											2015-06-10 19:31:19 +02:00
										 |  |  | 		profile->resize(collection.at(0).geometry().size()); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-07-29 12:24:18 -07:00
										 |  |  | 	profile->setFontPrintScale(printFontScale); | 
					
						
							| 
									
										
										
										
											2015-06-10 19:31:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	int elemNo = 0; | 
					
						
							| 
									
										
										
										
											2015-04-21 18:05:20 +02:00
										 |  |  | 	for (int i = 0; i < Pages; i++) { | 
					
						
							| 
									
										
										
										
											2015-06-10 19:31:19 +02:00
										 |  |  | 		// render the base Html template
 | 
					
						
							| 
									
										
										
										
											2015-04-21 18:05:20 +02:00
										 |  |  | 		webView->page()->mainFrame()->render(&painter, QWebFrame::ContentsLayer); | 
					
						
							| 
									
										
										
										
											2015-06-10 19:31:19 +02: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
 | 
					
						
							| 
									
										
										
										
											2015-07-28 13:35:04 -07:00
										 |  |  | 			QString diveIdString = collection.at(elemNo).attribute("id"); | 
					
						
							|  |  |  | 			int diveId = diveIdString.remove(0, 5).toInt(0, 10); | 
					
						
							|  |  |  | 			putProfileImage(collection.at(elemNo).geometry(), viewPort, &painter, get_dive_by_uniq_id(diveId), profile); | 
					
						
							| 
									
										
										
										
											2015-06-10 19:31:19 +02:00
										 |  |  | 			elemNo++; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// scroll the webview to the next page
 | 
					
						
							| 
									
										
										
										
											2015-06-17 17:05:14 +02:00
										 |  |  | 		webView->page()->mainFrame()->scroll(0, pageSize.height()); | 
					
						
							|  |  |  | 		viewPort.adjust(0, pageSize.height(), 0, pageSize.height()); | 
					
						
							| 
									
										
										
										
											2015-06-10 19:31:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// rendering progress is 4/5 of total work
 | 
					
						
							| 
									
										
										
										
											2017-03-23 08:13:49 +07:00
										 |  |  | 		emit(progessUpdated(lrint((i * 80.0 / Pages) + done))); | 
					
						
							| 
									
										
										
										
											2015-07-10 21:30:18 +02:00
										 |  |  | 		if (i < Pages - 1 && printMode == Printer::PRINT) | 
					
						
							| 
									
										
										
										
											2015-07-10 20:34:25 +02:00
										 |  |  | 			static_cast<QPrinter*>(paintDevice)->newPage(); | 
					
						
							| 
									
										
										
										
											2015-04-21 18:05:20 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	painter.end(); | 
					
						
							| 
									
										
										
										
											2015-06-10 19:31:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// return profle settings
 | 
					
						
							|  |  |  | 	profile->setFrameStyle(profileFrameStyle); | 
					
						
							|  |  |  | 	profile->setPrintMode(false); | 
					
						
							|  |  |  | 	profile->setFontPrintScale(fontScale); | 
					
						
							|  |  |  | 	profile->setToolTipVisibile(true); | 
					
						
							|  |  |  | 	profile->resize(originalSize); | 
					
						
							| 
									
										
										
										
											2018-08-20 11:51:48 +02:00
										 |  |  | 	qPrefDisplay::set_animation_speed(animationOriginal); | 
					
						
							| 
									
										
										
										
											2015-06-10 19:31:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	//replot the dive after returning the settings
 | 
					
						
							| 
									
										
										
										
											2018-05-16 15:25:57 +02:00
										 |  |  | 	profile->plotDive(0, true, true); | 
					
						
							| 
									
										
										
										
											2015-04-21 18:05:20 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-30 15:32:15 +02: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); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-06 23:17:49 +02:00
										 |  |  | QString Printer::exportHtml() { | 
					
						
							|  |  |  | 	TemplateLayout t(printOptions, templateOptions); | 
					
						
							|  |  |  | 	connect(&t, SIGNAL(progressUpdated(int)), this, SLOT(templateProgessUpdated(int))); | 
					
						
							|  |  |  | 	QString html; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (printOptions->type == print_options::DIVELIST) { | 
					
						
							|  |  |  | 		html = t.generate(); | 
					
						
							|  |  |  | 	} else if (printOptions->type == print_options::STATISTICS ) { | 
					
						
							|  |  |  | 		html = t.generateStatistics(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// TODO: write html to file
 | 
					
						
							|  |  |  | 	return html; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-21 18:05:20 +02:00
										 |  |  | void Printer::print() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-07-10 21:30:18 +02:00
										 |  |  | 	// we can only print if "PRINT" mode is selected
 | 
					
						
							|  |  |  | 	if (printMode != Printer::PRINT) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-19 23:02:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-10 20:34:25 +02:00
										 |  |  | 	QPrinter *printerPtr; | 
					
						
							|  |  |  | 	printerPtr = static_cast<QPrinter*>(paintDevice); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-02 22:26:31 +02:00
										 |  |  | 	TemplateLayout t(printOptions, templateOptions); | 
					
						
							| 
									
										
										
										
											2015-06-17 17:05:14 +02:00
										 |  |  | 	connect(&t, SIGNAL(progressUpdated(int)), this, SLOT(templateProgessUpdated(int))); | 
					
						
							| 
									
										
										
										
											2015-07-10 20:34:25 +02:00
										 |  |  | 	dpi = printerPtr->resolution(); | 
					
						
							| 
									
										
										
										
											2015-06-17 17:05:14 +02:00
										 |  |  | 	//rendering resolution = selected paper size in inchs * printer dpi
 | 
					
						
							| 
									
										
										
										
											2015-08-22 16:05:13 +02:00
										 |  |  | 	pageSize.setHeight(qCeil(printerPtr->pageRect(QPrinter::Inch).height() * dpi)); | 
					
						
							|  |  |  | 	pageSize.setWidth(qCeil(printerPtr->pageRect(QPrinter::Inch).width() * dpi)); | 
					
						
							| 
									
										
										
										
											2015-06-17 17:05:14 +02:00
										 |  |  | 	webView->page()->setViewportSize(pageSize); | 
					
						
							| 
									
										
										
										
											2015-08-08 00:55:12 +02:00
										 |  |  | 	webView->page()->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff); | 
					
						
							| 
									
										
										
										
											2019-03-19 12:40:33 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-11 22:50:27 +02:00
										 |  |  | 	// export border width with at least 1 pixel
 | 
					
						
							| 
									
										
										
										
											2017-03-06 18:41:54 +02:00
										 |  |  | 	// templateOptions->borderwidth = std::max(1, pageSize.width() / 1000);
 | 
					
						
							| 
									
										
										
										
											2015-08-21 19:12:33 +02:00
										 |  |  | 	if (printOptions->type == print_options::DIVELIST) { | 
					
						
							|  |  |  | 		webView->setHtml(t.generate()); | 
					
						
							|  |  |  | 	} else if (printOptions->type == print_options::STATISTICS ) { | 
					
						
							|  |  |  | 		webView->setHtml(t.generateStatistics()); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-07-10 20:34:25 +02:00
										 |  |  | 	if (printOptions->color_selected && printerPtr->colorMode()) { | 
					
						
							|  |  |  | 		printerPtr->setColorMode(QPrinter::Color); | 
					
						
							| 
									
										
										
										
											2015-07-10 16:20:14 +02:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2015-07-10 20:34:25 +02:00
										 |  |  | 		printerPtr->setColorMode(QPrinter::GrayScale); | 
					
						
							| 
									
										
										
										
											2015-07-10 16:20:14 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	// apply user settings
 | 
					
						
							|  |  |  | 	int divesPerPage; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// get number of dives per page from data-numberofdives attribute in the body of the selected template
 | 
					
						
							|  |  |  | 	bool ok; | 
					
						
							|  |  |  | 	divesPerPage = webView->page()->mainFrame()->findFirstElement("body").attribute("data-numberofdives").toInt(&ok); | 
					
						
							|  |  |  | 	if (!ok) { | 
					
						
							|  |  |  | 		divesPerPage = 1; // print each dive in a single page if the attribute is missing or malformed
 | 
					
						
							|  |  |  | 		//TODO: show warning
 | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-07-23 15:20:09 +02:00
										 |  |  | 	int Pages; | 
					
						
							|  |  |  | 	if (divesPerPage == 0) { | 
					
						
							| 
									
										
										
										
											2015-08-13 23:23:07 +02:00
										 |  |  | 		flowRender(); | 
					
						
							| 
									
										
										
										
											2015-07-23 15:20:09 +02:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2015-08-22 16:05:13 +02:00
										 |  |  | 		Pages = qCeil(getTotalWork(printOptions) / (float)divesPerPage); | 
					
						
							| 
									
										
										
										
											2015-08-13 23:23:07 +02:00
										 |  |  | 		render(Pages); | 
					
						
							| 
									
										
										
										
											2015-07-23 15:20:09 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-04-21 18:05:20 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-07-10 21:45:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | void Printer::previewOnePage() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (printMode == PREVIEW) { | 
					
						
							|  |  |  | 		TemplateLayout t(printOptions, templateOptions); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		pageSize.setHeight(paintDevice->height()); | 
					
						
							|  |  |  | 		pageSize.setWidth(paintDevice->width()); | 
					
						
							|  |  |  | 		webView->page()->setViewportSize(pageSize); | 
					
						
							| 
									
										
										
										
											2015-08-21 10:56:59 +02:00
										 |  |  | 		// initialize the border settings
 | 
					
						
							| 
									
										
										
										
											2017-03-06 18:41:54 +02:00
										 |  |  | 		// templateOptions->border_width = std::max(1, pageSize.width() / 1000);
 | 
					
						
							| 
									
										
										
										
											2015-08-21 19:12:33 +02:00
										 |  |  | 		if (printOptions->type == print_options::DIVELIST) { | 
					
						
							|  |  |  | 			webView->setHtml(t.generate()); | 
					
						
							|  |  |  | 		} else if (printOptions->type == print_options::STATISTICS ) { | 
					
						
							|  |  |  | 			webView->setHtml(t.generateStatistics()); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-08-21 11:02:10 +02:00
										 |  |  | 		bool ok; | 
					
						
							|  |  |  | 		int divesPerPage = webView->page()->mainFrame()->findFirstElement("body").attribute("data-numberofdives").toInt(&ok); | 
					
						
							|  |  |  | 		if (!ok) { | 
					
						
							|  |  |  | 			divesPerPage = 1; // print each dive in a single page if the attribute is missing or malformed
 | 
					
						
							|  |  |  | 			//TODO: show warning
 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (divesPerPage == 0) { | 
					
						
							|  |  |  | 			flowRender(); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			render(1); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-07-10 21:45:27 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | } |