Commit 2e482c3c authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

Prepare to build against KF6::TextTemplate (in progress)

parent 11b8cafb
Pipeline #131075 passed with stage
in 34 seconds
......@@ -41,8 +41,13 @@ find_package(KF5I18n ${KF5_MIN_VERSION} CONFIG REQUIRED)
find_package(KF5IconThemes ${KF5_MIN_VERSION} CONFIG REQUIRED)
find_package(KF5NewStuff ${KF5_MIN_VERSION} CONFIG REQUIRED)
find_package(Grantlee5 ${GRANTLEE_VERSION} CONFIG REQUIRED)
if (QT_MAJOR_VERSION STREQUAL "5")
find_package(Grantlee5 ${GRANTLEE_VERSION} CONFIG REQUIRED)
set(MAIN_LIB Grantlee5::Templates)
else()
find_package(KF6TextTemplate ${KF5_MIN_VERSION} CONFIG REQUIRED)
set(MAIN_LIB KF6TextTemplate::Templates)
endif()
ecm_setup_version(PROJECT VARIABLE_PREFIX GRANTLEETHEME
VERSION_HEADER "${CMAKE_CURRENT_BINARY_DIR}/grantleetheme_version.h"
PACKAGE_VERSION_FILE "${CMAKE_CURRENT_BINARY_DIR}/KF5GrantleeThemeConfigVersion.cmake"
......
......@@ -37,15 +37,13 @@ generate_export_header(KF5GrantleeTheme BASE_NAME grantleetheme)
target_include_directories(KF5GrantleeTheme INTERFACE "$<INSTALL_INTERFACE:${KDE_INSTALL_INCLUDEDIR_KF}/GrantleeTheme/>")
target_include_directories(KF5GrantleeTheme PUBLIC "$<BUILD_INTERFACE:${GrantleeTheme_SOURCE_DIR}/src;${GrantleeTheme_BINARY_DIR}/src;>")
target_link_libraries(KF5GrantleeTheme
PUBLIC
Grantlee5::Templates
${MAIN_LIB}
PRIVATE
KF5::NewStuff
KF5::I18n
KF5::NewStuff
KF5::I18n
)
set_target_properties(KF5GrantleeTheme PROPERTIES
VERSION ${GRANTLEETHEME_VERSION}
SOVERSION ${GRANTLEETHEME_SOVERSION}
......
......@@ -22,8 +22,13 @@ public:
QString mDefaultMainFile;
std::unique_ptr<Engine> const mEngine;
QString mErrorMessage;
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QSharedPointer<Grantlee::FileSystemTemplateLoader> mTemplateLoader;
Grantlee::Template mTemplate;
#else
QSharedPointer<KTextTemplate::FileSystemTemplateLoader> mTemplateLoader;
KTextTemplate::Template mTemplate;
#endif
};
GenericFormatter::GenericFormatter(const QString &defaultHtmlMain, const QString &themePath)
......@@ -52,7 +57,11 @@ void GenericFormatter::setDefaultHtmlMainFile(const QString &name)
void GenericFormatter::setTemplatePath(const QString &path)
{
if (!d->mTemplateLoader) {
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
d->mTemplateLoader.reset(new Grantlee::FileSystemTemplateLoader);
#else
d->mTemplateLoader.reset(new KTextTemplate::FileSystemTemplateLoader);
#endif
}
d->mTemplateLoader->setTemplateDirs(QStringList() << path);
d->mEngine->addTemplateLoader(d->mTemplateLoader);
......@@ -72,7 +81,11 @@ QString GenericFormatter::errorMessage() const
QString GenericFormatter::render(const QVariantHash &mapping) const
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
Grantlee::Context context(mapping);
#else
KTextTemplate::Context context(mapping);
#endif
context.setLocalizer(d->mEngine->localizer());
const QString contentHtml = d->mTemplate->render(&context);
......
......@@ -9,14 +9,21 @@
#include "grantleetheme_debug.h"
#include <QDebug>
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <grantlee/safestring.h>
#else
#include <KTextTemplate/safestring.h>
#endif
#include <KLocalizedString>
using namespace GrantleeTheme;
GrantleeKi18nLocalizer::GrantleeKi18nLocalizer()
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
: Grantlee::QtLocalizer()
#else
: KTextTemplate::QtLocalizer()
#endif
{
}
......@@ -101,7 +108,11 @@ QString GrantleeKi18nLocalizer::localizeMonetaryValue(qreal value, const QString
QString GrantleeKi18nLocalizer::currentLocale() const
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QString locale = Grantlee::QtLocalizer::currentLocale();
#else
QString locale = KTextTemplate::QtLocalizer::currentLocale();
#endif
const int f = locale.indexOf(QLatin1Char('_'));
if (f >= 0) {
locale.truncate(f);
......
......@@ -6,9 +6,13 @@
*/
#pragma once
#include "grantleetheme_export.h"
#include <QObject>
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <grantlee/qtlocalizer.h>
#else
#include <KTextTemplate/qtlocalizer.h>
#endif
#include <QLocale>
#include <QVariantHash>
......@@ -19,7 +23,11 @@ namespace GrantleeTheme
/**
* @brief The GrantleeKi18nLocalizer class
*/
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
class GRANTLEETHEME_EXPORT GrantleeKi18nLocalizer : public Grantlee::QtLocalizer
#else
class GRANTLEETHEME_EXPORT GrantleeKi18nLocalizer : public KTextTemplate::QtLocalizer
#endif
{
public:
explicit GrantleeKi18nLocalizer();
......
......@@ -21,7 +21,11 @@
using namespace GrantleeTheme;
QSharedPointer<GrantleeKi18nLocalizer> GrantleeTheme::ThemePrivate::sLocalizer;
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
Grantlee::Engine *GrantleeTheme::ThemePrivate::sEngine = nullptr;
#else
KTextTemplate::Engine *GrantleeTheme::ThemePrivate::sEngine = nullptr;
#endif
ThemePrivate::ThemePrivate()
: QSharedData()
......@@ -49,7 +53,6 @@ void ThemePrivate::setupLoader()
dir.cdUp();
templatePaths << dir.absolutePath();
}
loader = QSharedPointer<GrantleeTheme::QtResourceTemplateLoader>::create();
loader->setTemplateDirs(templatePaths);
loader->setTheme(dirName);
......@@ -59,20 +62,31 @@ void ThemePrivate::setupLoader()
}
sEngine->addTemplateLoader(loader);
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
Grantlee::Context ThemePrivate::createContext(const QVariantHash &data, const QByteArray &applicationDomain)
#else
KTextTemplate::Context ThemePrivate::createContext(const QVariantHash &data, const QByteArray &applicationDomain)
#endif
{
if (!sLocalizer) {
sLocalizer.reset(new GrantleeKi18nLocalizer());
}
sLocalizer->setApplicationDomain(applicationDomain);
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
Grantlee::Context ctx(data);
#else
KTextTemplate::Context ctx(data);
#endif
ctx.setLocalizer(sLocalizer);
return ctx;
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QString ThemePrivate::errorTemplate(const QString &reason, const QString &origTemplateName, const Grantlee::Template &failedTemplate)
#else
QString ThemePrivate::errorTemplate(const QString &reason, const QString &origTemplateName, const KTextTemplate::Template &failedTemplate)
#endif
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
Grantlee::Template tpl = sEngine->newTemplate(QStringLiteral("<h1>{{ error }}</h1>\n"
"<b>%1:</b> {{ templateName }}<br>\n"
"<b>%2:</b> {{ errorMessage }}")
......@@ -80,6 +94,15 @@ QString ThemePrivate::errorTemplate(const QString &reason, const QString &origTe
QStringLiteral("TemplateError"));
Grantlee::Context ctx = createContext();
#else
KTextTemplate::Template tpl = sEngine->newTemplate(QStringLiteral("<h1>{{ error }}</h1>\n"
"<b>%1:</b> {{ templateName }}<br>\n"
"<b>%2:</b> {{ errorMessage }}")
.arg(i18n("Template"), i18n("Error message")),
QStringLiteral("TemplateError"));
KTextTemplate::Context ctx = createContext();
#endif
ctx.insert(QStringLiteral("error"), reason);
ctx.insert(QStringLiteral("templateName"), origTemplateName);
const QString errorString = failedTemplate ? failedTemplate->errorString() : i18n("(null template)");
......@@ -193,13 +216,19 @@ QString Theme::render(const QString &templateName, const QVariantHash &data, con
<< ", please check your installation. Tried in these dirs:" << d->loader->templateDirs();
return {};
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
Grantlee::Template tpl = d->loader->loadByName(templateName, ThemePrivate::sEngine);
#else
KTextTemplate::Template tpl = d->loader->loadByName(templateName, ThemePrivate::sEngine);
#endif
if (!tpl || tpl->error()) {
return d->errorTemplate(i18n("Template parsing error"), templateName, tpl);
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
Grantlee::Context ctx = d->createContext(data, applicationDomain);
#else
KTextTemplate::Context ctx = d->createContext(data, applicationDomain);
#endif
const QString result = tpl->render(&ctx);
if (tpl->error()) {
return d->errorTemplate(i18n("Template rendering error"), templateName, tpl);
......
......@@ -8,10 +8,15 @@
#include "grantleeki18nlocalizer.h"
#include "grantleetheme.h"
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <grantlee/engine.h>
#include <grantlee/template.h>
#include <grantlee/templateloader.h>
#else
#include <KTextTemplate/engine.h>
#include <KTextTemplate/template.h>
#include <KTextTemplate/templateloader.h>
#endif
namespace GrantleeTheme
{
......@@ -24,8 +29,13 @@ public:
static void setupEngine();
void setupLoader();
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
Q_REQUIRED_RESULT Grantlee::Context createContext(const QVariantHash &data = QVariantHash(), const QByteArray &applicationDomain = QByteArray());
Q_REQUIRED_RESULT QString errorTemplate(const QString &reason, const QString &templateName, const Grantlee::Template &errorTemplate);
#else
Q_REQUIRED_RESULT KTextTemplate::Context createContext(const QVariantHash &data = QVariantHash(), const QByteArray &applicationDomain = QByteArray());
Q_REQUIRED_RESULT QString errorTemplate(const QString &reason, const QString &templateName, const KTextTemplate::Template &errorTemplate);
#endif
QStringList displayExtraVariables;
QString themeFileName;
......@@ -35,11 +45,17 @@ public:
QStringList absolutePaths;
QString author;
QString email;
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QSharedPointer<Grantlee::FileSystemTemplateLoader> loader;
#else
QSharedPointer<KTextTemplate::FileSystemTemplateLoader> loader;
#endif
static QSharedPointer<GrantleeKi18nLocalizer> sLocalizer;
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
static Grantlee::Engine *sEngine;
#else
static KTextTemplate::Engine *sEngine;
#endif
};
}
......@@ -22,7 +22,11 @@ public:
};
Engine::Engine(QObject *parent)
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
: Grantlee::Engine(parent)
#else
: KTextTemplate::Engine(parent)
#endif
, d(new GrantleeTheme::EnginePrivate)
{
addPluginPath(QStringLiteral(GRANTLEE_PLUGIN_INSTALL_DIR));
......
......@@ -7,7 +7,11 @@
#pragma once
#include <QSharedPointer>
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <grantlee/engine.h>
#else
#include <KTextTemplate/engine.h>
#endif
#include "grantleetheme_export.h"
......@@ -18,7 +22,11 @@ class EnginePrivate;
/**
* @brief The Engine class
*/
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
class GRANTLEETHEME_EXPORT Engine : public Grantlee::Engine
#else
class GRANTLEETHEME_EXPORT Engine : public KTextTemplate::Engine
#endif
{
Q_OBJECT
......
......@@ -13,7 +13,7 @@
#include <KConfigGroup>
#include <KDirWatch>
#include <KLocalizedString>
#include <KNewStuff3/KNS3/QtQuickDialogWrapper>
#include <KNS3/QtQuickDialogWrapper>
#include <KSharedConfig>
#include <KToggleAction>
#include <QAction>
......
......@@ -21,7 +21,7 @@ if (COMPILE_WITH_UNITY_CMAKE_SUPPORT)
endif()
kpim_grantlee_adjust_plugin_name(kde_grantlee_plugin)
target_link_libraries(kde_grantlee_plugin
Grantlee5::Templates
${MAIN_LIB}
KF5::IconThemes
KF5::GuiAddons
KF5::ConfigWidgets
......
......@@ -5,20 +5,33 @@
*/
#include "color.h"
#include <QObject>
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <grantlee/exception.h>
#include <grantlee/metatype.h>
#include <grantlee/parser.h>
#include <grantlee/variable.h>
#else
#include <KTextTemplate/exception.h>
#include <KTextTemplate/metatype.h>
#include <KTextTemplate/parser.h>
#include <KTextTemplate/variable.h>
#endif
#include <KColorUtils>
#include <QColor>
static QColor inputToColor(const QVariant &v)
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
if (v.canConvert<Grantlee::SafeString>()) {
return QColor(v.value<Grantlee::SafeString>().get());
}
#else
if (v.canConvert<KTextTemplate::SafeString>()) {
return QColor(v.value<KTextTemplate::SafeString>().get());
}
#endif
return v.value<QColor>();
}
......@@ -98,30 +111,49 @@ QVariant ColorSetAlphaFilter::doFilter(const QVariant &input, const QVariant &ar
}
ColorMixTag::ColorMixTag(QObject *parent)
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
: Grantlee::AbstractNodeFactory(parent)
#else
: KTextTemplate::AbstractNodeFactory(parent)
#endif
{
}
ColorMixTag::~ColorMixTag() = default;
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
Grantlee::Node *ColorMixTag::getNode(const QString &tagContent, Grantlee::Parser *p) const
#else
KTextTemplate::Node *ColorMixTag::getNode(const QString &tagContent, KTextTemplate::Parser *p) const
#endif
{
Q_UNUSED(p)
const auto parts = smartSplit(tagContent);
if (parts.size() != 4 && parts.size() != 6) {
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
throw Grantlee::Exception(Grantlee::TagSyntaxError, QStringLiteral("colormix tag needs 3 or 5 arguments"));
#else
throw KTextTemplate::Exception(KTextTemplate::TagSyntaxError, QStringLiteral("colormix tag needs 3 or 5 arguments"));
#endif
}
bool ok = false;
const auto ratio = parts.at(3).toDouble(&ok);
if (!ok) {
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
throw Grantlee::Exception(Grantlee::TagSyntaxError, QStringLiteral("colormix: invalid ratio"));
#else
throw KTextTemplate::Exception(KTextTemplate::TagSyntaxError, QStringLiteral("colormix: invalid ratio"));
#endif
}
QString varName;
if (parts.size() == 6) {
if (parts.at(4) != QLatin1String("as")) {
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
throw Grantlee::Exception(Grantlee::TagSyntaxError, QStringLiteral("colormix: syntax error"));
#else
throw KTextTemplate::Exception(KTextTemplate::TagSyntaxError, QStringLiteral("colormix: syntax error"));
#endif
}
varName = parts.at(5);
}
......@@ -130,7 +162,11 @@ Grantlee::Node *ColorMixTag::getNode(const QString &tagContent, Grantlee::Parser
}
ColorMixNode::ColorMixNode(const QString &color1Name, const QString &color2Name, double ratio, const QString &varName, QObject *parent)
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
: Grantlee::Node(parent)
#else
: KTextTemplate::Node(parent)
#endif
, m_color1Name(color1Name)
, m_color2Name(color2Name)
, m_varName(varName)
......@@ -139,18 +175,27 @@ ColorMixNode::ColorMixNode(const QString &color1Name, const QString &color2Name,
}
ColorMixNode::~ColorMixNode() = default;
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
static QColor resolveColor(const QString &name, Grantlee::Context *c)
#else
static QColor resolveColor(const QString &name, KTextTemplate::Context *c)
#endif
{
if (name.startsWith(QLatin1Char('"')) && name.endsWith(QLatin1Char('"'))) {
return QColor(QStringView(name).mid(1, name.size() - 2));
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
const auto val = Grantlee::Variable(name).resolve(c);
#else
const auto val = KTextTemplate::Variable(name).resolve(c);
#endif
return val.value<QColor>();
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
void ColorMixNode::render(Grantlee::OutputStream *stream, Grantlee::Context *c) const
#else
void ColorMixNode::render(KTextTemplate::OutputStream *stream, KTextTemplate::Context *c) const
#endif
{
const auto col1 = resolveColor(m_color1Name, c);
const auto col2 = resolveColor(m_color2Name, c);
......
......@@ -5,9 +5,14 @@
*/
#pragma once
#include <QObject>
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <grantlee/filter.h>
#include <grantlee/node.h>
#else
#include <KTextTemplate/filter.h>
#include <KTextTemplate/node.h>
#endif
/**
* @name colorHexRgb filter
......@@ -18,7 +23,11 @@
* {{ myColor|colorHexRgb }}
* @endcode
*/
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
class ColorHexRgbFilter : public Grantlee::Filter
#else
class ColorHexRgbFilter : public KTextTemplate::Filter
#endif
{
public:
QVariant doFilter(const QVariant &input, const QVariant &arg, bool autoescape) const override;
......@@ -33,7 +42,11 @@ public:
* {{ myColor|colorCssRgba }}
* @endcode
*/
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
class ColorCssRgbaFilter : public Grantlee::Filter
#else
class ColorCssRgbaFilter : public KTextTemplate::Filter
#endif
{
public:
QVariant doFilter(const QVariant &input, const QVariant &arg, bool autoescape) const override;
......@@ -48,7 +61,11 @@ public:
* {{ myColor|colorLighter:<factor> }}
* @endcode
*/
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
class ColorLighterFilter : public Grantlee::Filter
#else
class ColorLighterFilter : public KTextTemplate::Filter
#endif
{
public:
QVariant doFilter(const QVariant &input, const QVariant &arg, bool autoescape) const override;
......@@ -63,7 +80,11 @@ public:
* {{ myColor|colorDarker:<factor> }}
* @endcode
*/
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
class ColorDarkerFilter : public Grantlee::Filter
#else
class ColorDarkerFilter : public KTextTemplate::Filter
#endif
{
public:
QVariant doFilter(const QVariant &input, const QVariant &arg, bool autoescape) const override;
......@@ -78,7 +99,11 @@ public:
* {{ myColor|colorSetAlpha:<alpha-value-from-0-to-255> }}
* @endcode
*/
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
class ColorSetAlphaFilter : public Grantlee::Filter
#else
class ColorSetAlphaFilter : public KTextTemplate::Filter
#endif
{
public:
QVariant doFilter(const QVariant &input, const QVariant &arg, bool autoescape) const override;
......@@ -95,22 +120,38 @@ public:
*
* The tag generates a rgba() CSS color definition.
*/
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
class ColorMixTag : public Grantlee::AbstractNodeFactory
#else
class ColorMixTag : public KTextTemplate::AbstractNodeFactory
#endif
{
Q_OBJECT
public:
explicit ColorMixTag(QObject *parent = nullptr);
~ColorMixTag() override;
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
Grantlee::Node *getNode(const QString &tagContent, Grantlee::Parser *p) const override;
#else
KTextTemplate::Node *getNode(const QString &tagContent, KTextTemplate::Parser *p) const override;
#endif
};
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
class ColorMixNode : public Grantlee::Node
#else
class ColorMixNode : public KTextTemplate::Node
#endif
{
Q_OBJECT
public:
explicit ColorMixNode(const QString &color1Name, const QString &color2Name, double ratio, const QString &varName, QObject *parent = nullptr);
~ColorMixNode() override;
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
void render(Grantlee::OutputStream *stream, Grantlee::Context *c) const override;
#else
void render(KTextTemplate::OutputStream *stream, KTextTemplate::Context *c) const override;
#endif
private:
QString m_color1Name;
......
......@@ -5,8 +5,12 @@
*/
#include "colorscheme.h"
#include <QObject>
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <grantlee/metatype.h>
#else
#include <KTextTemplate/metatype.h>
#endif
#include <KColorScheme>
......@@ -45,5 +49,9 @@ GRANTLEE_END_LOOKUP
void ColorScheme::registerMetaType()
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
Grantlee::registerMetaType<KColorScheme>();
#else
KTextTemplate::registerMetaType<KColorScheme>();
#endif
}
......@@ -6,21 +6,33 @@
*/
#include "icon.h"
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <grantlee/exception.h>
#include <grantlee/parser.h>
#include <grantlee/variable.h>
#else
#include <KTextTemplate/exception.h>
#include <KTextTemplate/parser.h>
#include <KTextTemplate/variable.h>
#endif
#include <KIconLoader>
IconTag::IconTag(QObject *parent)
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
: Grantlee::AbstractNodeFactory(parent)
#else
: KTextTemplate::AbstractNodeFactory(parent)
#endif
{
}
IconTag::~IconTag() = default;
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
Grantlee::Node *IconTag::getNode(const QString &