From 7dc0ace48fa324d5d71118411cfc834473157fb5 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Sun, 21 Jul 2024 18:37:06 -0400 Subject: [PATCH] file viewer can be called from File menu or context menu in editor if word under cursor is a file --- tools/lammps-gui/codeeditor.cpp | 45 +++++++++++++++++++++++++++++++-- tools/lammps-gui/codeeditor.h | 1 + tools/lammps-gui/lammpsgui.cpp | 22 ++++++++++++++++ tools/lammps-gui/lammpsgui.h | 2 ++ tools/lammps-gui/lammpsgui.ui | 12 +++++++++ 5 files changed, 80 insertions(+), 2 deletions(-) diff --git a/tools/lammps-gui/codeeditor.cpp b/tools/lammps-gui/codeeditor.cpp index 2695eca170..2d349e28ab 100644 --- a/tools/lammps-gui/codeeditor.cpp +++ b/tools/lammps-gui/codeeditor.cpp @@ -12,6 +12,7 @@ ------------------------------------------------------------------------- */ #include "codeeditor.h" +#include "fileviewer.h" #include "lammpsgui.h" #include "linenumberarea.h" @@ -33,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -574,7 +576,7 @@ void CodeEditor::keyPressEvent(QKeyEvent *event) auto line = cursor.block().text(); if (line.isEmpty()) return; - // QTextCursor::WordUnderCursor is unusable here since recognizes '/' as word boundary. + // QTextCursor::WordUnderCursor is unusable here since it recognizes '/' as word boundary. // Work around it by manually searching for the location of the beginning of the word. int begin = qMin(cursor.positionInBlock(), line.length() - 1); @@ -736,6 +738,38 @@ void CodeEditor::contextMenuEvent(QContextMenuEvent *event) connect(action2, &QAction::triggered, this, &CodeEditor::open_help); } } + + // check if word under cursor is file + { + auto cursor = textCursor(); + auto line = cursor.block().text(); + if (!line.isEmpty()) { + // QTextCursor::WordUnderCursor is unusable here since it recognizes '/' as word + // boundary. Work around it by manually searching for the location of the beginning of + // the word. + int begin = qMin(cursor.positionInBlock(), line.length() - 1); + + while (begin >= 0) { + if (line[begin].isSpace()) break; + --begin; + } + int end = begin + 1; + while (end < line.length()) { + if (line[end].isSpace()) break; + ++end; + } + + QString word = line.mid(begin, end - begin).trimmed(); + QFileInfo fi(word); + if (fi.exists() && fi.isFile()) { + auto *action = menu->addAction(QString("View file '%1'").arg(word)); + action->setIcon(QIcon(":/icons/document-open.png")); + action->setData(word); + connect(action, &QAction::triggered, this, &CodeEditor::view_file); + } + } + } + auto *action = menu->addAction(QString("LAMMPS Manual")); action->setIcon(QIcon(":/icons/help-browser.png")); action->setData(QString()); @@ -1091,7 +1125,7 @@ void CodeEditor::insertCompletedCommand(const QString &completion) if (completer->widget() != this) return; // select the entire word (non-space text) under the cursor - // we need to do it in this compicated way, since QTextCursor does not recognize + // we need to do it in this complicated way, since QTextCursor does not recognize // special characters as part of a word. auto cursor = textCursor(); auto line = cursor.block().text(); @@ -1174,6 +1208,13 @@ void CodeEditor::open_help() QUrl(QString("https://docs.lammps.org/%1").arg(act->data().toString()))); } +void CodeEditor::view_file() +{ + auto *act = qobject_cast(sender()); + auto *viewer = new FileViewer(act->data().toString()); + viewer->show(); +} + // Local Variables: // c-basic-offset: 4 // End: diff --git a/tools/lammps-gui/codeeditor.h b/tools/lammps-gui/codeeditor.h index 744953af06..b36f0d8de0 100644 --- a/tools/lammps-gui/codeeditor.h +++ b/tools/lammps-gui/codeeditor.h @@ -78,6 +78,7 @@ private slots: void get_help(); void find_help(QString &page, QString &help); void open_help(); + void view_file(); void reformatCurrentLine(); void runCompletion(); void insertCompletedCommand(const QString &completion); diff --git a/tools/lammps-gui/lammpsgui.cpp b/tools/lammps-gui/lammpsgui.cpp index 101ee72484..99fc75d869 100644 --- a/tools/lammps-gui/lammpsgui.cpp +++ b/tools/lammps-gui/lammpsgui.cpp @@ -14,6 +14,7 @@ #include "lammpsgui.h" #include "chartviewer.h" +#include "fileviewer.h" #include "helpers.h" #include "highlighter.h" #include "imageviewer.h" @@ -193,6 +194,7 @@ LammpsGui::LammpsGui(QWidget *parent, const char *filename) : connect(ui->actionOpen, &QAction::triggered, this, &LammpsGui::open); connect(ui->actionSave, &QAction::triggered, this, &LammpsGui::save); connect(ui->actionSave_As, &QAction::triggered, this, &LammpsGui::save_as); + connect(ui->actionView, &QAction::triggered, this, &LammpsGui::view); connect(ui->actionQuit, &QAction::triggered, this, &LammpsGui::quit); connect(ui->actionCopy, &QAction::triggered, this, &LammpsGui::copy); connect(ui->actionCut, &QAction::triggered, this, &LammpsGui::cut); @@ -394,6 +396,12 @@ void LammpsGui::open() open_file(fileName); } +void LammpsGui::view() +{ + QString fileName = QFileDialog::getOpenFileName(this, "Open the file"); + view_file(fileName); +} + void LammpsGui::open_recent() { auto *act = qobject_cast(sender()); @@ -640,6 +648,20 @@ void LammpsGui::open_file(const QString &fileName) lammps.close(); } +// open file in read-only mode for viewing in separate window +void LammpsGui::view_file(const QString &fileName) +{ + QFile file(fileName); + if (!file.open(QIODevice::ReadOnly | QFile::Text)) { + QMessageBox::warning(this, "Warning", + "Cannot open file " + fileName + ": " + file.errorString() + ".\n"); + } else { + file.close(); + auto *viewer = new FileViewer(fileName); + viewer->show(); + } +} + // write file and update CWD to its folder void LammpsGui::write_file(const QString &fileName) diff --git a/tools/lammps-gui/lammpsgui.h b/tools/lammps-gui/lammpsgui.h index 0dd34f2c49..f41266b485 100644 --- a/tools/lammps-gui/lammpsgui.h +++ b/tools/lammps-gui/lammpsgui.h @@ -61,6 +61,7 @@ public: protected: void open_file(const QString &filename); + void view_file(const QString &filename); void write_file(const QString &filename); void update_recents(const QString &filename = ""); void update_variables(); @@ -75,6 +76,7 @@ public slots: private slots: void new_document(); void open(); + void view(); void open_recent(); void start_exe(); void save(); diff --git a/tools/lammps-gui/lammpsgui.ui b/tools/lammps-gui/lammpsgui.ui index ce66fd9515..d0354feb6c 100644 --- a/tools/lammps-gui/lammpsgui.ui +++ b/tools/lammps-gui/lammpsgui.ui @@ -37,6 +37,7 @@ + @@ -125,6 +126,17 @@ Ctrl+O + + + + + + &View + + + Ctrl+Shift+F + +