GrantleeTheme

icon.cpp
1/*
2 * SPDX-FileCopyrightText: 2015 Daniel Vrátil <dvratil@redhat.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 *
6 */
7
8#include "icon.h"
9#include <KTextTemplate/Exception>
10#include <KTextTemplate/Parser>
11#include <KTextTemplate/Variable>
12
13#include <KIconLoader>
14
15IconTag::IconTag(QObject *parent)
16 : KTextTemplate::AbstractNodeFactory(parent)
17{
18}
19
20IconTag::~IconTag() = default;
21KTextTemplate::Node *IconTag::getNode(const QString &tagContent, KTextTemplate::Parser *p) const
22{
23 Q_UNUSED(p)
24
25 static QHash<QString, int> sizeOrGroupLookup = {{QStringLiteral("toolbar"), KIconLoader::Toolbar},
26 {QStringLiteral("maintoolbar"), KIconLoader::MainToolbar},
27 {QStringLiteral("small"), KIconLoader::Small},
28 {QStringLiteral("dialog"), KIconLoader::Dialog},
29 {QStringLiteral("sizesmall"), KIconLoader::SizeSmall},
30 {QStringLiteral("sizesmallmedium"), KIconLoader::SizeSmallMedium},
31 {QStringLiteral("sizemedium"), KIconLoader::SizeMedium},
32 {QStringLiteral("sizelarge"), KIconLoader::SizeLarge},
33 {QStringLiteral("sizehuge"), KIconLoader::SizeHuge},
34 {QStringLiteral("sizeenormous"), KIconLoader::SizeEnormous}};
35
36 const QStringList parts = smartSplit(tagContent);
37 const int partsSize = parts.size();
38 if (partsSize < 2) {
39 throw KTextTemplate::Exception(KTextTemplate::TagSyntaxError, QStringLiteral("icon tag takes at least 1 argument"));
40 }
41 if (partsSize > 4) {
42 throw KTextTemplate::Exception(KTextTemplate::TagSyntaxError, QStringLiteral("icon tag takes at maximum 3 arguments, %1 given").arg(partsSize));
43 }
44
45 int sizeOrGroup = KIconLoader::Small;
46 QString altText;
47 if (partsSize >= 3) {
48 const QString sizeStr = parts.at(2);
49 bool ok = false;
50 // Try to convert to pixel size
51 sizeOrGroup = sizeStr.toInt(&ok);
52 if (!ok) {
53 // If failed, then try to map the string to one of tne enums
54 const auto size = sizeOrGroupLookup.constFind(sizeStr);
55 if (size == sizeOrGroupLookup.cend()) {
56 // If it's not a valid size string, assume it's an alt text
57 altText = sizeStr;
58 } else {
59 sizeOrGroup = (*size);
60 }
61 }
62 }
63 if (partsSize == 4) {
64 altText = parts.at(3);
65 }
66
67 return new IconNode(parts.at(1), sizeOrGroup, altText);
68}
69
70IconNode::IconNode(QObject *parent)
71 : KTextTemplate::Node(parent)
72{
73}
74
75IconNode::IconNode(const QString &iconName, int sizeOrGroup, const QString &altText, QObject *parent)
76 : KTextTemplate::Node(parent)
77 , mIconName(iconName)
78 , mAltText(altText)
79 , mSizeOrGroup(sizeOrGroup)
80{
81}
82
83IconNode::~IconNode() = default;
84void IconNode::render(KTextTemplate::OutputStream *stream, KTextTemplate::Context *c) const
85{
86 Q_UNUSED(c)
87
88 QString iconName = mIconName;
89 if (iconName.startsWith(QLatin1Char('"')) && iconName.endsWith(QLatin1Char('"'))) {
90 iconName = iconName.mid(1, iconName.size() - 2);
91 } else {
92 const QVariant val = KTextTemplate::Variable(mIconName).resolve(c);
93 if (val.metaType().id() == QMetaType::QString) {
94 iconName = val.toString();
95 } else {
96 iconName = val.value<KTextTemplate::SafeString>().get();
97 }
98 }
99
100 QString altText;
101 if (!mAltText.isEmpty()) {
102 if (mAltText.startsWith(QLatin1Char('"')) && mAltText.endsWith(QLatin1Char('"'))) {
103 altText = mAltText.mid(1, mAltText.size() - 2);
104 } else {
105 const QVariant v = KTextTemplate::Variable(mAltText).resolve(c);
106 if (v.isValid()) {
108 altText = v.value<KTextTemplate::SafeString>().get();
109 } else {
110 altText = v.toString();
111 }
112 }
113 }
114 }
115
116 QString iconPath = KIconLoader::global()->iconPath(iconName, mSizeOrGroup < KIconLoader::LastGroup ? mSizeOrGroup : -mSizeOrGroup);
117 if (iconPath.startsWith(QLatin1StringView(":/"))) {
118 iconPath = QStringLiteral("qrc") + iconPath;
119 } else {
120 iconPath = QStringLiteral("file://") + iconPath;
121 }
122
123 const QString html =
124 QStringLiteral("<img src=\"%1\" align=\"top\" height=\"%2\" width=\"%2\" alt=\"%3\" title=\"%4\" />")
125 .arg(iconPath)
126 .arg(mSizeOrGroup < KIconLoader::LastGroup ? KIconLoader::global()->currentSize(static_cast<KIconLoader::Group>(mSizeOrGroup)) : mSizeOrGroup)
127 .arg(altText.isEmpty() ? iconName : altText, altText); // title is intentionally blank if no alt is provided
128 (*stream) << KTextTemplate::SafeString(html, KTextTemplate::SafeString::IsSafe);
129}
130
131#include "moc_icon.cpp"
static KIconLoader * global()
QString iconPath(const QString &name, int group_or_size, bool canReturnNull, qreal scale) const
Q_INVOKABLE QStringList smartSplit(const QString &str) const
QVariant resolve(Context *c) const
KIOCORE_EXPORT TransferJob * get(const QUrl &url, LoadType reload=NoReload, JobFlags flags=DefaultFlags)
const_iterator cend() const const
const_iterator constFind(const Key &key) const const
const_reference at(qsizetype i) const const
qsizetype size() const const
int id() const const
QString arg(Args &&... args) const const
bool endsWith(QChar c, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
QString mid(qsizetype position, qsizetype n) const const
qsizetype size() const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
int toInt(bool *ok, int base) const const
bool canConvert() const const
bool isValid() const const
QMetaType metaType() const const
QString toString() const const
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:57:21 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.