diff --git a/CMakeLists.txt b/CMakeLists.txt --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.5) set(KF5_MIN_VERSION "5.57.0") -set(PIM_VERSION "5.11.40") +set(PIM_VERSION "5.11.41") project(GrantleeTheme VERSION ${PIM_VERSION}) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,6 +3,7 @@ add_subdirectory(plugin) set(libgrantleetheme_SRCS + genericformatter.cpp grantleetheme.cpp grantleethememanager.cpp grantleethemeengine.cpp @@ -43,6 +44,7 @@ ecm_generate_headers(GrantleeTheme_CamelCase_HEADERS HEADER_NAMES + GenericFormatter GrantleeThemeManager GrantleeTheme GrantleeThemeEngine @@ -74,5 +76,3 @@ install(FILES ${PRI_FILENAME} DESTINATION ${ECM_MKSPECS_INSTALL_DIR}) - - diff --git a/src/genericformatter.h b/src/genericformatter.h new file mode 100644 --- /dev/null +++ b/src/genericformatter.h @@ -0,0 +1,62 @@ +/* + Copyright (c) 2016-2019 Montel Laurent + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef GRANTLEETHEME_GENERICFORMATTER_H +#define GRANTLEETHEME_GENERICFORMATTER_H + +#include "grantleetheme_export.h" + +#include + +#include + +class QString; + +namespace GrantleeTheme { + +class GenericFormatterPrivate; + +/** Convenience class for using a Grantlee theme. */ +class GRANTLEETHEME_EXPORT GenericFormatter +{ +public: + GenericFormatter(); + explicit GenericFormatter(const QString &defaultHtmlMain, const QString &themePath); + ~GenericFormatter(); + + void setDefaultHtmlMainFile(const QString &name); + void setTemplatePath(const QString &path); + + /** Translation domain for the Grantlee localizer. */ + void setApplicationDomain(const QByteArray &domain); + + QString render(const QVariantHash &mapping) const; + QString errorMessage() const; + + /** Set template content from a string rather than a file. */ + void setTemplateContent(const QString &content); + + void reloadTemplate(); + +private: + std::unique_ptr const d; +}; + +} + +#endif // GRANTLEETHEME_GENERICFORMATTER_H diff --git a/src/genericformatter.cpp b/src/genericformatter.cpp new file mode 100644 --- /dev/null +++ b/src/genericformatter.cpp @@ -0,0 +1,109 @@ +/* + Copyright (c) 2016-2019 Montel Laurent + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "genericformatter.h" +#include "grantleethemeengine.h" +#include "grantleeki18nlocalizer.h" + +using namespace GrantleeTheme; + +class GrantleeTheme::GenericFormatterPrivate +{ +public: + GenericFormatterPrivate() + : mEngine(new Engine) + { + } + + QString mThemePath; + QString mDefaultMainFile; + std::unique_ptr mEngine; + QString mErrorMessage; + QSharedPointer mTemplateLoader; + Grantlee::Template mTemplate; +}; + + +GenericFormatter::GenericFormatter(const QString &defaultHtmlMain, const QString &themePath) + : d(new GenericFormatterPrivate) +{ + d->mThemePath = themePath; + d->mDefaultMainFile = defaultHtmlMain; + setTemplatePath(d->mThemePath); +} + +GenericFormatter::GenericFormatter() + : d(new GenericFormatterPrivate) +{ +} + +GenericFormatter::~GenericFormatter() = default; + +void GenericFormatter::setDefaultHtmlMainFile(const QString &name) +{ + if (d->mDefaultMainFile != name) { + d->mDefaultMainFile = name; + reloadTemplate(); + } +} + +void GenericFormatter::setTemplatePath(const QString &path) +{ + if (!d->mTemplateLoader) { + d->mTemplateLoader.reset(new Grantlee::FileSystemTemplateLoader); + } + d->mTemplateLoader->setTemplateDirs(QStringList() << path); + d->mEngine->addTemplateLoader(d->mTemplateLoader); + + reloadTemplate(); +} + +void GenericFormatter::setApplicationDomain(const QByteArray &domain) +{ + d->mEngine->localizer()->setApplicationDomain(domain); +} + +QString GenericFormatter::errorMessage() const +{ + return d->mErrorMessage; +} + +QString GenericFormatter::render(const QVariantHash &mapping) const +{ + Grantlee::Context context(mapping); + context.setLocalizer(d->mEngine->localizer()); + + const QString contentHtml = d->mTemplate->render(&context); + return contentHtml; +} + +void GenericFormatter::setTemplateContent(const QString &content) +{ + d->mTemplate = d->mEngine->newTemplate(content, QStringLiteral("content")); + if (d->mTemplate->error()) { + d->mErrorMessage = d->mTemplate->errorString() + QLatin1String("
"); + } +} + +void GenericFormatter::reloadTemplate() +{ + d->mTemplate = d->mEngine->loadByName(d->mDefaultMainFile); + if (d->mTemplate->error()) { + d->mErrorMessage += d->mTemplate->errorString() + QLatin1String("
"); + } +}