forked from CPP-Final-Project/Chat_Client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatWModel.cpp
88 lines (73 loc) · 1.98 KB
/
ChatWModel.cpp
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
#include "ChatWModel.h"
ChatWModel::ChatWModel(QObject* parent)
: QAbstractListModel(parent)
{}
ChatWModel::~ChatWModel()
{
clear();
}
void ChatWModel::clear()
{
beginResetModel();
model_chats.clear();
endResetModel();
}
void ChatWModel::addChat(const QVariant& new_chat_var_)
{
if (const auto msg = new_chat_var_.value<chatItemPtr>(); msg)
{
beginInsertRows(QModelIndex(), static_cast<int>(model_chats.size()), static_cast<int>(model_chats.size()));
model_chats.emplaceBack(msg);
endInsertRows();
}
}
void ChatWModel::addChats(const QVariantList& new_chat_list_)
{
if (!new_chat_list_.isEmpty())
{
beginInsertRows(QModelIndex(), static_cast<int>(model_chats.size()), static_cast<int>(model_chats.size() + new_chat_list_.size() - 1));
for (auto const& chat_var : new_chat_list_)
{
if (const auto msg = chat_var.value<chatItemPtr>(); msg)
{
model_chats.emplaceBack(msg);
}
}
endInsertRows();
}
}
bool ChatWModel::hashChat(const std::bitset<128>& hash) const
{
return model_hash_to_chat.contains(hash);
}
chatItemPtr ChatWModel::getChatFromId(const int id) const
{
return model_id_to_chat.value(id);
}
int ChatWModel::rowCount(const QModelIndex& parent) const
{
Q_UNUSED(parent)
return static_cast<int>(model_chats.size());
}
QVariant ChatWModel::data(const QModelIndex& index, int role) const
{
if (auto const chat = (index.isValid() && index.row() < rowCount()) ? model_chats.at(index.row()) : nullptr)
{
switch (role)
{
case Qt::DisplayRole:
return chat->getChatRoomName();
case Qt::SizeHintRole:
return QSize{ 200, 200 };
case ChatRole:
return QVariant::fromValue(chat);
default:
break;
}
}
return {};
}
Qt::ItemFlags ChatWModel::flags(const QModelIndex& index) const
{
return QAbstractListModel::flags(index);
}