Skip to content

Commit

Permalink
Qt: Center type icon in game list
Browse files Browse the repository at this point in the history
  • Loading branch information
kamfretoz authored and lightningterror committed Aug 24, 2024
1 parent d1a7430 commit 518728c
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions pcsx2-qt/GameList/GameListWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
#include "fmt/format.h"

#include <QtCore/QSortFilterProxyModel>
#include <QtGui/QPainter>
#include <QtGui/QPixmap>
#include <QtGui/QWheelEvent>
#include <QtWidgets/QApplication>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QMenu>
#include <QtWidgets/QScrollBar>
#include <QtWidgets/QStyledItemDelegate>

static const char* SUPPORTED_FORMATS_STRING = QT_TRANSLATE_NOOP(GameListWidget,
".bin/.iso (ISO Disc Images)\n"
Expand Down Expand Up @@ -95,6 +97,40 @@ class GameListSortModel final : public QSortFilterProxyModel
QString m_filter_name;
};

namespace
{
class GameListIconStyleDelegate final : public QStyledItemDelegate
{
public:
GameListIconStyleDelegate(QWidget* parent)
: QStyledItemDelegate(parent)
{
}
~GameListIconStyleDelegate() = default;

void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override
{
// https://stackoverflow.com/questions/32216568/how-to-set-icon-center-in-qtableview
Q_ASSERT(index.isValid());

// draw default item
QStyleOptionViewItem opt = option;
initStyleOption(&opt, index);
opt.icon = QIcon();
QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &opt, painter, 0);

const QRect r = option.rect;
const QPixmap pix = qvariant_cast<QPixmap>(index.data(Qt::DecorationRole));
const int pix_width = static_cast<int>(pix.width() / pix.devicePixelRatio());
const int pix_height = static_cast<int>(pix.width() / pix.devicePixelRatio());

// draw pixmap at center of item
const QPoint p = QPoint((r.width() - pix_width) / 2, (r.height() - pix_height) / 2);
painter->drawPixmap(r.topLeft() + p, pix);
}
};
} // namespace

GameListWidget::GameListWidget(QWidget* parent /* = nullptr */)
: QWidget(parent)
{
Expand Down Expand Up @@ -152,6 +188,7 @@ void GameListWidget::initialize()
m_table_view->verticalHeader()->hide();
m_table_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
m_table_view->setVerticalScrollMode(QAbstractItemView::ScrollMode::ScrollPerPixel);
m_table_view->setItemDelegateForColumn(0, new GameListIconStyleDelegate(this));

loadTableViewColumnVisibilitySettings();
loadTableViewColumnSortSettings();
Expand Down

0 comments on commit 518728c

Please sign in to comment.