From: "Lubomir I. Ivanov" <[email protected]>

The current Grantlee template loading scheme does not
allow a preprocessing layer. With the recent DiveObjectHelper
changes the layer is required if we don't want to add a set
of dummy methods and Q_PROPERTIES which will only inflate
the DiveObjectHelper class.

Use the already present helper readTemplate() to load the
raw HTML template and pass it to a static function which
does some variable replacement to accomudate DiveObjectHelper.

This change is done for the sake of not breaking the Grantlee
HTML variables on the user side!

Signed-off-by: Lubomir I. Ivanov <[email protected]>
---

if anyone wants to do the regex variant instead of QString::replace(),
you can amend preprocessTemplate() once the patch is pushed to master.
---
 desktop-widgets/templatelayout.cpp | 40 +++++++++++++++++++++++++++++++-------
 1 file changed, 33 insertions(+), 7 deletions(-)

diff --git a/desktop-widgets/templatelayout.cpp 
b/desktop-widgets/templatelayout.cpp
index f955666..34443b9 100644
--- a/desktop-widgets/templatelayout.cpp
+++ b/desktop-widgets/templatelayout.cpp
@@ -50,6 +50,33 @@ TemplateLayout::~TemplateLayout()
        delete m_engine;
 }
 
+/* a HTML pre-processor stage. acts like a compatibility layer
+ * between some Grantlee variables and DiveObjectHelper Q_PROPERTIES;
+ */
+static QString preprocessTemplate(const QString &in)
+{
+       QString out = in;
+
+       QList<QPair<QString, QString> > list;
+       list << qMakePair(QString("dive.weight"), QString("dive.weights."));
+       list << qMakePair(QString("dive.weights"), QString("dive.weightList"));
+       list << qMakePair(QString("dive.cylinder"), QString("dive.cylinders."));
+       list << qMakePair(QString("dive.cylinders"), 
QString("dive.cylinderList"));
+
+       /* 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 }}' */
+       for (int i = 0; i < list.length(); i++) {
+               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;
+}
+
 QString TemplateLayout::generate()
 {
        int progress = 0;
@@ -58,12 +85,6 @@ QString TemplateLayout::generate()
        QString htmlContent;
        delete m_engine;
        m_engine = new Grantlee::Engine(this);
-
-       QSharedPointer<Grantlee::FileSystemTemplateLoader> m_templateLoader =
-               QSharedPointer<Grantlee::FileSystemTemplateLoader>(new 
Grantlee::FileSystemTemplateLoader());
-       m_templateLoader->setTemplateDirs(QStringList() << 
getPrintingTemplatePathUser());
-       m_engine->addTemplateLoader(m_templateLoader);
-
        Grantlee::registerMetaType<template_options>();
        Grantlee::registerMetaType<print_options>();
 
@@ -85,7 +106,12 @@ QString TemplateLayout::generate()
        c.insert("template_options", QVariant::fromValue(*templateOptions));
        c.insert("print_options", QVariant::fromValue(*PrintOptions));
 
-       Grantlee::Template t = m_engine->loadByName(PrintOptions->p_template);
+       /* 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);
        if (!t || t->error()) {
                qDebug() << "Can't load template";
                return htmlContent;
-- 
1.7.11.msysgit.0

_______________________________________________
subsurface mailing list
[email protected]
http://lists.subsurface-divelog.org/cgi-bin/mailman/listinfo/subsurface

Reply via email to