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

KDE's Doxygen guidelines are available online.