-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
295 lines (276 loc) · 10.7 KB
/
mainwindow.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, can_login(false), button1(new QPushButton()), button2(new QPushButton()), ui(new Ui::MainWindow)
, log_in(new LoginWidget(this)), client(new QTcpSocket(this)), sign_up(nullptr), chat_window(nullptr)
, style(new Style()), animation_close(new QPropertyAnimation(this, "windowOpacity"))
, animation_minimize(new QPropertyAnimation(this, "windowOpacity"))
{
this->ui->setupUi(this);
this->setWindowIcon(QIcon("/img/icon.png"));
this->setWindowFlags(Qt::FramelessWindowHint);
this->style->hide();
client->connectToHost(QHostAddress("127.0.0.1"), 8899);
//客户端响应服务器
connect(client, &QTcpSocket::readyRead, [this]() {
auto recv = this->client->readAll();
auto recv_list = recv.split('\n');
for (auto& recv: recv_list) {
process_response(recv);
}
});
//点击×关闭窗口
connect(this->animation_close, &QPropertyAnimation::finished, this, &QMainWindow::close);
connect(this->ui->closeWindow, &QPushButton::clicked, [this]() {
this->animation_close->setDuration(100);
this->animation_close->setStartValue(1);
this->animation_close->setEndValue(0);
this->animation_close->start();
});
//点击-最小化窗口
connect(this->ui->minimizeWindow, &QPushButton::clicked, [this]() {
this->animation_minimize->setDuration(100);
this->animation_minimize->setStartValue(1);
this->animation_minimize->setEndValue(0);
this->animation_minimize->start();
});
connect(this->animation_minimize, &QPropertyAnimation::finished, [this]() {
this->showMinimized();
this->setProperty("windowOpacity", 1);
});
//点击登录,同时disable登录按钮
connect(this->ui->logIn, &QPushButton::clicked, [this](){
QString acc_psw = Utility::get_string_of_request_type(RequestType::LOGIN) + " " + this->ui->account->text() + " " + this->ui->password->text();
//将account和psw和请求类型传给服务器
this->client->write(acc_psw.toStdString().c_str());
this->ui->logIn->setText("Waiting...");
this->ui->logIn->setEnabled(false);
});
//点击注册label
connect(this->ui->signUp, &ClickableLabel::clicked, [this]() {
this->sign_up = new SignUp(this->client, this);
//ui->signUp->setStyleSheet()
this->sign_up->get_ui()->label->setStyleSheet(QString(styleStr).arg(file).arg(r1+g1+b1).arg(r2+g2+b2));
this->sign_up->show();
//TODO:bug,多次点击sign up label会产生多个注册窗口
});
//账号、密码有一个为空,disable登录按钮
connect(this->ui->account, &QLineEdit::textChanged, [this]() {
this->can_login = this->ui->account->text() != "" && this->ui->password->text() != "";
this->ui->logIn->setEnabled(can_login);
});
connect(this->ui->password, &QLineEdit::textChanged, [this]() {
this->can_login = this->ui->account->text() != "" && this->ui->password->text() != "";
this->ui->logIn->setEnabled(can_login);
});
//*dry<
connect(this->ui->styleButton, &ClickableLabel::clicked, [this]() {
this->style->show();
});
//connect(this->style, &QWidget::close, this, &QMainWindow::show);
connect(style,&Style::sig,this,[this](){
this->ui->window_border->setStyleSheet(QString("QLabel#window_border{background-image:url(:/new/patterns/patterns/%1.png);"
"background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #%2,stop:1 #%3);"
"background-position:center;}").arg(file).arg(r1+g1+b1).arg(r2+g2+b2));
});
//*>
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::my_slot()
{
qDebug() << "Click Button2\n";
this->close();
}
void MainWindow::recover_login_btn()
{
this->ui->logIn->setEnabled(true);
this->ui->logIn->setText("Login");
}
void MainWindow::process_response(QByteArray& bytes)
{
auto std_str = QString(bytes).toStdString();
if (std_str.empty()) {
return;
}
std::cout << "got: " + std_str << std::endl;
switch (std_str.front()) {
case 'a':
{
auto friends = QVector<QString>();
std::stringstream in;
//登录
int status;
int acc_exist;
int correct_psw;
int is_online;
in = std::stringstream(std_str.substr(9, std_str.size()));
sscanf_s(std_str.c_str(), "a %d %d %d %d", &status, &acc_exist, &correct_psw, &is_online);
if (status == 0) {
QApplication::beep();
auto pop_up = LoginHint("Server Error!", this);
qDebug() << "Server Error";
pop_up.exec();
this->recover_login_btn();
return;
}
if (!acc_exist) {
QApplication::beep();
auto pop_up = LoginHint("Illegal Account!", this);
qDebug() << "Illegal Account";
pop_up.exec();
this->recover_login_btn();
return;
}
if (!correct_psw) {
QApplication::beep();
auto pop_up = LoginHint("Error Password!", this);
qDebug() << "Error Password";
pop_up.exec();
this->recover_login_btn();
return;
}
if (is_online) {
QApplication::beep();
auto pop_up = LoginHint("Already Online!", this);
qDebug() << "Already Online";
pop_up.exec();
this->recover_login_btn();
return;
}
while (in) {
auto friend_acc = std::string();
in >> friend_acc;
friends.push_back(friend_acc.c_str());
}
friends.pop_back();
chat_window = new ChatWindow(friends, this->ui->account->text(), this->client, this->ui->window_border->styleSheet() != ""? QString("QLabel#windowBorder{background-image:url(:/new/patterns/patterns/%1.png);"
"background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #%2,stop:1 #%3);"
"background-position:center;}").arg(file).arg(r1+g1+b1).arg(r2+g2+b2):"");
//this->chat_window->get_ui()->centralwidget->setStyleSheet(QString("QWidget#centralWidget{background-image:url(:/new/patterns/patterns/%1.png);"
// "background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #%2,stop:1 #%3);"
// "background-position:center;}").arg(file).arg(r1+g1+b1).arg(r2+g2+b2));
this->hide();
chat_window->show();
}
break;
case 'b':
{
//注册
if (std_str == "b 1") {
qDebug() << "Success!";
auto pop_up = LoginHint("Success!", this->sign_up);
pop_up.exec();
} else if (std_str == "b 0") {
QApplication::beep();
qDebug() << "Account Already Exsits!";
auto pop_up = LoginHint("Account Already Exsits!", this->sign_up);
pop_up.exec();
} else {
QApplication::beep();
qDebug() << "Server Error!";
auto pop_up = LoginHint("Server Error!", this->sign_up);
pop_up.exec();
}
}
break;
case 'c':
{
//添加好友
if (std_str[2] == '1') {
this->chat_window->addFriend(std_str.substr(4, std_str.size()).c_str());
auto pop_up = LoginHint("Added Successfully", this->chat_window);
pop_up.exec();
} else {
QApplication::beep();
auto pop_up = LoginHint("No Such ID", this->chat_window);
pop_up.exec();
}
}
break;
case 'd':
{
//qDebug() << QString::fromUtf8("收到信息,准备发送");
int first_blank = 0;
int second_blank = 0;
for (int i = 0, blank_cnt = 0; i < std_str.size() && blank_cnt < 2; ++i) {
if (std_str[i] == ' ') {
if (blank_cnt == 0) {
first_blank = i;
} else {
second_blank = i;
}
++blank_cnt;
}
}
auto from = std_str.substr(first_blank + 1, second_blank - first_blank - 1);
auto msg = std_str.substr(second_blank + 1, std_str.size());
this->chat_window->recieve(from._Starts_with("__group__")?
QString(from.substr(9, from.size()).c_str()):
QString(from.c_str())
,QString(msg.c_str()));
//sscanf_s(std_str.c_str(), "d %s %s", from, msg);
std::cout << "recv_msg: {from: " << from << ", msg: " << msg << "}" << std::endl;
}
break;
case 'e':
{
qDebug() << "ready to join a group";
int status;
sscanf_s(std_str.c_str(), "e %d", &status);
qDebug() << QString("status = %1").arg(status);
if (!status) {
//已经存在这个群聊
auto pop_up = LoginHint("Group Has Been Existed!", this);
QApplication::beep();
pop_up.exec();
break;
}
auto group_name = QString::fromStdString(std_str.substr(4, std_str.size()));
qDebug() << "\"fake\" group name: " << group_name;
this->chat_window->addFriend(group_name);
auto pop_up = LoginHint("Created!");
pop_up.exec();
}
break;
default:
break;
}
//qDebug() << QString(bytes);
}
void MainWindow::mouseMoveEvent(QMouseEvent* event)
{
QPoint y = event->globalPos();
QPoint x = y - this->z;
this->move(x);
}
void MainWindow::mousePressEvent(QMouseEvent* event)
{
QPoint y = event->globalPos();
QPoint x = this->geometry().topLeft();
this->z = y - x;
}
void MainWindow::mouseReleaseEvent(QMouseEvent* event)
{
this->z = QPoint();
}
void MainWindow::keyPressEvent(QKeyEvent* event)
{
switch (event->key()) {
case Qt::Key_Enter:
if (this->ui->account->text().size() && this->ui->password->text().size()) {
this->ui->logIn->click();
}
break;
case Qt::Key_Return:
if (this->ui->account->text().size() && this->ui->password->text().size()) {
this->ui->logIn->click();
}
break;
default:
break;
}
}