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 22, 2017
1 parent 3bdd9ad commit 7ea4b1c
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 14 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
7 changes: 6 additions & 1 deletion Source/Core/DolphinQt2/GameList/GameFile.cpp
Expand Up @@ -368,5 +368,10 @@ QString FormatSize(qint64 size)
unit = i.next();
num /= 1024.0;
}
return QStringLiteral("%1 %2").arg(QString::number(num, 'f', 1)).arg(unit);
std::string number = QString::number(num, 'f', 1).toStdString();

if (num == static_cast<int>(num))
number = number.substr(0, number.length() - 2);

return QStringLiteral("%1 %2").arg(QString::fromStdString(number)).arg(unit);
}
2 changes: 1 addition & 1 deletion Source/Core/DolphinQt2/GameList/GameList.cpp
Expand Up @@ -83,7 +83,7 @@ void GameList::MakeTableView()
QHeaderView* hor_header = m_table->horizontalHeader();
hor_header->setSectionResizeMode(GameListModel::COL_PLATFORM, QHeaderView::ResizeToContents);
hor_header->setSectionResizeMode(GameListModel::COL_COUNTRY, QHeaderView::ResizeToContents);
hor_header->setSectionResizeMode(GameListModel::COL_ID, QHeaderView::ResizeToContents);
hor_header->setSectionResizeMode(GameListModel::COL_ID, QHeaderView::Fixed);
hor_header->setSectionResizeMode(GameListModel::COL_BANNER, QHeaderView::ResizeToContents);
hor_header->setSectionResizeMode(GameListModel::COL_TITLE, QHeaderView::Stretch);
hor_header->setSectionResizeMode(GameListModel::COL_MAKER, QHeaderView::Stretch);
Expand Down
25 changes: 18 additions & 7 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 @@ -38,8 +39,12 @@ void TableDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,
case GameListModel::COL_SIZE:
painter->drawText(option.rect, Qt::AlignCenter, FormatSize(data.toULongLong()));
break;
// Fall through.
case GameListModel::COL_ID:
painter->drawText(option.rect,
data.toString().length() > 6 ? Qt::AlignVCenter : Qt::AlignCenter,
data.toString());
break;
// Fall through.
case GameListModel::COL_TITLE:
case GameListModel::COL_DESCRIPTION:
case GameListModel::COL_MAKER:
Expand All @@ -52,24 +57,30 @@ 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;
return NORMAL_BANNER_SIZE * scale;
case GameListModel::COL_TITLE:
return QSize(1, 1) * scale;
default:
return QSize(0, 0);
}
}

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);
painter->drawPixmap(rect.left() + (rect.width() - pixmap.width() * scale) / 2,
rect.top() + (rect.height() - pixmap.height() * scale) / 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 7ea4b1c

Please sign in to comment.