QVariant GamesModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); if (role == Qt::UserRole) return index.row(); if (role != Qt::DisplayRole) return QVariant(); if ((index.row() >= gameList.size()) || (index.column() >= columnCount())) return QVariant(); ServerInfo_Game *g = gameList[index.row()]; switch (index.column()) { case 0: return g->getDescription(); case 1: return g->getCreatorInfo()->getName(); case 2: return g->getHasPassword() ? (g->getSpectatorsNeedPassword() ? tr("yes") : tr("yes, free for spectators")) : tr("no"); case 3: return QString("%1/%2").arg(g->getPlayerCount()).arg(g->getMaxPlayers()); case 4: return g->getSpectatorsAllowed() ? QVariant(g->getSpectatorCount()) : QVariant(tr("not allowed")); default: return QVariant(); } }
void GameSelector::actJoin() { bool spectator = sender() == spectateButton; QModelIndex ind = gameListView->currentIndex(); if (!ind.isValid()) return; ServerInfo_Game *game = gameListModel->getGame(ind.data(Qt::UserRole).toInt()); QString password; if (game->getHasPassword() && !(spectator && !game->getSpectatorsNeedPassword())) { bool ok; password = QInputDialog::getText(this, tr("Join game"), tr("Password:"), QLineEdit::Password, QString(), &ok); if (!ok) return; } Command_JoinGame *commandJoinGame = new Command_JoinGame(roomId, game->getGameId(), password, spectator); connect(commandJoinGame, SIGNAL(finished(ResponseCode)), this, SLOT(checkResponse(ResponseCode))); client->sendCommand(commandJoinGame); createButton->setEnabled(false); joinButton->setEnabled(false); spectateButton->setEnabled(false); }