From d74bb6df2688e670299cb8258d66bd309e43930e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Vr=C3=A1til?= Date: Thu, 30 Jun 2016 11:41:08 +0200 Subject: [PATCH] Add GrantleeTheme::Engine to avoid code duplication GrantleeTheme::Engine is a simple subclass of Grantlee::Engine which sets up the correct plugin path and loads the default KDE plugins. It also provides easy access to Ki18n localizer. --- src/CMakeLists.txt | 2 ++ src/grantleetheme.cpp | 7 ++-- src/grantleethemeengine.cpp | 64 +++++++++++++++++++++++++++++++++++++ src/grantleethemeengine.h | 47 +++++++++++++++++++++++++++ 4 files changed, 115 insertions(+), 5 deletions(-) create mode 100644 src/grantleethemeengine.cpp create mode 100644 src/grantleethemeengine.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 1843cff..3ff568c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -7,6 +7,7 @@ add_subdirectory(plugin) set(libgrantleetheme_SRCS grantleetheme.cpp grantleethememanager.cpp + grantleethemeengine.cpp grantleeki18nlocalizer.cpp qtresourcetemplateloader.cpp ) @@ -46,6 +47,7 @@ ecm_generate_headers(GrantleeTheme_CamelCase_HEADERS HEADER_NAMES GrantleeThemeManager GrantleeTheme + GrantleeThemeEngine QtResourceTemplateLoader GrantleeKi18nLocalizer REQUIRED_HEADERS GrantleeTheme_HEADERS diff --git a/src/grantleetheme.cpp b/src/grantleetheme.cpp index ecb4fd5..b86fc3a 100644 --- a/src/grantleetheme.cpp +++ b/src/grantleetheme.cpp @@ -18,6 +18,7 @@ #include "grantleetheme.h" #include "grantleetheme_p.h" #include "grantleetheme_debug.h" +#include "grantleethemeengine.h" #include "config-grantleetheme.h" #include @@ -57,11 +58,7 @@ ThemePrivate::~ThemePrivate() void ThemePrivate::setupEngine() { - sEngine = new Grantlee::Engine; - sEngine->addPluginPath(QStringLiteral(GRANTLEE_PLUGIN_INSTALL_DIR)); - sEngine->addDefaultLibrary(QStringLiteral("grantlee_i18ntags")); - sEngine->addDefaultLibrary(QStringLiteral("kde_grantlee_plugin")); - sEngine->setSmartTrimEnabled(true); + sEngine = new GrantleeTheme::Engine(); } void ThemePrivate::setupLoader() diff --git a/src/grantleethemeengine.cpp b/src/grantleethemeengine.cpp new file mode 100644 index 0000000..714426f --- /dev/null +++ b/src/grantleethemeengine.cpp @@ -0,0 +1,64 @@ +/* + Copyright (c) 2016 Daniel Vrátil + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License, version 2, as + published by the Free Software Foundation. + + 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 "grantleethemeengine.h" +#include "grantleeki18nlocalizer.h" + +#include "config-grantleetheme.h" + +using namespace GrantleeTheme; + +class GrantleeTheme::Engine::Private +{ +public: + Private() + { + } + + ~Private() + { + } + + QWeakPointer localizer; +}; + + +Engine::Engine(QObject *parent) + : Grantlee::Engine(parent) + , d(new Private) +{ + addPluginPath(QStringLiteral(GRANTLEE_PLUGIN_INSTALL_DIR)); + addDefaultLibrary(QStringLiteral("grantlee_i18ntags")); + addDefaultLibrary(QStringLiteral("kde_grantlee_plugin")); + addDefaultLibrary(QStringLiteral("grantlee_scriptabletags")); + setSmartTrimEnabled(true); +} + +Engine::~Engine() +{ + delete d; +} + +QSharedPointer Engine::localizer() const +{ + auto loc = d->localizer.toStrongRef(); + if (!loc) { + loc.reset(new GrantleeKi18nLocalizer()); + d->localizer = loc.toWeakRef(); + } + return loc; +} diff --git a/src/grantleethemeengine.h b/src/grantleethemeengine.h new file mode 100644 index 0000000..ae129f5 --- /dev/null +++ b/src/grantleethemeengine.h @@ -0,0 +1,47 @@ +/* + Copyright (c) 2016 Daniel Vrátil + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License, version 2, as + published by the Free Software Foundation. + + 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 GRANTLEETHEMEENGINE_H +#define GRANTLEETHEMEENGINE_H + +#include +#include + +#include "grantleetheme_export.h" + +namespace GrantleeTheme { + +class GrantleeKi18nLocalizer; + +class GRANTLEETHEME_EXPORT Engine : public Grantlee::Engine +{ + Q_OBJECT + +public: + Engine(QObject *parent = Q_NULLPTR); + ~Engine() Q_DECL_OVERRIDE; + + QSharedPointer localizer() const; + +private: + class Private; + Private *const d; +}; + +} + +#endif -- GitLab