-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatwindow.cpp
101 lines (89 loc) · 2.99 KB
/
chatwindow.cpp
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "chatwindow.h"
#include <QTextTable>
#include <QTextCursor>
#include <QTime>
#include <QScrollBar>
#include <QShortcut>
#include <QMenuBar>
#include "newconnectiondialog.h"
ChatWindow::ChatWindow(QWidget *parent) :
QMainWindow(parent)
{
qDebug() << "@ChatWindow::ChatWindow";
setupUi(this);
client = new Client;
nCW = new NewConnectionDialog(this);
textEdit->setReadOnly(true);
connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed()));
connect(client, SIGNAL(newMessage(QColor, QString)), this, SLOT(writeMessage(QColor,QString)));
connect(client, SIGNAL(newMongo(QColor)), this, SLOT(newMongo(QColor)));
connect(client, SIGNAL(mongoLeft(QColor)), this, SLOT(mongoLeft(QColor)));
connect(client, SIGNAL(stateChanged(State)), this, SLOT(setState(State)));
connect(actionNew_Connection, SIGNAL(triggered()), nCW, SLOT(show()));
connect(nCW, SIGNAL(accepted()), this, SLOT(callClientForNewConnection()));
tableFormat.setBorder(0);
nCW->hide();
}
ChatWindow::~ChatWindow(){
delete client;
}
void ChatWindow::setState(State s){
QString server, client;
switch (s) {
case ServerOn:
server = "server online";
break;
case ServerOff:
server = "server offline";
break;
case Connected:
client = "connected";
break;
case Unconnected:
client = "not connected";
break;
default:
client = server = "unknown";
break;
}
qDebug() << "@ChatWindow::setState: " << server << client;
stateLabel->setText("state: "+server+"\t"+client);
}
void ChatWindow::writeMessage(QColor who, const QString &msg){
qDebug() << "@ChatWindow::writeMessage" << who;
if(msg.isEmpty())
return;
textEdit->setTextColor(who);
textEdit->append(("["+QTime::currentTime().toString(Qt::ISODate)+"]")+" "+msg+"\n");
QScrollBar *bar = textEdit->verticalScrollBar();
bar->setValue(bar->maximum());
db.newEntry(QTime::currentTime().toString(Qt::ISODate),qColorToString(who),msg);
}
void ChatWindow::returnPressed(){
qDebug() << "@ChatWindow::returnPressed";
QString text = lineEdit->text();
if(text.isEmpty())
return;
client->sendMessage(text);
writeMessage(QColor(Qt::black), text);
lineEdit->clear();
}
void ChatWindow::newMongo(const QColor &who){
if(who == QColor(Qt::black))
return;
textEdit->setTextColor(who);
textEdit->append(("["+QTime::currentTime().toString(Qt::ISODate)+"]")+" Have joined");
textEdit->setTextColor(Qt::gray);
textEdit->append("#NoFakeNews");
}
void ChatWindow::mongoLeft(const QColor &who){
if(who == QColor(Qt::black))
return;
textEdit->setTextColor(who);
textEdit->append(("["+QTime::currentTime().toString(Qt::ISODate)+"]")+" Have left");
textEdit->setTextColor(Qt::gray);
textEdit->append("#NoFakeNews");
}
void ChatWindow::callClientForNewConnection(){
client->initiateNewConnection(QHostAddress(nCW->currentHostAddress()));
}