#include #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)); break; case ItemId_Event_ChatJoinChannel: processJoinChannelEvent(qobject_cast(event)); break; case ItemId_Event_ChatLeaveChannel: processLeaveChannelEvent(qobject_cast(event)); break; case ItemId_Event_ChatSay: processSayEvent(qobject_cast(event)); break; default: ; } } void TabChatChannel::processListPlayersEvent(Event_ChatListPlayers *event) { const QList &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("%1getMessage())); else textEdit->append(QString("%1: %2").arg(event->getPlayerName()).arg(event->getMessage())); QApplication::alert(this); }