Blame view

QChatClient/chatwindow.cpp 1.62 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
    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)));
}
27
28
29
30
31
void ChatWindow::notifyClose()
{
    emit ui->actionExit->triggered();
}
Imanol-Mikel Barba Sabariego authored
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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);
56
57
}
Imanol-Mikel Barba Sabariego authored
58
void ChatWindow::closeEvent(QCloseEvent *event)
59
{
Imanol-Mikel Barba Sabariego authored
60
61
    ((Chatroom*)this->parent())->removeChat(this->nickName);
    event->accept();
62
63
64
65
66
67
}

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