From 5e2c324bc4a01d43d5dfd27724b538534a8c8fe0 Mon Sep 17 00:00:00 2001 From: Toni Date: Mon, 3 Nov 2025 17:24:19 +0100 Subject: [PATCH] drag and drop --- src/mainwindow.cc | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/mainwindow.h | 4 ++++ 2 files changed, 49 insertions(+) diff --git a/src/mainwindow.cc b/src/mainwindow.cc index 6826d13..79c09b5 100644 --- a/src/mainwindow.cc +++ b/src/mainwindow.cc @@ -1,8 +1,11 @@ #include "mainwindow.h" #include +#include +#include #include #include #include +#include #include #include #include @@ -11,6 +14,8 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(std::make_unique()) { ui->setupUi(this); + setAcceptDrops(true); + ui->filePreview->setVisible(false); connect(ui->actionNew, &QAction::triggered, this, [this]() { @@ -201,3 +206,43 @@ void MainWindow::file_context_menu(const QPoint &pos) { menu.exec(ui->fsTreeWidget->mapToGlobal(pos)); } + +void MainWindow::dragEnterEvent(QDragEnterEvent *event) { + if (event->mimeData()->hasUrls()) { + const auto urls = event->mimeData()->urls(); + for (const QUrl &u : urls) { + if (u.isLocalFile()) { + event->acceptProposedAction(); + return; + } + } + } + event->ignore(); +} + +void MainWindow::dropEvent(QDropEvent *event) { + if (!event->mimeData()->hasUrls()) { + event->ignore(); + return; + } + if (!m_vault) { + return; + } + + for (const QUrl &u : event->mimeData()->urls()) { + if (!u.isLocalFile()) { + continue; + } + std::ifstream file(u.toLocalFile().toStdString(), std::ios::binary); + ASSERT(file.good()); + + std::string content((std::istreambuf_iterator(file)), + std::istreambuf_iterator()); + + m_vault->create_file(path_to_filename(u.toLocalFile().toStdString()), + content); + } + reload_fs_tree(); + + event->acceptProposedAction(); +} \ No newline at end of file diff --git a/src/mainwindow.h b/src/mainwindow.h index b9e29e1..8ecdc17 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -11,6 +11,10 @@ class MainWindow : public QMainWindow { public: explicit MainWindow(QWidget *parent = nullptr); +protected: + void dragEnterEvent(QDragEnterEvent *event) override; + void dropEvent(QDropEvent *event) override; + private: std::unique_ptr ui;