GrantleeTheme

icon.cpp
1 /*
2  * SPDX-FileCopyrightText: 2015 Daniel Vr├ítil <[email protected]>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  */
7 
8 #include "icon.h"
9 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
10 #include <grantlee/exception.h>
11 #include <grantlee/parser.h>
12 #include <grantlee/variable.h>
13 #else
14 #include <KTextTemplate/exception.h>
15 #include <KTextTemplate/parser.h>
16 #include <KTextTemplate/variable.h>
17 #endif
18 
19 #include <KIconLoader>
20 
21 IconTag::IconTag(QObject *parent)
22 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
23  : Grantlee::AbstractNodeFactory(parent)
24 #else
25  : KTextTemplate::AbstractNodeFactory(parent)
26 #endif
27 {
28 }
29 
30 IconTag::~IconTag() = default;
31 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
32 Grantlee::Node *IconTag::getNode(const QString &tagContent, Grantlee::Parser *p) const
33 #else
34 KTextTemplate::Node *IconTag::getNode(const QString &tagContent, KTextTemplate::Parser *p) const
35 #endif
36 {
37  Q_UNUSED(p)
38 
39  static QHash<QString, int> sizeOrGroupLookup = {{QStringLiteral("desktop"), KIconLoader::Desktop},
40  {QStringLiteral("toolbar"), KIconLoader::Toolbar},
41  {QStringLiteral("maintoolbar"), KIconLoader::MainToolbar},
42  {QStringLiteral("small"), KIconLoader::Small},
43  {QStringLiteral("panel"), KIconLoader::Panel},
44  {QStringLiteral("dialog"), KIconLoader::Dialog},
45  {QStringLiteral("sizesmall"), KIconLoader::SizeSmall},
46  {QStringLiteral("sizesmallmedium"), KIconLoader::SizeSmallMedium},
47  {QStringLiteral("sizemedium"), KIconLoader::SizeMedium},
48  {QStringLiteral("sizelarge"), KIconLoader::SizeLarge},
49  {QStringLiteral("sizehuge"), KIconLoader::SizeHuge},
50  {QStringLiteral("sizeenormous"), KIconLoader::SizeEnormous}};
51 
52  const QStringList parts = smartSplit(tagContent);
53  const int partsSize = parts.size();
54  if (partsSize < 2) {
55 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
56  throw Grantlee::Exception(Grantlee::TagSyntaxError, QStringLiteral("icon tag takes at least 1 argument"));
57 #else
58  throw KTextTemplate::Exception(KTextTemplate::TagSyntaxError, QStringLiteral("icon tag takes at least 1 argument"));
59 #endif
60  }
61  if (partsSize > 4) {
62 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
63  throw Grantlee::Exception(Grantlee::TagSyntaxError, QStringLiteral("icon tag takes at maximum 3 arguments, %1 given").arg(partsSize));
64 #else
65  throw KTextTemplate::Exception(KTextTemplate::TagSyntaxError, QStringLiteral("icon tag takes at maximum 3 arguments, %1 given").arg(partsSize));
66 #endif
67  }
68 
69  int sizeOrGroup = KIconLoader::Small;
70  QString altText;
71  if (partsSize >= 3) {
72  const QString sizeStr = parts.at(2);
73  bool ok = false;
74  // Try to convert to pixel size
75  sizeOrGroup = sizeStr.toInt(&ok);
76  if (!ok) {
77  // If failed, then try to map the string to one of tne enums
78  const auto size = sizeOrGroupLookup.constFind(sizeStr);
79  if (size == sizeOrGroupLookup.cend()) {
80  // If it's not a valid size string, assume it's an alt text
81  altText = sizeStr;
82  } else {
83  sizeOrGroup = (*size);
84  }
85  }
86  }
87  if (partsSize == 4) {
88  altText = parts.at(3);
89  }
90 
91  return new IconNode(parts.at(1), sizeOrGroup, altText);
92 }
93 
94 IconNode::IconNode(QObject *parent)
95 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
96  : Grantlee::Node(parent)
97 #else
98  : KTextTemplate::Node(parent)
99 #endif
100 {
101 }
102 
103 IconNode::IconNode(const QString &iconName, int sizeOrGroup, const QString &altText, QObject *parent)
104 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
105  : Grantlee::Node(parent)
106 #else
107  : KTextTemplate::Node(parent)
108 #endif
109  , mIconName(iconName)
110  , mAltText(altText)
111  , mSizeOrGroup(sizeOrGroup)
112 {
113 }
114 
115 IconNode::~IconNode() = default;
116 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
117 void IconNode::render(Grantlee::OutputStream *stream, Grantlee::Context *c) const
118 #else
119 void IconNode::render(KTextTemplate::OutputStream *stream, KTextTemplate::Context *c) const
120 #endif
121 {
122  Q_UNUSED(c)
123 
124  QString iconName = mIconName;
125  if (iconName.startsWith(QLatin1Char('"')) && iconName.endsWith(QLatin1Char('"'))) {
126  iconName = iconName.mid(1, iconName.size() - 2);
127  } else {
128 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
129  const QVariant val = Grantlee::Variable(mIconName).resolve(c);
130 #else
131  const QVariant val = KTextTemplate::Variable(mIconName).resolve(c);
132 #endif
133  if (val.type() == QVariant::String) {
134  iconName = val.toString();
135  } else {
136 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
137  iconName = val.value<Grantlee::SafeString>().get();
138 #else
139  iconName = val.value<KTextTemplate::SafeString>().get();
140 #endif
141  }
142  }
143 
144  QString altText;
145  if (!mAltText.isEmpty()) {
146  if (mAltText.startsWith(QLatin1Char('"')) && mAltText.endsWith(QLatin1Char('"'))) {
147  altText = mAltText.mid(1, mAltText.size() - 2);
148  } else {
149 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
150  const QVariant v = Grantlee::Variable(mAltText).resolve(c);
151 #else
152  const QVariant v = KTextTemplate::Variable(mAltText).resolve(c);
153 #endif
154  if (v.isValid()) {
155 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
156  if (v.canConvert<Grantlee::SafeString>()) {
157  altText = v.value<Grantlee::SafeString>().get();
158  } else {
159  altText = v.toString();
160  }
161 #else
162  if (v.canConvert<KTextTemplate::SafeString>()) {
163  altText = v.value<KTextTemplate::SafeString>().get();
164  } else {
165  altText = v.toString();
166  }
167 #endif
168  }
169  }
170  }
171 
172  const QString html =
173  QStringLiteral("<img src=\"file://%1\" align=\"top\" height=\"%2\" width=\"%2\" alt=\"%3\" title=\"%4\" />")
174  .arg(KIconLoader::global()->iconPath(iconName, mSizeOrGroup < KIconLoader::LastGroup ? mSizeOrGroup : -mSizeOrGroup))
175  .arg(mSizeOrGroup < KIconLoader::LastGroup ? KIconLoader::global()->currentSize(static_cast<KIconLoader::Group>(mSizeOrGroup)) : mSizeOrGroup)
176  .arg(altText.isEmpty() ? iconName : altText, altText); // title is intentionally blank if no alt is provided
177 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
178  (*stream) << Grantlee::SafeString(html, Grantlee::SafeString::IsSafe);
179 #else
180  (*stream) << KTextTemplate::SafeString(html, KTextTemplate::SafeString::IsSafe);
181 #endif
182 }
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const const
bool isValid() const const
int size() const const
T value() const const
QHash::const_iterator cend() const const
int size() const const
QVariant::Type type() const const
bool isEmpty() const const
const T & at(int i) const const
static KIconLoader * global()
int toInt(bool *ok, int base) const const
QHash::const_iterator constFind(const Key &key) const const
bool canConvert(int targetTypeId) const const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString mid(int position, int n) const const
virtual QVariant get(ScriptableExtension *callerPrincipal, quint64 objId, const QString &propName)
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Wed Sep 28 2022 04:06:59 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.