Cockatrice 2025-11-30-Development-2.11.0-beta.38
A cross-platform 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
141public:
147 explicit GamesProxyModel(QObject *parent = nullptr, const UserListProxy *_userListProxy = nullptr);
148
149 // Getters for filter parameters
150 [[nodiscard]] bool getHideBuddiesOnlyGames() const
151 {
153 }
154 [[nodiscard]] bool getHideIgnoredUserGames() const
155 {
157 }
158 [[nodiscard]] bool getHideFullGames() const
159 {
160 return hideFullGames;
161 }
162 [[nodiscard]] bool getHideGamesThatStarted() const
163 {
165 }
166 [[nodiscard]] bool getHidePasswordProtectedGames() const
167 {
169 }
170 [[nodiscard]] bool getHideNotBuddyCreatedGames() const
171 {
173 }
174 [[nodiscard]] bool getHideOpenDecklistGames() const
175 {
177 }
178 [[nodiscard]] QString getGameNameFilter() const
179 {
180 return gameNameFilter;
181 }
182 [[nodiscard]] QStringList getCreatorNameFilters() const
183 {
184 return creatorNameFilters;
185 }
186 [[nodiscard]] QSet<int> getGameTypeFilter() const
187 {
188 return gameTypeFilter;
189 }
190 [[nodiscard]] int getMaxPlayersFilterMin() const
191 {
192 return maxPlayersFilterMin;
193 }
194 [[nodiscard]] int getMaxPlayersFilterMax() const
195 {
196 return maxPlayersFilterMax;
197 }
198 [[nodiscard]] const QTime &getMaxGameAge() const
199 {
200 return maxGameAge;
201 }
202 [[nodiscard]] bool getShowOnlyIfSpectatorsCanWatch() const
203 {
205 }
206 [[nodiscard]] bool getShowSpectatorPasswordProtected() const
207 {
209 }
210 [[nodiscard]] bool getShowOnlyIfSpectatorsCanChat() const
211 {
213 }
214 [[nodiscard]] bool getShowOnlyIfSpectatorsCanSeeHands() const
215 {
217 }
218
222 void setGameFilters(bool _hideBuddiesOnlyGames,
223 bool _hideIgnoredUserGames,
224 bool _hideFullGames,
225 bool _hideGamesThatStarted,
226 bool _hidePasswordProtectedGames,
227 bool _hideNotBuddyCreatedGames,
228 bool _hideOpenDecklistGames,
229 const QString &_gameNameFilter,
230 const QStringList &_creatorNameFilter,
231 const QSet<int> &_gameTypeFilter,
232 int _maxPlayersFilterMin,
233 int _maxPlayersFilterMax,
234 const QTime &_maxGameAge,
235 bool _showOnlyIfSpectatorsCanWatch,
236 bool _showSpectatorPasswordProtected,
237 bool _showOnlyIfSpectatorsCanChat,
238 bool _showOnlyIfSpectatorsCanSeeHands);
239
240 // Individual setters
241 void setHideBuddiesOnlyGames(bool value)
242 {
243 hideBuddiesOnlyGames = value;
244 refresh();
245 }
246 void setHideIgnoredUserGames(bool value)
247 {
248 hideIgnoredUserGames = value;
249 refresh();
250 }
251 void setHideFullGames(bool value)
252 {
253 hideFullGames = value;
254 refresh();
255 }
256 void setHideGamesThatStarted(bool value)
257 {
258 hideGamesThatStarted = value;
259 refresh();
260 }
262 {
264 refresh();
265 }
267 {
269 refresh();
270 }
272 {
273 hideOpenDecklistGames = value;
274 refresh();
275 }
276 void setGameNameFilter(const QString &value)
277 {
278 gameNameFilter = value;
279 refresh();
280 }
281 void setCreatorNameFilters(const QStringList &values)
282 {
283 creatorNameFilters = values;
284 refresh();
285 }
286 void setGameTypeFilter(const QSet<int> &value)
287 {
288 gameTypeFilter = value;
289 refresh();
290 }
292 {
293 maxPlayersFilterMin = value;
294 refresh();
295 }
297 {
298 maxPlayersFilterMax = value;
299 refresh();
300 }
301 void setMaxGameAge(const QTime &value)
302 {
303 maxGameAge = value;
304 refresh();
305 }
307 {
309 refresh();
310 }
312 {
314 refresh();
315 }
317 {
319 refresh();
320 }
322 {
324 refresh();
325 }
326
330 [[nodiscard]] int getNumFilteredGames() const;
331
336
340 [[nodiscard]] bool areFilterParametersSetToDefaults() const;
341
346 void loadFilterParameters(const QMap<int, QString> &allGameTypes);
347
352 void saveFilterParameters(const QMap<int, QString> &allGameTypes);
353
357 void refresh();
358
359protected:
360 [[nodiscard]] bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
361 [[nodiscard]] bool filterAcceptsRow(int sourceRow) const;
362};
363
364#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:166
void setHideIgnoredUserGames(bool value)
Definition games_model.h:246
void setGameTypeFilter(const QSet< int > &value)
Definition games_model.h:286
bool getHideNotBuddyCreatedGames() const
Definition games_model.h:170
void setGameNameFilter(const QString &value)
Definition games_model.h:276
bool hidePasswordProtectedGames
Definition games_model.h:128
QStringList getCreatorNameFilters() const
Definition games_model.h:182
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:261
void setHideGamesThatStarted(bool value)
Definition games_model.h:256
int getMaxPlayersFilterMax() const
Definition games_model.h:194
bool hideNotBuddyCreatedGames
Definition games_model.h:129
void setMaxPlayersFilterMin(int value)
Definition games_model.h:291
QTime maxGameAge
Definition games_model.h:135
QStringList creatorNameFilters
Definition games_model.h:132
void setHideBuddiesOnlyGames(bool value)
Definition games_model.h:241
int getMaxPlayersFilterMin() const
Definition games_model.h:190
bool areFilterParametersSetToDefaults() const
Returns true if all filter parameters are set to their defaults.
Definition games_model.cpp:352
bool getHideBuddiesOnlyGames() const
Definition games_model.h:150
void resetFilterParameters()
Resets all filter parameters to default values.
Definition games_model.cpp:346
bool getShowOnlyIfSpectatorsCanChat() const
Definition games_model.h:210
void loadFilterParameters(const QMap< int, QString > &allGameTypes)
Loads filter parameters from persistent settings.
Definition games_model.cpp:362
void setHideFullGames(bool value)
Definition games_model.h:251
QString gameNameFilter
Definition games_model.h:131
void setHideOpenDecklistGames(bool value)
Definition games_model.h:271
bool hideGamesThatStarted
Definition games_model.h:127
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override
Definition games_model.cpp:415
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:385
bool showOnlyIfSpectatorsCanSeeHands
Definition games_model.h:139
void setShowOnlyIfSpectatorsCanSeeHands(bool value)
Definition games_model.h:321
bool hideOpenDecklistGames
Definition games_model.h:130
quint32 maxPlayersFilterMax
Definition games_model.h:134
void setShowOnlyIfSpectatorsCanChat(bool value)
Definition games_model.h:316
QSet< int > gameTypeFilter
Definition games_model.h:133
const QTime & getMaxGameAge() const
Definition games_model.h:198
bool getHideIgnoredUserGames() const
Definition games_model.h:154
void setCreatorNameFilters(const QStringList &values)
Definition games_model.h:281
void setShowSpectatorPasswordProtected(bool value)
Definition games_model.h:311
bool getShowOnlyIfSpectatorsCanWatch() const
Definition games_model.h:202
bool getShowOnlyIfSpectatorsCanSeeHands() const
Definition games_model.h:214
bool getShowSpectatorPasswordProtected() const
Definition games_model.h:206
bool showOnlyIfSpectatorsCanWatch
Definition games_model.h:136
QSet< int > getGameTypeFilter() const
Definition games_model.h:186
bool getHideGamesThatStarted() const
Definition games_model.h:162
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:331
void setShowOnlyIfSpectatorsCanWatch(bool value)
Definition games_model.h:306
void refresh()
Refreshes the proxy model (re-applies filters).
Definition games_model.cpp:506
void setHideNotBuddyCreatedGames(bool value)
Definition games_model.h:266
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:301
bool getHideFullGames() const
Definition games_model.h:158
bool getHideOpenDecklistGames() const
Definition games_model.h:174
QString getGameNameFilter() const
Definition games_model.h:178
void setMaxPlayersFilterMax(int value)
Definition games_model.h:296
Definition user_list_proxy.h:19