aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/utils/dropsupport.cpp
blob: 9d914e0f084bcb580831822bd9ce7cf837e2e2d6 (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "dropsupport.h"

#include "qtcassert.h"

#include <QUrl>
#include <QWidget>
#include <QDropEvent>
#include <QTimer>

#ifdef Q_OS_OSX
// for file drops from Finder, working around QTBUG-40449
#include "fileutils_mac.h"
#endif

namespace Utils {

static bool isFileDropMime(const QMimeData *d, QList<DropSupport::FileSpec> *files = nullptr)
{
    // internal drop
    if (const auto internalData = qobject_cast<const DropMimeData *>(d)) {
        if (files)
            *files = internalData->files();
        return !internalData->files().isEmpty();
    }

    // external drop
    if (files)
        files->clear();
    // Extract dropped files from Mime data.
    if (!d->hasUrls())
        return false;
    const QList<QUrl> urls = d->urls();
    if (urls.empty())
        return false;
    // Try to find local files
    bool hasFiles = false;
    const QList<QUrl>::const_iterator cend = urls.constEnd();
    for (QList<QUrl>::const_iterator it = urls.constBegin(); it != cend; ++it) {
        QUrl url = *it;
#ifdef Q_OS_OSX
        // for file drops from Finder, working around QTBUG-40449
        url = Internal::filePathUrl(url);
#endif
        const QString fileName = url.toLocalFile();
        if (!fileName.isEmpty()) {
            hasFiles = true;
            if (files)
                files->append(DropSupport::FileSpec(FilePath::fromString(fileName)));
            else
                break; // No result list, sufficient for checking
        }
    }
    return hasFiles;
}

DropSupport::DropSupport(QWidget *parentWidget, const DropFilterFunction &filterFunction)
    : QObject(parentWidget),
      m_filterFunction(filterFunction)
{
    QTC_ASSERT(parentWidget, return);
    parentWidget->setAcceptDrops(true);
    parentWidget->installEventFilter(this);
}

QStringList DropSupport::mimeTypesForFilePaths()
{
    return QStringList("text/uri-list");
}

bool DropSupport::isFileDrop(QDropEvent *event)
{
    return isFileDropMime(event->mimeData());
}

bool DropSupport::isValueDrop(QDropEvent *event)
{
    if (const auto internalData = qobject_cast<const DropMimeData *>(event->mimeData())) {
        return !internalData->values().isEmpty();
    }
    return false;
}

bool DropSupport::eventFilter(QObject *obj, QEvent *event)
{
    Q_UNUSED(obj)
    if (event->type() == QEvent::DragEnter) {
        auto dee = static_cast<QDragEnterEvent *>(event);
        if ((isFileDrop(dee) || isValueDrop(dee)) && (!m_filterFunction || m_filterFunction(dee, this))) {
            event->accept();
        } else {
            event->ignore();
        }
        return true;
    } else if (event->type() == QEvent::DragMove) {
        event->accept();
        return true;
    } else if (event->type() == QEvent::Drop) {
        bool accepted = false;
        auto de = static_cast<QDropEvent *>(event);
        if (!m_filterFunction || m_filterFunction(de, this)) {
            const auto fileDropMimeData = qobject_cast<const DropMimeData *>(de->mimeData());
            QList<FileSpec> tempFiles;
            if (isFileDropMime(de->mimeData(), &tempFiles)) {
                event->accept();
                accepted = true;
                if (fileDropMimeData && fileDropMimeData->isOverridingFileDropAction())
                    de->setDropAction(fileDropMimeData->overrideFileDropAction());
                else
                    de->acceptProposedAction();
                bool needToScheduleEmit = m_files.isEmpty();
                m_files.append(tempFiles);
                m_dropPos = de->pos();
                if (needToScheduleEmit) { // otherwise we already have a timer pending
                    // Delay the actual drop, to avoid conflict between
                    // actions that happen when opening files, and actions that the item views do
                    // after the drag operation.
                    // If we do not do this, e.g. dragging from Outline view crashes if the editor and
                    // the selected item changes
                    QTimer::singleShot(100, this, &DropSupport::emitFilesDropped);
                }
            } else if (fileDropMimeData && !fileDropMimeData->values().isEmpty()) {
                event->accept();
                accepted = true;
                bool needToScheduleEmit = m_values.isEmpty();
                m_values.append(fileDropMimeData->values());
                m_dropPos = de->pos();
                if (needToScheduleEmit)
                    QTimer::singleShot(100, this, &DropSupport::emitValuesDropped);
            }
        }
        if (!accepted) {
            event->ignore();
        }
        return true;
    }
    return false;
}

void DropSupport::emitFilesDropped()
{
    QTC_ASSERT(!m_files.isEmpty(), return);
    emit filesDropped(m_files, m_dropPos);
    m_files.clear();
}

void DropSupport::emitValuesDropped()
{
    QTC_ASSERT(!m_values.isEmpty(), return);
    emit valuesDropped(m_values, m_dropPos);
    m_values.clear();
}

/*!
    Sets the drop action to effectively use, instead of the "proposed" drop action from the
    drop event. This can be useful when supporting move drags within an item view, but not
    "moving" an item from the item view into a split.
 */
DropMimeData::DropMimeData()
    : m_overrideDropAction(Qt::IgnoreAction),
      m_isOverridingDropAction(false)
{

}

void DropMimeData::setOverrideFileDropAction(Qt::DropAction action)
{
    m_isOverridingDropAction = true;
    m_overrideDropAction = action;
}

Qt::DropAction DropMimeData::overrideFileDropAction() const
{
    return m_overrideDropAction;
}

bool DropMimeData::isOverridingFileDropAction() const
{
    return m_isOverridingDropAction;
}

void DropMimeData::addFile(const FilePath &filePath, int line, int column)
{
    // standard mime data
    QList<QUrl> currentUrls = urls();
    currentUrls.append(QUrl::fromLocalFile(filePath.toString()));
    setUrls(currentUrls);
    // special mime data
    m_files.append(DropSupport::FileSpec(filePath, line, column));
}

QList<DropSupport::FileSpec> DropMimeData::files() const
{
    return m_files;
}

void DropMimeData::addValue(const QVariant &value)
{
    m_values.append(value);
}

QList<QVariant> DropMimeData::values() const
{
    return m_values;
}

} // namespace Utils