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

246 lines
6.5 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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)
{
}
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));
IsReadOnlyText = true;
}
public void AddNext(BlockDesignerItemViewModel next)
{
2023-06-13 19:06:51 +08:00
if (this.Next == next)
{
AlignNext(next);
return;
}
2023-06-17 23:55:54 +08:00
var oldnext = this.Next;
2023-06-13 19:06:51 +08:00
RemoveNext();
next.Left = this.Left;
next.Top = this.Top + this.GetItemHeight();
next.ParentId = this.Id;
next.Parent = this;
this.Next = next;
2023-06-13 19:06:51 +08:00
if (next.Next != null)
{
next.AlignNext(next.Next);
}
2023-06-17 23:55:54 +08:00
if (oldnext != null)
{
System.Windows.Application.Current?.Dispatcher.BeginInvoke(new Action(async () => {
await Task.Delay(10);
GetLast().AddNext(oldnext);
}));
}
2023-06-13 19:06:51 +08:00
}
public void AlignNext(BlockDesignerItemViewModel next)
{
if (next != null && this.Next == next)
{
next.Left = this.Left;
next.Top = this.Top + this.GetItemHeight();
if (next.Next != null)
{
next.AlignNext(next.Next);
}
return;
}
}
public void RemoveNext()
{
var next = this.Next;
if (next != null)
{
next.ParentId = new Guid();
next.Parent = null;
this.Next = 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-17 23:55:54 +08:00
public virtual void AddChild(BlockDesignerItemViewModel child, BlockItemsContainerInfo container)
{
child.RemoveFromSelection();
2023-06-17 09:31:15 +08:00
this.GetRootContainItem.AddToSelection(true, true);
2023-06-13 19:06:51 +08:00
2023-06-13 22:51:34 +08:00
System.Windows.Application.Current?.Dispatcher.BeginInvoke(new Action(async () => {
2023-06-13 22:51:59 +08:00
await Task.Delay(10);
2023-06-13 22:51:34 +08:00
AlignNext(this.Next);
2023-06-16 19:20:44 +08:00
}));
}
2023-06-17 23:55:54 +08:00
public virtual void RemoveChild(BlockDesignerItemViewModel child, BlockItemsContainerInfo container)
{
this.RemoveFromSelection();
2023-06-15 22:44:12 +08:00
child.AddToSelection(true, true);
2023-06-13 19:06:51 +08:00
2023-06-13 22:51:34 +08:00
System.Windows.Application.Current?.Dispatcher.BeginInvoke(new Action(async () => {
2023-06-13 22:51:59 +08:00
await Task.Delay(10);
2023-06-13 22:51:34 +08:00
AlignNext(this.Next);
}));
}
2023-06-17 23:55:54 +08:00
public int BlockLevel
{
get
{
if (Prev == null)
{
return 0;
}
else
{
return Prev.BlockLevel + 1;
}
}
}
2023-06-17 09:31:15 +08:00
public BlockDesignerItemViewModel Prev
{
get
{
return Parent as BlockDesignerItemViewModel;
}
}
public BlockDesignerItemViewModel Next
{
get; set;
}
2023-06-17 23:55:54 +08:00
public bool CanContainTo
{
get; set;
} = true;
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-17 09:31:15 +08:00
public BlockDesignerItemViewModel GetFirst()
{
var parent = this.Next;
if (parent != null)
{
while (parent.Parent != null)
{
parent = parent.Parent as BlockDesignerItemViewModel;
}
}
return parent;
}
public BlockDesignerItemViewModel GetLast()
{
var next = this;
if (next != null)
{
while (next.Next != null)
{
next = next.Next;
}
}
return next;
}
2023-06-15 22:44:12 +08:00
2023-06-16 19:03:15 +08:00
public List<BlockItemsContainerInfo> GetAllContain()
2023-06-15 22:44:12 +08:00
{
2023-06-17 23:55:54 +08:00
return Containers.SelectMany(p => p.GetAllContain(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
}
}
}
}
}