mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-10 08:14:47 -07:00
fixed shuffling, more chat widget code
This commit is contained in:
parent
947cd1736c
commit
120193fb0b
8 changed files with 131 additions and 9 deletions
|
|
@ -2,8 +2,8 @@
|
|||
#include "chatwidget.h"
|
||||
#include "client.h"
|
||||
|
||||
ChannelWidget::ChannelWidget(QWidget *parent)
|
||||
: QWidget(parent)
|
||||
ChannelWidget::ChannelWidget(const QString &_name, QWidget *parent)
|
||||
: QWidget(parent), name(_name)
|
||||
{
|
||||
playerList = new QListWidget;
|
||||
|
||||
|
|
@ -15,20 +15,56 @@ ChannelWidget::ChannelWidget(QWidget *parent)
|
|||
vbox->addWidget(sayEdit);
|
||||
|
||||
QHBoxLayout *hbox = new QHBoxLayout;
|
||||
hbox->addLayout(vbox);
|
||||
hbox->addLayout(vbox, 1);
|
||||
hbox->addWidget(playerList);
|
||||
|
||||
setLayout(hbox);
|
||||
}
|
||||
|
||||
void ChannelWidget::joinEvent(const QString &playerName)
|
||||
{
|
||||
textEdit->append(tr("%1 has joined the channel.").arg(playerName));
|
||||
playerList->addItem(playerName);
|
||||
}
|
||||
|
||||
void ChannelWidget::listPlayersEvent(const QString &playerName)
|
||||
{
|
||||
playerList->addItem(playerName);
|
||||
}
|
||||
|
||||
void ChannelWidget::leaveEvent(const QString &playerName)
|
||||
{
|
||||
textEdit->append(tr("%1 has left the channel.").arg(playerName));
|
||||
for (int i = 0; i < playerList->count(); ++i)
|
||||
if (playerList->item(i)->text() == playerName) {
|
||||
delete playerList->takeItem(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void ChannelWidget::sayEvent(const QString &playerName, const QString &s)
|
||||
{
|
||||
textEdit->append(QString("<font color=\"red\">%1:</font> %2").arg(playerName).arg(s));
|
||||
}
|
||||
|
||||
ChatWidget::ChatWidget(Client *_client, QWidget *parent)
|
||||
: QWidget(parent), client(_client)
|
||||
{
|
||||
channelList = new QTreeWidget;
|
||||
|
||||
joinButton = new QPushButton;
|
||||
connect(joinButton, SIGNAL(clicked()), this, SLOT(joinClicked()));
|
||||
QHBoxLayout *buttonLayout = new QHBoxLayout;
|
||||
buttonLayout->addStretch();
|
||||
buttonLayout->addWidget(joinButton);
|
||||
QVBoxLayout *leftLayout = new QVBoxLayout;
|
||||
leftLayout->addWidget(channelList);
|
||||
leftLayout->addLayout(buttonLayout);
|
||||
|
||||
tab = new QTabWidget;
|
||||
|
||||
QHBoxLayout *hbox = new QHBoxLayout;
|
||||
hbox->addWidget(channelList);
|
||||
hbox->addLayout(leftLayout);
|
||||
hbox->addWidget(tab, 1);
|
||||
|
||||
retranslateUi();
|
||||
|
|
@ -37,6 +73,8 @@ ChatWidget::ChatWidget(Client *_client, QWidget *parent)
|
|||
|
||||
void ChatWidget::retranslateUi()
|
||||
{
|
||||
joinButton->setText(tr("Joi&n"));
|
||||
|
||||
QTreeWidgetItem *header = channelList->headerItem();
|
||||
Q_ASSERT(header != 0);
|
||||
header->setText(0, tr("Channel"));
|
||||
|
|
@ -68,18 +106,77 @@ void ChatWidget::chatEvent(const ChatEventData &data)
|
|||
break;
|
||||
}
|
||||
case eventChatJoinChannel: {
|
||||
if (msg.size() != 2)
|
||||
break;
|
||||
ChannelWidget *w = getChannel(msg[0]);
|
||||
if (!w)
|
||||
break;
|
||||
w->joinEvent(msg[1]);
|
||||
break;
|
||||
}
|
||||
case eventChatListPlayers: {
|
||||
if (msg.size() != 2)
|
||||
break;
|
||||
ChannelWidget *w = getChannel(msg[0]);
|
||||
if (!w)
|
||||
break;
|
||||
w->listPlayersEvent(msg[1]);
|
||||
break;
|
||||
}
|
||||
case eventChatLeaveChannel: {
|
||||
if (msg.size() != 2)
|
||||
break;
|
||||
ChannelWidget *w = getChannel(msg[0]);
|
||||
if (!w)
|
||||
break;
|
||||
w->leaveEvent(msg[1]);
|
||||
break;
|
||||
}
|
||||
case eventChatSay: {
|
||||
if (msg.size() != 3)
|
||||
break;
|
||||
ChannelWidget *w = getChannel(msg[0]);
|
||||
if (!w)
|
||||
break;
|
||||
w->sayEvent(msg[1], msg[2]);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ChatWidget::joinClicked()
|
||||
{
|
||||
QTreeWidgetItem *twi = channelList->currentItem();
|
||||
if (!twi)
|
||||
return;
|
||||
QString channelName = twi->text(0);
|
||||
if (getChannel(channelName))
|
||||
return;
|
||||
|
||||
PendingCommand *pc = client->chatJoinChannel(channelName);
|
||||
pc->setExtraData(channelName);
|
||||
connect(pc, SIGNAL(finished(ServerResponse)), this, SLOT(joinFinished(ServerResponse)));
|
||||
}
|
||||
|
||||
void ChatWidget::joinFinished(ServerResponse resp)
|
||||
{
|
||||
if (resp != RespOk)
|
||||
return;
|
||||
|
||||
PendingCommand *pc = qobject_cast<PendingCommand *>(sender());
|
||||
QString channelName = pc->getExtraData();
|
||||
ChannelWidget *cw = new ChannelWidget(channelName);
|
||||
tab->addTab(cw, channelName);
|
||||
}
|
||||
|
||||
ChannelWidget *ChatWidget::getChannel(const QString &name)
|
||||
{
|
||||
for (int i = 0; i < tab->count(); ++i) {
|
||||
ChannelWidget *cw = qobject_cast<ChannelWidget *>(tab->widget(i));
|
||||
if (cw->getName() == name)
|
||||
return cw;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue