Commit 431b25ad authored by Jochen Becher's avatar Jochen Becher

Introduce dragging for all explorer nodes.

Extend drop support with variant values. A drop may be a file drop or a
value drop or both.

Rename Utils::FileDropSupport to Utils::DropSupport and add methods to
add not only files but any QVariant value to the mime data. Project
explorer adds dragged nodes (which will be needed for future ModelEditor
plugin).

Change-Id: I799542c60fdecb3e64af0d3ba47b6caa9adbcfd7
Reviewed-by: default avatarEike Ziller <eike.ziller@theqtcompany.com>
parent d3118771
......@@ -35,7 +35,7 @@
#include <cplusplus/Scope.h>
#include <cplusplus/Literals.h>
#include <cplusplus/Symbols.h>
#include <utils/fileutils.h>
#include <utils/dropsupport.h>
using namespace CPlusPlus;
......@@ -258,12 +258,12 @@ Qt::DropActions OverviewModel::supportedDragActions() const
QStringList OverviewModel::mimeTypes() const
{
return Utils::FileDropSupport::mimeTypesForFilePaths();
return Utils::DropSupport::mimeTypesForFilePaths();
}
QMimeData *OverviewModel::mimeData(const QModelIndexList &indexes) const
{
auto mimeData = new Utils::FileDropMimeData;
auto mimeData = new Utils::DropMimeData;
foreach (const QModelIndex &index, indexes) {
const QVariant fileName = data(index, FileNameRole);
if (!fileName.canConvert<QString>())
......
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms and
** conditions see http://www.qt.io/terms-conditions. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#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
namespace Utils {
namespace Internal {
extern QUrl filePathUrl(const QUrl &url);
} // Internal
} // Utils
#endif
namespace Utils {
static bool isFileDrop(const QMimeData *d, QList<DropSupport::FileSpec> *files = 0)
{
// internal drop
if (const DropMimeData *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(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() << QStringLiteral("text/uri-list");
}
bool DropSupport::isFileDrop(QDropEvent *event) const
{
return Utils::isFileDrop(event->mimeData());
}
bool DropSupport::isValueDrop(QDropEvent *event) const
{
if (const DropMimeData *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 DropMimeData *fileDropMimeData = qobject_cast<const DropMimeData *>(de->mimeData());
QList<FileSpec> tempFiles;
if (Utils::isFileDrop(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);
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, SLOT(emitFilesDropped()));
}
}
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, SLOT(emitValuesDropped()));
}
}
if (!accepted) {
event->ignore();
}
return true;
}
return false;
}
void DropSupport::emitFilesDropped()
{
QTC_ASSERT(!m_files.isEmpty(), return);
emit filesDropped(m_files);
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 QString &filePath, int line, int column)
{
// standard mime data
QList<QUrl> currentUrls = urls();
currentUrls.append(QUrl::fromLocalFile(filePath));
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
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms and
** conditions see http://www.qt.io/terms-conditions. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef UTILS_DROPSUPPORT_H
#define UTILS_DROPSUPPORT_H
#include "utils_global.h"
#include <QObject>
#include <QMimeData>
#include <QPoint>
#include <functional>
QT_BEGIN_NAMESPACE
class QDropEvent;
class QWidget;
QT_END_NAMESPACE
namespace Utils {
class QTCREATOR_UTILS_EXPORT DropSupport : public QObject
{
Q_OBJECT
public:
struct FileSpec {
FileSpec(const QString &path, int r = -1, int c = -1) : filePath(path), line(r), column(c) {}
QString filePath;
int line;
int column;
};
// returns true if the event should be accepted
typedef std::function<bool(QDropEvent*,DropSupport*)> DropFilterFunction;
DropSupport(QWidget *parentWidget, const DropFilterFunction &filterFunction = DropFilterFunction());
static QStringList mimeTypesForFilePaths();
signals:
void filesDropped(const QList<Utils::DropSupport::FileSpec> &files);
void valuesDropped(const QList<QVariant> &values, const QPoint &dropPos);
public:
bool isFileDrop(QDropEvent *event) const;
bool isValueDrop(QDropEvent *event) const;
protected:
bool eventFilter(QObject *obj, QEvent *event);
private slots:
void emitFilesDropped();
void emitValuesDropped();
private:
DropFilterFunction m_filterFunction;
QList<FileSpec> m_files;
QList<QVariant> m_values;
QPoint m_dropPos;
};
class QTCREATOR_UTILS_EXPORT DropMimeData : public QMimeData
{
Q_OBJECT
public:
DropMimeData();
void setOverrideFileDropAction(Qt::DropAction action);
Qt::DropAction overrideFileDropAction() const;
bool isOverridingFileDropAction() const;
void addFile(const QString &filePath, int line = -1, int column = -1);
QList<DropSupport::FileSpec> files() const;
void addValue(const QVariant &value);
QList<QVariant> values() const;
private:
QList<DropSupport::FileSpec> m_files;
QList<QVariant> m_values;
Qt::DropAction m_overrideDropAction;
bool m_isOverridingDropAction;
};
} // namespace Utils
#endif // UTILS_DROPSUPPORT_H
......@@ -57,15 +57,6 @@ QDebug operator<<(QDebug dbg, const Utils::FileName &c)
QT_END_NAMESPACE
#ifdef Q_OS_OSX
// for file drops from Finder, working around QTBUG-40449
namespace Utils {
namespace Internal {
extern QUrl filePathUrl(const QUrl &url);
} // Internal
} // Utils
#endif
namespace Utils {
/*! \class Utils::FileUtils
......@@ -785,152 +776,6 @@ int FileNameList::removeDuplicates()
return removed;
}
static bool isFileDrop(const QMimeData *d, QList<FileDropSupport::FileSpec> *files = 0)
{
// internal drop
if (const FileDropMimeData *internalData = qobject_cast<const FileDropMimeData *>(d)) {
if (files)
*files = internalData->files();
return true;
}
// 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(FileDropSupport::FileSpec(fileName));
else
break; // No result list, sufficient for checking
}
}
return hasFiles;
}
FileDropSupport::FileDropSupport(QWidget *parentWidget, const DropFilterFunction &filterFunction)
: QObject(parentWidget),
m_filterFunction(filterFunction)
{
QTC_ASSERT(parentWidget, return);
parentWidget->setAcceptDrops(true);
parentWidget->installEventFilter(this);
}
QStringList FileDropSupport::mimeTypesForFilePaths()
{
return QStringList() << QStringLiteral("text/uri-list");
}
bool FileDropSupport::eventFilter(QObject *obj, QEvent *event)
{
Q_UNUSED(obj)
if (event->type() == QEvent::DragEnter) {
auto dee = static_cast<QDragEnterEvent *>(event);
if (isFileDrop(dee->mimeData())
&& (!m_filterFunction || m_filterFunction(dee)))
event->accept();
else
event->ignore();
return true;
} else if (event->type() == QEvent::DragMove) {
event->accept();
return true;
} else if (event->type() == QEvent::Drop) {
auto de = static_cast<QDropEvent *>(event);
QList<FileSpec> tempFiles;
if (isFileDrop(de->mimeData(), &tempFiles)
&& (!m_filterFunction || m_filterFunction(de))) {
const FileDropMimeData *fileDropMimeData = qobject_cast<const FileDropMimeData *>(de->mimeData());
event->accept();
if (fileDropMimeData && fileDropMimeData->isOverridingFileDropAction())
de->setDropAction(fileDropMimeData->overrideFileDropAction());
else
de->acceptProposedAction();
bool needToScheduleEmit = m_files.isEmpty();
m_files.append(tempFiles);
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, SLOT(emitFilesDropped()));
}
} else {
event->ignore();
}
return true;
}
return false;
}
void FileDropSupport::emitFilesDropped()
{
QTC_ASSERT(!m_files.isEmpty(), return);
emit filesDropped(m_files);
m_files.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.
*/
FileDropMimeData::FileDropMimeData()
: m_overrideDropAction(Qt::IgnoreAction),
m_isOverridingDropAction(false)
{
}
void FileDropMimeData::setOverrideFileDropAction(Qt::DropAction action)
{
m_isOverridingDropAction = true;
m_overrideDropAction = action;
}
Qt::DropAction FileDropMimeData::overrideFileDropAction() const
{
return m_overrideDropAction;
}
bool FileDropMimeData::isOverridingFileDropAction() const
{
return m_isOverridingDropAction;
}
void FileDropMimeData::addFile(const QString &filePath, int line, int column)
{
// standard mime data
QList<QUrl> currentUrls = urls();
currentUrls.append(QUrl::fromLocalFile(filePath));
setUrls(currentUrls);
// special mime data
m_files.append(FileDropSupport::FileSpec(filePath, line, column));
}
QList<FileDropSupport::FileSpec> FileDropMimeData::files() const
{
return m_files;
}
} // namespace Utils
QT_BEGIN_NAMESPACE
......
......@@ -36,7 +36,6 @@
#include <QCoreApplication>
#include <QXmlStreamWriter> // Mac.
#include <QMetaType>
#include <QMimeData>
#include <QStringList>
#include <functional>
......@@ -215,58 +214,6 @@ private:
bool m_autoRemove;
};
class QTCREATOR_UTILS_EXPORT FileDropSupport : public QObject
{
Q_OBJECT
public:
struct FileSpec {
FileSpec(const QString &path, int r = -1, int c = -1) : filePath(path), line(r), column(c) {}
QString filePath;
int line;
int column;
};
// returns true if the event should be accepted
typedef std::function<bool(QDropEvent*)> DropFilterFunction;
FileDropSupport(QWidget *parentWidget, const DropFilterFunction &filterFunction
= DropFilterFunction());
static QStringList mimeTypesForFilePaths();
signals:
void filesDropped(const QList<Utils::FileDropSupport::FileSpec> &files);
protected:
bool eventFilter(QObject *obj, QEvent *event);
private slots:
void emitFilesDropped();
private:
DropFilterFunction m_filterFunction;
QList<FileSpec> m_files;
};
class QTCREATOR_UTILS_EXPORT FileDropMimeData : public QMimeData
{
Q_OBJECT
public:
FileDropMimeData();
void setOverrideFileDropAction(Qt::DropAction action);
Qt::DropAction overrideFileDropAction() const;
bool isOverridingFileDropAction() const;
void addFile(const QString &filePath, int line = -1, int column = -1);
QList<FileDropSupport::FileSpec> files() const;
private:
QList<FileDropSupport::FileSpec> m_files;