mirror of
https://gitee.com/Zhaoxin59/my-chat_-client.git
synced 2026-02-14 00:51:48 +08:00
has been completed.
This commit is contained in:
@ -1,5 +1,9 @@
|
||||
#include "choosefrienddialog.h"
|
||||
|
||||
#include "model/datacenter.h"
|
||||
#include "toast.h"
|
||||
|
||||
using namespace model;
|
||||
|
||||
////////////////////////////////////////////////
|
||||
/// 选择好友窗口中的一个 元素/好友项
|
||||
@ -85,8 +89,9 @@ void ChooseFriendItem::leaveEvent(QEvent* event)
|
||||
this->update();
|
||||
}
|
||||
|
||||
ChooseFriendDialog::ChooseFriendDialog(QWidget *parent)
|
||||
: QDialog(parent)
|
||||
ChooseFriendDialog::ChooseFriendDialog(QWidget* parent, const QString& userId)
|
||||
: QDialog(parent),
|
||||
userId(userId)
|
||||
{
|
||||
// 1. 设置窗口的基本属性
|
||||
this->setWindowTitle("选择好友");
|
||||
@ -107,7 +112,8 @@ ChooseFriendDialog::ChooseFriendDialog(QWidget *parent)
|
||||
// 4. 针对右侧窗口进行初始化
|
||||
initRight(layout);
|
||||
|
||||
|
||||
//加载数据到窗口
|
||||
initData();
|
||||
}
|
||||
|
||||
void ChooseFriendDialog::initLeft(QHBoxLayout* layout)
|
||||
@ -213,14 +219,75 @@ void ChooseFriendDialog::initRight(QHBoxLayout* layout)
|
||||
}
|
||||
#endif
|
||||
|
||||
//// 8. 添加信号槽, 处理 ok 和 cancel 的点击
|
||||
//connect(okBtn, &QPushButton::clicked, this, &ChooseFriendDialog::clickOkBtn);
|
||||
//connect(cancelBtn, &QPushButton::clicked, this, [=]() {
|
||||
// this->close();
|
||||
// });
|
||||
// 8. 添加信号槽, 处理 ok 和 cancel 的点击
|
||||
connect(okBtn, &QPushButton::clicked, this, &ChooseFriendDialog::clickOkBtn);
|
||||
connect(cancelBtn, &QPushButton::clicked, this, [=]() {
|
||||
this->close();
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
void ChooseFriendDialog::clickOkBtn()
|
||||
{
|
||||
//根据选中好友列表中的元素,得到所有的要创建群聊会话的用户id列表
|
||||
QList<QString> userIdList = generateMemberList();
|
||||
if (userIdList.size() < 3) {
|
||||
Toast::showMessage("群聊中的成员不足三个,无法创建群聊");
|
||||
return;
|
||||
}
|
||||
|
||||
//发送网络请求,创建群聊
|
||||
DataCenter* dataCenter = DataCenter::getInstance();
|
||||
dataCenter->createGroupChatSessionAsync(userIdList);
|
||||
|
||||
//关闭当前窗口
|
||||
this->close();
|
||||
}
|
||||
|
||||
QList<QString> ChooseFriendDialog::generateMemberList()
|
||||
{
|
||||
QList<QString> result;
|
||||
|
||||
//把自己添加到结果中
|
||||
DataCenter* dataCenter = DataCenter::getInstance();
|
||||
if (dataCenter->getMyselfsync() == nullptr) {
|
||||
LOG() << "个人信息尚未加载...";
|
||||
return result;
|
||||
}
|
||||
result.push_back(dataCenter->getMyselfsync()->userId);
|
||||
|
||||
//遍历选中的列表
|
||||
QVBoxLayout* layout = dynamic_cast<QVBoxLayout*>(selectedContainer->layout());
|
||||
for (int i = 0; i < layout->count(); ++i) {
|
||||
auto* item = layout->itemAt(i);
|
||||
if (item == nullptr || item->widget() == nullptr) {
|
||||
continue;
|
||||
}
|
||||
auto* chooseFriendItem = dynamic_cast<ChooseFriendItem*>(item->widget());
|
||||
result.push_back(chooseFriendItem->getUserId());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
void ChooseFriendDialog::initData()
|
||||
{
|
||||
//遍历好友列表,把好友列表中的元素添加到这个窗口界面上
|
||||
DataCenter* dataCenter = DataCenter::getInstance();
|
||||
QList<UserInfo>* friendList = dataCenter->getFriendList();
|
||||
if (friendList == nullptr) {
|
||||
LOG() << "加载数据时发现好友列表为空";
|
||||
return;
|
||||
}
|
||||
for (auto it = friendList->begin(); it != friendList->end(); ++it) {
|
||||
if (it->userId == this->userId) {
|
||||
this->addSelectedFriend(it->userId, it->avatar, it->nickname);
|
||||
this->addFriend(it->userId, it->avatar, it->nickname, true);
|
||||
}
|
||||
this->addFriend(it->userId, it->avatar, it->nickname, false);
|
||||
}
|
||||
}
|
||||
|
||||
void ChooseFriendDialog::addFriend(const QString& userId, const QIcon& avatar, const QString& name, bool checked)
|
||||
{
|
||||
ChooseFriendItem* item = new ChooseFriendItem(this, userId, avatar, name, checked);
|
||||
@ -271,4 +338,4 @@ void ChooseFriendDialog::deleteSelectedFriend(const QString& userId)
|
||||
//已找到,取消勾选状态
|
||||
chooseFriendItem->getCheckBox()->setChecked(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user