implement drag-n-drop for files

This commit is contained in:
Axel Kohlmeyer
2023-07-28 05:04:19 -04:00
parent 7a9694b849
commit c1d690d190
6 changed files with 41 additions and 8 deletions

View File

@ -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)

View File

@ -13,7 +13,11 @@
#include "codeeditor.h"
#include "linenumberarea.h"
#include "lammpsgui.h"
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QMimeData>
#include <QPainter>
#include <QTextBlock>
@ -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<LammpsGui *>(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);

View File

@ -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);

View File

@ -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();

View File

@ -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);

View File

@ -25,7 +25,11 @@
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="CodeEditor" name="textEdit" native="true"/>
<widget class="CodeEditor" name="textEdit" native="true">
<property name="acceptDrops">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
@ -303,7 +307,7 @@
<customwidgets>
<customwidget>
<class>CodeEditor</class>
<extends>QWidget</extends>
<extends>QPlainTextEdit</extends>
<header>codeeditor.h</header>
</customwidget>
</customwidgets>