File indexing completed on 2024-11-24 03:56:28

0001 /*
0002  * SPDX-FileCopyrightText: 2019-2023 Mattia Basaglia <dev@dragon.best>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #pragma once
0008 
0009 #include <QHash>
0010 #include <QList>
0011 #include <QCoreApplication>
0012 
0013 #include "app/settings/settings_group.hpp"
0014 
0015 
0016 namespace app::settings {
0017 
0018 /**
0019  * \brief Singleton for handling settings
0020  */
0021 class Settings
0022 {
0023     Q_DECLARE_TR_FUNCTIONS(Settings)
0024 
0025 public:
0026     using iterator = std::vector<CustomSettingsGroup>::const_iterator;
0027 
0028     static Settings& instance()
0029     {
0030         static Settings singleton;
0031         return singleton;
0032     }
0033 
0034     /**
0035      * \brief Get the value of the given setting, or add a new (internal) setting if it hasn't been declared
0036      */
0037     QVariant define(const QString& group, const QString& setting, const QVariant& default_value);
0038 
0039     /**
0040      * \brief Get the value of a declared setting
0041      */
0042     QVariant get_value(const QString& group, const QString& setting) const;
0043 
0044     /**
0045      * \brief Set the value of a declared setting
0046      */
0047     bool set_value(const QString& group, const QString& setting, const QVariant& value);
0048 
0049     /**
0050      * \brief Get the default value of a declared setting
0051      */
0052     QVariant get_default(const QString& group, const QString& setting) const;
0053 
0054     /**
0055      * \brief Load all settings
0056      */
0057     void load();
0058 
0059     /**
0060      * \brief Save all settings
0061      */
0062     void save();
0063 
0064     iterator begin() const { return groups_.begin(); }
0065     iterator end() const  { return groups_.end(); }
0066 
0067     std::vector<CustomSettingsGroup>& groups() { return groups_; }
0068     void add_group(QString slug, utils::TranslatedString label, const QString& icon, SettingList settings);
0069     void add_group(CustomSettingsGroup group);
0070 
0071 private:
0072     Settings() = default;
0073     Settings(const Settings&) = delete;
0074     ~Settings() = default;
0075 
0076     QHash<QString, int> order;
0077     std::vector<CustomSettingsGroup> groups_;
0078 };
0079 
0080 
0081 
0082 /**
0083  * \brief Get the value of a declared setting
0084  */
0085 template<class T>
0086 T get(const QString& group, const QString& setting)
0087 {
0088     return Settings::instance().get_value(group, setting).value<T>();
0089 }
0090 
0091 /**
0092  * \brief Get the value of a declared setting
0093  * \returns \p default_value if the setting isn't found
0094  */
0095 template<class T>
0096 T get(const QString& group, const QString& setting, const T& defval)
0097 {
0098     auto var = Settings::instance().get_value(group, setting);
0099     if ( var.canConvert<T>() )
0100         return var.value<T>();
0101     return defval;
0102 }
0103 
0104 /**
0105  * \brief Get the default value of a declared setting
0106  */
0107 template<class T>
0108 T get_default(const QString& group, const QString& setting)
0109 {
0110     return Settings::instance().get_default(group, setting).value<T>();
0111 }
0112 
0113 /**
0114  * \brief Set the value of a declared setting
0115  */
0116 template<class T>
0117 bool set(const QString& group, const QString& setting, const T& value)
0118 {
0119     return Settings::instance().set_value(group, setting, QVariant::fromValue(value));
0120 }
0121 
0122 
0123 /**
0124  * \brief Get the value of the given setting, or add a new (internal) setting if it hasn't been declared
0125  */
0126 template<class T>
0127 T define(const QString& group, const QString& setting, const T& default_value)
0128 {
0129     QVariant var = Settings::instance().define(group, setting, QVariant::fromValue(default_value));
0130     if ( var.canConvert<T>() )
0131         return var.value<T>();
0132     return default_value;
0133 }
0134 
0135 } // namespace app::settings