Cockatrice 2026-04-21-Development-2.11.0-beta.61
A virtual tabletop for multiplayer card games
Loading...
Searching...
No Matches
games_model.h
Go to the documentation of this file.
1#ifndef GAMESMODEL_H
2#define GAMESMODEL_H
3
4#include "game_type_map.h"
5
6#include <QList>
7#include <QSet>
8#include <QSortFilterProxyModel>
9#include <QTime>
10#include <libcockatrice/protocol/pb/serverinfo_game.pb.h>
11
12class UserListProxy;
13
22class GamesModel : public QAbstractTableModel
23{
24 Q_OBJECT
25private:
26 QList<ServerInfo_Game> gameList;
27 QMap<int, QString> rooms;
28 QMap<int, GameTypeMap> gameTypes;
29
30 static const int NUM_COLS = 8;
31
32public:
33 static const int SORT_ROLE = Qt::UserRole + 1;
34
41 GamesModel(const QMap<int, QString> &_rooms, const QMap<int, GameTypeMap> &_gameTypes, QObject *parent = nullptr);
42
43 [[nodiscard]] int rowCount(const QModelIndex &parent = QModelIndex()) const override
44 {
45 return parent.isValid() ? 0 : gameList.size();
46 }
47
48 [[nodiscard]] int columnCount(const QModelIndex & /*parent*/ = QModelIndex()) const override
49 {
50 return NUM_COLS;
51 }
52
53 [[nodiscard]] QVariant data(const QModelIndex &index, int role) const override;
54
55 [[nodiscard]] QVariant
56 headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
57
63 static const QString getGameCreatedString(const int secs);
64
70 const ServerInfo_Game &getGame(int row);
71
76 void updateGameList(const ServerInfo_Game &game);
77
82 {
83 return 0;
84 }
85
90 {
91 return 1;
92 }
93
97 const QMap<int, GameTypeMap> &getGameTypes()
98 {
99 return gameTypes;
100 }
101};
102
111class GamesProxyModel : public QSortFilterProxyModel
112{
113 Q_OBJECT
114private:
116
117 // If adding any additional filters, make sure to update:
118 // - GamesProxyModel()
119 // - resetFilterParameters()
120 // - areFilterParametersSetToDefaults()
121 // - loadFilterParameters()
122 // - saveFilterParameters()
123 // - filterAcceptsRow()
133 QSet<int> gameTypeFilter;
140
141signals:
143
144public:
150 explicit GamesProxyModel(QObject *parent = nullptr, const UserListProxy *_userListProxy = nullptr);
151
152 // Getters for filter parameters
153 [[nodiscard]] bool getHideBuddiesOnlyGames() const
154 {
156 }
157 [[nodiscard]] bool getHideIgnoredUserGames() const
158 {
160 }
161 [[nodiscard]] bool getHideFullGames() const
162 {
163 return hideFullGames;
164 }
165 [[nodiscard]] bool getHideGamesThatStarted() const
166 {
168 }
169 [[nodiscard]] bool getHidePasswordProtectedGames() const
170 {
172 }
173 [[nodiscard]] bool getHideNotBuddyCreatedGames() const
174 {
176 }
177 [[nodiscard]] bool getHideOpenDecklistGames() const
178 {
180 }
181 [[nodiscard]] QString getGameNameFilter() const
182 {
183 return gameNameFilter;
184 }
185 [[nodiscard]] QStringList getCreatorNameFilters() const
186 {
187 return creatorNameFilters;
188 }
189 [[nodiscard]] QSet<int> getGameTypeFilter() const
190 {
191 return gameTypeFilter;
192 }
193 [[nodiscard]] int getMaxPlayersFilterMin() const
194 {
195 return maxPlayersFilterMin;
196 }
197 [[nodiscard]] int getMaxPlayersFilterMax() const
198 {
199 return maxPlayersFilterMax;
200 }
201 [[nodiscard]] const QTime &getMaxGameAge() const
202 {
203 return maxGameAge;
204 }
205 [[nodiscard]] bool getShowOnlyIfSpectatorsCanWatch() const
206 {
208 }
209 [[nodiscard]] bool getShowSpectatorPasswordProtected() const
210 {
212 }
213 [[nodiscard]] bool getShowOnlyIfSpectatorsCanChat() const
214 {
216 }
217 [[nodiscard]] bool getShowOnlyIfSpectatorsCanSeeHands() const
218 {
220 }
221
225 void setGameFilters(bool _hideBuddiesOnlyGames,
226 bool _hideIgnoredUserGames,
227 bool _hideFullGames,
228 bool _hideGamesThatStarted,
229 bool _hidePasswordProtectedGames,
230 bool _hideNotBuddyCreatedGames,
231 bool _hideOpenDecklistGames,
232 const QString &_gameNameFilter,
233 const QStringList &_creatorNameFilter,
234 const QSet<int> &_gameTypeFilter,
235 int _maxPlayersFilterMin,
236 int _maxPlayersFilterMax,
237 const QTime &_maxGameAge,
238 bool _showOnlyIfSpectatorsCanWatch,
239 bool _showSpectatorPasswordProtected,
240 bool _showOnlyIfSpectatorsCanChat,
241 bool _showOnlyIfSpectatorsCanSeeHands);
242
243 // Individual setters
244 void setHideBuddiesOnlyGames(bool value)
245 {
246 hideBuddiesOnlyGames = value;
247 refresh();
248 }
249 void setHideIgnoredUserGames(bool value)
250 {
251 hideIgnoredUserGames = value;
252 refresh();
253 }
254 void setHideFullGames(bool value)
255 {
256 hideFullGames = value;
257 refresh();
258 }
259 void setHideGamesThatStarted(bool value)
260 {
261 hideGamesThatStarted = value;
262 refresh();
263 }
265 {
267 refresh();
268 }
270 {
272 refresh();
273 }
275 {
276 hideOpenDecklistGames = value;
277 refresh();
278 }
279 void setGameNameFilter(const QString &value)
280 {
281 gameNameFilter = value;
282 refresh();
283 }
284 void setCreatorNameFilters(const QStringList &values)
285 {
286 creatorNameFilters = values;
287 refresh();
288 }
289 void setGameTypeFilter(const QSet<int> &value)
290 {
291 gameTypeFilter = value;
292 refresh();
293 }
295 {
296 maxPlayersFilterMin = value;
297 refresh();
298 }
300 {
301 maxPlayersFilterMax = value;
302 refresh();
303 }
304 void setMaxGameAge(const QTime &value)
305 {
306 maxGameAge = value;
307 refresh();
308 }
310 {
312 refresh();
313 }
315 {
317 refresh();
318 }
320 {
322 refresh();
323 }
325 {
327 refresh();
328 }
329
333 [[nodiscard]] int getNumFilteredGames() const;
334
339
343 [[nodiscard]] bool areFilterParametersSetToDefaults() const;
344
349 void loadFilterParameters(const QMap<int, QString> &allGameTypes);
350
355 void saveFilterParameters(const QMap<int, QString> &allGameTypes);
356
360 void refresh();
361
362protected:
363 [[nodiscard]] bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
364 [[nodiscard]] bool filterAcceptsRow(int sourceRow) const;
365};
366
367#endif
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
Definition games_model.cpp:210
QMap< int, GameTypeMap > gameTypes
Definition games_model.h:28
const QMap< int, GameTypeMap > & getGameTypes()
Returns the map of game types per room.
Definition games_model.h:97
const ServerInfo_Game & getGame(int row)
Returns a reference to a specific game by row index.
Definition games_model.cpp:252
int columnCount(const QModelIndex &=QModelIndex()) const override
Definition games_model.h:48
void updateGameList(const ServerInfo_Game &game)
Updates the game list with a new or updated game.
Definition games_model.cpp:258
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Definition games_model.h:43
int startTimeColIndex()
Returns the index of the start time column.
Definition games_model.h:89
QVariant data(const QModelIndex &index, int role) const override
Definition games_model.cpp:68
GamesModel(const QMap< int, QString > &_rooms, const QMap< int, GameTypeMap > &_gameTypes, QObject *parent=nullptr)
Constructs a GamesModel.
Definition games_model.cpp:63
int roomColIndex()
Returns the index of the room column.
Definition games_model.h:81
QList< ServerInfo_Game > gameList
Definition games_model.h:26
static const QString getGameCreatedString(const int secs)
Formats the game creation time into a human-readable string.
Definition games_model.cpp:30
static const int SORT_ROLE
Definition games_model.h:33
static const int NUM_COLS
Definition games_model.h:30
QMap< int, QString > rooms
Definition games_model.h:27
bool getHidePasswordProtectedGames() const
Definition games_model.h:169
void setHideIgnoredUserGames(bool value)
Definition games_model.h:249
void setGameTypeFilter(const QSet< int > &value)
Definition games_model.h:289
bool getHideNotBuddyCreatedGames() const
Definition games_model.h:173
void setGameNameFilter(const QString &value)
Definition games_model.h:279
bool hidePasswordProtectedGames
Definition games_model.h:128
QStringList getCreatorNameFilters() const
Definition games_model.h:185
void setGameFilters(bool _hideBuddiesOnlyGames, bool _hideIgnoredUserGames, bool _hideFullGames, bool _hideGamesThatStarted, bool _hidePasswordProtectedGames, bool _hideNotBuddyCreatedGames, bool _hideOpenDecklistGames, const QString &_gameNameFilter, const QStringList &_creatorNameFilter, const QSet< int > &_gameTypeFilter, int _maxPlayersFilterMin, int _maxPlayersFilterMax, const QTime &_maxGameAge, bool _showOnlyIfSpectatorsCanWatch, bool _showSpectatorPasswordProtected, bool _showOnlyIfSpectatorsCanChat, bool _showOnlyIfSpectatorsCanSeeHands)
Sets all game filters at once.
Definition games_model.cpp:286
void setHidePasswordProtectedGames(bool value)
Definition games_model.h:264
void setHideGamesThatStarted(bool value)
Definition games_model.h:259
int getMaxPlayersFilterMax() const
Definition games_model.h:197
bool hideNotBuddyCreatedGames
Definition games_model.h:129
void setMaxPlayersFilterMin(int value)
Definition games_model.h:294
QTime maxGameAge
Definition games_model.h:135
QStringList creatorNameFilters
Definition games_model.h:132
void setHideBuddiesOnlyGames(bool value)
Definition games_model.h:244
int getMaxPlayersFilterMin() const
Definition games_model.h:193
bool areFilterParametersSetToDefaults() const
Returns true if all filter parameters are set to their defaults.
Definition games_model.cpp:353
bool getHideBuddiesOnlyGames() const
Definition games_model.h:153
void resetFilterParameters()
Resets all filter parameters to default values.
Definition games_model.cpp:347
bool getShowOnlyIfSpectatorsCanChat() const
Definition games_model.h:213
void loadFilterParameters(const QMap< int, QString > &allGameTypes)
Loads filter parameters from persistent settings.
Definition games_model.cpp:363
void setHideFullGames(bool value)
Definition games_model.h:254
QString gameNameFilter
Definition games_model.h:131
void setHideOpenDecklistGames(bool value)
Definition games_model.h:274
bool hideGamesThatStarted
Definition games_model.h:127
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override
Definition games_model.cpp:416
const UserListProxy * userListProxy
Definition games_model.h:115
quint32 maxPlayersFilterMin
Definition games_model.h:134
bool showOnlyIfSpectatorsCanChat
Definition games_model.h:138
void saveFilterParameters(const QMap< int, QString > &allGameTypes)
Saves filter parameters to persistent settings.
Definition games_model.cpp:386
bool showOnlyIfSpectatorsCanSeeHands
Definition games_model.h:139
void setShowOnlyIfSpectatorsCanSeeHands(bool value)
Definition games_model.h:324
bool hideOpenDecklistGames
Definition games_model.h:130
quint32 maxPlayersFilterMax
Definition games_model.h:134
void setShowOnlyIfSpectatorsCanChat(bool value)
Definition games_model.h:319
QSet< int > gameTypeFilter
Definition games_model.h:133
const QTime & getMaxGameAge() const
Definition games_model.h:201
bool getHideIgnoredUserGames() const
Definition games_model.h:157
void setCreatorNameFilters(const QStringList &values)
Definition games_model.h:284
void setShowSpectatorPasswordProtected(bool value)
Definition games_model.h:314
bool getShowOnlyIfSpectatorsCanWatch() const
Definition games_model.h:205
bool getShowOnlyIfSpectatorsCanSeeHands() const
Definition games_model.h:217
bool getShowSpectatorPasswordProtected() const
Definition games_model.h:209
bool showOnlyIfSpectatorsCanWatch
Definition games_model.h:136
QSet< int > getGameTypeFilter() const
Definition games_model.h:189
bool getHideGamesThatStarted() const
Definition games_model.h:165
bool showSpectatorPasswordProtected
Definition games_model.h:137
bool hideBuddiesOnlyGames
Definition games_model.h:124
int getNumFilteredGames() const
Returns the number of games filtered out by the current filter.
Definition games_model.cpp:332
void setShowOnlyIfSpectatorsCanWatch(bool value)
Definition games_model.h:309
void refresh()
Refreshes the proxy model (re-applies filters).
Definition games_model.cpp:505
void setHideNotBuddyCreatedGames(bool value)
Definition games_model.h:269
GamesProxyModel(QObject *parent=nullptr, const UserListProxy *_userListProxy=nullptr)
Constructs a GamesProxyModel.
Definition games_model.cpp:278
bool hideIgnoredUserGames
Definition games_model.h:125
bool hideFullGames
Definition games_model.h:126
void setMaxGameAge(const QTime &value)
Definition games_model.h:304
bool getHideFullGames() const
Definition games_model.h:161
bool getHideOpenDecklistGames() const
Definition games_model.h:177
QString getGameNameFilter() const
Definition games_model.h:181
void setMaxPlayersFilterMax(int value)
Definition games_model.h:299
void filtersChanged()
Definition user_list_proxy.h:19