From 1737e81b16a035413fa852b28cce39c6f68b2374 Mon Sep 17 00:00:00 2001 From: spycrab Date: Tue, 30 May 2017 22:42:21 +0200 Subject: [PATCH] Qt: Fix HiDPI icon scaling --- Source/Core/DolphinQt2/Main.cpp | 3 + Source/Core/DolphinQt2/Resources.cpp | 92 +++++++++++++++++++++------- Source/Core/DolphinQt2/Resources.h | 10 ++- Source/Core/DolphinQt2/ToolBar.cpp | 21 +++---- 4 files changed, 91 insertions(+), 35 deletions(-) diff --git a/Source/Core/DolphinQt2/Main.cpp b/Source/Core/DolphinQt2/Main.cpp index f140564563..781f89d2da 100644 --- a/Source/Core/DolphinQt2/Main.cpp +++ b/Source/Core/DolphinQt2/Main.cpp @@ -19,6 +19,9 @@ int main(int argc, char* argv[]) { + QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); + QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); + QApplication app(argc, argv); UICommon::SetUserDirectory(""); diff --git a/Source/Core/DolphinQt2/Resources.cpp b/Source/Core/DolphinQt2/Resources.cpp index ed96ffb04f..02c3b9c8f7 100644 --- a/Source/Core/DolphinQt2/Resources.cpp +++ b/Source/Core/DolphinQt2/Resources.cpp @@ -2,46 +2,92 @@ // Licensed under GPLv2+ // Refer to the license.txt file included. +#include +#include +#include +#include #include #include "Common/CommonPaths.h" #include "Common/FileUtil.h" #include "DolphinQt2/Resources.h" +#include "DolphinQt2/Settings.h" QList Resources::m_platforms; QList Resources::m_countries; QList Resources::m_ratings; QList Resources::m_misc; +QIcon Resources::GetIcon(const QString& name, const QString& dir) +{ + QString base_path = dir + name; + + const auto dpr = QGuiApplication::primaryScreen()->devicePixelRatio(); + + QIcon icon(base_path.append(QStringLiteral(".png"))); + + if (dpr > 2) + { + QPixmap pixmap(base_path.append(QStringLiteral("@4x.png"))); + if (!pixmap.isNull()) + { + pixmap.setDevicePixelRatio(4.0); + icon.addPixmap(pixmap); + } + } + + return icon; +} + +QPixmap Resources::GetPixmap(const QString& name, const QString& dir) +{ + const auto icon = GetIcon(name, dir); + return icon.pixmap(icon.availableSizes()[0]); +} + +QIcon Resources::GetScaledIcon(const std::string& name) +{ + return GetIcon(QString::fromStdString(name), Settings().GetResourcesDir()); +} + +QIcon Resources::GetScaledThemeIcon(const std::string& name) +{ + return GetIcon(QString::fromStdString(name), Settings().GetThemeDir()); +} + +QPixmap Resources::GetScaledPixmap(const std::string& name) +{ + return GetPixmap(QString::fromStdString(name), Settings().GetResourcesDir()); +} + +QPixmap Resources::GetScaledThemePixmap(const std::string& name) +{ + return GetPixmap(QString::fromStdString(name), Settings().GetThemeDir()); +} + void Resources::Init() { QString sys_dir = QString::fromStdString(File::GetSysDirectory() + RESOURCES_DIR + DIR_SEP); - QStringList platforms{QStringLiteral("Platform_Gamecube.png"), QStringLiteral("Platform_Wii.png"), - QStringLiteral("Platform_Wad.png"), QStringLiteral("Platform_File.png")}; - for (QString platform : platforms) - m_platforms.append(QPixmap(platform.prepend(sys_dir))); + for (const std::string& platform : + {"Platform_Gamecube", "Platform_Wii", "Platform_Wad", "Platform_File"}) + { + m_platforms.append(GetScaledPixmap(platform)); + } - QStringList countries{ - QStringLiteral("Flag_Europe.png"), QStringLiteral("Flag_Japan.png"), - QStringLiteral("Flag_USA.png"), QStringLiteral("Flag_Australia.png"), - QStringLiteral("Flag_France.png"), QStringLiteral("Flag_Germany.png"), - QStringLiteral("Flag_Italy.png"), QStringLiteral("Flag_Korea.png"), - QStringLiteral("Flag_Netherlands.png"), QStringLiteral("Flag_Russia.png"), - QStringLiteral("Flag_Spain.png"), QStringLiteral("Flag_Taiwan.png"), - QStringLiteral("Flag_International.png"), QStringLiteral("Flag_Unknown.png")}; - for (QString country : countries) - m_countries.append(QPixmap(country.prepend(sys_dir))); + for (const std::string& 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)); + } + for (int stars = 0; stars <= 5; stars++) + m_ratings.append(GetScaledPixmap("rating" + std::to_string(stars))); - QStringList ratings{QStringLiteral("rating0.png"), QStringLiteral("rating1.png"), - QStringLiteral("rating2.png"), QStringLiteral("rating3.png"), - QStringLiteral("rating4.png"), QStringLiteral("rating5.png")}; - for (QString rating : ratings) - m_ratings.append(QPixmap(rating.prepend(sys_dir))); - - m_misc.append(QPixmap(QStringLiteral("nobanner.png").prepend(sys_dir))); - m_misc.append(QPixmap(QStringLiteral("dolphin_logo.png").prepend(sys_dir))); - m_misc.append(QPixmap(QStringLiteral("Dolphin.png").prepend(sys_dir))); + m_misc.append(GetScaledPixmap("nobanner")); + m_misc.append(GetScaledPixmap("dolphin_logo")); + m_misc.append(GetScaledPixmap("Dolphin")); } QPixmap Resources::GetPlatform(int platform) diff --git a/Source/Core/DolphinQt2/Resources.h b/Source/Core/DolphinQt2/Resources.h index a6631da550..3a9027e3f4 100644 --- a/Source/Core/DolphinQt2/Resources.h +++ b/Source/Core/DolphinQt2/Resources.h @@ -5,7 +5,6 @@ #pragma once #include -#include // Store for various QPixmaps that will be used repeatedly. class Resources final @@ -26,8 +25,17 @@ public: LOGO_SMALL }; + static QIcon GetScaledIcon(const std::string& name); + static QIcon GetScaledThemeIcon(const std::string& name); + + static QPixmap GetScaledPixmap(const std::string& name); + static QPixmap GetScaledThemePixmap(const std::string& name); + private: Resources() {} + static QIcon GetIcon(const QString& name, const QString& dir); + static QPixmap GetPixmap(const QString& name, const QString& dir); + static QList m_platforms; static QList m_countries; static QList m_ratings; diff --git a/Source/Core/DolphinQt2/ToolBar.cpp b/Source/Core/DolphinQt2/ToolBar.cpp index e53b30cead..adb344a2a3 100644 --- a/Source/Core/DolphinQt2/ToolBar.cpp +++ b/Source/Core/DolphinQt2/ToolBar.cpp @@ -4,6 +4,7 @@ #include +#include "DolphinQt2/Resources.h" #include "DolphinQt2/Settings.h" #include "DolphinQt2/ToolBar.h" @@ -91,15 +92,13 @@ void ToolBar::MakeActions() void ToolBar::UpdateIcons() { - QString dir = Settings().GetThemeDir(); - - m_open_action->setIcon(QIcon(QStringLiteral("open.png").prepend(dir))); - m_paths_action->setIcon(QIcon(QStringLiteral("browse.png").prepend(dir))); - m_play_action->setIcon(QIcon(QStringLiteral("play.png").prepend(dir))); - m_pause_action->setIcon(QIcon(QStringLiteral("pause.png").prepend(dir))); - m_stop_action->setIcon(QIcon(QStringLiteral("stop.png").prepend(dir))); - m_fullscreen_action->setIcon(QIcon(QStringLiteral("fullscreen.png").prepend(dir))); - m_screenshot_action->setIcon(QIcon(QStringLiteral("screenshot.png").prepend(dir))); - m_config_action->setIcon(QIcon(QStringLiteral("config.png").prepend(dir))); - m_controllers_action->setIcon(QIcon(QStringLiteral("classic.png").prepend(dir))); + m_open_action->setIcon(Resources::GetScaledThemeIcon("open")); + m_paths_action->setIcon(Resources::GetScaledThemeIcon("browse")); + m_play_action->setIcon(Resources::GetScaledThemeIcon("play")); + m_pause_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")); }