servatrice/cockatrice/src/tab_chatchannel.cpp
Max-Wilhelm Bruker 64aa68cd26 Chat works!
2009-11-13 18:27:06 +01:00

78 lines
2.4 KiB
C++

#include <QtGui>
#include "tab_chatchannel.h"
#include "client.h"
#include "protocol_items.h"
TabChatChannel::TabChatChannel(Client *_client, const QString &_channelName)
: QWidget(), client(_client), channelName(_channelName)
{
playerList = new QListWidget;
playerList->setFixedWidth(150);
textEdit = new QTextEdit;
textEdit->setReadOnly(true);
sayEdit = new QLineEdit;
connect(sayEdit, SIGNAL(returnPressed()), this, SLOT(sendMessage()));
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(textEdit);
vbox->addWidget(sayEdit);
QHBoxLayout *hbox = new QHBoxLayout;
hbox->addLayout(vbox);
hbox->addWidget(playerList);
setLayout(hbox);
}
void TabChatChannel::sendMessage()
{
if (sayEdit->text().isEmpty())
return;
client->sendCommand(new Command_ChatSay(channelName, sayEdit->text()));
sayEdit->clear();
}
void TabChatChannel::processChatEvent(ChatEvent *event)
{
switch (event->getItemId()) {
case ItemId_Event_ChatListPlayers: processListPlayersEvent(qobject_cast<Event_ChatListPlayers *>(event)); break;
case ItemId_Event_ChatJoinChannel: processJoinChannelEvent(qobject_cast<Event_ChatJoinChannel *>(event)); break;
case ItemId_Event_ChatLeaveChannel: processLeaveChannelEvent(qobject_cast<Event_ChatLeaveChannel *>(event)); break;
case ItemId_Event_ChatSay: processSayEvent(qobject_cast<Event_ChatSay *>(event)); break;
default: ;
}
}
void TabChatChannel::processListPlayersEvent(Event_ChatListPlayers *event)
{
const QList<ServerPlayerInfo> &players = event->getPlayerList();
for (int i = 0; i < players.size(); ++i)
playerList->addItem(players[i].getName());
}
void TabChatChannel::processJoinChannelEvent(Event_ChatJoinChannel *event)
{
textEdit->append(tr("%1 has joined the channel.").arg(event->getPlayerName()));
playerList->addItem(event->getPlayerName());
}
void TabChatChannel::processLeaveChannelEvent(Event_ChatLeaveChannel *event)
{
textEdit->append(tr("%1 has left the channel.").arg(event->getPlayerName()));
for (int i = 0; i < playerList->count(); ++i)
if (playerList->item(i)->text() == event->getPlayerName()) {
delete playerList->takeItem(i);
break;
}
}
void TabChatChannel::processSayEvent(Event_ChatSay *event)
{
if (event->getPlayerName().isEmpty())
textEdit->append(QString("<font color=\"blue\">%1</font").arg(event->getMessage()));
else
textEdit->append(QString("<font color=\"red\">%1:</font> %2").arg(event->getPlayerName()).arg(event->getMessage()));
QApplication::alert(this);
}