GrantleeTheme

genericformatter.cpp
1 /*
2  Copyright (c) 2016-2020 Laurent Montel <[email protected]>
3 
4  This program is free software; you can redistribute it and/or modify it
5  under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful, but
10  WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #include "genericformatter.h"
20 #include "grantleethemeengine.h"
21 #include "grantleeki18nlocalizer.h"
22 
23 using namespace GrantleeTheme;
24 
25 class GrantleeTheme::GenericFormatterPrivate
26 {
27 public:
28  GenericFormatterPrivate()
29  : mEngine(new Engine)
30  {
31  }
32 
33  QString mThemePath;
34  QString mDefaultMainFile;
35  std::unique_ptr<Engine> mEngine;
36  QString mErrorMessage;
38  Grantlee::Template mTemplate;
39 };
40 
41 GenericFormatter::GenericFormatter(const QString &defaultHtmlMain, const QString &themePath)
42  : d(new GenericFormatterPrivate)
43 {
44  d->mThemePath = themePath;
45  d->mDefaultMainFile = defaultHtmlMain;
46  setTemplatePath(d->mThemePath);
47 }
48 
49 GenericFormatter::GenericFormatter()
50  : d(new GenericFormatterPrivate)
51 {
52 }
53 
54 GenericFormatter::~GenericFormatter() = default;
55 
56 void GenericFormatter::setDefaultHtmlMainFile(const QString &name)
57 {
58  if (d->mDefaultMainFile != name) {
59  d->mDefaultMainFile = name;
60  reloadTemplate();
61  }
62 }
63 
64 void GenericFormatter::setTemplatePath(const QString &path)
65 {
66  if (!d->mTemplateLoader) {
67  d->mTemplateLoader.reset(new Grantlee::FileSystemTemplateLoader);
68  }
69  d->mTemplateLoader->setTemplateDirs(QStringList() << path);
70  d->mEngine->addTemplateLoader(d->mTemplateLoader);
71 
72  reloadTemplate();
73 }
74 
76 {
77  d->mEngine->localizer()->setApplicationDomain(domain);
78 }
79 
80 QString GenericFormatter::errorMessage() const
81 {
82  return d->mErrorMessage;
83 }
84 
85 QString GenericFormatter::render(const QVariantHash &mapping) const
86 {
87  Grantlee::Context context(mapping);
88  context.setLocalizer(d->mEngine->localizer());
89 
90  const QString contentHtml = d->mTemplate->render(&context);
91  return contentHtml;
92 }
93 
95 {
96  d->mTemplate = d->mEngine->newTemplate(content, QStringLiteral("content"));
97  if (d->mTemplate->error()) {
98  d->mErrorMessage = d->mTemplate->errorString() + QLatin1String("<br>");
99  }
100 }
101 
102 void GenericFormatter::reloadTemplate()
103 {
104  d->mTemplate = d->mEngine->loadByName(d->mDefaultMainFile);
105  if (d->mTemplate->error()) {
106  d->mErrorMessage += d->mTemplate->errorString() + QLatin1String("<br>");
107  }
108 }
void setApplicationDomain(const QByteArray &domain)
Translation domain for the Grantlee localizer.
The Engine class.
void setTemplateContent(const QString &content)
Set template content from a string rather than a file.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed May 27 2020 22:46:09 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.