-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurlfilter.cpp
93 lines (86 loc) · 3.03 KB
/
urlfilter.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
// Copyright 2022-present Contributors to the jobman project.
// SPDX-License-Identifier: BSD-3-Clause
// https://github.com/mikaelsundell/jobman
#include "urlfilter.h"
#include <QDragEnterEvent>
#include <QFileInfo>
#include <QLabel>
#include <QLineEdit>
#include <QListWidget>
#include <QMimeData>
#include <QDebug>
Urlfilter::Urlfilter(QObject* parent)
: QObject(parent)
{
}
Urlfilter::~Urlfilter()
{
}
bool
Urlfilter::eventFilter(QObject* obj, QEvent* event)
{
if (event->type() == QEvent::DragEnter) {
QDragEnterEvent* dragEvent = static_cast<QDragEnterEvent *>(event);
if (dragEvent->mimeData()->hasUrls()) {
QList<QUrl> urls = dragEvent->mimeData()->urls();
if (!urls.isEmpty()) {
QString path = urls.first().toLocalFile();
QFileInfo fileInfo(path);
if (fileInfo.isDir() || fileInfo.isFile()) {
dragEvent->acceptProposedAction();
return true;
}
}
}
return false;
} else if (event->type() == QEvent::Drop) {
QDropEvent* dropEvent = static_cast<QDropEvent *>(event);
QList<QUrl> urls = dropEvent->mimeData()->urls();
if (!urls.isEmpty()) {
QUrl url = urls.first();
QString path = url.toLocalFile();
QFileInfo fileinfo(path);
QString dirpath;
if (fileinfo.isFile()) {
dirpath = fileinfo.absolutePath();
} else if (fileinfo.isDir()) {
dirpath = fileinfo.absoluteFilePath();
}
if (dirpath.endsWith('\\') || dirpath.endsWith('/')) {
dirpath.chop(1);
}
if (!dirpath.isEmpty()) {
bool found = false;
if (QListWidget* listWidget = qobject_cast<QListWidget*>(obj)) {
bool exists = false;
for (int i = 0; i < listWidget->count(); ++i) {
if (listWidget->item(i)->text() == dirpath) {
exists = true;
break;
}
}
if (!exists) {
listWidget->addItem(dirpath);
found = true;
}
}
else if (QLabel* label = qobject_cast<QLabel*>(obj)) {
QFontMetrics metrics(label->font());
label->setText(metrics.elidedText(dirpath, Qt::ElideRight, label->maximumSize().width()));
found = true;
}
else if (QLineEdit* lineEdit = qobject_cast<QLineEdit*>(obj)) {
lineEdit->setText(dirpath);
found = true;
}
if (found) {
dropEvent->acceptProposedAction();
urlChanged(url);
return true;
}
}
}
return false;
}
return QObject::eventFilter(obj, event);
}