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: Eike Ziller <eike.ziller@theqtcompany.com>
This commit is contained in:
Jochen Becher
2015-06-04 12:35:59 +02:00
parent d311877142
commit 431b25ad27
23 changed files with 406 additions and 250 deletions

View File

@@ -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