-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnewfriend.cpp
More file actions
304 lines (252 loc) · 10.6 KB
/
Copy pathnewfriend.cpp
File metadata and controls
304 lines (252 loc) · 10.6 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
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
293
294
295
296
297
298
299
300
301
302
303
#include "newfriend.h"
#include "ui_newfriend.h"
#include"user.h"
#include<QListWidget>
#include<QListWidgetItem>
#include<QList>
#include<QDebug>
#include<QMap>
#include<QMessageBox>
#include<QCheckBox>
#include"answer.h"
#include"user_info.h"
extern QList<application>applyList;
extern QMap<QString,QString>corre;
extern QList <USER> list;
extern USER succeed;//单独全局变量已登录的用户
QList<USER>temp;//搜索栏临时列表
QList<application>my_applicants;
USER selected;
USER applicant;
newFriend::newFriend(QWidget *parent) :
QDialog(parent),
ui(new Ui::newFriend)
{
ui->setupUi(this);
this->setWindowTitle("新的朋友");
for(int i=0;i<list.size();i++){
corre[list[i].phone]=list[i].name;
qDebug()<<corre[list[i].phone];
}
ui->name_accord->setCheckState(Qt::Checked);
for(int i=0;i<applyList.size();i++){
application app=applyList[i];
if(applyList[i].from==succeed.phone){
QListWidgetItem *item;
item=new QListWidgetItem;
if(applyList[i].accepted=="2"){
item->setText(QString("请求添加 "+corre[applyList[i].to]+" 为好友 申请中"));
item->setTextAlignment(Qt::AlignLeft);
item->setFlags(item->flags() & ~Qt::ItemIsSelectable);
item->setFlags(Qt::ItemIsEnabled);
item->setCheckState(Qt::Unchecked);//默认 不选中状态
ui->applyList->addItem(item);
my_applicants<<app;
}
if(app.accepted=="1"){
item->setText(QString("请求添加 "+corre[app.to]+" 为好友 已通过"));
item->setTextAlignment(Qt::AlignLeft);
item->setFlags(item->flags() & ~Qt::ItemIsSelectable);
item->setFlags(Qt::ItemIsEnabled);
item->setCheckState(Qt::Unchecked);//默认 不选中状态
ui->applyList->addItem(item);
my_applicants<<app;
}
if(app.accepted=="0"){
item->setText(QString("请求添加 "+corre[app.to]+" 为好友 被拒绝"));
item->setTextAlignment(Qt::AlignLeft);
item->setFlags(item->flags() & ~Qt::ItemIsSelectable);
item->setFlags(Qt::ItemIsEnabled);
item->setCheckState(Qt::Unchecked);//默认 不选中状态
ui->applyList->addItem(item);
my_applicants<<app;
}
}
if(app.to==succeed.phone){
QListWidgetItem *item;
item=new QListWidgetItem;
if(app.accepted=="2"){
item->setText(QString(corre[app.from]+" 请求添加你为好友 申请中"));
item->setTextAlignment(Qt::AlignLeft);
item->setFlags(Qt::ItemIsUserCheckable);
item->setFlags(item->flags() & ~Qt::ItemIsSelectable);
item->setFlags(Qt::ItemIsEnabled);
item->setCheckState(Qt::Unchecked);//默认 不选中状态
ui->applyList->addItem(item);
my_applicants<<app;
}
if(app.accepted=="1"){
item->setText(QString(corre[app.from]+" 请求添加你为好友 已通过"));
item->setTextAlignment(Qt::AlignLeft);
item->setFlags(item->flags() & ~Qt::ItemIsSelectable);
item->setFlags(Qt::ItemIsEnabled);
item->setCheckState(Qt::Unchecked);//默认 不选中状态
ui->applyList->addItem(item);
my_applicants<<app;
}
if(app.accepted=="0"){
item->setText(QString(corre[app.from]+" 请求添加你为好友 已拒绝"));
item->setTextAlignment(Qt::AlignLeft);
item->setFlags(item->flags() & ~Qt::ItemIsSelectable);
item->setFlags(Qt::ItemIsEnabled);
item->setCheckState(Qt::Unchecked);//默认 不选中状态
ui->applyList->addItem(item);
my_applicants<<app;
}
}
}
}
newFriend::~newFriend()
{
delete ui;
}
void newFriend::on_return_2_clicked()
{
this->close();
User *pic=new User;
pic->show();
}
void newFriend::on_name_accord_stateChanged(int arg1)
{
if(arg1==Qt::CheckState::Checked){
ui->phone_accord->blockSignals(true);
ui->phone_accord->setCheckState(Qt::CheckState::Unchecked);
ui->phone_accord->blockSignals(false);
}
}
void newFriend::on_phone_accord_stateChanged(int arg1)
{
if(arg1==Qt::CheckState::Checked){
ui->name_accord->blockSignals(true);
ui->name_accord->setCheckState(Qt::CheckState::Unchecked);
ui->name_accord->blockSignals(false);
}
}
void newFriend::on_go_clicked()
{
ui->result->clear();
temp.clear();
if(ui->name_accord->checkState()==Qt::Unchecked&&ui->phone_accord->checkState()==Qt::Unchecked){
QMessageBox::critical(this,tr("提示"),tr("请选择一种搜索方式!"));
}
if(ui->name_accord->checkState()==Qt::Checked&&ui->phone_accord->checkState()==Qt::Unchecked){
QString res=ui->searchLine->text();
int flag=0;
for(int i=0;i<list.size();i++){
if(list[i].name_permit==1&&list[i].name.indexOf(res)!=-1){
QListWidgetItem *item;
item=new QListWidgetItem;
item->setText(QString(list[i].name));
item->setFlags(item->flags() & ~Qt::ItemIsSelectable);
item->setFlags(Qt::ItemIsEnabled|Qt::ItemIsUserCheckable);//双击可选中
item->setCheckState(Qt::Unchecked);//默认 不选中状态
//最后将初始化好的子项插入到listWidget控件中
ui->result->addItem(item);
temp<<list[i];
flag=1;
}
}
if(flag==0)QMessageBox::information(this,tr(""),tr("查无结果,可能输入错误或对方设置不可被查找"));
}
if(ui->name_accord->checkState()==Qt::Unchecked&&ui->phone_accord->checkState()==Qt::Checked){
QString res=ui->searchLine->text();
int flag=0;
for(int i=0;i<list.size();i++){
if(list[i].phone_permit==1&&list[i].phone.indexOf(res)!=-1){
QListWidgetItem *item;
item=new QListWidgetItem;
item->setText(QString(list[i].name));
item->setFlags(item->flags() & ~Qt::ItemIsSelectable);
item->setFlags(Qt::ItemIsEnabled|Qt::ItemIsUserCheckable);//双击可选中
item->setCheckState(Qt::Unchecked);//默认 不选中状态
ui->result->addItem(item);
temp<<list[i];
flag=1;
}
}
if(flag==0)QMessageBox::information(this,tr(""),tr("查无结果,可能输入错误或对方设置不可被查找"));
}
}
void newFriend::on_result_doubleClicked(const QModelIndex &index)
{
selected=temp[index.row()];
this->close();
Answer*pic=new Answer;
pic->show();
}
void newFriend::on_applyList_doubleClicked(const QModelIndex &index)
{
application appli=my_applicants[index.row()];
if(appli.accepted=="1"||appli.accepted=="0")return;
if(appli.to==succeed.phone&&appli.accepted=="2"){
QMessageBox msgBox;
msgBox.setText("你的问题是:"+succeed.question+"\n他的回答是:"+appli.answer+"\n是否接受申请?");
msgBox.setStandardButtons(QMessageBox::Ok| QMessageBox::No);
int ret = msgBox.exec();
switch(ret){
case QMessageBox::Ok:{
ui->applyList->item(index.row())->setText(QString(corre[appli.from]+" 请求添加你为好友 已通过"));
succeed.contact<<appli.from;
for(int i=0;i<list.size();i++){
if(list[i].phone==appli.from){
list[i].contact<<succeed.phone;
}
}
for(int i=0;i<list.size();i++)if(list[i].phone==succeed.phone)list[i]=succeed;
QFile file("contact.txt");
bool isok=file.open(QIODevice::WriteOnly|QIODevice::Truncate);
if(isok){
QTextStream out(&file);
out.setCodec("UTF-8");
for(int i=0;i<list.size();i++){
for(int j=0;j<list[i].contact.size();j++)out<<list[i].contact[j]<<" ";
out<<endl;
}
}
else qDebug()<<"error";
file.close();
for(int i=0;i<applyList.size();i++){
if(applyList[i].from==appli.from&&applyList[i].to==succeed.phone){
applyList[i].accepted="1";
}
if(applyList[i].to==appli.from&&applyList[i].from==succeed.phone){
applyList[i].accepted="1";
}
}
QFile file1("application.txt");
bool isok1=file1.open(QIODevice::WriteOnly|QIODevice::Truncate);
if(isok1){
QTextStream out(&file1);
out.setCodec("UTF-8");
for(int i=0;i<applyList.size();i++){
out<<applyList[i].from<<" "<<applyList[i].to<<" "<<applyList[i].accepted<<" "<<applyList[i].answer<<endl;
}
}
else qDebug()<<"error";
file1.close();
break;
}
case QMessageBox::No:{
ui->applyList->item(index.row())->setText(QString(corre[appli.from]+" 请求添加你为好友 已拒绝"));
for(int i=0;i<applyList.size();i++){
if(applyList[i].from==appli.from&&applyList[i].to==succeed.phone){
applyList[i].accepted="0";
}
}
QFile file2("application.txt");
bool isok2=file2.open(QIODevice::WriteOnly|QIODevice::Truncate);
if(isok2){
QTextStream out(&file2);
out.setCodec("UTF-8");
for(int i=0;i<applyList.size();i++){
out<<applyList[i].from<<" "<<applyList[i].to<<" "<<applyList[i].accepted<<" "<<applyList[i].answer<<endl;
}
}
else qDebug()<<"error";
file2.close();
break;
}
default:break;
}
}
}