From 70e8862efc7189342ee9c016e85054538b4d7b99 Mon Sep 17 00:00:00 2001 From: Gehad elrobey Date: Fri, 21 Aug 2015 19:00:46 +0200 Subject: Printing: export a statistics data structure to the Grantlee backend Add a YearInfo class which represents a year statistics. Export the class to the Grantlee backend, also export all it's members to the printing backend. Signed-off-by: Lubomir I. Ivanov Signed-off-by: Gehad elrobey --- templatelayout.h | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'templatelayout.h') diff --git a/templatelayout.h b/templatelayout.h index 41a3cbfa9..47ca31f4c 100644 --- a/templatelayout.h +++ b/templatelayout.h @@ -4,6 +4,8 @@ #include #include "mainwindow.h" #include "printoptions.h" +#include "statistics.h" +#include "helpers.h" int getTotalWork(print_options *printOptions); void find_all_templates(); @@ -98,9 +100,22 @@ public: QString sac() const; }; +class YearInfo { +public: + stats_t *year; + YearInfo(stats_t& year) + :year(&year) + { + + } + YearInfo(); + ~YearInfo(); +}; + Q_DECLARE_METATYPE(Dive) Q_DECLARE_METATYPE(template_options) Q_DECLARE_METATYPE(print_options) +Q_DECLARE_METATYPE(YearInfo) GRANTLEE_BEGIN_LOOKUP(Dive) if (property == "number") @@ -182,4 +197,41 @@ if (property == "grayscale") { } GRANTLEE_END_LOOKUP +GRANTLEE_BEGIN_LOOKUP(YearInfo) + +if (property == "year") { + return object.year->period; +} else if (property == "dives") { + return object.year->selection_size; +} else if (property == "min_temp") { + const char *unit; + double temp = get_temp_units(object.year->min_temp, &unit); + return object.year->min_temp == 0 ? "0" : QString::number(temp, 'g', 2) + unit; +} else if (property == "max_temp") { + const char *unit; + double temp = get_temp_units(object.year->max_temp, &unit); + return object.year->max_temp == 0 ? "0" : QString::number(temp, 'g', 2) + unit; +} else if (property == "total_time") { + return get_time_string(object.year->total_time.seconds, 0); +} else if (property == "avg_time") { + return get_minutes(object.year->total_time.seconds / object.year->selection_size); +} else if (property == "shortest_time") { + return get_minutes(object.year->shortest_time.seconds); +} else if (property == "longest_time") { + return get_minutes(object.year->longest_time.seconds); +} else if (property == "avg_depth") { + return get_depth_string(object.year->avg_depth); +} else if (property == "min_depth") { + return get_depth_string(object.year->min_depth); +} else if (property == "max_depth") { + return get_depth_string(object.year->max_depth); +} else if (property == "avg_sac") { + return get_volume_string(object.year->avg_sac); +} else if (property == "min_sac") { + return get_volume_string(object.year->min_sac); +} else if (property == "max_sac") { + return get_volume_string(object.year->max_sac); +} +GRANTLEE_END_LOOKUP + #endif -- cgit v1.2.3-70-g09d2 From b0e84e7e2b2a6e7252cd24d6df9d0b93af127d77 Mon Sep 17 00:00:00 2001 From: Gehad elrobey Date: Fri, 21 Aug 2015 19:03:42 +0200 Subject: Printing: generate a Grantlee statistics template As there are two types of templates, Grantlee need to support both. This commit adds the support to generating a statistics HTML code from a statistics template. Signed-off-by: Lubomir I. Ivanov Signed-off-by: Gehad elrobey --- templatelayout.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ templatelayout.h | 4 ++-- 2 files changed, 49 insertions(+), 2 deletions(-) (limited to 'templatelayout.h') diff --git a/templatelayout.cpp b/templatelayout.cpp index 9245befbe..c52968f09 100644 --- a/templatelayout.cpp +++ b/templatelayout.cpp @@ -108,6 +108,53 @@ QString TemplateLayout::generate() return htmlContent; } +QString TemplateLayout::generateStatistics() +{ + QString htmlContent; + m_engine = new Grantlee::Engine(this); + + QSharedPointer m_templateLoader = + QSharedPointer(new Grantlee::FileSystemTemplateLoader()); + m_templateLoader->setTemplateDirs(QStringList() << getSubsurfaceDataPath("printing_templates/statistics")); + m_engine->addTemplateLoader(m_templateLoader); + + Grantlee::registerMetaType(); + Grantlee::registerMetaType(); + Grantlee::registerMetaType(); + + QVariantHash mapping; + QVariantList years; + + int i = 0; + while (stats_yearly != NULL && stats_yearly[i].period) { + YearInfo year(stats_yearly[i]); + years.append(QVariant::fromValue(year)); + i++; + } + + mapping.insert("years", years); + mapping.insert("template_options", QVariant::fromValue(*templateOptions)); + mapping.insert("print_options", QVariant::fromValue(*PrintOptions)); + + Grantlee::Context c(mapping); + + 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; +} + QString TemplateLayout::readTemplate(QString template_name) { QFile qfile(getSubsurfaceDataPath("printing_templates") + QDir::separator() + template_name); diff --git a/templatelayout.h b/templatelayout.h index 47ca31f4c..d9fa29295 100644 --- a/templatelayout.h +++ b/templatelayout.h @@ -10,7 +10,7 @@ int getTotalWork(print_options *printOptions); void find_all_templates(); -extern QList grantlee_templates; +extern QList grantlee_templates, grantlee_statistics_templates; class TemplateLayout : public QObject { Q_OBJECT @@ -18,6 +18,7 @@ public: TemplateLayout(print_options *PrintOptions, template_options *templateOptions); ~TemplateLayout(); QString generate(); + QString generateStatistics(); static QString readTemplate(QString template_name); static void writeTemplate(QString template_name, QString grantlee_template); @@ -198,7 +199,6 @@ if (property == "grayscale") { GRANTLEE_END_LOOKUP GRANTLEE_BEGIN_LOOKUP(YearInfo) - if (property == "year") { return object.year->period; } else if (property == "dives") { -- cgit v1.2.3-70-g09d2