Skip to content

Commit 7e04671

Browse files
committed
GameList: Use fixed size for icon column
Fixes sluggish icon size change and speeds up startup since we no longer need to load every icon.
1 parent 393724a commit 7e04671

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

src/duckstation-qt/gamelistwidget.cpp

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ const char* GameListModel::getColumnName(Column col)
134134

135135
GameListModel::GameListModel(GameListWidget* parent)
136136
: QAbstractTableModel(parent), m_device_pixel_ratio(QtUtils::GetDevicePixelRatioForWidget(parent)),
137-
m_memcard_pixmap_cache(MIN_COVER_CACHE_SIZE)
137+
m_icon_pixmap_cache(MIN_COVER_CACHE_SIZE)
138138
{
139139
m_cover_scale = Host::GetBaseFloatSettingValue("UI", "GameListCoverArtScale", 0.45f);
140140
m_icon_size = Host::GetBaseFloatSettingValue("UI", "GameListIconSize", MIN_ICON_SIZE);
@@ -171,10 +171,10 @@ void GameListModel::setShowCoverTitles(bool enabled)
171171
emit dataChanged(index(0, Column_Cover), index(rowCount() - 1, Column_Cover), {Qt::DisplayRole});
172172
}
173173

174-
int GameListModel::calculateRowHeight(const QWidget* const widget) const
174+
void GameListModel::updateRowHeight(const QWidget* const widget)
175175
{
176-
return m_icon_size + MEMORY_CARD_ICON_PADDING +
177-
widget->style()->pixelMetric(QStyle::PM_FocusFrameVMargin, nullptr, widget);
176+
m_row_height = m_icon_size + MEMORY_CARD_ICON_PADDING +
177+
widget->style()->pixelMetric(QStyle::PM_FocusFrameVMargin, nullptr, widget);
178178
}
179179

180180
void GameListModel::setShowGameIcons(bool enabled)
@@ -188,7 +188,7 @@ void GameListModel::setShowGameIcons(bool enabled)
188188

189189
void GameListModel::refreshIcons()
190190
{
191-
m_memcard_pixmap_cache.Clear();
191+
m_icon_pixmap_cache.Clear();
192192
emit dataChanged(index(0, Column_Icon), index(rowCount() - 1, Column_Icon), {Qt::DecorationRole});
193193
}
194194

@@ -212,6 +212,11 @@ void GameListModel::setIconSize(int size)
212212
refreshIcons();
213213
}
214214

215+
int GameListModel::getIconColumnWidth() const
216+
{
217+
return m_icon_size + MEMORY_CARD_ICON_PADDING * 2;
218+
}
219+
215220
void GameListModel::setCoverScale(float scale)
216221
{
217222
if (m_cover_scale == scale)
@@ -430,7 +435,7 @@ const QPixmap& GameListModel::getIconPixmapForEntry(const GameList::Entry* ge) c
430435
// We only do this for discs/disc sets for now.
431436
if (m_show_game_icons && (!ge->serial.empty() && (ge->IsDisc() || ge->IsDiscSet())))
432437
{
433-
QPixmap* item = m_memcard_pixmap_cache.Lookup(ge->serial);
438+
QPixmap* item = m_icon_pixmap_cache.Lookup(ge->serial);
434439
if (item)
435440
{
436441
if (!item->isNull())
@@ -456,11 +461,11 @@ const QPixmap& GameListModel::getIconPixmapForEntry(const GameList::Entry* ge) c
456461

457462
pm.setDevicePixelRatio(m_device_pixel_ratio);
458463

459-
return *m_memcard_pixmap_cache.Insert(ge->serial, std::move(pm));
464+
return *m_icon_pixmap_cache.Insert(ge->serial, std::move(pm));
460465
}
461466

462467
// Stop it trying again in the future.
463-
m_memcard_pixmap_cache.Insert(ge->serial, {});
468+
m_icon_pixmap_cache.Insert(ge->serial, {});
464469
}
465470
}
466471

@@ -499,7 +504,7 @@ QIcon GameListModel::getIconForGame(const QString& path)
499504
// Provides a small performance boost when using default size icons.
500505
if (m_icon_size == MEMORY_CARD_ICON_SIZE)
501506
{
502-
if (const QPixmap* pm = m_memcard_pixmap_cache.Lookup(entry->serial))
507+
if (const QPixmap* pm = m_icon_pixmap_cache.Lookup(entry->serial))
503508
{
504509
// If we already have the icon cached, return it.
505510
ret = QIcon(*pm);
@@ -679,6 +684,17 @@ QVariant GameListModel::data(const QModelIndex& index, int role, const GameList:
679684
return Qt::AscendingOrder;
680685
}
681686

687+
case Qt::SizeHintRole:
688+
{
689+
switch (index.column())
690+
{
691+
case Column_Icon:
692+
return QSize(getIconColumnWidth(), m_row_height);
693+
default:
694+
return {};
695+
}
696+
}
697+
682698
case Qt::DecorationRole:
683699
{
684700
switch (index.column())
@@ -812,7 +828,7 @@ void GameListModel::refresh()
812828
m_taken_entries.reset();
813829

814830
// Invalidate memcard LRU cache, forcing a re-query of the memcard timestamps.
815-
m_memcard_pixmap_cache.Clear();
831+
m_icon_pixmap_cache.Clear();
816832

817833
endResetModel();
818834
}
@@ -1695,7 +1711,9 @@ void GameListWidget::onScaleChanged()
16951711
void GameListWidget::onIconSizeChanged(int size)
16961712
{
16971713
// update size of rows
1698-
m_list_view->verticalHeader()->setDefaultSectionSize(m_model->calculateRowHeight(m_list_view));
1714+
m_model->updateRowHeight(m_list_view);
1715+
m_list_view->setFixedColumnWidth(GameListModel::Column_Icon, m_model->getIconColumnWidth());
1716+
m_list_view->verticalHeader()->setDefaultSectionSize(m_model->getRowHeight());
16991717
onScaleChanged();
17001718
}
17011719

@@ -1760,7 +1778,6 @@ GameListListView::GameListListView(GameListModel* model, GameListSortModel* sort
17601778

17611779
horizontal_header->setSectionResizeMode(GameListModel::Column_Title, QHeaderView::Stretch);
17621780
horizontal_header->setSectionResizeMode(GameListModel::Column_FileTitle, QHeaderView::Stretch);
1763-
horizontal_header->setSectionResizeMode(GameListModel::Column_Icon, QHeaderView::ResizeToContents);
17641781

17651782
verticalHeader()->hide();
17661783

@@ -1845,6 +1862,7 @@ void GameListListView::setFixedColumnWidths()
18451862
setFixedColumnWidth(fm, GameListModel::Column_FileSize, size_width);
18461863
setFixedColumnWidth(fm, GameListModel::Column_UncompressedSize, size_width);
18471864

1865+
setFixedColumnWidth(GameListModel::Column_Icon, m_model->getIconColumnWidth());
18481866
setFixedColumnWidth(GameListModel::Column_Region, 55);
18491867
setFixedColumnWidth(GameListModel::Column_Achievements, 100);
18501868
setFixedColumnWidth(GameListModel::Column_Compatibility, 100);

src/duckstation-qt/gamelistwidget.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,11 @@ class GameListModel final : public QAbstractTableModel
9393
bool getShowCoverTitles() const { return m_show_titles_for_covers; }
9494
void setShowCoverTitles(bool enabled);
9595

96-
int calculateRowHeight(const QWidget* const widget) const;
96+
void updateRowHeight(const QWidget* const widget);
97+
int getRowHeight() const { return m_row_height; }
9798
int getIconSize() const { return m_icon_size; }
9899
void setIconSize(int size);
100+
int getIconColumnWidth() const;
99101
bool getShowGameIcons() const { return m_show_game_icons; }
100102
void setShowGameIcons(bool enabled);
101103
QIcon getIconForGame(const QString& path);
@@ -141,6 +143,7 @@ class GameListModel final : public QAbstractTableModel
141143

142144
float m_cover_scale = 0.0f;
143145
int m_icon_size = 0;
146+
int m_row_height = 0;
144147
bool m_show_localized_titles = false;
145148
bool m_show_titles_for_covers = false;
146149
bool m_show_game_icons = false;
@@ -158,9 +161,9 @@ class GameListModel final : public QAbstractTableModel
158161

159162
mutable PreferUnorderedStringMap<QPixmap> m_flag_pixmap_cache;
160163

161-
mutable LRUCache<std::string, QPixmap> m_cover_pixmap_cache;
164+
mutable LRUCache<std::string, QPixmap> m_icon_pixmap_cache;
162165

163-
mutable LRUCache<std::string, QPixmap> m_memcard_pixmap_cache;
166+
mutable LRUCache<std::string, QPixmap> m_cover_pixmap_cache;
164167
};
165168

166169
class GameListListView final : public QTableView
@@ -171,6 +174,7 @@ class GameListListView final : public QTableView
171174
GameListListView(GameListModel* model, GameListSortModel* sort_model, QWidget* parent);
172175
~GameListListView() override;
173176

177+
void setFixedColumnWidth(int column, int width);
174178
void setAndSaveColumnHidden(int column, bool hidden);
175179

176180
protected:
@@ -182,7 +186,6 @@ class GameListListView final : public QTableView
182186
void onHeaderSortIndicatorChanged(int, Qt::SortOrder);
183187
void onHeaderContextMenuRequested(const QPoint& point);
184188

185-
void setFixedColumnWidth(int column, int width);
186189
void setFixedColumnWidth(const QFontMetrics& fm, int column, int str_width);
187190
void setFixedColumnWidths();
188191

0 commit comments

Comments
 (0)