implement drag-n-drop for files
This commit is contained in:
@ -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)
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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>
|
||||
|
||||
Reference in New Issue
Block a user