Files
aistudio-wpf-diagram/AIStudio.Wpf.DiagramDesigner/ViewModels/BlockViewModel/BlockDesignerItemViewModel.cs

641 lines
19 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
2023-07-22 19:11:08 +08:00
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Documents;
2023-06-18 16:21:18 +08:00
using AIStudio.Wpf.DiagramDesigner.Geometrys;
using AIStudio.Wpf.DiagramDesigner.Models;
2023-06-13 19:06:51 +08:00
using static System.Net.Mime.MediaTypeNames;
namespace AIStudio.Wpf.DiagramDesigner
{
public class BlockDesignerItemViewModel : DesignerItemViewModelBase
{
public BlockDesignerItemViewModel()
{
}
public BlockDesignerItemViewModel(IDiagramViewModel root) : base(root)
{
}
public BlockDesignerItemViewModel(IDiagramViewModel root, SelectableItemBase designer) : base(root, designer)
{
}
public BlockDesignerItemViewModel(IDiagramViewModel root, SerializableItem serializableItem, string serializableType) : base(root, serializableItem, serializableType)
{
}
2023-07-15 10:20:50 +08:00
public override SelectableItemBase GetSerializableObject()
{
return new BlockDesignerItem(this);
}
protected override void InitNew()
{
ItemWidth = double.NaN;
ItemHeight = double.NaN;
AddConnector(new BlockConnectorInfo(this.Root, this, ConnectorOrientation.Top));
AddConnector(new BlockConnectorInfo(this.Root, this, ConnectorOrientation.Bottom));
2023-07-15 10:20:50 +08:00
}
protected override void Init(IDiagramViewModel root, bool initNew)
{
base.Init(root, initNew);
IsReadOnlyText = true;
}
2023-07-15 10:20:50 +08:00
protected override void LoadDesignerItemViewModel(SelectableItemBase designerbase)
{
base.LoadDesignerItemViewModel(designerbase);
if (designerbase is BlockDesignerItem designer)
{
if (designer.Connectors != null)
{
ClearConnectors();
foreach (var connector in designer.Connectors)
{
BlockConnectorInfo fullyCreatedConnectorInfo = new BlockConnectorInfo(this.Root, this, connector);
AddConnector(fullyCreatedConnectorInfo);
}
}
if (designer.Containers != null)
{
ClearContainers();
foreach (var container in designer.Containers)
{
BlockItemsContainerInfo blockitemsContainerInfo = new BlockItemsContainerInfo(this.Root, this, container);
AddContainer(blockitemsContainerInfo);
}
}
}
}
public void AddNext(BlockDesignerItemViewModel next, BlockDesignerItemViewModel first = null)
{
if (LinkNode.Next?.Value == next)
2023-06-13 19:06:51 +08:00
{
AlignNext(LinkNode.Next);
2023-06-13 19:06:51 +08:00
return;
}
2023-07-01 21:38:23 +08:00
var linkedList = LinkNode.List;
var nextlinkedList = next.LinkNode.List;
if (nextlinkedList == linkedList)//后面的拖到前面去了
2023-07-01 21:38:23 +08:00
{
if (first != null)
{
first.RemovePrevious();
}
else
{
next.LinkNode.Previous?.Value.RemoveNext();
}
nextlinkedList = next.LinkNode.List;
2023-07-01 21:38:23 +08:00
}
linkedList.InsertRange(LinkNode, nextlinkedList);
nextlinkedList.Clear();
2023-07-01 21:38:23 +08:00
AlignNext(LinkNode.Next);
}
2023-06-13 19:06:51 +08:00
public void AlignNext(LinkedListNode<BlockDesignerItemViewModel> nextnode)
{
if (nextnode != null && LinkNode.Next == nextnode)
2023-06-13 19:06:51 +08:00
{
nextnode.Value.Left = this.Left;
nextnode.Value.Top = this.Top + this.GetItemHeight();
if (nextnode.Next != null)
{
nextnode.Value.AlignNext(nextnode.Next);
}
2023-06-13 19:06:51 +08:00
}
}
public List<BlockDesignerItemViewModel> RemoveNext(bool allbreak = false)
{
List<BlockDesignerItemViewModel> removes = new List<BlockDesignerItemViewModel>();
var next = LinkNode?.Next;
if (next != null)
2023-06-17 23:55:54 +08:00
{
var linkedList = LinkNode.List;
var list = linkedList.ToList();
int index = list.IndexOf(next.Value);
//BlockDesignerItemViewModel[] array = new BlockDesignerItemViewModel[linkedList.Count];
//linkedList.CopyTo(array, index);
var nextlinkedList = new LinkedList<BlockDesignerItemViewModel>();
for (int i = list.Count - 1; i >= index; i--)
2023-07-09 21:03:35 +08:00
{
var nextnode = linkedList.Find(list[i]);
linkedList.Remove(nextnode);
if (allbreak)//全部变成散落的
{
list[i].ParentId = new Guid();
}
else//后面的连接成一个链表
{
list[i].LinkNode = nextlinkedList.AddFirst(list[i]);
}
removes.Add(list[i]);
2023-07-09 21:03:35 +08:00
}
2023-06-18 16:21:18 +08:00
if (nextlinkedList.First != null)
{
nextlinkedList.First.Value.ParentId = new Guid();
}
2023-06-17 23:55:54 +08:00
}
return removes;
2023-06-13 19:06:51 +08:00
}
public List<BlockDesignerItemViewModel> RemoveSelf(bool allbreak = false)
2023-06-13 19:06:51 +08:00
{
List<BlockDesignerItemViewModel> removes = new List<BlockDesignerItemViewModel>();
var linkedList = LinkNode.List;
var list = linkedList.ToList();
int index = list.IndexOf(this);
var nextlinkedList = new LinkedList<BlockDesignerItemViewModel>();
for (int i = list.Count - 1; i >= index; i--)
2023-06-13 19:06:51 +08:00
{
var nextnode = linkedList.Find(list[i]);
linkedList.Remove(nextnode);
if (allbreak)//全部变成散落的
2023-06-13 19:06:51 +08:00
{
list[i].ParentId = new Guid();
2023-06-13 19:06:51 +08:00
}
else//后面的连接成一个链表
{
list[i].LinkNode = nextlinkedList.AddFirst(list[i]);
}
removes.Add(list[i]);
2023-06-13 19:06:51 +08:00
}
if (nextlinkedList.First != null)
{
nextlinkedList.First.Value.ParentId = new Guid();
}
return removes;
}
public BlockDesignerItemViewModel RemovePrevious(bool allbreak = false)
{
var previous = this.LinkNode.Previous;
if (previous != null)
{
var linkedList = LinkNode.List;
var previouslinkedList = new LinkedList<BlockDesignerItemViewModel>();
var list = linkedList.ToList();
int index = list.IndexOf(previous.Value);
for (int i = 0; i <= index; i++)
{
var previousnode = linkedList.Find(list[i]);
linkedList.Remove(previousnode);
if (allbreak)//全部变成散落的
{
list[i].ParentId = new Guid();
}
else//前面的连接成一个链表
{
list[i].LinkNode = previouslinkedList.AddLast(list[i]);
}
}
this.ParentId = new Guid();
return previous.Value;
2023-07-01 21:38:23 +08:00
}
else
{
return null;
}
}
public override void AddToSelection(bool selected, bool clearother)
{
if (clearother)
{
foreach (SelectableDesignerItemViewModelBase item in Root.SelectedItems.ToList())
{
if (item != this)
{
item.RemoveFromSelection();
}
}
}
IsSelected = selected;
}
2023-06-27 22:57:08 +08:00
public virtual void InsertChild(BlockDesignerItemViewModel child, BlockItemsContainerInfo container, int index)
{
2023-06-18 16:21:18 +08:00
if (container == null)
{
container = FirstContainer;
}
if (container.OnlyOneChild)
{
var oldchildren = container.Children.FirstOrDefault();
if (oldchildren != null)
{
this.RemoveChild(oldchildren, container);
}
2023-07-02 11:23:00 +08:00
Root.Items.Remove(child);
if (child.LinkNode?.Previous != null)
2023-07-02 11:23:00 +08:00
{
child.LinkNode.Previous.Value.RemoveNext();
2023-07-02 11:23:00 +08:00
}
if (child.LinkNode?.Next != null)
2023-07-02 11:23:00 +08:00
{
child.LinkNode.Next.Value.RemovePrevious();
2023-07-02 11:23:00 +08:00
}
container.InsertChild(child, index);
}
else
{
var items = new List<BlockDesignerItemViewModel>();
var linkedList = child.LinkNode.List;
if (child.LinkNode?.Previous != null)
{
items = child.LinkNode.Previous.Value.RemoveNext(true);
}
else
{
items = child.LinkNode.Value.RemoveSelf(true);
}
items.ForEach(p => {
2023-07-02 11:23:00 +08:00
Root.Items.Remove(p);
container.InsertChild(p, index);
});
2023-06-18 16:21:18 +08:00
}
child.RemoveFromSelection();
2023-06-17 09:31:15 +08:00
this.GetRootContainItem.AddToSelection(true, true);
2023-06-13 19:06:51 +08:00
System.Windows.Application.Current?.Dispatcher.BeginInvoke(new Action(async () => {
await Task.Delay(10);
AlignNext(LinkNode?.Next);
}));
}
2023-06-17 23:55:54 +08:00
public virtual void RemoveChild(BlockDesignerItemViewModel child, BlockItemsContainerInfo container)
{
2023-06-18 16:21:18 +08:00
if (container == null)
{
container = FirstContainer;
}
Root.Items.Add(child);
container.RemoveChild(child);
2023-06-15 22:44:12 +08:00
2023-06-18 16:21:18 +08:00
this.RemoveFromSelection();
2023-06-15 22:44:12 +08:00
child.AddToSelection(true, true);
2023-06-13 19:06:51 +08:00
System.Windows.Application.Current?.Dispatcher.BeginInvoke(new Action(async () => {
await Task.Delay(10);
AlignNext(LinkNode?.Next);
}));
}
2023-07-22 19:11:08 +08:00
private bool _isExecuting;
public bool IsExecuting
{
get
{
return _isExecuting;
}
set
{
SetProperty(ref _isExecuting, value);
}
}
2023-06-18 16:21:18 +08:00
public string Flag
{
get; set;
}
public int LinkNodeLevel
2023-06-17 23:55:54 +08:00
{
get
{
if (LinkNode?.Previous == null)
2023-06-17 23:55:54 +08:00
{
return 0;
}
else
{
return LinkNode.Previous.Value.LinkNodeLevel + 1;
2023-06-17 23:55:54 +08:00
}
}
}
private LinkedListNode<BlockDesignerItemViewModel> _linkNode;
public LinkedListNode<BlockDesignerItemViewModel> LinkNode
2023-06-17 09:31:15 +08:00
{
get
{
if (_linkNode == null || _linkNode.List == null)
{
var linklist = new LinkedList<BlockDesignerItemViewModel>(); ;
_linkNode = linklist.AddFirst(this);
}
return _linkNode;
2023-06-17 09:31:15 +08:00
}
2023-07-01 21:38:23 +08:00
set
{
_linkNode = value;
2023-07-01 21:38:23 +08:00
}
2023-06-17 09:31:15 +08:00
}
2023-06-17 23:55:54 +08:00
public BlockItemsContainerInfo ParentContainer
2023-06-17 09:31:15 +08:00
{
get; set;
}
2023-06-17 23:55:54 +08:00
public ObservableCollection<BlockItemsContainerInfo> Containers
{
get; set;
2023-06-16 19:03:15 +08:00
} = new ObservableCollection<BlockItemsContainerInfo>();
2023-06-17 23:55:54 +08:00
public BlockItemsContainerInfo FirstContainer
{
get
{
2023-06-17 23:55:54 +08:00
return Containers?.FirstOrDefault();
}
2023-06-17 09:31:15 +08:00
}
2023-06-17 23:55:54 +08:00
public BlockItemsContainerInfo SecondContainer
{
get
{
return Containers?.Skip(1)?.FirstOrDefault();
}
}
public BlockItemsContainerInfo ThirdContainer
{
get
{
return Containers?.Skip(2)?.FirstOrDefault();
}
2023-06-18 16:21:18 +08:00
}
2023-06-17 23:55:54 +08:00
2023-06-18 18:43:48 +08:00
public void AddContainer(BlockItemsContainerInfo container)
2023-06-15 22:44:12 +08:00
{
2023-06-18 18:43:48 +08:00
Containers.Add(container);
RaisePropertyChanged(nameof(FirstContainer));
RaisePropertyChanged(nameof(SecondContainer));
RaisePropertyChanged(nameof(ThirdContainer));
}
public void RemoveContainer(BlockItemsContainerInfo container)
{
Containers.Remove(container);
RaisePropertyChanged(nameof(FirstContainer));
RaisePropertyChanged(nameof(SecondContainer));
RaisePropertyChanged(nameof(ThirdContainer));
}
public void ClearContainers()
{
Containers.Clear();
RaisePropertyChanged(nameof(FirstContainer));
RaisePropertyChanged(nameof(SecondContainer));
RaisePropertyChanged(nameof(ThirdContainer));
}
public List<BlockItemsContainerInfo> GetAllContainers()
{
return Containers.SelectMany(p => p.GetAllContainers(p.Children, true)).ToList();
2023-06-15 22:44:12 +08:00
}
2023-06-17 09:31:15 +08:00
public BlockDesignerItemViewModel GetRootContainItem
2023-06-15 22:44:12 +08:00
{
get
{
2023-06-17 23:55:54 +08:00
if (ParentContainer == null)
2023-06-15 22:44:12 +08:00
{
return this;
}
else
{
2023-06-17 23:55:54 +08:00
return ParentContainer.DataItem.GetRootContainItem;
2023-06-15 22:44:12 +08:00
}
}
}
2023-07-18 22:44:31 +08:00
#region
2023-08-03 23:01:33 +08:00
protected override async void ExecuteEditCommand(object param)
{
await this.LinkNode.List.First.Value.Execute();
2023-08-03 23:01:33 +08:00
}
public override void PreviewExecuteEdit()
2023-08-04 19:03:42 +08:00
{
var items = this.LinkNode.List.ToList();
2023-08-04 22:05:46 +08:00
items.ForEach(p => p.IsExecuting = true);
}
public override void ExitPreviewExecuteEdit()
2023-08-04 22:05:46 +08:00
{
var items = this.LinkNode.List.ToList();
2023-08-04 22:05:46 +08:00
items.ForEach(p => p.IsExecuting = false);
2023-08-04 19:03:42 +08:00
}
2023-07-23 17:40:44 +08:00
public async Task Execute()
2023-07-18 22:44:31 +08:00
{
2023-08-13 11:36:47 +08:00
await StopExecution();
await BeforeExecution();
if (await Executing())
{
await AfterExecution();
}
else
{
IsExecuting = false;
}
2023-07-22 19:11:08 +08:00
}
2023-07-18 22:44:31 +08:00
2023-08-13 11:36:47 +08:00
public virtual Task BeforeExecution()
2023-07-22 19:11:08 +08:00
{
IsExecuting = true;
2023-07-23 17:40:44 +08:00
return Task.CompletedTask;
2023-07-22 19:11:08 +08:00
}
2023-08-13 11:36:47 +08:00
public virtual Task<bool> Executing()
{
return Task.FromResult(true);
}
public virtual Task StopExecution()
2023-07-22 19:11:08 +08:00
{
2023-07-23 17:40:44 +08:00
return Task.CompletedTask;
2023-07-22 19:11:08 +08:00
}
2023-08-13 11:36:47 +08:00
public virtual async Task AfterExecution()
2023-07-22 19:11:08 +08:00
{
2023-07-27 19:27:37 +08:00
if (IsExecuting)
{
IsExecuting = false;
if (LinkNode.Next != null)
await LinkNode.Next.Value.Execute();
2023-07-27 19:27:37 +08:00
}
2023-07-18 22:44:31 +08:00
}
2023-07-20 19:12:43 +08:00
public virtual object GetResult()
{
if (FirstContainer != null)
{
return FirstContainer.GetResult();
}
else if (SecondContainer != null)
{
return SecondContainer.GetResult();
}
else if (ThirdContainer != null)
{
return ThirdContainer.GetResult();
}
return null;
}
2023-07-18 22:44:31 +08:00
#endregion
}
2023-06-18 16:21:18 +08:00
#region
public class BlockContainDesignerItemViewModel : BlockDesignerItemViewModel
{
public BlockContainDesignerItemViewModel()
{
}
public BlockContainDesignerItemViewModel(IDiagramViewModel root) : base(root)
{
}
public BlockContainDesignerItemViewModel(IDiagramViewModel root, SelectableItemBase designer) : base(root, designer)
{
}
public BlockContainDesignerItemViewModel(IDiagramViewModel root, SerializableItem serializableItem, string serializableType) : base(root, serializableItem, serializableType)
{
}
protected override void InitNew()
{
base.InitNew();
Containers.Add(new BlockItemsContainerInfo(this.Root, this, true, null));
}
}
public class BlockContainListDesignerItemViewModel : BlockDesignerItemViewModel
{
public BlockContainListDesignerItemViewModel()
{
}
public BlockContainListDesignerItemViewModel(IDiagramViewModel root) : base(root)
{
}
public BlockContainListDesignerItemViewModel(IDiagramViewModel root, SelectableItemBase designer) : base(root, designer)
{
}
public BlockContainListDesignerItemViewModel(IDiagramViewModel root, SerializableItem serializableItem, string serializableType) : base(root, serializableItem, serializableType)
{
}
protected override void InitNew()
{
base.InitNew();
Containers.Add(new BlockItemsContainerInfo(this.Root, this, false, null));
}
}
#endregion
#region
public class BlockDesignerItemTempLink
{
public LinkedList<BlockDesignerItemViewModel> LinkList
2023-06-18 16:21:18 +08:00
{
get; set;
} = new LinkedList<BlockDesignerItemViewModel>();
2023-06-18 16:21:18 +08:00
public RectangleBase GetBounds()
{
return LinkList.FirstOrDefault().GetBounds();
2023-06-18 16:21:18 +08:00
}
public List<FullyCreatedConnectorInfo> Connectors
{
get
{
List<FullyCreatedConnectorInfo> connectors = new List<FullyCreatedConnectorInfo>();
if (LinkList.FirstOrDefault().TopConnector != null)
2023-06-18 16:21:18 +08:00
{
connectors.Add(LinkList.FirstOrDefault().TopConnector);
2023-06-18 16:21:18 +08:00
}
if (LinkList.FirstOrDefault().LeftConnector != null)
2023-06-18 16:21:18 +08:00
{
connectors.Add(LinkList.FirstOrDefault().LeftConnector);
2023-06-18 16:21:18 +08:00
}
if (LinkList.LastOrDefault().BottomConnector != null)
2023-06-18 16:21:18 +08:00
{
connectors.Add(LinkList.LastOrDefault().BottomConnector);
2023-06-18 16:21:18 +08:00
}
if (LinkList.LastOrDefault().RightConnector != null)
2023-06-18 16:21:18 +08:00
{
connectors.Add(LinkList.LastOrDefault().RightConnector);
2023-06-18 16:21:18 +08:00
}
return connectors;
}
}
public static List<BlockDesignerItemTempLink> Build(List<BlockDesignerItemViewModel> blocks)
{
List<BlockDesignerItemTempLink> links = new List<BlockDesignerItemTempLink>();
foreach (var blockgroup in blocks.GroupBy(p => p.LinkNode.List))
2023-07-02 11:23:00 +08:00
{
var link = new BlockDesignerItemTempLink();
foreach (var block in blockgroup.OrderBy(p => p.LinkNodeLevel))
2023-06-18 16:21:18 +08:00
{
link.LinkList.AddLast(block);
2023-06-18 16:21:18 +08:00
}
links.Add(link);
2023-06-18 16:21:18 +08:00
}
return links;
}
}
#endregion
}