enhancements to Settings to pave the way to deprecating CustomOptions in UniversalEditor

This commit is contained in:
Michael Becker 2021-05-22 02:22:12 -04:00
parent 12b4f9cfe4
commit 9481a8fa23
No known key found for this signature in database
GPG Key ID: 98C333A81F18C22C
12 changed files with 230 additions and 7 deletions

View File

@ -0,0 +1,62 @@
//
// CustomSettingsProvider.cs
//
// Author:
// Michael Becker <alcexhim@gmail.com>
//
// 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 <http://www.gnu.org/licenses/>.
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);
}
}
}

View File

@ -112,6 +112,8 @@
<Compile Include="CommandStylePreset.cs" /> <Compile Include="CommandStylePreset.cs" />
<Compile Include="Settings\CustomSetting.cs" /> <Compile Include="Settings\CustomSetting.cs" />
<Compile Include="CardinalDirection.cs" /> <Compile Include="CardinalDirection.cs" />
<Compile Include="CustomSettingsProvider.cs" />
<Compile Include="Settings\VersionSetting.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Logic\" /> <Folder Include="Logic\" />

View File

@ -25,12 +25,14 @@ namespace MBS.Framework
{ {
public abstract class Setting 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; Name = name;
Title = title; Title = title;
DefaultValue = defaultValue; DefaultValue = defaultValue;
mvarValue = defaultValue; mvarValue = defaultValue;
Enabled = enabled;
Visible = visible;
} }
public Guid ID { get; set; } = Guid.Empty; public Guid ID { get; set; } = Guid.Empty;
@ -41,6 +43,9 @@ namespace MBS.Framework
public string Title { get; set; } = String.Empty; public string Title { get; set; } = String.Empty;
public string Description { 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 public class SettingCollection
: System.Collections.ObjectModel.Collection<Setting> : System.Collections.ObjectModel.Collection<Setting>
{ {

View File

@ -23,7 +23,7 @@ namespace MBS.Framework.Settings
{ {
public class BooleanSetting : Setting 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)
{ {
} }

View File

@ -23,7 +23,7 @@ namespace MBS.Framework.Settings
{ {
public class ChoiceSetting : Setting 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) if (values == null)
{ {
@ -31,9 +31,9 @@ namespace MBS.Framework.Settings
} }
if (defaultValue != null) if (defaultValue != null)
{ {
if (defaultValue.Value != null) if (defaultValue != null)
{ {
base.DefaultValue = defaultValue.Value.ToString(); base.DefaultValue = defaultValue;
} }
else else
{ {
@ -45,6 +45,7 @@ namespace MBS.Framework.Settings
{ {
ValidValues.Add(value); ValidValues.Add(value);
} }
MultipleSelect = multiple;
} }
public class ChoiceSettingValue public class ChoiceSettingValue
@ -58,6 +59,15 @@ namespace MBS.Framework.Settings
public string Title { get; set; } = String.Empty; public string Title { get; set; } = String.Empty;
public object Value { get; set; } = null; 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) public ChoiceSettingValue(string name, string title, object value)
{ {
Name = name; Name = name;
@ -70,5 +80,6 @@ namespace MBS.Framework.Settings
public ChoiceSettingValue SelectedValue { get; set; } = null; public ChoiceSettingValue SelectedValue { get; set; } = null;
public bool RequireSelectionFromList { get; set; } = true; public bool RequireSelectionFromList { get; set; } = true;
public bool MultipleSelect { get; set; } = false;
} }
} }

View File

@ -32,10 +32,24 @@ namespace MBS.Framework.Settings
{ {
public bool RequireExistingFile { get; set; } = true; public bool RequireExistingFile { get; set; } = true;
public FileSettingMode Mode { get; set; } = FileSettingMode.Open; public FileSettingMode Mode { get; set; } = FileSettingMode.Open;
/// <summary>
/// A semicolon-separated list of glob-style (*.xxx) filters used in the file selection dialog for this <see cref="FileSetting" />.
/// </summary>
/// <value>The file name filter.</value>
public string FileNameFilter { get; set; } = null;
public FileSetting(string name, string title, string defaultValue = "", bool requireExistingFile = true) : base(name, title, defaultValue) /// <summary>
/// Initializes a new instance of the <see cref="FileSetting" /> class.
/// </summary>
/// <param name="name">Name.</param>
/// <param name="title">Title.</param>
/// <param name="defaultValue">Default value.</param>
/// <param name="requireExistingFile">If set to <c>true</c> require existing file.</param>
/// <param name="fileNameFilter">A semicolon-separated list of glob-style (*.xxx) filters used in the file selection dialog for this <see cref="FileSetting" />.</param>
public FileSetting(string name, string title, string defaultValue = "", bool requireExistingFile = true, string fileNameFilter = null) : base(name, title, defaultValue)
{ {
RequireExistingFile = requireExistingFile; RequireExistingFile = requireExistingFile;
FileNameFilter = fileNameFilter;
} }
} }
} }

View File

@ -25,6 +25,21 @@ namespace MBS.Framework.Settings
{ {
public Setting.SettingCollection Options { get; } = new Setting.SettingCollection(); public Setting.SettingCollection Options { get; } = new Setting.SettingCollection();
public Setting.SettingCollection HeaderSettings { 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) 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;
}
} }
} }

View File

@ -23,8 +23,11 @@ namespace MBS.Framework.Settings
{ {
public class TextSetting : Setting 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;
} }
} }
} }

View File

@ -0,0 +1,33 @@
//
// VersionSetting.cs
//
// Author:
// Michael Becker <alcexhim@gmail.com>
//
// 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 <http://www.gnu.org/licenses/>.
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;
}
}
}

View File

@ -20,6 +20,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using MBS.Framework.Settings;
namespace MBS.Framework namespace MBS.Framework
{ {
@ -145,6 +146,21 @@ namespace MBS.Framework
} }
public Setting.SettingCollection Settings { get; } = new Setting.SettingCollection(); public Setting.SettingCollection Settings { get; } = new Setting.SettingCollection();
public int Priority { get; set; } = -1; 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 () public override string ToString ()
{ {

View File

@ -34,5 +34,14 @@ namespace MBS.Framework
public Guid ID { get; set; } = Guid.Empty; public Guid ID { get; set; } = Guid.Empty;
public string Title { get; set; } = null; public string Title { get; set; } = null;
public SettingsProfile()
{
}
public SettingsProfile(Guid id, string title)
{
ID = id;
Title = title;
}
} }
} }

View File

@ -19,6 +19,7 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
using System; using System;
using MBS.Framework.Settings;
namespace MBS.Framework namespace MBS.Framework
{ {
@ -53,6 +54,27 @@ namespace MBS.Framework
public SettingsGroup.SettingsGroupCollection SettingsGroups { get; } = new SettingsGroup.SettingsGroupCollection(); 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() protected virtual void InitializeInternal()
{ {
} }
@ -76,5 +98,18 @@ namespace MBS.Framework
{ {
SaveSettingsInternal (); SaveSettingsInternal ();
} }
public int Count
{
get
{
int count = 0;
for (int i = 0; i < SettingsGroups.Count; i++)
{
count += SettingsGroups[i].Count;
}
return count;
}
}
} }
} }