1 #include "dlg_filter_games.h"
2 
3 #include <QCheckBox>
4 #include <QComboBox>
5 #include <QCryptographicHash>
6 #include <QDialogButtonBox>
7 #include <QGridLayout>
8 #include <QGroupBox>
9 #include <QHBoxLayout>
10 #include <QLabel>
11 #include <QLineEdit>
12 #include <QPushButton>
13 #include <QSpinBox>
14 #include <QVBoxLayout>
15 
DlgFilterGames(const QMap<int,QString> & _allGameTypes,const GamesProxyModel * _gamesProxyModel,QWidget * parent)16 DlgFilterGames::DlgFilterGames(const QMap<int, QString> &_allGameTypes,
17                                const GamesProxyModel *_gamesProxyModel,
18                                QWidget *parent)
19     : QDialog(parent), allGameTypes(_allGameTypes), gamesProxyModel(_gamesProxyModel),
20       gameAgeMap({{QTime(), tr("no limit")},
21                   {QTime(0, 5), tr("5 minutes")},
22                   {QTime(0, 10), tr("10 minutes")},
23                   {QTime(0, 30), tr("30 minutes")},
24                   {QTime(1, 0), tr("1 hour")},
25                   {QTime(2, 0), tr("2 hours")}})
26 {
27     showBuddiesOnlyGames = new QCheckBox(tr("Show '&buddies only' games"));
28     showBuddiesOnlyGames->setChecked(gamesProxyModel->getShowBuddiesOnlyGames());
29 
30     showFullGames = new QCheckBox(tr("Show &full games"));
31     showFullGames->setChecked(gamesProxyModel->getShowFullGames());
32 
33     showGamesThatStarted = new QCheckBox(tr("Show games &that have started"));
34     showGamesThatStarted->setChecked(gamesProxyModel->getShowGamesThatStarted());
35 
36     showPasswordProtectedGames = new QCheckBox(tr("Show &password protected games"));
37     showPasswordProtectedGames->setChecked(gamesProxyModel->getShowPasswordProtectedGames());
38 
39     hideIgnoredUserGames = new QCheckBox(tr("Hide '&ignored user' games"));
40     hideIgnoredUserGames->setChecked(gamesProxyModel->getHideIgnoredUserGames());
41 
42     maxGameAgeComboBox = new QComboBox();
43     maxGameAgeComboBox->setEditable(false);
44     maxGameAgeComboBox->addItems(gameAgeMap.values());
45     QTime gameAge = gamesProxyModel->getMaxGameAge();
46     maxGameAgeComboBox->setCurrentIndex(gameAgeMap.keys().indexOf(gameAge)); // index is -1 if unknown
47     auto *maxGameAgeLabel = new QLabel(tr("&Newer than:"));
48     maxGameAgeLabel->setBuddy(maxGameAgeComboBox);
49 
50     gameNameFilterEdit = new QLineEdit;
51     gameNameFilterEdit->setText(gamesProxyModel->getGameNameFilter());
52     auto *gameNameFilterLabel = new QLabel(tr("Game &description:"));
53     gameNameFilterLabel->setBuddy(gameNameFilterEdit);
54     creatorNameFilterEdit = new QLineEdit;
55     creatorNameFilterEdit->setText(gamesProxyModel->getCreatorNameFilter());
56     auto *creatorNameFilterLabel = new QLabel(tr("&Creator name:"));
57     creatorNameFilterLabel->setBuddy(creatorNameFilterEdit);
58 
59     auto *generalGrid = new QGridLayout;
60     generalGrid->addWidget(gameNameFilterLabel, 0, 0);
61     generalGrid->addWidget(gameNameFilterEdit, 0, 1);
62     generalGrid->addWidget(creatorNameFilterLabel, 1, 0);
63     generalGrid->addWidget(creatorNameFilterEdit, 1, 1);
64     generalGrid->addWidget(maxGameAgeLabel, 2, 0);
65     generalGrid->addWidget(maxGameAgeComboBox, 2, 1);
66     generalGroupBox = new QGroupBox(tr("General"));
67     generalGroupBox->setLayout(generalGrid);
68 
69     auto *gameTypeFilterLayout = new QVBoxLayout;
70     QMapIterator<int, QString> gameTypesIterator(allGameTypes);
71     while (gameTypesIterator.hasNext()) {
72         gameTypesIterator.next();
73 
74         auto *temp = new QCheckBox(gameTypesIterator.value());
75         temp->setChecked(gamesProxyModel->getGameTypeFilter().contains(gameTypesIterator.key()));
76 
77         gameTypeFilterCheckBoxes.insert(gameTypesIterator.key(), temp);
78         gameTypeFilterLayout->addWidget(temp);
79     }
80     QGroupBox *gameTypeFilterGroupBox;
81     if (!allGameTypes.isEmpty()) {
82         gameTypeFilterGroupBox = new QGroupBox(tr("&Game types"));
83         gameTypeFilterGroupBox->setLayout(gameTypeFilterLayout);
84     } else
85         gameTypeFilterGroupBox = nullptr;
86 
87     auto *maxPlayersFilterMinLabel = new QLabel(tr("at &least:"));
88     maxPlayersFilterMinSpinBox = new QSpinBox;
89     maxPlayersFilterMinSpinBox->setMinimum(1);
90     maxPlayersFilterMinSpinBox->setMaximum(99);
91     maxPlayersFilterMinSpinBox->setValue(gamesProxyModel->getMaxPlayersFilterMin());
92     maxPlayersFilterMinLabel->setBuddy(maxPlayersFilterMinSpinBox);
93 
94     auto *maxPlayersFilterMaxLabel = new QLabel(tr("at &most:"));
95     maxPlayersFilterMaxSpinBox = new QSpinBox;
96     maxPlayersFilterMaxSpinBox->setMinimum(1);
97     maxPlayersFilterMaxSpinBox->setMaximum(99);
98     maxPlayersFilterMaxSpinBox->setValue(gamesProxyModel->getMaxPlayersFilterMax());
99     maxPlayersFilterMaxLabel->setBuddy(maxPlayersFilterMaxSpinBox);
100 
101     auto *maxPlayersFilterLayout = new QGridLayout;
102     maxPlayersFilterLayout->addWidget(maxPlayersFilterMinLabel, 0, 0);
103     maxPlayersFilterLayout->addWidget(maxPlayersFilterMinSpinBox, 0, 1);
104     maxPlayersFilterLayout->addWidget(maxPlayersFilterMaxLabel, 1, 0);
105     maxPlayersFilterLayout->addWidget(maxPlayersFilterMaxSpinBox, 1, 1);
106 
107     auto *maxPlayersGroupBox = new QGroupBox(tr("Maximum player count"));
108     maxPlayersGroupBox->setLayout(maxPlayersFilterLayout);
109 
110     auto *restrictionsLayout = new QGridLayout;
111     restrictionsLayout->addWidget(showFullGames, 0, 0);
112     restrictionsLayout->addWidget(showGamesThatStarted, 1, 0);
113     restrictionsLayout->addWidget(showPasswordProtectedGames, 2, 0);
114     restrictionsLayout->addWidget(showBuddiesOnlyGames, 3, 0);
115     restrictionsLayout->addWidget(hideIgnoredUserGames, 4, 0);
116 
117     auto *restrictionsGroupBox = new QGroupBox(tr("Restrictions"));
118     restrictionsGroupBox->setLayout(restrictionsLayout);
119 
120     showOnlyIfSpectatorsCanWatch = new QCheckBox(tr("Show games only if &spectators can watch"));
121     showOnlyIfSpectatorsCanWatch->setChecked(gamesProxyModel->getShowOnlyIfSpectatorsCanWatch());
122     connect(showOnlyIfSpectatorsCanWatch, SIGNAL(toggled(bool)), this, SLOT(toggleSpectatorCheckboxEnabledness(bool)));
123 
124     showSpectatorPasswordProtected = new QCheckBox(tr("Show spectator password p&rotected games"));
125     showSpectatorPasswordProtected->setChecked(gamesProxyModel->getShowSpectatorPasswordProtected());
126     showOnlyIfSpectatorsCanChat = new QCheckBox(tr("Show only if spectators can ch&at"));
127     showOnlyIfSpectatorsCanChat->setChecked(gamesProxyModel->getShowOnlyIfSpectatorsCanChat());
128     showOnlyIfSpectatorsCanSeeHands = new QCheckBox(tr("Show only if spectators can see &hands"));
129     showOnlyIfSpectatorsCanSeeHands->setChecked(gamesProxyModel->getShowOnlyIfSpectatorsCanSeeHands());
130     toggleSpectatorCheckboxEnabledness(getShowOnlyIfSpectatorsCanWatch());
131 
132     auto *spectatorsLayout = new QGridLayout;
133     spectatorsLayout->addWidget(showOnlyIfSpectatorsCanWatch, 0, 0);
134     spectatorsLayout->addWidget(showSpectatorPasswordProtected, 1, 0);
135     spectatorsLayout->addWidget(showOnlyIfSpectatorsCanChat, 2, 0);
136     spectatorsLayout->addWidget(showOnlyIfSpectatorsCanSeeHands, 3, 0);
137 
138     auto *spectatorsGroupBox = new QGroupBox(tr("Spectators"));
139     spectatorsGroupBox->setLayout(spectatorsLayout);
140 
141     auto *leftGrid = new QGridLayout;
142     leftGrid->addWidget(generalGroupBox, 0, 0, 1, 2);
143     leftGrid->addWidget(maxPlayersGroupBox, 2, 0, 1, 2);
144     leftGrid->addWidget(restrictionsGroupBox, 3, 0, 1, 2);
145 
146     auto *leftColumn = new QVBoxLayout;
147     leftColumn->addLayout(leftGrid);
148     leftColumn->addStretch();
149 
150     auto *rightGrid = new QGridLayout;
151     rightGrid->addWidget(gameTypeFilterGroupBox, 0, 0, 1, 1);
152     rightGrid->addWidget(spectatorsGroupBox, 1, 0, 1, 1);
153 
154     auto *rightColumn = new QVBoxLayout;
155     rightColumn->addLayout(rightGrid);
156     rightColumn->addStretch();
157 
158     auto *hbox = new QHBoxLayout;
159     hbox->addLayout(leftColumn);
160     hbox->addLayout(rightColumn);
161 
162     auto *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
163     connect(buttonBox, SIGNAL(accepted()), this, SLOT(actOk()));
164     connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
165 
166     auto *mainLayout = new QVBoxLayout;
167     mainLayout->addLayout(hbox);
168     mainLayout->addWidget(buttonBox);
169 
170     setLayout(mainLayout);
171     setWindowTitle(tr("Filter games"));
172 
173     setFixedHeight(sizeHint().height());
174 }
175 
actOk()176 void DlgFilterGames::actOk()
177 {
178     accept();
179 }
180 
toggleSpectatorCheckboxEnabledness(bool spectatorsEnabled)181 void DlgFilterGames::toggleSpectatorCheckboxEnabledness(bool spectatorsEnabled)
182 {
183     showSpectatorPasswordProtected->setDisabled(!spectatorsEnabled);
184     showOnlyIfSpectatorsCanChat->setDisabled(!spectatorsEnabled);
185     showOnlyIfSpectatorsCanSeeHands->setDisabled(!spectatorsEnabled);
186 }
187 
getShowFullGames() const188 bool DlgFilterGames::getShowFullGames() const
189 {
190     return showFullGames->isChecked();
191 }
192 
getShowGamesThatStarted() const193 bool DlgFilterGames::getShowGamesThatStarted() const
194 {
195     return showGamesThatStarted->isChecked();
196 }
197 
getShowBuddiesOnlyGames() const198 bool DlgFilterGames::getShowBuddiesOnlyGames() const
199 {
200     return showBuddiesOnlyGames->isChecked();
201 }
202 
setShowBuddiesOnlyGames(bool _showBuddiesOnlyGames)203 void DlgFilterGames::setShowBuddiesOnlyGames(bool _showBuddiesOnlyGames)
204 {
205     showBuddiesOnlyGames->setChecked(_showBuddiesOnlyGames);
206 }
207 
getShowPasswordProtectedGames() const208 bool DlgFilterGames::getShowPasswordProtectedGames() const
209 {
210     return showPasswordProtectedGames->isChecked();
211 }
212 
setShowPasswordProtectedGames(bool _passwordProtectedGamesHidden)213 void DlgFilterGames::setShowPasswordProtectedGames(bool _passwordProtectedGamesHidden)
214 {
215     showPasswordProtectedGames->setChecked(_passwordProtectedGamesHidden);
216 }
217 
getHideIgnoredUserGames() const218 bool DlgFilterGames::getHideIgnoredUserGames() const
219 {
220     return hideIgnoredUserGames->isChecked();
221 }
222 
setHideIgnoredUserGames(bool _hideIgnoredUserGames)223 void DlgFilterGames::setHideIgnoredUserGames(bool _hideIgnoredUserGames)
224 {
225     hideIgnoredUserGames->setChecked(_hideIgnoredUserGames);
226 }
227 
getGameNameFilter() const228 QString DlgFilterGames::getGameNameFilter() const
229 {
230     return gameNameFilterEdit->text();
231 }
232 
setGameNameFilter(const QString & _gameNameFilter)233 void DlgFilterGames::setGameNameFilter(const QString &_gameNameFilter)
234 {
235     gameNameFilterEdit->setText(_gameNameFilter);
236 }
237 
getCreatorNameFilter() const238 QString DlgFilterGames::getCreatorNameFilter() const
239 {
240     return creatorNameFilterEdit->text();
241 }
242 
setCreatorNameFilter(const QString & _creatorNameFilter)243 void DlgFilterGames::setCreatorNameFilter(const QString &_creatorNameFilter)
244 {
245     creatorNameFilterEdit->setText(_creatorNameFilter);
246 }
247 
getGameTypeFilter() const248 QSet<int> DlgFilterGames::getGameTypeFilter() const
249 {
250     QSet<int> result;
251     QMapIterator<int, QCheckBox *> i(gameTypeFilterCheckBoxes);
252     while (i.hasNext()) {
253         i.next();
254         if (i.value()->isChecked())
255             result.insert(i.key());
256     }
257     return result;
258 }
259 
setGameTypeFilter(const QSet<int> & _gameTypeFilter)260 void DlgFilterGames::setGameTypeFilter(const QSet<int> &_gameTypeFilter)
261 {
262     QMapIterator<int, QCheckBox *> i(gameTypeFilterCheckBoxes);
263     while (i.hasNext()) {
264         i.next();
265         i.value()->setChecked(_gameTypeFilter.contains(i.key()));
266     }
267 }
268 
getMaxPlayersFilterMin() const269 int DlgFilterGames::getMaxPlayersFilterMin() const
270 {
271     return maxPlayersFilterMinSpinBox->value();
272 }
273 
getMaxPlayersFilterMax() const274 int DlgFilterGames::getMaxPlayersFilterMax() const
275 {
276     return maxPlayersFilterMaxSpinBox->value();
277 }
278 
getMaxGameAge() const279 const QTime &DlgFilterGames::getMaxGameAge() const
280 {
281     int index = maxGameAgeComboBox->currentIndex();
282     if (index < 0 || index >= gameAgeMap.size()) { // index is out of bounds
283         return gamesProxyModel->getMaxGameAge();   // leave the setting unchanged
284     }
285     return gameAgeMap.keys().at(index);
286 }
287 
setMaxPlayersFilter(int _maxPlayersFilterMin,int _maxPlayersFilterMax)288 void DlgFilterGames::setMaxPlayersFilter(int _maxPlayersFilterMin, int _maxPlayersFilterMax)
289 {
290     maxPlayersFilterMinSpinBox->setValue(_maxPlayersFilterMin);
291     maxPlayersFilterMaxSpinBox->setValue(_maxPlayersFilterMax == -1 ? maxPlayersFilterMaxSpinBox->maximum()
292                                                                     : _maxPlayersFilterMax);
293 }
294 
getShowOnlyIfSpectatorsCanWatch() const295 bool DlgFilterGames::getShowOnlyIfSpectatorsCanWatch() const
296 {
297     return showOnlyIfSpectatorsCanWatch->isChecked();
298 }
299 
getShowSpectatorPasswordProtected() const300 bool DlgFilterGames::getShowSpectatorPasswordProtected() const
301 {
302     return showSpectatorPasswordProtected->isEnabled() && showSpectatorPasswordProtected->isChecked();
303 }
304 
getShowOnlyIfSpectatorsCanChat() const305 bool DlgFilterGames::getShowOnlyIfSpectatorsCanChat() const
306 {
307     return showOnlyIfSpectatorsCanChat->isEnabled() && showOnlyIfSpectatorsCanChat->isChecked();
308 }
309 
getShowOnlyIfSpectatorsCanSeeHands() const310 bool DlgFilterGames::getShowOnlyIfSpectatorsCanSeeHands() const
311 {
312     return showOnlyIfSpectatorsCanSeeHands->isEnabled() && showOnlyIfSpectatorsCanSeeHands->isChecked();
313 }
314