summaryrefslogtreecommitdiffstats
path: root/kbattleship/kbattleship/kchatwidget.cpp
blob: 1e9dd005d05fe62729c32e675697770b522ee477 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/***************************************************************************
                              kchatwidget.cpp
                             -----------------
    Developers: (c) 2000-2001 Nikolas Zimmermann <wildfox@kde.org>
                (c) 2000-2001 Daniel Molkentin <molkentin@kde.org>

 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <kapplication.h>
#include "kchatwidget.moc"

KChatWidget::KChatWidget(TQWidget *tqparent, const char *name) : chatDlg(tqparent, name)
{
	connect(sendBtn, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotComputeMessage()));
	connect(commentEdit, TQT_SIGNAL(returnPressed()), this, TQT_SLOT(slotComputeMessage()));
	chatView->setFocusProxy(commentEdit);
	chatView->setMinimumSize(0, 50);
	commentEdit->installEventFilter(this);

	m_currentNickname = TQString();
	slotAcceptMsg(false);
}

void KChatWidget::clear()
{
	m_currentNickname = TQString();
	slotAcceptMsg(false);
	chatView->clear();
	commentEdit->clear();
}

void KChatWidget::slotAcceptMsg(bool value)
{
	m_acceptMsgs = value;
}

void KChatWidget::slotReceivedMessage(const TQString &nickname, const TQString &msg, bool fromenemy)
{
	// Niko Z:
	// IRC roxxx :)
	if(msg.startsWith("/me "))
		chatView->append(TQString(" * ") + nickname + TQString(" ") + msg.mid(4));
	else if(msg.startsWith("/nick "))
		if(fromenemy)
			emit sigChangeEnemyNickname(msg.mid(6));
		else
			emit sigChangeOwnNickname(msg.mid(6));
	else
		chatView->append(nickname + TQString(": ") + msg);
	chatView->setCursorPosition(chatView->numLines(), 0);
}

bool KChatWidget::eventFilter(TQObject *obj, TQEvent *e)
{
	if(TQT_BASE_OBJECT(obj) == TQT_BASE_OBJECT(commentEdit) && e->type() == TQEvent::Wheel)
	{
		kapp->notify(TQT_TQOBJECT(chatView), e);
		return true;
	}
	return chatDlg::eventFilter(obj, e);
}

void KChatWidget::slotComputeMessage()
{
	if(!commentEdit->text().stripWhiteSpace().isEmpty() && m_acceptMsgs)
	{
		slotReceivedMessage(m_currentNickname, commentEdit->text(), false);
		emit sigSendMessage(commentEdit->text());
		commentEdit->setText("");
	}
	else if(commentEdit->text().stripWhiteSpace().isEmpty() && m_acceptMsgs)
		commentEdit->setText("");
	commentEdit->setFocus();
}