From 8a6ce16db54606e8e9185bd19514e352feec5a20 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Thu, 20 Jul 2017 19:19:28 +0800 Subject: [PATCH] support drag and drop into edit area to open external files --- src/main.cpp | 3 +-- src/veditarea.cpp | 3 --- src/veditwindow.cpp | 36 ++++++++++++++++++++++++++++++++++++ src/veditwindow.h | 6 ++++++ src/vwebview.cpp | 1 + 5 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index ff29ab68..fffbef93 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -125,9 +125,8 @@ int main(int argc, char *argv[]) // Need to use absolute path here since VNote may be launched // in different working directory. filePath = QDir::cleanPath(fi.absoluteFilePath()); + filePaths.append(filePath); } - - filePaths.append(filePath); } } diff --git a/src/veditarea.cpp b/src/veditarea.cpp index 51f9839f..f1badd3b 100644 --- a/src/veditarea.cpp +++ b/src/veditarea.cpp @@ -597,9 +597,6 @@ int VEditArea::focusNextWindow(int p_biaIdx) void VEditArea::moveCurrentTabOneSplit(bool p_right) { - if (splitter->count() < 2) { - return; - } getWindow(curWindowIndex)->moveCurrentTabOneSplit(p_right); } diff --git a/src/veditwindow.cpp b/src/veditwindow.cpp index 297a6407..1bf25137 100644 --- a/src/veditwindow.cpp +++ b/src/veditwindow.cpp @@ -20,6 +20,7 @@ VEditWindow::VEditWindow(VNote *vnote, VEditArea *editArea, QWidget *parent) : QTabWidget(parent), vnote(vnote), m_editArea(editArea), m_curTabWidget(NULL), m_lastTabWidget(NULL) { + setAcceptDrops(true); initTabActions(); setupCornerWidget(); @@ -808,6 +809,7 @@ void VEditWindow::moveCurrentTabOneSplit(bool p_right) if (idx == -1) { return; } + moveTabOneSplit(idx, p_right); } @@ -918,3 +920,37 @@ VEditTab* VEditWindow::getTab(int tabIndex) const return dynamic_cast(widget(tabIndex)); } +void VEditWindow::dragEnterEvent(QDragEnterEvent *p_event) +{ + if (p_event->mimeData()->hasFormat("text/uri-list")) { + p_event->acceptProposedAction(); + } +} + +void VEditWindow::dropEvent(QDropEvent *p_event) +{ + const QMimeData *mime = p_event->mimeData(); + if (mime->hasFormat("text/uri-list") && mime->hasUrls()) { + // Open external files in this edit window. + QStringList files; + QList urls = mime->urls(); + for (int i = 0; i < urls.size(); ++i) { + QString file; + if (urls[i].isLocalFile()) { + file = urls[i].toLocalFile(); + QFileInfo fi(file); + if (fi.exists() && fi.isFile()) { + file = QDir::cleanPath(fi.absoluteFilePath()); + files.append(file); + } + } + } + + if (!files.isEmpty()) { + focusWindow(); + g_vnote->getMainWindow()->openExternalFiles(files); + } + + p_event->acceptProposedAction(); + } +} diff --git a/src/veditwindow.h b/src/veditwindow.h index 10f84df7..c264fa01 100644 --- a/src/veditwindow.h +++ b/src/veditwindow.h @@ -63,6 +63,12 @@ public: protected: void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE; + // To accept specific drop. + void dragEnterEvent(QDragEnterEvent *p_event) Q_DECL_OVERRIDE; + + // Drop the data. + void dropEvent(QDropEvent *p_event) Q_DECL_OVERRIDE; + signals: // Status of current VEditTab has update. void tabStatusUpdated(const VEditTabInfo &p_info); diff --git a/src/vwebview.cpp b/src/vwebview.cpp index 0505d33f..fc554f96 100644 --- a/src/vwebview.cpp +++ b/src/vwebview.cpp @@ -19,6 +19,7 @@ static const QString c_ClipboardPropertyMark = "CopiedImageURLAltered"; VWebView::VWebView(VFile *p_file, QWidget *p_parent) : QWebEngineView(p_parent), m_file(p_file), m_actionHooked(false) { + setAcceptDrops(false); } void VWebView::contextMenuEvent(QContextMenuEvent *p_event)