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/CMakeLists.txt | 10 ++++++++- lib/configuration/configuration.cpp | 9 ++++---- lib/configuration/configuration.h | 42 +++++++++++++++++++++++++++++++------ 3 files changed, 49 insertions(+), 12 deletions(-) (limited to 'lib') 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>(), "arguments") ; + arguments_desc.add("args", -1); + configuration_desc.add_options() ("browser.stylesheet", po::value()) @@ -119,12 +122,8 @@ Configuration::Configuration() ("downloads.path", po::value()->default_value("~/Downloads")) ("downloads.shortcut", po::value()->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 #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