2015-04-21 15:23:13 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "templatelayout.h"
|
|
|
|
#include "helpers.h"
|
2015-06-23 05:37:43 +00:00
|
|
|
#include "display.h"
|
2015-04-21 15:23:13 +00:00
|
|
|
|
2015-08-21 16:01:27 +00:00
|
|
|
QList<QString> grantlee_templates, grantlee_statistics_templates;
|
2015-07-24 07:26:25 +00:00
|
|
|
|
2015-06-21 04:13:22 +00:00
|
|
|
int getTotalWork(print_options *printOptions)
|
2015-05-30 13:32:15 +00:00
|
|
|
{
|
2015-06-21 04:13:22 +00: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 17:32:42 +00:00
|
|
|
return dive_table.nr;
|
2015-05-30 13:32:15 +00:00
|
|
|
}
|
2015-04-21 15:23:13 +00:00
|
|
|
|
2015-07-24 07:26:25 +00:00
|
|
|
void find_all_templates()
|
|
|
|
{
|
2015-07-26 13:58:00 +00:00
|
|
|
grantlee_templates.clear();
|
2015-08-21 16:01:27 +00:00
|
|
|
grantlee_statistics_templates.clear();
|
2015-10-18 21:25:14 +00:00
|
|
|
QDir dir(getPrintingTemplatePathUser());
|
2016-01-08 17:42:27 +00:00
|
|
|
QStringList list = dir.entryList(QDir::Files | QDir::NoDotAndDotDot);
|
|
|
|
foreach (const QString& filename, list) {
|
2015-07-24 07:26:25 +00:00
|
|
|
if (filename.at(filename.size() - 1) != '~') {
|
2016-01-08 17:42:27 +00:00
|
|
|
grantlee_templates.append(filename);
|
2015-07-24 07:26:25 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-08 17:42:27 +00:00
|
|
|
|
2015-08-21 16:01:27 +00:00
|
|
|
// find statistics templates
|
2015-10-18 21:25:14 +00:00
|
|
|
dir.setPath(getPrintingTemplatePathUser() + QDir::separator() + "statistics");
|
2016-01-08 17:42:27 +00:00
|
|
|
list = dir.entryList(QDir::Files | QDir::NoDotAndDotDot);
|
|
|
|
foreach (const QString& filename, list) {
|
2015-08-21 16:01:27 +00:00
|
|
|
if (filename.at(filename.size() - 1) != '~') {
|
2016-01-08 17:42:27 +00:00
|
|
|
grantlee_statistics_templates.append(filename);
|
2015-08-21 16:01:27 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-24 07:26:25 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 20:26:31 +00:00
|
|
|
TemplateLayout::TemplateLayout(print_options *PrintOptions, template_options *templateOptions) :
|
2015-06-22 13:42:02 +00:00
|
|
|
m_engine(NULL)
|
2015-06-10 11:39:15 +00:00
|
|
|
{
|
2015-06-14 04:25:35 +00:00
|
|
|
this->PrintOptions = PrintOptions;
|
2015-07-02 20:26:31 +00:00
|
|
|
this->templateOptions = templateOptions;
|
2015-06-10 11:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TemplateLayout::~TemplateLayout()
|
|
|
|
{
|
|
|
|
delete m_engine;
|
|
|
|
}
|
|
|
|
|
2015-04-21 15:23:13 +00:00
|
|
|
QString TemplateLayout::generate()
|
|
|
|
{
|
2015-05-30 13:32:15 +00:00
|
|
|
int progress = 0;
|
2015-06-21 04:13:22 +00:00
|
|
|
int totalWork = getTotalWork(PrintOptions);
|
2015-05-30 13:32:15 +00:00
|
|
|
|
2015-04-21 15:23:13 +00:00
|
|
|
QString htmlContent;
|
2016-01-08 17:46:24 +00:00
|
|
|
delete m_engine;
|
2015-04-21 15:23:13 +00:00
|
|
|
m_engine = new Grantlee::Engine(this);
|
|
|
|
|
|
|
|
QSharedPointer<Grantlee::FileSystemTemplateLoader> m_templateLoader =
|
|
|
|
QSharedPointer<Grantlee::FileSystemTemplateLoader>(new Grantlee::FileSystemTemplateLoader());
|
2015-10-18 21:25:14 +00:00
|
|
|
m_templateLoader->setTemplateDirs(QStringList() << getPrintingTemplatePathUser());
|
2015-04-21 15:23:13 +00:00
|
|
|
m_engine->addTemplateLoader(m_templateLoader);
|
|
|
|
|
2015-07-02 20:26:31 +00:00
|
|
|
Grantlee::registerMetaType<template_options>();
|
2015-07-16 09:39:27 +00:00
|
|
|
Grantlee::registerMetaType<print_options>();
|
2015-04-21 15:23:13 +00:00
|
|
|
|
|
|
|
QVariantList diveList;
|
|
|
|
|
|
|
|
struct dive *dive;
|
|
|
|
int i;
|
|
|
|
for_each_dive (i, dive) {
|
|
|
|
//TODO check for exporting selected dives only
|
2015-06-21 04:13:22 +00:00
|
|
|
if (!dive->selected && PrintOptions->print_selected)
|
2015-04-21 15:23:13 +00:00
|
|
|
continue;
|
2016-01-07 18:01:24 +00:00
|
|
|
DiveObjectHelper *d = new DiveObjectHelper(dive);
|
2015-04-21 15:23:13 +00:00
|
|
|
diveList.append(QVariant::fromValue(d));
|
2015-05-30 13:32:15 +00:00
|
|
|
progress++;
|
|
|
|
emit progressUpdated(progress * 100.0 / totalWork);
|
2015-04-21 15:23:13 +00:00
|
|
|
}
|
2015-08-28 16:15:56 +00: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 15:23:13 +00:00
|
|
|
|
2015-07-24 07:53:07 +00:00
|
|
|
Grantlee::Template t = m_engine->loadByName(PrintOptions->p_template);
|
2015-04-21 15:23:13 +00: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 17:03:42 +00:00
|
|
|
QString TemplateLayout::generateStatistics()
|
|
|
|
{
|
|
|
|
QString htmlContent;
|
2016-01-08 17:46:24 +00:00
|
|
|
delete m_engine;
|
2015-08-21 17:03:42 +00:00
|
|
|
m_engine = new Grantlee::Engine(this);
|
|
|
|
|
|
|
|
QSharedPointer<Grantlee::FileSystemTemplateLoader> m_templateLoader =
|
|
|
|
QSharedPointer<Grantlee::FileSystemTemplateLoader>(new Grantlee::FileSystemTemplateLoader());
|
2015-10-18 21:25:14 +00:00
|
|
|
m_templateLoader->setTemplateDirs(QStringList() << getPrintingTemplatePathUser() + QDir::separator() + QString("statistics"));
|
2015-08-21 17:03:42 +00: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 16:15:56 +00: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 17:03:42 +00: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 04:21:39 +00:00
|
|
|
QString TemplateLayout::readTemplate(QString template_name)
|
|
|
|
{
|
2015-10-18 21:25:14 +00:00
|
|
|
QFile qfile(getPrintingTemplatePathUser() + QDir::separator() + template_name);
|
2015-07-05 04:21:39 +00: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-18 21:25:14 +00:00
|
|
|
QFile qfile(getPrintingTemplatePathUser() + QDir::separator() + template_name);
|
2015-07-05 04:21:39 +00:00
|
|
|
if (qfile.open(QFile::ReadWrite | QFile::Text)) {
|
|
|
|
qfile.write(grantlee_template.toUtf8().data());
|
2015-07-07 01:12:36 +00:00
|
|
|
qfile.resize(qfile.pos());
|
2015-07-05 04:21:39 +00:00
|
|
|
qfile.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-21 17:00:46 +00:00
|
|
|
YearInfo::YearInfo()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
YearInfo::~YearInfo()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|