|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the examples of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include <QtGui> |
|
43 |
|
44 #include "chatdialog.h" |
|
45 |
|
46 ChatDialog::ChatDialog(QWidget *parent) |
|
47 : QDialog(parent) |
|
48 { |
|
49 setupUi(this); |
|
50 |
|
51 lineEdit->setFocusPolicy(Qt::StrongFocus); |
|
52 textEdit->setFocusPolicy(Qt::NoFocus); |
|
53 textEdit->setReadOnly(true); |
|
54 listWidget->setFocusPolicy(Qt::NoFocus); |
|
55 |
|
56 connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed())); |
|
57 #ifdef Q_OS_SYMBIAN |
|
58 connect(sendButton, SIGNAL(clicked()), this, SLOT(returnPressed())); |
|
59 #endif |
|
60 connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed())); |
|
61 connect(&client, SIGNAL(newMessage(QString,QString)), |
|
62 this, SLOT(appendMessage(QString,QString))); |
|
63 connect(&client, SIGNAL(newParticipant(QString)), |
|
64 this, SLOT(newParticipant(QString))); |
|
65 connect(&client, SIGNAL(participantLeft(QString)), |
|
66 this, SLOT(participantLeft(QString))); |
|
67 |
|
68 myNickName = client.nickName(); |
|
69 newParticipant(myNickName); |
|
70 tableFormat.setBorder(0); |
|
71 QTimer::singleShot(10 * 1000, this, SLOT(showInformation())); |
|
72 } |
|
73 |
|
74 void ChatDialog::appendMessage(const QString &from, const QString &message) |
|
75 { |
|
76 if (from.isEmpty() || message.isEmpty()) |
|
77 return; |
|
78 |
|
79 QTextCursor cursor(textEdit->textCursor()); |
|
80 cursor.movePosition(QTextCursor::End); |
|
81 QTextTable *table = cursor.insertTable(1, 2, tableFormat); |
|
82 table->cellAt(0, 0).firstCursorPosition().insertText('<' + from + "> "); |
|
83 table->cellAt(0, 1).firstCursorPosition().insertText(message); |
|
84 QScrollBar *bar = textEdit->verticalScrollBar(); |
|
85 bar->setValue(bar->maximum()); |
|
86 } |
|
87 |
|
88 void ChatDialog::returnPressed() |
|
89 { |
|
90 QString text = lineEdit->text(); |
|
91 if (text.isEmpty()) |
|
92 return; |
|
93 |
|
94 if (text.startsWith(QChar('/'))) { |
|
95 QColor color = textEdit->textColor(); |
|
96 textEdit->setTextColor(Qt::red); |
|
97 textEdit->append(tr("! Unknown command: %1") |
|
98 .arg(text.left(text.indexOf(' ')))); |
|
99 textEdit->setTextColor(color); |
|
100 } else { |
|
101 client.sendMessage(text); |
|
102 appendMessage(myNickName, text); |
|
103 } |
|
104 |
|
105 lineEdit->clear(); |
|
106 } |
|
107 |
|
108 void ChatDialog::newParticipant(const QString &nick) |
|
109 { |
|
110 if (nick.isEmpty()) |
|
111 return; |
|
112 |
|
113 QColor color = textEdit->textColor(); |
|
114 textEdit->setTextColor(Qt::gray); |
|
115 textEdit->append(tr("* %1 has joined").arg(nick)); |
|
116 textEdit->setTextColor(color); |
|
117 listWidget->addItem(nick); |
|
118 } |
|
119 |
|
120 void ChatDialog::participantLeft(const QString &nick) |
|
121 { |
|
122 if (nick.isEmpty()) |
|
123 return; |
|
124 |
|
125 QList<QListWidgetItem *> items = listWidget->findItems(nick, |
|
126 Qt::MatchExactly); |
|
127 if (items.isEmpty()) |
|
128 return; |
|
129 |
|
130 delete items.at(0); |
|
131 QColor color = textEdit->textColor(); |
|
132 textEdit->setTextColor(Qt::gray); |
|
133 textEdit->append(tr("* %1 has left").arg(nick)); |
|
134 textEdit->setTextColor(color); |
|
135 } |
|
136 |
|
137 void ChatDialog::showInformation() |
|
138 { |
|
139 if (listWidget->count() == 1) { |
|
140 QMessageBox::information(this, tr("Chat"), |
|
141 tr("Launch several instances of this " |
|
142 "program on your local network and " |
|
143 "start chatting!")); |
|
144 } |
|
145 } |