X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fuisupport%2Ficon.cpp;h=2a9174390066d270618aed4d128de3a2607faa5a;hp=791380dd0c76c0ad11953f878aa4835ec6bc0214;hb=a95ad2de573027f9bee36db972bcae4195168d0c;hpb=c346526a5414105dea8e7dabac2494e33622d06b diff --git a/src/uisupport/icon.cpp b/src/uisupport/icon.cpp index 791380dd..2a917439 100644 --- a/src/uisupport/icon.cpp +++ b/src/uisupport/icon.cpp @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2005-08 by the Quassel IRC Team * + * Copyright (C) 2005-2020 by the Quassel Project * * devel@quassel-irc.org * * * * This program is free software; you can redistribute it and/or modify * @@ -15,31 +15,71 @@ * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * ***************************************************************************/ -#ifndef HAVE_KDE - #include "icon.h" -#include "iconloader.h" -Icon::Icon() : QIcon() { +#include -} +#include + +namespace icon { + +namespace { -Icon::Icon(const QString &name) : QIcon() { - addPixmap(IconLoader::global()->loadIcon(name, IconLoader::Desktop)); +void printWarning(const QString& iconName, const QString& extra = {}) +{ + static std::set warnedAbout; + if (warnedAbout.insert(iconName).second) { + qWarning() << "Missing icon:" << iconName << qPrintable(extra); + } } -Icon::Icon(const QIcon& copy) : QIcon(copy) { +} // namespace +QIcon get(const QString& iconName, const QString& fallbackPath) +{ + return get(std::vector{iconName}, fallbackPath); } -Icon& Icon::operator=(const Icon &other) { - if (this != &other) { - QIcon::operator=(other); - } - return *this; +QIcon get(const std::vector& iconNames, const QString& fallbackPath) +{ + for (auto&& iconName : iconNames) { + // Exact match + if (QIcon::hasThemeIcon(iconName)) { + return QIcon::fromTheme(iconName); + } + } + + for (auto&& iconName : iconNames) { + // Try to get something from the theme anyway (i.e. a more generic fallback) + QIcon fallback = QIcon::fromTheme(iconName); + if (!fallback.availableSizes().isEmpty()) { + printWarning(iconName, QString{"(using fallback: \"%1\")"}.arg(fallback.name())); + return fallback; + } + } + + // Build error string + QStringList requested; + for (auto&& iconName : iconNames) { + requested << iconName; + } + QString missing = "{" + requested.join(", ") + "}"; + + // Nothing from the theme, so try to load from path if given + if (!fallbackPath.isEmpty()) { + QIcon fallback{fallbackPath}; + if (!fallback.availableSizes().isEmpty()) { + printWarning(missing, QString{"(using fallback: \"%1\")"}.arg(fallbackPath)); + return fallback; + } + } + + // Meh. + printWarning(missing); + return {}; } -#endif +} // namespace icon