implement context specific help either via right-click or CTRL-?
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
project(lammps-gui VERSION 1.1.7 LANGUAGES CXX)
|
||||
project(lammps-gui VERSION 1.1.8 LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_AUTOUIC ON)
|
||||
set(CMAKE_AUTOMOC ON)
|
||||
|
||||
@ -25,5 +25,4 @@ LAMMPS-GUI TODO list:
|
||||
- support single stepping, i.e. process input line by line (need to detect continuation chars!) with highlighting active line(s)
|
||||
- have command text input file in/above status bar where individual commands can be tested. have insert button to copy line into file at the current point
|
||||
- support text completion as done with lammps-shell
|
||||
- have context menu for known commands to offer retrieving help by dispatching URL to webbrowser (process index from sphinx for that purpose)
|
||||
- add a "python" mode, where instead of launching LAMMPS, python is loaded that the LAMMPS python module is made available.
|
||||
|
||||
@ -15,19 +15,59 @@
|
||||
#include "lammpsgui.h"
|
||||
#include "linenumberarea.h"
|
||||
|
||||
#include <QAction>
|
||||
#include <QDesktopServices>
|
||||
#include <QDragEnterEvent>
|
||||
#include <QDropEvent>
|
||||
#include <QIcon>
|
||||
#include <QKeySequence>
|
||||
#include <QMenu>
|
||||
#include <QMimeData>
|
||||
#include <QPainter>
|
||||
#include <QRegularExpression>
|
||||
#include <QTextBlock>
|
||||
#include <QUrl>
|
||||
|
||||
CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent)
|
||||
{
|
||||
lineNumberArea = new LineNumberArea(this);
|
||||
help_action = new QShortcut(QKeySequence::fromString("Ctrl+?"), parent);
|
||||
connect(help_action, &QShortcut::activated, this, &CodeEditor::get_help);
|
||||
|
||||
// initialize help system
|
||||
QFile help_index(":/help_index.table");
|
||||
if (help_index.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||
while (!help_index.atEnd()) {
|
||||
auto line = QString(help_index.readLine());
|
||||
auto words = line.trimmed().split(' ');
|
||||
if (words.size() > 2) {
|
||||
if (words.at(1) == "pair_style") {
|
||||
pair_map[words.at(2)] = words.at(0);
|
||||
} else if (words.at(1) == "bond_style") {
|
||||
bond_map[words.at(2)] = words.at(0);
|
||||
} else if (words.at(1) == "angle_style") {
|
||||
angle_map[words.at(2)] = words.at(0);
|
||||
} else if (words.at(1) == "dihedral_style") {
|
||||
dihedral_map[words.at(2)] = words.at(0);
|
||||
} else if (words.at(1) == "improper_style") {
|
||||
improper_map[words.at(2)] = words.at(0);
|
||||
} else if (words.at(1) == "fix") {
|
||||
fix_map[words.at(2)] = words.at(0);
|
||||
} else if (words.at(1) == "compute") {
|
||||
compute_map[words.at(2)] = words.at(0);
|
||||
}
|
||||
// ignoring: min_style, kspace_style, dump, fix_modify ATC
|
||||
} else if (words.size() == 2) {
|
||||
cmd_map[words.at(1)] = words.at(0);
|
||||
} else {
|
||||
fprintf(stderr, "unhandled: %s", line.toStdString().c_str());
|
||||
}
|
||||
}
|
||||
help_index.close();
|
||||
}
|
||||
|
||||
lineNumberArea = new LineNumberArea(this);
|
||||
connect(this, &CodeEditor::blockCountChanged, this, &CodeEditor::updateLineNumberAreaWidth);
|
||||
connect(this, &CodeEditor::updateRequest, this, &CodeEditor::updateLineNumberArea);
|
||||
|
||||
updateLineNumberAreaWidth(0);
|
||||
}
|
||||
|
||||
@ -138,6 +178,85 @@ void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
|
||||
}
|
||||
}
|
||||
|
||||
void CodeEditor::contextMenuEvent(QContextMenuEvent *event)
|
||||
{
|
||||
// reposition the cursor here?
|
||||
QString page, help;
|
||||
find_help(page, help);
|
||||
|
||||
// print augmented context menu if an entry was found
|
||||
auto *menu = createStandardContextMenu();
|
||||
if (!page.isEmpty()) {
|
||||
menu->addSeparator();
|
||||
auto action = menu->addAction(QString("Look up help for '%1'").arg(help));
|
||||
action->setIcon(QIcon(":/system-help.png"));
|
||||
action->setData(page);
|
||||
connect(action, &QAction::triggered, this, &CodeEditor::open_help);
|
||||
}
|
||||
menu->exec(event->globalPos());
|
||||
delete menu;
|
||||
}
|
||||
|
||||
void CodeEditor::get_help()
|
||||
{
|
||||
QString page, help;
|
||||
find_help(page, help);
|
||||
if (!page.isEmpty())
|
||||
QDesktopServices::openUrl(QUrl(QString("https://docs.lammps.org/%1").arg(page)));
|
||||
}
|
||||
|
||||
void CodeEditor::find_help(QString &page, QString &help)
|
||||
{
|
||||
// process line of text where the cursor is
|
||||
auto text = textCursor().block().text().replace('\t', ' ').trimmed();
|
||||
auto style =
|
||||
QRegularExpression("^(pair|bond|angle|dihedral|improper)_style\\s+(\\S+)").match(text);
|
||||
help.clear();
|
||||
page.clear();
|
||||
if (style.hasMatch()) {
|
||||
if (style.captured(1) == "pair") {
|
||||
page = pair_map.value(style.captured(2), QString());
|
||||
help = QString("pair_style %1").arg(style.captured(2));
|
||||
} else if (style.captured(1) == "bond") {
|
||||
page = bond_map.value(style.captured(2), QString());
|
||||
help = QString("bond_style %1").arg(style.captured(2));
|
||||
} else if (style.captured(1) == "angle") {
|
||||
page = angle_map.value(style.captured(2), QString());
|
||||
help = QString("angle_style %1").arg(style.captured(2));
|
||||
} else if (style.captured(1) == "dihedral") {
|
||||
page = dihedral_map.value(style.captured(2), QString());
|
||||
help = QString("dihedral_style %1").arg(style.captured(2));
|
||||
} else if (style.captured(1) == "improper") {
|
||||
page = improper_map.value(style.captured(2), QString());
|
||||
help = QString("improper_style %1").arg(style.captured(2));
|
||||
}
|
||||
}
|
||||
|
||||
style = QRegularExpression("^(fix|compute)\\s+\\w+\\s+\\w+\\s+(\\S+)").match(text);
|
||||
if (style.hasMatch()) {
|
||||
help = QString("%1 %2").arg(style.captured(1), style.captured(2));
|
||||
if (style.captured(1) == "fix") {
|
||||
page = fix_map.value(style.captured(2), QString());
|
||||
} else if (style.captured(1) == "compute") {
|
||||
page = compute_map.value(style.captured(2), QString());
|
||||
}
|
||||
}
|
||||
|
||||
// could not find a matching "style", now try the plain command
|
||||
if (page.isEmpty() && !text.isEmpty()) {
|
||||
auto cmd = text.split(' ').at(0);
|
||||
help = cmd;
|
||||
page = cmd_map.value(cmd, QString());
|
||||
}
|
||||
}
|
||||
|
||||
void CodeEditor::open_help()
|
||||
{
|
||||
QAction *act = qobject_cast<QAction *>(sender());
|
||||
QDesktopServices::openUrl(
|
||||
QUrl(QString("https://docs.lammps.org/%1").arg(act->data().toString())));
|
||||
}
|
||||
|
||||
// Local Variables:
|
||||
// c-basic-offset: 4
|
||||
// End:
|
||||
|
||||
@ -14,7 +14,10 @@
|
||||
#ifndef CODEEDITOR_H
|
||||
#define CODEEDITOR_H
|
||||
|
||||
#include <QMap>
|
||||
#include <QPlainTextEdit>
|
||||
#include <QShortcut>
|
||||
#include <QString>
|
||||
|
||||
class CodeEditor : public QPlainTextEdit {
|
||||
Q_OBJECT
|
||||
@ -30,14 +33,29 @@ protected:
|
||||
void dragEnterEvent(QDragEnterEvent *event) override;
|
||||
bool canInsertFromMimeData(const QMimeData *source) const override;
|
||||
void dropEvent(QDropEvent *event) override;
|
||||
void contextMenuEvent(QContextMenuEvent *event) override;
|
||||
|
||||
private slots:
|
||||
void updateLineNumberAreaWidth(int newBlockCount);
|
||||
void highlightCurrentLine();
|
||||
void updateLineNumberArea(const QRect &rect, int dy);
|
||||
void get_help();
|
||||
void find_help(QString &page, QString &help);
|
||||
void open_help();
|
||||
|
||||
private:
|
||||
QWidget *lineNumberArea;
|
||||
QShortcut *help_action;
|
||||
|
||||
QMap<QString, QString> cmd_map;
|
||||
QMap<QString, QString> fix_map;
|
||||
QMap<QString, QString> compute_map;
|
||||
QMap<QString, QString> pair_map;
|
||||
QMap<QString, QString> bond_map;
|
||||
QMap<QString, QString> angle_map;
|
||||
QMap<QString, QString> dihedral_map;
|
||||
QMap<QString, QString> improper_map;
|
||||
QMap<QString, QString> dump_map;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
1443
tools/lammps-gui/help_index.table
Normal file
1443
tools/lammps-gui/help_index.table
Normal file
File diff suppressed because it is too large
Load Diff
@ -1,29 +1,35 @@
|
||||
<!DOCTYPE RCC><RCC version="1.0">
|
||||
<qresource>
|
||||
<qresource prefix="/">
|
||||
<file>lammps-icon-128x128.png</file>
|
||||
</qresource>
|
||||
<qresource>
|
||||
<qresource prefix="/">
|
||||
<file>help_index.table</file>
|
||||
</qresource>
|
||||
<qresource prefix="/">
|
||||
<file>system-help.png</file>
|
||||
</qresource>
|
||||
<qresource prefix="/">
|
||||
<file>gtk-zoom-in.png</file>
|
||||
</qresource>
|
||||
<qresource>
|
||||
<qresource prefix="/">
|
||||
<file>gtk-zoom-out.png</file>
|
||||
</qresource>
|
||||
<qresource>
|
||||
<qresource prefix="/">
|
||||
<file>gtk-zoom-fit.png</file>
|
||||
</qresource>
|
||||
<qresource>
|
||||
<qresource prefix="/">
|
||||
<file>edit-delete.png</file>
|
||||
</qresource>
|
||||
<qresource>
|
||||
<qresource prefix="/">
|
||||
<file>object-rotate-right.png</file>
|
||||
</qresource>
|
||||
<qresource>
|
||||
<qresource prefix="/">
|
||||
<file>object-rotate-left.png</file>
|
||||
</qresource>
|
||||
<qresource>
|
||||
<qresource prefix="/">
|
||||
<file>gtk-go-up.png</file>
|
||||
</qresource>
|
||||
<qresource>
|
||||
<qresource prefix="/">
|
||||
<file>gtk-go-down.png</file>
|
||||
</qresource>
|
||||
</RCC>
|
||||
|
||||
@ -318,6 +318,9 @@
|
||||
<property name="toolTip">
|
||||
<string>About LAMMPS-GUI</string>
|
||||
</property>
|
||||
<property name="shortcut">
|
||||
<string>Ctrl+Shift+A</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="action_Help">
|
||||
<property name="icon">
|
||||
@ -331,7 +334,7 @@
|
||||
<string>Quick Help</string>
|
||||
</property>
|
||||
<property name="shortcut">
|
||||
<string>Ctrl+Shift+/</string>
|
||||
<string>Ctrl+Shift+H</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actionPreferences">
|
||||
@ -354,6 +357,9 @@
|
||||
<property name="text">
|
||||
<string>LAMMPS &Manual</string>
|
||||
</property>
|
||||
<property name="shortcut">
|
||||
<string>Ctrl+Shift+M</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actionDefaults">
|
||||
<property name="icon">
|
||||
@ -372,6 +378,9 @@
|
||||
<property name="text">
|
||||
<string>&Log Window</string>
|
||||
</property>
|
||||
<property name="shortcut">
|
||||
<string>Ctrl+Shift+L</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actionView_Graph_Window">
|
||||
<property name="icon">
|
||||
@ -381,6 +390,9 @@
|
||||
<property name="text">
|
||||
<string>&Chart Window</string>
|
||||
</property>
|
||||
<property name="shortcut">
|
||||
<string>Ctrl+Shift+C</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="action_1">
|
||||
<property name="text">
|
||||
@ -415,6 +427,9 @@
|
||||
<property name="text">
|
||||
<string>&Image Window</string>
|
||||
</property>
|
||||
<property name="shortcut">
|
||||
<string>Ctrl+Shift+I</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actionSet_Variables">
|
||||
<property name="icon">
|
||||
|
||||
BIN
tools/lammps-gui/system-help.png
Normal file
BIN
tools/lammps-gui/system-help.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.5 KiB |
4
tools/lammps-gui/update-help-index.sh
Executable file
4
tools/lammps-gui/update-help-index.sh
Executable file
@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
# this updates the help index table
|
||||
|
||||
grep '\.\. index::' ../../doc/src/*.rst | sort | sed -e 's/^.*src\/\([^/]\+\)\.rst:/\1.html /' -e 's/\.\. \+index:: \+//' > help_index.table
|
||||
Reference in New Issue
Block a user