Support right-click on game list menu (#5522)

This commit is contained in:
Zach H 2025-01-25 09:03:29 -05:00 committed by GitHub
parent f6c31bf901
commit 37a0c00b3f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 57 additions and 5 deletions

View file

@ -38,6 +38,9 @@ GameSelector::GameSelector(AbstractClient *_client,
: QGroupBox(parent), client(_client), tabSupervisor(_tabSupervisor), room(_room), showFilters(_showfilters)
{
gameListView = new QTreeView;
gameListView->setContextMenuPolicy(Qt::CustomContextMenu);
connect(gameListView, &QTreeView::customContextMenuRequested, this, &GameSelector::customContextMenu);
gameListModel = new GamesModel(_rooms, _gameTypes, this);
if (showFilters) {
gameListProxyModel = new GamesProxyModel(this, tabSupervisor->getUserListManager());
@ -113,7 +116,7 @@ GameSelector::GameSelector(AbstractClient *_client,
setMinimumHeight(200);
connect(joinButton, &QPushButton::clicked, this, &GameSelector::actJoin);
connect(spectateButton, &QPushButton::clicked, this, &GameSelector::actJoin);
connect(spectateButton, &QPushButton::clicked, this, &GameSelector::actSpectate);
connect(gameListView->selectionModel(), &QItemSelectionModel::currentRowChanged, this,
&GameSelector::actSelectedGameChanged);
connect(gameListView, &QTreeView::activated, this, &GameSelector::actJoin);
@ -241,21 +244,65 @@ void GameSelector::checkResponse(const Response &response)
void GameSelector::actJoin()
{
QModelIndex ind = gameListView->currentIndex();
if (!ind.isValid())
return joinGame(false);
}
void GameSelector::actSpectate()
{
return joinGame(true);
}
void GameSelector::customContextMenu(const QPoint &point)
{
const auto &index = gameListView->indexAt(point);
if (!index.isValid()) {
return;
}
QAction joinGame(tr("Join Game"));
connect(&joinGame, &QAction::triggered, this, &GameSelector::actJoin);
QAction spectateGame(tr("Spectate Game"));
connect(&spectateGame, &QAction::triggered, this, &GameSelector::actSpectate);
QAction getGameInfo(tr("Game Information"));
connect(&getGameInfo, &QAction::triggered, this, [=, this]() {
const ServerInfo_Game &gameInfo = gameListModel->getGame(index.data(Qt::UserRole).toInt());
const QMap<int, QString> &gameTypes = gameListModel->getGameTypes().value(gameInfo.room_id());
DlgCreateGame dlg(gameInfo, gameTypes, this);
dlg.exec();
});
QMenu menu;
menu.addAction(&joinGame);
menu.addAction(&spectateGame);
menu.addAction(&getGameInfo);
menu.exec(gameListView->mapToGlobal(point));
}
void GameSelector::joinGame(const bool isSpectator)
{
QModelIndex ind = gameListView->currentIndex();
if (!ind.isValid()) {
return;
}
const ServerInfo_Game &game = gameListModel->getGame(ind.data(Qt::UserRole).toInt());
if (tabSupervisor->switchToGameTabIfAlreadyExists(game.game_id())) {
return;
}
bool spectator = sender() == spectateButton || game.player_count() == game.max_players();
bool spectator = isSpectator || game.player_count() == game.max_players();
bool overrideRestrictions = !tabSupervisor->getAdminLocked();
QString password;
if (game.with_password() && !(spectator && !game.spectators_need_password()) && !overrideRestrictions) {
bool ok;
password = getTextWithMax(this, tr("Join game"), tr("Password:"), QLineEdit::Password, QString(), &ok);
if (!ok)
if (!ok) {
return;
}
}
Command_JoinGame cmd;

View file

@ -27,7 +27,11 @@ private slots:
void actSetFilter();
void actClearFilter();
void actCreate();
void actJoin();
void actSpectate();
void customContextMenu(const QPoint &point);
void actSelectedGameChanged(const QModelIndex &current, const QModelIndex &previous);
void checkResponse(const Response &response);
@ -53,6 +57,7 @@ private:
void disableButtons();
void enableButtons();
void enableButtonsForIndex(const QModelIndex &current);
void joinGame(const bool isSpectator);
public:
GameSelector(AbstractClient *_client,