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 #include "settings_group.hpp" 0008 0009 #include <set> 0010 0011 #include "app/settings/widget_builder.hpp" 0012 0013 app::settings::SettingsGroup::SettingsGroup(QString slug, utils::TranslatedString label, const QString& icon, SettingList settings) 0014 : slug_(std::move(slug)), 0015 label_(std::move(label)), 0016 icon_(std::move(icon)), 0017 settings_(std::move(settings)) 0018 { 0019 } 0020 0021 app::settings::SettingsGroup::SettingsGroup(SettingList settings) 0022 : settings_(std::move(settings)) 0023 { 0024 } 0025 0026 QString app::settings::SettingsGroup::slug() const 0027 { 0028 return slug_; 0029 } 0030 0031 QString app::settings::SettingsGroup::label() const 0032 { 0033 return label_; 0034 } 0035 0036 QIcon app::settings::SettingsGroup::icon() const 0037 { 0038 return QIcon::fromTheme(icon_); 0039 } 0040 0041 void app::settings::SettingsGroup::load(QSettings& settings) 0042 { 0043 auto avail_keys = settings.childKeys(); 0044 std::set<QString> unprocessed_keys(avail_keys.begin(), avail_keys.end()); 0045 0046 for ( const Setting& setting : settings_ ) 0047 { 0048 unprocessed_keys.erase(setting.slug); 0049 values_[setting.slug] = settings.value(setting.slug, setting.default_value); 0050 if ( setting.side_effects ) 0051 setting.side_effects(values_[setting.slug]); 0052 } 0053 0054 for ( const QString& key : unprocessed_keys ) 0055 values_[key] = settings.value(key); 0056 } 0057 0058 void app::settings::SettingsGroup::save(QSettings& settings) 0059 { 0060 for ( const Setting& setting : settings_ ) 0061 settings.setValue(setting.slug, setting.get_variant(values_)); 0062 } 0063 0064 QWidget* app::settings::SettingsGroup::make_widget(QWidget* parent) 0065 { 0066 return new SettingsGroupWidget(this, parent); 0067 } 0068 0069 bool app::settings::SettingsGroup::has_visible_settings() const 0070 { 0071 for ( const auto& set : settings_ ) 0072 if ( set.type != Setting::Internal ) 0073 return true; 0074 return false; 0075 } 0076 0077 QVariant app::settings::SettingsGroup::get_variant(const QString& setting_slug) const 0078 { 0079 for ( const Setting& setting : settings_ ) 0080 if ( setting.slug == setting_slug ) 0081 return setting.get_variant(values_); 0082 return {}; 0083 } 0084 0085 bool app::settings::SettingsGroup::set_variant(const QString& setting_slug, const QVariant& value) 0086 { 0087 for ( const Setting& setting : settings_ ) 0088 { 0089 if ( setting.slug == setting_slug ) 0090 { 0091 if ( !setting.valid_variant(value) ) 0092 return false; 0093 values_[setting.slug] = value; 0094 if ( setting.side_effects ) 0095 setting.side_effects(value); 0096 return true; 0097 } 0098 } 0099 return false; 0100 } 0101 0102 QVariant app::settings::SettingsGroup::get_default(const QString& setting_slug) const 0103 { 0104 for ( const Setting& setting : settings_ ) 0105 if ( setting.slug == setting_slug ) 0106 return setting.default_value; 0107 return {}; 0108 } 0109 0110 QVariant app::settings::SettingsGroup::define(const QString& setting_slug, const QVariant& default_value) 0111 { 0112 for ( const Setting& setting : settings_ ) 0113 if ( setting.slug == setting_slug ) 0114 return setting.get_variant(values_); 0115 0116 settings_.push_back(Setting{setting_slug, {}, {}, Setting::Internal, default_value}); 0117 0118 auto it = values_.find(setting_slug); 0119 if ( it != values_.end() ) 0120 return *it; 0121 0122 return default_value; 0123 }