forked from CPP-Final-Project/Chat_Client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatWView.cpp
113 lines (100 loc) · 2.86 KB
/
ChatWView.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "ChatWView.h"
ChatWView::ChatWView(QWidget* parent)
: QListView(parent)
{
setFont(CHAT_FONT);
QTimer::singleShot(100, [this]
{
const auto model = new ChatWModel(this);
setModel(model);
setItemDelegate(new ChatWDelegate(this));
connect(model, &ChatWModel::rowsInserted, this, &ChatWView::onRowsInserted);
}
);
// important !!!
setResizeMode(Adjust);
setContextMenuPolicy(Qt::ActionsContextMenu);
setSelectionMode(QAbstractItemView::SingleSelection);
setSpacing(0);
setMouseTracking(true);
setStyleSheet(VIEW_CHAT_STYLE_SHEET);
connect(this, &ChatWView::customContextMenuRequested, this, &ChatWView::onCustomContextMenuRequested);
}
ChatWView::~ChatWView() = default;
void ChatWView::onChatAdded(const QVariant& new_msg) const
{
const auto model = qobject_cast<ChatWModel*>(this->model());
if (!model)
return;
model->addChat(new_msg);
}
void ChatWView::onChatsAdded(const QVariantList& msg_list) const
{
const auto model = qobject_cast<ChatWModel*>(this->model());
if (!model)
return;
model->addChats(msg_list);
}
void ChatWView::onCustomContextMenuRequested(const QPoint& pos)
{
QModelIndexList indexes = selectionModel()->selectedIndexes();
if (!indexes.isEmpty())
{
auto chat = indexes.front().data(ChatWModel::ChatRole).value<chatItemPtr>();
QMenu menu;
menu.addAction("Action...", [this, chat]
{
//QMessageBox::information(this, "Action", QString("Action for message: %1").arg(msg->get_msg_id()));
});
menu.exec(mapToGlobal(pos));
}
}
void ChatWView::onRowsInserted(const QModelIndex& parent, int first, int last)
{
ensurePolished();
scrollToBottom();
}
void ChatWView::mouseMoveEvent(QMouseEvent* event)
{
if (const auto& model = qobject_cast<ChatWModel*>(this->model()))
{
auto const& item_index = indexAt(event->pos());
if (_last_index != item_index)
{
if (_last_index.isValid())
{
if (const auto& chat = _last_index.data(ChatWModel::ChatRole).value<chatItemPtr>())
{
chat->setChatRoomCurrRow(_last_index.row());
chat->setIsHovered(false);
emit model->dataChanged(_last_index, _last_index);
}
}
}
_last_index = item_index;
if (_last_index.isValid())
{
const auto msg = _last_index.data(ChatWModel::ChatRole).value<chatItemPtr>();
msg->setIsHovered(true);
emit model->dataChanged(_last_index, _last_index);
}
}
QListView::mouseMoveEvent(event);
}
void ChatWView::mouseDoubleClickEvent(QMouseEvent* event)
{
if (const auto& model = qobject_cast<ChatWModel*>(this->model()))
{
auto const& item_index = indexAt(event->pos());
if (item_index.isValid())
{
const auto& chat = item_index.data(ChatWModel::ChatRole).value< chatItemPtr>();
const auto& chat_rects = chat->getChatRoomCurrBox();
if (chat_rects.contains(event->pos()))
{
Q_EMIT chatClicked(chat->getChatId(), chat->getChatRoomName());
}
}
}
QListView::mouseDoubleClickEvent(event);
}