GrantleeTheme

genericformatter.cpp
1 /*
2  SPDX-FileCopyrightText: 2016-2022 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "genericformatter.h"
8 #include "grantleeki18nlocalizer.h"
9 #include "grantleethemeengine.h"
10 
11 using namespace GrantleeTheme;
12 
13 class GrantleeTheme::GenericFormatterPrivate
14 {
15 public:
16  GenericFormatterPrivate()
17  : mEngine(new Engine)
18  {
19  }
20 
21  QString mThemePath;
22  QString mDefaultMainFile;
23  std::unique_ptr<Engine> const mEngine;
24  QString mErrorMessage;
25 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
27  Grantlee::Template mTemplate;
28 #else
30  KTextTemplate::Template mTemplate;
31 #endif
32 };
33 
34 GenericFormatter::GenericFormatter(const QString &defaultHtmlMain, const QString &themePath)
35  : d(new GenericFormatterPrivate)
36 {
37  d->mThemePath = themePath;
38  d->mDefaultMainFile = defaultHtmlMain;
39  setTemplatePath(d->mThemePath);
40 }
41 
42 GenericFormatter::GenericFormatter()
43  : d(new GenericFormatterPrivate)
44 {
45 }
46 
47 GenericFormatter::~GenericFormatter() = default;
48 
49 void GenericFormatter::setDefaultHtmlMainFile(const QString &name)
50 {
51  if (d->mDefaultMainFile != name) {
52  d->mDefaultMainFile = name;
53  reloadTemplate();
54  }
55 }
56 
57 void GenericFormatter::setTemplatePath(const QString &path)
58 {
59  if (!d->mTemplateLoader) {
60 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
61  d->mTemplateLoader.reset(new Grantlee::FileSystemTemplateLoader);
62 #else
63  d->mTemplateLoader.reset(new KTextTemplate::FileSystemTemplateLoader);
64 #endif
65  }
66  d->mTemplateLoader->setTemplateDirs(QStringList() << path);
67  d->mEngine->addTemplateLoader(d->mTemplateLoader);
68 
69  reloadTemplate();
70 }
71 
73 {
74  d->mEngine->localizer()->setApplicationDomain(domain);
75 }
76 
77 QString GenericFormatter::errorMessage() const
78 {
79  return d->mErrorMessage;
80 }
81 
82 QString GenericFormatter::render(const QVariantHash &mapping) const
83 {
84 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
85  Grantlee::Context context(mapping);
86 #else
87  KTextTemplate::Context context(mapping);
88 #endif
89  context.setLocalizer(d->mEngine->localizer());
90 
91  const QString contentHtml = d->mTemplate->render(&context);
92  return contentHtml;
93 }
94 
96 {
97  d->mTemplate = d->mEngine->newTemplate(content, QStringLiteral("content"));
98  if (d->mTemplate->error()) {
99  d->mErrorMessage = d->mTemplate->errorString() + QLatin1String("<br>");
100  }
101 }
102 
103 void GenericFormatter::reloadTemplate()
104 {
105  d->mTemplate = d->mEngine->loadByName(d->mDefaultMainFile);
106  if (d->mTemplate->error()) {
107  d->mErrorMessage += d->mTemplate->errorString() + QLatin1String("<br>");
108  }
109 }
void setApplicationDomain(const QByteArray &domain)
Translation domain for the Grantlee localizer.
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-2022 The KDE developers.
Generated on Wed Sep 28 2022 04:06:59 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.