From 93836636a1b758617018e426e4f47a1ceea55691 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Tue, 9 Jul 2024 21:26:23 +1000 Subject: [PATCH] Qt: Center type icon in game list --- src/duckstation-qt/gamelistwidget.cpp | 33 +++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/duckstation-qt/gamelistwidget.cpp b/src/duckstation-qt/gamelistwidget.cpp index 35e987dc8..12e3aa163 100644 --- a/src/duckstation-qt/gamelistwidget.cpp +++ b/src/duckstation-qt/gamelistwidget.cpp @@ -16,11 +16,13 @@ #include #include +#include #include #include #include #include #include +#include static constexpr float MIN_SCALE = 0.1f; static constexpr float MAX_SCALE = 2.0f; @@ -103,6 +105,36 @@ private: bool m_merge_disc_sets = true; }; +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(index.data(Qt::DecorationRole)); + const int pix_width = static_cast(pix.width() / pix.devicePixelRatio()); + const int pix_height = static_cast(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) { } @@ -167,6 +199,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();