mirror of
https://gitee.com/Zhaoxin59/my-chat_-client.git
synced 2026-02-14 00:51:48 +08:00
add sendMessage function
This commit is contained in:
@ -3,12 +3,15 @@
|
||||
|
||||
#include "debug.h"
|
||||
#include "model/data.h"
|
||||
#include "model/datacenter.h"
|
||||
#include "mainwidget.h"
|
||||
|
||||
using namespace model;
|
||||
|
||||
SessionFriendArea::SessionFriendArea(QWidget *parent)
|
||||
: QScrollArea {parent}
|
||||
{
|
||||
//设置必要属性
|
||||
//设置必要属性
|
||||
//设置这个才能有滚动效果
|
||||
this->setWidgetResizable(true);
|
||||
//设置滚动条相关样式
|
||||
@ -114,9 +117,9 @@ SessionFriendItem::SessionFriendItem(QWidget* owner, const QIcon& avatar, const
|
||||
nameLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
||||
|
||||
//创建消息预览的label
|
||||
QLabel* messageLabel = new QLabel();
|
||||
messageLabel = new QLabel();
|
||||
messageLabel->setText(text);
|
||||
messageLabel->setStyleSheet("QLabel { font-size: 18px; font-weight: 600; }");
|
||||
messageLabel->setStyleSheet("QLabel { font-size: 12px; font-weight: 600; color: rgb(153, 153, 153); }");
|
||||
messageLabel->setFixedHeight(35);
|
||||
messageLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
||||
|
||||
@ -255,7 +258,6 @@ void SessionFriendArea::clickItem(int index)
|
||||
item->select();
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////
|
||||
/// 会话Item的实现
|
||||
////////////////////////////////////////
|
||||
@ -263,7 +265,52 @@ SessionItem::SessionItem(QWidget *owner, const QString &chatSessionId, const QIc
|
||||
const QString &name, const QString &lastmessage)
|
||||
:SessionFriendItem(owner, avatar, name, lastmessage), chatSessionId(chatSessionId)
|
||||
{
|
||||
//处理更新最后一个消息的信号
|
||||
DataCenter* dataCenter = DataCenter::getInstance();
|
||||
connect(dataCenter, &DataCenter::updateLastMessage, this, &SessionItem::updateLastMessage);
|
||||
}
|
||||
|
||||
void SessionItem::updateLastMessage(const QString& chatSessionId)
|
||||
{
|
||||
DataCenter* dataCenter = DataCenter::getInstance();
|
||||
//先判定,信号中的会话id得和当前元素自身持有放到会话id一致,才真正处理
|
||||
if (this->chatSessionId != chatSessionId) {
|
||||
//当前SessionItem不是正在发消息的SessionItem
|
||||
return;
|
||||
}
|
||||
//chatSession匹配,真正更新最后一条消息
|
||||
|
||||
//把最后一条消息获取到
|
||||
QList<Message>* messageList = dataCenter->getRecentMessageList(chatSessionId);
|
||||
if (messageList == nullptr || messageList->size() == 0) {
|
||||
//当前会话没有任何消息,无需更新
|
||||
return;
|
||||
}
|
||||
const Message& lastMessage = messageList->back();
|
||||
|
||||
//明确显示的文本内容
|
||||
QString text;
|
||||
if (lastMessage.messageType == TEXT_TYPE) {
|
||||
text = lastMessage.content;
|
||||
}
|
||||
else if (lastMessage.messageType == IMAGE_TYPE) {
|
||||
text = "[图片]";
|
||||
}
|
||||
else if (lastMessage.messageType == FILE_TYPE) {
|
||||
text = "[文件]";
|
||||
}
|
||||
else if (lastMessage.messageType == SPEECH_TYPE) {
|
||||
text = "[语音]";
|
||||
}
|
||||
else {
|
||||
LOG() << "错误的消息类型";
|
||||
return;
|
||||
}
|
||||
|
||||
//把这个内容显示到界面上
|
||||
//后续还要考虑到“未读消息”情况,
|
||||
//TODO
|
||||
this->messageLabel->setText(text);
|
||||
}
|
||||
|
||||
void SessionItem::active()
|
||||
@ -290,6 +337,9 @@ void FriendItem::active()
|
||||
{
|
||||
//点击之后,要激活对应的会话列元素
|
||||
LOG() << "click FriendItem... userId: " << userId;
|
||||
|
||||
MainWidget* mainWidget = MainWidget::getInstance();
|
||||
mainWidget->switchSession(userId);
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
Reference in New Issue
Block a user