-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainclass.cpp
118 lines (106 loc) · 3.82 KB
/
mainclass.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* @file mainclass.cpp
* @brief this is the cpp file for "mainclass" class
* @author Giang Nguyen
* Contact: [email protected]
*/
#include <QDebug>
#include "mainclass.h"
typedef std::pair<QString, QObject*> chatWindowPair;
MainClass::MainClass(QObject *parent) :
QObject(parent)
{
}
/**
* @brief init save the engine and component pointer
* @param engine the pointer to the engine
* @param component the pointer to the component
*/
void MainClass::init(QQmlApplicationEngine *engine, QQmlComponent *component)
{
this->engine=engine;
this->component=component;
}
/**
* @brief createMainWindow create new main.qml window
* @param userIP an integer storing the order of this MainClass in the engine
*/
void MainClass::createMainWindow(int userIP)
{
engine->load(QUrl(QStringLiteral("qrc:/Main.qml")));
mainQMLObject = engine->rootObjects()[userIP];
QQmlProperty::write(mainQMLObject,"userIP",userIP);
QObject::connect(mainQMLObject, SIGNAL(userloggedin(QString)),this, SLOT(login(QString)));
QObject::connect(mainQMLObject, SIGNAL(userloggedout()),this, SLOT(logout()));
QObject::connect(mainQMLObject, SIGNAL(newconversation(QString)),this, SLOT(createNewChatWindow(QString)));
}
/**
* @brief login log the user in
* @param username a string storing the username
*/
void MainClass::login(QString username)
{
this->username=username;
}
/**
* @brief logout log the user out
*/
void MainClass::logout()
{
this->username="";
while (!chatWindowObjects.empty()) {
qDebug() << "Close chat window with: " + chatWindowObjects.begin()->first;
QMetaObject::invokeMethod(chatWindowObjects.begin()->second,"close");
// the close method will cause a closing signal to call the closeSelectWindow method
// therefore, there's no need to call erase()
}
}
/**
* @brief createNewChatWindow create a new chat window with given partner
* @param partner a string storing the partner's username
*/
void MainClass::createNewChatWindow(QString partner)
{
if (chatWindowObjects.find(partner) == chatWindowObjects.end()) {
qDebug() << "Open new chat window with: " + partner;
QObject *object = component->create();
QQmlProperty::write(object,"username",username);
QQmlProperty::write(object,"partner",partner);
chatWindowObjects.insert(chatWindowPair(partner,object));
QObject::connect(&(*object),SIGNAL(closechatwindow(QString)),this,SLOT(closeSelectWindow(QString)));
QVariant QVusername = username;
QVariant QVpartner = partner;
QMetaObject::invokeMethod(mainQMLObject,"newWindow",Q_ARG(QVariant,QVusername),Q_ARG(QVariant,QVpartner));
}
}
/**
* @brief closeSelectWindow close the selected window with given partner
* @param partner a string storing the partner's username
*/
void MainClass::closeSelectWindow(QString partner)
{
chatWindowObjects.erase(partner);
}
/**
* @brief updateNewMessage update the new message to user's chat window
* @param partner a string storing the partner's username
* @param sender a string storing the sender's username
* @param message a string storing the message
* @param isNew a string storing "True" or "False", corresponding to this is a new message or not
*/
void MainClass::updateNewMessage(QString partner, QString sender, QString message, QString isNew)
{
QVariant QVsender = sender;
QVariant QVmessage = message;
QVariant QVisNew = isNew;
std::map<QString, QObject*>::iterator it;
it = chatWindowObjects.find(partner);
if (it == chatWindowObjects.end()) {
createNewChatWindow(partner);
}
else {
QObject *object = it->second->findChild<QObject*>("wnd");
QMetaObject::invokeMethod(object, "receiveMessage",
Q_ARG(QVariant,QVsender),Q_ARG(QVariant, QVmessage),Q_ARG(QVariant, QVisNew));
}
}