From c090f9db389f9ad8bab4c6d6c04f5e7b44ba11a3 Mon Sep 17 00:00:00 2001 From: VocalFan <45863583+VocalFan@users.noreply.github.com> Date: Sun, 17 Sep 2023 05:21:32 -0400 Subject: [PATCH] Add name sorting. (#347) --- src/game_models.cpp | 26 ++++++++++++++++++++++++++ src/game_models.hpp | 1 + src/main_window.cpp | 11 +++++++---- src/main_window.hpp | 2 +- 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/game_models.cpp b/src/game_models.cpp index 19626682f..eb1ff18e6 100644 --- a/src/game_models.cpp +++ b/src/game_models.cpp @@ -21,6 +21,11 @@ QPixmap Game::icon() const // Construct icon object. QPixmap icon(path.c_str()); + // For games with large icon sizes. + if (icon.width() != 512 || icon.height() != 512) { + icon = icon.scaled(512, 512, Qt::KeepAspectRatio, Qt::SmoothTransformation); + } + icon.setDevicePixelRatio(2.0); return icon; @@ -42,6 +47,8 @@ void GameListModel::add(Game *game) beginInsertRows(QModelIndex(), m_items.size(), m_items.size()); m_items.append(game); endInsertRows(); + + sort(0); } void GameListModel::clear() @@ -73,3 +80,22 @@ QVariant GameListModel::data(const QModelIndex &index, int role) const return QVariant(); } } + +void GameListModel::sort(int column, Qt::SortOrder order) +{ + if (column != 0) + return; + + emit layoutAboutToBeChanged(); + + auto compare = [order](const Game* a, const Game* b) { + if (order == Qt::AscendingOrder) + return a->name().toLower() < b->name().toLower(); + else + return a->name().toLower() > b->name().toLower(); + }; + + std::sort(m_items.begin(), m_items.end(), compare); + + emit layoutChanged(); +} diff --git a/src/game_models.hpp b/src/game_models.hpp index 0ebb8950a..3b0a81d0f 100644 --- a/src/game_models.hpp +++ b/src/game_models.hpp @@ -28,6 +28,7 @@ class GameListModel final : public QAbstractListModel { void add(Game *game); Game *get(int i) const { return m_items[i]; } void clear(); + void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override; public: int rowCount(const QModelIndex &parent = QModelIndex()) const override; diff --git a/src/main_window.cpp b/src/main_window.cpp index ba4bfbbf4..3980762a5 100644 --- a/src/main_window.cpp +++ b/src/main_window.cpp @@ -157,6 +157,7 @@ bool MainWindow::loadGames() // Load games progress.setLabelText("Loading games..."); + auto gameList = reinterpret_cast(m_games->model()); for (auto &gameId : games) { if (progress.wasCanceled() || !loadGame(gameId)) { @@ -166,6 +167,8 @@ bool MainWindow::loadGames() progress.setValue(++step); } + gameList->sort(0, Qt::AscendingOrder); // TODO add ability to select descending order (button?) + return true; } @@ -206,7 +209,7 @@ void MainWindow::closeEvent(QCloseEvent *event) void MainWindow::resizeEvent(QResizeEvent *event) { // Allows the games list to resort if window is resized. - if (m_games) { + if (m_tab->currentIndex() == 0) { m_games->updateGeometry(); m_games->doItemsLayout(); } @@ -214,10 +217,10 @@ void MainWindow::resizeEvent(QResizeEvent *event) QMainWindow::resizeEvent(event); } -void MainWindow::tabChanged(int index) +void MainWindow::tabChanged() { - // Check if the Games tab is selected - if (index == 0 && m_games) { + // Update games list if window was resized on another tab. + if (m_tab->currentIndex() == 0) { m_games->updateGeometry(); m_games->doItemsLayout(); } diff --git a/src/main_window.hpp b/src/main_window.hpp index 99c4f58fd..3a0c291e6 100644 --- a/src/main_window.hpp +++ b/src/main_window.hpp @@ -19,7 +19,7 @@ class MainWindow final : public QMainWindow { void resizeEvent(QResizeEvent *event) override; private slots: - void tabChanged(int index); + void tabChanged(); void installPkg(); void openSystemFolder(); void reportIssue();