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 
10 #include <grantlee/exception.h>
11 #include <grantlee/parser.h>
12 #include <grantlee/variable.h>
13 
14 #include <KIconLoader>
15 
16 IconTag::IconTag(QObject *parent)
17  : Grantlee::AbstractNodeFactory(parent)
18 {
19 }
20 
21 IconTag::~IconTag()
22 {
23 }
24 
25 Grantlee::Node *IconTag::getNode(const QString &tagContent, Grantlee::Parser *p) const
26 {
27  Q_UNUSED(p)
28 
29  static QHash<QString, int> sizeOrGroupLookup
30  = { { QStringLiteral("desktop"), KIconLoader::Desktop },
31  { QStringLiteral("toolbar"), KIconLoader::Toolbar },
32  { QStringLiteral("maintoolbar"), KIconLoader::MainToolbar },
33  { QStringLiteral("small"), KIconLoader::Small },
34  { QStringLiteral("panel"), KIconLoader::Panel },
35  { QStringLiteral("dialog"), KIconLoader::Dialog },
36  { QStringLiteral("sizesmall"), KIconLoader::SizeSmall },
37  { QStringLiteral("sizesmallmedium"), KIconLoader::SizeSmallMedium },
38  { QStringLiteral("sizemedium"), KIconLoader::SizeMedium },
39  { QStringLiteral("sizelarge"), KIconLoader::SizeLarge },
40  { QStringLiteral("sizehuge"), KIconLoader::SizeHuge },
41  { QStringLiteral("sizeenormous"), KIconLoader::SizeEnormous }};
42 
43  const QStringList parts = smartSplit(tagContent);
44  const int partsSize = parts.size();
45  if (partsSize < 2) {
46  throw Grantlee::Exception(Grantlee::TagSyntaxError, QStringLiteral("icon tag takes at least 1 argument"));
47  }
48  if (partsSize > 4) {
49  throw Grantlee::Exception(Grantlee::TagSyntaxError, QStringLiteral("icon tag takes at maximum 3 arguments, %1 given").arg(partsSize));
50  }
51 
52  int sizeOrGroup = KIconLoader::Small;
53  QString altText;
54  if (partsSize >= 3) {
55  const QString sizeStr = parts.at(2);
56  bool ok = false;
57  // Try to convert to pixel size
58  sizeOrGroup = sizeStr.toInt(&ok);
59  if (!ok) {
60  // If failed, then try to map the string to one of tne enums
61  const auto size = sizeOrGroupLookup.constFind(sizeStr);
62  if (size == sizeOrGroupLookup.cend()) {
63  // If it's not a valid size string, assume it's an alt text
64  altText = sizeStr;
65  } else {
66  sizeOrGroup = (*size);
67  }
68  }
69  }
70  if (partsSize == 4) {
71  altText = parts.at(3);
72  }
73 
74  return new IconNode(parts.at(1), sizeOrGroup, altText);
75 }
76 
77 IconNode::IconNode(QObject *parent)
78  : Grantlee::Node(parent)
79 {
80 }
81 
82 IconNode::IconNode(const QString &iconName, int sizeOrGroup, const QString &altText, QObject *parent)
83  : Grantlee::Node(parent)
84  , mIconName(iconName)
85  , mAltText(altText)
86  , mSizeOrGroup(sizeOrGroup)
87 {
88 }
89 
90 IconNode::~IconNode()
91 {
92 }
93 
94 void IconNode::render(Grantlee::OutputStream *stream, Grantlee::Context *c) const
95 {
96  Q_UNUSED(c)
97 
98  QString iconName = mIconName;
99  if (iconName.startsWith(QLatin1Char('"')) && iconName.endsWith(QLatin1Char('"'))) {
100  iconName = iconName.mid(1, iconName.size() - 2);
101  } else {
102  const QVariant val = Grantlee::Variable(mIconName).resolve(c);
103  if (val.type() == QVariant::String) {
104  iconName = val.toString();
105  } else {
106  iconName = val.value<Grantlee::SafeString>().get();
107  }
108  }
109 
110  QString altText;
111  if (!mAltText.isEmpty()) {
112  if (mAltText.startsWith(QLatin1Char('"')) && mAltText.endsWith(QLatin1Char('"'))) {
113  altText = mAltText.mid(1, mAltText.size() - 2);
114  } else {
115  const QVariant v = Grantlee::Variable(mAltText).resolve(c);
116  if (v.isValid()) {
117  if (v.canConvert<Grantlee::SafeString>()) {
118  altText = v.value<Grantlee::SafeString>().get();
119  } else {
120  altText = v.toString();
121  }
122  }
123  }
124  }
125 
126  const QString html = QStringLiteral("<img src=\"file://%1\" align=\"top\" height=\"%2\" width=\"%2\" alt=\"%3\" title=\"%4\" />")
127  .arg(KIconLoader::global()->iconPath(iconName, mSizeOrGroup < KIconLoader::LastGroup ? mSizeOrGroup : -mSizeOrGroup))
128  .arg(mSizeOrGroup < KIconLoader::LastGroup
129  ? KIconLoader::global()->currentSize(static_cast<KIconLoader::Group>(mSizeOrGroup))
130  : mSizeOrGroup)
131  .arg(altText.isEmpty() ? iconName : altText, altText); // title is intentionally blank if no alt is provided
132  (*stream) << Grantlee::SafeString(html, Grantlee::SafeString::IsSafe);
133 }
bool canConvert(int targetTypeId) const const
const T & at(int i) const const
T value() const const
int size() const const
int toInt(bool *ok, int base) const const
bool isEmpty() const const
static KIconLoader * global()
QString mid(int position, int n) const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
bool isValid() const const
QVariant::Type type() const const
QObject * parent() const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Jan 23 2021 07:18:48 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.