Add enable/disable checkbox to group browser

This commit is contained in:
azakhi 2018-09-06 09:59:09 +03:00
parent ae38197052
commit 1f6cbeec86
7 changed files with 150 additions and 43 deletions

View File

@ -14,6 +14,7 @@ namespace Filtration.ObjectModel
public interface IItemFilterBlock : IItemFilterBlockBase public interface IItemFilterBlock : IItemFilterBlockBase
{ {
bool Enabled { get; set; } bool Enabled { get; set; }
event EventHandler EnabledStatusChanged;
string Description { get; set; } string Description { get; set; }
ItemFilterBlockGroup BlockGroup { get; set; } ItemFilterBlockGroup BlockGroup { get; set; }
BlockAction Action { get; set; } BlockAction Action { get; set; }
@ -105,8 +106,12 @@ namespace Filtration.ObjectModel
{ {
_enabled = value; _enabled = value;
IsEdited = true; IsEdited = true;
EnabledStatusChanged?.Invoke(null, null);
} }
} }
public event EventHandler EnabledStatusChanged;
public string Description public string Description
{ {
get { return _description; } get { return _description; }
@ -198,14 +203,23 @@ namespace Filtration.ObjectModel
private void OnBlockGroupStatusChanged(object sender, EventArgs e) private void OnBlockGroupStatusChanged(object sender, EventArgs e)
{ {
if (BlockGroup.IsChecked == false && Action == BlockAction.Show) if (BlockGroup.IsShowChecked == false && Action == BlockAction.Show)
{ {
Action = BlockAction.Hide; Action = BlockAction.Hide;
} }
else if (BlockGroup.IsChecked && Action == BlockAction.Hide) else if (BlockGroup.IsShowChecked && Action == BlockAction.Hide)
{ {
Action = BlockAction.Show; Action = BlockAction.Show;
} }
if (BlockGroup.IsEnableChecked == false && Enabled)
{
Enabled = false;
}
else if (BlockGroup.IsEnableChecked && !Enabled)
{
Enabled = true;
}
} }
public Color DisplayTextColor public Color DisplayTextColor

View File

@ -5,7 +5,8 @@ namespace Filtration.ObjectModel
{ {
public class ItemFilterBlockGroup public class ItemFilterBlockGroup
{ {
private bool _isChecked; private bool _isShowChecked;
private bool _isEnableChecked;
public ItemFilterBlockGroup(string groupName, ItemFilterBlockGroup parent, bool advanced = false) public ItemFilterBlockGroup(string groupName, ItemFilterBlockGroup parent, bool advanced = false)
{ {
@ -22,14 +23,14 @@ namespace Filtration.ObjectModel
public List<ItemFilterBlockGroup> ChildGroups { get; } public List<ItemFilterBlockGroup> ChildGroups { get; }
public bool Advanced { get; } public bool Advanced { get; }
public bool IsChecked public bool IsShowChecked
{ {
get { return _isChecked; } get { return _isShowChecked; }
set set
{ {
if (value != _isChecked) if (value != _isShowChecked)
{ {
_isChecked = value; _isShowChecked = value;
// Raise an event to let blocks that have this block group assigned that // Raise an event to let blocks that have this block group assigned that
// they might need to change their Action due to the block group status changing. // they might need to change their Action due to the block group status changing.
BlockGroupStatusChanged?.Invoke(null, null); BlockGroupStatusChanged?.Invoke(null, null);
@ -37,6 +38,21 @@ namespace Filtration.ObjectModel
} }
} }
public bool IsEnableChecked
{
get { return _isEnableChecked; }
set
{
if (value != _isEnableChecked)
{
_isEnableChecked = value;
// Raise an event to let blocks that have this block group assigned that
// they might need to change their Enabled due to the block group status changing.
BlockGroupStatusChanged?.Invoke(null, null);
}
}
}
public override string ToString() public override string ToString()
{ {
var currentBlockGroup = this; var currentBlockGroup = this;

View File

@ -133,7 +133,7 @@ namespace Filtration.Parser.Tests.Services
_testUtility.Translator.TranslateStringToItemFilterBlock(inputString, Mock.Of<IItemFilterScript>(i => i.ItemFilterScriptSettings.BlockGroupsEnabled)); _testUtility.Translator.TranslateStringToItemFilterBlock(inputString, Mock.Of<IItemFilterScript>(i => i.ItemFilterScriptSettings.BlockGroupsEnabled));
// Assert // Assert
Assert.AreEqual(true, inputBlockGroup.IsChecked); Assert.AreEqual(true, inputBlockGroup.IsShowChecked);
} }
[Test] [Test]
@ -148,7 +148,7 @@ namespace Filtration.Parser.Tests.Services
_testUtility.Translator.TranslateStringToItemFilterBlock(inputString, Mock.Of<IItemFilterScript>(i => i.ItemFilterScriptSettings.BlockGroupsEnabled)); _testUtility.Translator.TranslateStringToItemFilterBlock(inputString, Mock.Of<IItemFilterScript>(i => i.ItemFilterScriptSettings.BlockGroupsEnabled));
// Assert // Assert
Assert.AreEqual(false, inputBlockGroup.IsChecked); Assert.AreEqual(false, inputBlockGroup.IsShowChecked);
} }
[Test] [Test]

View File

@ -667,7 +667,8 @@ namespace Filtration.Parser.Services
if (blockGroups.Count(b => !string.IsNullOrEmpty(b.Trim())) > 0) if (blockGroups.Count(b => !string.IsNullOrEmpty(b.Trim())) > 0)
{ {
block.BlockGroup = _blockGroupHierarchyBuilder.IntegrateStringListIntoBlockGroupHierarchy(blockGroups); block.BlockGroup = _blockGroupHierarchyBuilder.IntegrateStringListIntoBlockGroupHierarchy(blockGroups);
block.BlockGroup.IsChecked = block.Action == BlockAction.Show; block.BlockGroup.IsShowChecked = block.Action == BlockAction.Show;
block.BlockGroup.IsEnableChecked = block.Enabled;
} }
} }

View File

@ -8,9 +8,12 @@ namespace Filtration.ViewModels
{ {
internal class ItemFilterBlockGroupViewModel : ViewModelBase internal class ItemFilterBlockGroupViewModel : ViewModelBase
{ {
private bool? _isChecked; private bool? _isShowChecked;
private bool _reentrancyCheck; private bool? _isEnableChecked;
private bool _postMapComplete; private bool _showReentrancyCheck;
private bool _enableReentrancyCheck;
private bool _showPostMapComplete;
private bool _enablePostMapComplete;
private bool _isExpanded; private bool _isExpanded;
public ItemFilterBlockGroupViewModel() public ItemFilterBlockGroupViewModel()
@ -24,7 +27,8 @@ namespace Filtration.ViewModels
ParentGroup = parent; ParentGroup = parent;
Advanced = itemFilterBlockGroup.Advanced; Advanced = itemFilterBlockGroup.Advanced;
SourceBlockGroup = itemFilterBlockGroup; SourceBlockGroup = itemFilterBlockGroup;
IsChecked = itemFilterBlockGroup.IsChecked; IsShowChecked = itemFilterBlockGroup.IsShowChecked;
IsEnableChecked = itemFilterBlockGroup.IsEnableChecked;
ChildGroups = new ObservableCollection<ItemFilterBlockGroupViewModel>(); ChildGroups = new ObservableCollection<ItemFilterBlockGroupViewModel>();
foreach (var childGroup in itemFilterBlockGroup.ChildGroups.Where(c => showAdvanced || !c.Advanced)) foreach (var childGroup in itemFilterBlockGroup.ChildGroups.Where(c => showAdvanced || !c.Advanced))
@ -40,17 +44,30 @@ namespace Filtration.ViewModels
private void SetIsCheckedBasedOnChildGroups() private void SetIsCheckedBasedOnChildGroups()
{ {
if (ChildGroups.All(g => g.IsChecked == true)) if (ChildGroups.All(g => g.IsShowChecked == true))
{ {
IsChecked = true; IsShowChecked = true;
} }
else if (ChildGroups.Any(g => g.IsChecked == true || g.IsChecked == null)) else if (ChildGroups.Any(g => g.IsShowChecked == true || g.IsShowChecked == null))
{ {
IsChecked = null; IsShowChecked = null;
} }
else else
{ {
IsChecked = false; IsShowChecked = false;
}
if (ChildGroups.All(g => g.IsEnableChecked == true))
{
IsEnableChecked = true;
}
else if (ChildGroups.Any(g => g.IsEnableChecked == true || g.IsEnableChecked == null))
{
IsEnableChecked = null;
}
else
{
IsEnableChecked = false;
} }
} }
@ -60,34 +77,67 @@ namespace Filtration.ViewModels
public bool Advanced { get; internal set; } public bool Advanced { get; internal set; }
public ItemFilterBlockGroup SourceBlockGroup { get; internal set; } public ItemFilterBlockGroup SourceBlockGroup { get; internal set; }
public bool? IsChecked public bool? IsShowChecked
{ {
get get
{ {
return _isChecked; return _isShowChecked;
} }
set set
{ {
if (!_postMapComplete) if (!_showPostMapComplete)
{ {
_isChecked = value; _isShowChecked = value;
_postMapComplete = true; _showPostMapComplete = true;
} }
else else
{ {
if (_isChecked != value) if (_isShowChecked != value)
{ {
if (_reentrancyCheck) if (_showReentrancyCheck)
{ {
return; return;
} }
_reentrancyCheck = true; _showReentrancyCheck = true;
_isChecked = value; _isShowChecked = value;
UpdateCheckState(); UpdateCheckState(true);
RaisePropertyChanged(); RaisePropertyChanged();
SourceBlockGroup.IsChecked = value ?? false; SourceBlockGroup.IsShowChecked = value ?? false;
_reentrancyCheck = false; _showReentrancyCheck = false;
}
}
}
}
public bool? IsEnableChecked
{
get
{
return _isEnableChecked;
}
set
{
if (!_enablePostMapComplete)
{
_isEnableChecked = value;
_enablePostMapComplete = true;
}
else
{
if (_isEnableChecked != value)
{
if (_enableReentrancyCheck)
{
return;
}
_enableReentrancyCheck = true;
_isEnableChecked = value;
UpdateCheckState(false);
RaisePropertyChanged();
SourceBlockGroup.IsEnableChecked = value ?? false;
_enableReentrancyCheck = false;
} }
} }
} }
@ -103,39 +153,58 @@ namespace Filtration.ViewModels
} }
} }
private void UpdateCheckState() private void UpdateCheckState(bool isShowCheck)
{ {
// update all children: // update all children:
if (ChildGroups.Count != 0) if (ChildGroups.Count != 0)
{ {
UpdateChildrenCheckState(); UpdateChildrenCheckState(isShowCheck);
} }
// update parent item // update parent item
if (ParentGroup != null) if (ParentGroup != null)
{ {
var parentIsChecked = ParentGroup.DetermineCheckState(); var parentIsChecked = ParentGroup.DetermineCheckState(isShowCheck);
ParentGroup.IsChecked = parentIsChecked; if(isShowCheck)
{
ParentGroup.IsShowChecked = parentIsChecked;
}
else
{
ParentGroup.IsEnableChecked = parentIsChecked;
}
} }
} }
private void UpdateChildrenCheckState() private void UpdateChildrenCheckState(bool isShowCheck)
{ {
foreach (var childGroup in ChildGroups.Where(c => IsChecked != null)) if(isShowCheck)
{ {
childGroup.IsChecked = IsChecked; foreach (var childGroup in ChildGroups.Where(c => IsShowChecked != null))
{
childGroup.IsShowChecked = IsShowChecked;
}
}
else
{
foreach (var childGroup in ChildGroups.Where(c => IsEnableChecked != null))
{
childGroup.IsEnableChecked = IsEnableChecked;
}
} }
} }
private bool? DetermineCheckState() private bool? DetermineCheckState(bool isShowCheck)
{ {
var allChildrenChecked = ChildGroups.Count(x => x.IsChecked == true) == ChildGroups.Count; var allChildrenChecked = (isShowCheck ? ChildGroups.Count(x => x.IsShowChecked == true) :
ChildGroups.Count(x => x.IsEnableChecked == true)) == ChildGroups.Count;
if (allChildrenChecked) if (allChildrenChecked)
{ {
return true; return true;
} }
var allChildrenUnchecked = ChildGroups.Count(x => x.IsChecked == false) == ChildGroups.Count; var allChildrenUnchecked = (isShowCheck ? ChildGroups.Count(x => x.IsShowChecked == false) :
ChildGroups.Count(x => x.IsEnableChecked == false)) == ChildGroups.Count;
if (allChildrenUnchecked) if (allChildrenUnchecked)
{ {
return false; return false;

View File

@ -65,6 +65,7 @@ namespace Filtration.ViewModels
_parentScriptViewModel = parentScriptViewModel; _parentScriptViewModel = parentScriptViewModel;
Block = itemFilterBlock; Block = itemFilterBlock;
Block.EnabledStatusChanged += OnBlockEnabledStatusChanged;
itemFilterBlock.BlockItems.CollectionChanged += OnBlockItemsCollectionChanged; itemFilterBlock.BlockItems.CollectionChanged += OnBlockItemsCollectionChanged;
@ -438,6 +439,11 @@ namespace Filtration.ViewModels
} }
} }
private void OnBlockEnabledStatusChanged(object sender, EventArgs e)
{
RaisePropertyChanged(nameof(BlockEnabled));
}
private void OnBlockItemChanged(object sender, EventArgs e) private void OnBlockItemChanged(object sender, EventArgs e)
{ {
var itemFilterBlockItem = sender as IItemFilterBlockItem; var itemFilterBlockItem = sender as IItemFilterBlockItem;

View File

@ -38,8 +38,9 @@
<TreeView.Resources> <TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type viewModels:ItemFilterBlockGroupViewModel}" ItemsSource="{Binding ChildGroups}"> <HierarchicalDataTemplate DataType="{x:Type viewModels:ItemFilterBlockGroupViewModel}" ItemsSource="{Binding ChildGroups}">
<WrapPanel> <WrapPanel>
<CheckBox IsThreeState="True" IsChecked="{Binding IsChecked}" Click="BlockGroupCheckBox_Clicked" /> <CheckBox IsThreeState="True" IsChecked="{Binding IsShowChecked}" Click="BlockGroupCheckBox_Clicked" ToolTip="Show/Hide" />
<TextBlock Text="{Binding GroupName}" VerticalAlignment="Center" Foreground="{Binding Advanced, Converter={StaticResource BlockGroupAdvancedColorConverter}}" /> <CheckBox IsThreeState="True" IsChecked="{Binding IsEnableChecked}" Click="BlockGroupCheckBox_Clicked" ToolTip="Enable/Disable" />
<TextBlock Text="{Binding GroupName}" VerticalAlignment="Center" Foreground="{Binding Advanced, Converter={StaticResource BlockGroupAdvancedColorConverter}}" />
</WrapPanel> </WrapPanel>
</HierarchicalDataTemplate> </HierarchicalDataTemplate>
</TreeView.Resources> </TreeView.Resources>