WIP: Refactor gamesproxymodel to own the persistence layer.

This commit is contained in:
Jeffrey Oliver 2015-01-02 15:05:33 -08:00
parent 16bbc5e8c0
commit 6a4384f903
5 changed files with 98 additions and 60 deletions

View file

@ -1,4 +1,5 @@
#include "dlg_filter_games.h"
#include <QDebug>
#include <QCheckBox>
#include <QPushButton>
#include <QLabel>
@ -12,49 +13,38 @@
#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);
QVBoxLayout *gameTypeFilterLayout = new QVBoxLayout;
QMapIterator<int, QString> gameTypesIterator(allGameTypes);
while (gameTypesIterator.hasNext()) {
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);
@ -65,34 +55,30 @@ DlgFilterGames::DlgFilterGames(const QMap<int, QString> &_allGameTypes, QWidget
gameTypeFilterGroupBox->setLayout(gameTypeFilterLayout);
} else
gameTypeFilterGroupBox = 0;
QLabel *maxPlayersFilterMinLabel = new QLabel(tr("at &least:"));
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;
maxPlayersFilterLayout->addWidget(maxPlayersFilterMinLabel, 0, 0);
maxPlayersFilterLayout->addWidget(maxPlayersFilterMinSpinBox, 0, 1);
maxPlayersFilterLayout->addWidget(maxPlayersFilterMaxLabel, 1, 0);
maxPlayersFilterLayout->addWidget(maxPlayersFilterMaxSpinBox, 1, 1);
QGroupBox *maxPlayersGroupBox = new QGroupBox(tr("Maximum player count"));
maxPlayersGroupBox->setLayout(maxPlayersFilterLayout);
QGridLayout *leftGrid = new QGridLayout;
leftGrid->addWidget(gameNameFilterLabel, 0, 0);
leftGrid->addWidget(gameNameFilterEdit, 0, 1);
@ -101,26 +87,26 @@ DlgFilterGames::DlgFilterGames(const QMap<int, QString> &_allGameTypes, QWidget
leftGrid->addWidget(maxPlayersGroupBox, 2, 0, 1, 2);
leftGrid->addWidget(unavailableGamesVisibleCheckBox, 3, 0, 1, 2);
leftGrid->addWidget(passwordProtectedGamesVisibleCheckBox, 4, 0, 1, 2);
QVBoxLayout *leftColumn = new QVBoxLayout;
leftColumn->addLayout(leftGrid);
leftColumn->addStretch();
QVBoxLayout *rightColumn = new QVBoxLayout;
rightColumn->addWidget(gameTypeFilterGroupBox);
QHBoxLayout *hbox = new QHBoxLayout;
hbox->addLayout(leftColumn);
hbox->addLayout(rightColumn);
QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
connect(buttonBox, SIGNAL(accepted()), this, SLOT(actOk()));
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(hbox);
mainLayout->addWidget(buttonBox);
setLayout(mainLayout);
setWindowTitle(tr("Filter games"));
}

View file

@ -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,8 +33,8 @@ 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);
bool getPasswordProtectedGamesVisible() const;

View file

@ -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
@ -82,16 +92,17 @@ GameSelector::GameSelector(AbstractClient *_client, const TabSupervisor *_tabSup
void GameSelector::actSetFilter()
{
GameTypeMap gameTypeMap;
if (room)
gameTypeMap = gameListModel->getGameTypes().value(room->getRoomId());
DlgFilterGames dlg(gameTypeMap, this);
GameTypeMap gameTypeMap;
if (room)
gameTypeMap = gameListModel->getGameTypes().value(room->getRoomId());
qDebug() << "Check unavailable" << gameListProxyModel->getUnavailableGamesVisible();
DlgFilterGames dlg(gameTypeMap, gameListProxyModel, this);
if (!dlg.exec())
return;
clearFilterButton->setEnabled(true);
gameListProxyModel->setUnavailableGamesVisible(dlg.getUnavailableGamesVisible());
gameListProxyModel->setPasswordProtectedGamesVisible(dlg.getPasswordProtectedGamesVisible());
gameListProxyModel->setGameNameFilter(dlg.getGameNameFilter());
@ -103,7 +114,7 @@ void GameSelector::actSetFilter()
void GameSelector::actClearFilter()
{
clearFilterButton->setEnabled(false);
gameListProxyModel->resetFilterParameters();
}
@ -136,7 +147,7 @@ void GameSelector::checkResponse(const Response &response)
void GameSelector::actJoin()
{
bool spectator = sender() == spectateButton;
QModelIndex ind = gameListView->currentIndex();
if (!ind.isValid())
return;
@ -149,19 +160,19 @@ void GameSelector::actJoin()
if (!ok)
return;
}
Command_JoinGame cmd;
cmd.set_game_id(game.game_id());
cmd.set_password(password.toStdString());
cmd.set_spectator(spectator);
cmd.set_override_restrictions(overrideRestrictions);
TabRoom *r = tabSupervisor->getRoomTabs().value(game.room_id());
if (!r) {
QMessageBox::critical(this, tr("Error"), tr("Please join the respective room first."));
return;
}
PendingCommand *pend = r->prepareRoomCommand(cmd);
connect(pend, SIGNAL(finished(Response, CommandContainer, QVariant)), this, SLOT(checkResponse(Response)));
r->sendRoomCommand(pend);

View file

@ -3,6 +3,8 @@
#include <QDebug>
#include <QStringList>
#include <QDateTime>
#include <QSettings>
#include <QCryptographicHash>
namespace {
const unsigned SECS_PER_MIN = 60;
@ -10,7 +12,7 @@ namespace {
/**
* Pretty print an integer number of seconds ago. Accurate to only one unit,
* rounded; <5 minutes and >5 hours are displayed as such. As a special case,
* rounded; <5 minutes and >5 hours are displayed as such. As a special case,
* time between 60 and 90 minutes will display both the hours and minutes.
*
* For example...
@ -85,7 +87,7 @@ QVariant GamesModel::data(const QModelIndex &index, int role) const
QDateTime then;
then.setTime_t(g.start_time());
unsigned int secs = then.secsTo(QDateTime::currentDateTime());
switch (role) {
case Qt::DisplayRole: return prettyPrintSecsAgo(secs);
case SORT_ROLE: return QVariant(secs);
@ -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();
}

View file

@ -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;
};