Skip to content

Commit

Permalink
allow to compare parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
NeVeSpl committed Oct 12, 2023
1 parent 97f3d60 commit 20c41ef
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 120 deletions.
7 changes: 6 additions & 1 deletion sources/RevitDBExplorer/Domain/DataModel/SnoopableItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@

namespace RevitDBExplorer.Domain.DataModel
{
internal abstract class SnoopableItem : BaseViewModel, IAmSourceOfEverything
internal abstract class SnoopableItem : BaseViewModel, IAmSourceOfEverything, IComparable<SnoopableItem>, IEquatable<SnoopableItem>
{
private readonly SnoopableObject parent;
private readonly IAccessor accessor;
private readonly IValueViewModel itemValueViewModel;

public event Action ParentObjectChanged;
public string AccessorName { get; private set; }
public virtual string Name { get;}
public IValueViewModel ValueViewModel { get; private set; } = EmptyPresenter.Instance;
public bool CanBeSnooped
{
Expand Down Expand Up @@ -101,5 +102,9 @@ public void Freeze(int candies)
}
isFrozen = true;
}


public abstract int CompareTo(SnoopableItem other);
public abstract bool Equals(SnoopableItem other);
}
}
21 changes: 15 additions & 6 deletions sources/RevitDBExplorer/Domain/DataModel/SnoopableMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

namespace RevitDBExplorer.Domain.DataModel
{
internal sealed class SnoopableMember : SnoopableItem, IComparable<SnoopableMember>, IEquatable<SnoopableMember>
internal sealed class SnoopableMember : SnoopableItem
{
private readonly MemberDescriptor memberDescriptor;


public DeclaringType DeclaringType => memberDescriptor.DeclaringType;
public MemberKind MemberKind => memberDescriptor.Kind;
public string Name => memberDescriptor.Name;
public override string Name => memberDescriptor.Name;
public DocXml Documentation => memberDescriptor.Documentation;


Expand All @@ -38,13 +38,22 @@ public override SourceOfObjects Snoop()



public int CompareTo(SnoopableMember other)
public override int CompareTo(SnoopableItem other)
{
return memberDescriptor.CompareTo(other.memberDescriptor);
if (other is SnoopableMember snoopableMember)
{
return memberDescriptor.CompareTo(snoopableMember.memberDescriptor);
}
return 1;

}
public bool Equals(SnoopableMember other)
public override bool Equals(SnoopableItem other)
{
return memberDescriptor.Equals(other.memberDescriptor);
if (other is SnoopableMember snoopableMember)
{
return memberDescriptor.Equals(snoopableMember.memberDescriptor);
}
return false;
}
}
}
21 changes: 20 additions & 1 deletion sources/RevitDBExplorer/Domain/DataModel/SnoopableParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal class SnoopableParameter : SnoopableItem
private readonly Parameter parameter;


public string Name => parameter.Definition.Name;
public override string Name => parameter.Definition.Name;


public SnoopableParameter(SnoopableObject parent, Parameter parameter) : base(parent, new ParameterAccessor(parameter))
Expand All @@ -24,5 +24,24 @@ public override SourceOfObjects Snoop()
{
return new SourceOfObjects(this);
}



public override int CompareTo(SnoopableItem other)
{
if (other is SnoopableParameter snoopableParameter)
{
return parameter.Id.Compare(snoopableParameter.parameter.Id);
}
return -1;
}
public override bool Equals(SnoopableItem other)
{
if (other is SnoopableParameter snoopableParameter)
{
return parameter.Id.Equals(snoopableParameter.parameter.Id);
}
return false;
}
}
}
113 changes: 68 additions & 45 deletions sources/RevitDBExplorer/UIComponents/List/ListVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ internal class ListVM : BaseViewModel
private readonly ObservableCollection<DynamicGridViewColumn> columnsFor1;
private readonly ObservableCollection<DynamicGridViewColumn> columnsFor2;
private readonly IAmWindowOpener windowOpener;
private ObservableCollection<ListItem> listItems = new();
private ObservableCollection<IListItem> listItems = new();
private ObservableCollection<DynamicGridViewColumn> columns = new();
private ListItem listSelectedItem = null;
private IListItem listSelectedItem = null;
private TreeItem treeSelectedItem = null;
private string filterPhrase = "";
private bool isMemberViewVisible = true;
private bool hasParameters;


public ObservableCollection<ListItem> ListItems
public ObservableCollection<IListItem> ListItems
{
get
{
Expand All @@ -56,7 +56,7 @@ public ObservableCollection<DynamicGridViewColumn> Columns
OnPropertyChanged();
}
}
public ListItem ListSelectedItem
public IListItem ListSelectedItem
{
get
{
Expand Down Expand Up @@ -98,6 +98,7 @@ public bool IsMemberViewVisible
isMemberViewVisible = value;
OnPropertyChanged();
OnPropertyChanged(nameof(IsParameterViewVisible));

PopulateListView();
}
}
Expand Down Expand Up @@ -153,34 +154,52 @@ public void ClearItems()
{
ListItems = new();
}


public async Task PopulateListView(SnoopableObjectTreeItem snoopableObjectTreeItem)
{
treeSelectedItem = snoopableObjectTreeItem;
await PopulateListView();
}
private async Task PopulateListView()
public async Task<bool> PopulateListView(UtilityGroupTreeItem utilityGroupTreeItem)
{
treeSelectedItem = utilityGroupTreeItem;
return await PopulateListView();
}
private async Task<bool> PopulateListView()
{
HasParameters = false;
if (treeSelectedItem is SnoopableObjectTreeItem snoopableObjectTreeItem)
{
HasParameters = snoopableObjectTreeItem.Object.HasParameters;
Columns = columnsFor1;
if (IsMemberViewVisible)
{
var members = await ExternalExecutor.ExecuteInRevitContextAsync(x => snoopableObjectTreeItem.Object.GetMembers(x).ToList());
ListItems = new(members.Select(x => new ListItemForSM(x, null, Reload)));
SetupListView();
}
else
{
var parameters = await ExternalExecutor.ExecuteInRevitContextAsync(x => snoopableObjectTreeItem.Object.GetParameters(x).ToList());
ListItems = new(parameters.Select(x => new ListItemForSP(x, null, Reload)));
}

var items = await GetSnoopableItemsFromRevit(snoopableObjectTreeItem.Object);
ListItems = new(items.Select(x => ListItemFactory.Create(x, null, Reload)));
SetupListView();

return true;
}
if (treeSelectedItem is UtilityGroupTreeItem utilityGroupTreeItem)
{
return await PopulateListViewForComparison(utilityGroupTreeItem);
}
return false;
}
public async Task<bool> PopulateListView(UtilityGroupTreeItem utilityGroupTreeItem)
private async Task<IList<SnoopableItem>> GetSnoopableItemsFromRevit(SnoopableObject snoopableObject)
{
HasParameters = false;
if (IsMemberViewVisible)
{
var members = await ExternalExecutor.ExecuteInRevitContextAsync(x => snoopableObject.GetMembers(x).OrderBy(x => x).OfType<SnoopableItem>().ToList());
return members;
}
var parameters = await ExternalExecutor.ExecuteInRevitContextAsync(x => snoopableObject.GetParameters(x).OrderBy(x => x).OfType<SnoopableItem>().ToList());
return parameters;
}


private async Task<bool> PopulateListViewForComparison(UtilityGroupTreeItem utilityGroupTreeItem)
{
if (utilityGroupTreeItem.Items?.Count < 2)
{
return false;
Expand All @@ -194,64 +213,68 @@ public async Task<bool> PopulateListView(UtilityGroupTreeItem utilityGroupTreeIt
return false;
}

var leftMembers = await ExternalExecutor.ExecuteInRevitContextAsync(x => leftItem.Object.GetMembers(x).OrderBy(x => x).ToList());
var rightMembers = await ExternalExecutor.ExecuteInRevitContextAsync(x => rightItem.Object.GetMembers(x).OrderBy(x => x).ToList());
HasParameters = leftItem.Object.HasParameters;

var leftItems = await GetSnoopableItemsFromRevit(leftItem.Object);
var rightItems = await GetSnoopableItemsFromRevit(rightItem.Object);
var mergedItems = MergeTwoLists(leftItems, rightItems).Select(x => ListItemFactory.Create(x.first, x.second, Reload, true));

var members = new List<ListItemForSM>();
ListItems = new(mergedItems);
SetupListView();
return true;
}
private IEnumerable<(SnoopableItem first, SnoopableItem second)> MergeTwoLists(IList<SnoopableItem> leftItems, IList<SnoopableItem> rightItems)
{
int i = 0;
int j = 0;
while (i < leftMembers.Count && j < rightMembers.Count)
while (i < leftItems.Count && j < rightItems.Count)
{
var left = leftMembers[i];
var right = rightMembers[j];
var left = leftItems[i];
var right = rightItems[j];

if (left.Equals(right))
{
members.Add(new ListItemForSM(left, right, Reload, true));
yield return (left, right);
++i;
++j;
continue;
}
if (left.CompareTo(right) < 0)
{
members.Add(new ListItemForSM(left, null, Reload, true));
++i;
yield return (left, null);
++i;
continue;
}
members.Add(new ListItemForSM(null, right, Reload, true));
++j;

yield return (null, right);
++j;
}

while (i < leftMembers.Count)
while (i < leftItems.Count)
{
members.Add(new ListItemForSM(leftMembers[i], null, Reload, true));
yield return (leftItems[i], null);
++i;
}
while (j < rightMembers.Count)
while (j < rightItems.Count)
{
members.Add(new ListItemForSM(null, rightMembers[j], Reload, true));
yield return (null, rightItems[j]);
++j;
}

ListItems = new(members);
SetupListView();
return true;
}
private void SetupListView()
{
var lcv = (ListCollectionView)CollectionViewSource.GetDefaultView(ListItems);

if (ListItems.Count < 666)
{
lcv.GroupDescriptions.Add(new PropertyGroupDescription(nameof(ListItem.GroupingKey)));
lcv.SortDescriptions.Add(new SortDescription(nameof(ListItem.SortingKey), ListSortDirection.Ascending));
lcv.GroupDescriptions.Add(new PropertyGroupDescription(nameof(IListItem.GroupingKey)));
lcv.SortDescriptions.Add(new SortDescription(nameof(IListItem.SortingKey), ListSortDirection.Ascending));
}
lcv.Filter = ListViewFilter;
}
private bool ListViewFilter(object item)
{
if (item is ListItem listItem)
if (item is IListItem listItem)
{
return listItem.Filter(FilterPhrase);
}
Expand All @@ -275,9 +298,9 @@ private void FilterListView()

private void KeyDown_Enter(object obj)
{
if (ListSelectedItem is ListItemForSM listItemForSM)
if (ListSelectedItem is ListItemForMember ListItemForMember)
{
ListItem_Click(listItemForSM[0]);
ListItem_Click(ListItemForMember[0]);
}
}
private async void ListItem_Click(object obj)
Expand All @@ -301,14 +324,14 @@ private async void ListItem_Click(object obj)

private void CopyName(object obj)
{
if (obj is ListItem listItem)
if (obj is IListItem listItem)
{
Clipboard.SetDataObject($"{listItem.Name}");
}
}
private void OpenCHM(object obj)
{
if (obj is ListItemForSM listItem)
if (obj is ListItemForMember listItem)
{
CHMService.OpenCHM(listItem[0]);
}
Expand Down
8 changes: 5 additions & 3 deletions sources/RevitDBExplorer/UIComponents/List/ListView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
</Style>


<DataTemplate DataType="{x:Type localVM:ListItemForSM}">
<DataTemplate DataType="{x:Type localVM:ListItemForMember}">
<StackPanel Orientation="Horizontal"
ContextMenu="{StaticResource ContextMenuForNameColumn}"
Style="{StaticResource sStackPanel}"
Expand All @@ -99,8 +99,10 @@
</DataTemplate.Resources>
</DataTemplate>

<DataTemplate DataType="{x:Type localVM:ListItemForSP}">
<StackPanel Orientation="Horizontal" >
<DataTemplate DataType="{x:Type localVM:ListItemForParameter}">
<StackPanel Orientation="Horizontal"
Style="{StaticResource sStackPanel}"
>
<TextBlock Text="{Binding Name, Mode=OneWay}" Foreground="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
</StackPanel>
</DataTemplate>
Expand Down
Loading

0 comments on commit 20c41ef

Please sign in to comment.