-
Notifications
You must be signed in to change notification settings - Fork 0
/
filedrop.cpp
112 lines (99 loc) · 2.34 KB
/
filedrop.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
// Copyright 2022-present Contributors to the jobman project.
// SPDX-License-Identifier: BSD-3-Clause
// https://github.com/mikaelsundell/jobman
#include "filedrop.h"
#include <QFileInfo>
#include <QLabel>
#include <QVBoxLayout>
#include <QDragEnterEvent>
#include <QMimeData>
#include <QPointer>
#include <QStyle>
class FiledropPrivate : public QObject
{
Q_OBJECT
public:
FiledropPrivate();
void init();
void update();
public:
QList<QString> files;
QPointer<QLabel> label;
QPointer<Filedrop> widget;
};
FiledropPrivate::FiledropPrivate()
{
}
void
FiledropPrivate::init()
{
widget->setAcceptDrops(true);
widget->setAttribute(Qt::WA_StyledBackground, true); // needed for stylesheet
// layout
QVBoxLayout* layout = new QVBoxLayout(widget);
label = new QLabel(widget);
QPixmap pixmap(":/icons/resources/Arrow.png");
label->setPixmap(pixmap.scaled(64, 64, Qt::KeepAspectRatio, Qt::SmoothTransformation));
label->setFixedSize(64, 64);
layout->addWidget(label, 0, Qt::AlignHCenter);
}
void
FiledropPrivate::update()
{
widget->style()->unpolish(widget);
widget->style()->polish(widget);
widget->update();
}
#include "filedrop.moc"
Filedrop::Filedrop(QWidget* parent)
: QWidget(parent)
, p(new FiledropPrivate())
{
p->widget = this;
p->init();
}
Filedrop::~Filedrop()
{
}
void
Filedrop::dragEnterEvent(QDragEnterEvent* event)
{
Q_UNUSED(event);
if (event->mimeData()->hasUrls()) {
p->files.clear();
bool found = false;
QList<QUrl> urls = event->mimeData()->urls();
for (const QUrl &url : urls) {
QFileInfo fileinfo(url.toLocalFile());
if (fileinfo.isFile()) {
p->files.append(fileinfo.filePath());
found = true;
}
}
if (found) {
event->acceptProposedAction();
setProperty("dragging", true);
p->update();
} else {
event->ignore();
}
} else {
event->ignore();
}
}
void
Filedrop::dragLeaveEvent(QDragLeaveEvent* event)
{
Q_UNUSED(event);
setProperty("dragging", false);
p->update();
}
void
Filedrop::dropEvent(QDropEvent* event)
{
Q_UNUSED(event);
setProperty("dragging", false);
filesDropped(p->files);
p->update();
event->accept();
}