summaryrefslogtreecommitdiff
path: root/src/libs/utils
diff options
context:
space:
mode:
authorEike Ziller <eike.ziller@digia.com>2014-07-07 16:16:49 +0200
committerEike Ziller <eike.ziller@digia.com>2014-07-09 15:28:45 +0200
commit8273b899e90f9e1dfbffd63483a2d20c9c79fce9 (patch)
tree452e7234d7ae59ae9631fc546232da148b1f113f /src/libs/utils
parent13346936c6039056cc5c6e3e94bec02a2d51135e (diff)
downloadqt-creator-8273b899e90f9e1dfbffd63483a2d20c9c79fce9.tar.gz
Core: Refactor file dropping into reusable component
Change-Id: I42d291d8f8c0e76e289e56055044c369e8be8a89 Reviewed-by: David Schulz <david.schulz@digia.com>
Diffstat (limited to 'src/libs/utils')
-rw-r--r--src/libs/utils/fileutils.cpp70
-rw-r--r--src/libs/utils/fileutils.h21
2 files changed, 91 insertions, 0 deletions
diff --git a/src/libs/utils/fileutils.cpp b/src/libs/utils/fileutils.cpp
index 52aafce6dc..02ac2d4242 100644
--- a/src/libs/utils/fileutils.cpp
+++ b/src/libs/utils/fileutils.cpp
@@ -36,7 +36,11 @@
#include <QDir>
#include <QDebug>
#include <QDateTime>
+#include <QDragEnterEvent>
+#include <QDropEvent>
#include <QMessageBox>
+#include <QMimeData>
+#include <QTimer>
#ifdef Q_OS_WIN
#include <qt_windows.h>
@@ -679,6 +683,72 @@ FileName &FileName::appendString(QChar str)
return *this;
}
+static bool isDesktopFileManagerDrop(const QMimeData *d, QStringList *files = 0)
+{
+ 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) {
+ const QString fileName = it->toLocalFile();
+ if (!fileName.isEmpty()) {
+ hasFiles = true;
+ if (files)
+ files->push_back(fileName);
+ else
+ break; // No result list, sufficient for checking
+ }
+ }
+ return hasFiles;
+}
+
+FileDropSupport::FileDropSupport(QWidget *parentWidget)
+ : QObject(parentWidget)
+{
+ QTC_ASSERT(parentWidget, return);
+ parentWidget->setAcceptDrops(true);
+ parentWidget->installEventFilter(this);
+}
+
+bool FileDropSupport::eventFilter(QObject *obj, QEvent *event)
+{
+ Q_UNUSED(obj)
+ if (event->type() == QEvent::DragEnter) {
+ auto dee = static_cast<QDragEnterEvent *>(event);
+ if (isDesktopFileManagerDrop(dee->mimeData()))
+ event->accept();
+ else
+ event->ignore();
+ } else if (event->type() == QEvent::Drop) {
+ auto de = static_cast<QDropEvent *>(event);
+ QStringList tempFiles;
+ if (isDesktopFileManagerDrop(de->mimeData(), &tempFiles)) {
+ event->accept();
+ bool needToScheduleEmit = m_files.isEmpty();
+ m_files.append(tempFiles);
+ if (needToScheduleEmit) // otherwise we already have a timer pending
+ QTimer::singleShot(0, this, SLOT(emitFilesDropped()));
+ } else {
+ event->ignore();
+ }
+ }
+ return false;
+}
+
+void FileDropSupport::emitFilesDropped()
+{
+ QTC_ASSERT(!m_files.isEmpty(), return);
+ emit filesDropped(m_files);
+ m_files.clear();
+}
+
} // namespace Utils
QT_BEGIN_NAMESPACE
diff --git a/src/libs/utils/fileutils.h b/src/libs/utils/fileutils.h
index 414e72095d..b2dbd66ac1 100644
--- a/src/libs/utils/fileutils.h
+++ b/src/libs/utils/fileutils.h
@@ -35,6 +35,7 @@
#include <QCoreApplication>
#include <QXmlStreamWriter> // Mac.
#include <QMetaType>
+#include <QStringList>
namespace Utils {class FileName; }
@@ -192,6 +193,26 @@ private:
bool m_autoRemove;
};
+class QTCREATOR_UTILS_EXPORT FileDropSupport : public QObject
+{
+ Q_OBJECT
+public:
+ FileDropSupport(QWidget *parentWidget);
+
+signals:
+ void filesDropped(const QStringList &files);
+
+protected:
+ bool eventFilter(QObject *obj, QEvent *event);
+
+private slots:
+ void emitFilesDropped();
+
+private:
+ QStringList m_files;
+
+};
+
} // namespace Utils
QT_BEGIN_NAMESPACE