1 /* Copyright (C) 2006 - 2015 Jan Kundrát <jkt@kde.org>
2
3 This file is part of the Trojita Qt IMAP e-mail client,
4 http://trojita.flaska.net/
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of
9 the License or (at your option) version 3 or any later version
10 accepted by the membership of KDE e.V. (or its successor approved
11 by the membership of KDE e.V.), which shall act as a proxy
12 defined in Section 14 of version 3 of the license.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "IconLoader.h"
24 #include <QCoreApplication>
25 #include <QFileInfo>
26 #include <QHash>
27
28 namespace UiUtils {
29
30 /** @short Wrapper around the QIcon::fromTheme with sane fallback
31 *
32 * The issue with the QIcon::fromTheme is that it does not really work on non-X11
33 * platforms, unless you ship your own theme index and specify your theme name.
34 * In Trojitá, we do not really want to hardcode anything, because the
35 * preference is to use whatever is available from the current theme, but *also*
36 * provide an icon as a fallback. And that is exactly why this function is here.
37 * */
loadIcon(const QString & name)38 QIcon loadIcon(const QString &name)
39 {
40 using Cache = QHash<QString, QIcon>;
41 static Cache iconDict;
42
43 // static to ensure that this connection is only set up once during the app's lifetime
44 static auto conn = QObject::connect(qApp, &QCoreApplication::aboutToQuit, [](){
45 iconDict.clear();
46 });
47
48 auto it = iconDict.constFind(name);
49 if (it != iconDict.constEnd())
50 return *it;
51
52 auto overrideIcon = QStringLiteral(":/icons/%1/%2.svg").arg(QIcon::themeName(), name);
53 if (QFileInfo(overrideIcon).exists()) {
54 QIcon icon(overrideIcon);
55 iconDict.insert(name, icon);
56 return icon;
57 }
58
59 // A QIcon(QString) constructor creates non-null QIcons, even though the resulting icon will
60 // not ever return a valid and usable pixmap. This means that we have to actually look at the
61 // icon's pixmap to find out what to return.
62 // If we do not do that, the GUI shows empty pixmaps instead of a text fallback, which is
63 // clearly suboptimal.
64 QIcon res = QIcon::fromTheme(name, QIcon(QStringLiteral(":/icons/%1").arg(name)));
65 if (res.pixmap(QSize(16, 16)).isNull()) {
66 res = QIcon();
67 }
68 iconDict.insert(name, res);
69 return res;
70 }
71
72 }
73