2015-11-27 01:33:07 -07:00
|
|
|
// Copyright 2015 Dolphin Emulator Project
|
2021-07-04 19:22:19 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2015-11-27 01:33:07 -07:00
|
|
|
|
2021-12-09 19:22:16 -07:00
|
|
|
#include "DolphinQt/Resources.h"
|
|
|
|
|
2023-04-23 04:43:49 -06:00
|
|
|
#include <QFileInfo>
|
2017-05-30 14:42:21 -06:00
|
|
|
#include <QIcon>
|
2023-04-23 04:43:49 -06:00
|
|
|
#include <QImageReader>
|
2017-05-30 14:42:21 -06:00
|
|
|
#include <QPixmap>
|
2015-11-27 01:33:07 -07:00
|
|
|
|
2023-04-23 04:43:49 -06:00
|
|
|
#include "Common/Assert.h"
|
2015-11-27 01:33:07 -07:00
|
|
|
#include "Common/FileUtil.h"
|
2018-05-27 19:48:04 -06:00
|
|
|
|
2021-12-30 19:00:39 -07:00
|
|
|
#include "Core/Config/MainSettings.h"
|
2018-05-27 19:48:04 -06:00
|
|
|
|
2018-07-06 16:40:15 -06:00
|
|
|
#include "DolphinQt/Settings.h"
|
2015-11-27 01:33:07 -07:00
|
|
|
|
2023-04-23 04:43:49 -06:00
|
|
|
bool Resources::m_svg_supported;
|
|
|
|
QList<QIcon> Resources::m_platforms;
|
|
|
|
QList<QIcon> Resources::m_countries;
|
|
|
|
QList<QIcon> Resources::m_misc;
|
2018-03-26 00:13:15 -06:00
|
|
|
|
2023-04-23 04:43:49 -06:00
|
|
|
QIcon Resources::LoadNamedIcon(std::string_view name, const QString& dir)
|
2017-05-30 14:42:21 -06:00
|
|
|
{
|
2023-04-23 04:43:49 -06:00
|
|
|
const QString base_path = dir + QLatin1Char{'/'} + QString::fromLatin1(name);
|
|
|
|
const QString svg_path = base_path + QStringLiteral(".svg");
|
2017-05-30 14:42:21 -06:00
|
|
|
|
2023-04-23 04:43:49 -06:00
|
|
|
// Prefer svg
|
|
|
|
if (m_svg_supported && QFileInfo(svg_path).exists())
|
|
|
|
return QIcon(svg_path);
|
2017-05-30 14:42:21 -06:00
|
|
|
|
2023-04-23 04:43:49 -06:00
|
|
|
QIcon icon;
|
2017-05-30 14:42:21 -06:00
|
|
|
|
2023-04-23 04:43:49 -06:00
|
|
|
auto load_png = [&](int scale) {
|
|
|
|
QString suffix = QStringLiteral(".png");
|
|
|
|
if (scale > 1)
|
|
|
|
suffix = QString::fromLatin1("@%1x.png").arg(scale);
|
|
|
|
|
|
|
|
QPixmap pixmap(base_path + suffix);
|
2017-05-30 14:42:21 -06:00
|
|
|
if (!pixmap.isNull())
|
|
|
|
{
|
2023-04-23 04:43:49 -06:00
|
|
|
pixmap.setDevicePixelRatio(scale);
|
2017-05-30 14:42:21 -06:00
|
|
|
icon.addPixmap(pixmap);
|
|
|
|
}
|
2023-04-23 04:43:49 -06:00
|
|
|
};
|
2017-05-30 14:42:21 -06:00
|
|
|
|
2023-04-23 04:43:49 -06:00
|
|
|
// Since we are caching the files, we need to try and load all known sizes up-front.
|
|
|
|
// Otherwise, a dynamic change of devicePixelRatio could result in use of non-ideal image from
|
|
|
|
// cache while a better one exists on disk.
|
|
|
|
for (auto scale : {1, 2, 4})
|
|
|
|
load_png(scale);
|
2017-05-30 14:42:21 -06:00
|
|
|
|
2023-04-23 04:43:49 -06:00
|
|
|
ASSERT(icon.availableSizes().size() > 0);
|
|
|
|
|
|
|
|
return icon;
|
2017-05-30 14:42:21 -06:00
|
|
|
}
|
|
|
|
|
2017-06-22 16:11:53 -06:00
|
|
|
static QString GetCurrentThemeDir()
|
|
|
|
{
|
2021-12-30 19:00:39 -07:00
|
|
|
return QString::fromStdString(File::GetThemeDir(Config::Get(Config::MAIN_THEME_NAME)));
|
2017-06-22 16:11:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static QString GetResourcesDir()
|
|
|
|
{
|
|
|
|
return QString::fromStdString(File::GetSysDirectory() + "Resources");
|
|
|
|
}
|
|
|
|
|
2023-04-23 04:43:49 -06:00
|
|
|
QIcon Resources::GetResourceIcon(std::string_view name)
|
2017-05-30 14:42:21 -06:00
|
|
|
{
|
2023-04-23 04:43:49 -06:00
|
|
|
return LoadNamedIcon(name, GetResourcesDir());
|
2017-05-30 14:42:21 -06:00
|
|
|
}
|
|
|
|
|
2023-04-23 04:43:49 -06:00
|
|
|
QIcon Resources::GetThemeIcon(std::string_view name)
|
2017-05-30 14:42:21 -06:00
|
|
|
{
|
2023-04-23 04:43:49 -06:00
|
|
|
return LoadNamedIcon(name, GetCurrentThemeDir());
|
2017-05-30 14:42:21 -06:00
|
|
|
}
|
|
|
|
|
2015-11-27 01:33:07 -07:00
|
|
|
void Resources::Init()
|
|
|
|
{
|
2023-04-23 04:43:49 -06:00
|
|
|
m_svg_supported = QImageReader::supportedImageFormats().contains("svg");
|
|
|
|
|
2021-05-17 13:00:13 -06:00
|
|
|
for (std::string_view platform :
|
2017-05-30 14:42:21 -06:00
|
|
|
{"Platform_Gamecube", "Platform_Wii", "Platform_Wad", "Platform_File"})
|
|
|
|
{
|
2023-04-23 04:43:49 -06:00
|
|
|
m_platforms.append(GetResourceIcon(platform));
|
2017-05-30 14:42:21 -06:00
|
|
|
}
|
|
|
|
|
2021-05-17 13:00:13 -06:00
|
|
|
for (std::string_view country :
|
2017-05-30 14:42:21 -06:00
|
|
|
{"Flag_Europe", "Flag_Japan", "Flag_USA", "Flag_Australia", "Flag_France", "Flag_Germany",
|
|
|
|
"Flag_Italy", "Flag_Korea", "Flag_Netherlands", "Flag_Russia", "Flag_Spain", "Flag_Taiwan",
|
|
|
|
"Flag_International", "Flag_Unknown"})
|
|
|
|
{
|
2023-04-23 04:43:49 -06:00
|
|
|
m_countries.append(GetResourceIcon(country));
|
2017-05-30 14:42:21 -06:00
|
|
|
}
|
|
|
|
|
2023-04-23 04:43:49 -06:00
|
|
|
m_misc.append(GetResourceIcon("nobanner"));
|
|
|
|
m_misc.append(GetResourceIcon("dolphin_logo"));
|
2015-11-27 01:33:07 -07:00
|
|
|
}
|
|
|
|
|
2023-04-23 04:43:49 -06:00
|
|
|
QIcon Resources::GetPlatform(DiscIO::Platform platform)
|
2015-11-27 01:33:07 -07:00
|
|
|
{
|
2018-07-10 13:10:03 -06:00
|
|
|
return m_platforms[static_cast<int>(platform)];
|
2015-11-27 01:33:07 -07:00
|
|
|
}
|
|
|
|
|
2023-04-23 04:43:49 -06:00
|
|
|
QIcon Resources::GetCountry(DiscIO::Country country)
|
2015-11-27 01:33:07 -07:00
|
|
|
{
|
2018-07-10 13:10:03 -06:00
|
|
|
return m_countries[static_cast<int>(country)];
|
2015-11-27 01:33:07 -07:00
|
|
|
}
|
|
|
|
|
2023-04-23 04:43:49 -06:00
|
|
|
QIcon Resources::GetMisc(MiscID id)
|
2015-11-27 01:33:07 -07:00
|
|
|
{
|
2018-07-10 13:10:03 -06:00
|
|
|
return m_misc[static_cast<int>(id)];
|
2015-11-27 01:33:07 -07:00
|
|
|
}
|
2018-03-26 00:13:15 -06:00
|
|
|
|
|
|
|
QIcon Resources::GetAppIcon()
|
|
|
|
{
|
2023-04-23 04:43:49 -06:00
|
|
|
return GetMisc(MiscID::Logo);
|
2018-03-26 00:13:15 -06:00
|
|
|
}
|