Skip to content

Commit

Permalink
Qt: Fix HiDPI icon scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
spycrab committed May 21, 2017
1 parent 3bdd9ad commit 1919a22
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 9 deletions.
1 change: 1 addition & 0 deletions Source/Core/DolphinQt2/CMakeLists.txt
Expand Up @@ -8,6 +8,7 @@ set(CMAKE_AUTOMOC ON)

set(SRCS
AboutDialog.cpp
DPI.cpp
Host.cpp
InDevelopmentWarning.cpp
Main.cpp
Expand Down
11 changes: 7 additions & 4 deletions Source/Core/DolphinQt2/Config/ControllersWindow.cpp
Expand Up @@ -28,6 +28,7 @@
#include "Core/IOS/USB/Bluetooth/BTReal.h"
#include "Core/NetPlayProto.h"
#include "DolphinQt2/Config/Mapping/MappingWindow.h"
#include "DolphinQt2/DPI.h"
#include "DolphinQt2/Settings.h"
#include "UICommon/UICommon.h"

Expand Down Expand Up @@ -69,8 +70,9 @@ static int FromWiimoteMenuIndex(const int menudevice)
ControllersWindow::ControllersWindow(QWidget* parent)
: QDialog(parent),
m_configure_icon(Settings().GetThemeDir().append(QStringLiteral("config.png"))),
m_gamecube_icon(Settings().GetResourcesDir().append(QStringLiteral("Platform_Gamecube.png"))),
m_wii_icon(Settings().GetResourcesDir().append(QStringLiteral("Platform_Wii.png")))
m_gamecube_icon(
Settings().GetResourcesDir().append(QStringLiteral("Platform_Gamecube@2x.png"))),
m_wii_icon(Settings().GetResourcesDir().append(QStringLiteral("Platform_Wii@2x.png")))
{
setWindowTitle(tr("Controller Settings"));

Expand All @@ -93,7 +95,8 @@ void ControllersWindow::CreateGamecubeLayout()
m_gc_box = new QGroupBox();
m_gc_layout = new QFormLayout();
m_gc_label = new QLabel();
m_gc_label->setPixmap(QPixmap(m_gamecube_icon));

m_gc_label->setPixmap(DPI::GetScaledPixmap(this, m_gamecube_icon));
m_gc_label->setAlignment(Qt::AlignCenter);
m_gc_layout->addRow(m_gc_label);

Expand Down Expand Up @@ -168,7 +171,7 @@ void ControllersWindow::CreateWiimoteLayout()
m_wiimote_layout->setLabelAlignment(Qt::AlignLeft);

m_wii_label = new QLabel();
m_wii_label->setPixmap(QPixmap(m_wii_icon));
m_wii_label->setPixmap(DPI::GetScaledPixmap(this, m_wii_icon));
m_wii_label->setAlignment(Qt::AlignCenter);
m_wiimote_layout->addRow(m_wii_label);

Expand Down
21 changes: 21 additions & 0 deletions Source/Core/DolphinQt2/DPI.cpp
@@ -0,0 +1,21 @@
// Copyright 2017 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include "DolphinQt2/DPI.h"

#include <QPaintDevice>
#include <QSize>

double DPI::GetScale(const QPaintDevice* device)
{
return (static_cast<double>(device->logicalDpiX()) + static_cast<double>(device->logicalDpiY())) /
2 / 100;
}

QPixmap DPI::GetScaledPixmap(const QPaintDevice* device, const QString& path)
{
const auto scale = GetScale(device);
QPixmap pixmap(path);
return pixmap.scaled(pixmap.size() * scale, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
16 changes: 16 additions & 0 deletions Source/Core/DolphinQt2/DPI.h
@@ -0,0 +1,16 @@
// Copyright 2017 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#pragma once

#include <QPixmap>
#include <QString>

class QPaintDevice;

namespace DPI
{
double GetScale(const QPaintDevice* device);
QPixmap GetScaledPixmap(const QPaintDevice* device, const QString& path);
}
1 change: 1 addition & 0 deletions Source/Core/DolphinQt2/DolphinQt2.vcxproj
Expand Up @@ -134,6 +134,7 @@
<ClCompile Include="$(QtMocOutPrefix)ToolBar.cpp" />

<ClCompile Include="AboutDialog.cpp" />
<ClCompile Include="DPI.cpp" />
<ClCompile Include="Config\ControllersWindow.cpp" />
<ClCompile Include="Config\FilesystemWidget.cpp" />
<ClCompile Include="Config\InfoWidget.cpp" />
Expand Down
13 changes: 9 additions & 4 deletions Source/Core/DolphinQt2/GameList/TableDelegate.cpp
Expand Up @@ -4,6 +4,7 @@

#include <QPainter>

#include "DolphinQt2/DPI.h"
#include "DolphinQt2/GameList/GameFile.h"
#include "DolphinQt2/GameList/GameListModel.h"
#include "DolphinQt2/GameList/TableDelegate.h"
Expand Down Expand Up @@ -52,14 +53,15 @@ void TableDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,

QSize TableDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{
const auto scale = DPI::GetScale(static_cast<QWidget*>(parent()));
switch (index.column())
{
case GameListModel::COL_PLATFORM:
return Resources::GetPlatform(0).size();
return Resources::GetPlatform(0).size() * scale;
case GameListModel::COL_COUNTRY:
return Resources::GetCountry(0).size();
return Resources::GetCountry(0).size() * scale;
case GameListModel::COL_RATING:
return Resources::GetRating(0).size();
return Resources::GetRating(0).size() * scale;
case GameListModel::COL_BANNER:
return NORMAL_BANNER_SIZE;
default:
Expand All @@ -69,7 +71,10 @@ QSize TableDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIn

void TableDelegate::DrawPixmap(QPainter* painter, const QRect& rect, const QPixmap& pixmap) const
{
const auto scale = DPI::GetScale(painter->device());

// We don't want to stretch the pixmap out, so center it in the rect.
painter->drawPixmap(rect.left() + (rect.width() - pixmap.width()) / 2,
rect.top() + (rect.height() - pixmap.height()) / 2, pixmap);
rect.top() + (rect.height() - pixmap.height()) / 2, pixmap.width() * scale,
pixmap.height() * scale, pixmap);
}
2 changes: 1 addition & 1 deletion Source/Core/DolphinQt2/GameList/TableDelegate.h
Expand Up @@ -11,7 +11,7 @@ class TableDelegate final : public QStyledItemDelegate
Q_OBJECT

public:
explicit TableDelegate(QWidget* parent = nullptr);
explicit TableDelegate(QWidget* parent);
void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const;
QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const;

Expand Down

0 comments on commit 1919a22

Please sign in to comment.