From 9481a8fa235ceb2fe77f7ecef6d7eef7ddd20818 Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Sat, 22 May 2021 02:22:12 -0400 Subject: [PATCH] enhancements to Settings to pave the way to deprecating CustomOptions in UniversalEditor --- MBS.Framework/CustomSettingsProvider.cs | 62 ++++++++++++++++++++++++ MBS.Framework/MBS.Framework.csproj | 2 + MBS.Framework/Setting.cs | 7 ++- MBS.Framework/Settings/BooleanSetting.cs | 2 +- MBS.Framework/Settings/ChoiceSetting.cs | 17 +++++-- MBS.Framework/Settings/FileSetting.cs | 16 +++++- MBS.Framework/Settings/GroupSetting.cs | 33 +++++++++++++ MBS.Framework/Settings/TextSetting.cs | 5 +- MBS.Framework/Settings/VersionSetting.cs | 33 +++++++++++++ MBS.Framework/SettingsGroup.cs | 16 ++++++ MBS.Framework/SettingsProfile.cs | 9 ++++ MBS.Framework/SettingsProvider.cs | 35 +++++++++++++ 12 files changed, 230 insertions(+), 7 deletions(-) create mode 100644 MBS.Framework/CustomSettingsProvider.cs create mode 100644 MBS.Framework/Settings/VersionSetting.cs diff --git a/MBS.Framework/CustomSettingsProvider.cs b/MBS.Framework/CustomSettingsProvider.cs new file mode 100644 index 0000000..6332b51 --- /dev/null +++ b/MBS.Framework/CustomSettingsProvider.cs @@ -0,0 +1,62 @@ +// +// CustomSettingsProvider.cs +// +// Author: +// Michael Becker +// +// Copyright (c) 2020 Mike Becker's Software +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +using System; +using MBS.Framework; + +namespace MBS.Framework +{ + public class CustomSettingsProvider : SettingsProvider + { + public CustomSettingsProvider() + { + } + public CustomSettingsProvider(SettingsGroup[] groups) + { + for (int i = 0; i < groups.Length; i++) + { + SettingsGroups.Add(groups[i]); + } + } + + public event EventHandler SettingsLoaded; + protected virtual void OnSettingsLoaded(EventArgs e) + { + SettingsLoaded?.Invoke(this, e); + } + protected override void LoadSettingsInternal() + { + base.LoadSettingsInternal(); + OnSettingsLoaded(EventArgs.Empty); + } + + public event EventHandler SettingsSaved; + protected virtual void OnSettingsSaved(EventArgs e) + { + SettingsSaved?.Invoke(this, e); + } + + protected override void SaveSettingsInternal() + { + base.SaveSettingsInternal(); + OnSettingsSaved(EventArgs.Empty); + } + } +} diff --git a/MBS.Framework/MBS.Framework.csproj b/MBS.Framework/MBS.Framework.csproj index 06e9726..5f5d503 100644 --- a/MBS.Framework/MBS.Framework.csproj +++ b/MBS.Framework/MBS.Framework.csproj @@ -112,6 +112,8 @@ + + diff --git a/MBS.Framework/Setting.cs b/MBS.Framework/Setting.cs index a0dcf93..bfa3759 100644 --- a/MBS.Framework/Setting.cs +++ b/MBS.Framework/Setting.cs @@ -25,12 +25,14 @@ namespace MBS.Framework { public abstract class Setting { - public Setting(string name, string title, object defaultValue = null) + public Setting(string name, string title, object defaultValue = null, bool enabled = true, bool visible = true) { Name = name; Title = title; DefaultValue = defaultValue; mvarValue = defaultValue; + Enabled = enabled; + Visible = visible; } public Guid ID { get; set; } = Guid.Empty; @@ -41,6 +43,9 @@ namespace MBS.Framework public string Title { get; set; } = String.Empty; public string Description { get; set; } = String.Empty; + public bool Enabled { get; set; } = true; + public bool Visible { get; set; } = true; + public class SettingCollection : System.Collections.ObjectModel.Collection { diff --git a/MBS.Framework/Settings/BooleanSetting.cs b/MBS.Framework/Settings/BooleanSetting.cs index de6f557..a96897f 100644 --- a/MBS.Framework/Settings/BooleanSetting.cs +++ b/MBS.Framework/Settings/BooleanSetting.cs @@ -23,7 +23,7 @@ namespace MBS.Framework.Settings { public class BooleanSetting : Setting { - public BooleanSetting(string name, string title, bool defaultValue = false) : base(name, title, defaultValue) + public BooleanSetting(string name, string title, bool defaultValue = false, bool enabled = true, bool visible = true) : base(name, title, defaultValue, enabled, visible) { } diff --git a/MBS.Framework/Settings/ChoiceSetting.cs b/MBS.Framework/Settings/ChoiceSetting.cs index b2e8df4..0adbfd3 100644 --- a/MBS.Framework/Settings/ChoiceSetting.cs +++ b/MBS.Framework/Settings/ChoiceSetting.cs @@ -23,7 +23,7 @@ namespace MBS.Framework.Settings { public class ChoiceSetting : Setting { - public ChoiceSetting(string name, string title, ChoiceSettingValue defaultValue = null, ChoiceSettingValue[] values = null) : base(name, title, null) + public ChoiceSetting(string name, string title, object defaultValue = null, ChoiceSettingValue[] values = null, bool multiple = false) : base(name, title, null) { if (values == null) { @@ -31,9 +31,9 @@ namespace MBS.Framework.Settings } if (defaultValue != null) { - if (defaultValue.Value != null) + if (defaultValue != null) { - base.DefaultValue = defaultValue.Value.ToString(); + base.DefaultValue = defaultValue; } else { @@ -45,6 +45,7 @@ namespace MBS.Framework.Settings { ValidValues.Add(value); } + MultipleSelect = multiple; } public class ChoiceSettingValue @@ -58,6 +59,15 @@ namespace MBS.Framework.Settings public string Title { get; set; } = String.Empty; public object Value { get; set; } = null; + public ChoiceSettingValue(object value) + { + if (value != null) + { + Name = value.ToString(); + Title = value.ToString(); + } + Value = value; + } public ChoiceSettingValue(string name, string title, object value) { Name = name; @@ -70,5 +80,6 @@ namespace MBS.Framework.Settings public ChoiceSettingValue SelectedValue { get; set; } = null; public bool RequireSelectionFromList { get; set; } = true; + public bool MultipleSelect { get; set; } = false; } } diff --git a/MBS.Framework/Settings/FileSetting.cs b/MBS.Framework/Settings/FileSetting.cs index 0db00f7..0b70671 100644 --- a/MBS.Framework/Settings/FileSetting.cs +++ b/MBS.Framework/Settings/FileSetting.cs @@ -32,10 +32,24 @@ namespace MBS.Framework.Settings { public bool RequireExistingFile { get; set; } = true; public FileSettingMode Mode { get; set; } = FileSettingMode.Open; + /// + /// A semicolon-separated list of glob-style (*.xxx) filters used in the file selection dialog for this . + /// + /// The file name filter. + public string FileNameFilter { get; set; } = null; - public FileSetting(string name, string title, string defaultValue = "", bool requireExistingFile = true) : base(name, title, defaultValue) + /// + /// Initializes a new instance of the class. + /// + /// Name. + /// Title. + /// Default value. + /// If set to true require existing file. + /// A semicolon-separated list of glob-style (*.xxx) filters used in the file selection dialog for this . + public FileSetting(string name, string title, string defaultValue = "", bool requireExistingFile = true, string fileNameFilter = null) : base(name, title, defaultValue) { RequireExistingFile = requireExistingFile; + FileNameFilter = fileNameFilter; } } } diff --git a/MBS.Framework/Settings/GroupSetting.cs b/MBS.Framework/Settings/GroupSetting.cs index e811d21..8718887 100644 --- a/MBS.Framework/Settings/GroupSetting.cs +++ b/MBS.Framework/Settings/GroupSetting.cs @@ -25,6 +25,21 @@ namespace MBS.Framework.Settings { public Setting.SettingCollection Options { get; } = new Setting.SettingCollection(); public Setting.SettingCollection HeaderSettings { get; } = new Setting.SettingCollection(); + public int Count + { + get + { + int count = Options.Count; + for (int i = 0; i < Options.Count; i++) + { + if (Options[i] is GroupSetting) + { + count += (Options[i] as GroupSetting).Count; + } + } + return count; + } + } public GroupSetting(string name, string title, Setting[] options = null) : base(name, title) { @@ -36,5 +51,23 @@ namespace MBS.Framework.Settings } } } + + public Setting FindSetting(string name) + { + foreach (Setting s in Options) + { + if (s is GroupSetting) + { + Setting r = (s as GroupSetting).FindSetting(name); + if (r != null) return r; + } + else + { + if (s.Name == name) + return s; + } + } + return null; + } } } diff --git a/MBS.Framework/Settings/TextSetting.cs b/MBS.Framework/Settings/TextSetting.cs index f4194b4..8968b8b 100644 --- a/MBS.Framework/Settings/TextSetting.cs +++ b/MBS.Framework/Settings/TextSetting.cs @@ -23,8 +23,11 @@ namespace MBS.Framework.Settings { public class TextSetting : Setting { - public TextSetting(string name, string title, string defaultValue = "") : base(name, title, defaultValue) + public int? MaximumLength { get; set; } = null; + + public TextSetting(string name, string title, string defaultValue = "", int? maxLength = null) : base(name, title, defaultValue) { + MaximumLength = maxLength; } } } diff --git a/MBS.Framework/Settings/VersionSetting.cs b/MBS.Framework/Settings/VersionSetting.cs new file mode 100644 index 0000000..df48c56 --- /dev/null +++ b/MBS.Framework/Settings/VersionSetting.cs @@ -0,0 +1,33 @@ +// +// VersionSetting.cs +// +// Author: +// Michael Becker +// +// Copyright (c) 2021 Mike Becker's Software +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +using System; +namespace MBS.Framework.Settings +{ + public class VersionSetting : Setting + { + public int ComponentCount { get; set; } = 4; + + public VersionSetting(string name, string title, Version defaultValue = null, int componentCount = 4) : base(name, title, defaultValue) + { + ComponentCount = componentCount; + } + } +} diff --git a/MBS.Framework/SettingsGroup.cs b/MBS.Framework/SettingsGroup.cs index b07ffd3..a0b0d04 100644 --- a/MBS.Framework/SettingsGroup.cs +++ b/MBS.Framework/SettingsGroup.cs @@ -20,6 +20,7 @@ // along with this program. If not, see . using System; using System.Collections.Generic; +using MBS.Framework.Settings; namespace MBS.Framework { @@ -145,6 +146,21 @@ namespace MBS.Framework } public Setting.SettingCollection Settings { get; } = new Setting.SettingCollection(); public int Priority { get; set; } = -1; + public int Count + { + get + { + int count = Settings.Count; + for (int i = 0; i < Settings.Count; i++) + { + if (Settings[i] is GroupSetting) + { + count += (Settings[i] as GroupSetting).Count; + } + } + return count; + } + } public override string ToString () { diff --git a/MBS.Framework/SettingsProfile.cs b/MBS.Framework/SettingsProfile.cs index 4f18592..4a3bc70 100644 --- a/MBS.Framework/SettingsProfile.cs +++ b/MBS.Framework/SettingsProfile.cs @@ -34,5 +34,14 @@ namespace MBS.Framework public Guid ID { get; set; } = Guid.Empty; public string Title { get; set; } = null; + + public SettingsProfile() + { + } + public SettingsProfile(Guid id, string title) + { + ID = id; + Title = title; + } } } diff --git a/MBS.Framework/SettingsProvider.cs b/MBS.Framework/SettingsProvider.cs index 543dd16..05a388e 100644 --- a/MBS.Framework/SettingsProvider.cs +++ b/MBS.Framework/SettingsProvider.cs @@ -19,6 +19,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . using System; +using MBS.Framework.Settings; namespace MBS.Framework { @@ -53,6 +54,27 @@ namespace MBS.Framework public SettingsGroup.SettingsGroupCollection SettingsGroups { get; } = new SettingsGroup.SettingsGroupCollection(); + public Setting FindSetting(string name) + { + foreach (SettingsGroup sg in SettingsGroups) + { + foreach (Setting s in sg.Settings) + { + if (s is GroupSetting) + { + Setting r = (s as GroupSetting).FindSetting(name); + if (r != null) return r; + } + else + { + if (s.Name == name) + return s; + } + } + } + return null; + } + protected virtual void InitializeInternal() { } @@ -76,5 +98,18 @@ namespace MBS.Framework { SaveSettingsInternal (); } + + public int Count + { + get + { + int count = 0; + for (int i = 0; i < SettingsGroups.Count; i++) + { + count += SettingsGroups[i].Count; + } + return count; + } + } } }