Blame view

QChatClient/chatwindow.cpp 1.55 KB
1
2
3
#include "chatwindow.h"
#include "ui_chatwindow.h"
4
5
using namespace std;
Imanol-Mikel Barba Sabariego authored
6
ChatWindow::ChatWindow(QString nick, QWidget *parent) :
7
8
9
10
    QMainWindow(parent),
    ui(new Ui::ChatWindow)
{
    ui->setupUi(this);
11
12
13
14
    QList<int> sizes;
    sizes.push_front(ui->chatText->height());
    sizes.push_front(100);
    ui->splitter->setSizes(sizes);
Imanol-Mikel Barba Sabariego authored
15
16
17
18
19

    nickName = nick;
    myNickname = ((Chatroom*)this->parent())->getNickname();
    this->setWindowTitle("Chat with " + nick);
20
    connect(ui->actionExit,SIGNAL(triggered()),this,SLOT(close()));
Imanol-Mikel Barba Sabariego authored
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
    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);
51
52
}
Imanol-Mikel Barba Sabariego authored
53
void ChatWindow::closeEvent(QCloseEvent *event)
54
{
Imanol-Mikel Barba Sabariego authored
55
56
    ((Chatroom*)this->parent())->removeChat(this->nickName);
    event->accept();
57
58
59
60
61
62
}

ChatWindow::~ChatWindow()
{
    delete ui;
}