From 426cdd16112d0c0855374ccee42fdcebba90ddd8 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Fri, 4 Apr 2025 21:44:51 +1000 Subject: [PATCH] Qt: Center icons in grid view --- src/duckstation-qt/gamelistwidget.cpp | 31 +++++++++++++++++++++++++++ src/duckstation-qt/gamelistwidget.h | 13 ++++++++--- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/duckstation-qt/gamelistwidget.cpp b/src/duckstation-qt/gamelistwidget.cpp index a1bb4d10f..d59d1e2fd 100644 --- a/src/duckstation-qt/gamelistwidget.cpp +++ b/src/duckstation-qt/gamelistwidget.cpp @@ -1196,7 +1196,9 @@ void GameListWidget::initialize() m_list_view->setContextMenuPolicy(Qt::CustomContextMenu); m_list_view->setFrameStyle(QFrame::NoFrame); m_list_view->setVerticalScrollMode(QAbstractItemView::ScrollMode::ScrollPerPixel); + m_list_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); m_list_view->verticalScrollBar()->setSingleStep(15); + onCoverScaleChanged(); connect(m_list_view->selectionModel(), &QItemSelectionModel::currentChanged, this, @@ -1441,10 +1443,13 @@ void GameListWidget::onCoverScaleChanged() m_model->updateCacheSize(width(), height()); m_list_view->setSpacing(m_model->getCoverArtSpacing()); + m_list_view->setIconSize(QSize(m_model->getCoverArtWidth(), m_model->getCoverArtHeight())); QFont font; font.setPointSizeF(20.0f * m_model->getCoverScale()); m_list_view->setFont(font); + + m_list_view->updateLayout(); } void GameListWidget::listZoom(float delta) @@ -1784,3 +1789,29 @@ void GameListGridListView::wheelEvent(QWheelEvent* e) QListView::wheelEvent(e); } + +void GameListGridListView::resizeEvent(QResizeEvent* e) +{ + updateLayout(); + QListView::resizeEvent(e); +} + +void GameListGridListView::updateLayout() +{ + const int scrollbar_width = verticalScrollBar()->width(); + const int item_spacing = spacing(); + const int icon_margin = style()->pixelMetric(QStyle::PM_FocusFrameHMargin, nullptr, this) * 2; + + // I hate this +2. Not sure what's not being accounted for, but without it the calculation is off by 2 pixels... + const int icon_width = iconSize().width() + icon_margin + item_spacing + 2; + const int available_width = width() - scrollbar_width - item_spacing - icon_margin; + const int items_per_row = available_width / icon_width; + const int margin = (available_width - (items_per_row * icon_width)) / 2; + + m_horizontal_offset = margin; +} + +int GameListGridListView::horizontalOffset() const +{ + return QListView::horizontalOffset() - m_horizontal_offset; +} diff --git a/src/duckstation-qt/gamelistwidget.h b/src/duckstation-qt/gamelistwidget.h index 8a186e7c7..d52ab4df0 100644 --- a/src/duckstation-qt/gamelistwidget.h +++ b/src/duckstation-qt/gamelistwidget.h @@ -152,22 +152,29 @@ private: mutable LRUCache m_memcard_pixmap_cache; }; -class GameListGridListView : public QListView +class GameListGridListView final : public QListView { Q_OBJECT public: GameListGridListView(QWidget* parent = nullptr); + void updateLayout(); + int horizontalOffset() const override; + Q_SIGNALS: void zoomOut(); void zoomIn(); protected: - void wheelEvent(QWheelEvent* e); + void wheelEvent(QWheelEvent* e) override; + void resizeEvent(QResizeEvent* e) override; + +private: + int m_horizontal_offset = 0; }; -class GameListWidget : public QWidget +class GameListWidget final : public QWidget { Q_OBJECT