1 #ifndef GAMESMODEL_H
2 #define GAMESMODEL_H
3 
4 #include "gametypemap.h"
5 #include "pb/serverinfo_game.pb.h"
6 #include "tab_supervisor.h"
7 
8 #include <QAbstractTableModel>
9 #include <QList>
10 #include <QSet>
11 #include <QSortFilterProxyModel>
12 #include <QStringList>
13 #include <QTime>
14 
15 class GamesModel : public QAbstractTableModel
16 {
17     Q_OBJECT
18 private:
19     QList<ServerInfo_Game> gameList;
20     QMap<int, QString> rooms;
21     QMap<int, GameTypeMap> gameTypes;
22 
23     static const int NUM_COLS = 8;
24 
25 public:
26     static const int SORT_ROLE = Qt::UserRole + 1;
27 
28     GamesModel(const QMap<int, QString> &_rooms, const QMap<int, GameTypeMap> &_gameTypes, QObject *parent = nullptr);
29     int rowCount(const QModelIndex &parent = QModelIndex()) const
30     {
31         return parent.isValid() ? 0 : gameList.size();
32     }
33     int columnCount(const QModelIndex & /*parent*/ = QModelIndex()) const
34     {
35         return NUM_COLS;
36     }
37     QVariant data(const QModelIndex &index, int role) const;
38     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
39     static const QString getGameCreatedString(const int secs);
40     const ServerInfo_Game &getGame(int row);
41 
42     /**
43      * Update game list with a (possibly new) game.
44      */
45     void updateGameList(const ServerInfo_Game &game);
46 
roomColIndex()47     int roomColIndex()
48     {
49         return 0;
50     }
startTimeColIndex()51     int startTimeColIndex()
52     {
53         return 1;
54     }
55 
getGameTypes()56     const QMap<int, GameTypeMap> &getGameTypes()
57     {
58         return gameTypes;
59     }
60 };
61 
62 class ServerInfo_User;
63 
64 class GamesProxyModel : public QSortFilterProxyModel
65 {
66     Q_OBJECT
67 private:
68     bool ownUserIsRegistered;
69     const TabSupervisor *tabSupervisor;
70 
71     // If adding any additional filters, make sure to update:
72     // - GamesProxyModel()
73     // - resetFilterParameters()
74     // - areFilterParametersSetToDefaults()
75     // - loadFilterParameters()
76     // - saveFilterParameters()
77     // - filterAcceptsRow()
78     bool showBuddiesOnlyGames;
79     bool hideIgnoredUserGames;
80     bool showFullGames;
81     bool showGamesThatStarted;
82     bool showPasswordProtectedGames;
83     QString gameNameFilter, creatorNameFilter;
84     QSet<int> gameTypeFilter;
85     quint32 maxPlayersFilterMin, maxPlayersFilterMax;
86     QTime maxGameAge;
87     bool showOnlyIfSpectatorsCanWatch, showSpectatorPasswordProtected, showOnlyIfSpectatorsCanChat,
88         showOnlyIfSpectatorsCanSeeHands;
89 
90 public:
91     GamesProxyModel(QObject *parent = nullptr, const TabSupervisor *_tabSupervisor = nullptr);
92 
getShowBuddiesOnlyGames()93     bool getShowBuddiesOnlyGames() const
94     {
95         return showBuddiesOnlyGames;
96     }
97     void setShowBuddiesOnlyGames(bool _showBuddiesOnlyGames);
getHideIgnoredUserGames()98     bool getHideIgnoredUserGames() const
99     {
100         return hideIgnoredUserGames;
101     }
102     void setHideIgnoredUserGames(bool _hideIgnoredUserGames);
getShowFullGames()103     bool getShowFullGames() const
104     {
105         return showFullGames;
106     }
107     void setShowFullGames(bool _showFullGames);
getShowGamesThatStarted()108     bool getShowGamesThatStarted() const
109     {
110         return showGamesThatStarted;
111     }
112     void setShowGamesThatStarted(bool _showGamesThatStarted);
getShowPasswordProtectedGames()113     bool getShowPasswordProtectedGames() const
114     {
115         return showPasswordProtectedGames;
116     }
117     void setShowPasswordProtectedGames(bool _showPasswordProtectedGames);
getGameNameFilter()118     QString getGameNameFilter() const
119     {
120         return gameNameFilter;
121     }
122     void setGameNameFilter(const QString &_gameNameFilter);
getCreatorNameFilter()123     QString getCreatorNameFilter() const
124     {
125         return creatorNameFilter;
126     }
127     void setCreatorNameFilter(const QString &_creatorNameFilter);
getGameTypeFilter()128     QSet<int> getGameTypeFilter() const
129     {
130         return gameTypeFilter;
131     }
132     void setGameTypeFilter(const QSet<int> &_gameTypeFilter);
getMaxPlayersFilterMin()133     int getMaxPlayersFilterMin() const
134     {
135         return maxPlayersFilterMin;
136     }
getMaxPlayersFilterMax()137     int getMaxPlayersFilterMax() const
138     {
139         return maxPlayersFilterMax;
140     }
141     void setMaxPlayersFilter(int _maxPlayersFilterMin, int _maxPlayersFilterMax);
getMaxGameAge()142     const QTime &getMaxGameAge() const
143     {
144         return maxGameAge;
145     }
146     void setMaxGameAge(const QTime &_maxGameAge);
getShowOnlyIfSpectatorsCanWatch()147     bool getShowOnlyIfSpectatorsCanWatch() const
148     {
149         return showOnlyIfSpectatorsCanWatch;
150     }
151     void setShowOnlyIfSpectatorsCanWatch(bool _showOnlyIfSpectatorsCanWatch);
getShowSpectatorPasswordProtected()152     bool getShowSpectatorPasswordProtected() const
153     {
154         return showSpectatorPasswordProtected;
155     }
156     void setShowSpectatorPasswordProtected(bool _showSpectatorPasswordProtected);
getShowOnlyIfSpectatorsCanChat()157     bool getShowOnlyIfSpectatorsCanChat() const
158     {
159         return showOnlyIfSpectatorsCanChat;
160     }
161     void setShowOnlyIfSpectatorsCanChat(bool _showOnlyIfSpectatorsCanChat);
getShowOnlyIfSpectatorsCanSeeHands()162     bool getShowOnlyIfSpectatorsCanSeeHands() const
163     {
164         return showOnlyIfSpectatorsCanSeeHands;
165     }
166     void setShowOnlyIfSpectatorsCanSeeHands(bool _showOnlyIfSpectatorsCanSeeHands);
167 
168     int getNumFilteredGames() const;
169     void resetFilterParameters();
170     bool areFilterParametersSetToDefaults() const;
171     void loadFilterParameters(const QMap<int, QString> &allGameTypes);
172     void saveFilterParameters(const QMap<int, QString> &allGameTypes);
173     void refresh();
174 
175 protected:
176     bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
177     bool filterAcceptsRow(int sourceRow) const;
178 };
179 
180 #endif
181