add reformat to context menu, formatting width may be changed in preferences
This commit is contained in:
@ -25,6 +25,7 @@
|
||||
#include <QMimeData>
|
||||
#include <QPainter>
|
||||
#include <QRegularExpression>
|
||||
#include <QSettings>
|
||||
#include <QTextBlock>
|
||||
#include <QUrl>
|
||||
|
||||
@ -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
|
||||
|
||||
if (i < 3) {
|
||||
// additional space for types or type ranges
|
||||
if ((words[0] == "pair_coeff") && (i < 3))
|
||||
for (int j = words[i].size(); j < 4; ++j)
|
||||
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)
|
||||
// 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;
|
||||
|
||||
@ -49,6 +49,7 @@ private slots:
|
||||
void get_help();
|
||||
void find_help(QString &page, QString &help);
|
||||
void open_help();
|
||||
void reformatCurrentLine();
|
||||
|
||||
private:
|
||||
QWidget *lineNumberArea;
|
||||
|
||||
BIN
tools/lammps-gui/format-indent-less-3.png
Normal file
BIN
tools/lammps-gui/format-indent-less-3.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.5 KiB |
@ -20,6 +20,7 @@
|
||||
<file>edit-redo.png</file>
|
||||
<file>edit-undo.png</file>
|
||||
<file>emblem-photos.png</file>
|
||||
<file>format-indent-less-3.png</file>
|
||||
<file>go-first.png</file>
|
||||
<file>go-last.png</file>
|
||||
<file>go-next-2.png</file>
|
||||
|
||||
@ -37,6 +37,7 @@
|
||||
#include <QRadioButton>
|
||||
#include <QSettings>
|
||||
#include <QSpacerItem>
|
||||
#include <QSpinBox>
|
||||
#include <QTabWidget>
|
||||
#include <QVBoxLayout>
|
||||
|
||||
@ -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<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();
|
||||
}
|
||||
|
||||
@ -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:
|
||||
|
||||
@ -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:
|
||||
|
||||
Reference in New Issue
Block a user