aboutsummaryrefslogtreecommitdiff
path: root/lib/configuration/configuration.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/configuration/configuration.h')
-rw-r--r--lib/configuration/configuration.h42
1 files changed, 36 insertions, 6 deletions
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 <QAction>
#include <QString>
#include <QStringList>
#include <QVariant>
@@ -17,11 +18,13 @@
#include <string>
#include <vector>
-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<T, std::string>) {
- if (vm[path].value().type() == typeid(int)) {
+ if(vm[path].value().type() == typeid(int)) {
return std::optional<std::string>(std::to_string(vm[path].as<int>()));
}
- if (vm[path].value().type() == typeid(bool)) {
+ if(vm[path].value().type() == typeid(bool)) {
return std::optional<std::string>(vm[path].as<bool>() ? "true" : "false");
}
@@ -75,6 +79,29 @@ public:
return std::optional<T>(vm[path].as<T>());
}
+ template <typename T>
+ 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<QString>(name).value()));
+ connect(this, &Configuration::settingChanged, action, [=](const std::string &path, const QString &value) {
+ if(path == name)
+ action->setShortcut(QKeySequence::fromString(value));
+ });
+ }
+
QHash<QString, QString> 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;