From a2223124ad1cc4869fc25a362b6c2e94a7965334 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Wed, 10 Nov 2021 13:35:39 +1000 Subject: [PATCH] Qt: Correct behavior of sorting in game list model Turns out Qt inverts it for us. --- src/duckstation-qt/gamelistmodel.cpp | 62 +++++++++++---------------- src/duckstation-qt/gamelistmodel.h | 4 +- src/duckstation-qt/gamelistwidget.cpp | 3 +- 3 files changed, 29 insertions(+), 40 deletions(-) diff --git a/src/duckstation-qt/gamelistmodel.cpp b/src/duckstation-qt/gamelistmodel.cpp index 5e06c2ce3..e7cd82cef 100644 --- a/src/duckstation-qt/gamelistmodel.cpp +++ b/src/duckstation-qt/gamelistmodel.cpp @@ -375,7 +375,7 @@ void GameListModel::refresh() endResetModel(); } -bool GameListModel::titlesLessThan(int left_row, int right_row, bool ascending) const +bool GameListModel::titlesLessThan(int left_row, int right_row) const { if (left_row < 0 || left_row >= static_cast(m_game_list->GetEntryCount()) || right_row < 0 || right_row >= static_cast(m_game_list->GetEntryCount())) @@ -385,12 +385,10 @@ bool GameListModel::titlesLessThan(int left_row, int right_row, bool ascending) const GameListEntry& left = m_game_list->GetEntries().at(left_row); const GameListEntry& right = m_game_list->GetEntries().at(right_row); - return ascending ? (StringUtil::Strcasecmp(left.title.c_str(), right.title.c_str()) < 0) : - (StringUtil::Strcasecmp(right.title.c_str(), left.title.c_str()) > 0); + return (StringUtil::Strcasecmp(left.title.c_str(), right.title.c_str()) < 0); } -bool GameListModel::lessThan(const QModelIndex& left_index, const QModelIndex& right_index, int column, - bool ascending) const +bool GameListModel::lessThan(const QModelIndex& left_index, const QModelIndex& right_index, int column) const { if (!left_index.isValid() || !right_index.isValid()) return false; @@ -410,23 +408,21 @@ bool GameListModel::lessThan(const QModelIndex& left_index, const QModelIndex& r case Column_Type: { if (left.type == right.type) - return titlesLessThan(left_row, right_row, ascending); + return titlesLessThan(left_row, right_row); - return ascending ? (static_cast(left.type) < static_cast(right.type)) : - (static_cast(right.type) > static_cast(left.type)); + return (static_cast(left.type) < static_cast(right.type)); } case Column_Code: { if (left.code == right.code) - return titlesLessThan(left_row, right_row, ascending); - return ascending ? (StringUtil::Strcasecmp(left.code.c_str(), right.code.c_str()) < 0) : - (StringUtil::Strcasecmp(right.code.c_str(), left.code.c_str()) > 0); + return titlesLessThan(left_row, right_row); + return (StringUtil::Strcasecmp(left.code.c_str(), right.code.c_str()) < 0); } case Column_Title: { - return titlesLessThan(left_row, right_row, ascending); + return titlesLessThan(left_row, right_row); } case Column_FileTitle: @@ -434,68 +430,62 @@ bool GameListModel::lessThan(const QModelIndex& left_index, const QModelIndex& r const std::string_view file_title_left(FileSystem::GetFileTitleFromPath(left.path)); const std::string_view file_title_right(FileSystem::GetFileTitleFromPath(right.path)); if (file_title_left == file_title_right) - return titlesLessThan(left_row, right_row, ascending); + return titlesLessThan(left_row, right_row); const std::size_t smallest = std::min(file_title_left.size(), file_title_right.size()); - return ascending ? (StringUtil::Strncasecmp(file_title_left.data(), file_title_right.data(), smallest) < 0) : - (StringUtil::Strncasecmp(file_title_right.data(), file_title_left.data(), smallest) > 0); + return (StringUtil::Strncasecmp(file_title_left.data(), file_title_right.data(), smallest) < 0); } case Column_Region: { if (left.region == right.region) - return titlesLessThan(left_row, right_row, ascending); - return ascending ? (static_cast(left.region) < static_cast(right.region)) : - (static_cast(right.region) > static_cast(left.region)); + return titlesLessThan(left_row, right_row); + return (static_cast(left.region) < static_cast(right.region)); } case Column_Compatibility: { if (left.compatibility_rating == right.compatibility_rating) - return titlesLessThan(left_row, right_row, ascending); + return titlesLessThan(left_row, right_row); - return ascending ? (static_cast(left.compatibility_rating) < static_cast(right.compatibility_rating)) : - (static_cast(right.compatibility_rating) > static_cast(left.compatibility_rating)); + return (static_cast(left.compatibility_rating) < static_cast(right.compatibility_rating)); } case Column_Size: { if (left.total_size == right.total_size) - return titlesLessThan(left_row, right_row, ascending); + return titlesLessThan(left_row, right_row); - return ascending ? (left.total_size < right.total_size) : (right.total_size > left.total_size); + return (left.total_size < right.total_size); } case Column_Genre: { if (left.genre == right.genre) - return titlesLessThan(left_row, right_row, ascending); - return ascending ? (StringUtil::Strcasecmp(left.genre.c_str(), right.genre.c_str()) < 0) : - (StringUtil::Strcasecmp(right.genre.c_str(), left.genre.c_str()) > 0); + return titlesLessThan(left_row, right_row); + return (StringUtil::Strcasecmp(left.genre.c_str(), right.genre.c_str()) < 0); } case Column_Developer: { if (left.developer == right.developer) - return titlesLessThan(left_row, right_row, ascending); - return ascending ? (StringUtil::Strcasecmp(left.developer.c_str(), right.developer.c_str()) < 0) : - (StringUtil::Strcasecmp(right.developer.c_str(), left.developer.c_str()) > 0); + return titlesLessThan(left_row, right_row); + return (StringUtil::Strcasecmp(left.developer.c_str(), right.developer.c_str()) < 0); } case Column_Publisher: { if (left.publisher == right.publisher) - return titlesLessThan(left_row, right_row, ascending); - return ascending ? (StringUtil::Strcasecmp(left.publisher.c_str(), right.publisher.c_str()) < 0) : - (StringUtil::Strcasecmp(right.publisher.c_str(), left.publisher.c_str()) > 0); + return titlesLessThan(left_row, right_row); + return (StringUtil::Strcasecmp(left.publisher.c_str(), right.publisher.c_str()) < 0); } case Column_Year: { if (left.release_date == right.release_date) - return titlesLessThan(left_row, right_row, ascending); + return titlesLessThan(left_row, right_row); - return ascending ? (left.release_date < right.release_date) : (right.release_date > left.release_date); + return (left.release_date < right.release_date); } case Column_Players: @@ -503,9 +493,9 @@ bool GameListModel::lessThan(const QModelIndex& left_index, const QModelIndex& r u8 left_players = (left.min_players << 4) + left.max_players; u8 right_players = (right.min_players << 4) + right.max_players; if (left_players == right_players) - return titlesLessThan(left_row, right_row, ascending); + return titlesLessThan(left_row, right_row); - return ascending ? (left_players < right_players) : (right_players > left_players); + return (left_players < right_players); } default: diff --git a/src/duckstation-qt/gamelistmodel.h b/src/duckstation-qt/gamelistmodel.h index 2fae6a5d0..1bd7c9815 100644 --- a/src/duckstation-qt/gamelistmodel.h +++ b/src/duckstation-qt/gamelistmodel.h @@ -47,9 +47,9 @@ public: void refresh(); - bool titlesLessThan(int left_row, int right_row, bool ascending) const; + bool titlesLessThan(int left_row, int right_row) const; - bool lessThan(const QModelIndex& left_index, const QModelIndex& right_index, int column, bool ascending) const; + bool lessThan(const QModelIndex& left_index, const QModelIndex& right_index, int column) const; bool getShowCoverTitles() const { return m_show_titles_for_covers; } void setShowCoverTitles(bool enabled) { m_show_titles_for_covers = enabled; } diff --git a/src/duckstation-qt/gamelistwidget.cpp b/src/duckstation-qt/gamelistwidget.cpp index 9064dbe65..1b9c13876 100644 --- a/src/duckstation-qt/gamelistwidget.cpp +++ b/src/duckstation-qt/gamelistwidget.cpp @@ -24,8 +24,7 @@ public: bool lessThan(const QModelIndex& source_left, const QModelIndex& source_right) const override { - const bool ascending = sortOrder() == Qt::AscendingOrder; - return m_model->lessThan(source_left, source_right, source_left.column(), ascending); + return m_model->lessThan(source_left, source_right, source_left.column()); } private: