-
Notifications
You must be signed in to change notification settings - Fork 0
/
dropfilter.cpp
55 lines (49 loc) · 1.54 KB
/
dropfilter.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
// Copyright 2022-present Contributors to the eventfilter project.
// SPDX-License-Identifier: BSD-3-Clause
// https://github.com/mikaelsundell/eventfilter
#include "dropfilter.h"
#include <QDragEnterEvent>
#include <QFileInfo>
#include <QLabel>
#include <QMimeData>
#include <QDebug>
Dropfilter::Dropfilter(QObject* parent)
: QObject(parent)
{
}
Dropfilter::~Dropfilter()
{
}
bool
Dropfilter::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() && QFileInfo(urls.first().toLocalFile()).isDir()) {
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()) {
QString dirpath = urls.first().toLocalFile();
if (dirpath.endsWith("/")) {
dirpath.chop(1);
}
QLabel* label = qobject_cast<QLabel *>(obj);
if (label) {
label->setText(dirpath);
textChanged(dirpath);
dropEvent->acceptProposedAction();
return true;
}
}
return false;
}
return QObject::eventFilter(obj, event);
}