-
Notifications
You must be signed in to change notification settings - Fork 4
/
friendswidget.cpp
218 lines (198 loc) · 6.31 KB
/
friendswidget.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
#include "friendswidget.h"
FriendsWidget::FriendsWidget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::FriendsWidgetClass)
{
ui->setupUi(this);
//更新HarmonyOS字体
QFont font;
int font_Id = QFontDatabase::addApplicationFont(":/src/font/HarmonyOS_Sans_SC_Regular.ttf");
QStringList fontName = QFontDatabase::applicationFontFamilies(font_Id);
font.setFamily(fontName.at(0));
auto listWidget = findChildren<QWidget*>();
for (auto& widget : listWidget) //遍历所有组件
{
font.setWeight(widget->font().weight());
font.setPointSize(widget->font().pointSize());
widget->setFont(font);
}
//多线程
thread = new QThread();
msgService = new MsgService(nullptr, 2);
msgService->moveToThread(thread);
thread->start();
connect(this, &FriendsWidget::searchMember, msgService, &MsgService::searchMember);
connect(msgService, &MsgService::searchMemRes, this, &FriendsWidget::setMemberInfo);
connect(this, &FriendsWidget::sendApply, msgService, &MsgService::sendApply);
connect(this, &FriendsWidget::loadApplyInfo, msgService, &MsgService::loadApplyInfo);
connect(this, &FriendsWidget::operateApply, msgService, &MsgService::operateApply);
connect(msgService, &MsgService::sendApplyFinished, this, [=](QString res) {
ui->btn_sendApply->setText("发送验证信息");
ui->btn_sendApply->setEnabled(true);
if(res == "Exist")
QMessageBox::warning(this, "错误", "已存在发送的验证信息或当前已互为好友。", QMessageBox::Ok);
else if (res == "OK")
{
ui->lineEdit_applyInfo->clear();
QMessageBox::information(this, "提示", "发送好友验证成功,等待对方验证。", QMessageBox::Ok);
}
else
QMessageBox::warning(this, "错误", res, QMessageBox::Ok);
});
connect(msgService, &MsgService::loadApplyInfoFinished, this, [=](QString res) {
ui->textBrowser_applyInfo->setText(res);
if (!this->isVisible())
{
this->showMinimized();
this->showNormal();
}
emit loadApplyInfoFinished();
});
connect(msgService, &MsgService::operateApplyFinished, this, [=](QString res) {
ui->label_status->setText("处理完成");
ui->btn_agree->setEnabled(true);
ui->btn_reject->setEnabled(true);
if (res.indexOf("错误信息") != -1)
{
QMessageBox::warning(this, "错误", res, QMessageBox::Ok);
return;
}
if (res == "OK_1")
QMessageBox::information(this, "提示", QString("已成功添加 [%1] 为好友,请刷新好友列表。").arg(applyUid), QMessageBox::Ok);
else
QMessageBox::information(this, "提示", QString("已拒绝添加 [%1] 为好友。").arg(applyUid), QMessageBox::Ok);
applyUid = -1;
initInfo();
this->close();
});
}
FriendsWidget::~FriendsWidget()
{
//在此处等待所有线程停止
if (thread->isRunning())
{
thread->quit();
thread->wait();
}
delete msgService;
delete thread;
delete ui;
}
void FriendsWidget::loading()
{
QString str = "加载中...";
ui->uid->setText(str);
ui->name->setText(str);
ui->group->setText(str);
ui->department->setText(str);
ui->avatar->setPixmap(QPixmap(":/images/color_icon/user.svg"));
}
void FriendsWidget::initInfo()
{
QString str = "--";
ui->uid->setText(str);
ui->name->setText(str);
ui->group->setText(str);
ui->department->setText(str);
ui->avatar->setPixmap(QPixmap(":/images/color_icon/user.svg"));
ui->textBrowser_applyInfo->clear();
ui->lineEdit_searchUid->clear();
ui->lineEdit_applyInfo->clear();
ui->label_status->setText("");
}
void FriendsWidget::loadApply(const QString& uid)
{
applyUid = uid;
initInfo();
loading();
emit searchMember(applyUid);
emit loadApplyInfo(this->uid, applyUid);
ui->group_addFriends->setEnabled(false);
ui->group_applyInfo->setEnabled(true);
ui->group_applyInfo->setVisible(true);
this->adjustSize();
}
void FriendsWidget::setMemberInfo(QByteArray array)
{
ui->btn_search->setEnabled(true);
QDataStream stream(&array, QIODevice::ReadOnly);
QString name, group, department;
QPixmap avatar;
bool res;
stream >> name >> group >> department >> avatar >> res;
ui->group->setText(group);
ui->department->setText(department);
ui->avatar->setPixmap(service::setAvatarStyle(avatar));
if (res)
{
ui->uid->setText(searchUid);
ui->name->setText(name);
}
else
{
searchUid = "-1";
ui->uid->setText("用户不存在");
ui->name->setText("--");
}
if(applyUid != "-1")
ui->uid->setText(applyUid);
}
void FriendsWidget::on_btn_sendApply_clicked()
{
if (ui->lineEdit_applyInfo->text().isEmpty() || searchUid == "-1" || searchUid == uid)
{
QMessageBox::warning(this, "错误", "请填写正确的信息后重试。", QMessageBox::Ok);
return;
}
ui->btn_sendApply->setText("正在请求...");
ui->btn_sendApply->setEnabled(false);
emit sendApply(uid, searchUid, ui->lineEdit_applyInfo->text());
}
void FriendsWidget::on_btn_agree_clicked()
{
ui->label_status->setText("处理中...");
ui->btn_agree->setEnabled(false);
ui->btn_reject->setEnabled(false);
emit operateApply(uid, applyUid, 1);
}
void FriendsWidget::on_btn_reject_clicked()
{
ui->label_status->setText("处理中...");
ui->btn_agree->setEnabled(false);
ui->btn_reject->setEnabled(false);
emit operateApply(uid, applyUid, 2);
}
void FriendsWidget::on_btn_search_clicked()
{
if (ui->lineEdit_searchUid->text().isEmpty())
return;
loading();
applyUid = "-1";
searchUid = ui->lineEdit_searchUid->text();
ui->btn_search->setEnabled(false);
emit searchMember(ui->lineEdit_searchUid->text());
}
void FriendsWidget::setUid(const QString& uid)
{
this->uid = uid;
}
void FriendsWidget::set_group_applyInfo_enabled(bool flag)
{
ui->group_applyInfo->setEnabled(flag);
ui->group_applyInfo->setVisible(flag);
this->adjustSize();
}
void FriendsWidget::set_group_addFriends_enabled(bool flag)
{
ui->group_addFriends->setEnabled(flag);
}
void FriendsWidget::keyPressEvent(QKeyEvent* event)
{
switch (event->key())
{
case Qt::Key_Escape:
this->close(); break;
default:
QWidget::keyPressEvent(event);
}
}