filter running games; minor server code cleanup

This commit is contained in:
Max-Wilhelm Bruker 2011-07-03 16:45:52 +02:00
parent 9727a38956
commit 898623f1ba
24 changed files with 369 additions and 289 deletions

View file

@ -27,23 +27,34 @@ GameSelector::GameSelector(AbstractClient *_client, TabRoom *_room, const QMap<i
gameListView->header()->setResizeMode(1, QHeaderView::ResizeToContents);
showFullGamesCheckBox = new QCheckBox;
showRunningGamesCheckBox = new QCheckBox;
QVBoxLayout *filterLayout = new QVBoxLayout;
filterLayout->addWidget(showFullGamesCheckBox);
filterLayout->addWidget(showRunningGamesCheckBox);
if (room)
createButton = new QPushButton;
else
createButton = 0;
joinButton = new QPushButton;
spectateButton = new QPushButton;
QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addWidget(showFullGamesCheckBox);
buttonLayout->addStretch();
if (room)
buttonLayout->addWidget(createButton);
buttonLayout->addWidget(joinButton);
buttonLayout->addWidget(spectateButton);
buttonLayout->setAlignment(Qt::AlignTop);
QHBoxLayout *hbox = new QHBoxLayout;
hbox->addLayout(filterLayout);
hbox->addStretch();
hbox->addLayout(buttonLayout);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(gameListView);
mainLayout->addLayout(buttonLayout);
mainLayout->addLayout(hbox);
retranslateUi();
setLayout(mainLayout);
@ -52,6 +63,7 @@ GameSelector::GameSelector(AbstractClient *_client, TabRoom *_room, const QMap<i
setMinimumHeight(200);
connect(showFullGamesCheckBox, SIGNAL(stateChanged(int)), this, SLOT(showFullGamesChanged(int)));
connect(showRunningGamesCheckBox, SIGNAL(stateChanged(int)), this, SLOT(showRunningGamesChanged(int)));
connect(createButton, SIGNAL(clicked()), this, SLOT(actCreate()));
connect(joinButton, SIGNAL(clicked()), this, SLOT(actJoin()));
connect(spectateButton, SIGNAL(clicked()), this, SLOT(actJoin()));
@ -62,6 +74,11 @@ void GameSelector::showFullGamesChanged(int state)
gameListProxyModel->setFullGamesVisible(state);
}
void GameSelector::showRunningGamesChanged(int state)
{
gameListProxyModel->setRunningGamesVisible(state);
}
void GameSelector::actCreate()
{
DlgCreateGame dlg(client, room->getRoomId(), room->getGameTypes(), this);
@ -118,6 +135,7 @@ void GameSelector::retranslateUi()
{
setTitle(tr("Games"));
showFullGamesCheckBox->setText(tr("Show &full games"));
showRunningGamesCheckBox->setText(tr("Show &running games"));
if (createButton)
createButton->setText(tr("C&reate"));
joinButton->setText(tr("&Join"));

View file

@ -18,6 +18,7 @@ class GameSelector : public QGroupBox {
Q_OBJECT
private slots:
void showFullGamesChanged(int state);
void showRunningGamesChanged(int state);
void actCreate();
void actJoin();
void checkResponse(ResponseCode response);
@ -31,7 +32,7 @@ private:
GamesModel *gameListModel;
GamesProxyModel *gameListProxyModel;
QPushButton *createButton, *joinButton, *spectateButton;
QCheckBox *showFullGamesCheckBox;
QCheckBox *showFullGamesCheckBox, *showRunningGamesCheckBox;
public:
GameSelector(AbstractClient *_client, TabRoom *_room, const QMap<int, QString> &_rooms, const QMap<int, GameTypeMap> &_gameTypes, QWidget *parent = 0);
void retranslateUi();

View file

@ -85,7 +85,7 @@ void GamesModel::updateGameList(ServerInfo_Game *_game)
for (int i = 0; i < oldGameTypeList.size(); ++i)
gameTypeList.append(new GameTypeId(oldGameTypeList[i]->getData()));
ServerInfo_Game *game = new ServerInfo_Game(_game->getRoomId(), _game->getGameId(), _game->getDescription(), _game->getHasPassword(), _game->getPlayerCount(), _game->getMaxPlayers(), gameTypeList, new ServerInfo_User(_game->getCreatorInfo()), _game->getOnlyBuddies(), _game->getOnlyRegistered(), _game->getSpectatorsAllowed(), _game->getSpectatorsNeedPassword(), _game->getSpectatorCount());
ServerInfo_Game *game = new ServerInfo_Game(_game->getRoomId(), _game->getGameId(), _game->getDescription(), _game->getHasPassword(), _game->getPlayerCount(), _game->getMaxPlayers(), _game->getStarted(), gameTypeList, new ServerInfo_User(_game->getCreatorInfo()), _game->getOnlyBuddies(), _game->getOnlyRegistered(), _game->getSpectatorsAllowed(), _game->getSpectatorsNeedPassword(), _game->getSpectatorCount());
for (int i = 0; i < gameList.size(); i++)
if (gameList[i]->getGameId() == game->getGameId()) {
if (game->getPlayerCount() == 0) {
@ -118,17 +118,22 @@ void GamesProxyModel::setFullGamesVisible(bool _fullGamesVisible)
invalidateFilter();
}
void GamesProxyModel::setRunningGamesVisible(bool _runningGamesVisible)
{
runningGamesVisible = _runningGamesVisible;
invalidateFilter();
}
bool GamesProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &/*sourceParent*/) const
{
if (fullGamesVisible)
return true;
GamesModel *model = qobject_cast<GamesModel *>(sourceModel());
if (!model)
return false;
ServerInfo_Game *game = model->getGame(sourceRow);
if (game->getPlayerCount() == game->getMaxPlayers())
if ((game->getPlayerCount() == game->getMaxPlayers()) && !fullGamesVisible)
return false;
if (game->getStarted() && !runningGamesVisible)
return false;
return true;

View file

@ -30,9 +30,11 @@ class GamesProxyModel : public QSortFilterProxyModel {
Q_OBJECT
private:
bool fullGamesVisible;
bool runningGamesVisible;
public:
GamesProxyModel(QObject *parent = 0);
void setFullGamesVisible(bool _fullGamesVisible);
void setRunningGamesVisible(bool _runningGamesVisible);
protected:
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
};