From 8141901679fe8c66d8ea079f441546c0a81693ef Mon Sep 17 00:00:00 2001 From: arxanas Date: Mon, 7 Jul 2014 17:31:42 -0400 Subject: [PATCH 1/2] Fix #137: Game filter preferences are saved. --- cockatrice/src/dlg_filter_games.cpp | 76 ++++++++++++++++++++++++++--- cockatrice/src/dlg_filter_games.h | 4 ++ cockatrice/src/gameselector.cpp | 6 --- 3 files changed, 73 insertions(+), 13 deletions(-) diff --git a/cockatrice/src/dlg_filter_games.cpp b/cockatrice/src/dlg_filter_games.cpp index 5e402294..b4ce11c8 100644 --- a/cockatrice/src/dlg_filter_games.cpp +++ b/cockatrice/src/dlg_filter_games.cpp @@ -9,26 +9,52 @@ #include #include #include +#include -DlgFilterGames::DlgFilterGames(const QMap &allGameTypes, QWidget *parent) - : QDialog(parent) +DlgFilterGames::DlgFilterGames(const QMap &_allGameTypes, QWidget *parent) + : QDialog(parent), + allGameTypes(_allGameTypes) { + QSettings settings; + settings.beginGroup("filter_games"); + unavailableGamesVisibleCheckBox = new QCheckBox(tr("Show &unavailable games")); + unavailableGamesVisibleCheckBox->setChecked( + settings.value("unavailable_games_visible", false).toBool() + ); + passwordProtectedGamesVisibleCheckBox = new QCheckBox(tr("Show &password protected games")); + passwordProtectedGamesVisibleCheckBox->setChecked( + settings.value("password_protected_games_visible", false).toBool() + ); - QLabel *gameNameFilterLabel = new QLabel(tr("Game &description:")); gameNameFilterEdit = new QLineEdit; + gameNameFilterEdit->setText( + settings.value("game_name_filter", "").toString() + ); + QLabel *gameNameFilterLabel = new QLabel(tr("Game &description:")); gameNameFilterLabel->setBuddy(gameNameFilterEdit); - QLabel *creatorNameFilterLabel = new QLabel(tr("&Creator name:")); creatorNameFilterEdit = new QLineEdit; + creatorNameFilterEdit->setText( + settings.value("creator_name_filter", "").toString() + ); + QLabel *creatorNameFilterLabel = new QLabel(tr("&Creator name:")); creatorNameFilterLabel->setBuddy(creatorNameFilterEdit); QVBoxLayout *gameTypeFilterLayout = new QVBoxLayout; QMapIterator gameTypesIterator(allGameTypes); while (gameTypesIterator.hasNext()) { gameTypesIterator.next(); + QCheckBox *temp = new QCheckBox(gameTypesIterator.value()); + temp->setChecked( + settings.value( + "game_type/" + gameTypesIterator.value(), + false + ).toBool() + ); + gameTypeFilterCheckBoxes.insert(gameTypesIterator.key(), temp); gameTypeFilterLayout->addWidget(temp); } @@ -43,14 +69,18 @@ DlgFilterGames::DlgFilterGames(const QMap &allGameTypes, QWidget * maxPlayersFilterMinSpinBox = new QSpinBox; maxPlayersFilterMinSpinBox->setMinimum(1); maxPlayersFilterMinSpinBox->setMaximum(99); - maxPlayersFilterMinSpinBox->setValue(1); + maxPlayersFilterMinSpinBox->setValue( + settings.value("min_players", 1).toInt() + ); maxPlayersFilterMinLabel->setBuddy(maxPlayersFilterMinSpinBox); QLabel *maxPlayersFilterMaxLabel = new QLabel(tr("at &most:")); maxPlayersFilterMaxSpinBox = new QSpinBox; maxPlayersFilterMaxSpinBox->setMinimum(1); maxPlayersFilterMaxSpinBox->setMaximum(99); - maxPlayersFilterMaxSpinBox->setValue(99); + maxPlayersFilterMaxSpinBox->setValue( + settings.value("max_players", 99).toInt() + ); maxPlayersFilterMaxLabel->setBuddy(maxPlayersFilterMaxSpinBox); QGridLayout *maxPlayersFilterLayout = new QGridLayout; @@ -83,7 +113,7 @@ DlgFilterGames::DlgFilterGames(const QMap &allGameTypes, QWidget * hbox->addLayout(rightColumn); QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); - connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept())); + connect(buttonBox, SIGNAL(accepted()), this, SLOT(actOk())); connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject())); QVBoxLayout *mainLayout = new QVBoxLayout; @@ -94,6 +124,38 @@ DlgFilterGames::DlgFilterGames(const QMap &allGameTypes, QWidget * setWindowTitle(tr("Filter games")); } +void DlgFilterGames::actOk() { + QSettings settings; + settings.beginGroup("filter_games"); + settings.setValue( + "unavailable_games_visible", + unavailableGamesVisibleCheckBox->isChecked() + ); + settings.setValue( + "password_protected_games_visible", + passwordProtectedGamesVisibleCheckBox->isChecked() + ); + settings.setValue("game_name_filter", gameNameFilterEdit->text()); + settings.setValue("creator_name_filter", creatorNameFilterEdit->text()); + + QMapIterator gameTypeIterator(allGameTypes); + QMapIterator checkboxIterator(gameTypeFilterCheckBoxes); + while (gameTypeIterator.hasNext()) { + gameTypeIterator.next(); + checkboxIterator.next(); + + settings.setValue( + "game_type/" + gameTypeIterator.value(), + checkboxIterator.value()->isChecked() + ); + } + + settings.setValue("min_players", maxPlayersFilterMinSpinBox->value()); + settings.setValue("max_players", maxPlayersFilterMaxSpinBox->value()); + + accept(); +} + bool DlgFilterGames::getUnavailableGamesVisible() const { return unavailableGamesVisibleCheckBox->isChecked(); diff --git a/cockatrice/src/dlg_filter_games.h b/cockatrice/src/dlg_filter_games.h index 8059da45..ac727d19 100644 --- a/cockatrice/src/dlg_filter_games.h +++ b/cockatrice/src/dlg_filter_games.h @@ -19,6 +19,10 @@ private: QMap gameTypeFilterCheckBoxes; QSpinBox *maxPlayersFilterMinSpinBox; QSpinBox *maxPlayersFilterMaxSpinBox; + + const QMap &allGameTypes; +private slots: + void actOk(); public: DlgFilterGames(const QMap &allGameTypes, QWidget *parent = 0); diff --git a/cockatrice/src/gameselector.cpp b/cockatrice/src/gameselector.cpp index 6a05d905..f5ef9ec8 100644 --- a/cockatrice/src/gameselector.cpp +++ b/cockatrice/src/gameselector.cpp @@ -81,12 +81,6 @@ void GameSelector::actSetFilter() if (room) gameTypeMap = gameListModel->getGameTypes().value(room->getRoomId()); DlgFilterGames dlg(gameTypeMap, this); - dlg.setUnavailableGamesVisible(gameListProxyModel->getUnavailableGamesVisible()); - dlg.setPasswordProtectedGamesVisible(gameListProxyModel->getPasswordProtectedGamesVisible()); - dlg.setGameNameFilter(gameListProxyModel->getGameNameFilter()); - dlg.setCreatorNameFilter(gameListProxyModel->getCreatorNameFilter()); - dlg.setGameTypeFilter(gameListProxyModel->getGameTypeFilter()); - dlg.setMaxPlayersFilter(gameListProxyModel->getMaxPlayersFilterMin(), gameListProxyModel->getMaxPlayersFilterMax()); if (!dlg.exec()) return; From 1217689ed63f5721f5ab6310f0f31cb933790a1a Mon Sep 17 00:00:00 2001 From: arxanas Date: Mon, 7 Jul 2014 19:04:01 -0400 Subject: [PATCH 2/2] Hash the game type. --- cockatrice/src/dlg_filter_games.cpp | 9 +++++++-- cockatrice/src/dlg_filter_games.h | 6 ++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/cockatrice/src/dlg_filter_games.cpp b/cockatrice/src/dlg_filter_games.cpp index b4ce11c8..eb2e8f78 100644 --- a/cockatrice/src/dlg_filter_games.cpp +++ b/cockatrice/src/dlg_filter_games.cpp @@ -10,6 +10,7 @@ #include #include #include +#include DlgFilterGames::DlgFilterGames(const QMap &_allGameTypes, QWidget *parent) : QDialog(parent), @@ -50,7 +51,7 @@ DlgFilterGames::DlgFilterGames(const QMap &_allGameTypes, QWidget QCheckBox *temp = new QCheckBox(gameTypesIterator.value()); temp->setChecked( settings.value( - "game_type/" + gameTypesIterator.value(), + "game_type/" + hashGameType(gameTypesIterator.value()), false ).toBool() ); @@ -145,7 +146,7 @@ void DlgFilterGames::actOk() { checkboxIterator.next(); settings.setValue( - "game_type/" + gameTypeIterator.value(), + "game_type/" + hashGameType(gameTypeIterator.value()), checkboxIterator.value()->isChecked() ); } @@ -156,6 +157,10 @@ void DlgFilterGames::actOk() { accept(); } +QString DlgFilterGames::hashGameType(const QString &gameType) const { + return QCryptographicHash::hash(gameType.toUtf8(), QCryptographicHash::Md5).toHex(); +} + bool DlgFilterGames::getUnavailableGamesVisible() const { return unavailableGamesVisibleCheckBox->isChecked(); diff --git a/cockatrice/src/dlg_filter_games.h b/cockatrice/src/dlg_filter_games.h index ac727d19..2480e642 100644 --- a/cockatrice/src/dlg_filter_games.h +++ b/cockatrice/src/dlg_filter_games.h @@ -21,6 +21,12 @@ private: QSpinBox *maxPlayersFilterMaxSpinBox; const QMap &allGameTypes; + + /* + * The game type might contain special characters, so to use it in + * QSettings we just hash it. + */ + QString hashGameType(const QString &gameType) const; private slots: void actOk(); public: