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 "codeeditor.h"
|
||||||
|
#include "fileviewer.h"
|
||||||
#include "lammpsgui.h"
|
#include "lammpsgui.h"
|
||||||
#include "linenumberarea.h"
|
#include "linenumberarea.h"
|
||||||
|
|
||||||
@ -33,6 +34,7 @@
|
|||||||
#include <QSettings>
|
#include <QSettings>
|
||||||
#include <QShortcut>
|
#include <QShortcut>
|
||||||
#include <QStringListModel>
|
#include <QStringListModel>
|
||||||
|
#include <QStringRef>
|
||||||
#include <QTextBlock>
|
#include <QTextBlock>
|
||||||
#include <QTextDocumentFragment>
|
#include <QTextDocumentFragment>
|
||||||
#include <QUrl>
|
#include <QUrl>
|
||||||
@ -574,7 +576,7 @@ void CodeEditor::keyPressEvent(QKeyEvent *event)
|
|||||||
auto line = cursor.block().text();
|
auto line = cursor.block().text();
|
||||||
if (line.isEmpty()) return;
|
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.
|
// Work around it by manually searching for the location of the beginning of the word.
|
||||||
int begin = qMin(cursor.positionInBlock(), line.length() - 1);
|
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);
|
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"));
|
auto *action = menu->addAction(QString("LAMMPS Manual"));
|
||||||
action->setIcon(QIcon(":/icons/help-browser.png"));
|
action->setIcon(QIcon(":/icons/help-browser.png"));
|
||||||
action->setData(QString());
|
action->setData(QString());
|
||||||
@ -1091,7 +1125,7 @@ void CodeEditor::insertCompletedCommand(const QString &completion)
|
|||||||
if (completer->widget() != this) return;
|
if (completer->widget() != this) return;
|
||||||
|
|
||||||
// select the entire word (non-space text) under the cursor
|
// 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.
|
// special characters as part of a word.
|
||||||
auto cursor = textCursor();
|
auto cursor = textCursor();
|
||||||
auto line = cursor.block().text();
|
auto line = cursor.block().text();
|
||||||
@ -1174,6 +1208,13 @@ void CodeEditor::open_help()
|
|||||||
QUrl(QString("https://docs.lammps.org/%1").arg(act->data().toString())));
|
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:
|
// Local Variables:
|
||||||
// c-basic-offset: 4
|
// c-basic-offset: 4
|
||||||
// End:
|
// End:
|
||||||
|
|||||||
@ -78,6 +78,7 @@ private slots:
|
|||||||
void get_help();
|
void get_help();
|
||||||
void find_help(QString &page, QString &help);
|
void find_help(QString &page, QString &help);
|
||||||
void open_help();
|
void open_help();
|
||||||
|
void view_file();
|
||||||
void reformatCurrentLine();
|
void reformatCurrentLine();
|
||||||
void runCompletion();
|
void runCompletion();
|
||||||
void insertCompletedCommand(const QString &completion);
|
void insertCompletedCommand(const QString &completion);
|
||||||
|
|||||||
@ -14,6 +14,7 @@
|
|||||||
#include "lammpsgui.h"
|
#include "lammpsgui.h"
|
||||||
|
|
||||||
#include "chartviewer.h"
|
#include "chartviewer.h"
|
||||||
|
#include "fileviewer.h"
|
||||||
#include "helpers.h"
|
#include "helpers.h"
|
||||||
#include "highlighter.h"
|
#include "highlighter.h"
|
||||||
#include "imageviewer.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->actionOpen, &QAction::triggered, this, &LammpsGui::open);
|
||||||
connect(ui->actionSave, &QAction::triggered, this, &LammpsGui::save);
|
connect(ui->actionSave, &QAction::triggered, this, &LammpsGui::save);
|
||||||
connect(ui->actionSave_As, &QAction::triggered, this, &LammpsGui::save_as);
|
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->actionQuit, &QAction::triggered, this, &LammpsGui::quit);
|
||||||
connect(ui->actionCopy, &QAction::triggered, this, &LammpsGui::copy);
|
connect(ui->actionCopy, &QAction::triggered, this, &LammpsGui::copy);
|
||||||
connect(ui->actionCut, &QAction::triggered, this, &LammpsGui::cut);
|
connect(ui->actionCut, &QAction::triggered, this, &LammpsGui::cut);
|
||||||
@ -394,6 +396,12 @@ void LammpsGui::open()
|
|||||||
open_file(fileName);
|
open_file(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LammpsGui::view()
|
||||||
|
{
|
||||||
|
QString fileName = QFileDialog::getOpenFileName(this, "Open the file");
|
||||||
|
view_file(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
void LammpsGui::open_recent()
|
void LammpsGui::open_recent()
|
||||||
{
|
{
|
||||||
auto *act = qobject_cast<QAction *>(sender());
|
auto *act = qobject_cast<QAction *>(sender());
|
||||||
@ -640,6 +648,20 @@ void LammpsGui::open_file(const QString &fileName)
|
|||||||
lammps.close();
|
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
|
// write file and update CWD to its folder
|
||||||
|
|
||||||
void LammpsGui::write_file(const QString &fileName)
|
void LammpsGui::write_file(const QString &fileName)
|
||||||
|
|||||||
@ -61,6 +61,7 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
void open_file(const QString &filename);
|
void open_file(const QString &filename);
|
||||||
|
void view_file(const QString &filename);
|
||||||
void write_file(const QString &filename);
|
void write_file(const QString &filename);
|
||||||
void update_recents(const QString &filename = "");
|
void update_recents(const QString &filename = "");
|
||||||
void update_variables();
|
void update_variables();
|
||||||
@ -75,6 +76,7 @@ public slots:
|
|||||||
private slots:
|
private slots:
|
||||||
void new_document();
|
void new_document();
|
||||||
void open();
|
void open();
|
||||||
|
void view();
|
||||||
void open_recent();
|
void open_recent();
|
||||||
void start_exe();
|
void start_exe();
|
||||||
void save();
|
void save();
|
||||||
|
|||||||
@ -37,6 +37,7 @@
|
|||||||
<addaction name="actionNew"/>
|
<addaction name="actionNew"/>
|
||||||
<addaction name="separator"/>
|
<addaction name="separator"/>
|
||||||
<addaction name="actionOpen"/>
|
<addaction name="actionOpen"/>
|
||||||
|
<addaction name="actionView"/>
|
||||||
<addaction name="separator"/>
|
<addaction name="separator"/>
|
||||||
<addaction name="action_1"/>
|
<addaction name="action_1"/>
|
||||||
<addaction name="action_2"/>
|
<addaction name="action_2"/>
|
||||||
@ -125,6 +126,17 @@
|
|||||||
<string>Ctrl+O</string>
|
<string>Ctrl+O</string>
|
||||||
</property>
|
</property>
|
||||||
</action>
|
</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">
|
<action name="actionSave">
|
||||||
<property name="icon">
|
<property name="icon">
|
||||||
<iconset theme=":/icons/document-save.png"/>
|
<iconset theme=":/icons/document-save.png"/>
|
||||||
|
|||||||
Reference in New Issue
Block a user