1 | // Copyright (C) 2021 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | |
4 | #include "inputlistview.h" |
5 | |
6 | #include <QDropEvent> |
7 | #include <QMimeData> |
8 | |
9 | InputListView::InputListView(QWidget *parent) : QListWidget(parent) |
10 | { |
11 | setSelectionMode(QAbstractItemView::ExtendedSelection); |
12 | setAcceptDrops(true); |
13 | } |
14 | |
15 | bool InputListView::tryAddItem(const QString &label) |
16 | { |
17 | if (containsItem(needle: label)) |
18 | return false; |
19 | |
20 | addItem(label); |
21 | return true; |
22 | } |
23 | |
24 | void InputListView::dropEvent(QDropEvent *event) |
25 | { |
26 | constexpr int MAX_URLS = 1024; |
27 | const QMimeData *mimeData = event->mimeData(); |
28 | |
29 | if (mimeData->hasUrls()) { |
30 | QList<QUrl> urlList = mimeData->urls(); |
31 | for (int i = 0; i < urlList.size() && i < MAX_URLS; ++i) { |
32 | const QUrl &url = urlList.at(i); |
33 | const auto filename = url.toLocalFile(); |
34 | if (url.isLocalFile() && !containsItem(needle: filename)) |
35 | addItem(label: filename); |
36 | } |
37 | } |
38 | } |
39 | |
40 | void InputListView::dragEnterEvent(QDragEnterEvent *event) |
41 | { |
42 | event->acceptProposedAction(); |
43 | } |
44 | |
45 | void InputListView::dragMoveEvent(QDragMoveEvent *event) |
46 | { |
47 | event->acceptProposedAction(); |
48 | } |
49 | |
50 | void InputListView::dragLeaveEvent(QDragLeaveEvent *event) |
51 | { |
52 | event->accept(); |
53 | } |
54 | |
55 | bool InputListView::containsItem(const QString &needle) |
56 | { |
57 | for (int i = 0; i < count(); ++i) { |
58 | if (item(row: i)->text() == needle) |
59 | return true; |
60 | } |
61 | return false; |
62 | } |
63 |