Files
aistudio-wpf-diagram/AIStudio.Wpf.Mind/ViewModels/MindDiagramViewModel.cs
2023-03-08 19:45:07 +08:00

496 lines
15 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using AIStudio.Wpf.DiagramDesigner;
namespace AIStudio.Wpf.Mind.ViewModels
{
public class MindDiagramViewModel : DiagramViewModel, IMindDiagramViewModel
{
#region
private MindType _mindType = Mind.MindType.Mind;
public MindType MindType
{
get
{
return _mindType;
}
set
{
if (SetProperty(ref _mindType, value))
{
ExecutedChangeMindTypeCommand(value);
}
}
}
public MindNode RootItem
{
get
{
return Items.OfType<MindNode>().FirstOrDefault();
}
}
#endregion
#region
private SimpleCommand _addParentCommand;
public SimpleCommand AddParentCommand
{
get
{
return this._addParentCommand ?? (this._addParentCommand = new SimpleCommand(MindLevelEnable, ExecuteAddParentCommand));
}
}
private SimpleCommand _addChildCommand;
public SimpleCommand AddChildCommand
{
get
{
return this._addChildCommand ?? (this._addChildCommand = new SimpleCommand(MindExecuteEnable, this.ExecuteAddChildCommand));
}
}
private SimpleCommand _addPeerCommand;
public SimpleCommand AddPeerCommand
{
get
{
return this._addPeerCommand ?? (this._addPeerCommand = new SimpleCommand(MindLevelEnable, this.ExecuteAddPeerCommand));
}
}
private SimpleCommand _deleteSelfCommand;
public SimpleCommand DeleteSelfCommand
{
get
{
return this._deleteSelfCommand ?? (this._deleteSelfCommand = new SimpleCommand(MindLevelEnable, ExecuteDeleteSelfCommand));
}
}
private SimpleCommand _moveForwardCommand;
public SimpleCommand MoveForwardCommand
{
get
{
return this._moveForwardCommand ?? (this._moveForwardCommand = new SimpleCommand(MindExecuteEnable, ExecuteMoveForwardCommand));
}
}
private SimpleCommand _moveBackCommand;
public SimpleCommand MoveBackCommand
{
get
{
return this._moveBackCommand ?? (this._moveBackCommand = new SimpleCommand(MindExecuteEnable, ExecuteMoveBackCommand));
}
}
private SimpleCommand _changeMindTypeCommand;
public SimpleCommand ChangeMindTypeCommand
{
get
{
return this._changeMindTypeCommand ?? (this._changeMindTypeCommand = new SimpleCommand(MindExecuteEnable, this.ExecutedChangeMindTypeCommand));
}
}
private SimpleCommand _changeMindThemeCommand;
public SimpleCommand ChangeMindThemeCommand
{
get
{
return this._changeMindThemeCommand ?? (this._changeMindThemeCommand = new SimpleCommand(MindExecuteEnable, this.ExecutedChangeMindThemeCommand));
}
}
private SimpleCommand _expand2Level1Command;
public SimpleCommand Expand2Level1Command
{
get
{
return this._expand2Level1Command ?? (this._expand2Level1Command = new SimpleCommand(MindExecuteEnable, this.ExecutedExpand2Level1Command));
}
}
private SimpleCommand _expand2Level2Command;
public SimpleCommand Expand2Level2Command
{
get
{
return this._expand2Level2Command ?? (this._expand2Level2Command = new SimpleCommand(MindExecuteEnable, this.ExecutedExpand2Level2Command));
}
}
private SimpleCommand _expand2Level3Command;
public SimpleCommand Expand2Level3Command
{
get
{
return this._expand2Level3Command ?? (this._expand2Level3Command = new SimpleCommand(MindExecuteEnable, this.ExecutedExpand2Level3Command));
}
}
private SimpleCommand _expand2Level4Command;
public SimpleCommand Expand2Level4Command
{
get
{
return this._expand2Level4Command ?? (this._expand2Level4Command = new SimpleCommand(MindExecuteEnable, this.ExecutedExpand2Level4Command));
}
}
private SimpleCommand _expand2Level5Command;
public SimpleCommand Expand2Level5Command
{
get
{
return this._expand2Level5Command ?? (this._expand2Level5Command = new SimpleCommand(MindExecuteEnable, this.ExecutedExpand2Level5Command));
}
}
private SimpleCommand _expand2Level6Command;
public SimpleCommand Expand2Level6Command
{
get
{
return this._expand2Level6Command ?? (this._expand2Level6Command = new SimpleCommand(MindExecuteEnable, this.ExecutedExpand2Level6Command));
}
}
#endregion
public MindDiagramViewModel()
{
MindNode level1node = new MindNode(this) { Root = this, Id = Guid.NewGuid(), Left = 200, Top = 200, Text = "思维导图" };
level1node.InitLayout(true);
Items.Add(level1node);
level1node.IsSelected = true;
}
public bool MindExecuteEnable(object para)
{
if (ExecuteEnable(para) == false) return false;
if (SelectedItem is MindNode) return true;
return false;
}
private bool MindLevelEnable(object obj)
{
if (MindExecuteEnable(obj) == false) return false;
return (SelectedItem as MindNode).NodeLevel != 0;
}
#region
public void ExecuteAddChildCommand(object parameter)
{
List<MindNode> items = new List<MindNode>();
if (parameter is MindNode parent)
{
}
else if (parameter is IEnumerable<MindNode> para)
{
parent = para.FirstOrDefault();//第一个固定为父节点
items = para.Skip(1).ToList();
}
else
{
parent = SelectedItem as MindNode;
}
if (items?.Count == 0)
{
var node = new MindNode(this) { Text = "分支主题" };
items.Add(node);
}
DoCommandManager.DoNewCommand(this.ToString(),
() => {
foreach (var item in items)
{
parent.AddChild(item);
}
parent.LayoutUpdated();
},
() => {
foreach (var item in items)
{
parent.RemoveChild(item);
}
parent.LayoutUpdated();
});
}
public void ExecuteAddParentCommand(object parameter)
{
List<MindNode> items = new List<MindNode>();
if (parameter is MindNode node)
{
}
else if (parameter is IEnumerable<MindNode> para)
{
node = para.FirstOrDefault();//第一个固定为父节点
items = para.Skip(1).ToList();
}
else
{
node = SelectedItem as MindNode;
}
if (items?.Count == 0)
{
items.Add(new MindNode(this) { Text = "分支主题" });
}
if (node.Parent is MindNode parent)
{
DoCommandManager.DoNewCommand(this.ToString(),
() => {
int index = parent.Children.IndexOf(node);
parent.AddChild(items[0], index + 1);
parent.RemoveChild(node);
items[0].AddChild(node);
parent.LayoutUpdated();
},
() => {
int index = parent.Children.IndexOf(items[0]);
items[0].RemoveChild(node);
parent.AddChild(node, index + 1);
parent.LayoutUpdated();
});
}
}
public void ExecuteAddPeerCommand(object parameter)
{
List<MindNode> items = new List<MindNode>();
if (parameter is MindNode pear)
{
}
else if (parameter is IEnumerable<MindNode> para)
{
pear = para.FirstOrDefault();//第一个固定为同级节点
items = para.Skip(1).ToList();
}
else
{
pear = SelectedItem as MindNode;
}
if (items?.Count == 0)
{
var node = new MindNode(this) { Text = "分支主题" };
items.Add(node);
}
if (pear.Parent is MindNode parent)
{
DoCommandManager.DoNewCommand(this.ToString(),
() => {
int index = parent.Children.IndexOf(pear);
for (int i = 0; i < items.Count; i++)
{
parent.AddChild(items[i], index + i + 1);
}
parent.LayoutUpdated();
},
() => {
for (int i = 0; i < items.Count; i++)
{
parent.RemoveChild(items[i]);
}
parent.LayoutUpdated();
});
}
}
private void ExecuteMoveBackCommand(object parameter)
{
if (parameter is MindNode node)
{
}
else
{
node = SelectedItem as MindNode;
}
if (node.Parent is MindNode parent)
{
DoCommandManager.DoNewCommand(this.ToString(),
() => {
int index = parent.Children.IndexOf(node);
if (index < parent.Children.Count - 1)
{
parent.RemoveChild(node);
parent.AddChild(node, index + 1);
parent.LayoutUpdated();
}
},
() => {
int index = parent.Children.IndexOf(node);
if (index > 0)
{
parent.RemoveChild(node);
parent.AddChild(node, index - 1);
parent.LayoutUpdated();
}
});
}
}
private void ExecuteMoveForwardCommand(object parameter)
{
if (parameter is MindNode node)
{
}
else
{
node = SelectedItem as MindNode;
}
if (node.Parent is MindNode parent)
{
DoCommandManager.DoNewCommand(this.ToString(),
() => {
int index = parent.Children.IndexOf(node);
if (index > 0)
{
parent.RemoveChild(node);
parent.AddChild(node, index - 1);
parent.LayoutUpdated();
}
},
() => {
int index = parent.Children.IndexOf(node);
if (index < parent.Children.Count - 1)
{
parent.RemoveChild(node);
parent.AddChild(node, index + 1);
parent.LayoutUpdated();
}
});
}
}
private void ExecuteDeleteSelfCommand(object parameter)
{
if (parameter is MindNode node)
{
}
else
{
node = SelectedItem as MindNode;
}
if (node.Parent is MindNode parent)
{
int index = parent.Children.IndexOf(node);
DoCommandManager.DoNewCommand(this.ToString(),
() => {
parent.RemoveChild(node, true);
parent.LayoutUpdated();
},
() => {
parent.AddChild(node, index);
parent.LayoutUpdated();
});
}
}
private void ExecutedChangeMindTypeCommand(object obj)
{
if (obj is MindType mindType)
{
Items.OfType<MindNode>().ToList().ForEach(item => { item.InitLayout(true); });
Items.OfType<MindNode>().ToList().ForEach(item => { item.InitConnectLayout(); });
RootItem?.LayoutUpdated();
}
}
private void ExecutedChangeMindThemeCommand(object obj)
{
}
private void ExecutedExpand2Level1Command(object obj)
{
throw new NotImplementedException();
}
private void ExecutedExpand2Level2Command(object obj)
{
throw new NotImplementedException();
}
private void ExecutedExpand2Level3Command(object obj)
{
throw new NotImplementedException();
}
private void ExecutedExpand2Level4Command(object obj)
{
throw new NotImplementedException();
}
private void ExecutedExpand2Level5Command(object obj)
{
throw new NotImplementedException();
}
private void ExecutedExpand2Level6Command(object obj)
{
throw new NotImplementedException();
}
#endregion
protected override void ExecutedInitLayoutCommand(object obj)
{
GetChildren(RootItem);
RootItem?.LayoutUpdated();
}
protected override void ExecutedResetLayoutCommand(object obj)
{
foreach (var item in Items.OfType<MindNode>())
{
item.Offset = new DiagramDesigner.Geometrys.PointBase();
}
RootItem?.LayoutUpdated();
}
private void GetChildren(MindNode parent)
{
if (parent == null)
return;
parent.Children = new System.Collections.ObjectModel.ObservableCollection<MindNode>(Items.OfType<MindNode>().Where(p => p.ParentId == parent.Id));
foreach (var item in Items.OfType<MindNode>().Where(p => p.ParentId == parent.Id))
{
GetChildren(item);
}
}
}
}