GrantleeTheme

icon.cpp
1 /*
2  * Copyright (C) 2015 Daniel Vr├ítil <[email protected]>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  */
19 
20 #include "icon.h"
21 
22 #include <grantlee/exception.h>
23 #include <grantlee/parser.h>
24 #include <grantlee/variable.h>
25 
26 #include <KIconLoader>
27 
28 IconTag::IconTag(QObject *parent)
29  : Grantlee::AbstractNodeFactory(parent)
30 {
31 }
32 
33 IconTag::~IconTag()
34 {
35 }
36 
37 Grantlee::Node *IconTag::getNode(const QString &tagContent, Grantlee::Parser *p) const
38 {
39  Q_UNUSED(p);
40 
41  static QHash<QString, int> sizeOrGroupLookup
42  = { { QStringLiteral("desktop"), KIconLoader::Desktop },
43  { QStringLiteral("toolbar"), KIconLoader::Toolbar },
44  { QStringLiteral("maintoolbar"), KIconLoader::MainToolbar },
45  { QStringLiteral("small"), KIconLoader::Small },
46  { QStringLiteral("panel"), KIconLoader::Panel },
47  { QStringLiteral("dialog"), KIconLoader::Dialog },
48  { QStringLiteral("sizesmall"), KIconLoader::SizeSmall },
49  { QStringLiteral("sizesmallmedium"), KIconLoader::SizeSmallMedium },
50  { QStringLiteral("sizemedium"), KIconLoader::SizeMedium },
51  { QStringLiteral("sizelarge"), KIconLoader::SizeLarge },
52  { QStringLiteral("sizehuge"), KIconLoader::SizeHuge },
53  { QStringLiteral("sizeenormous"), KIconLoader::SizeEnormous }};
54 
55  const QStringList parts = smartSplit(tagContent);
56  const int partsSize = parts.size();
57  if (partsSize < 2) {
58  throw Grantlee::Exception(Grantlee::TagSyntaxError, QStringLiteral("icon tag takes at least 1 argument"));
59  }
60  if (partsSize > 4) {
61  throw Grantlee::Exception(Grantlee::TagSyntaxError, QStringLiteral("icon tag takes at maximum 3 arguments, %1 given").arg(partsSize));
62  }
63 
64  int sizeOrGroup = KIconLoader::Small;
65  QString altText;
66  if (partsSize >= 3) {
67  const QString sizeStr = parts.at(2);
68  bool ok = false;
69  // Try to convert to pixel size
70  sizeOrGroup = sizeStr.toInt(&ok);
71  if (!ok) {
72  // If failed, then try to map the string to one of tne enums
73  const auto size = sizeOrGroupLookup.constFind(sizeStr);
74  if (size == sizeOrGroupLookup.cend()) {
75  // If it's not a valid size string, assume it's an alt text
76  altText = sizeStr;
77  } else {
78  sizeOrGroup = (*size);
79  }
80  }
81  }
82  if (partsSize == 4) {
83  altText = parts.at(3);
84  }
85 
86  return new IconNode(parts.at(1), sizeOrGroup, altText);
87 }
88 
89 IconNode::IconNode(QObject *parent)
90  : Grantlee::Node(parent)
91 {
92 }
93 
94 IconNode::IconNode(const QString &iconName, int sizeOrGroup, const QString &altText, QObject *parent)
95  : Grantlee::Node(parent)
96  , mIconName(iconName)
97  , mAltText(altText)
98  , mSizeOrGroup(sizeOrGroup)
99 {
100 }
101 
102 IconNode::~IconNode()
103 {
104 }
105 
106 void IconNode::render(Grantlee::OutputStream *stream, Grantlee::Context *c) const
107 {
108  Q_UNUSED(c);
109 
110  QString iconName = mIconName;
111  if (iconName.startsWith(QLatin1Char('"')) && iconName.endsWith(QLatin1Char('"'))) {
112  iconName = iconName.mid(1, iconName.size() - 2);
113  } else {
114  const QVariant val = Grantlee::Variable(mIconName).resolve(c);
115  if (val.type() == QVariant::String) {
116  iconName = val.toString();
117  } else {
118  iconName = val.value<Grantlee::SafeString>().get();
119  }
120  }
121 
122  QString altText;
123  if (!mAltText.isEmpty()) {
124  if (mAltText.startsWith(QLatin1Char('"')) && mAltText.endsWith(QLatin1Char('"'))) {
125  altText = mAltText.mid(1, mAltText.size() - 2);
126  } else {
127  const QVariant v = Grantlee::Variable(mAltText).resolve(c);
128  if (v.isValid()) {
129  if (v.canConvert<Grantlee::SafeString>()) {
130  altText = v.value<Grantlee::SafeString>().get();
131  } else {
132  altText = v.toString();
133  }
134  }
135  }
136  }
137 
138  const QString html = QStringLiteral("<img src=\"file://%1\" align=\"top\" height=\"%2\" width=\"%2\" alt=\"%3\" title=\"%4\" />")
139  .arg(KIconLoader::global()->iconPath(iconName, mSizeOrGroup < KIconLoader::LastGroup ? mSizeOrGroup : -mSizeOrGroup))
140  .arg(mSizeOrGroup < KIconLoader::LastGroup
141  ? KIconLoader::global()->currentSize(static_cast<KIconLoader::Group>(mSizeOrGroup))
142  : mSizeOrGroup)
143  .arg(altText.isEmpty() ? iconName : altText, altText); // title is intentionally blank if no alt is provided
144  (*stream) << Grantlee::SafeString(html, Grantlee::SafeString::IsSafe);
145 }
bool canConvert(int targetTypeId) const const
const T & at(int i) const const
int size() const const
T value() const const
QHash::const_iterator constFind(const Key &key) const const
QHash::const_iterator cend() const const
int size() const const
int toInt(bool *ok, int base) const const
bool isEmpty() const const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
bool endsWith(const QString &s, Qt::CaseSensitivity cs) 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-2020 The KDE developers.
Generated on Tue Jun 2 2020 23:12:01 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.