Skip to content

Commit

Permalink
Add name sorting. (#347)
Browse files Browse the repository at this point in the history
  • Loading branch information
FluffyOMC authored Sep 17, 2023
1 parent 4fccc1a commit c090f9d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
26 changes: 26 additions & 0 deletions src/game_models.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
Expand Down Expand Up @@ -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();
}
1 change: 1 addition & 0 deletions src/game_models.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 7 additions & 4 deletions src/main_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ bool MainWindow::loadGames()

// Load games
progress.setLabelText("Loading games...");
auto gameList = reinterpret_cast<GameListModel *>(m_games->model());

for (auto &gameId : games) {
if (progress.wasCanceled() || !loadGame(gameId)) {
Expand All @@ -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;
}

Expand Down Expand Up @@ -206,18 +209,18 @@ 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();
}

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();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main_window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit c090f9d

Please sign in to comment.