From ccbaaa91bc235244e8407b7d7f8e714dd91bcf63 Mon Sep 17 00:00:00 2001 From: houmaster Date: Tue, 4 Aug 2020 20:46:20 +0200 Subject: [PATCH] Accepting file drops on whole window --- src/MainWindow.cpp | 16 ++++++++++++++++ src/MainWindow.h | 2 ++ src/editors/EditorManager.cpp | 16 ---------------- src/editors/EditorManager.h | 4 ---- 4 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index d4dda453..50833037 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -23,6 +23,7 @@ #include #include #include +#include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) @@ -38,6 +39,8 @@ MainWindow::MainWindow(QWidget *parent) mUi->setupUi(this); setContentsMargins(1, 0, 1, 1); + setAcceptDrops(true); + auto icon = QIcon(":images/16x16/icon.png"); icon.addFile(":images/32x32/icon.png"); icon.addFile(":images/64x64/icon.png"); @@ -338,6 +341,19 @@ void MainWindow::readSettings() handleDarkThemeChanging(settings.darkTheme()); } +void MainWindow::dragEnterEvent(QDragEnterEvent *event) +{ + if (event->mimeData()->hasUrls()) { + event->acceptProposedAction(); + } +} + +void MainWindow::dropEvent(QDropEvent *event) +{ + for (const QUrl &url : event->mimeData()->urls()) + openFile(url.toLocalFile()); +} + void MainWindow::closeEvent(QCloseEvent *event) { if (closeSession()) diff --git a/src/MainWindow.h b/src/MainWindow.h index bd888d33..e0197d7b 100644 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -45,6 +45,8 @@ public Q_SLOTS: protected: void closeEvent(QCloseEvent *event) override; + void dragEnterEvent(QDragEnterEvent *event) override; + void dropEvent(QDropEvent *event) override; private: void writeSettings(); diff --git a/src/editors/EditorManager.cpp b/src/editors/EditorManager.cpp index 20bf4344..74b68470 100644 --- a/src/editors/EditorManager.cpp +++ b/src/editors/EditorManager.cpp @@ -7,7 +7,6 @@ #include #include #include -#include #include EditorManager::EditorManager(QWidget *parent) @@ -19,7 +18,6 @@ EditorManager::EditorManager(QWidget *parent) setDockOptions(AnimatedDocks | AllowNestedDocks | AllowTabbedDocks); setDocumentMode(true); setContentsMargins(0, 1, 0, 0); - setAcceptDrops(true); } EditorManager::~EditorManager() = default; @@ -63,20 +61,6 @@ int EditorManager::openNotSavedDialog(const QString& fileName) return dialog.exec(); } -void EditorManager::dragEnterEvent(QDragEnterEvent *e) -{ - const auto urls = e->mimeData()->urls(); - if (!urls.isEmpty() && !urls.front().toLocalFile().isEmpty()) - e->accept(); -} - -void EditorManager::dropEvent(QDropEvent *e) -{ - const auto urls = e->mimeData()->urls(); - for (const QUrl &url : urls) - openEditor(url.toLocalFile()); -} - int EditorManager::getFocusedEditorIndex() const { auto index = 0; diff --git a/src/editors/EditorManager.h b/src/editors/EditorManager.h index 54e43533..de18d579 100644 --- a/src/editors/EditorManager.h +++ b/src/editors/EditorManager.h @@ -60,10 +60,6 @@ class EditorManager final : public DockWindow void editorRenamed(const QString &prevFileName, const QString &fileName); void sourceTypeChanged(SourceType sourceType); -protected: - void dragEnterEvent(QDragEnterEvent *e) override; - void dropEvent(QDropEvent *e) override; - private: int getFocusedEditorIndex() const; bool focusEditorByIndex(int index);