add reformat to context menu, formatting width may be changed in preferences

This commit is contained in:
Axel Kohlmeyer
2023-08-31 23:25:30 -04:00
parent 7a9aa7950c
commit 156ab0b338
6 changed files with 133 additions and 26 deletions

View File

@ -25,6 +25,7 @@
#include <QMimeData> #include <QMimeData>
#include <QPainter> #include <QPainter>
#include <QRegularExpression> #include <QRegularExpression>
#include <QSettings>
#include <QTextBlock> #include <QTextBlock>
#include <QUrl> #include <QUrl>
@ -212,16 +213,24 @@ QString CodeEditor::reformatLine(const QString &line)
{ {
auto words = split_line(line.toStdString()); auto words = split_line(line.toStdString());
QString newtext; QString newtext;
QSettings settings;
settings.beginGroup("reformat");
int cmdsize = settings.value("command", "16").toInt();
int typesize = settings.value("type", "4").toInt();
int idsize = settings.value("id", "4").toInt();
int namesize = settings.value("name", "8").toInt();
settings.endGroup();
if (words.size()) { if (words.size()) {
// commented line. do nothing // commented line. do nothing
if (words[0][0] == '#') return line; if (words[0][0] == '#') return line;
// append LAMMPS command padded to 16 chars to the next work // start with LAMMPS command plus padding if another word follows
newtext += words[0].c_str(); newtext = words[0].c_str();
if (words.size() > 1) if (words.size() > 1) {
for (int i = 0; i < (15 - words[0].size()); ++i) for (int i = words[0].size() + 1; i < cmdsize; ++i)
newtext += ' '; newtext += ' ';
}
// append remaining words with just a single blank added. // append remaining words with just a single blank added.
for (int i = 1; i < words.size(); ++i) { for (int i = 1; i < words.size(); ++i) {
@ -230,15 +239,31 @@ QString CodeEditor::reformatLine(const QString &line)
// special cases // special cases
if (i < 3) {
// additional space for types or type ranges // additional space for types or type ranges
if ((words[0] == "pair_coeff") && (i < 3)) if (words[0] == "pair_coeff")
for (int j = words[i].size(); j < 4; ++j) for (int j = words[i].size(); j < typesize; ++j)
newtext += ' '; newtext += ' ';
if ((i < 2) && ((words[0] == "bond_coeff") || (words[0] == "angle_coeff") || // pad 4 for IDs and 8 for groups
(words[0] == "dihedral_coeff") || (words[0] == "improper_coeff"))) if ((words[0] == "fix") || (words[0] == "compute") || (words[0] == "dump")) {
for (int j = words[i].size(); j < 4; ++j) if (i == 1) {
for (int j = words[i].size(); j < idsize; ++j)
newtext += ' '; newtext += ' ';
} else if (i == 2) {
for (int j = words[i].size(); j < namesize; ++j)
newtext += ' ';
}
}
}
if (i < 2) {
if ((words[0] == "bond_coeff") || (words[0] == "angle_coeff") ||
(words[0] == "dihedral_coeff") || (words[0] == "improper_coeff") ||
(words[0] == "mass"))
for (int j = words[i].size(); j < typesize; ++j)
newtext += ' ';
}
} }
} }
return newtext; return newtext;
@ -247,18 +272,7 @@ QString CodeEditor::reformatLine(const QString &line)
void CodeEditor::keyPressEvent(QKeyEvent *event) void CodeEditor::keyPressEvent(QKeyEvent *event)
{ {
if (event->key() == Qt::Key_Tab) { if (event->key() == Qt::Key_Tab) {
auto cursor = textCursor(); reformatCurrentLine();
auto text = cursor.block().text();
auto newtext = reformatLine(text);
// perform edit but only if text has changed
if (QString::compare(text, newtext)) {
cursor.beginEditBlock();
cursor.movePosition(QTextCursor::StartOfLine);
cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor, 1);
cursor.insertText(newtext);
cursor.endEditBlock();
}
return; return;
} }
if (event->key() == Qt::Key_Backtab) { if (event->key() == Qt::Key_Backtab) {
@ -364,7 +378,12 @@ void CodeEditor::contextMenuEvent(QContextMenuEvent *event)
auto *menu = createStandardContextMenu(); auto *menu = createStandardContextMenu();
if (!page.isEmpty()) { if (!page.isEmpty()) {
menu->addSeparator(); menu->addSeparator();
auto action = menu->addAction(QString("View Documentation for '%1'").arg(help)); auto action = menu->addAction(QString("Reformat '%1' command").arg(help));
action->setIcon(QIcon(":/format-indent-less-3.png"));
connect(action, &QAction::triggered, this, &CodeEditor::reformatCurrentLine);
menu->addSeparator();
action = menu->addAction(QString("View Documentation for '%1'").arg(help));
action->setIcon(QIcon(":/system-help.png")); action->setIcon(QIcon(":/system-help.png"));
action->setData(page); action->setData(page);
connect(action, &QAction::triggered, this, &CodeEditor::open_help); connect(action, &QAction::triggered, this, &CodeEditor::open_help);
@ -390,6 +409,22 @@ void CodeEditor::contextMenuEvent(QContextMenuEvent *event)
delete menu; delete menu;
} }
void CodeEditor::reformatCurrentLine()
{
auto cursor = textCursor();
auto text = cursor.block().text();
auto newtext = reformatLine(text);
// perform edit but only if text has changed
if (QString::compare(text, newtext)) {
cursor.beginEditBlock();
cursor.movePosition(QTextCursor::StartOfLine);
cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor, 1);
cursor.insertText(newtext);
cursor.endEditBlock();
}
}
void CodeEditor::get_help() void CodeEditor::get_help()
{ {
QString page, help; QString page, help;

View File

@ -49,6 +49,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 reformatCurrentLine();
private: private:
QWidget *lineNumberArea; QWidget *lineNumberArea;

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

View File

@ -20,6 +20,7 @@
<file>edit-redo.png</file> <file>edit-redo.png</file>
<file>edit-undo.png</file> <file>edit-undo.png</file>
<file>emblem-photos.png</file> <file>emblem-photos.png</file>
<file>format-indent-less-3.png</file>
<file>go-first.png</file> <file>go-first.png</file>
<file>go-last.png</file> <file>go-last.png</file>
<file>go-next-2.png</file> <file>go-next-2.png</file>

View File

@ -37,6 +37,7 @@
#include <QRadioButton> #include <QRadioButton>
#include <QSettings> #include <QSettings>
#include <QSpacerItem> #include <QSpacerItem>
#include <QSpinBox>
#include <QTabWidget> #include <QTabWidget>
#include <QVBoxLayout> #include <QVBoxLayout>
@ -70,6 +71,7 @@ Preferences::Preferences(LammpsWrapper *_lammps, QWidget *parent) :
tabWidget->addTab(new GeneralTab(settings, lammps), "&General Settings"); tabWidget->addTab(new GeneralTab(settings, lammps), "&General Settings");
tabWidget->addTab(new AcceleratorTab(settings, lammps), "&Accelerators"); tabWidget->addTab(new AcceleratorTab(settings, lammps), "&Accelerators");
tabWidget->addTab(new SnapshotTab(settings), "&Snapshot Image"); tabWidget->addTab(new SnapshotTab(settings), "&Snapshot Image");
tabWidget->addTab(new EditorTab(settings), "&Editor Settings");
connect(buttonBox, &QDialogButtonBox::accepted, this, &Preferences::accept); connect(buttonBox, &QDialogButtonBox::accepted, this, &Preferences::accept);
connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
@ -80,7 +82,7 @@ Preferences::Preferences(LammpsWrapper *_lammps, QWidget *parent) :
setLayout(layout); setLayout(layout);
setWindowIcon(QIcon(":/lammps-icon-128x128.png")); setWindowIcon(QIcon(":/lammps-icon-128x128.png"));
setWindowTitle("LAMMPS-GUI - Preferences"); setWindowTitle("LAMMPS-GUI - Preferences");
resize(500, 400); resize(600, 450);
} }
Preferences::~Preferences() Preferences::~Preferences()
@ -175,6 +177,20 @@ void Preferences::accept()
const char *arg0 = mystrdup(QCoreApplication::arguments().at(0).toStdString()); const char *arg0 = mystrdup(QCoreApplication::arguments().at(0).toStdString());
execl(path, arg0, (char *)NULL); execl(path, arg0, (char *)NULL);
} }
// reformattting settings
settings->beginGroup("reformat");
auto spin = tabWidget->findChild<QSpinBox *>("cmdval");
if (spin) settings->setValue("command", spin->value());
spin = tabWidget->findChild<QSpinBox *>("typeval");
if (spin) settings->setValue("type", spin->value());
spin = tabWidget->findChild<QSpinBox *>("idval");
if (spin) settings->setValue("id", spin->value());
spin = tabWidget->findChild<QSpinBox *>("nameval");
if (spin) settings->setValue("name", spin->value());
settings->endGroup();
QDialog::accept(); QDialog::accept();
} }
@ -483,6 +499,50 @@ SnapshotTab::SnapshotTab(QSettings *_settings, QWidget *parent) :
setLayout(grid); setLayout(grid);
} }
EditorTab::EditorTab(QSettings *_settings, QWidget *parent) : QWidget(parent), settings(_settings)
{
settings->beginGroup("reformat");
auto *grid = new QGridLayout;
auto *reformat = new QLabel("Tab Reformatting settings:");
auto *cmdlbl = new QLabel("Command width:");
auto *typelbl = new QLabel("Type width:");
auto *idlbl = new QLabel("ID width:");
auto *namelbl = new QLabel("Name width:");
auto *cmdval = new QSpinBox;
auto *typeval = new QSpinBox;
auto *idval = new QSpinBox;
auto *nameval = new QSpinBox;
cmdval->setRange(1, 32);
cmdval->setValue(settings->value("command", "16").toInt());
cmdval->setObjectName("cmdval");
typeval->setRange(1, 32);
typeval->setValue(settings->value("type", "4").toInt());
typeval->setObjectName("typeval");
idval->setRange(1, 32);
idval->setValue(settings->value("id", "8").toInt());
idval->setObjectName("idval");
nameval->setRange(1, 32);
nameval->setValue(settings->value("name", "8").toInt());
nameval->setObjectName("nameval");
settings->endGroup();
int i = 0;
grid->addWidget(reformat, i++, 0, 1, 2, Qt::AlignTop | Qt::AlignHCenter);
grid->addWidget(cmdlbl, i, 0, Qt::AlignTop);
grid->addWidget(cmdval, i++, 1, Qt::AlignTop);
grid->addWidget(typelbl, i, 0, Qt::AlignTop);
grid->addWidget(typeval, i++, 1, Qt::AlignTop);
grid->addWidget(idlbl, i, 0, Qt::AlignTop);
grid->addWidget(idval, i++, 1, Qt::AlignTop);
grid->addWidget(namelbl, i, 0, Qt::AlignTop);
grid->addWidget(nameval, i++, 1, Qt::AlignTop);
grid->addItem(new QSpacerItem(100, 100, QSizePolicy::Minimum, QSizePolicy::Expanding), i, 0);
grid->addItem(new QSpacerItem(100, 100, QSizePolicy::Minimum, QSizePolicy::Expanding), i, 1);
grid->addItem(new QSpacerItem(100, 100, QSizePolicy::Expanding, QSizePolicy::Expanding), i, 2);
setLayout(grid);
}
// Local Variables: // Local Variables:
// c-basic-offset: 4 // c-basic-offset: 4
// End: // End:

View File

@ -83,6 +83,16 @@ private:
QSettings *settings; QSettings *settings;
}; };
class EditorTab : public QWidget {
Q_OBJECT
public:
explicit EditorTab(QSettings *settings, QWidget *parent = nullptr);
private:
QSettings *settings;
};
#endif #endif
// Local Variables: // Local Variables: