From 156ab0b338f7030d8b8109bcebdd169f131118e6 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Thu, 31 Aug 2023 23:25:30 -0400 Subject: [PATCH] add reformat to context menu, formatting width may be changed in preferences --- tools/lammps-gui/codeeditor.cpp | 85 +++++++++++++++------- tools/lammps-gui/codeeditor.h | 1 + tools/lammps-gui/format-indent-less-3.png | Bin 0 -> 2555 bytes tools/lammps-gui/lammpsgui.qrc | 1 + tools/lammps-gui/preferences.cpp | 62 +++++++++++++++- tools/lammps-gui/preferences.h | 10 +++ 6 files changed, 133 insertions(+), 26 deletions(-) create mode 100644 tools/lammps-gui/format-indent-less-3.png diff --git a/tools/lammps-gui/codeeditor.cpp b/tools/lammps-gui/codeeditor.cpp index 0f2c5eab70..9eb96e6a7a 100644 --- a/tools/lammps-gui/codeeditor.cpp +++ b/tools/lammps-gui/codeeditor.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -212,16 +213,24 @@ QString CodeEditor::reformatLine(const QString &line) { auto words = split_line(line.toStdString()); 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()) { // commented line. do nothing if (words[0][0] == '#') return line; - // append LAMMPS command padded to 16 chars to the next work - newtext += words[0].c_str(); - if (words.size() > 1) - for (int i = 0; i < (15 - words[0].size()); ++i) + // start with LAMMPS command plus padding if another word follows + newtext = words[0].c_str(); + if (words.size() > 1) { + for (int i = words[0].size() + 1; i < cmdsize; ++i) newtext += ' '; + } // append remaining words with just a single blank added. for (int i = 1; i < words.size(); ++i) { @@ -230,15 +239,31 @@ QString CodeEditor::reformatLine(const QString &line) // special cases - // additional space for types or type ranges - if ((words[0] == "pair_coeff") && (i < 3)) - for (int j = words[i].size(); j < 4; ++j) - newtext += ' '; + if (i < 3) { + // additional space for types or type ranges + if (words[0] == "pair_coeff") + for (int j = words[i].size(); j < typesize; ++j) + newtext += ' '; - if ((i < 2) && ((words[0] == "bond_coeff") || (words[0] == "angle_coeff") || - (words[0] == "dihedral_coeff") || (words[0] == "improper_coeff"))) - for (int j = words[i].size(); j < 4; ++j) - newtext += ' '; + // pad 4 for IDs and 8 for groups + if ((words[0] == "fix") || (words[0] == "compute") || (words[0] == "dump")) { + if (i == 1) { + for (int j = words[i].size(); j < idsize; ++j) + 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; @@ -247,18 +272,7 @@ QString CodeEditor::reformatLine(const QString &line) void CodeEditor::keyPressEvent(QKeyEvent *event) { if (event->key() == Qt::Key_Tab) { - 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(); - } + reformatCurrentLine(); return; } if (event->key() == Qt::Key_Backtab) { @@ -364,7 +378,12 @@ void CodeEditor::contextMenuEvent(QContextMenuEvent *event) auto *menu = createStandardContextMenu(); if (!page.isEmpty()) { 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->setData(page); connect(action, &QAction::triggered, this, &CodeEditor::open_help); @@ -390,6 +409,22 @@ void CodeEditor::contextMenuEvent(QContextMenuEvent *event) 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() { QString page, help; diff --git a/tools/lammps-gui/codeeditor.h b/tools/lammps-gui/codeeditor.h index fb32de17f5..d3e5d50777 100644 --- a/tools/lammps-gui/codeeditor.h +++ b/tools/lammps-gui/codeeditor.h @@ -49,6 +49,7 @@ private slots: void get_help(); void find_help(QString &page, QString &help); void open_help(); + void reformatCurrentLine(); private: QWidget *lineNumberArea; diff --git a/tools/lammps-gui/format-indent-less-3.png b/tools/lammps-gui/format-indent-less-3.png new file mode 100644 index 0000000000000000000000000000000000000000..d4fc5659440fe8aa2b217c8bca492d326665ce63 GIT binary patch literal 2555 zcmai$c{J4BAIHC#WNi>--;%9nLza;r*_X+dEt1g~F=h;A7-MUwgz_Z2WQ!@Xl}wgA z_T{leTC7DS^jKydG@`<9Je}wK&hL-k`Q39q_kGSi@7H~Q?mze5WJh}|VF76Y004w- ztj(P{3g0h2Ue4^!y?uityqD~(%sI*#+h)(NaD+d^`aB5$Y+mgbSGh&X*8XBd$eGI= z{hJ4dnYY{lfbWov`5Bjhnbq?zNaB4Gd%rVhz&0mJvOKzTFoyil!%BW2kQy|m|GjHT zOnWlEy+UOnw@o=K`caqwuk*FfA0FKcvvsaec`zWE38-$FNh+sZJE@esbNBWtGpB24 zc&Nth74&gUSeDOQ=GU)ZhgxQ%Yp<}v!GB2{UFtRK11rQcj+pXUYZQIoNJXIi0ooET z&6b|xqM4;Trocs-^)#!-=;4CJJY^-#B{V)Kj7_lwXn5TWSKHr5i?;E{3%5J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IslWy&|SPP z4n|tOG!0;FajDY_(Off|l>q#(Q*zQmy>4OVr7pv3B8{6L59a@iUBR^0v0LtYwpJ73 z6prC}lnR_COzMU50d=0sazlTFt{bgNrnH+T)urQpAKj0SWu>gS5DLb@LuKsn_c*Qb7MqaZUAiA>|AGp zG6@)h332;Uu@Vjc%5%%Yv5SBQZ7zTcU$zw8S+sDuz0Ui&L;acS>epyK+8r~0s=kr{ zoCnU2Y|Iuk8Q0RQSg8@Jy%Q4EY^qZC4onUJ`m}t=J5#eA3|Zax@W3Pf09GmBwb+3-%kUt*_RD=6`ZVLOB}ASN$_gI@BFY;9X*v+`Z!0*?juv<7#(62jJQ6H*$@Pdkf#txEd&IvhlIluXaWQO0Q*klFNy;o;1D;^XR! h`lol_Ff#OVFp=>0ZZNIScc>gMU}Ir#USo<*_%}xie2M@7 literal 0 HcmV?d00001 diff --git a/tools/lammps-gui/lammpsgui.qrc b/tools/lammps-gui/lammpsgui.qrc index a27d6abbea..f68caade2f 100644 --- a/tools/lammps-gui/lammpsgui.qrc +++ b/tools/lammps-gui/lammpsgui.qrc @@ -20,6 +20,7 @@ edit-redo.png edit-undo.png emblem-photos.png + format-indent-less-3.png go-first.png go-last.png go-next-2.png diff --git a/tools/lammps-gui/preferences.cpp b/tools/lammps-gui/preferences.cpp index e4fc276912..fa584fc289 100644 --- a/tools/lammps-gui/preferences.cpp +++ b/tools/lammps-gui/preferences.cpp @@ -37,6 +37,7 @@ #include #include #include +#include #include #include @@ -70,6 +71,7 @@ Preferences::Preferences(LammpsWrapper *_lammps, QWidget *parent) : tabWidget->addTab(new GeneralTab(settings, lammps), "&General Settings"); tabWidget->addTab(new AcceleratorTab(settings, lammps), "&Accelerators"); tabWidget->addTab(new SnapshotTab(settings), "&Snapshot Image"); + tabWidget->addTab(new EditorTab(settings), "&Editor Settings"); connect(buttonBox, &QDialogButtonBox::accepted, this, &Preferences::accept); connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); @@ -80,7 +82,7 @@ Preferences::Preferences(LammpsWrapper *_lammps, QWidget *parent) : setLayout(layout); setWindowIcon(QIcon(":/lammps-icon-128x128.png")); setWindowTitle("LAMMPS-GUI - Preferences"); - resize(500, 400); + resize(600, 450); } Preferences::~Preferences() @@ -175,6 +177,20 @@ void Preferences::accept() const char *arg0 = mystrdup(QCoreApplication::arguments().at(0).toStdString()); execl(path, arg0, (char *)NULL); } + + // reformattting settings + + settings->beginGroup("reformat"); + auto spin = tabWidget->findChild("cmdval"); + if (spin) settings->setValue("command", spin->value()); + spin = tabWidget->findChild("typeval"); + if (spin) settings->setValue("type", spin->value()); + spin = tabWidget->findChild("idval"); + if (spin) settings->setValue("id", spin->value()); + spin = tabWidget->findChild("nameval"); + if (spin) settings->setValue("name", spin->value()); + settings->endGroup(); + QDialog::accept(); } @@ -483,6 +499,50 @@ SnapshotTab::SnapshotTab(QSettings *_settings, QWidget *parent) : 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: // c-basic-offset: 4 // End: diff --git a/tools/lammps-gui/preferences.h b/tools/lammps-gui/preferences.h index 919bc48798..44ff5b8951 100644 --- a/tools/lammps-gui/preferences.h +++ b/tools/lammps-gui/preferences.h @@ -83,6 +83,16 @@ private: QSettings *settings; }; +class EditorTab : public QWidget { + Q_OBJECT + +public: + explicit EditorTab(QSettings *settings, QWidget *parent = nullptr); + +private: + QSettings *settings; +}; + #endif // Local Variables: