File drag&drop: Move mime data creation to central place

Especially when this gets more complicated with additional fancy windows
mime types and custom mime type for opening a file at a specific
location (dropping from Outline, Type Hierarchy et al), we should not
create that complex mime data everywhere by hand.

Change-Id: I37f6ceb287b0cd055501fdd033ac29816434a020
Reviewed-by: Daniel Teske <daniel.teske@digia.com>
This commit is contained in:
Eike Ziller
2014-09-10 14:00:12 +02:00
parent e815fdac76
commit d19628ddf8
3 changed files with 30 additions and 7 deletions

View File

@@ -30,6 +30,7 @@
#include "fileutils.h"
#include "savefile.h"
#include "algorithm.h"
#include "hostosinfo.h"
#include "qtcassert.h"
@@ -718,6 +719,26 @@ FileDropSupport::FileDropSupport(QWidget *parentWidget)
parentWidget->installEventFilter(this);
}
QStringList FileDropSupport::mimeTypesForFilePaths()
{
return QStringList() << QStringLiteral("text/uri-list");
}
QMimeData *FileDropSupport::mimeDataForFilePaths(const QStringList &filePaths)
{
QList<QUrl> localUrls = Utils::transform(filePaths, [filePaths](const QString &path) {
return QUrl::fromLocalFile(path);
});
auto data = new QMimeData;
data->setUrls(localUrls);
return data;
}
QMimeData *FileDropSupport::mimeDataForFilePath(const QString &filePath)
{
return mimeDataForFilePaths(QStringList() << filePath);
}
bool FileDropSupport::eventFilter(QObject *obj, QEvent *event)
{
Q_UNUSED(obj)

View File

@@ -41,6 +41,7 @@ namespace Utils {class FileName; }
QT_BEGIN_NAMESPACE
class QFile;
class QMimeData;
class QTemporaryFile;
class QWidget;
class QTextStream;
@@ -199,6 +200,10 @@ class QTCREATOR_UTILS_EXPORT FileDropSupport : public QObject
public:
FileDropSupport(QWidget *parentWidget);
static QStringList mimeTypesForFilePaths();
static QMimeData *mimeDataForFilePaths(const QStringList &filePaths);
static QMimeData *mimeDataForFilePath(const QString &filePath);
signals:
void filesDropped(const QStringList &files);

View File

@@ -40,7 +40,6 @@
#include <QFileInfo>
#include <QFont>
#include <QMimeData>
#include <QUrl>
namespace ProjectExplorer {
@@ -491,20 +490,18 @@ void FlatModel::reset()
QStringList FlatModel::mimeTypes() const
{
return QStringList() << QStringLiteral("text/uri-list");
return Utils::FileDropSupport::mimeTypesForFilePaths();
}
QMimeData *FlatModel::mimeData(const QModelIndexList &indexes) const
{
QList<QUrl> urls;
QStringList filePaths;
foreach (const QModelIndex &index, indexes) {
Node *node = nodeForIndex(index);
if (qobject_cast<FileNode *>(node))
urls.append(QUrl::fromLocalFile(node->path()));
filePaths.append(node->path());
}
auto data = new QMimeData;
data->setUrls(urls);
return data;
return Utils::FileDropSupport::mimeDataForFilePaths(filePaths);
}
QModelIndex FlatModel::indexForNode(const Node *node_)