aboutsummaryrefslogtreecommitdiff
path: root/lib/configuration
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2018-06-23 10:25:28 +0200
committerAqua-sama <aqua@iserlohn-fortress.net>2018-06-23 10:25:28 +0200
commit84137d9c3bf21e0a32f8a50a9bf9a93584754b75 (patch)
treecb539940bc33375dd477db545bb1fac9ab17ade6 /lib/configuration
parentclang-format pass (diff)
downloadsmolbote-84137d9c3bf21e0a32f8a50a9bf9a93584754b75.tar.xz
Add Configuration::setValue and Configuration::setShortcut
Change MainWindow to use setShortcut
Diffstat (limited to 'lib/configuration')
-rw-r--r--lib/configuration/CMakeLists.txt10
-rw-r--r--lib/configuration/configuration.cpp9
-rw-r--r--lib/configuration/configuration.h42
3 files changed, 49 insertions, 12 deletions
diff --git a/lib/configuration/CMakeLists.txt b/lib/configuration/CMakeLists.txt
index 0db2b67..ed0235d 100644
--- a/lib/configuration/CMakeLists.txt
+++ b/lib/configuration/CMakeLists.txt
@@ -1,3 +1,11 @@
+# Find includes in corresponding build directories
+set(CMAKE_INCLUDE_CURRENT_DIR ON)
+
+# Instruct CMake to run moc automatically when needed.
+set(CMAKE_AUTOMOC ON)
+#set(CMAKE_AUTOUIC ON)
+#set(CMAKE_AUTORCC ON)
+
add_library(configuration
configuration.cpp
configuration.h)
@@ -6,6 +14,6 @@ target_include_directories(configuration
PRIVATE ${Boost_INCLUDE_DIRS})
target_link_libraries(configuration
- Qt5::Core
+ Qt5::Core Qt5::Widgets
${Boost_LIBRARIES}
)
diff --git a/lib/configuration/configuration.cpp b/lib/configuration/configuration.cpp
index e767c65..0c49a83 100644
--- a/lib/configuration/configuration.cpp
+++ b/lib/configuration/configuration.cpp
@@ -38,7 +38,8 @@ constexpr const char *defaultSocketPath()
#endif
}
-Configuration::Configuration()
+Configuration::Configuration(QObject *parent)
+ : QObject(parent)
{
m_homePath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation).toStdString();
@@ -53,6 +54,8 @@ Configuration::Configuration()
("args", po::value<std::vector<std::string>>(), "arguments")
;
+ arguments_desc.add("args", -1);
+
configuration_desc.add_options()
("browser.stylesheet", po::value<std::string>())
@@ -119,12 +122,8 @@ Configuration::Configuration()
("downloads.path", po::value<std::string>()->default_value("~/Downloads"))
("downloads.shortcut", po::value<std::string>()->default_value("Ctrl+D"))
;
-
- arguments_desc.add("args", -1);
}
-Configuration::~Configuration() = default;
-
bool Configuration::parse(const std::string &path)
{
std::ifstream f(path, std::ifstream::in);
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;