| 
									
										
										
										
											2017-04-27 20:26:05 +02:00
										 |  |  | // SPDX-License-Identifier: GPL-2.0
 | 
					
						
							| 
									
										
										
										
											2015-04-21 17:23:13 +02:00
										 |  |  | #include <string>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "templatelayout.h"
 | 
					
						
							| 
									
										
										
										
											2016-04-05 09:40:03 -07:00
										 |  |  | #include "core/helpers.h"
 | 
					
						
							|  |  |  | #include "core/display.h"
 | 
					
						
							| 
									
										
										
										
											2015-04-21 17:23:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-21 18:01:27 +02:00
										 |  |  | QList<QString> grantlee_templates, grantlee_statistics_templates; | 
					
						
							| 
									
										
										
										
											2015-07-24 09:26:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-21 06:13:22 +02:00
										 |  |  | int getTotalWork(print_options *printOptions) | 
					
						
							| 
									
										
										
										
											2015-05-30 15:32:15 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-06-21 06:13:22 +02:00
										 |  |  | 	if (printOptions->print_selected) { | 
					
						
							|  |  |  | 		// return the correct number depending on all/selected dives
 | 
					
						
							|  |  |  | 		// but don't return 0 as we might divide by this number
 | 
					
						
							|  |  |  | 		return amount_selected ? amount_selected : 1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-01-08 15:32:42 -02:00
										 |  |  | 	return dive_table.nr; | 
					
						
							| 
									
										
										
										
											2015-05-30 15:32:15 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-04-21 17:23:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-24 09:26:25 +02:00
										 |  |  | void find_all_templates() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-07-26 15:58:00 +02:00
										 |  |  | 	grantlee_templates.clear(); | 
					
						
							| 
									
										
										
										
											2015-08-21 18:01:27 +02:00
										 |  |  | 	grantlee_statistics_templates.clear(); | 
					
						
							| 
									
										
										
										
											2015-10-19 00:25:14 +03:00
										 |  |  | 	QDir dir(getPrintingTemplatePathUser()); | 
					
						
							| 
									
										
										
										
											2016-01-08 15:42:27 -02:00
										 |  |  | 	QStringList list = dir.entryList(QDir::Files | QDir::NoDotAndDotDot); | 
					
						
							|  |  |  | 	foreach (const QString& filename, list) { | 
					
						
							| 
									
										
										
										
											2015-07-24 09:26:25 +02:00
										 |  |  | 		if (filename.at(filename.size() - 1) != '~') { | 
					
						
							| 
									
										
										
										
											2016-01-08 15:42:27 -02:00
										 |  |  | 			grantlee_templates.append(filename); | 
					
						
							| 
									
										
										
										
											2015-07-24 09:26:25 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-01-08 15:42:27 -02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-21 18:01:27 +02:00
										 |  |  | 	// find statistics templates
 | 
					
						
							| 
									
										
										
										
											2015-10-19 00:25:14 +03:00
										 |  |  | 	dir.setPath(getPrintingTemplatePathUser() + QDir::separator() + "statistics"); | 
					
						
							| 
									
										
										
										
											2016-01-08 15:42:27 -02:00
										 |  |  | 	list = dir.entryList(QDir::Files | QDir::NoDotAndDotDot); | 
					
						
							|  |  |  | 	foreach (const QString& filename, list) { | 
					
						
							| 
									
										
										
										
											2015-08-21 18:01:27 +02:00
										 |  |  | 		if (filename.at(filename.size() - 1) != '~') { | 
					
						
							| 
									
										
										
										
											2016-01-08 15:42:27 -02:00
										 |  |  | 			grantlee_statistics_templates.append(filename); | 
					
						
							| 
									
										
										
										
											2015-08-21 18:01:27 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-07-24 09:26:25 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-02 22:26:31 +02:00
										 |  |  | TemplateLayout::TemplateLayout(print_options *PrintOptions, template_options *templateOptions) : | 
					
						
							| 
									
										
										
										
											2015-06-22 06:42:02 -07:00
										 |  |  | 	m_engine(NULL) | 
					
						
							| 
									
										
										
										
											2015-06-10 13:39:15 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-06-14 06:25:35 +02:00
										 |  |  | 	this->PrintOptions = PrintOptions; | 
					
						
							| 
									
										
										
										
											2015-07-02 22:26:31 +02:00
										 |  |  | 	this->templateOptions = templateOptions; | 
					
						
							| 
									
										
										
										
											2015-06-10 13:39:15 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TemplateLayout::~TemplateLayout() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	delete m_engine; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-29 16:42:06 +02:00
										 |  |  | /* a HTML pre-processor stage. acts like a compatibility layer
 | 
					
						
							|  |  |  |  * between some Grantlee variables and DiveObjectHelper Q_PROPERTIES; | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static QString preprocessTemplate(const QString &in) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-03-01 00:15:34 +02:00
										 |  |  | 	int i; | 
					
						
							| 
									
										
										
										
											2016-02-29 16:42:06 +02:00
										 |  |  | 	QString out = in; | 
					
						
							| 
									
										
										
										
											2016-03-01 00:15:34 +02:00
										 |  |  | 	QString iStr; | 
					
						
							| 
									
										
										
										
											2016-02-29 16:42:06 +02:00
										 |  |  | 	QList<QPair<QString, QString> > list; | 
					
						
							| 
									
										
										
										
											2016-03-01 00:15:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* populate known variables in a QPair list */ | 
					
						
							| 
									
										
										
										
											2016-02-29 16:42:06 +02:00
										 |  |  | 	list << qMakePair(QString("dive.weights"), QString("dive.weightList")); | 
					
						
							| 
									
										
										
										
											2016-03-01 00:15:34 +02:00
										 |  |  | 	for (i = 0; i < MAX_WEIGHTSYSTEMS; i++) | 
					
						
							|  |  |  | 		list << qMakePair(QString("dive.weight%1").arg(i), QString("dive.weights.%1").arg(i)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-29 16:42:06 +02:00
										 |  |  | 	list << qMakePair(QString("dive.cylinders"), QString("dive.cylinderList")); | 
					
						
							| 
									
										
										
										
											2016-03-01 00:15:34 +02:00
										 |  |  | 	for (i = 0; i < MAX_CYLINDERS; i++) | 
					
						
							|  |  |  | 		list << qMakePair(QString("dive.cylinder%1").arg(i), QString("dive.cylinders.%1").arg(i)); | 
					
						
							| 
									
										
										
										
											2016-02-29 16:42:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* lazy method of variable replacement without regex. the Grantlee parser
 | 
					
						
							|  |  |  | 	 * works with a single or no space next to the variable markers - | 
					
						
							|  |  |  | 	 * e.g. '{{ var }}' */ | 
					
						
							| 
									
										
										
										
											2016-03-01 00:15:34 +02:00
										 |  |  | 	for (i = 0; i < list.length(); i++) { | 
					
						
							| 
									
										
										
										
											2016-02-29 16:42:06 +02:00
										 |  |  | 		QPair<QString, QString> p = list.at(i); | 
					
						
							|  |  |  | 		out.replace("{{ " + p.first + " }}", "{{ " + p.second + " }}"); | 
					
						
							|  |  |  | 		out.replace("{{" + p.first + "}}", "{{" + p.second + "}}"); | 
					
						
							|  |  |  | 		out.replace("{{ " + p.first + "}}", "{{ " + p.second + "}}"); | 
					
						
							|  |  |  | 		out.replace("{{" + p.first + " }}", "{{" + p.second + " }}"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return out; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-21 17:23:13 +02:00
										 |  |  | QString TemplateLayout::generate() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-05-30 15:32:15 +02:00
										 |  |  | 	int progress = 0; | 
					
						
							| 
									
										
										
										
											2015-06-21 06:13:22 +02:00
										 |  |  | 	int totalWork = getTotalWork(PrintOptions); | 
					
						
							| 
									
										
										
										
											2015-05-30 15:32:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-21 17:23:13 +02:00
										 |  |  | 	QString htmlContent; | 
					
						
							| 
									
										
										
										
											2016-01-08 15:46:24 -02:00
										 |  |  | 	delete m_engine; | 
					
						
							| 
									
										
										
										
											2015-04-21 17:23:13 +02:00
										 |  |  | 	m_engine = new Grantlee::Engine(this); | 
					
						
							| 
									
										
										
										
											2015-07-02 22:26:31 +02:00
										 |  |  | 	Grantlee::registerMetaType<template_options>(); | 
					
						
							| 
									
										
										
										
											2015-07-16 11:39:27 +02:00
										 |  |  | 	Grantlee::registerMetaType<print_options>(); | 
					
						
							| 
									
										
										
										
											2015-04-21 17:23:13 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	QVariantList diveList; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	struct dive *dive; | 
					
						
							|  |  |  | 	int i; | 
					
						
							|  |  |  | 	for_each_dive (i, dive) { | 
					
						
							|  |  |  | 		//TODO check for exporting selected dives only
 | 
					
						
							| 
									
										
										
										
											2015-06-21 06:13:22 +02:00
										 |  |  | 		if (!dive->selected && PrintOptions->print_selected) | 
					
						
							| 
									
										
										
										
											2015-04-21 17:23:13 +02:00
										 |  |  | 			continue; | 
					
						
							| 
									
										
										
										
											2016-01-07 16:01:24 -02:00
										 |  |  | 		DiveObjectHelper *d = new DiveObjectHelper(dive); | 
					
						
							| 
									
										
										
										
											2015-04-21 17:23:13 +02:00
										 |  |  | 		diveList.append(QVariant::fromValue(d)); | 
					
						
							| 
									
										
										
										
											2015-05-30 15:32:15 +02:00
										 |  |  | 		progress++; | 
					
						
							| 
									
										
										
										
											2017-03-23 08:13:49 +07:00
										 |  |  | 		emit progressUpdated(lrint(progress * 100.0 / totalWork)); | 
					
						
							| 
									
										
										
										
											2015-04-21 17:23:13 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-08-28 09:15:56 -07:00
										 |  |  | 	Grantlee::Context c; | 
					
						
							|  |  |  | 	c.insert("dives", diveList); | 
					
						
							|  |  |  | 	c.insert("template_options", QVariant::fromValue(*templateOptions)); | 
					
						
							|  |  |  | 	c.insert("print_options", QVariant::fromValue(*PrintOptions)); | 
					
						
							| 
									
										
										
										
											2015-04-21 17:23:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-29 16:42:06 +02:00
										 |  |  | 	/* don't use the Grantlee loader API */ | 
					
						
							|  |  |  | 	QString templateContents = readTemplate(PrintOptions->p_template); | 
					
						
							|  |  |  | 	QString preprocessed = preprocessTemplate(templateContents); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* create the template from QString; is this thing allocating memory? */ | 
					
						
							|  |  |  | 	Grantlee::Template t = m_engine->newTemplate(preprocessed, PrintOptions->p_template); | 
					
						
							| 
									
										
										
										
											2015-04-21 17:23:13 +02:00
										 |  |  | 	if (!t || t->error()) { | 
					
						
							|  |  |  | 		qDebug() << "Can't load template"; | 
					
						
							|  |  |  | 		return htmlContent; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	htmlContent = t->render(&c); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (t->error()) { | 
					
						
							|  |  |  | 		qDebug() << "Can't render template"; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return htmlContent; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-21 19:03:42 +02:00
										 |  |  | QString TemplateLayout::generateStatistics() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	QString htmlContent; | 
					
						
							| 
									
										
										
										
											2016-01-08 15:46:24 -02:00
										 |  |  | 	delete m_engine; | 
					
						
							| 
									
										
										
										
											2015-08-21 19:03:42 +02:00
										 |  |  | 	m_engine = new Grantlee::Engine(this); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	QSharedPointer<Grantlee::FileSystemTemplateLoader> m_templateLoader = | 
					
						
							|  |  |  | 		QSharedPointer<Grantlee::FileSystemTemplateLoader>(new Grantlee::FileSystemTemplateLoader()); | 
					
						
							| 
									
										
										
										
											2015-10-19 00:25:14 +03:00
										 |  |  | 	m_templateLoader->setTemplateDirs(QStringList() << getPrintingTemplatePathUser() + QDir::separator() + QString("statistics")); | 
					
						
							| 
									
										
										
										
											2015-08-21 19:03:42 +02:00
										 |  |  | 	m_engine->addTemplateLoader(m_templateLoader); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Grantlee::registerMetaType<YearInfo>(); | 
					
						
							|  |  |  | 	Grantlee::registerMetaType<template_options>(); | 
					
						
							|  |  |  | 	Grantlee::registerMetaType<print_options>(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	QVariantList years; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	int i = 0; | 
					
						
							|  |  |  | 	while (stats_yearly != NULL && stats_yearly[i].period) { | 
					
						
							|  |  |  | 		YearInfo year(stats_yearly[i]); | 
					
						
							|  |  |  | 		years.append(QVariant::fromValue(year)); | 
					
						
							|  |  |  | 		i++; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-28 09:15:56 -07:00
										 |  |  | 	Grantlee::Context c; | 
					
						
							|  |  |  | 	c.insert("years", years); | 
					
						
							|  |  |  | 	c.insert("template_options", QVariant::fromValue(*templateOptions)); | 
					
						
							|  |  |  | 	c.insert("print_options", QVariant::fromValue(*PrintOptions)); | 
					
						
							| 
									
										
										
										
											2015-08-21 19:03:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	Grantlee::Template t = m_engine->loadByName(PrintOptions->p_template); | 
					
						
							|  |  |  | 	if (!t || t->error()) { | 
					
						
							|  |  |  | 		qDebug() << "Can't load template"; | 
					
						
							|  |  |  | 		return htmlContent; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	htmlContent = t->render(&c); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (t->error()) { | 
					
						
							|  |  |  | 		qDebug() << "Can't render template"; | 
					
						
							|  |  |  | 		return htmlContent; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	emit progressUpdated(100); | 
					
						
							|  |  |  | 	return htmlContent; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-05 06:21:39 +02:00
										 |  |  | QString TemplateLayout::readTemplate(QString template_name) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-10-19 00:25:14 +03:00
										 |  |  | 	QFile qfile(getPrintingTemplatePathUser() + QDir::separator() + template_name); | 
					
						
							| 
									
										
										
										
											2015-07-05 06:21:39 +02:00
										 |  |  | 	if (qfile.open(QFile::ReadOnly | QFile::Text)) { | 
					
						
							|  |  |  | 		QTextStream in(&qfile); | 
					
						
							|  |  |  | 		return in.readAll(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return ""; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TemplateLayout::writeTemplate(QString template_name, QString grantlee_template) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-10-19 00:25:14 +03:00
										 |  |  | 	QFile qfile(getPrintingTemplatePathUser() + QDir::separator() + template_name); | 
					
						
							| 
									
										
										
										
											2015-07-05 06:21:39 +02:00
										 |  |  | 	if (qfile.open(QFile::ReadWrite | QFile::Text)) { | 
					
						
							|  |  |  | 		qfile.write(grantlee_template.toUtf8().data()); | 
					
						
							| 
									
										
										
										
											2015-07-07 03:12:36 +02:00
										 |  |  | 		qfile.resize(qfile.pos()); | 
					
						
							| 
									
										
										
										
											2015-07-05 06:21:39 +02:00
										 |  |  | 		qfile.close(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-21 19:00:46 +02:00
										 |  |  | YearInfo::YearInfo() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | YearInfo::~YearInfo() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |