FOSSredder 0.5.0
Loading...
Searching...
No Matches
DropHandler.cpp
Go to the documentation of this file.
1
5
7
8#include <QDragEnterEvent>
9#include <QDragMoveEvent>
10#include <QDropEvent>
11#include <QEvent>
12#include <QMimeData>
13
14namespace ui::window {
15
16namespace {
17
18QStringList droppedLocalFiles(const QMimeData* mimeData) {
19 QStringList files;
20 if (!mimeData || !mimeData->hasUrls())
21 return files;
22
23 const auto urls = mimeData->urls();
24 for (const auto& url : urls) {
25 const QString localFile = url.toLocalFile();
26 if (!localFile.isEmpty())
27 files.push_back(localFile);
28 }
29 return files;
30}
31
32} // namespace
33
35 if (!event)
36 return {};
37
38 switch (event->type()) {
39 case QEvent::DragEnter: {
40 auto* dragEvent = static_cast<QDragEnterEvent*>(event);
41 const QStringList files = droppedLocalFiles(dragEvent->mimeData());
42 if (!files.isEmpty())
43 dragEvent->acceptProposedAction();
44 else
45 dragEvent->ignore();
46 return {true, !files.isEmpty(), files};
47 }
48 case QEvent::DragMove: {
49 auto* dragEvent = static_cast<QDragMoveEvent*>(event);
50 const QStringList files = droppedLocalFiles(dragEvent->mimeData());
51 if (!files.isEmpty())
52 dragEvent->acceptProposedAction();
53 else
54 dragEvent->ignore();
55 return {true, !files.isEmpty(), files};
56 }
57 case QEvent::Drop: {
58 auto* dropEvent = static_cast<QDropEvent*>(event);
59 const QStringList files = droppedLocalFiles(dropEvent->mimeData());
60 if (!files.isEmpty())
61 dropEvent->acceptProposedAction();
62 else
63 dropEvent->ignore();
64 return {true, !files.isEmpty(), files};
65 }
66 default:
67 return {};
68 }
69}
70
71} // namespace ui::window
Declarations for the UI drop handling component.
DropEventOutcome handle(QEvent *event) const
Handles a drag, drag-move or drop event.
Result of handling a drag/drop event.
Definition DropHandler.h:17