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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
package ChatDialog;
use strict;
use warnings;
use QtCore4;
use QtGui4;
use Ui_ChatDialog;
use Client;
use QtCore4::isa qw( Qt::Dialog );
use QtCore4::slots
appendMessage => ['const QString &', 'const QString &'],
returnPressed => [],
newParticipant => ['const QString &'],
participantLeft => ['const QString &'],
showInformation => [];
sub client() {
return this->{client};
}
sub myNickName() {
return this->{myNickName};
}
sub tableFormat() {
return this->{tableFormat};
}
sub ui() {
return this->{ui};
}
sub NEW
{
my ($class, $parent) = @_;
$class->SUPER::NEW($parent);
this->{client} = Client();
this->{tableFormat} = Qt::TextTableFormat();
this->{ui} = Ui_ChatDialog->setupUi(this);
ui->lineEdit->setFocusPolicy(Qt::StrongFocus());
ui->textEdit->setFocusPolicy(Qt::NoFocus());
ui->textEdit->setReadOnly(1);
ui->listWidget->setFocusPolicy(Qt::NoFocus());
this->connect(ui->lineEdit, SIGNAL 'returnPressed()', this, SLOT 'returnPressed()');
this->connect(client, SIGNAL 'newMessage(QString,QString)',
this, SLOT 'appendMessage(QString,QString)');
this->connect(client, SIGNAL 'newParticipant(QString)',
this, SLOT 'newParticipant(QString)');
this->connect(client, SIGNAL 'participantLeft(QString)',
this, SLOT 'participantLeft(QString)');
this->{myNickName} = client->nickName();
newParticipant(myNickName);
tableFormat->setBorder(0);
Qt::Timer::singleShot(10 * 1000, this, SLOT 'showInformation()');
}
sub appendMessage
{
my ($from, $message) = @_;
if (!defined $from || !defined $message) {
return;
}
my $cursor = Qt::TextCursor(ui->textEdit->textCursor());
$cursor->movePosition(Qt::TextCursor::End());
my $table = $cursor->insertTable(1, 2, tableFormat);
$table->cellAt(0, 0)->firstCursorPosition()->insertText('<' . $from . '> ');
$table->cellAt(0, 1)->firstCursorPosition()->insertText($message);
my $bar = ui->textEdit->verticalScrollBar();
$bar->setValue($bar->maximum());
}
sub returnPressed
{
my $text = ui->lineEdit->text();
if (!defined $text) {
return;
}
if ($text =~ m#^/#) {
my $color = ui->textEdit->textColor();
ui->textEdit->setTextColor(Qt::Color(Qt::red()));
$text =~ s/ .*//g;
ui->textEdit->append(this->tr('! Unknown command: ') . $text);
ui->textEdit->setTextColor($color);
} else {
client->sendMessage($text);
appendMessage(myNickName, $text);
}
ui->lineEdit->clear();
}
sub newParticipant
{
my ($nick) = @_;
if (!defined $nick) {
return;
}
my $color = ui->textEdit->textColor();
ui->textEdit->setTextColor(Qt::Color(Qt::gray()));
ui->textEdit->append( '* ' . $nick . this->tr('has joined'));
ui->textEdit->setTextColor($color);
ui->listWidget->addItem($nick);
}
sub participantLeft
{
my ($nick) = @_;
if (!defined $nick) {
return;
}
my $items = ui->listWidget->findItems($nick, Qt::MatchExactly());
if (!defined $items) {
return;
}
ui->listWidget->removeItemWidget( $items->[0] );
my $color = ui->textEdit->textColor();
ui->textEdit->setTextColor(Qt::Color(Qt::gray()));
ui->textEdit->append( '* ' . $nick . this->tr('has left'));
ui->textEdit->setTextColor($color);
}
sub showInformation
{
if (ui->listWidget->count() == 1) {
Qt::MessageBox::information(this, this->tr('Chat'),
this->tr('Launch several instances of this ' .
'program on your local network and ' .
'start chatting!'));
}
}
1;
|