game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
game_list_proxy_model.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// ui/qt/game_list_proxy_model.cpp
4// ==============================================================================
5// Implements the filter/sort proxy over GameListModel.
6// ==============================================================================
7
9
10#include "models/game.h"
11#include "widgets/game_list_delegate.h" // GameDataRole enum
13
14#include <QStringList>
15
16namespace
17{
18// Qt 6.10+ exposes beginFilterChange()/endFilterChange(). invalidateFilter()
19// is slated for deprecation in Qt 6.13. We route through a single helper so
20// the choice is localised.
21} // namespace
22
23GameListProxyModel::GameListProxyModel(QObject* parent) : QSortFilterProxyModel(parent)
24{
25 setFilterCaseSensitivity(Qt::CaseInsensitive);
26 setDynamicSortFilter(true);
27 // No proxy-level sorting: the library controller already orders
28 // filteredGames_ per the current SortOption (Name/LastPlayed/PlayTime/
29 // DateAdded, Asc/Desc) and GameListModel::setGames() preserves that
30 // order. Calling sort() here would force the proxy's lessThan() to
31 // re-order the rows — and because lessThan() always compares by
32 // SortKey::Name in ascending order, it would silently clobber any
33 // descending sort the user selected (e.g. "Z-A" displaying as A-Z).
34}
35
36// ==============================================================================
37// Filter setters
38// ==============================================================================
39
40void GameListProxyModel::setSearchText(const QString& text)
41{
42 if (searchText_ == text)
43 return;
44 searchText_ = text;
45 reapplyFilter();
46}
47
48void GameListProxyModel::setSourceFilter(const QString& source)
49{
50 if (sourceFilter_ == source)
51 return;
52 sourceFilter_ = source;
53 reapplyFilter();
54}
55
56void GameListProxyModel::setRunnerFilter(const QString& runner)
57{
58 if (runnerFilter_ == runner)
59 return;
60 runnerFilter_ = runner;
61 reapplyFilter();
62}
63
64void GameListProxyModel::setCollectionFilter(const QString& collection)
65{
66 if (collectionFilter_ == collection)
67 return;
68 collectionFilter_ = collection;
69 reapplyFilter();
70}
71
73{
74 if (showRunningOnly_ == running)
75 return;
76 showRunningOnly_ = running;
77 reapplyFilter();
78}
79
81{
82 if (showUpdatesOnly_ == updatesOnly)
83 return;
84 showUpdatesOnly_ = updatesOnly;
85 reapplyFilter();
86}
87
89{
90 if (showFavoritesOnly_ == favoritesOnly)
91 return;
92 showFavoritesOnly_ = favoritesOnly;
93 reapplyFilter();
94}
95
97{
98 searchText_.clear();
99 sourceFilter_.clear();
100 runnerFilter_.clear();
101 collectionFilter_.clear();
102 showRunningOnly_ = false;
103 showUpdatesOnly_ = false;
104 showFavoritesOnly_ = false;
105 reapplyFilter();
106}
107
108void GameListProxyModel::reapplyFilter()
109{
110#if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
111 beginFilterChange();
112 endFilterChange(); // equivalent semantics; see Qt 6.10 changelog
113#else
114 invalidateFilter();
115#endif
116}
117
119{
120 if (sortKey_ == key)
121 return;
122 sortKey_ = key;
123 invalidate(); // triggers re-sort via lessThan()
124}
125
126// ==============================================================================
127// filterAcceptsRow
128// ==============================================================================
129
130bool GameListProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const
131{
132 const QAbstractItemModel* m = sourceModel();
133 if (!m)
134 return false;
135 const QModelIndex idx = m->index(sourceRow, 0, sourceParent);
136 if (!idx.isValid())
137 return false;
138
139 // Search text — substring match against the name.
140 if (!searchText_.isEmpty())
141 {
142 const QString name = idx.data(Qt::DisplayRole).toString();
143 if (!name.contains(searchText_, Qt::CaseInsensitive))
144 return false;
145 }
146
147 if (!sourceFilter_.isEmpty())
148 {
149 if (idx.data(GameSourceRole).toString() != sourceFilter_)
150 return false;
151 }
152
153 if (!runnerFilter_.isEmpty())
154 {
155 if (idx.data(GameRunnerRole).toString() != runnerFilter_)
156 return false;
157 }
158
159 if (!collectionFilter_.isEmpty())
160 {
161 // Collections live on the Game*; look it up by asking the source model
162 // via its concrete type (we control the source so this is fine).
163 const auto* gm = qobject_cast<const GameListModel*>(sourceModel());
164 if (!gm)
165 return false;
166 const Game* g = gm->gameAt(sourceRow);
167 if (!g)
168 return false;
169 const std::string target = collectionFilter_.toStdString();
170 const auto& cols = g->collections;
171 if (std::find(cols.begin(), cols.end(), target) == cols.end())
172 return false;
173 }
174
175 if (showRunningOnly_ && !idx.data(GameIsRunningRole).toBool())
176 return false;
177
178 if (showUpdatesOnly_ && !idx.data(GameNeedsUpdateRole).toBool())
179 return false;
180
181 if (showFavoritesOnly_ && !idx.data(GameIsFavoriteRole).toBool())
182 return false;
183
184 return true;
185}
186
187// ==============================================================================
188// lessThan (sort)
189// ==============================================================================
190
191bool GameListProxyModel::lessThan(const QModelIndex& left, const QModelIndex& right) const
192{
193 switch (sortKey_)
194 {
195 case SortKey::Name:
196 {
197 const QString a = left.data(Qt::DisplayRole).toString();
198 const QString b = right.data(Qt::DisplayRole).toString();
199 return QString::compare(a, b, Qt::CaseInsensitive) < 0;
200 }
202 {
203 // Newer (larger) timestamp first — invert natural ordering.
204 return left.data(GameLastPlayedRole).toLongLong() >
205 right.data(GameLastPlayedRole).toLongLong();
206 }
208 {
209 return left.data(GamePlayTimeRole).toInt() > right.data(GamePlayTimeRole).toInt();
210 }
211 case SortKey::Recent:
212 {
213 // Fall back to last-played if dateAdded isn't available on roles.
214 return left.data(GameLastPlayedRole).toLongLong() >
215 right.data(GameLastPlayedRole).toLongLong();
216 }
217 }
218 return false;
219}
void setRunnerFilter(const QString &runner)
Restrict to a specific runner version ("proton-ge-9", "Wine 9.0", etc.).
void setShowRunningOnly(bool running)
Additional booleans applied on top of the main filters.
GameListProxyModel(QObject *parent=nullptr)
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override
void setSourceFilter(const QString &source)
Restrict to a specific source name ("Steam", "GOG", etc.) or empty for all.
void setCollectionFilter(const QString &collection)
Restrict to a collection name, or empty for all.
void setShowFavoritesOnly(bool favoritesOnly)
void setSearchText(const QString &text)
Free-text search (matches against name, developer, publisher).
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override
void setShowUpdatesOnlyGame(bool updatesOnly)
void clearAllFilters()
Reset all filters in one shot.
Complete representation of a game in the library.
Definition game.h:767
std::vector< std::string > collections
Collections this game belongs to.
Definition game.h:804
@ GameRunnerRole
@ GameNeedsUpdateRole
@ GameSourceRole
@ GameIsRunningRole
@ GameLastPlayedRole
@ GamePlayTimeRole
@ GameIsFavoriteRole