Explorer: support drag&drop a directory to VNote to open it in Explorer

This commit is contained in:
Le Tan 2018-07-04 19:49:21 +08:00
parent 5ebc7b8243
commit d67ef089b6
5 changed files with 60 additions and 15 deletions

View File

@ -15,10 +15,15 @@
#include "utils/viconutils.h" #include "utils/viconutils.h"
#include "vcart.h" #include "vcart.h"
#include "vhistorylist.h" #include "vhistorylist.h"
#include "vnote.h"
#include "vexplorer.h"
extern VConfigManager *g_config; extern VConfigManager *g_config;
extern VMainWindow *g_mainWin; extern VMainWindow *g_mainWin;
extern VNote *g_vnote;
#define GET_TAB_FROM_SENDER() static_cast<QAction *>(sender())->data().toInt() #define GET_TAB_FROM_SENDER() static_cast<QAction *>(sender())->data().toInt()
VEditWindow::VEditWindow(VEditArea *editArea, QWidget *parent) VEditWindow::VEditWindow(VEditArea *editArea, QWidget *parent)
@ -1189,25 +1194,42 @@ void VEditWindow::dropEvent(QDropEvent *p_event)
{ {
const QMimeData *mime = p_event->mimeData(); const QMimeData *mime = p_event->mimeData();
if (mime->hasFormat("text/uri-list") && mime->hasUrls()) { 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; QStringList files;
QList<QUrl> urls = mime->urls(); QList<QUrl> urls = mime->urls();
for (int i = 0; i < urls.size(); ++i) { for (int i = 0; i < urls.size(); ++i) {
QString file;
if (urls[i].isLocalFile()) { if (urls[i].isLocalFile()) {
file = urls[i].toLocalFile(); QFileInfo fi(urls[i].toLocalFile());
QFileInfo fi(file); if (!fi.exists()) {
if (fi.exists() && fi.isFile()) { continue;
file = QDir::cleanPath(fi.absoluteFilePath()); }
QString file = QDir::cleanPath(fi.absoluteFilePath());
if (fi.isFile()) {
files.append(file);
} else if (urls.size() == 1) {
isDir = true;
files.append(file); files.append(file);
} }
} }
} }
if (!files.isEmpty()) { if (!files.isEmpty()) {
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(); focusWindow();
g_mainWin->openFiles(files); g_mainWin->openFiles(files);
} }
}
p_event->acceptProposedAction(); p_event->acceptProposedAction();
return; return;

View File

@ -197,7 +197,7 @@ void VExplorer::setupUI()
// If there is no directory entry currently, new one using the parent dir. // If there is no directory entry currently, new one using the parent dir.
if (m_index == -1 || m_entries.isEmpty()) { if (m_index == -1 || m_entries.isEmpty()) {
setAsRootDirectory(VUtils::basePathFromPath(files[0])); setRootDirectory(VUtils::basePathFromPath(files[0]));
} }
openFiles(files, g_config->getNoteOpenMode()); 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")); setRootAct->setToolTip(tr("Set current folder as the root directory to explore"));
connect(setRootAct, &QAction::triggered, connect(setRootAct, &QAction::triggered,
this, [this, filePath]() { this, [this, filePath]() {
setAsRootDirectory(filePath); setRootDirectory(filePath);
}); });
menu.addAction(setRootAct); 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()) { if (p_path.isEmpty()) {
return; return;
} }
init();
qDebug() << "set new root directory" << p_path; qDebug() << "set new root directory" << p_path;
int idx = addEntry(p_path); int idx = addEntry(p_path);

View File

@ -21,6 +21,8 @@ class VExplorer : public QWidget
public: public:
explicit VExplorer(QWidget *p_parent = nullptr); explicit VExplorer(QWidget *p_parent = nullptr);
void setRootDirectory(const QString &p_path);
protected: protected:
void showEvent(QShowEvent *p_event) Q_DECL_OVERRIDE; void showEvent(QShowEvent *p_event) Q_DECL_OVERRIDE;
@ -67,8 +69,6 @@ private:
void renameFile(const QString &p_filePath); void renameFile(const QString &p_filePath);
void setAsRootDirectory(const QString &p_path);
bool m_initialized; bool m_initialized;
bool m_uiInitialized; bool m_uiInitialized;

View File

@ -68,7 +68,13 @@ extern QFile g_logFile;
#define COLOR_PIXMAP_ICON_SIZE 64 #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) VMainWindow::VMainWindow(VSingleInstanceGuard *p_guard, QWidget *p_parent)
@ -3291,7 +3297,13 @@ void VMainWindow::kickOffStartUpTimer(const QStringList &p_files)
void VMainWindow::showNotebookPanel() void VMainWindow::showNotebookPanel()
{ {
changePanelView(PanelViewState::VerticalMode); 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) void VMainWindow::stayOnTop(bool p_enabled)

View File

@ -124,6 +124,10 @@ public:
void focusEditArea() const; void focusEditArea() const;
void showExplorerPanel(bool p_focus = false);
VExplorer *getExplorer() const;
signals: signals:
// Emit when editor related configurations were changed by user. // Emit when editor related configurations were changed by user.
void editorConfigUpdated(); void editorConfigUpdated();
@ -522,4 +526,9 @@ inline VNotebookSelector *VMainWindow::getNotebookSelector() const
{ {
return m_notebookSelector; return m_notebookSelector;
} }
inline VExplorer *VMainWindow::getExplorer() const
{
return m_explorer;
}
#endif // VMAINWINDOW_H #endif // VMAINWINDOW_H