chatwindow.cpp
1.55 KB
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
#include "chatwindow.h"
#include "ui_chatwindow.h"
using namespace std;
ChatWindow::ChatWindow(QString nick, QWidget *parent) :
QMainWindow(parent),
ui(new Ui::ChatWindow)
{
ui->setupUi(this);
QList<int> sizes;
sizes.push_front(ui->chatText->height());
sizes.push_front(100);
ui->splitter->setSizes(sizes);
nickName = nick;
myNickname = ((Chatroom*)this->parent())->getNickname();
this->setWindowTitle("Chat with " + nick);
connect(ui->actionExit,SIGNAL(triggered()),this,SLOT(close()));
connect(ui->actionSave_chat,SIGNAL(triggered()),ui->chatText,SLOT(saveChatToFile()));
connect(ui->inputText,SIGNAL(msgReady()),this,SLOT(sendMsg()));
connect(ui->sendButton,SIGNAL(clicked()),this,SLOT(sendMsg()));
connect(this,SIGNAL(msgToPrint(QString)),this,SLOT(printMsg(QString)));
}
void ChatWindow::notifyPrint(string &msg)
{
emit msgToPrint(QString::fromStdString(msg));
}
void ChatWindow::printMsg(QString str)
{
string msg = str.toStdString();
if(str[0] == '@')
{
ui->chatText->printMsg(myNickname.toStdString() + ":" + msg.substr(msg.find(" ")));
}
else
{
ui->chatText->printMsg(msg);
}
}
void ChatWindow::sendMsg()
{
QString msg = ui->inputText->toPlainText();
ui->inputText->clear();
msg = "@" + nickName + " " +msg;
((Chatroom*)this->parent())->putMsgToSendQueue(msg);
}
void ChatWindow::closeEvent(QCloseEvent *event)
{
((Chatroom*)this->parent())->removeChat(this->nickName);
event->accept();
}
ChatWindow::~ChatWindow()
{
delete ui;
}