WIP: Refactor gamesproxymodel to own the persistence layer.
This commit is contained in:
parent
16bbc5e8c0
commit
6a4384f903
5 changed files with 98 additions and 60 deletions
|
@ -1,4 +1,5 @@
|
|||
#include "dlg_filter_games.h"
|
||||
#include <QDebug>
|
||||
#include <QCheckBox>
|
||||
#include <QPushButton>
|
||||
#include <QLabel>
|
||||
|
@ -12,34 +13,28 @@
|
|||
#include <QSettings>
|
||||
#include <QCryptographicHash>
|
||||
|
||||
DlgFilterGames::DlgFilterGames(const QMap<int, QString> &_allGameTypes, QWidget *parent)
|
||||
DlgFilterGames::DlgFilterGames(const QMap<int, QString> &_allGameTypes, GamesProxyModel *_gamesProxyModel, QWidget *parent)
|
||||
: QDialog(parent),
|
||||
allGameTypes(_allGameTypes)
|
||||
allGameTypes(_allGameTypes),
|
||||
gamesProxyModel(_gamesProxyModel)
|
||||
{
|
||||
QSettings settings;
|
||||
settings.beginGroup("filter_games");
|
||||
|
||||
unavailableGamesVisibleCheckBox = new QCheckBox(tr("Show &unavailable games"));
|
||||
unavailableGamesVisibleCheckBox->setChecked(
|
||||
settings.value("unavailable_games_visible", false).toBool()
|
||||
);
|
||||
qDebug() << "getUnavailableGamesVisible() == " << gamesProxyModel->getUnavailableGamesVisible();
|
||||
unavailableGamesVisibleCheckBox->setChecked(gamesProxyModel->getUnavailableGamesVisible());
|
||||
|
||||
passwordProtectedGamesVisibleCheckBox = new QCheckBox(tr("Show &password protected games"));
|
||||
passwordProtectedGamesVisibleCheckBox->setChecked(
|
||||
settings.value("password_protected_games_visible", false).toBool()
|
||||
);
|
||||
passwordProtectedGamesVisibleCheckBox->setChecked(gamesProxyModel->getPasswordProtectedGamesVisible());
|
||||
|
||||
gameNameFilterEdit = new QLineEdit;
|
||||
gameNameFilterEdit->setText(
|
||||
settings.value("game_name_filter", "").toString()
|
||||
);
|
||||
gameNameFilterEdit->setText(gamesProxyModel->getGameNameFilter());
|
||||
QLabel *gameNameFilterLabel = new QLabel(tr("Game &description:"));
|
||||
gameNameFilterLabel->setBuddy(gameNameFilterEdit);
|
||||
|
||||
creatorNameFilterEdit = new QLineEdit;
|
||||
creatorNameFilterEdit->setText(
|
||||
settings.value("creator_name_filter", "").toString()
|
||||
);
|
||||
creatorNameFilterEdit->setText(gamesProxyModel->getCreatorNameFilter());
|
||||
QLabel *creatorNameFilterLabel = new QLabel(tr("&Creator name:"));
|
||||
creatorNameFilterLabel->setBuddy(creatorNameFilterEdit);
|
||||
|
||||
|
@ -49,12 +44,7 @@ DlgFilterGames::DlgFilterGames(const QMap<int, QString> &_allGameTypes, QWidget
|
|||
gameTypesIterator.next();
|
||||
|
||||
QCheckBox *temp = new QCheckBox(gameTypesIterator.value());
|
||||
temp->setChecked(
|
||||
settings.value(
|
||||
"game_type/" + hashGameType(gameTypesIterator.value()),
|
||||
false
|
||||
).toBool()
|
||||
);
|
||||
temp->setChecked(gamesProxyModel->getGameTypeFilter().contains(gameTypesIterator.key()));
|
||||
|
||||
gameTypeFilterCheckBoxes.insert(gameTypesIterator.key(), temp);
|
||||
gameTypeFilterLayout->addWidget(temp);
|
||||
|
@ -70,18 +60,14 @@ DlgFilterGames::DlgFilterGames(const QMap<int, QString> &_allGameTypes, QWidget
|
|||
maxPlayersFilterMinSpinBox = new QSpinBox;
|
||||
maxPlayersFilterMinSpinBox->setMinimum(1);
|
||||
maxPlayersFilterMinSpinBox->setMaximum(99);
|
||||
maxPlayersFilterMinSpinBox->setValue(
|
||||
settings.value("min_players", 1).toInt()
|
||||
);
|
||||
maxPlayersFilterMinSpinBox->setValue(gamesProxyModel->getMaxPlayersFilterMin());
|
||||
maxPlayersFilterMinLabel->setBuddy(maxPlayersFilterMinSpinBox);
|
||||
|
||||
QLabel *maxPlayersFilterMaxLabel = new QLabel(tr("at &most:"));
|
||||
maxPlayersFilterMaxSpinBox = new QSpinBox;
|
||||
maxPlayersFilterMaxSpinBox->setMinimum(1);
|
||||
maxPlayersFilterMaxSpinBox->setMaximum(99);
|
||||
maxPlayersFilterMaxSpinBox->setValue(
|
||||
settings.value("max_players", 99).toInt()
|
||||
);
|
||||
maxPlayersFilterMaxSpinBox->setValue(gamesProxyModel->getMaxPlayersFilterMax());
|
||||
maxPlayersFilterMaxLabel->setBuddy(maxPlayersFilterMaxSpinBox);
|
||||
|
||||
QGridLayout *maxPlayersFilterLayout = new QGridLayout;
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#include <QDialog>
|
||||
#include <QSet>
|
||||
#include <QMap>
|
||||
#include "gamesmodel.h"
|
||||
|
||||
class QCheckBox;
|
||||
class QLineEdit;
|
||||
|
@ -21,6 +22,8 @@ private:
|
|||
QSpinBox *maxPlayersFilterMaxSpinBox;
|
||||
|
||||
const QMap<int, QString> &allGameTypes;
|
||||
// This needs a const someplace
|
||||
GamesProxyModel *gamesProxyModel;
|
||||
|
||||
/*
|
||||
* The game type might contain special characters, so to use it in
|
||||
|
@ -30,7 +33,7 @@ private:
|
|||
private slots:
|
||||
void actOk();
|
||||
public:
|
||||
DlgFilterGames(const QMap<int, QString> &allGameTypes, QWidget *parent = 0);
|
||||
DlgFilterGames(const QMap<int, QString> &_allGameTypes, GamesProxyModel *_gamesProxyModel, QWidget *parent = 0);
|
||||
|
||||
bool getUnavailableGamesVisible() const;
|
||||
void setUnavailableGamesVisible(bool _unavailableGamesVisible);
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
#include <QDebug>
|
||||
#include <QTreeView>
|
||||
#include <QCheckBox>
|
||||
#include <QPushButton>
|
||||
|
@ -32,8 +33,17 @@ GameSelector::GameSelector(AbstractClient *_client, const TabSupervisor *_tabSup
|
|||
gameListView->setRootIsDecorated(true);
|
||||
if (_room)
|
||||
gameListView->header()->hideSection(gameListModel->roomColIndex());
|
||||
else
|
||||
gameListProxyModel->setUnavailableGamesVisible(true);
|
||||
|
||||
GameTypeMap gameTypeMap;
|
||||
if (room)
|
||||
gameTypeMap = gameListModel->getGameTypes().value(room->getRoomId());
|
||||
|
||||
gameListProxyModel->loadFilterParameters(gameTypeMap);
|
||||
|
||||
qDebug() << "Check unavailable" << gameListProxyModel->getUnavailableGamesVisible();
|
||||
|
||||
// set the reset filter button enabled
|
||||
|
||||
#if QT_VERSION < 0x050000
|
||||
gameListView->header()->setResizeMode(0, QHeaderView::ResizeToContents);
|
||||
#else
|
||||
|
@ -85,7 +95,8 @@ void GameSelector::actSetFilter()
|
|||
GameTypeMap gameTypeMap;
|
||||
if (room)
|
||||
gameTypeMap = gameListModel->getGameTypes().value(room->getRoomId());
|
||||
DlgFilterGames dlg(gameTypeMap, this);
|
||||
qDebug() << "Check unavailable" << gameListProxyModel->getUnavailableGamesVisible();
|
||||
DlgFilterGames dlg(gameTypeMap, gameListProxyModel, this);
|
||||
|
||||
if (!dlg.exec())
|
||||
return;
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
#include <QDebug>
|
||||
#include <QStringList>
|
||||
#include <QDateTime>
|
||||
#include <QSettings>
|
||||
#include <QCryptographicHash>
|
||||
|
||||
namespace {
|
||||
const unsigned SECS_PER_MIN = 60;
|
||||
|
@ -221,8 +223,32 @@ void GamesProxyModel::resetFilterParameters()
|
|||
gameNameFilter = QString();
|
||||
creatorNameFilter = QString();
|
||||
gameTypeFilter.clear();
|
||||
maxPlayersFilterMin = -1;
|
||||
maxPlayersFilterMax = -1;
|
||||
maxPlayersFilterMin = 1;
|
||||
maxPlayersFilterMax = 99;
|
||||
|
||||
invalidateFilter();
|
||||
}
|
||||
|
||||
void GamesProxyModel::loadFilterParameters(const QMap<int, QString> &allGameTypes)
|
||||
{
|
||||
QSettings settings;
|
||||
settings.beginGroup("filter_games");
|
||||
|
||||
unavailableGamesVisible = settings.value("unavailable_games_visible", false).toBool();
|
||||
qDebug() << "Load unavailable = " << unavailableGamesVisible;
|
||||
passwordProtectedGamesVisible = settings.value("password_protected_games_visible", false).toBool();
|
||||
gameNameFilter = settings.value("game_name_filter", "").toString();
|
||||
creatorNameFilter = settings.value("creator_name_filter", "").toString();
|
||||
maxPlayersFilterMin = settings.value("min_players", 1).toInt();
|
||||
maxPlayersFilterMax = settings.value("max_players", 99).toInt();
|
||||
|
||||
QMapIterator<int, QString> gameTypesIterator(allGameTypes);
|
||||
while (gameTypesIterator.hasNext()) {
|
||||
gameTypesIterator.next();
|
||||
if (settings.value("game_type/" + hashGameType(gameTypesIterator.value()), false).toBool()) {
|
||||
gameTypeFilter.insert(gameTypesIterator.key());
|
||||
}
|
||||
}
|
||||
|
||||
invalidateFilter();
|
||||
}
|
||||
|
@ -265,3 +291,7 @@ bool GamesProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &/*sourc
|
|||
|
||||
return true;
|
||||
}
|
||||
|
||||
QString GamesProxyModel::hashGameType(const QString &gameType) const {
|
||||
return QCryptographicHash::hash(gameType.toUtf8(), QCryptographicHash::Md5).toHex();
|
||||
}
|
||||
|
|
|
@ -49,6 +49,12 @@ private:
|
|||
QString gameNameFilter, creatorNameFilter;
|
||||
QSet<int> gameTypeFilter;
|
||||
int maxPlayersFilterMin, maxPlayersFilterMax;
|
||||
|
||||
/*
|
||||
* The game type might contain special characters, so to use it in
|
||||
* QSettings we just hash it.
|
||||
*/
|
||||
QString hashGameType(const QString &gameType) const;
|
||||
public:
|
||||
GamesProxyModel(QObject *parent = 0, ServerInfo_User *_ownUser = 0);
|
||||
|
||||
|
@ -66,6 +72,8 @@ public:
|
|||
int getMaxPlayersFilterMax() const { return maxPlayersFilterMax; }
|
||||
void setMaxPlayersFilter(int _maxPlayersFilterMin, int _maxPlayersFilterMax);
|
||||
void resetFilterParameters();
|
||||
void loadFilterParameters(const QMap<int, QString> &allGameTypes);
|
||||
void saveFilterParameters();
|
||||
protected:
|
||||
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue