GrantleeTheme

genericformatter.cpp
1/*
2 SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "genericformatter.h"
8using namespace Qt::Literals::StringLiterals;
9
10#include "grantleeki18nlocalizer.h"
11#include "grantleethemeengine.h"
12
13using namespace GrantleeTheme;
14
15class GrantleeTheme::GenericFormatterPrivate
16{
17public:
18 GenericFormatterPrivate()
19 : mEngine(new Engine)
20 {
21 }
22
23 QString mThemePath;
24 QString mDefaultMainFile;
25 std::unique_ptr<Engine> const mEngine;
26 QString mErrorMessage;
29};
30
31GenericFormatter::GenericFormatter(const QString &defaultHtmlMain, const QString &themePath)
32 : d(new GenericFormatterPrivate)
33{
34 d->mThemePath = themePath;
35 d->mDefaultMainFile = defaultHtmlMain;
36 setTemplatePath(d->mThemePath);
37}
38
39GenericFormatter::GenericFormatter()
40 : d(new GenericFormatterPrivate)
41{
42}
43
44GenericFormatter::~GenericFormatter() = default;
45
46void GenericFormatter::setDefaultHtmlMainFile(const QString &name)
47{
48 if (d->mDefaultMainFile != name) {
49 d->mDefaultMainFile = name;
50 reloadTemplate();
51 }
52}
53
54void GenericFormatter::setTemplatePath(const QString &path)
55{
56 if (!d->mTemplateLoader) {
57 d->mTemplateLoader.reset(new KTextTemplate::FileSystemTemplateLoader);
58 }
59 d->mTemplateLoader->setTemplateDirs(QStringList() << path);
60 d->mEngine->addTemplateLoader(d->mTemplateLoader);
61
62 reloadTemplate();
63}
64
66{
67 d->mEngine->localizer()->setApplicationDomain(domain);
68}
69
70QString GenericFormatter::errorMessage() const
71{
72 return d->mErrorMessage;
73}
74
75QString GenericFormatter::render(const QVariantHash &mapping) const
76{
77 KTextTemplate::Context context(mapping);
78 context.setLocalizer(d->mEngine->localizer());
79
80 const QString contentHtml = d->mTemplate->render(&context);
81 return contentHtml;
82}
83
85{
86 d->mTemplate = d->mEngine->newTemplate(content, QStringLiteral("content"));
87 if (d->mTemplate->error()) {
88 d->mErrorMessage = d->mTemplate->errorString() + "<br>"_L1;
89 }
90}
91
92void GenericFormatter::reloadTemplate()
93{
94 d->mTemplate = d->mEngine->loadByName(d->mDefaultMainFile);
95 if (d->mTemplate->error()) {
96 d->mErrorMessage += d->mTemplate->errorString() + "<br>"_L1;
97 }
98}
void setTemplateContent(const QString &content)
Set template content from a string rather than a file.
void setApplicationDomain(const QByteArray &domain)
Translation domain for the Grantlee localizer.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:07:43 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.