mirror of
https://gitee.com/Zhaoxin59/my-chat_-client.git
synced 2026-02-14 00:51:48 +08:00
Add a taxonomy
This commit is contained in:
@ -71,7 +71,7 @@ SessionFriendArea::SessionFriendArea(QWidget *parent)
|
||||
#if TEST_UI
|
||||
QIcon icon(":/resource/image/defaultAv.png");
|
||||
for(int i = 0; i < 30; i++) {
|
||||
this->addItem(icon, "张三" + QString::number(i), "test: last message..." + QString::number(i));
|
||||
this->addItem(SessionItemType, QString::number(i), icon, "张三" + QString::number(i), "test: last message..." + QString::number(i));
|
||||
}
|
||||
// LOG() << "hello world!";
|
||||
#endif
|
||||
@ -195,12 +195,78 @@ void SessionFriendArea::clear()
|
||||
}
|
||||
}
|
||||
|
||||
void SessionFriendArea::addItem(const QIcon &avatar, const QString &name, const QString &text)
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//此时这个函数添加的就不是SessionFriendItem了,而是其子类
|
||||
//SessionItem FriendItem ApplyItem
|
||||
void SessionFriendArea::addItem(ItemType itemType, const QString& id, const QIcon &avatar, const QString &name, const QString &text)
|
||||
{
|
||||
//构造一个Item
|
||||
SessionFriendItem* Item = new SessionFriendItem(this, avatar, name, text);
|
||||
//将Item添加到container中
|
||||
container->layout()->addWidget(Item);
|
||||
// //构造一个Item
|
||||
// SessionFriendItem* Item = new SessionFriendItem(this, avatar, name, text);
|
||||
// //将Item添加到container中
|
||||
// container->layout()->addWidget(Item);
|
||||
|
||||
SessionFriendItem* item = nullptr;
|
||||
switch (itemType) {
|
||||
case SessionItemType:
|
||||
item = new SessionItem(this, id, avatar, name, text);
|
||||
break;
|
||||
case FriendItemType:
|
||||
item = new FriendItem(this, id, avatar, name, text);
|
||||
break;
|
||||
case ApplyItemType:
|
||||
item = new ApplyItem(this, id, avatar, name);
|
||||
break;
|
||||
default:
|
||||
LOG() << "Error ItemType: " << itemType;
|
||||
return;
|
||||
}
|
||||
|
||||
container->layout()->addWidget(item);
|
||||
}
|
||||
|
||||
void SessionFriendArea::clickItem(int index)
|
||||
{
|
||||
if(index < 0 || index > container->layout()->count()) {
|
||||
LOG() << "The subscript of the clicked element is out of range... index:" << index;
|
||||
return;
|
||||
}
|
||||
QLayoutItem* layoutItem = container->layout()->itemAt(index);
|
||||
if(layoutItem == nullptr || layoutItem->widget() == nullptr) {
|
||||
LOG() << "The specified element does not exist... index: " << index;
|
||||
return;
|
||||
}
|
||||
SessionFriendItem* item = dynamic_cast<SessionFriendItem*>(layoutItem->widget());
|
||||
item->select();
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////
|
||||
/// 会话Item的实现
|
||||
////////////////////////////////////////
|
||||
SessionItem::SessionItem(QWidget *owner, const QString &chatSessionId, const QIcon &avatar,
|
||||
const QString &name, const QString &lastmessage)
|
||||
:SessionFriendItem(owner, avatar, name, lastmessage), chatSessionId(chatSessionId)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
/// 好友Item的实现
|
||||
////////////////////////////////////////
|
||||
FriendItem::FriendItem(QWidget *owner, const QString &userId, const QIcon &avatar,
|
||||
const QString &name, const QString &description)
|
||||
:SessionFriendItem(owner, avatar, name, description), userId(userId)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
/// 好友申请Item的实现
|
||||
////////////////////////////////////////
|
||||
ApplyItem::ApplyItem(QWidget *owner, const QString &userId, const QIcon &avatar,
|
||||
const QString &name)
|
||||
:SessionFriendItem(owner, avatar, name, ""), userId(userId)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@ -14,12 +14,23 @@
|
||||
#include <QStyleOption>
|
||||
#include <QPainter>
|
||||
#include <QEnterEvent>
|
||||
#include "model/data.h"
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////
|
||||
/// 滚动区域的Item的类型
|
||||
////////////////////////////////////////
|
||||
enum ItemType{
|
||||
SessionItemType,
|
||||
FriendItemType,
|
||||
ApplyItemType
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////////////
|
||||
/// 整个滚动区域的实现
|
||||
////////////////////////////////////////
|
||||
|
||||
class SessionFriendArea : public QScrollArea
|
||||
{
|
||||
Q_OBJECT
|
||||
@ -30,9 +41,10 @@ public:
|
||||
void clear();
|
||||
|
||||
//添加一个Item
|
||||
void addItem(const QIcon& avatar, const QString& name, const QString& text);
|
||||
void addItem(ItemType itemType, const QString& id, const QIcon& avatar, const QString& name, const QString& text);
|
||||
|
||||
//
|
||||
//选中某一个指定的item,通过index下标来访问item
|
||||
void clickItem(int index);
|
||||
|
||||
private:
|
||||
//后续向container中的layout添加元素,就会有QScrollArea的滚动
|
||||
@ -69,5 +81,43 @@ private:
|
||||
bool selected = false;
|
||||
};
|
||||
|
||||
////////////////////////////////////////
|
||||
/// 会话Item的实现
|
||||
////////////////////////////////////////
|
||||
class SessionItem : public SessionFriendItem {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
SessionItem(QWidget* owner, const QString& chatSessionId, const QIcon& avatar, const QString& name, const QString& lastmessage);
|
||||
private:
|
||||
//当前会话Id
|
||||
QString chatSessionId;
|
||||
|
||||
};
|
||||
|
||||
////////////////////////////////////////
|
||||
/// 好友Item的实现
|
||||
////////////////////////////////////////
|
||||
class FriendItem : public SessionFriendItem {
|
||||
Q_OBJECT
|
||||
public:
|
||||
FriendItem(QWidget* owner, const QString& userId, const QIcon& avatar, const QString& name, const QString& description);
|
||||
private:
|
||||
// 好友的用户Id
|
||||
QString userId;
|
||||
};
|
||||
|
||||
////////////////////////////////////////
|
||||
/// 好友申请Item的实现
|
||||
////////////////////////////////////////
|
||||
class ApplyItem : public SessionFriendItem {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
ApplyItem(QWidget* owner, const QString& userId, const QIcon& avatar, const QString& name);
|
||||
private:
|
||||
//好友申请Id
|
||||
QString userId;
|
||||
};
|
||||
|
||||
#endif // SESSIONFRIENDAREA_H
|
||||
|
||||
Reference in New Issue
Block a user