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 <vector>
0010 #include <functional>
0011 
0012 #include <QVariant>
0013 #include <QString>
0014 
0015 #include "app/utils/translated_string.hpp"
0016 
0017 namespace app::settings {
0018 
0019 struct Setting
0020 {
0021     enum Type
0022     {
0023         Internal,
0024         Info,
0025         Bool,
0026         Int,
0027         Float,
0028         String,
0029         Color,
0030     };
0031 
0032     Setting(QString slug, utils::TranslatedString label, utils::TranslatedString description)
0033         : type(Info),
0034         slug(std::move(slug)),
0035         label(std::move(label)),
0036         description(std::move(description))
0037     {}
0038 
0039     Setting(QString slug, utils::TranslatedString label, utils::TranslatedString description, bool default_value)
0040         : type(Bool),
0041         slug(std::move(slug)),
0042         label(std::move(label)),
0043         description(std::move(description)),
0044         default_value(default_value)
0045     {}
0046 
0047     Setting(QString slug, utils::TranslatedString label, utils::TranslatedString description, int default_value, int min, int max)
0048         : type(Int),
0049         slug(std::move(slug)),
0050         label(std::move(label)),
0051         description(std::move(description)),
0052         default_value(default_value),
0053         min(min),
0054         max(max)
0055     {}
0056 
0057     Setting(QString slug, utils::TranslatedString label, utils::TranslatedString description, float default_value, float min, float max)
0058         : type(Float),
0059         slug(std::move(slug)),
0060         label(std::move(label)),
0061         description(std::move(description)),
0062         default_value(default_value),
0063         min(min),
0064         max(max)
0065     {}
0066 
0067     Setting(QString slug, utils::TranslatedString label, utils::TranslatedString description, const QString& default_value)
0068         : type(String), slug(std::move(slug)), label(std::move(label)),
0069         description(std::move(description)), default_value(default_value)
0070     {}
0071 
0072     Setting(QString slug, utils::TranslatedString label, utils::TranslatedString description, Type type,
0073             QVariant default_value, QVariantMap choices = {},
0074             std::function<void(const QVariant&)> side_effects = {}
0075            )
0076         : type(type),
0077         slug(std::move(slug)),
0078         label(std::move(label)),
0079         description(std::move(description)),
0080         default_value(std::move(default_value)),
0081         choices(std::move(choices)),
0082         side_effects(std::move(side_effects))
0083     {}
0084 
0085     Setting(QString slug, utils::TranslatedString label, utils::TranslatedString description, const QColor& default_value)
0086         : type(Color),
0087         slug(std::move(slug)),
0088         label(std::move(label)),
0089         description(std::move(description)),
0090         default_value(QVariant::fromValue(default_value))
0091     {}
0092 
0093     QVariant get_variant(const QVariantMap& map) const
0094     {
0095         auto it = map.find(slug);
0096         if ( it != map.end() && valid_variant(*it) )
0097             return *it;
0098         return default_value;
0099     }
0100 
0101     template<class CastType>
0102     CastType get(const QVariantMap& map) const
0103     {
0104         return get_variant(map).value<CastType>();
0105     }
0106 
0107     bool valid_variant(const QVariant& v) const
0108     {
0109         switch ( type )
0110         {
0111             case Info:
0112             case Internal:
0113                 return true;
0114             case Bool:
0115                 return v.canConvert<bool>();
0116             case Int:
0117                 return v.canConvert<int>();
0118             case Float:
0119                 return v.canConvert<float>();
0120             case String:
0121                 return v.canConvert<QString>();
0122             case Color:
0123                 return v.canConvert<QColor>();
0124             default:
0125                 return false;
0126         }
0127     }
0128 
0129     Type type;
0130     QString slug;
0131     utils::TranslatedString label;
0132     utils::TranslatedString description;
0133     QVariant default_value;
0134     float min = -1;
0135     float max = -1;
0136     QVariantMap choices;
0137     std::function<void(const QVariant&)> side_effects;
0138 
0139 };
0140 
0141 using SettingList = std::vector<Setting>;
0142 
0143 } // namespace app::settings