author | unc0rr |
Thu, 27 Nov 2008 15:01:12 +0000 | |
changeset 1516 | bb9fa5809c49 |
parent 1457 | 44cc464de8f3 |
child 1520 | f72f538eba05 |
permissions | -rw-r--r-- |
480 | 1 |
/* |
1066 | 2 |
* Hedgewars, a free turn based strategy game |
486 | 3 |
* Copyright (c) 2007 Igor Ulyanov <iulyanov@gmail.com> |
480 | 4 |
* |
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
17 |
*/ |
|
18 |
||
461 | 19 |
#include <QListWidget> |
20 |
#include <QLineEdit> |
|
1391 | 21 |
#include <QAction> |
461 | 22 |
|
23 |
#include "chatwidget.h" |
|
24 |
||
25 |
HWChatWidget::HWChatWidget(QWidget* parent) : |
|
26 |
QWidget(parent), |
|
27 |
mainLayout(this) |
|
28 |
{ |
|
29 |
mainLayout.setSpacing(1); |
|
30 |
mainLayout.setMargin(1); |
|
462 | 31 |
mainLayout.setSizeConstraint(QLayout::SetMinimumSize); |
464 | 32 |
mainLayout.setColumnStretch(0, 75); |
33 |
mainLayout.setColumnStretch(1, 25); |
|
461 | 34 |
|
35 |
chatEditLine = new QLineEdit(this); |
|
36 |
connect(chatEditLine, SIGNAL(returnPressed()), this, SLOT(returnPressed())); |
|
37 |
||
463 | 38 |
mainLayout.addWidget(chatEditLine, 1, 0, 1, 2); |
462 | 39 |
|
461 | 40 |
chatText = new QListWidget(this); |
41 |
chatText->setMinimumHeight(10); |
|
462 | 42 |
chatText->setMinimumWidth(10); |
463 | 43 |
chatText->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); |
461 | 44 |
mainLayout.addWidget(chatText, 0, 0); |
462 | 45 |
|
46 |
chatNicks = new QListWidget(this); |
|
47 |
chatNicks->setMinimumHeight(10); |
|
48 |
chatNicks->setMinimumWidth(10); |
|
463 | 49 |
chatNicks->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); |
1391 | 50 |
chatNicks->setContextMenuPolicy(Qt::ActionsContextMenu); |
462 | 51 |
mainLayout.addWidget(chatNicks, 0, 1); |
1391 | 52 |
|
53 |
QAction * acBan = new QAction(QAction::tr("Kick"), chatNicks); |
|
54 |
connect(acBan, SIGNAL(triggered(bool)), this, SLOT(onKick())); |
|
55 |
chatNicks->insertAction(0, acBan); |
|
461 | 56 |
} |
57 |
||
58 |
void HWChatWidget::returnPressed() |
|
59 |
{ |
|
60 |
emit chatLine(chatEditLine->text()); |
|
61 |
chatEditLine->clear(); |
|
62 |
} |
|
63 |
||
1360 | 64 |
void HWChatWidget::onChatString(const QString& str) |
461 | 65 |
{ |
1516
bb9fa5809c49
Limit chat history to 250 entries to avoid DoS attack with its use
unc0rr
parents:
1457
diff
changeset
|
66 |
QListWidget* w = chatText; |
bb9fa5809c49
Limit chat history to 250 entries to avoid DoS attack with its use
unc0rr
parents:
1457
diff
changeset
|
67 |
|
bb9fa5809c49
Limit chat history to 250 entries to avoid DoS attack with its use
unc0rr
parents:
1457
diff
changeset
|
68 |
if (w->count() > 250) |
bb9fa5809c49
Limit chat history to 250 entries to avoid DoS attack with its use
unc0rr
parents:
1457
diff
changeset
|
69 |
delete w->item(0); |
bb9fa5809c49
Limit chat history to 250 entries to avoid DoS attack with its use
unc0rr
parents:
1457
diff
changeset
|
70 |
|
bb9fa5809c49
Limit chat history to 250 entries to avoid DoS attack with its use
unc0rr
parents:
1457
diff
changeset
|
71 |
w->addItem(str); |
bb9fa5809c49
Limit chat history to 250 entries to avoid DoS attack with its use
unc0rr
parents:
1457
diff
changeset
|
72 |
w->scrollToBottom(); |
bb9fa5809c49
Limit chat history to 250 entries to avoid DoS attack with its use
unc0rr
parents:
1457
diff
changeset
|
73 |
w->setSelectionMode(QAbstractItemView::NoSelection); |
461 | 74 |
} |
465 | 75 |
|
76 |
void HWChatWidget::nickAdded(const QString& nick) |
|
77 |
{ |
|
1391 | 78 |
QListWidgetItem * item = new QListWidgetItem(nick); |
79 |
chatNicks->addItem(item); |
|
465 | 80 |
} |
81 |
||
82 |
void HWChatWidget::nickRemoved(const QString& nick) |
|
83 |
{ |
|
1391 | 84 |
QList<QListWidgetItem *> items = chatNicks->findItems(nick, Qt::MatchExactly); |
465 | 85 |
for(QList<QListWidgetItem *>::iterator it=items.begin(); it!=items.end();) { |
86 |
chatNicks->takeItem(chatNicks->row(*it)); |
|
87 |
++it; |
|
88 |
} |
|
89 |
} |
|
90 |
||
91 |
void HWChatWidget::clear() |
|
92 |
{ |
|
1311 | 93 |
chatText->clear(); |
94 |
chatNicks->clear(); |
|
465 | 95 |
} |
1391 | 96 |
|
97 |
void HWChatWidget::onKick() |
|
98 |
{ |
|
99 |
QListWidgetItem * curritem = chatNicks->currentItem(); |
|
100 |
if (curritem) |
|
101 |
emit kick(curritem->text()); |
|
102 |
} |
|
1405 | 103 |
|
104 |
void HWChatWidget::setReadyStatus(const QString & nick, bool isReady) |
|
105 |
{ |
|
106 |
QList<QListWidgetItem *> items = chatNicks->findItems(nick, Qt::MatchExactly); |
|
107 |
if (items.size() != 1) |
|
108 |
{ |
|
109 |
qWarning("Bug: cannot find user in chat"); |
|
110 |
return; |
|
111 |
} |
|
112 |
||
113 |
if(isReady) |
|
1457 | 114 |
items[0]->setIcon(QIcon(":/res/lightbulb_on.png")); |
1405 | 115 |
else |
1457 | 116 |
items[0]->setIcon(QIcon(":/res/lightbulb_off.png")); |
1405 | 117 |
} |