-
Notifications
You must be signed in to change notification settings - Fork 88
/
recvtextedit.cpp
228 lines (194 loc) · 5.59 KB
/
recvtextedit.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "recvtextedit.h"
#include <QDate>
#include "emoji.h"
#include <QMouseEvent>
RecvTextEdit::RecvTextEdit(QWidget *parent)
:QTextEdit(parent)
{
setTextInteractionFlags(Qt::LinksAccessibleByMouse|Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard);
}
void RecvTextEdit::mousePressEvent(QMouseEvent *e)
{
mPressedAnchor = (e->button() & Qt::LeftButton) ? anchorAt(e->pos()) : "";
QTextEdit::mousePressEvent(e);
}
void RecvTextEdit::mouseReleaseEvent(QMouseEvent *e)
{
if (e->button() & Qt::LeftButton)
{
if (anchorAt(e->pos()) == mPressedAnchor && !mPressedAnchor.isEmpty())
parseLink(mPressedAnchor);
}
QTextEdit::mouseReleaseEvent(e);
}
void RecvTextEdit::addFellowContent(const Content *content, long long msSinceEpoch)
{
addContent(content, msSinceEpoch, false);
}
void RecvTextEdit::addMyContent(const Content *content, long long msSinceEpoch)
{
addContent(content, msSinceEpoch, true);
}
void RecvTextEdit::addContent(const Content *content, long long msSinceEpoch, bool mySelf)
{
drawDaySeperatorIfNewDay(msSinceEpoch);
showHint(msSinceEpoch, mySelf);
showContent(content, mySelf);
append("\n");
moveCursor(QTextCursor::End);
}
void RecvTextEdit::showHint(long long msSinceEpoch, bool mySelf)
{
QString name("");
QString color("black");
if (mySelf)
{
name = "我";
color = "blue";
}
else
{
name = mFellow == nullptr ? "匿名" : mFellow->getName().c_str();
color = "green";
}
QString hint = "<font color="+color+">"+ name+" "+timeStr(msSinceEpoch)+"</font>";
moveCursor(QTextCursor::End);
insertHtml(hint);
append("");
}
void RecvTextEdit::setCurFellow(const Fellow *fellow)
{
if (mFellow)
mDocs[mFellow] = document()->clone();//document将被清除或删除了,需clone
auto it = mDocs.find(fellow);
if (it != mDocs.end())
{
setDocument((*it).second);
moveCursor(QTextCursor::End);
}
else
{
clear();
}
mFellow = fellow;
}
void RecvTextEdit::addWarning(const QString &warning)
{
auto align = alignment();
setAlignment(Qt::AlignCenter);
auto color = textColor();
setTextColor(QColor(128,128,128));
append(warning);
append("");//结束当前段落,否则下一行恢复对齐方式时会将刚append的内容左对齐
setAlignment(align);
setTextColor(color);
}
const Fellow *RecvTextEdit::curFellow()
{
return mFellow;
}
void RecvTextEdit::parseLink(const QString &link)
{
QStringList parts = link.split("_");
if (parts.count()<3)
return;
auto packetNo = parts.at(0).toLongLong();
auto fileId = parts.at(1).toLongLong();
bool upload = parts.at(2) == "up";
emit navigateToFileTask(packetNo, fileId, upload);
}
QString RecvTextEdit::timeStr(long long msSinceEpoch)
{
QDateTime time;
time.setMSecsSinceEpoch(msSinceEpoch);
return time.toString("MM-dd HH:mm:ss");
}
void RecvTextEdit::showContent(const Content *content, bool mySelf)
{
switch (content->type())
{
case ContentType::File:
showFile(static_cast<const FileContent*>(content), mySelf);
break;
case ContentType::Knock:
showKnock(static_cast<const KnockContent*>(content), mySelf);
break;
case ContentType::Image:
showImage(static_cast<const ImageContent*>(content));
break;
case ContentType::Text:
showText(static_cast<const TextContent*>(content));
break;
default:
showUnSupport();
break;
}
}
void RecvTextEdit::showFile(const FileContent *content, bool fromMySelf)
{
if (content->fileType == IPMSG_FILE_REGULAR)
{
stringstream ss;
ss<<"<a href="<<content->packetNo<<"_"<<content->fileId<<"_"<<(fromMySelf?"up":"down")<<">"
<<content->filename<<"("<<content->size<<")"
<<"</a>";
insertHtml(ss.str().c_str());
}
else
{
showUnSupport("对方发来非普通文件(可能是文件夹),收不来……");
}
}
void RecvTextEdit::showImage(const ImageContent *content)
{
showUnSupport("对方发来图片,来图片,图片,片……额~还不支持!");
}
void RecvTextEdit::showText(const TextContent *content)
{
insertHtml(textHtmlStr(content));
}
void RecvTextEdit::showKnock(const KnockContent *content, bool mySelf)
{
if (mySelf)
insertHtml("[发送了一个窗口抖动]");
else
insertHtml("[发来窗口抖动]");
}
void RecvTextEdit::showUnSupport(const QString& text)
{
QString t = text;
if (t.isEmpty())
t = "对方发来尚未支持的内容,无法显示";
insertHtml("<font color=\"red\">"+t+"</font>");
}
void RecvTextEdit::drawDaySeperatorIfNewDay(long long sinceEpoch)
{
QDateTime cur;
cur.setMSecsSinceEpoch(sinceEpoch);
if (mLastEdit > 0)
{
QDateTime last;
last.setMSecsSinceEpoch(mLastEdit);
if (last.daysTo(cur)>0)
{
addWarning("-----------------------------");
}
}
mLastEdit = sinceEpoch;
}
QString RecvTextEdit::textHtmlStr(const TextContent *content)
{
auto str = QString(content->text.c_str());
auto htmlStr = str.toHtmlEscaped();
htmlStr.replace("\r\n", "<br>");
htmlStr.replace("\r", "<br>");
htmlStr.replace("\n", "<br>");
for (auto i = 0; i < EMOJI_LEN; i++)
{
auto resName = QString(":/default/res/face/")+QString::number(i+1)+".gif";
auto emojiStr = QString(g_emojis[i]).toHtmlEscaped();
QString imgTag = "<img src=\""+resName+"\"/>";
htmlStr.replace(emojiStr, imgTag);
}
return htmlStr;
}