mirror of
https://gitee.com/akwkevin/aistudio.-wpf.-diagram
synced 2026-03-03 00:00:57 +08:00
block使用linklist重写,把代码复杂度降低点
This commit is contained in:
@@ -5,6 +5,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Documents;
|
||||
using AIStudio.Wpf.DiagramDesigner.Geometrys;
|
||||
using AIStudio.Wpf.DiagramDesigner.Models;
|
||||
using static System.Net.Mime.MediaTypeNames;
|
||||
@@ -41,7 +42,6 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
ItemHeight = double.NaN;
|
||||
AddConnector(new BlockConnectorInfo(this.Root, this, ConnectorOrientation.Top));
|
||||
AddConnector(new BlockConnectorInfo(this.Root, this, ConnectorOrientation.Bottom));
|
||||
|
||||
}
|
||||
|
||||
protected override void Init(IDiagramViewModel root, bool initNew)
|
||||
@@ -79,69 +79,147 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
}
|
||||
}
|
||||
|
||||
public void AddNext(BlockDesignerItemViewModel next, bool first = true)
|
||||
public void AddNext(BlockDesignerItemViewModel next, BlockDesignerItemViewModel first = null)
|
||||
{
|
||||
if (this.Next == next)
|
||||
if (LinkNode.Next?.Value == next)
|
||||
{
|
||||
AlignNext(next);
|
||||
AlignNext(LinkNode.Next);
|
||||
return;
|
||||
}
|
||||
|
||||
if (next.Prev != null)
|
||||
var linkedList = LinkNode.List;
|
||||
var nextlinkedList = next.LinkNode.List;
|
||||
if (nextlinkedList == linkedList)//后面的拖到前面去了
|
||||
{
|
||||
next.Prev.RemoveNext();
|
||||
}
|
||||
|
||||
var oldnext = RemoveNext();
|
||||
|
||||
next.Left = this.Left;
|
||||
next.Top = this.Top + this.GetItemHeight();
|
||||
next.ParentId = this.Id;
|
||||
next.Prev = this;
|
||||
this.Next = next;
|
||||
if (next.Next != null)
|
||||
{
|
||||
next.AlignNext(next.Next);
|
||||
}
|
||||
if (oldnext != null && first == true)
|
||||
{
|
||||
//if (this.GetItemHeight() > 0)
|
||||
//{
|
||||
// GetLast().AddNext(oldnext, false);
|
||||
//}
|
||||
//else
|
||||
if (first != null)
|
||||
{
|
||||
System.Windows.Application.Current?.Dispatcher.BeginInvoke(new Action(async () => {
|
||||
await Task.Delay(10);
|
||||
GetLast().AddNext(oldnext, false);
|
||||
}));
|
||||
first.RemovePrevious();
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
next.LinkNode.Previous?.Value.RemoveNext();
|
||||
}
|
||||
nextlinkedList = next.LinkNode.List;
|
||||
}
|
||||
linkedList.InsertRange(LinkNode, nextlinkedList);
|
||||
nextlinkedList.Clear();
|
||||
|
||||
AlignNext(LinkNode.Next);
|
||||
}
|
||||
|
||||
public void AlignNext(BlockDesignerItemViewModel next)
|
||||
public void AlignNext(LinkedListNode<BlockDesignerItemViewModel> nextnode)
|
||||
{
|
||||
if (next != null && this.Next == next)
|
||||
if (nextnode != null && LinkNode.Next == nextnode)
|
||||
{
|
||||
next.Left = this.Left;
|
||||
next.Top = this.Top + this.GetItemHeight();
|
||||
if (next.Next != null)
|
||||
nextnode.Value.Left = this.Left;
|
||||
nextnode.Value.Top = this.Top + this.GetItemHeight();
|
||||
if (nextnode.Next != null)
|
||||
{
|
||||
next.AlignNext(next.Next);
|
||||
nextnode.Value.AlignNext(nextnode.Next);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public BlockDesignerItemViewModel RemoveNext()
|
||||
public List<BlockDesignerItemViewModel> RemoveNext(bool allbreak = false)
|
||||
{
|
||||
var next = this.Next;
|
||||
List<BlockDesignerItemViewModel> removes = new List<BlockDesignerItemViewModel>();
|
||||
var next = LinkNode?.Next;
|
||||
if (next != null)
|
||||
{
|
||||
next.ParentId = new Guid();
|
||||
next.Prev = null;
|
||||
this.Next = null;
|
||||
return next;
|
||||
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--)
|
||||
{
|
||||
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]);
|
||||
}
|
||||
|
||||
if (nextlinkedList.First != null)
|
||||
{
|
||||
nextlinkedList.First.Value.ParentId = new Guid();
|
||||
}
|
||||
}
|
||||
|
||||
return removes;
|
||||
}
|
||||
|
||||
public List<BlockDesignerItemViewModel> RemoveSelf(bool allbreak = false)
|
||||
{
|
||||
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--)
|
||||
{
|
||||
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]);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -149,6 +227,7 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public override void AddToSelection(bool selected, bool clearother)
|
||||
{
|
||||
if (clearother)
|
||||
@@ -180,48 +259,44 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
this.RemoveChild(oldchildren, container);
|
||||
}
|
||||
Root.Items.Remove(child);
|
||||
if (child.Prev != null)
|
||||
if (child.LinkNode?.Previous != null)
|
||||
{
|
||||
child.Prev.RemoveNext();
|
||||
child.LinkNode.Previous.Value.RemoveNext();
|
||||
}
|
||||
if (child.Next != null)
|
||||
if (child.LinkNode?.Next != null)
|
||||
{
|
||||
child.RemoveNext();
|
||||
child.LinkNode.Next.Value.RemovePrevious();
|
||||
}
|
||||
container.InsertChild(child, index);
|
||||
}
|
||||
else
|
||||
{
|
||||
var list = child.GetNexts(true);
|
||||
list.Reverse();
|
||||
list.ForEach(p => {
|
||||
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 => {
|
||||
Root.Items.Remove(p);
|
||||
if (p.Prev != null)
|
||||
{
|
||||
p.Prev.RemoveNext();
|
||||
}
|
||||
if (p.Next != null)
|
||||
{
|
||||
p.RemoveNext();
|
||||
}
|
||||
container.InsertChild(p, index);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
child.RemoveFromSelection();
|
||||
this.GetRootContainItem.AddToSelection(true, true);
|
||||
|
||||
//if (this.GetItemHeight() > 0)
|
||||
//{
|
||||
// AlignNext(this.Next);
|
||||
//}
|
||||
//else
|
||||
{
|
||||
System.Windows.Application.Current?.Dispatcher.BeginInvoke(new Action(async () => {
|
||||
await Task.Delay(10);
|
||||
AlignNext(this.Next);
|
||||
}));
|
||||
}
|
||||
System.Windows.Application.Current?.Dispatcher.BeginInvoke(new Action(async () => {
|
||||
await Task.Delay(10);
|
||||
AlignNext(LinkNode?.Next);
|
||||
}));
|
||||
}
|
||||
|
||||
public virtual void RemoveChild(BlockDesignerItemViewModel child, BlockItemsContainerInfo container)
|
||||
@@ -236,17 +311,10 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
this.RemoveFromSelection();
|
||||
child.AddToSelection(true, true);
|
||||
|
||||
//if (this.GetItemHeight() > 0)
|
||||
//{
|
||||
// AlignNext(this.Next);
|
||||
//}
|
||||
//else
|
||||
{
|
||||
System.Windows.Application.Current?.Dispatcher.BeginInvoke(new Action(async () => {
|
||||
await Task.Delay(10);
|
||||
AlignNext(this.Next);
|
||||
}));
|
||||
}
|
||||
System.Windows.Application.Current?.Dispatcher.BeginInvoke(new Action(async () => {
|
||||
await Task.Delay(10);
|
||||
AlignNext(LinkNode?.Next);
|
||||
}));
|
||||
}
|
||||
|
||||
private bool _isExecuting;
|
||||
@@ -267,41 +335,40 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
get; set;
|
||||
}
|
||||
|
||||
public int BlockLevel
|
||||
public int LinkNodeLevel
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Prev == null)
|
||||
if (LinkNode?.Previous == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return Prev.BlockLevel + 1;
|
||||
return LinkNode.Previous.Value.LinkNodeLevel + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public BlockDesignerItemViewModel Prev
|
||||
private LinkedListNode<BlockDesignerItemViewModel> _linkNode;
|
||||
public LinkedListNode<BlockDesignerItemViewModel> LinkNode
|
||||
{
|
||||
get
|
||||
{
|
||||
return Parent as BlockDesignerItemViewModel;
|
||||
if (_linkNode == null || _linkNode.List == null)
|
||||
{
|
||||
var linklist = new LinkedList<BlockDesignerItemViewModel>(); ;
|
||||
_linkNode = linklist.AddFirst(this);
|
||||
}
|
||||
|
||||
return _linkNode;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (Parent != value)
|
||||
{
|
||||
Parent = value;
|
||||
}
|
||||
_linkNode = value;
|
||||
}
|
||||
}
|
||||
|
||||
public BlockDesignerItemViewModel Next
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
public BlockItemsContainerInfo ParentContainer
|
||||
{
|
||||
get; set;
|
||||
@@ -336,48 +403,6 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
}
|
||||
}
|
||||
|
||||
public BlockDesignerItemViewModel GetFirst()
|
||||
{
|
||||
var parent = this.Prev;
|
||||
while (parent?.Prev != null)
|
||||
{
|
||||
parent = parent.Prev;
|
||||
}
|
||||
|
||||
return parent;
|
||||
}
|
||||
|
||||
public BlockDesignerItemViewModel GetLast()
|
||||
{
|
||||
var next = this;
|
||||
while (next.Next != null)
|
||||
{
|
||||
next = next.Next;
|
||||
}
|
||||
|
||||
return next;
|
||||
}
|
||||
|
||||
public List<BlockDesignerItemViewModel> GetNexts(bool self)
|
||||
{
|
||||
List<BlockDesignerItemViewModel> blockDesignerItemViewModels = new List<BlockDesignerItemViewModel>();
|
||||
if (self)
|
||||
{
|
||||
blockDesignerItemViewModels.Add(this);
|
||||
}
|
||||
var next = this;
|
||||
while (next != null)
|
||||
{
|
||||
next = next.Next;
|
||||
if (next != null)
|
||||
{
|
||||
blockDesignerItemViewModels.Add(next);
|
||||
}
|
||||
}
|
||||
|
||||
return blockDesignerItemViewModels;
|
||||
}
|
||||
|
||||
public void AddContainer(BlockItemsContainerInfo container)
|
||||
{
|
||||
Containers.Add(container);
|
||||
@@ -425,26 +450,26 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
#region 执行
|
||||
protected override async void ExecuteEditCommand(object param)
|
||||
{
|
||||
await (this.GetFirst() ?? this).Execute();
|
||||
await this.LinkNode.List.First.Value.Execute();
|
||||
}
|
||||
|
||||
public override async void PreviewExecuteEdit()
|
||||
public override void PreviewExecuteEdit()
|
||||
{
|
||||
var items = (this.GetFirst() ?? this).GetNexts(true);
|
||||
var items = this.LinkNode.List.ToList();
|
||||
|
||||
items.ForEach(p => p.IsExecuting = true);
|
||||
}
|
||||
|
||||
public override async void ExitPreviewExecuteEdit()
|
||||
public override void ExitPreviewExecuteEdit()
|
||||
{
|
||||
var items = (this.GetFirst() ?? this).GetNexts(true);
|
||||
var items = this.LinkNode.List.ToList();
|
||||
|
||||
items.ForEach(p => p.IsExecuting = false);
|
||||
}
|
||||
|
||||
public async Task Execute()
|
||||
{
|
||||
await BeforeExecute();
|
||||
await BeforeExecute();
|
||||
await Executing();
|
||||
await AfterExecute();
|
||||
}
|
||||
@@ -465,8 +490,8 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
if (IsExecuting)
|
||||
{
|
||||
IsExecuting = false;
|
||||
if (Next != null)
|
||||
await Next.Execute();
|
||||
if (LinkNode.Next != null)
|
||||
await LinkNode.Next.Value.Execute();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -548,14 +573,14 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
#region 帮助
|
||||
public class BlockDesignerItemTempLink
|
||||
{
|
||||
public List<BlockDesignerItemViewModel> Items
|
||||
public LinkedList<BlockDesignerItemViewModel> LinkList
|
||||
{
|
||||
get; set;
|
||||
} = new List<BlockDesignerItemViewModel>();
|
||||
} = new LinkedList<BlockDesignerItemViewModel>();
|
||||
|
||||
public RectangleBase GetBounds()
|
||||
{
|
||||
return Items.FirstOrDefault().GetBounds();
|
||||
return LinkList.FirstOrDefault().GetBounds();
|
||||
}
|
||||
|
||||
public List<FullyCreatedConnectorInfo> Connectors
|
||||
@@ -563,21 +588,21 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
get
|
||||
{
|
||||
List<FullyCreatedConnectorInfo> connectors = new List<FullyCreatedConnectorInfo>();
|
||||
if (Items.FirstOrDefault().TopConnector != null)
|
||||
if (LinkList.FirstOrDefault().TopConnector != null)
|
||||
{
|
||||
connectors.Add(Items.FirstOrDefault().TopConnector);
|
||||
connectors.Add(LinkList.FirstOrDefault().TopConnector);
|
||||
}
|
||||
if (Items.FirstOrDefault().LeftConnector != null)
|
||||
if (LinkList.FirstOrDefault().LeftConnector != null)
|
||||
{
|
||||
connectors.Add(Items.FirstOrDefault().LeftConnector);
|
||||
connectors.Add(LinkList.FirstOrDefault().LeftConnector);
|
||||
}
|
||||
if (Items.LastOrDefault().BottomConnector != null)
|
||||
if (LinkList.LastOrDefault().BottomConnector != null)
|
||||
{
|
||||
connectors.Add(Items.LastOrDefault().BottomConnector);
|
||||
connectors.Add(LinkList.LastOrDefault().BottomConnector);
|
||||
}
|
||||
if (Items.LastOrDefault().RightConnector != null)
|
||||
if (LinkList.LastOrDefault().RightConnector != null)
|
||||
{
|
||||
connectors.Add(Items.LastOrDefault().RightConnector);
|
||||
connectors.Add(LinkList.LastOrDefault().RightConnector);
|
||||
}
|
||||
return connectors;
|
||||
}
|
||||
@@ -586,23 +611,15 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
public static List<BlockDesignerItemTempLink> Build(List<BlockDesignerItemViewModel> blocks)
|
||||
{
|
||||
List<BlockDesignerItemTempLink> links = new List<BlockDesignerItemTempLink>();
|
||||
foreach (var block in blocks.OrderBy(p => p.BlockLevel).ToList())
|
||||
|
||||
foreach (var blockgroup in blocks.GroupBy(p => p.LinkNode.List))
|
||||
{
|
||||
bool success = false;
|
||||
foreach (var link in links)
|
||||
var link = new BlockDesignerItemTempLink();
|
||||
foreach (var block in blockgroup.OrderBy(p => p.LinkNodeLevel))
|
||||
{
|
||||
if (link.Items.LastOrDefault() == block.Prev)
|
||||
{
|
||||
link.Items.Add(block);
|
||||
success = true;
|
||||
}
|
||||
}
|
||||
if (success == false)
|
||||
{
|
||||
BlockDesignerItemTempLink link = new BlockDesignerItemTempLink();
|
||||
link.Items.Add(block);
|
||||
links.Add(link);
|
||||
link.LinkList.AddLast(block);
|
||||
}
|
||||
links.Add(link);
|
||||
}
|
||||
return links;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user