-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsendtextedit.cpp
More file actions
executable file
·140 lines (126 loc) · 3.47 KB
/
Copy pathsendtextedit.cpp
File metadata and controls
executable file
·140 lines (126 loc) · 3.47 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
#include "sendtextedit.h"
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QMimeData>
#include <QFileInfo>
#include <QDebug>
#include <QFileIconProvider>
SendTextEdit::SendTextEdit(QWidget *parent)
: QTextEdit(parent)
{
setAcceptDrops(true);
installEventFilter(this);
}
void SendTextEdit::newLine()
{
append("");
}
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();
for (auto url : urls)
{
QString localFile = url.toLocalFile();
QFileInfo fileInfo(localFile);
// 检查是否是图片文件
QString suffix = fileInfo.suffix().toLower();
if (suffix == "jpg" || suffix == "jpeg" || suffix == "png" || suffix == "gif" || suffix == "bmp")
{
// 处理图片文件拖拽
QImage image(localFile);
if (!image.isNull())
{
QTextCursor cursor = textCursor();
QTextDocument *doc = document();
doc->addResource(QTextDocument::ImageResource, url, image);
cursor.insertImage(url.toString());
mImages.append(fileInfo);
}
}
else
{
//显示文件图标和文件名
QFileIconProvider iconProvider;
QIcon icon = iconProvider.icon(fileInfo);
QPixmap pixmap = icon.pixmap(32, 32);
QTextCursor cursor = textCursor();
QTextDocument *doc = document();
doc->addResource(QTextDocument::ImageResource, url, pixmap);
// 插入文件图标和文件名
cursor.insertImage(url.toString());
cursor.insertText(" " + fileInfo.fileName());
cursor.insertText("\n");
// 处理其他文件
mFiles.append(fileInfo);
}
}
if (!mImages.isEmpty() || !mFiles.isEmpty())
{
e->accept();
}
}
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;
auto ctrl = keyEvent->modifiers() == Qt::ControlModifier;
if (enter && ctrl)
{
if(!tryEmitMimeFileSignal())
emit ctrlEnterPressed();
return true;
}
else if (enter)
{
if(!tryEmitMimeFileSignal())
emit enterPressed();
return true;
}
}
return false;
}
bool SendTextEdit::tryEmitMimeFileSignal()
{
bool hasFiles = !mImages.isEmpty() || !mFiles.isEmpty();
if (!mImages.isEmpty())
{
emit acceptDropImages(mImages);
mImages.clear();
clear();
}
if (!mFiles.isEmpty())
{
emit acceptDropFiles(mFiles);
mFiles.clear();
clear();
}
return hasFiles;
}