chatwindow.cpp 1.55 KB
#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;
}