96 changes: 44 additions & 52 deletions Source/Core/DolphinQt/Resources.cpp
Expand Up @@ -3,51 +3,56 @@

#include "DolphinQt/Resources.h"

#include <QGuiApplication>
#include <QFileInfo>
#include <QIcon>
#include <QImageReader>
#include <QPixmap>
#include <QScreen>

#include "Common/Assert.h"
#include "Common/FileUtil.h"

#include "Core/Config/MainSettings.h"

#include "DolphinQt/Settings.h"

#ifdef _WIN32
#include "DolphinQt/QtUtils/WinIconHelper.h"
#endif
bool Resources::m_svg_supported;
QList<QIcon> Resources::m_platforms;
QList<QIcon> Resources::m_countries;
QList<QIcon> Resources::m_misc;

QList<QPixmap> Resources::m_platforms;
QList<QPixmap> Resources::m_countries;
QList<QPixmap> Resources::m_misc;

QIcon Resources::GetIcon(std::string_view name, const QString& dir)
QIcon Resources::LoadNamedIcon(std::string_view name, const QString& dir)
{
QString name_owned = QString::fromLatin1(name.data(), static_cast<int>(name.size()));
QString base_path = dir + QLatin1Char{'/'} + name_owned;
const QString base_path = dir + QLatin1Char{'/'} + QString::fromLatin1(name);
const QString svg_path = base_path + QStringLiteral(".svg");

const auto dpr = QGuiApplication::primaryScreen()->devicePixelRatio();
// Prefer svg
if (m_svg_supported && QFileInfo(svg_path).exists())
return QIcon(svg_path);

QIcon icon(base_path.append(QStringLiteral(".png")));
QIcon icon;

if (dpr > 2)
{
QPixmap pixmap(base_path.append(QStringLiteral("@4x.png")));
auto load_png = [&](int scale) {
QString suffix = QStringLiteral(".png");
if (scale > 1)
suffix = QString::fromLatin1("@%1x.png").arg(scale);

QPixmap pixmap(base_path + suffix);
if (!pixmap.isNull())
{
pixmap.setDevicePixelRatio(4.0);
pixmap.setDevicePixelRatio(scale);
icon.addPixmap(pixmap);
}
}
};

return icon;
}
// 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);

QPixmap Resources::GetPixmap(std::string_view name, const QString& dir)
{
const auto icon = GetIcon(name, dir);
return icon.pixmap(icon.availableSizes()[0]);
ASSERT(icon.availableSizes().size() > 0);

return icon;
}

static QString GetCurrentThemeDir()
Expand All @@ -60,67 +65,54 @@ static QString GetResourcesDir()
return QString::fromStdString(File::GetSysDirectory() + "Resources");
}

QIcon Resources::GetScaledIcon(std::string_view name)
QIcon Resources::GetResourceIcon(std::string_view name)
{
return GetIcon(name, GetResourcesDir());
return LoadNamedIcon(name, GetResourcesDir());
}

QIcon Resources::GetScaledThemeIcon(std::string_view name)
QIcon Resources::GetThemeIcon(std::string_view name)
{
return GetIcon(name, GetCurrentThemeDir());
}

QPixmap Resources::GetScaledPixmap(std::string_view name)
{
return GetPixmap(name, GetResourcesDir());
return LoadNamedIcon(name, GetCurrentThemeDir());
}

void Resources::Init()
{
m_svg_supported = QImageReader::supportedImageFormats().contains("svg");

for (std::string_view platform :
{"Platform_Gamecube", "Platform_Wii", "Platform_Wad", "Platform_File"})
{
m_platforms.append(GetScaledPixmap(platform));
m_platforms.append(GetResourceIcon(platform));
}

for (std::string_view country :
{"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(GetScaledPixmap(country));
m_countries.append(GetResourceIcon(country));
}

m_misc.append(GetScaledPixmap("nobanner"));
m_misc.append(GetScaledPixmap("dolphin_logo"));
m_misc.append(GetScaledPixmap("Dolphin"));
m_misc.append(GetResourceIcon("nobanner"));
m_misc.append(GetResourceIcon("dolphin_logo"));
}

QPixmap Resources::GetPlatform(DiscIO::Platform platform)
QIcon Resources::GetPlatform(DiscIO::Platform platform)
{
return m_platforms[static_cast<int>(platform)];
}

QPixmap Resources::GetCountry(DiscIO::Country country)
QIcon Resources::GetCountry(DiscIO::Country country)
{
return m_countries[static_cast<int>(country)];
}

QPixmap Resources::GetMisc(MiscID id)
QIcon Resources::GetMisc(MiscID id)
{
return m_misc[static_cast<int>(id)];
}

QIcon Resources::GetAppIcon()
{
QIcon icon;

#ifdef _WIN32
icon = WinIconHelper::GetNativeIcon();
#else
icon.addPixmap(GetScaledPixmap("dolphin_logo"));
icon.addPixmap(GetScaledPixmap("Dolphin"));
#endif

return icon;
return GetMisc(MiscID::Logo);
}
27 changes: 12 additions & 15 deletions Source/Core/DolphinQt/Resources.h
Expand Up @@ -3,8 +3,8 @@

#pragma once

#include <QIcon>
#include <QList>
#include <QPixmap>
#include <string_view>

namespace DiscIO
Expand All @@ -20,29 +20,26 @@ class Resources final
enum class MiscID
{
BannerMissing,
LogoLarge,
LogoSmall
Logo,
};

static void Init();

static QPixmap GetPlatform(DiscIO::Platform platform);
static QPixmap GetCountry(DiscIO::Country country);
static QIcon GetPlatform(DiscIO::Platform platform);
static QIcon GetCountry(DiscIO::Country country);

static QPixmap GetMisc(MiscID id);
static QIcon GetMisc(MiscID id);

static QIcon GetScaledIcon(std::string_view name);
static QIcon GetScaledThemeIcon(std::string_view name);
static QIcon GetResourceIcon(std::string_view name);
static QIcon GetThemeIcon(std::string_view name);
static QIcon GetAppIcon();

static QPixmap GetScaledPixmap(std::string_view name);

private:
Resources() {}
static QIcon GetIcon(std::string_view name, const QString& dir);
static QPixmap GetPixmap(std::string_view name, const QString& dir);
static QIcon LoadNamedIcon(std::string_view name, const QString& dir);

static QList<QPixmap> m_platforms;
static QList<QPixmap> m_countries;
static QList<QPixmap> m_misc;
static bool m_svg_supported;
static QList<QIcon> m_platforms;
static QList<QIcon> m_countries;
static QList<QIcon> m_misc;
};
38 changes: 19 additions & 19 deletions Source/Core/DolphinQt/ToolBar.cpp
Expand Up @@ -156,41 +156,41 @@ void ToolBar::UpdatePausePlayButtonState(const bool playing_state)
{
disconnect(m_pause_play_action, nullptr, nullptr, nullptr);
m_pause_play_action->setText(tr("Pause"));
m_pause_play_action->setIcon(Resources::GetScaledThemeIcon("pause"));
m_pause_play_action->setIcon(Resources::GetThemeIcon("pause"));
connect(m_pause_play_action, &QAction::triggered, this, &ToolBar::PausePressed);
}
else
{
disconnect(m_pause_play_action, nullptr, nullptr, nullptr);
m_pause_play_action->setText(tr("Play"));
m_pause_play_action->setIcon(Resources::GetScaledThemeIcon("play"));
m_pause_play_action->setIcon(Resources::GetThemeIcon("play"));
connect(m_pause_play_action, &QAction::triggered, this, &ToolBar::PlayPressed);
}
}

void ToolBar::UpdateIcons()
{
m_step_action->setIcon(Resources::GetScaledThemeIcon("debugger_step_in"));
m_step_over_action->setIcon(Resources::GetScaledThemeIcon("debugger_step_over"));
m_step_out_action->setIcon(Resources::GetScaledThemeIcon("debugger_step_out"));
m_skip_action->setIcon(Resources::GetScaledThemeIcon("debugger_skip"));
m_show_pc_action->setIcon(Resources::GetScaledThemeIcon("debugger_show_pc"));
m_set_pc_action->setIcon(Resources::GetScaledThemeIcon("debugger_set_pc"));
m_step_action->setIcon(Resources::GetThemeIcon("debugger_step_in"));
m_step_over_action->setIcon(Resources::GetThemeIcon("debugger_step_over"));
m_step_out_action->setIcon(Resources::GetThemeIcon("debugger_step_out"));
m_skip_action->setIcon(Resources::GetThemeIcon("debugger_skip"));
m_show_pc_action->setIcon(Resources::GetThemeIcon("debugger_show_pc"));
m_set_pc_action->setIcon(Resources::GetThemeIcon("debugger_set_pc"));

m_open_action->setIcon(Resources::GetScaledThemeIcon("open"));
m_refresh_action->setIcon(Resources::GetScaledThemeIcon("refresh"));
m_open_action->setIcon(Resources::GetThemeIcon("open"));
m_refresh_action->setIcon(Resources::GetThemeIcon("refresh"));

const Core::State state = Core::GetState();
const bool playing = state != Core::State::Uninitialized && state != Core::State::Paused;
if (!playing)
m_pause_play_action->setIcon(Resources::GetScaledThemeIcon("play"));
m_pause_play_action->setIcon(Resources::GetThemeIcon("play"));
else
m_pause_play_action->setIcon(Resources::GetScaledThemeIcon("pause"));

m_stop_action->setIcon(Resources::GetScaledThemeIcon("stop"));
m_fullscreen_action->setIcon(Resources::GetScaledThemeIcon("fullscreen"));
m_screenshot_action->setIcon(Resources::GetScaledThemeIcon("screenshot"));
m_config_action->setIcon(Resources::GetScaledThemeIcon("config"));
m_controllers_action->setIcon(Resources::GetScaledThemeIcon("classic"));
m_graphics_action->setIcon(Resources::GetScaledThemeIcon("graphics"));
m_pause_play_action->setIcon(Resources::GetThemeIcon("pause"));

m_stop_action->setIcon(Resources::GetThemeIcon("stop"));
m_fullscreen_action->setIcon(Resources::GetThemeIcon("fullscreen"));
m_screenshot_action->setIcon(Resources::GetThemeIcon("screenshot"));
m_config_action->setIcon(Resources::GetThemeIcon("config"));
m_controllers_action->setIcon(Resources::GetThemeIcon("classic"));
m_graphics_action->setIcon(Resources::GetThemeIcon("graphics"));
}