implement context specific help either via right-click or CTRL-?

This commit is contained in:
Axel Kohlmeyer
2023-08-12 17:22:34 -04:00
parent 3957b4b194
commit bbcfbe957b
9 changed files with 1618 additions and 14 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.16) 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_AUTOUIC ON)
set(CMAKE_AUTOMOC ON) set(CMAKE_AUTOMOC ON)

View File

@ -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) - 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 - 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 - 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. - add a "python" mode, where instead of launching LAMMPS, python is loaded that the LAMMPS python module is made available.

View File

@ -15,19 +15,59 @@
#include "lammpsgui.h" #include "lammpsgui.h"
#include "linenumberarea.h" #include "linenumberarea.h"
#include <QAction>
#include <QDesktopServices>
#include <QDragEnterEvent> #include <QDragEnterEvent>
#include <QDropEvent> #include <QDropEvent>
#include <QIcon>
#include <QKeySequence>
#include <QMenu>
#include <QMimeData> #include <QMimeData>
#include <QPainter> #include <QPainter>
#include <QRegularExpression>
#include <QTextBlock> #include <QTextBlock>
#include <QUrl>
CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent) 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::blockCountChanged, this, &CodeEditor::updateLineNumberAreaWidth);
connect(this, &CodeEditor::updateRequest, this, &CodeEditor::updateLineNumberArea); connect(this, &CodeEditor::updateRequest, this, &CodeEditor::updateLineNumberArea);
updateLineNumberAreaWidth(0); 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: // Local Variables:
// c-basic-offset: 4 // c-basic-offset: 4
// End: // End:

View File

@ -14,7 +14,10 @@
#ifndef CODEEDITOR_H #ifndef CODEEDITOR_H
#define CODEEDITOR_H #define CODEEDITOR_H
#include <QMap>
#include <QPlainTextEdit> #include <QPlainTextEdit>
#include <QShortcut>
#include <QString>
class CodeEditor : public QPlainTextEdit { class CodeEditor : public QPlainTextEdit {
Q_OBJECT Q_OBJECT
@ -30,14 +33,29 @@ protected:
void dragEnterEvent(QDragEnterEvent *event) override; void dragEnterEvent(QDragEnterEvent *event) override;
bool canInsertFromMimeData(const QMimeData *source) const override; bool canInsertFromMimeData(const QMimeData *source) const override;
void dropEvent(QDropEvent *event) override; void dropEvent(QDropEvent *event) override;
void contextMenuEvent(QContextMenuEvent *event) override;
private slots: private slots:
void updateLineNumberAreaWidth(int newBlockCount); void updateLineNumberAreaWidth(int newBlockCount);
void highlightCurrentLine(); void highlightCurrentLine();
void updateLineNumberArea(const QRect &rect, int dy); void updateLineNumberArea(const QRect &rect, int dy);
void get_help();
void find_help(QString &page, QString &help);
void open_help();
private: private:
QWidget *lineNumberArea; 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 #endif

File diff suppressed because it is too large Load Diff

View File

@ -1,29 +1,35 @@
<!DOCTYPE RCC><RCC version="1.0"> <!DOCTYPE RCC><RCC version="1.0">
<qresource> <qresource prefix="/">
<file>lammps-icon-128x128.png</file> <file>lammps-icon-128x128.png</file>
</qresource> </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> <file>gtk-zoom-in.png</file>
</qresource> </qresource>
<qresource> <qresource prefix="/">
<file>gtk-zoom-out.png</file> <file>gtk-zoom-out.png</file>
</qresource> </qresource>
<qresource> <qresource prefix="/">
<file>gtk-zoom-fit.png</file> <file>gtk-zoom-fit.png</file>
</qresource> </qresource>
<qresource> <qresource prefix="/">
<file>edit-delete.png</file> <file>edit-delete.png</file>
</qresource> </qresource>
<qresource> <qresource prefix="/">
<file>object-rotate-right.png</file> <file>object-rotate-right.png</file>
</qresource> </qresource>
<qresource> <qresource prefix="/">
<file>object-rotate-left.png</file> <file>object-rotate-left.png</file>
</qresource> </qresource>
<qresource> <qresource prefix="/">
<file>gtk-go-up.png</file> <file>gtk-go-up.png</file>
</qresource> </qresource>
<qresource> <qresource prefix="/">
<file>gtk-go-down.png</file> <file>gtk-go-down.png</file>
</qresource> </qresource>
</RCC> </RCC>

View File

@ -318,6 +318,9 @@
<property name="toolTip"> <property name="toolTip">
<string>About LAMMPS-GUI</string> <string>About LAMMPS-GUI</string>
</property> </property>
<property name="shortcut">
<string>Ctrl+Shift+A</string>
</property>
</action> </action>
<action name="action_Help"> <action name="action_Help">
<property name="icon"> <property name="icon">
@ -331,7 +334,7 @@
<string>Quick Help</string> <string>Quick Help</string>
</property> </property>
<property name="shortcut"> <property name="shortcut">
<string>Ctrl+Shift+/</string> <string>Ctrl+Shift+H</string>
</property> </property>
</action> </action>
<action name="actionPreferences"> <action name="actionPreferences">
@ -354,6 +357,9 @@
<property name="text"> <property name="text">
<string>LAMMPS &amp;Manual</string> <string>LAMMPS &amp;Manual</string>
</property> </property>
<property name="shortcut">
<string>Ctrl+Shift+M</string>
</property>
</action> </action>
<action name="actionDefaults"> <action name="actionDefaults">
<property name="icon"> <property name="icon">
@ -372,6 +378,9 @@
<property name="text"> <property name="text">
<string>&amp;Log Window</string> <string>&amp;Log Window</string>
</property> </property>
<property name="shortcut">
<string>Ctrl+Shift+L</string>
</property>
</action> </action>
<action name="actionView_Graph_Window"> <action name="actionView_Graph_Window">
<property name="icon"> <property name="icon">
@ -381,6 +390,9 @@
<property name="text"> <property name="text">
<string>&amp;Chart Window</string> <string>&amp;Chart Window</string>
</property> </property>
<property name="shortcut">
<string>Ctrl+Shift+C</string>
</property>
</action> </action>
<action name="action_1"> <action name="action_1">
<property name="text"> <property name="text">
@ -415,6 +427,9 @@
<property name="text"> <property name="text">
<string>&amp;Image Window</string> <string>&amp;Image Window</string>
</property> </property>
<property name="shortcut">
<string>Ctrl+Shift+I</string>
</property>
</action> </action>
<action name="actionSet_Variables"> <action name="actionSet_Variables">
<property name="icon"> <property name="icon">

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

View 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