From 40de7d18bb5e91c049947344b8c632c40989ad10 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Mon, 5 Mar 2018 15:35:32 +0100 Subject: Added poi-config - view default configuration --- CMakeLists.txt | 2 + config/CMakeLists.txt | 15 ++++++++ config/main.cpp | 21 +++++++++++ config/settingsdialog.cpp | 37 ++++++++++++++++++ config/settingsdialog.h | 31 +++++++++++++++ lib/settings/settingsdialog.cpp | 83 ----------------------------------------- lib/settings/settingsdialog.h | 35 ----------------- lib/settings/settingsdialog.ui | 67 --------------------------------- src/configuration.h | 19 +++++++++- 9 files changed, 124 insertions(+), 186 deletions(-) create mode 100644 config/CMakeLists.txt create mode 100644 config/main.cpp create mode 100644 config/settingsdialog.cpp create mode 100644 config/settingsdialog.h delete mode 100644 lib/settings/settingsdialog.cpp delete mode 100644 lib/settings/settingsdialog.h delete mode 100644 lib/settings/settingsdialog.ui diff --git a/CMakeLists.txt b/CMakeLists.txt index 74bddd2..841c64d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,6 +47,8 @@ add_subdirectory(lib/downloads) add_subdirectory(plugins/ProfileEditor) +add_subdirectory(config) + # configure a header file to pass version information # if you don't have git, or are building this off the source tarball, define versions in version.h.in execute_process(COMMAND hg log -r '.' --template={latesttag} WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} OUTPUT_VARIABLE VcsVersion OUTPUT_STRIP_TRAILING_WHITESPACE) diff --git a/config/CMakeLists.txt b/config/CMakeLists.txt new file mode 100644 index 0000000..129b2f2 --- /dev/null +++ b/config/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 3.1.0) + +# Find includes in corresponding build directories +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +add_executable(poi-config + main.cpp + settingsdialog.cpp + settingsdialog.h + ../src/configuration.h + ../src/configuration.cpp + ) + +target_link_libraries(poi-config Qt5::Core Qt5::Widgets) +target_link_libraries(poi-config ${Boost_LIBRARIES}) diff --git a/config/main.cpp b/config/main.cpp new file mode 100644 index 0000000..25751e6 --- /dev/null +++ b/config/main.cpp @@ -0,0 +1,21 @@ +/* + * 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 + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + app.setQuitOnLastWindowClosed(true); + + SettingsDialog dlg; + dlg.show(); + + return app.exec(); +} \ No newline at end of file diff --git a/config/settingsdialog.cpp b/config/settingsdialog.cpp new file mode 100644 index 0000000..56acc01 --- /dev/null +++ b/config/settingsdialog.cpp @@ -0,0 +1,37 @@ +/* + * 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 +#include +#include +#include + +SettingsDialog::SettingsDialog(QWidget *parent) + : QMainWindow(parent) +{ + resize(800, 600); + + // main menu + settingsMenu.setTitle(tr("Settings")); + settingsMenu.addAction(tr("Quit"), qApp, &QApplication::quit, QKeySequence("Ctrl+Q")); + menuBar()->addMenu(&settingsMenu); + + setCentralWidget(&treeWidget); + treeWidget.setColumnCount(2); + + //config.read() + + for(const auto &option : config.options()) { + auto *item = new QTreeWidgetItem(&treeWidget); + item->setText(0, QString::fromStdString(option->long_name())); + item->setText(1, QString::fromStdString(config.value(option->long_name().c_str()).value_or(std::string()))); + } +} + +SettingsDialog::~SettingsDialog() = default; diff --git a/config/settingsdialog.h b/config/settingsdialog.h new file mode 100644 index 0000000..82cd1be --- /dev/null +++ b/config/settingsdialog.h @@ -0,0 +1,31 @@ +/* + * 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 + */ + +#ifndef SETTINGSDIALOG_H +#define SETTINGSDIALOG_H + +#include "../src/configuration.h" +#include +#include +#include + +class SettingsDialog : public QMainWindow +{ + Q_OBJECT + +public: + explicit SettingsDialog(QWidget *parent = nullptr); + ~SettingsDialog() override; + +private: + QMenu settingsMenu; + QTreeWidget treeWidget; + Configuration config; +}; + +#endif // SETTINGSDIALOG_H diff --git a/lib/settings/settingsdialog.cpp b/lib/settings/settingsdialog.cpp deleted file mode 100644 index bd0aa49..0000000 --- a/lib/settings/settingsdialog.cpp +++ /dev/null @@ -1,83 +0,0 @@ -/* - * 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; -} diff --git a/lib/settings/settingsdialog.h b/lib/settings/settingsdialog.h deleted file mode 100644 index 3d822d4..0000000 --- a/lib/settings/settingsdialog.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * 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 - */ - -#ifndef SETTINGSDIALOG_H -#define SETTINGSDIALOG_H - -#include -#include - -namespace Ui -{ -class SettingsDialog; -} - -class Configuration; -class SettingsDialog : public QDialog -{ - Q_OBJECT - -public: - explicit SettingsDialog(std::shared_ptr &settings, QWidget *parent = nullptr); - ~SettingsDialog(); - -private: - Ui::SettingsDialog *ui; -}; - -[[nodiscard]] QWidget *widgetForGroup(std::shared_ptr &settings, const std::string &group, QWidget *parent); - -#endif // SETTINGSDIALOG_H diff --git a/lib/settings/settingsdialog.ui b/lib/settings/settingsdialog.ui deleted file mode 100644 index 522754d..0000000 --- a/lib/settings/settingsdialog.ui +++ /dev/null @@ -1,67 +0,0 @@ - - - SettingsDialog - - - - 0 - 0 - 640 - 480 - - - - Settings - - - - - - - - - Qt::Horizontal - - - QDialogButtonBox::Cancel|QDialogButtonBox::Save - - - - - - - - - buttonBox - accepted() - SettingsDialog - accept() - - - 248 - 254 - - - 157 - 274 - - - - - buttonBox - rejected() - SettingsDialog - reject() - - - 316 - 260 - - - 286 - 274 - - - - - diff --git a/src/configuration.h b/src/configuration.h index c9223eb..5038a4d 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -37,7 +37,20 @@ public: #endif if constexpr(std::is_same_v) { - std::string r = vm[path].as(); + std::string r; + try { + r = vm[path].as(); + } catch (boost::bad_any_cast &) { + // try int + try { + r = std::to_string(vm[path].as()); + } catch (boost::bad_any_cast &) { + + // try bool, and crash if not that either + r = vm[path].as() ? "true" : "false"; + } + + } // check if it's a path if(r.front() == '~') { @@ -49,6 +62,10 @@ public: return std::optional(vm[path].as()); } + const std::vector> & options() { + return desc.options(); + } + private: boost::program_options::options_description desc; boost::program_options::variables_map vm; -- cgit v1.2.1