kick users out of games

This commit is contained in:
Max-Wilhelm Bruker 2011-03-03 17:05:44 +01:00
parent d6083a85c7
commit 731dfcad5c
13 changed files with 263 additions and 86 deletions

View file

@ -2,10 +2,37 @@
#include "playerlistwidget.h"
#include "protocol_datastructures.h"
#include "pixmapgenerator.h"
#include "abstractclient.h"
#include "tab_game.h"
#include "tab_supervisor.h"
#include "tab_userlists.h"
#include "protocol_items.h"
#include "userlist.h"
#include "userinfobox.h"
#include <QDebug>
#include <QMouseEvent>
#include <QAction>
#include <QMenu>
PlayerListWidget::PlayerListWidget(QWidget *parent)
: QTreeWidget(parent), gameStarted(false)
PlayerListItemDelegate::PlayerListItemDelegate(QObject *const parent)
: QStyledItemDelegate(parent)
{
}
bool PlayerListItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
if ((event->type() == QEvent::MouseButtonPress) && index.isValid()) {
QMouseEvent *const mouseEvent = static_cast<QMouseEvent *>(event);
if (mouseEvent->button() == Qt::RightButton) {
static_cast<PlayerListWidget *>(parent())->showContextMenu(mouseEvent->globalPos(), index);
return true;
}
}
return QStyledItemDelegate::editorEvent(event, model, option, index);
}
PlayerListWidget::PlayerListWidget(TabSupervisor *_tabSupervisor, AbstractClient *_client, TabGame *_game, bool _gameCreator, QWidget *parent)
: QTreeWidget(parent), tabSupervisor(_tabSupervisor), client(_client), game(_game), gameCreator(_gameCreator), gameStarted(false)
{
readyIcon = QIcon(":/resources/icon_ready_start.svg");
notReadyIcon = QIcon(":/resources/icon_not_ready_start.svg");
@ -13,23 +40,20 @@ PlayerListWidget::PlayerListWidget(QWidget *parent)
playerIcon = QIcon(":/resources/icon_player.svg");
spectatorIcon = QIcon(":/resources/icon_spectator.svg");
setMinimumHeight(100);
itemDelegate = new PlayerListItemDelegate(this);
setItemDelegate(itemDelegate);
setMinimumHeight(60);
setIconSize(QSize(20, 15));
setColumnCount(6);
setHeaderHidden(true);
setRootIsDecorated(false);
setSelectionMode(NoSelection);
header()->setResizeMode(QHeaderView::ResizeToContents);
retranslateUi();
}
void PlayerListWidget::retranslateUi()
{
headerItem()->setText(0, QString());
headerItem()->setText(1, QString());
headerItem()->setText(2, QString());
headerItem()->setText(3, QString());
headerItem()->setText(4, tr("Player name"));
headerItem()->setText(5, tr("Deck"));
}
void PlayerListWidget::addPlayer(ServerInfo_PlayerProperties *player)
@ -52,13 +76,15 @@ void PlayerListWidget::updatePlayerProperties(ServerInfo_PlayerProperties *prop)
player->setText(4, prop->getUserInfo()->getName());
if (!prop->getUserInfo()->getCountry().isEmpty())
player->setIcon(4, QIcon(CountryPixmapGenerator::generatePixmap(12, prop->getUserInfo()->getCountry())));
player->setData(4, Qt::UserRole, prop->getUserInfo()->getName());
player->setData(4, Qt::UserRole + 1, prop->getPlayerId());
QString deckText;
if (!prop->getSpectator())
switch (prop->getDeckId()) {
case -2: deckText = tr("---"); break;
case -1: deckText = tr("local"); break;
default: deckText = tr("#%1").arg(prop->getDeckId());
case -2: deckText = QString(); break;
case -1: deckText = tr("local deck"); break;
default: deckText = tr("deck #%1").arg(prop->getDeckId());
}
player->setText(5, deckText);
}
@ -100,3 +126,66 @@ void PlayerListWidget::setGameStarted(bool _gameStarted)
twi->setIcon(2, gameStarted ? QIcon() : notReadyIcon);
}
}
void PlayerListWidget::showContextMenu(const QPoint &pos, const QModelIndex &index)
{
const QString &userName = index.sibling(index.row(), 4).data(Qt::UserRole).toString();
int playerId = index.sibling(index.row(), 4).data(Qt::UserRole + 1).toInt();
QAction *aUserName = new QAction(userName, this);
aUserName->setEnabled(false);
QAction *aDetails = new QAction(tr("User &details"), this);
QAction *aChat = new QAction(tr("Direct &chat"), this);
QAction *aAddToBuddyList = new QAction(tr("Add to &buddy list"), this);
QAction *aRemoveFromBuddyList = new QAction(tr("Remove from &buddy list"), this);
QAction *aAddToIgnoreList = new QAction(tr("Add to &ignore list"), this);
QAction *aRemoveFromIgnoreList = new QAction(tr("Remove from &ignore list"), this);
QAction *aKick = new QAction(tr("Kick from &game"), this);
QMenu *menu = new QMenu(this);
menu->addAction(aUserName);
menu->addSeparator();
menu->addAction(aDetails);
menu->addAction(aChat);
menu->addSeparator();
if (tabSupervisor->getUserListsTab()->getBuddyList()->userInList(userName))
menu->addAction(aRemoveFromBuddyList);
else
menu->addAction(aAddToBuddyList);
if (tabSupervisor->getUserListsTab()->getIgnoreList()->userInList(userName))
menu->addAction(aRemoveFromIgnoreList);
else
menu->addAction(aAddToIgnoreList);
if (gameCreator) {
menu->addSeparator();
menu->addAction(aKick);
}
QAction *actionClicked = menu->exec(pos);
if (actionClicked == aDetails) {
UserInfoBox *infoWidget = new UserInfoBox(client, true, this, Qt::Dialog | Qt::WindowTitleHint | Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint);
infoWidget->setAttribute(Qt::WA_DeleteOnClose);
infoWidget->updateInfo(userName);
} else if (actionClicked == aChat)
emit openMessageDialog(userName, true);
else if (actionClicked == aAddToBuddyList)
client->sendCommand(new Command_AddToList("buddy", userName));
else if (actionClicked == aRemoveFromBuddyList)
client->sendCommand(new Command_RemoveFromList("buddy", userName));
else if (actionClicked == aAddToIgnoreList)
client->sendCommand(new Command_AddToList("ignore", userName));
else if (actionClicked == aRemoveFromIgnoreList)
client->sendCommand(new Command_RemoveFromList("ignore", userName));
else if (actionClicked == aKick)
game->sendGameCommand(new Command_KickFromGame(-1, playerId));
delete menu;
delete aUserName;
delete aDetails;
delete aChat;
delete aAddToBuddyList;
delete aRemoveFromBuddyList;
delete aAddToIgnoreList;
delete aRemoveFromIgnoreList;
delete aKick;
}