Repository navigation
Expand file tree
/
Copy pathsendtextedit.cpp
More file actions
258 lines (230 loc) · 6.32 KB
/
Copy pathsendtextedit.cpp
File metadata and controls
258 lines (230 loc) · 6.32 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include "sendtextedit.h"
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QMimeData>
#include <QFileInfo>
#include <QDebug>
#include <QDateTime>
#include <QDir>
#include <QImage>
#include <QImageReader>
#include <QMenu>
#include <QPixmap>
#include <QStandardPaths>
#include <QTextBlock>
#include <QTextFragment>
#include <QTextImageFormat>
#include "emoji.h"
SendTextEdit::SendTextEdit(QWidget *parent)
:QTextEdit(parent)
{
setAcceptDrops(true);
installEventFilter(this);
}
void SendTextEdit::newLine()
{
append("");
}
void SendTextEdit::insertEmoji(const QString& emoji)
{
auto index = emojiIndexOf(emoji);
if (index < 0)
{
insertPlainText(emoji);
return;
}
QTextImageFormat imageFormat;
imageFormat.setName(emojiResourceName(index));
textCursor().insertImage(imageFormat);
}
QString SendTextEdit::textWithEmojiCode() const
{
QString text;
auto doc = document();
for (auto block = doc->begin(); block != doc->end(); block = block.next())
{
if (block != doc->begin())
text.append("\n");
for (auto it = block.begin(); !it.atEnd(); ++it)
{
auto fragment = it.fragment();
if (!fragment.isValid())
continue;
auto format = fragment.charFormat();
if (format.isImageFormat())
{
auto imageName = format.toImageFormat().name();
for (auto i = 0; i < EMOJI_LEN; i++)
{
if (imageName == emojiResourceName(i))
{
text.append(g_emojis[i]);
break;
}
}
}
else
{
text.append(fragment.text());
}
}
}
return text;
}
void SendTextEdit::contextMenuEvent(QContextMenuEvent *e)
{
QMenu menu(this);
auto undoAction = menu.addAction("撤销");
auto redoAction = menu.addAction("重做");
menu.addSeparator();
auto cutAction = menu.addAction("剪切");
auto copyAction = menu.addAction("复制");
auto pasteAction = menu.addAction("粘贴");
auto deleteAction = menu.addAction("删除");
menu.addSeparator();
auto selectAllAction = menu.addAction("全选");
undoAction->setEnabled(document()->isUndoAvailable());
redoAction->setEnabled(document()->isRedoAvailable());
cutAction->setEnabled(textCursor().hasSelection());
copyAction->setEnabled(textCursor().hasSelection());
deleteAction->setEnabled(textCursor().hasSelection());
selectAllAction->setEnabled(!document()->isEmpty());
auto selected = menu.exec(e->globalPos());
if (selected == undoAction)
undo();
else if (selected == redoAction)
redo();
else if (selected == cutAction)
cut();
else if (selected == copyAction)
copy();
else if (selected == pasteAction)
paste();
else if (selected == deleteAction)
textCursor().removeSelectedText();
else if (selected == selectAllAction)
selectAll();
}
bool SendTextEdit::canInsertFromMimeData(const QMimeData *source) const
{
if (source->hasImage())
return true;
if (source->hasUrls())
{
for (auto url : source->urls())
{
if (isImageFile(url.toLocalFile()))
return true;
}
}
return QTextEdit::canInsertFromMimeData(source);
}
void SendTextEdit::insertFromMimeData(const QMimeData *source)
{
QList<QFileInfo> images;
if (source->hasImage())
{
auto data = source->imageData();
QImage image;
if (data.canConvert<QImage>())
image = qvariant_cast<QImage>(data);
else if (data.canConvert<QPixmap>())
image = qvariant_cast<QPixmap>(data).toImage();
auto path = saveClipboardImage(image);
if (!path.isEmpty())
images.append(QFileInfo(path));
}
else if (source->hasUrls())
{
for (auto url : source->urls())
{
auto path = url.toLocalFile();
if (isImageFile(path))
images.append(QFileInfo(path));
}
}
if (!images.isEmpty())
{
emit acceptPasteImages(images);
return;
}
QTextEdit::insertFromMimeData(source);
}
QString SendTextEdit::saveClipboardImage(const QImage &image) const
{
if (image.isNull())
return "";
auto dirPath = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
if (dirPath.isEmpty())
dirPath = QDir::home().filePath(".feiq_cache");
dirPath = QDir(dirPath).filePath("pasted_images");
QDir().mkpath(dirPath);
auto fileName = QString("paste_%1.png").arg(QDateTime::currentMSecsSinceEpoch());
auto path = QDir(dirPath).filePath(fileName);
if (!image.save(path, "PNG"))
return "";
return path;
}
bool SendTextEdit::isImageFile(const QString &path) const
{
if (path.isEmpty() || !QFileInfo(path).isFile())
return false;
return QImageReader(path).canRead();
}
void SendTextEdit::dragEnterEvent(QDragEnterEvent *e)
{
if (e->mimeData()->hasUrls())
{
auto urls = e->mimeData()->urls();
for (auto url : urls)
{
if (QFileInfo(url.toLocalFile()).isFile())
{
e->accept();
return;
}
}
}
else
{
QTextEdit::dragEnterEvent(e);
}
}
void SendTextEdit::dropEvent(QDropEvent *e)
{
if (e->mimeData()->hasUrls())
{
auto urls = e->mimeData()->urls();
QList<QFileInfo> files;
for (auto url : urls)
{
files.append(QFileInfo(url.toLocalFile()));
e->accept();
}
emit acceptDropFiles(files);
}
else
{
QTextEdit::dropEvent(e);
}
}
bool SendTextEdit::eventFilter(QObject *, QEvent * e)
{
if (e->type() == QEvent::KeyPress)
{
auto keyEvent = static_cast<QKeyEvent*>(e);
auto enter = keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter;
auto ctrl = keyEvent->modifiers() & Qt::ControlModifier;
if (enter && ctrl)
{
emit ctrlEnterPressed();
return true;
}
else if (enter)
{
emit enterPressed();
return true;
}
}
return false;
}