-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
84 lines (74 loc) · 2.22 KB
/
Copy pathmainwindow.cpp
File metadata and controls
84 lines (74 loc) · 2.22 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDesktopWidget>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
dialog = new DIalogNickName(this);
webSock = new webSocket(QUrl("ws://62.109.21.96/ws/"), this);
connect(webSock, SIGNAL(onLogined(bool)), this, SLOT(onLogined(bool)));
connect(webSock, SIGNAL(onNewMessage(QJsonDocument)), this, SLOT(onNewMessage(QJsonDocument)));
connect(webSock, SIGNAL(connected()), this, SLOT(onConnected()));
connect(webSock, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
this->setWindowIcon(QIcon(":/Ico.png"));
this->setGeometry(QApplication::desktop()->width() / 2 - this->width() / 2 , QApplication::desktop()->height() / 2 - this->height() / 2, 0, 0);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::onConnected()
{
ui->statusBar->showMessage("Connected!", 20000);
QString name;
DIalogNickName* dialog = new DIalogNickName(this);
name = dialog->getNickName();
webSock->loginIn(name);
}
void MainWindow::onDisconnected()
{
ui->statusBar->showMessage("Connecting...");
QMessageBox::critical(this, "Warning!", "A web socket is not connected");
}
void MainWindow::on_buttonOk_clicked()
{
if(ui->textEdit->toPlainText().isEmpty())
return;
if(webSock->isConnected())
{
webSock->sendMessage(ui->textEdit->toPlainText().trimmed());
ui->textEdit->setPlainText("");
}
else
{
QMessageBox::critical(this, "Fault", "A web socket is not connected");
}
}
void MainWindow::onNewMessage(QJsonDocument doc)
{
QJsonArray arr = doc.object().value("body").toObject().value("messages").toArray();
for(int i = 0; i < arr.count(); i++)
{
QJsonObject obj = arr.at(i).toObject();
QString userName = obj.value("username").toString();
QString messageText = obj.value("message").toString();
int timeStamp = obj.value("dateTime").toInt();
ui->listMessages->append(Helpers::getActionHtml(Helpers::getDateTimeS(timeStamp), userName, messageText));
}
}
void MainWindow::onLogined(bool b)
{
if(b)
{
ui->buttonOk->setEnabled(true);
}
else
{
QMessageBox::critical(this, "Fault", "Error during authorization, try another nickname.");
QString name;
name = dialog->getNickName();
webSock->loginIn(name);
}
}