| 1 | // Copyright 2015 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Resources.h" |
| 5 | |
| 6 | #include <QFileInfo> |
| 7 | #include <QIcon> |
| 8 | #include <QImageReader> |
| 9 | #include <QPixmap> |
| 10 | |
| 11 | #include "Common/FileUtil.h" |
| 12 | #include "Common/Logging/Log.h" |
| 13 | |
| 14 | #include "Core/Config/MainSettings.h" |
| 15 | |
| 16 | bool Resources::m_svg_supported; |
| 17 | QList<QIcon> Resources::m_platforms; |
| 18 | QList<QIcon> Resources::m_countries; |
| 19 | QList<QIcon> Resources::m_misc; |
| 20 | |
| 21 | QIcon Resources::LoadNamedIcon(std::string_view name, const QString& dir) |
| 22 | { |
| 23 | const QString base_path = dir + QLatin1Char{'/'} + QString::fromLatin1(name); |
| 24 | const QString svg_path = base_path + QStringLiteral(".svg"); |
| 25 | |
| 26 | // Prefer svg |
| 27 | if (m_svg_supported && QFileInfo(svg_path).exists()) |
| 28 | return QIcon(svg_path); |
| 29 | |
| 30 | QIcon icon; |
| 31 | |
| 32 | auto load_png = [&](int scale) { |
| 33 | QString suffix = QStringLiteral(".png"); |
| 34 | if (scale > 1) |
| 35 | suffix = QString::fromLatin1("@%1x.png").arg(scale); |
| 36 | |
| 37 | QPixmap pixmap(base_path + suffix); |
| 38 | if (!pixmap.isNull()) |
| 39 | { |
| 40 | pixmap.setDevicePixelRatio(scale); |
| 41 | icon.addPixmap(pixmap); |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | // Since we are caching the files, we need to try and load all known sizes up-front. |
| 46 | // Otherwise, a dynamic change of devicePixelRatio could result in use of non-ideal image from |
| 47 | // cache while a better one exists on disk. |
| 48 | for (auto scale : {1, 2, 4}) |
| 49 | load_png(scale); |
| 50 | |
| 51 | if (icon.isNull()) |
| 52 | { |
| 53 | ERROR_LOG_FMT(COMMON, "LoadNamedIcon: \"{}\" could not be loaded.", name); |
| 54 | } |
| 55 | |
| 56 | return icon; |
| 57 | } |
| 58 | |
| 59 | static QString GetCurrentThemeDir() |
| 60 | { |
| 61 | return QString::fromStdString(File::GetThemeDir(Config::Get(Config::MAIN_THEME_NAME))); |
| 62 | } |
| 63 | |
| 64 | static QString GetResourcesDir() |
| 65 | { |
| 66 | return QString::fromStdString(File::GetSysDirectory() + "Resources"); |
| 67 | } |
| 68 | |
| 69 | QIcon Resources::GetResourceIcon(std::string_view name) |
| 70 | { |
| 71 | return LoadNamedIcon(name, GetResourcesDir()); |
| 72 | } |
| 73 | |
| 74 | QIcon Resources::GetThemeIcon(std::string_view name) |
| 75 | { |
| 76 | return LoadNamedIcon(name, GetCurrentThemeDir()); |
| 77 | } |
| 78 | |
| 79 | void Resources::Init() |
| 80 | { |
| 81 | m_svg_supported = QImageReader::supportedImageFormats().contains("svg"); |
| 82 | |
| 83 | for (std::string_view platform : |
| 84 | {"Platform_Gamecube", "Platform_Triforce", "Platform_Wii", "Platform_Wad", "Platform_File"}) |
| 85 | { |
| 86 | m_platforms.append(GetResourceIcon(platform)); |
| 87 | } |
| 88 | |
| 89 | for (std::string_view country : |
| 90 | {"Flag_Europe", "Flag_Japan", "Flag_USA", "Flag_Australia", "Flag_France", "Flag_Germany", |
| 91 | "Flag_Italy", "Flag_Korea", "Flag_Netherlands", "Flag_Russia", "Flag_Spain", "Flag_Taiwan", |
| 92 | "Flag_International", "Flag_Unknown"}) |
| 93 | { |
| 94 | m_countries.append(GetResourceIcon(country)); |
| 95 | } |
| 96 | |
| 97 | m_misc.append(GetResourceIcon("nobanner")); |
| 98 | m_misc.append(GetResourceIcon("dolphin_logo")); |
| 99 | } |
| 100 | |
| 101 | QIcon Resources::GetPlatform(DiscIO::Platform platform) |
| 102 | { |
| 103 | return m_platforms[static_cast<int>(platform)]; |
| 104 | } |
| 105 | |
| 106 | QIcon Resources::GetCountry(DiscIO::Country country) |
| 107 | { |
| 108 | return m_countries[static_cast<int>(country)]; |
| 109 | } |
| 110 | |
| 111 | QIcon Resources::GetMisc(MiscID id) |
| 112 | { |
| 113 | return m_misc[static_cast<int>(id)]; |
| 114 | } |
| 115 | |
| 116 | QIcon Resources::GetAppIcon() |
| 117 | { |
| 118 | return GetMisc(MiscID::Logo); |
| 119 | } |