From 84137d9c3bf21e0a32f8a50a9bf9a93584754b75 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sat, 23 Jun 2018 10:25:28 +0200 Subject: Add Configuration::setValue and Configuration::setShortcut Change MainWindow to use setShortcut --- lib/configuration/configuration.h | 42 +++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) (limited to 'lib/configuration/configuration.h') diff --git a/lib/configuration/configuration.h b/lib/configuration/configuration.h index bb1b271..ec834ae 100644 --- a/lib/configuration/configuration.h +++ b/lib/configuration/configuration.h @@ -9,6 +9,7 @@ #ifndef SMOLBOTE_CONFIGURATION_H #define SMOLBOTE_CONFIGURATION_H +#include #include #include #include @@ -17,11 +18,13 @@ #include #include -class Configuration +class Configuration : public QObject { + Q_OBJECT + public: - explicit Configuration(); - ~Configuration(); + explicit Configuration(QObject *parent = nullptr); + ~Configuration() = default; bool parse(const std::string &path); bool parse(int argc, char **argv); @@ -31,7 +34,8 @@ public: return configuration_desc.options(); } - bool exists(const char *path) { + bool exists(const char *path) + { return vm.count(path) ? true : false; } @@ -54,11 +58,11 @@ public: } else if constexpr(std::is_same_v) { - if (vm[path].value().type() == typeid(int)) { + if(vm[path].value().type() == typeid(int)) { return std::optional(std::to_string(vm[path].as())); } - if (vm[path].value().type() == typeid(bool)) { + if(vm[path].value().type() == typeid(bool)) { return std::optional(vm[path].as() ? "true" : "false"); } @@ -75,6 +79,29 @@ public: return std::optional(vm[path].as()); } + template + void setValue(const char *path, const T &value) + { + if(vm.count(path) == 0) { + qWarning("value(%s) does not exist", path); + } + + vm.at(path).value() = value; + + emit settingChanged(path, value); + } + + void setShortcut(QAction *action, const char *name) const + { + Q_CHECK_PTR(action); + + action->setShortcut(QKeySequence::fromString(value(name).value())); + connect(this, &Configuration::settingChanged, action, [=](const std::string &path, const QString &value) { + if(path == name) + action->setShortcut(QKeySequence::fromString(value)); + }); + } + QHash section(const std::string &prefix) const; const boost::program_options::options_description commandlineOptions() const { @@ -85,6 +112,9 @@ public: return configuration_desc; } +signals: + void settingChanged(const std::string &path, const QString &value); + private: boost::program_options::options_description commandLine_desc; boost::program_options::options_description configuration_desc; -- cgit v1.2.1