complete implementation of variable setting dialog

This commit is contained in:
Axel Kohlmeyer
2023-08-12 10:08:02 -04:00
parent 9110c23fcb
commit 62b4318801
5 changed files with 118 additions and 21 deletions

View File

@ -15,32 +15,45 @@
#include <QDialogButtonBox>
#include <QGridLayout>
#include <QIcon>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QSizePolicy>
SetVariables::SetVariables(QList<QPair<QString, QString>> &vars, QWidget *parent) : QDialog(parent)
SetVariables::SetVariables(QList<QPair<QString, QString>> &_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<QPair<QString, QString>> &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<QLineEdit *>(row->itemAt(0)->widget());
auto *val = dynamic_cast<QLineEdit *>(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: