From 62b431880183ff85e251c6e8ce1c82f94e1a802c Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Sat, 12 Aug 2023 10:08:02 -0400 Subject: [PATCH] complete implementation of variable setting dialog --- tools/lammps-gui/lammpsgui.cpp | 27 +++++++-- tools/lammps-gui/lammpsgui.qrc | 3 + tools/lammps-gui/lammpsgui.ui | 6 +- tools/lammps-gui/setvariables.cpp | 97 ++++++++++++++++++++++++++----- tools/lammps-gui/setvariables.h | 6 ++ 5 files changed, 118 insertions(+), 21 deletions(-) diff --git a/tools/lammps-gui/lammpsgui.cpp b/tools/lammps-gui/lammpsgui.cpp index 65a73bde33..ebcbfe9d14 100644 --- a/tools/lammps-gui/lammpsgui.cpp +++ b/tools/lammps-gui/lammpsgui.cpp @@ -271,35 +271,35 @@ void LammpsGui::update_recents(const QString &filename) ui->action_1->setVisible(false); if ((recent.size() > 0) && !recent[0].isEmpty()) { QFileInfo fi(recent[0]); - ui->action_1->setText(QString("1. ") + fi.fileName()); + ui->action_1->setText(QString("&1. ") + fi.fileName()); ui->action_1->setData(recent[0]); ui->action_1->setVisible(true); } ui->action_2->setVisible(false); if ((recent.size() > 1) && !recent[1].isEmpty()) { QFileInfo fi(recent[1]); - ui->action_2->setText(QString("2. ") + fi.fileName()); + ui->action_2->setText(QString("&2. ") + fi.fileName()); ui->action_2->setData(recent[1]); ui->action_2->setVisible(true); } ui->action_3->setVisible(false); if ((recent.size() > 2) && !recent[2].isEmpty()) { QFileInfo fi(recent[2]); - ui->action_3->setText(QString("3. ") + fi.fileName()); + ui->action_3->setText(QString("&3. ") + fi.fileName()); ui->action_3->setData(recent[2]); ui->action_3->setVisible(true); } ui->action_4->setVisible(false); if ((recent.size() > 3) && !recent[3].isEmpty()) { QFileInfo fi(recent[3]); - ui->action_4->setText(QString("4. ") + fi.fileName()); + ui->action_4->setText(QString("&4. ") + fi.fileName()); ui->action_4->setData(recent[3]); ui->action_4->setVisible(true); } ui->action_5->setVisible(false); if ((recent.size() > 4) && !recent[4].isEmpty()) { QFileInfo fi(recent[4]); - ui->action_5->setText(QString("5. ") + fi.fileName()); + ui->action_5->setText(QString("&5. ") + fi.fileName()); ui->action_5->setData(recent[4]); ui->action_5->setVisible(true); } @@ -898,7 +898,10 @@ void LammpsGui::edit_variables() { QList> newvars = variables; SetVariables vars(newvars); - if (vars.exec() == QDialog::Accepted) variables = newvars; + if (vars.exec() == QDialog::Accepted) { + variables = newvars; + lammps.close(); + } } void LammpsGui::preferences() @@ -968,6 +971,18 @@ void LammpsGui::start_lammps() lammps_args.push_back(mystrdup("screen")); } + // add variables, if defined + for (auto &var : variables) { + QString name = var.first; + QString value = var.second; + if (!name.isEmpty() && !value.isEmpty()) { + lammps_args.push_back(mystrdup("-var")); + lammps_args.push_back(mystrdup(name.toStdString())); + for (const auto &v : value.split(' ')) + lammps_args.push_back(mystrdup(v.toStdString())); + } + } + char **args = lammps_args.data(); int narg = lammps_args.size(); lammps.open(narg, args); diff --git a/tools/lammps-gui/lammpsgui.qrc b/tools/lammps-gui/lammpsgui.qrc index 6d5abb1d3f..40b8bd9978 100644 --- a/tools/lammps-gui/lammpsgui.qrc +++ b/tools/lammps-gui/lammpsgui.qrc @@ -11,6 +11,9 @@ gtk-zoom-fit.png + + edit-delete.png + object-rotate-right.png diff --git a/tools/lammps-gui/lammpsgui.ui b/tools/lammps-gui/lammpsgui.ui index 3617017804..4032a44c31 100644 --- a/tools/lammps-gui/lammpsgui.ui +++ b/tools/lammps-gui/lammpsgui.ui @@ -418,11 +418,15 @@ - + + .. Set &Variables + + Ctrl+Shift+V + diff --git a/tools/lammps-gui/setvariables.cpp b/tools/lammps-gui/setvariables.cpp index 8393dff60e..4729ca077b 100644 --- a/tools/lammps-gui/setvariables.cpp +++ b/tools/lammps-gui/setvariables.cpp @@ -15,32 +15,45 @@ #include #include +#include #include #include +#include #include -SetVariables::SetVariables(QList> &vars, QWidget *parent) : QDialog(parent) +SetVariables::SetVariables(QList> &_vars, QWidget *parent) : + QDialog(parent), vars(_vars), layout(new QVBoxLayout) { - auto *layout = new QGridLayout; - auto *top = new QLabel("Set Variables:"); - layout->addWidget(top, 0, 0, 1, 2, Qt::AlignHCenter); - - auto *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); - connect(buttonBox, &QDialogButtonBox::accepted, this, &SetVariables::accept); - connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); + auto *top = new QLabel("Set Variables:"); + layout->addWidget(top, 0, Qt::AlignHCenter); int i = 1; for (const auto &v : vars) { + auto *row = new QHBoxLayout; auto *name = new QLineEdit(v.first); auto *val = new QLineEdit(v.second); + auto *del = new QPushButton(QIcon(":/edit-delete.png"), ""); name->setObjectName("varname"); val->setObjectName("varval"); - layout->addWidget(name, i, 0); - layout->addWidget(val, i, 1); + del->setObjectName(QString::number(i)); + connect(del, &QPushButton::released, this, &SetVariables::del_row); + row->addWidget(name); + row->addWidget(val); + row->addWidget(del); + layout->addLayout(row); ++i; } - layout->addItem(new QSpacerItem(10, 10, QSizePolicy::Expanding, QSizePolicy::Expanding), i, 0); - layout->addWidget(buttonBox, i + 1, 0, 1, 2); + layout->addSpacerItem(new QSpacerItem(10, 10, QSizePolicy::Minimum, QSizePolicy::Expanding)); + + auto *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); + auto *add = new QPushButton("&Add Row"); + add->setObjectName("add_row"); + buttonBox->addButton(add, QDialogButtonBox::ActionRole); + connect(add, &QPushButton::released, this, &SetVariables::add_row); + connect(buttonBox, &QDialogButtonBox::accepted, this, &SetVariables::accept); + connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); + + layout->addWidget(buttonBox); setLayout(layout); setWindowTitle("LAMMPS-GUI - Set Variables"); resize(500, 400); @@ -48,8 +61,64 @@ SetVariables::SetVariables(QList> &vars, QWidget *parent void SetVariables::accept() { - // store all data in settings class - // and then confirm accepting + // store all data in variables class and then confirm accepting + vars.clear(); + int nrows = layout->count() - 2; + for (int i = 1; i < nrows; ++i) { + auto *row = layout->itemAt(i)->layout(); + auto *var = dynamic_cast(row->itemAt(0)->widget()); + auto *val = dynamic_cast(row->itemAt(1)->widget()); + if (var && val) vars.append(qMakePair(var->text(), val->text())); + } QDialog::accept(); } + +void SetVariables::add_row() +{ + int nrows = layout->count(); + auto *row = new QHBoxLayout; + auto *name = new QLineEdit(QString()); + auto *val = new QLineEdit(QString()); + auto *del = new QPushButton(QIcon(":/edit-delete.png"), ""); + name->setObjectName("varname"); + val->setObjectName("varval"); + del->setObjectName(QString::number(nrows - 2)); + connect(del, &QPushButton::released, this, &SetVariables::del_row); + row->addWidget(name); + row->addWidget(val); + row->addWidget(del); + layout->insertLayout(nrows - 2, row); +} + +void SetVariables::del_row() +{ + int nrows = layout->count(); + auto *who = sender(); + if (who) { + // figure out which row was deleted and delete its layout and widgets + int delrow = who->objectName().toInt(); + auto *row = layout->takeAt(delrow); + while (row->layout()->count() > 0) { + auto *item = row->layout()->takeAt(0); + row->layout()->removeItem(item); + if (item) { + delete item->widget(); + delete item; + } + } + layout->removeItem(row); + delete row->layout(); + + // renumber the delete pushbutton names + for (int i = delrow; i < nrows - 3; ++i) { + auto *row = layout->itemAt(i)->layout(); + auto *widget = row->itemAt(2)->widget(); + widget->setObjectName(QString::number(i)); + } + } +} + +// Local Variables: +// c-basic-offset: 4 +// End: diff --git a/tools/lammps-gui/setvariables.h b/tools/lammps-gui/setvariables.h index 810111f844..1d1ca30bf2 100644 --- a/tools/lammps-gui/setvariables.h +++ b/tools/lammps-gui/setvariables.h @@ -28,6 +28,12 @@ public: private slots: void accept() override; + void add_row(); + void del_row(); + +private: + QList> &vars; + class QVBoxLayout *layout; }; #endif