/* * This file is part of smolbote. It's copyrighted by the contributors recorded * in the version control history of the file, available from its original * location: https://neueland.iserlohn-fortress.net/smolbote.hg * * SPDX-License-Identifier: GPL-3.0 */ #include "settingsdialog.h" #include "../../src/configuration.h" #include "ui_settingsdialog.h" #include #include #include #include #include inline QHBoxLayout *createEntry(Configuration *config, const std::string &path, QWidget *widget); SettingsDialog::SettingsDialog(std::shared_ptr &settings, QWidget *parent) : QDialog(parent) , ui(new Ui::SettingsDialog) { ui->setupUi(this); for(const std::string &group : settings->childrenGroups("")) { QScrollArea *area = new QScrollArea(this); area->setWidgetResizable(true); area->setWidget(widgetForGroup(settings, group, this)); ui->tabWidget->addTab(area, QString::fromStdString(group)); } } SettingsDialog::~SettingsDialog() { delete ui; } QWidget *widgetForGroup(std::shared_ptr &settings, const std::string &group, QWidget *parent) { QWidget *widget = new QWidget(parent); QVBoxLayout *layout = new QVBoxLayout(); widget->setLayout(layout); // add entry for every key QFormLayout *form = new QFormLayout(); for(const std::string &key : settings->childrenSettings(group.c_str())) { QHBoxLayout *hBox = createEntry(settings.get(), group + '.' + key, widget); form->addRow(parent->tr(key.c_str()), hBox); } layout->addLayout(form); // TODO: iterate through groups for(const std::string &childGroup : settings->childrenGroups(group.c_str())) { QGroupBox *groupBox = new QGroupBox(QString::fromStdString(childGroup), widget); layout->addWidget(groupBox); QFormLayout *boxForm = new QFormLayout(groupBox); groupBox->setLayout(boxForm); const std::string groupPath = group + '.' + childGroup; for(const std::string &key : settings->childrenSettings(groupPath.c_str())) { QHBoxLayout *hBox = createEntry(settings.get(), groupPath + '.' + key, groupBox); boxForm->addRow(parent->tr(key.c_str()), hBox); } } return widget; } inline QHBoxLayout *createEntry(Configuration *config, const std::string &path, QWidget *widget) { QLineEdit *lineEdit = new QLineEdit(widget); lineEdit->setText(QString::fromStdString(config->value(path.c_str()).value())); QObject::connect(lineEdit, &QLineEdit::editingFinished, widget, [config, path, lineEdit]() { config->setValue(path, lineEdit->text().toStdString()); }); QHBoxLayout *hBox = new QHBoxLayout(); hBox->addWidget(lineEdit); return hBox; }