Files
Pf2eDataScrapper/Pf2eDataScrapper/Forms/Controls/ProcessorCollectionControl.cs
T

174 lines
5.3 KiB
C#

using System.ComponentModel;
using Pf2eDataScrapper.App;
namespace Pf2eDataScrapper.Forms.Controls;
internal partial class ProcessorCollectionControl : UserControl
{
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public ProcessorCollection Collection
{
get => _collection;
set
{
_collection = value;
SetContent();
}
}
private ProcessorCollection _collection = null!;
public ProcessorCollectionControl()
{
InitializeComponent();
}
private Dictionary<ProcessorGroupEnum, CheckBox> GroupCheckBoxes { get; } = [];
private Dictionary<ProcessorGroupEnum, FlowLayoutPanel> GroupFlowLayoutPanels { get; } = [];
private Dictionary<string, CheckBox> ItemCheckBoxes { get; } = [];
private void SetContent()
{
RootFlowLayoutPanel.Controls.Clear();
foreach (ProcessorGroupEnum group in Enum.GetValues<ProcessorGroupEnum>())
{
GroupCheckBoxes[group] = new()
{
Text = group.GetDescription(),
Tag = group,
Dock = DockStyle.Fill,
ThreeState = true,
Checked = true,
AutoCheck = false,
CheckAlign = ContentAlignment.TopLeft,
};
GroupCheckBoxes[group].Click += GroupCheckBox_Click;
GroupFlowLayoutPanels[group] = new()
{
AutoSize = true,
Dock = DockStyle.Fill,
FlowDirection = FlowDirection.LeftToRight,
WrapContents = true,
BorderStyle = BorderStyle.FixedSingle,
Margin = new(10, 0, 10, 15),
};
RootFlowLayoutPanel.Controls.Add(GroupCheckBoxes[group]);
RootFlowLayoutPanel.Controls.Add(GroupFlowLayoutPanels[group]);
}
foreach (ProcessorCollectionItem item in Collection.AllItems)
{
ItemCheckBoxes[item.Name] = new()
{
Text = item.Description,
Tag = item,
AutoSize = true,
// Dock = DockStyle.Fill,
// MaximumSize = new Size(Width, 20),
// Width = 200,
Checked = item.Active,
CheckAlign = ContentAlignment.TopLeft,
};
ItemCheckBoxes[item.Name].CheckedChanged += ItemCheckBox_CheckedChanged;
GroupFlowLayoutPanels[item.Group].Controls.Add(ItemCheckBoxes[item.Name]);
}
UpdateContent();
}
private void UpdateContent()
{
Dictionary<ProcessorGroupEnum, CheckState> checkStates = [];
List<string> checkedItems = [];
foreach ((string key, CheckBox checkBox) in ItemCheckBoxes)
{
ProcessorCollectionItem item = Collection[key];
if (checkStates.TryGetValue(item.Group, out CheckState state))
{
/*
* | Checked | Unchecked | Indeterminate
* -------+---------------+---------------+---------------
* true | Checked | Indeterminate | Indeterminate
* false | Indeterminate | Unchecked | Indeterminate
*/
checkStates[item.Group] =
checkBox.Checked && state == CheckState.Checked ? CheckState.Checked
: !checkBox.Checked && state == CheckState.Unchecked ? CheckState.Unchecked
: CheckState.Indeterminate;
}
else
{
checkStates[item.Group] = checkBox.Checked
? CheckState.Checked
: CheckState.Unchecked;
}
if (checkBox.Checked)
{
checkedItems.Add(key);
}
}
foreach ((ProcessorGroupEnum groupEnum, CheckBox checkBox) in GroupCheckBoxes)
{
checkBox.CheckState = checkStates.GetValueOrDefault(
groupEnum,
CheckState.Indeterminate
);
}
Collection.FilterProcessors(checkedItems);
}
private void GroupCheckBox_Click(object? sender, EventArgs e)
{
if (sender is null)
{
return;
}
CheckBox groupCheckBox = (CheckBox)sender;
ProcessorGroupEnum groupEnum = GroupCheckBoxes
.First(pair => pair.Value == groupCheckBox)
.Key;
CheckState nextState = groupCheckBox.CheckState switch
{
CheckState.Unchecked => CheckState.Checked,
CheckState.Checked => CheckState.Unchecked,
CheckState.Indeterminate => CheckState.Checked,
_ => throw new ArgumentOutOfRangeException(
$"Неизвестное состояние {groupCheckBox.CheckState}"
),
};
groupCheckBox.CheckState = nextState;
foreach (
(string _, CheckBox checkBox) in ItemCheckBoxes.Where(pair =>
((ProcessorCollectionItem?)pair.Value.Tag)?.Group == groupEnum
)
)
{
checkBox.Checked = nextState == CheckState.Checked;
}
UpdateContent();
}
private void ItemCheckBox_CheckedChanged(object? sender, EventArgs e)
{
UpdateContent();
}
}