-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchatmessagedelegate.cpp
210 lines (188 loc) · 6.55 KB
/
chatmessagedelegate.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "chatmessagedelegate.h"
#include "chatmessageitem.h"
#include "imessageform.h"
#include "inmessageform.h"
#include "outmessageform.h"
#include <QPainter>
#include <QPushButton>
#include <QDebug>
#include "chatmessageview.h"
#include <QApplication>
#include <QLabel>
#include <QHBoxLayout>
#include "message.h"
ChatMessageDelegate::ChatMessageDelegate(QObject *parent) :
QItemDelegate(parent)
{
m_parent = (ChatMessageView* )parent;
connect(&m_timerCheck, SIGNAL(timeout()), this, SLOT(timerCheck()));
m_timerCheck.start(1000);
setClipping(false);
m_createInRealTime = false;
//m_createInRealTime = true;
}
void ChatMessageDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
//qDebug() << "ChatMessageDelegate::paint" << option.rect;
//drawDecoration(painter, option, option.rect, QPixmap(":/picture/pic/back.jpg"));
if (index.data(ChatMessageItem::DATA_ROLE_MESSAGE).canConvert<Message>())
{
if (m_parent)
{
m_parent->openPersistentEditor(index);
painter->drawLine(option.rect.x(), option.rect.y(), option.rect.x()+option.rect.width(), option.rect.y());
//qDebug() << "openPersistentEditor index row: " << index.row();
//m_mapIndexClock[index] = 0;
if(!m_createInRealTime)
{
return;
}
QRect parentRect = m_parent->rect();
int viewStartHeight = parentRect.y();
int viewEndHeight = parentRect.y() + parentRect.height();
for(int row = 0; row < m_parent->model()->rowCount(); row++)
{
QModelIndex idx = m_parent->model()->index(row, 0);
QRect rc = m_parent->visualRect(idx);
int indexStartHeight = rc.y();
if ( indexStartHeight <= viewStartHeight - rc.height()
|| indexStartHeight >= viewEndHeight + rc.height() )
{
//if(m_mapIndexClock[idx] >= 10)
{
m_parent->closePersistentEditor(idx);
//m_mapIndexClock.remove(idx);
m_mapEditor[idx].editor = nullptr;
//qDebug() << "closePersistentEditor index row: " << idx.row();
}
}
}
}
}
else
{
QItemDelegate::paint(painter, option, index);
}
}
QSize ChatMessageDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if ( index.isValid() && index.data(ChatMessageItem::DATA_ROLE_MESSAGE).canConvert<Message>() )
{
if ( index.data(ChatMessageItem::DATA_ROLE_MESSAGE).canConvert<Message>() )
{
if (m_mapEditor.contains(index))
{
if (m_mapEditor[index].editor != nullptr)
{
auto sz = m_mapEditor[index].editor->sizeHint();
//sz.setWidth(m_parent->width()-50);
m_mapEditor[index].lastSize = sz;
//auto msg = index.data(ChatMessageItem::DATA_ROLE_MESSAGE).value<Message>();
//qDebug() << "ChatMessageDelegate::sizeHint " << sz;// << "msg:" << msg.items().at(0).data;
return sz;
}
else
{
return m_mapEditor[index].lastSize;
}
}
}
}
return QItemDelegate::sizeHint(option, index);
}
QWidget *ChatMessageDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
//qDebug() << "ChatMessageDelegate::createEditor";
if ( index.data(ChatMessageItem::DATA_ROLE_MESSAGE).canConvert<Message>() )
{
auto msg = index.data(ChatMessageItem::DATA_ROLE_MESSAGE).value<Message>();
QWidget *editor = nullptr;
if(msg.direction() == Message::MessageIn)
{
editor = new InMessageForm(parent);
}
else if(msg.direction() == Message::MessageOut)
{
editor = new OutMessageForm(parent);
}
m_mapEditor[index].editor = editor;
m_mapIndexClock[index] = 0;
return editor;
}
return QItemDelegate::createEditor(parent, option, index);
}
void ChatMessageDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
//qDebug() << "ChatMessageDelegate::setEditorData";
if ( index.data(ChatMessageItem::DATA_ROLE_MESSAGE).canConvert<Message>() )
{
auto msg = index.data(ChatMessageItem::DATA_ROLE_MESSAGE).value<Message>();
if(msg.direction() == Message::MessageIn)
{
auto widget = qobject_cast<InMessageForm*>(editor);
if (widget)
{
widget->setMessage(msg);
}
}
else
{
auto widget = qobject_cast<OutMessageForm*>(editor);
if (widget)
{
widget->setMessage(msg);
}
}
}
else
{
QItemDelegate::setEditorData(editor, index);
}
}
void ChatMessageDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
//qDebug() << "ChatMessageDelegate::setModelData";
if ( index.data(ChatMessageItem::DATA_ROLE_MESSAGE).canConvert<Message>() )
{
auto msg = index.data(ChatMessageItem::DATA_ROLE_MESSAGE).value<Message>();
if(msg.direction() == Message::MessageIn)
{
auto widget = qobject_cast<InMessageForm*>(editor);
if (widget)
{
model->setData(index, QVariant::fromValue<Message>(widget->message()));
}
}
else
{
auto widget = qobject_cast<OutMessageForm*>(editor);
if (widget)
{
model->setData(index, QVariant::fromValue<Message>(widget->message()));
}
}
}
else
{
QItemDelegate::setModelData(editor, model, index);
}
}
bool ChatMessageDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
QItemDelegate::editorEvent(event, model, option, index);
}
void ChatMessageDelegate::timerCheck()
{
// foreach (QModelIndex index, m_mapIndexClock.keys())
// {
// m_mapIndexClock[index]++;
// }
}
bool ChatMessageDelegate::createInRealTime() const
{
return m_createInRealTime;
}
void ChatMessageDelegate::setCreateInRealTime(bool createInRealTime)
{
m_createInRealTime = createInRealTime;
}