diff --git a/Client/Client.pro b/Client/Client.pro index e60b656970c80a0483f34c93a2ee368ed1a10640..9b1851d5650ec659626c3a4338ad40c4480f6aad 100644 --- a/Client/Client.pro +++ b/Client/Client.pro @@ -44,7 +44,6 @@ HEADERS += \ databaseoperation.h \ myhighlighter.h \ typedef.h \ - userregister.cpp \ kuang.h \ ltest.h \ mainwindow.h \ diff --git a/Client/clientdatacenter.cpp b/Client/clientdatacenter.cpp index b2a08e19a98f5d97699748295edec4a34fde14ad..0c6a5049534f5636f3c31d8a20b856c842db3849 100644 --- a/Client/clientdatacenter.cpp +++ b/Client/clientdatacenter.cpp @@ -69,6 +69,7 @@ void ClientDataCenter::RegisterMessage(QJsonObject data){ OnlineMessage *temp = new OnlineMessage(data); registerMessage(temp); getSession(data["SessionID"].toInt()).getLatestMessageID() = data["MessageID"].toInt(); + emit sessionorderchange(data); } } diff --git a/Client/clientdatacenter.h b/Client/clientdatacenter.h index 8e8f6cbb0aa3a1f239aebbabbe24df3af857ac95..c423fcec92c02a37508297c22aa0b075349cbb56 100644 --- a/Client/clientdatacenter.h +++ b/Client/clientdatacenter.h @@ -41,6 +41,7 @@ signals: void addsessionmessage(QJsonObject data); void FriendSessionDataReceived(QJsonObject data); void GroupSessionDataReceived(QJsonObject data); + void sessionorderchange(QJsonObject data); public slots: void clean(); diff --git a/Client/clientmain.cpp b/Client/clientmain.cpp index 056f5f2984eeb58c62c61cbf35af02c231f2ebc5..e3e5646cba27f54f1495142b63d54f40c3564764 100644 --- a/Client/clientmain.cpp +++ b/Client/clientmain.cpp @@ -2,7 +2,7 @@ #include #include #include - +#include ClientMain::ClientMain(QString IPAddress, int portOpen, QObject *parent) : QObject(parent), ipAdd(IPAddress), port(portOpen) @@ -39,18 +39,13 @@ void ClientMain::createmainwindow(QJsonObject data){ //搜索按钮点击后发送消息到server connect(main,&MainWindow::SendSearchInfo,this,&ClientMain::SendSearchInfoToSever); connect(this,&ClientMain::SearchInfoReceived,main,&MainWindow::dealsearchinfo); - connect(main,&MainWindow::SendNewFriendInfo,this,&ClientMain::SendNewFriendInfoToSever); + connect(main,&MainWindow::SendNewFriendInfo1,this,&ClientMain::SendNewFriendInfoToSever); connect(main,&MainWindow::sendnewgroup,this,&ClientMain::send); + connect(&datacenter,&ClientDataCenter::sessionorderchange,main,&MainWindow::Sessionorderchange); } void ClientMain::SendNewFriendInfoToSever(Kuang *This){ Kuang::KuangChosenNow = nullptr; - int i,n = main->kuangstore.size(); - for(i=0;isearchlayout->removeWidget(main->kuangstore[i]); - delete main->kuangstore[i]; - } - main->kuangstore = {}; QJsonObject data = {{"MsgType","AddFriendRequest"}, {"FromUsername", main->username}, {"ToUsername", This->USERNAME}}; send(data); } diff --git a/Client/mainwindow.cpp b/Client/mainwindow.cpp index 6df5a5b99edf568003c696bdf8c1fdc06bb4906d..ab7d71a366b2500bb786dfd2184365f7a611bb8e 100644 --- a/Client/mainwindow.cpp +++ b/Client/mainwindow.cpp @@ -2,7 +2,7 @@ #include "ui_mainwindow.h" #include "kuang.h" #include "clientdatacenter.h" - +#include #include #include #include @@ -201,12 +201,45 @@ void MainWindow::dealsearchinfo(QJsonObject data){ searchlayout->addWidget(k); } } +void MainWindow::SendNewFriendInfo(Kuang *This){ + int j,n1 = friendsession.size(); + if(This->USERNAME == username){ + QMessageBox::information(this,"提示","不能添加自己为好友"); + return; + } + for(j=0;j < n1;j++){ + if(friendsession[j]->USERNAME == This->USERNAME){ + QMessageBox::information(this,"提示",friendsession[j]->USERNAME + "已经是您的好友了"); + return; + } + } + emit SendNewFriendInfo1(This); +} + +void MainWindow::Sessionorderchange(QJsonObject data){ + int ID = data["SessionID"].toInt(),i,n; + n = friendsession.size(); + for(i=0;iSessionID == ID){ + friendlayout->removeWidget(friendsession[i]); + friendlayout->insertWidget(0,friendsession[i]); + } + } + n = groupsession.size(); + for(i=0;iSessionID == ID){ + grouplayout->removeWidget(groupsession[i]); + grouplayout->insertWidget(0,groupsession[i]); + } + } +} void MainWindow::FriendSessionAdd(QJsonObject data){ QLayoutItem * lastItem = friendlayout->itemAt(friendlayout->count() - 1); // 头像弹簧 friendlayout->removeItem(lastItem); Kuang *k = new Kuang(username,data,this); friendlayout->addWidget(k); + friendsession.append(k); connect(k,&Kuang::KuangChosenChanged,this,&MainWindow::clearlistview); Kuang *t = new Kuang(username,data,this); newgrouplayout->addWidget(t); @@ -234,6 +267,7 @@ void MainWindow::GroupSessionAdd(QJsonObject data){ grouplayout->removeItem(lastItem); Kuang *k = new Kuang(data,this); grouplayout->addWidget(k); + groupsession.append(k); connect(k,&Kuang::KuangChosenChanged,this,&MainWindow::clearlistview); grouplayout->addStretch(); } @@ -261,6 +295,11 @@ void MainWindow::AddMessagetoListview(QJsonObject data){ void MainWindow::on_sendgroup_clicked() { + QString msg = ui->lineEdit->text(); + if(msg.size()==0){ + QMessageBox::information(this,"提示","请为您的群聊设置一个昵称"); + return; + } QJsonArray temp; int i, n = kuanggroupstore.size(); for(i=0;ilineEdit->text(); QJsonObject data = {{"MsgType","NewGroup"},{"SessionName",msg}}; data["Members"] = temp; emit sendnewgroup(data); diff --git a/Client/mainwindow.h b/Client/mainwindow.h index 328ba443b7b0f564b7a41ddee691a75949154f74..058f78910eb6e964b1b970432c02f60cea8cf4e9 100644 --- a/Client/mainwindow.h +++ b/Client/mainwindow.h @@ -36,7 +36,7 @@ public: QVBoxLayout *newgrouplayout; QVBoxLayout *newgroupviewlayout; QString username; - QVectorkuangstore,kuanggroupstore; + QVectorkuangstore,kuanggroupstore,friendsession,groupsession; //处理信息 void dealMessage(Message *messageW, QListWidgetItem *item, QString text, QString time, Message::UserType type); //处理搜索框好友显示 @@ -47,13 +47,15 @@ public: void clearlistview(); void dealsearchinfo(QJsonObject data); void groupchanged(Kuang *This); + void Sessionorderchange(QJsonObject data); + void SendNewFriendInfo(Kuang *This); signals: + void SendNewFriendInfo1(Kuang *This); void updatelistview(); void SendMessageToServer(const QString &sendername,const QString &text); bool eventFilter(QObject *watched, QEvent *event); void SendSearchInfo(QString msg); - void SendNewFriendInfo(Kuang *This); void sendnewgroup(QJsonObject data); private slots: diff --git a/Client/userregister.cpp b/Client/userregister.cpp index 89be22f245a5f0aa563d8d5b78ea9d44b5d2d9b0..f2de1e74851bc5e6775e555bf66c86abf2d5915c 100644 --- a/Client/userregister.cpp +++ b/Client/userregister.cpp @@ -1,64 +1,63 @@ -#include "userregister.h" -#include "ui_userregister.h" -#include -#include -#include -#include -#include - - -UserRegister::UserRegister(QWidget *parent) : - QWidget(parent), - ui(new Ui::UserRegister) -{ - ui->setupUi(this); - connect(ui->btnRegister,&QPushButton::clicked,[=](){ - bool islegal = true; - QJsonObject registration_info; - QString s = ui->lERgUserName->text(), s1; - if(s.size()==0){ - QMessageBox::critical(this,"Error!","用户名不能为空"); - islegal = false; - } - s = ui->lERgPassword1->text(); - s1 = ui->lERgPassword2->text(); - if((s.size()==0 || s1.size()==0) && islegal){ - QMessageBox::critical(this,"Error!","密码不能为空"); - islegal = false; - } - if(s != s1 && islegal){ - QMessageBox::critical(this,"Error!","两次输入的密码不一致"); - islegal = false; - } - if(islegal){ - //发送注册信息 - registration_info.insert("Username",ui->lERgUserName->text()); - registration_info.insert("Nickname",ui->lERgNickName->text()); - registration_info.insert("Password",ui->lERgPassword1->text()); - registration_info.insert("MsgType","Regist"); - sendregistdata(registration_info); - } - }); -} - -void UserRegister::registconfirm(QJsonObject data){ - bool legal = data["IsLegal"].toBool(); - if(legal){ - QMessageBox::information(this,"提示","注册成功"); - emit UserRegister::registfinished(); - this->close(); - } - else{ - QMessageBox::critical(this,"Error!","用户名已存在"); - } -} - -UserRegister::~UserRegister() -{ - delete ui; -} - -void UserRegister::on_lERgPassword1_returnPressed() -{ - emit ui->btnRegister->clicked(); -} +#include "userregister.h" +#include "ui_userregister.h" +#include +#include +#include +#include +#include + + +UserRegister::UserRegister(QWidget *parent) : + QWidget(parent), + ui(new Ui::UserRegister) +{ + ui->setupUi(this); + connect(ui->btnRegister,&QPushButton::clicked,[=](){ + bool islegal = true; + QJsonObject registration_info; + QString s = ui->lERgUserName->text(), s1; + if(s.size()==0){ + QMessageBox::critical(this,"Error!","用户名不能为空"); + islegal = false; + } + s = ui->lERgPassword1->text(); + s1 = ui->lERgPassword2->text(); + if((s.size()==0 || s1.size()==0) && islegal){ + QMessageBox::critical(this,"Error!","密码不能为空"); + islegal = false; + } + if(ui->lERgNickName->text().size()==0 && islegal){ + QMessageBox::information(this,"提示","请设置您的昵称"); + islegal = false; + } + if(s != s1 && islegal){ + QMessageBox::critical(this,"Error!","两次输入的密码不一致"); + islegal = false; + } + if(islegal){ + //发送注册信息 + registration_info.insert("Username",ui->lERgUserName->text()); + registration_info.insert("Nickname",ui->lERgNickName->text()); + registration_info.insert("Password",ui->lERgPassword1->text()); + registration_info.insert("MsgType","Regist"); + sendregistdata(registration_info); + } + }); +} + +void UserRegister::registconfirm(QJsonObject data){ + bool legal = data["IsLegal"].toBool(); + if(legal){ + QMessageBox::information(this,"提示","注册成功"); + emit UserRegister::registfinished(); + this->close(); + } + else{ + QMessageBox::critical(this,"Error!","用户名已存在"); + } +} + +UserRegister::~UserRegister() +{ + delete ui; +}