From c1d690d190b4ad94ab9876025e2a01d254911a74 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Fri, 28 Jul 2023 05:04:19 -0400 Subject: [PATCH] implement drag-n-drop for files --- tools/lammps-gui/TODO.md | 1 - tools/lammps-gui/codeeditor.cpp | 27 +++++++++++++++++++++++++++ tools/lammps-gui/codeeditor.h | 3 +++ tools/lammps-gui/lammpsgui.cpp | 7 +++---- tools/lammps-gui/lammpsgui.h | 3 ++- tools/lammps-gui/lammpsgui.ui | 8 ++++++-- 6 files changed, 41 insertions(+), 8 deletions(-) diff --git a/tools/lammps-gui/TODO.md b/tools/lammps-gui/TODO.md index 2c1e70a647..f6c97c12b6 100644 --- a/tools/lammps-gui/TODO.md +++ b/tools/lammps-gui/TODO.md @@ -3,7 +3,6 @@ LAMMPS-GUI TODO list: # Short term goals - add indicator for when the file in editor is modified (-> status bar?) -- add support for drag-n-drop of files into the editor - add "Help" entry to menu bar. Should open a popup window with a one page description of how to use it. Use HTML or Markdown text. - add dialog when exiting asking if file should be saved when it is modified, similar dialog when opening a new file - add CTRL-q hotkey to log windows so you can exit the entire application (add do you really want to? dialog to this) diff --git a/tools/lammps-gui/codeeditor.cpp b/tools/lammps-gui/codeeditor.cpp index ff3fa2f300..13519b0652 100644 --- a/tools/lammps-gui/codeeditor.cpp +++ b/tools/lammps-gui/codeeditor.cpp @@ -13,7 +13,11 @@ #include "codeeditor.h" #include "linenumberarea.h" +#include "lammpsgui.h" +#include +#include +#include #include #include @@ -58,6 +62,29 @@ void CodeEditor::updateLineNumberArea(const QRect &rect, int dy) if (rect.contains(viewport()->rect())) updateLineNumberAreaWidth(0); } +void CodeEditor::dragEnterEvent(QDragEnterEvent *event) +{ + event->acceptProposedAction(); +} + +bool CodeEditor::canInsertFromMimeData(const QMimeData *source) const +{ + return source->hasUrls(); // || source->hasText(); +} + +void CodeEditor::dropEvent(QDropEvent *event) +{ + event->acceptProposedAction(); + if (event->mimeData()->hasUrls()) { + auto file = event->mimeData()->urls()[0].url().remove("file://"); + auto gui = dynamic_cast(parent()); + if (gui) gui->open_file(file); + } else if (event->mimeData()->hasText()) { + fprintf(stderr, "Drag - Drop for text block not yet implemented: text=%s\n", + event->mimeData()->text().toStdString().c_str()); + } +} + void CodeEditor::resizeEvent(QResizeEvent *e) { QPlainTextEdit::resizeEvent(e); diff --git a/tools/lammps-gui/codeeditor.h b/tools/lammps-gui/codeeditor.h index c501e8771c..49abf81553 100644 --- a/tools/lammps-gui/codeeditor.h +++ b/tools/lammps-gui/codeeditor.h @@ -27,6 +27,9 @@ public: protected: void resizeEvent(QResizeEvent *event) override; + void dragEnterEvent(QDragEnterEvent *event) override; + bool canInsertFromMimeData(const QMimeData *source) const override; + void dropEvent(QDropEvent *event) override; private slots: void updateLineNumberAreaWidth(int newBlockCount); diff --git a/tools/lammps-gui/lammpsgui.cpp b/tools/lammps-gui/lammpsgui.cpp index bb6a1a4549..4f6d7d74fa 100644 --- a/tools/lammps-gui/lammpsgui.cpp +++ b/tools/lammps-gui/lammpsgui.cpp @@ -79,7 +79,8 @@ LammpsGui::LammpsGui(QWidget *parent, const char *filename) : #if defined(LAMMPS_GUI_USE_PLUGIN) liblammpsplugin_t *lammps = nullptr; - for (const auto libfile : {"liblammps.so", "./liblammps.so", "liblammps.dylib", "./liblammps.dylib", "liblammps.dll"}) { + for (const auto libfile : {"liblammps.so", "./liblammps.so", "liblammps.dylib", + "./liblammps.dylib", "liblammps.dll"}) { if (!lammps) lammps = liblammpsplugin_load(libfile); if (lammps) { plugin_path = libfile; @@ -106,7 +107,6 @@ LammpsGui::LammpsGui(QWidget *parent, const char *filename) : progress->setRange(0, 1000); progress->setFixedWidth(500); ui->statusbar->addWidget(progress); - } LammpsGui::~LammpsGui() @@ -141,7 +141,6 @@ void LammpsGui::open() // open file and switch CWD to path of file void LammpsGui::open_file(const QString &fileName) { - QFileInfo path(fileName); current_file = path.fileName(); current_dir = path.absolutePath(); @@ -393,7 +392,7 @@ void LammpsGui::about() #else version += " - LAMMPS linked statically"; #endif - std::string info = "LAMMPS is currently running. LAMMPS config info not available."; + std::string info = "LAMMPS is currently running. LAMMPS config info not available."; // LAMMPS is not re-entrant, so we can only query LAMMPS when it is not running if (!is_running) { diff --git a/tools/lammps-gui/lammpsgui.h b/tools/lammps-gui/lammpsgui.h index 5a7b4fb0e6..1ce4946bec 100644 --- a/tools/lammps-gui/lammpsgui.h +++ b/tools/lammps-gui/lammpsgui.h @@ -36,9 +36,10 @@ class StdCapture; class LammpsGui : public QMainWindow { Q_OBJECT + friend class CodeEditor; public: LammpsGui(QWidget *parent = nullptr, const char *filename = nullptr); - ~LammpsGui(); + ~LammpsGui() override; protected: void open_file(const QString &filename); diff --git a/tools/lammps-gui/lammpsgui.ui b/tools/lammps-gui/lammpsgui.ui index d2d1cc9bad..d9f9ca077a 100644 --- a/tools/lammps-gui/lammpsgui.ui +++ b/tools/lammps-gui/lammpsgui.ui @@ -25,7 +25,11 @@ - + + + true + + @@ -303,7 +307,7 @@ CodeEditor - QWidget + QPlainTextEdit
codeeditor.h