Resources.cpp 2.9 KB

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