From d67ef089b6dfa3a098070fc54734b7e0240f1092 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Wed, 4 Jul 2018 19:49:21 +0800 Subject: [PATCH] Explorer: support drag&drop a directory to VNote to open it in Explorer --- src/veditwindow.cpp | 38 ++++++++++++++++++++++++++++++-------- src/vexplorer.cpp | 8 +++++--- src/vexplorer.h | 4 ++-- src/vmainwindow.cpp | 16 ++++++++++++++-- src/vmainwindow.h | 9 +++++++++ 5 files changed, 60 insertions(+), 15 deletions(-) diff --git a/src/veditwindow.cpp b/src/veditwindow.cpp index f514c559..bf718b66 100644 --- a/src/veditwindow.cpp +++ b/src/veditwindow.cpp @@ -15,10 +15,15 @@ #include "utils/viconutils.h" #include "vcart.h" #include "vhistorylist.h" +#include "vnote.h" +#include "vexplorer.h" extern VConfigManager *g_config; + extern VMainWindow *g_mainWin; +extern VNote *g_vnote; + #define GET_TAB_FROM_SENDER() static_cast(sender())->data().toInt() VEditWindow::VEditWindow(VEditArea *editArea, QWidget *parent) @@ -1189,24 +1194,41 @@ 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. + // Open external files in this edit window or open a direcotry in Explorer. + bool isDir = false; 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()); + QFileInfo fi(urls[i].toLocalFile()); + if (!fi.exists()) { + continue; + } + + QString file = QDir::cleanPath(fi.absoluteFilePath()); + if (fi.isFile()) { + files.append(file); + } else if (urls.size() == 1) { + isDir = true; files.append(file); } } } if (!files.isEmpty()) { - focusWindow(); - g_mainWin->openFiles(files); + if (isDir) { + VDirectory *dir = g_vnote->getInternalDirectory(files[0]); + if (dir) { + g_mainWin->locateDirectory(dir); + } else { + // External directory. + g_mainWin->showExplorerPanel(true); + g_mainWin->getExplorer()->setRootDirectory(files[0]); + } + } else { + focusWindow(); + g_mainWin->openFiles(files); + } } p_event->acceptProposedAction(); diff --git a/src/vexplorer.cpp b/src/vexplorer.cpp index 5822b6b9..a3f35c98 100644 --- a/src/vexplorer.cpp +++ b/src/vexplorer.cpp @@ -197,7 +197,7 @@ void VExplorer::setupUI() // If there is no directory entry currently, new one using the parent dir. if (m_index == -1 || m_entries.isEmpty()) { - setAsRootDirectory(VUtils::basePathFromPath(files[0])); + setRootDirectory(VUtils::basePathFromPath(files[0])); } openFiles(files, g_config->getNoteOpenMode()); @@ -460,7 +460,7 @@ void VExplorer::handleContextMenuRequested(QPoint p_pos) setRootAct->setToolTip(tr("Set current folder as the root directory to explore")); connect(setRootAct, &QAction::triggered, this, [this, filePath]() { - setAsRootDirectory(filePath); + setRootDirectory(filePath); }); menu.addAction(setRootAct); @@ -757,12 +757,14 @@ void VExplorer::renameFile(const QString &p_filePath) } } -void VExplorer::setAsRootDirectory(const QString &p_path) +void VExplorer::setRootDirectory(const QString &p_path) { if (p_path.isEmpty()) { return; } + init(); + qDebug() << "set new root directory" << p_path; int idx = addEntry(p_path); diff --git a/src/vexplorer.h b/src/vexplorer.h index 57af349f..d681a178 100644 --- a/src/vexplorer.h +++ b/src/vexplorer.h @@ -21,6 +21,8 @@ class VExplorer : public QWidget public: explicit VExplorer(QWidget *p_parent = nullptr); + void setRootDirectory(const QString &p_path); + protected: void showEvent(QShowEvent *p_event) Q_DECL_OVERRIDE; @@ -67,8 +69,6 @@ private: void renameFile(const QString &p_filePath); - void setAsRootDirectory(const QString &p_path); - bool m_initialized; bool m_uiInitialized; diff --git a/src/vmainwindow.cpp b/src/vmainwindow.cpp index b03e53ea..865c21d4 100644 --- a/src/vmainwindow.cpp +++ b/src/vmainwindow.cpp @@ -68,7 +68,13 @@ extern QFile g_logFile; #define COLOR_PIXMAP_ICON_SIZE 64 -#define NAVI_BOX_NOTEBOOKS_IDX 0 +enum NaviBoxIndex +{ + NotebookPanel = 0, + HistoryList, + Explorer, + TagExplorer +}; VMainWindow::VMainWindow(VSingleInstanceGuard *p_guard, QWidget *p_parent) @@ -3291,7 +3297,13 @@ void VMainWindow::kickOffStartUpTimer(const QStringList &p_files) void VMainWindow::showNotebookPanel() { changePanelView(PanelViewState::VerticalMode); - m_naviBox->setCurrentIndex(NAVI_BOX_NOTEBOOKS_IDX, false); + m_naviBox->setCurrentIndex(NaviBoxIndex::NotebookPanel, false); +} + +void VMainWindow::showExplorerPanel(bool p_focus) +{ + changePanelView(PanelViewState::VerticalMode); + m_naviBox->setCurrentIndex(NaviBoxIndex::Explorer, p_focus); } void VMainWindow::stayOnTop(bool p_enabled) diff --git a/src/vmainwindow.h b/src/vmainwindow.h index 098c5c7c..e65ab32f 100644 --- a/src/vmainwindow.h +++ b/src/vmainwindow.h @@ -124,6 +124,10 @@ public: void focusEditArea() const; + void showExplorerPanel(bool p_focus = false); + + VExplorer *getExplorer() const; + signals: // Emit when editor related configurations were changed by user. void editorConfigUpdated(); @@ -522,4 +526,9 @@ inline VNotebookSelector *VMainWindow::getNotebookSelector() const { return m_notebookSelector; } + +inline VExplorer *VMainWindow::getExplorer() const +{ + return m_explorer; +} #endif // VMAINWINDOW_H