clazy: fix QString(QLatin1String(...))
[trojita.git] / src / UiUtils / IconLoader.cpp
blob6b7544d9bd7aaa7ce32e0661d9a3505f5e78c39f
1 /* Copyright (C) 2006 - 2015 Jan Kundrát <jkt@kde.org>
3 This file is part of the Trojita Qt IMAP e-mail client,
4 http://trojita.flaska.net/
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.
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.
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/>.
23 #include "IconLoader.h"
24 #include <QFileInfo>
25 #include <QHash>
27 namespace UiUtils {
29 /** @short Wrapper around the QIcon::fromTheme with sane fallback
31 * The issue with the QIcon::fromTheme is that it does not really work on non-X11
32 * platforms, unless you ship your own theme index and specify your theme name.
33 * In Trojitá, we do not really want to hardcode anything, because the
34 * preference is to use whatever is available from the current theme, but *also*
35 * provide an icon as a fallback. And that is exactly why this function is here.
37 * It is implemented inline in order to prevent a dependency from the Imap lib
38 * into the Gui lib.
39 * */
40 QIcon loadIcon(const QString &name)
42 static QHash<QString, QIcon> iconDict;
43 QHash<QString, QIcon>::const_iterator it = iconDict.constFind(name);
44 if (it != iconDict.constEnd())
45 return *it;
47 auto overrideIcon = QStringLiteral(":/icons/%1/%2.svg").arg(QIcon::themeName(), name);
48 if (QFileInfo(overrideIcon).exists()) {
49 QIcon icon(overrideIcon);
50 iconDict.insert(name, icon);
51 return icon;
54 QString iconInTheme = name;
55 if (QIcon::themeName().toLower() == QLatin1String("breeze")) {
56 if (name == QLatin1String("mail-flagged")) {
57 iconInTheme = QStringLiteral("flag-yellow");
58 } else if (name == QLatin1String("folder-bookmark")) {
59 iconInTheme = QStringLiteral("folder-blue");
60 } else if (name == QLatin1String("folder-open")) {
61 iconInTheme = QStringLiteral("folder");
62 } else if (name == QLatin1String("mail-read")) {
63 iconInTheme = QStringLiteral("mail-mark-read");
64 } else if (name == QLatin1String("mail-unread")) {
65 iconInTheme = QStringLiteral("mail-mark-unread");
69 // A QIcon(QString) constructor creates non-null QIcons, even though the resulting icon will
70 // not ever return a valid and usable pixmap. This means that we have to actually look at the
71 // icon's pixmap to find out what to return.
72 // If we do not do that, the GUI shows empty pixmaps instead of a text fallback, which is
73 // clearly suboptimal.
74 QIcon res = QIcon::fromTheme(iconInTheme, QIcon(QString::fromUtf8(":/icons/%1").arg(name)));
75 if (res.pixmap(QSize(16, 16)).isNull()) {
76 res = QIcon();
78 iconDict.insert(name, res);
79 return res;