Skip to content

Commit

Permalink
a new way of synchronizing pinned objects for comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
NeVeSpl committed Nov 1, 2023
1 parent 7f551d0 commit ad2e69a
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 38 deletions.
15 changes: 1 addition & 14 deletions sources/RevitDBExplorer/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
using RevitDBExplorer.UIComponents.List.ViewModels;
using RevitDBExplorer.UIComponents.QueryEditor;
using RevitDBExplorer.UIComponents.QueryVisualization;
using RevitDBExplorer.UIComponents.Trees.Base;
using RevitDBExplorer.UIComponents.Trees.Base.Items;
using RevitDBExplorer.UIComponents.Workspaces;
using RevitDBExplorer.Utils;
Expand Down Expand Up @@ -214,24 +213,12 @@ private void SnoopEvents_Click(object sender, RoutedEventArgs e)
private void Workspaces_SelectedItemChanged(SelectedItemChangedEventArgs obj)
{
UpdateRDV();
}
private async void Tree_SelectedItemChanged(TreeSelectedItemChangedEventArgs eventArgs)
{
if ((!IsActive && IsLoaded) || ignoreEvents)
return;




}
bool ignoreEvents = false;
void IAmWindowOpener.Open(SourceOfObjects sourceOfObjects)
{
ignoreEvents = true;
var window = new MainWindow(sourceOfObjects);
window.Owner = this;
window.Show();
ignoreEvents = false;
window.Show();
}
private async void TryQueryDatabase(string query)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RevitDBExplorer.Domain.DataModel;

// (c) Revit Database Explorer https://github.com/NeVeSpl/RevitDBExplorer/blob/main/license.md

namespace RevitDBExplorer.UIComponents.Trees.Base.Items
{
internal class UtilityGroupTreeItem : TreeItem
{
public UtilityGroupTreeItem(TreeItemsCommands commands) : base(commands)
public UtilityGroupTreeItem(TreeItemsCommands commands, IEnumerable<SnoopableObject> snoopableObjects) : base(commands)
{
Items = new System.Collections.ObjectModel.ObservableCollection<TreeItem>();
Items = new ObservableCollection<TreeItem>(snoopableObjects.Select(x => new SnoopableObjectTreeItem(x, commands)));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using RevitDBExplorer.Domain.DataModel;
using System;
using System.Linq;
using System.Collections.ObjectModel;
using System.Windows;
using RevitDBExplorer.Domain.DataModel;
using RevitDBExplorer.UIComponents.Trees.Base;
using RevitDBExplorer.UIComponents.Trees.Base.Items;
using RevitDBExplorer.WPF;
Expand All @@ -9,47 +13,82 @@ namespace RevitDBExplorer.UIComponents.Trees.Utility
{
internal class UtilityTreeViewModel : BaseTreeViewModel
{
private static UtilityGroupTreeItem rootItem;
private static ObservableCollection<SnoopableObject> cache = new ObservableCollection<SnoopableObject>();
public static event EventHandler<RemoveItemsEventArgs> RemoveItemsEvent;
public static event EventHandler<AddObjectEventArgs> AddObjectEvent;
public static event EventHandler<MoveItemEventArgs> MoveItemEvent;

private UtilityGroupTreeItem rootItem;

public RelayCommand RemoveCommand { get; }


public UtilityTreeViewModel()
{
AllowToFrezeeItem = true;
rootItem ??= new UtilityGroupTreeItem(TreeItemsCommands) { IsExpanded = true };
rootItem = new UtilityGroupTreeItem(TreeItemsCommands, cache) { IsExpanded = true };
TreeItems.Add(rootItem);
RemoveCommand = new RelayCommand(RemoveItems);

WeakEventManager<UtilityTreeViewModel, RemoveItemsEventArgs>.AddHandler(null, nameof(UtilityTreeViewModel.RemoveItemsEvent), RemoveItemsEventHandler);
WeakEventManager<UtilityTreeViewModel, AddObjectEventArgs>.AddHandler(null, nameof(UtilityTreeViewModel.AddObjectEvent), AddObjectEventHandler);
WeakEventManager<UtilityTreeViewModel, MoveItemEventArgs>.AddHandler(null, nameof(UtilityTreeViewModel.MoveItemEvent), MoveItemEventHandler);
}


public void AddObject(SnoopableObject inputObject)
{
var objectCopy = new SnoopableObject(inputObject.Context.Document, inputObject.Object);
cache.Add(objectCopy);
AddObjectEvent?.Invoke(null, new AddObjectEventArgs(objectCopy));
}
private void AddObjectEventHandler(object sender, AddObjectEventArgs evntArgs)
{
var vm = new SnoopableObjectTreeItem(evntArgs.Object, TreeItemsCommands);
rootItem.Items.Add(vm);
}

private void RemoveItems(object item)
{
if (item is SnoopableObjectTreeItem snoopableObjectTreeItem)
{
rootItem.Items.Remove(snoopableObjectTreeItem);
var index = cache.IndexOf(snoopableObjectTreeItem.Object);
cache.Remove(snoopableObjectTreeItem.Object);
RemoveItemsEvent?.Invoke(null, new RemoveItemsEventArgs(index));
}
else
{
if (SelectedItem != null)
{
SelectedItem.IsSelected = false;
}
rootItem.Items.Clear();
}
{
cache.Clear();
RemoveItemsEvent?.Invoke(null, new RemoveItemsEventArgs(null));
}
}
public void AddObject(SnoopableObject inputObject)
private void RemoveItemsEventHandler(object sender, RemoveItemsEventArgs evntArgs)
{
var objectCopy = new SnoopableObject(inputObject.Context.Document, inputObject.Object);
var vmCopy = new SnoopableObjectTreeItem(objectCopy, TreeItemsCommands);
rootItem.Items.Add(vmCopy);
if (evntArgs.Index is not null)
{
rootItem.Items.RemoveAt(evntArgs.Index.Value);
}
else
{
rootItem.Items.Clear();
}
}

public void MoveItem(SnoopableObjectTreeItem item, SnoopableObjectTreeItem target)
{
var oldIndex = rootItem.Items.IndexOf(item);
var newIndex = rootItem.Items.IndexOf(target);
var o1 = item.Object;
var o2 = target.Object;

var oldIndex = cache.IndexOf(o1);
var newIndex = cache.IndexOf(o2);

rootItem.Items.Move(oldIndex, newIndex);
cache.Move(oldIndex, newIndex);

MoveItemEvent?.Invoke(null, new MoveItemEventArgs(oldIndex, newIndex));
}
private void MoveItemEventHandler(object sender, MoveItemEventArgs evntArgs)
{
rootItem.Items.Move(evntArgs.OldIndex, evntArgs.NewIndex);
}


Expand All @@ -69,5 +108,37 @@ void RemoveSelection(TreeItem item)
}
}
}


internal class RemoveItemsEventArgs : EventArgs
{
public int? Index { get; }

public RemoveItemsEventArgs(int? index)
{
Index = index;
}
}
internal class AddObjectEventArgs : EventArgs
{
public SnoopableObject Object { get; }

public AddObjectEventArgs(SnoopableObject @object)
{
Object = @object;
}
}
internal class MoveItemEventArgs : EventArgs
{
public int OldIndex { get; }
public int NewIndex { get; }


public MoveItemEventArgs(int item, int target)
{
OldIndex = item;
NewIndex = target;
}
}
}
}

0 comments on commit ad2e69a

Please sign in to comment.