dolphin/Source/Core/DolphinQt/Resources.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

119 lines
2.9 KiB
C++
Raw Normal View History

2015-11-27 09:33:07 +01:00
// Copyright 2015 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
2015-11-27 09:33:07 +01:00
2021-12-10 03:22:16 +01:00
#include "DolphinQt/Resources.h"
#include <QFileInfo>
2017-05-30 22:42:21 +02:00
#include <QIcon>
#include <QImageReader>
2017-05-30 22:42:21 +02:00
#include <QPixmap>
2015-11-27 09:33:07 +01:00
#include "Common/Assert.h"
2015-11-27 09:33:07 +01:00
#include "Common/FileUtil.h"
2018-05-28 03:48:04 +02:00
#include "Core/Config/MainSettings.h"
2018-05-28 03:48:04 +02:00
2018-07-07 00:40:15 +02:00
#include "DolphinQt/Settings.h"
2015-11-27 09:33:07 +01:00
bool Resources::m_svg_supported;
QList<QIcon> Resources::m_platforms;
QList<QIcon> Resources::m_countries;
QList<QIcon> Resources::m_misc;
2018-03-26 08:13:15 +02:00
QIcon Resources::LoadNamedIcon(std::string_view name, const QString& dir)
2017-05-30 22:42:21 +02:00
{
const QString base_path = dir + QLatin1Char{'/'} + QString::fromLatin1(name);
const QString svg_path = base_path + QStringLiteral(".svg");
2017-05-30 22:42:21 +02:00
// Prefer svg
if (m_svg_supported && QFileInfo(svg_path).exists())
return QIcon(svg_path);
2017-05-30 22:42:21 +02:00
QIcon icon;
2017-05-30 22:42:21 +02: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 22:42:21 +02:00
if (!pixmap.isNull())
{
pixmap.setDevicePixelRatio(scale);
2017-05-30 22:42:21 +02:00
icon.addPixmap(pixmap);
}
};
2017-05-30 22:42:21 +02: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 22:42:21 +02:00
ASSERT(icon.availableSizes().size() > 0);
return icon;
2017-05-30 22:42:21 +02:00
}
static QString GetCurrentThemeDir()
{
return QString::fromStdString(File::GetThemeDir(Config::Get(Config::MAIN_THEME_NAME)));
}
static QString GetResourcesDir()
{
return QString::fromStdString(File::GetSysDirectory() + "Resources");
}
QIcon Resources::GetResourceIcon(std::string_view name)
2017-05-30 22:42:21 +02:00
{
return LoadNamedIcon(name, GetResourcesDir());
2017-05-30 22:42:21 +02:00
}
QIcon Resources::GetThemeIcon(std::string_view name)
2017-05-30 22:42:21 +02:00
{
return LoadNamedIcon(name, GetCurrentThemeDir());
2017-05-30 22:42:21 +02:00
}
2015-11-27 09:33:07 +01:00
void Resources::Init()
{
m_svg_supported = QImageReader::supportedImageFormats().contains("svg");
for (std::string_view platform :
2017-05-30 22:42:21 +02:00
{"Platform_Gamecube", "Platform_Wii", "Platform_Wad", "Platform_File"})
{
m_platforms.append(GetResourceIcon(platform));
2017-05-30 22:42:21 +02:00
}
for (std::string_view country :
2017-05-30 22:42:21 +02: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"})
{
m_countries.append(GetResourceIcon(country));
2017-05-30 22:42:21 +02:00
}
m_misc.append(GetResourceIcon("nobanner"));
m_misc.append(GetResourceIcon("dolphin_logo"));
2015-11-27 09:33:07 +01:00
}
QIcon Resources::GetPlatform(DiscIO::Platform platform)
2015-11-27 09:33:07 +01:00
{
return m_platforms[static_cast<int>(platform)];
2015-11-27 09:33:07 +01:00
}
QIcon Resources::GetCountry(DiscIO::Country country)
2015-11-27 09:33:07 +01:00
{
return m_countries[static_cast<int>(country)];
2015-11-27 09:33:07 +01:00
}
QIcon Resources::GetMisc(MiscID id)
2015-11-27 09:33:07 +01:00
{
return m_misc[static_cast<int>(id)];
2015-11-27 09:33:07 +01:00
}
2018-03-26 08:13:15 +02:00
QIcon Resources::GetAppIcon()
{
return GetMisc(MiscID::Logo);
2018-03-26 08:13:15 +02:00
}