/* * 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/gitea/aqua/smolbote * * SPDX-License-Identifier: GPL-3.0 */ #ifndef SMOLBOTE_CONFIGURATION_H #define SMOLBOTE_CONFIGURATION_H #include #include #include #include #include #include #include #include class Configuration : public QObject { Q_OBJECT public: explicit Configuration(int argc, char** argv, const std::string &path, QObject *parent = nullptr); ~Configuration() = default; bool exists(const char *path) { return vm.count(path) ? true : false; } template std::optional value(const char *path) const { // if setting doesn't exist, we crash // in debug builds, check if setting exists if(vm.count(path) == 0) { #ifdef QT_DEBUG qWarning("value(%s) does not exist", path); #endif return std::nullopt; } // path is guaranteed to exist, so using vm[path] is safe if constexpr(std::is_same_v) { QStringList r; for(const std::string &item : vm[path].as>()) { r.append(QString::fromStdString(item)); } return std::optional(r); } else if constexpr(std::is_same_v || std::is_same_v) { if(vm[path].value().type() == typeid(int)) { if constexpr(std::is_same_v) return std::optional(std::to_string(vm[path].as())); else if constexpr(std::is_same_v) return std::optional(QString::number(vm[path].as())); } if(vm[path].value().type() == typeid(bool)) { return std::optional(vm[path].as() ? "true" : "false"); } std::string r = vm[path].as(); // check if it's a path if(r.front() == '~') { r.replace(0, 1, m_homePath); } if constexpr(std::is_same_v) return std::optional(r); else if constexpr(std::is_same_v) return std::optional(QString::fromStdString(r)); } else 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); const auto shortcutText = this->value(name); if(shortcutText) { const QString tooltip = action->toolTip(); action->setShortcut(QKeySequence::fromString(shortcutText.value())); action->setToolTip(QString("%1 (%2)").arg(tooltip, shortcutText.value())); connect(this, &Configuration::settingChanged, action, [=](const std::string &path, const QString &value) { if(path == name) { action->setShortcut(QKeySequence::fromString(value)); action->setToolTip(QString("%1 (%2)").arg(tooltip, value)); } }); } } QHash section(const std::string &prefix) const; const boost::program_options::options_description& description() const { return configuration_desc; } signals: void settingChanged(const std::string &path, const QString &value); private: boost::program_options::options_description configuration_desc; boost::program_options::variables_map vm; const std::string m_homePath; }; #endif // SMOLBOTE_CONFIGURATION_H