KCalUtils

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

KDE's Doxygen guidelines are available online.