QTfrontend/chatwidget.cpp
author unc0rr
Fri, 09 Jan 2009 17:27:13 +0000
changeset 1626 58fd5bc49a04
parent 1589 53eb1d3953ef
child 1666 b66e014510e7
permissions -rw-r--r--
Colorize join/quit messages
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
480
09434d6c3865 GPL headers added
displacer
parents: 465
diff changeset
     1
/*
1066
1f1b3686a2b0 Update copyright headers a bit
unc0rr
parents: 575
diff changeset
     2
 * Hedgewars, a free turn based strategy game
486
7ea71cd3acd5 - Change proto version to 4
unc0rr
parents: 480
diff changeset
     3
 * Copyright (c) 2007 Igor Ulyanov <iulyanov@gmail.com>
1577
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
     4
 * Copyright (c) 2009 Andrey Korotaev <unC0Rr@gmail.com>
480
09434d6c3865 GPL headers added
displacer
parents: 465
diff changeset
     5
 *
09434d6c3865 GPL headers added
displacer
parents: 465
diff changeset
     6
 * This program is free software; you can redistribute it and/or modify
09434d6c3865 GPL headers added
displacer
parents: 465
diff changeset
     7
 * it under the terms of the GNU General Public License as published by
09434d6c3865 GPL headers added
displacer
parents: 465
diff changeset
     8
 * the Free Software Foundation; version 2 of the License
09434d6c3865 GPL headers added
displacer
parents: 465
diff changeset
     9
 *
09434d6c3865 GPL headers added
displacer
parents: 465
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
09434d6c3865 GPL headers added
displacer
parents: 465
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
09434d6c3865 GPL headers added
displacer
parents: 465
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
09434d6c3865 GPL headers added
displacer
parents: 465
diff changeset
    13
 * GNU General Public License for more details.
09434d6c3865 GPL headers added
displacer
parents: 465
diff changeset
    14
 *
09434d6c3865 GPL headers added
displacer
parents: 465
diff changeset
    15
 * You should have received a copy of the GNU General Public License
09434d6c3865 GPL headers added
displacer
parents: 465
diff changeset
    16
 * along with this program; if not, write to the Free Software
09434d6c3865 GPL headers added
displacer
parents: 465
diff changeset
    17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
09434d6c3865 GPL headers added
displacer
parents: 465
diff changeset
    18
 */
09434d6c3865 GPL headers added
displacer
parents: 465
diff changeset
    19
1520
f72f538eba05 Refactor chat widget to use QTextBrower instead of QListWidget:
unc0rr
parents: 1516
diff changeset
    20
#include <QTextBrowser>
461
105af110b0e6 chatWidget with layout
displacer
parents:
diff changeset
    21
#include <QListWidget>
105af110b0e6 chatWidget with layout
displacer
parents:
diff changeset
    22
#include <QLineEdit>
1391
735f6d43780b Implement kick
unc0rr
parents: 1360
diff changeset
    23
#include <QAction>
1577
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
    24
#include <QApplication>
1587
b8a3d449bed7 Don't escape chat strings which go to engine
unc0rr
parents: 1584
diff changeset
    25
#include <QTextDocument>
461
105af110b0e6 chatWidget with layout
displacer
parents:
diff changeset
    26
105af110b0e6 chatWidget with layout
displacer
parents:
diff changeset
    27
#include "chatwidget.h"
105af110b0e6 chatWidget with layout
displacer
parents:
diff changeset
    28
105af110b0e6 chatWidget with layout
displacer
parents:
diff changeset
    29
HWChatWidget::HWChatWidget(QWidget* parent) :
105af110b0e6 chatWidget with layout
displacer
parents:
diff changeset
    30
  QWidget(parent),
105af110b0e6 chatWidget with layout
displacer
parents:
diff changeset
    31
  mainLayout(this)
105af110b0e6 chatWidget with layout
displacer
parents:
diff changeset
    32
{
1577
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
    33
	mainLayout.setSpacing(1);
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
    34
	mainLayout.setMargin(1);
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
    35
	mainLayout.setSizeConstraint(QLayout::SetMinimumSize);
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
    36
	mainLayout.setColumnStretch(0, 75);
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
    37
	mainLayout.setColumnStretch(1, 25);
461
105af110b0e6 chatWidget with layout
displacer
parents:
diff changeset
    38
1577
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
    39
	chatEditLine = new QLineEdit(this);
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
    40
	chatEditLine->setMaxLength(300);
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
    41
	connect(chatEditLine, SIGNAL(returnPressed()), this, SLOT(returnPressed()));
461
105af110b0e6 chatWidget with layout
displacer
parents:
diff changeset
    42
1577
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
    43
	mainLayout.addWidget(chatEditLine, 1, 0, 1, 2);
462
91baab07b79a resizing and grid problems
displacer
parents: 461
diff changeset
    44
1577
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
    45
	chatText = new QTextBrowser(this);
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
    46
	chatText->setMinimumHeight(20);
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
    47
	chatText->setMinimumWidth(10);
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
    48
	chatText->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
1589
53eb1d3953ef Correctly handle urls in chat widget
unc0rr
parents: 1588
diff changeset
    49
	chatText->setOpenExternalLinks(true);
1577
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
    50
	mainLayout.addWidget(chatText, 0, 0);
462
91baab07b79a resizing and grid problems
displacer
parents: 461
diff changeset
    51
1577
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
    52
	chatNicks = new QListWidget(this);
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
    53
	chatNicks->setMinimumHeight(10);
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
    54
	chatNicks->setMinimumWidth(10);
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
    55
	chatNicks->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
    56
	chatNicks->setContextMenuPolicy(Qt::ActionsContextMenu);
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
    57
	mainLayout.addWidget(chatNicks, 0, 1);
1391
735f6d43780b Implement kick
unc0rr
parents: 1360
diff changeset
    58
1577
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
    59
	QAction * acBan = new QAction(QAction::tr("Kick"), chatNicks);
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
    60
	connect(acBan, SIGNAL(triggered(bool)), this, SLOT(onKick()));
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
    61
	chatNicks->insertAction(0, acBan);
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
    62
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
    63
	QAction * acInfo = new QAction(QAction::tr("Info"), chatNicks);
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
    64
	connect(acInfo, SIGNAL(triggered(bool)), this, SLOT(onInfo()));
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
    65
	chatNicks->insertAction(0, acInfo);
461
105af110b0e6 chatWidget with layout
displacer
parents:
diff changeset
    66
}
105af110b0e6 chatWidget with layout
displacer
parents:
diff changeset
    67
105af110b0e6 chatWidget with layout
displacer
parents:
diff changeset
    68
void HWChatWidget::returnPressed()
105af110b0e6 chatWidget with layout
displacer
parents:
diff changeset
    69
{
1577
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
    70
	emit chatLine(chatEditLine->text());
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
    71
	chatEditLine->clear();
461
105af110b0e6 chatWidget with layout
displacer
parents:
diff changeset
    72
}
105af110b0e6 chatWidget with layout
displacer
parents:
diff changeset
    73
1360
d3e285281c26 Fix doubling chat message
unc0rr
parents: 1357
diff changeset
    74
void HWChatWidget::onChatString(const QString& str)
461
105af110b0e6 chatWidget with layout
displacer
parents:
diff changeset
    75
{
1520
f72f538eba05 Refactor chat widget to use QTextBrower instead of QListWidget:
unc0rr
parents: 1516
diff changeset
    76
	if (chatStrings.size() > 250)
f72f538eba05 Refactor chat widget to use QTextBrower instead of QListWidget:
unc0rr
parents: 1516
diff changeset
    77
		chatStrings.removeFirst();
1626
58fd5bc49a04 Colorize join/quit messages
unc0rr
parents: 1589
diff changeset
    78
58fd5bc49a04 Colorize join/quit messages
unc0rr
parents: 1589
diff changeset
    79
	QString formattedStr = Qt::escape(str);
58fd5bc49a04 Colorize join/quit messages
unc0rr
parents: 1589
diff changeset
    80
	if (formattedStr.startsWith("***"))
58fd5bc49a04 Colorize join/quit messages
unc0rr
parents: 1589
diff changeset
    81
		formattedStr = QString("<font color=grey>%1</font>").arg(formattedStr);
1516
bb9fa5809c49 Limit chat history to 250 entries to avoid DoS attack with its use
unc0rr
parents: 1457
diff changeset
    82
	
1626
58fd5bc49a04 Colorize join/quit messages
unc0rr
parents: 1589
diff changeset
    83
	chatStrings.append(formattedStr);
1587
b8a3d449bed7 Don't escape chat strings which go to engine
unc0rr
parents: 1584
diff changeset
    84
	
b8a3d449bed7 Don't escape chat strings which go to engine
unc0rr
parents: 1584
diff changeset
    85
	chatText->setHtml(chatStrings.join("<br>"));
b8a3d449bed7 Don't escape chat strings which go to engine
unc0rr
parents: 1584
diff changeset
    86
b8a3d449bed7 Don't escape chat strings which go to engine
unc0rr
parents: 1584
diff changeset
    87
	chatText->moveCursor(QTextCursor::End);
b8a3d449bed7 Don't escape chat strings which go to engine
unc0rr
parents: 1584
diff changeset
    88
}
b8a3d449bed7 Don't escape chat strings which go to engine
unc0rr
parents: 1584
diff changeset
    89
b8a3d449bed7 Don't escape chat strings which go to engine
unc0rr
parents: 1584
diff changeset
    90
void HWChatWidget::onServerMessage(const QString& str)
b8a3d449bed7 Don't escape chat strings which go to engine
unc0rr
parents: 1584
diff changeset
    91
{
b8a3d449bed7 Don't escape chat strings which go to engine
unc0rr
parents: 1584
diff changeset
    92
	if (chatStrings.size() > 250)
b8a3d449bed7 Don't escape chat strings which go to engine
unc0rr
parents: 1584
diff changeset
    93
		chatStrings.removeFirst();
b8a3d449bed7 Don't escape chat strings which go to engine
unc0rr
parents: 1584
diff changeset
    94
	
1588
0988ae7771d3 Place server message between two horizontal lines
unc0rr
parents: 1587
diff changeset
    95
	chatStrings.append("<hr>" + str + "<hr>");
1520
f72f538eba05 Refactor chat widget to use QTextBrower instead of QListWidget:
unc0rr
parents: 1516
diff changeset
    96
	
1584
90f6a5abad17 Save much space for chat widget on lobby page by removing server message widget (now this messages goes to chat)
unc0rr
parents: 1577
diff changeset
    97
	chatText->setHtml(chatStrings.join("<br>"));
1516
bb9fa5809c49 Limit chat history to 250 entries to avoid DoS attack with its use
unc0rr
parents: 1457
diff changeset
    98
1520
f72f538eba05 Refactor chat widget to use QTextBrower instead of QListWidget:
unc0rr
parents: 1516
diff changeset
    99
	chatText->moveCursor(QTextCursor::End);
461
105af110b0e6 chatWidget with layout
displacer
parents:
diff changeset
   100
}
465
07eca0a2546c chatWidget with nicks ready
displacer
parents: 464
diff changeset
   101
07eca0a2546c chatWidget with nicks ready
displacer
parents: 464
diff changeset
   102
void HWChatWidget::nickAdded(const QString& nick)
07eca0a2546c chatWidget with nicks ready
displacer
parents: 464
diff changeset
   103
{
1391
735f6d43780b Implement kick
unc0rr
parents: 1360
diff changeset
   104
	QListWidgetItem * item = new QListWidgetItem(nick);
735f6d43780b Implement kick
unc0rr
parents: 1360
diff changeset
   105
	chatNicks->addItem(item);
465
07eca0a2546c chatWidget with nicks ready
displacer
parents: 464
diff changeset
   106
}
07eca0a2546c chatWidget with nicks ready
displacer
parents: 464
diff changeset
   107
07eca0a2546c chatWidget with nicks ready
displacer
parents: 464
diff changeset
   108
void HWChatWidget::nickRemoved(const QString& nick)
07eca0a2546c chatWidget with nicks ready
displacer
parents: 464
diff changeset
   109
{
1520
f72f538eba05 Refactor chat widget to use QTextBrower instead of QListWidget:
unc0rr
parents: 1516
diff changeset
   110
	QList<QListWidgetItem *> items = chatNicks->findItems(nick, Qt::MatchExactly);
f72f538eba05 Refactor chat widget to use QTextBrower instead of QListWidget:
unc0rr
parents: 1516
diff changeset
   111
	for(QList<QListWidgetItem *>::iterator it=items.begin(); it!=items.end();) {
f72f538eba05 Refactor chat widget to use QTextBrower instead of QListWidget:
unc0rr
parents: 1516
diff changeset
   112
		chatNicks->takeItem(chatNicks->row(*it));
f72f538eba05 Refactor chat widget to use QTextBrower instead of QListWidget:
unc0rr
parents: 1516
diff changeset
   113
		++it;
f72f538eba05 Refactor chat widget to use QTextBrower instead of QListWidget:
unc0rr
parents: 1516
diff changeset
   114
	}
465
07eca0a2546c chatWidget with nicks ready
displacer
parents: 464
diff changeset
   115
}
07eca0a2546c chatWidget with nicks ready
displacer
parents: 464
diff changeset
   116
07eca0a2546c chatWidget with nicks ready
displacer
parents: 464
diff changeset
   117
void HWChatWidget::clear()
07eca0a2546c chatWidget with nicks ready
displacer
parents: 464
diff changeset
   118
{
1311
49beb1221c0b - Add stub page for rooms list
unc0rr
parents: 1066
diff changeset
   119
	chatText->clear();
1520
f72f538eba05 Refactor chat widget to use QTextBrower instead of QListWidget:
unc0rr
parents: 1516
diff changeset
   120
	chatStrings.clear();
1311
49beb1221c0b - Add stub page for rooms list
unc0rr
parents: 1066
diff changeset
   121
	chatNicks->clear();
465
07eca0a2546c chatWidget with nicks ready
displacer
parents: 464
diff changeset
   122
}
1391
735f6d43780b Implement kick
unc0rr
parents: 1360
diff changeset
   123
735f6d43780b Implement kick
unc0rr
parents: 1360
diff changeset
   124
void HWChatWidget::onKick()
735f6d43780b Implement kick
unc0rr
parents: 1360
diff changeset
   125
{
735f6d43780b Implement kick
unc0rr
parents: 1360
diff changeset
   126
	QListWidgetItem * curritem = chatNicks->currentItem();
735f6d43780b Implement kick
unc0rr
parents: 1360
diff changeset
   127
	if (curritem)
735f6d43780b Implement kick
unc0rr
parents: 1360
diff changeset
   128
		emit kick(curritem->text());
735f6d43780b Implement kick
unc0rr
parents: 1360
diff changeset
   129
}
1405
ee4b28a1861d Show players ready status in chat nick list
unc0rr
parents: 1391
diff changeset
   130
1577
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
   131
void HWChatWidget::onInfo()
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
   132
{
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
   133
	QListWidgetItem * curritem = chatNicks->currentItem();
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
   134
	if (curritem)
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
   135
		emit info(curritem->text());
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
   136
}
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1521
diff changeset
   137
1405
ee4b28a1861d Show players ready status in chat nick list
unc0rr
parents: 1391
diff changeset
   138
void HWChatWidget::setReadyStatus(const QString & nick, bool isReady)
ee4b28a1861d Show players ready status in chat nick list
unc0rr
parents: 1391
diff changeset
   139
{
ee4b28a1861d Show players ready status in chat nick list
unc0rr
parents: 1391
diff changeset
   140
	QList<QListWidgetItem *> items = chatNicks->findItems(nick, Qt::MatchExactly);
ee4b28a1861d Show players ready status in chat nick list
unc0rr
parents: 1391
diff changeset
   141
	if (items.size() != 1)
ee4b28a1861d Show players ready status in chat nick list
unc0rr
parents: 1391
diff changeset
   142
	{
ee4b28a1861d Show players ready status in chat nick list
unc0rr
parents: 1391
diff changeset
   143
		qWarning("Bug: cannot find user in chat");
ee4b28a1861d Show players ready status in chat nick list
unc0rr
parents: 1391
diff changeset
   144
		return;
ee4b28a1861d Show players ready status in chat nick list
unc0rr
parents: 1391
diff changeset
   145
	}
ee4b28a1861d Show players ready status in chat nick list
unc0rr
parents: 1391
diff changeset
   146
ee4b28a1861d Show players ready status in chat nick list
unc0rr
parents: 1391
diff changeset
   147
	if(isReady)
1457
44cc464de8f3 Add lightbulbs, some more changes to look
unc0rr
parents: 1405
diff changeset
   148
		items[0]->setIcon(QIcon(":/res/lightbulb_on.png"));
1405
ee4b28a1861d Show players ready status in chat nick list
unc0rr
parents: 1391
diff changeset
   149
	else
1457
44cc464de8f3 Add lightbulbs, some more changes to look
unc0rr
parents: 1405
diff changeset
   150
		items[0]->setIcon(QIcon(":/res/lightbulb_off.png"));
1405
ee4b28a1861d Show players ready status in chat nick list
unc0rr
parents: 1391
diff changeset
   151
}