KCalUtils

icon.cpp
1/*
2 * SPDX-FileCopyrightText: 2015 Daniel Vrátil <dvratil@redhat.com>
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
16IconTag::IconTag(QObject *parent)
17 : KTextTemplate::AbstractNodeFactory(parent)
18{
19}
20
21IconTag::~IconTag()
22{
23}
24KTextTemplate::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
73IconNode::IconNode(QObject *parent)
74 : KTextTemplate::Node(parent)
75 , mSizeOrGroup(KIconLoader::Small)
76{
77}
78
79IconNode::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
87IconNode::~IconNode()
88{
89}
90void 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()) {
109 altText = v.value<KTextTemplate::SafeString>().get();
110 } else {
111 altText = v.toString();
112 }
113 }
114 }
115 }
116
117 QString iconPath = KIconLoader::global()->iconPath(iconName, mSizeOrGroup < KIconLoader::LastGroup ? mSizeOrGroup : -mSizeOrGroup);
118 if (iconPath.startsWith(QLatin1StringView(":/"))) {
119 iconPath = QStringLiteral("qrc") + iconPath;
120 } else {
121 iconPath = QStringLiteral("file://") + iconPath;
122 }
123
124 const QString html =
125 QStringLiteral("<img src=\"%1\" align=\"top\" height=\"%2\" width=\"%2\" alt=\"%3\" title=\"%4\" />")
126 .arg(iconPath)
127 .arg(mSizeOrGroup < KIconLoader::LastGroup ? KIconLoader::global()->currentSize(static_cast<KIconLoader::Group>(mSizeOrGroup)) : mSizeOrGroup)
128 .arg(altText.isEmpty() ? iconName : altText, altText); // title is intentionally blank if no alt is provided
129 (*stream) << KTextTemplate::SafeString(html, KTextTemplate::SafeString::IsSafe);
130}
131
132#include "moc_icon.cpp"
static KIconLoader * global()
QString iconPath(const QString &name, int group_or_size, bool canReturnNull, qreal scale) const
Q_INVOKABLE QStringList smartSplit(const QString &str) const
QVariant resolve(Context *c) const
KIOCORE_EXPORT TransferJob * get(const QUrl &url, LoadType reload=NoReload, JobFlags flags=DefaultFlags)
const_iterator cend() const const
const_iterator constFind(const Key &key) const const
const_reference at(qsizetype i) const const
qsizetype size() const const
QString arg(Args &&... args) const const
bool endsWith(QChar c, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
QString mid(qsizetype position, qsizetype n) const const
qsizetype size() const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
int toInt(bool *ok, int base) const const
bool canConvert() const const
bool isValid() const const
QString toString() const const
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:09:16 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.