file viewer can be called from File menu or context menu in editor if word under cursor is a file
This commit is contained in:
@ -12,6 +12,7 @@
|
||||
------------------------------------------------------------------------- */
|
||||
|
||||
#include "codeeditor.h"
|
||||
#include "fileviewer.h"
|
||||
#include "lammpsgui.h"
|
||||
#include "linenumberarea.h"
|
||||
|
||||
@ -33,6 +34,7 @@
|
||||
#include <QSettings>
|
||||
#include <QShortcut>
|
||||
#include <QStringListModel>
|
||||
#include <QStringRef>
|
||||
#include <QTextBlock>
|
||||
#include <QTextDocumentFragment>
|
||||
#include <QUrl>
|
||||
@ -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<QAction *>(sender());
|
||||
auto *viewer = new FileViewer(act->data().toString());
|
||||
viewer->show();
|
||||
}
|
||||
|
||||
// Local Variables:
|
||||
// c-basic-offset: 4
|
||||
// End:
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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<QAction *>(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)
|
||||
|
||||
@ -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();
|
||||
|
||||
@ -37,6 +37,7 @@
|
||||
<addaction name="actionNew"/>
|
||||
<addaction name="separator"/>
|
||||
<addaction name="actionOpen"/>
|
||||
<addaction name="actionView"/>
|
||||
<addaction name="separator"/>
|
||||
<addaction name="action_1"/>
|
||||
<addaction name="action_2"/>
|
||||
@ -125,6 +126,17 @@
|
||||
<string>Ctrl+O</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actionView">
|
||||
<property name="icon">
|
||||
<iconset theme=":/icons/document-open.png"/>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>&View</string>
|
||||
</property>
|
||||
<property name="shortcut">
|
||||
<string>Ctrl+Shift+F</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actionSave">
|
||||
<property name="icon">
|
||||
<iconset theme=":/icons/document-save.png"/>
|
||||
|
||||
Reference in New Issue
Block a user