调整了一下各种命令的位置,便于其他应用调用

This commit is contained in:
艾竹
2022-12-08 20:54:45 +08:00
parent 9a8d4c95f0
commit 9f91fbcdd3
41 changed files with 1363 additions and 769 deletions

View File

@@ -8,6 +8,9 @@ using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using AIStudio.Wpf.DiagramDesigner.Helpers;
using AIStudio.Wpf.DiagramDesigner.Models;
using Newtonsoft.Json;
namespace AIStudio.Wpf.DiagramDesigner
{
@@ -353,36 +356,61 @@ namespace AIStudio.Wpf.DiagramDesigner
/// 用于wpf大小与物理像素之间转换
/// </summary>
public double ScreenScale { get; set; } = 1;
private double OffsetX = 10;
private double OffsetY = 10;
#endregion
private DoCommandManager DoCommandManager = new DoCommandManager();
public DiagramViewModel()
{
CreateNewDiagramCommand = new SimpleCommand(ExecuteCreateNewDiagramCommand);
AddItemCommand = new SimpleCommand(ExecuteAddItemCommand);
DirectAddItemCommand = new SimpleCommand(ExecuteDirectAddItemCommand);
RemoveItemCommand = new SimpleCommand(ExecuteRemoveItemCommand);
DirectRemoveItemCommand = new SimpleCommand(ExecuteDirectRemoveItemCommand);
ClearSelectedItemsCommand = new SimpleCommand(ExecuteClearSelectedItemsCommand);
CreateNewDiagramCommand = new SimpleCommand(ExecuteEnable, ExecuteCreateNewDiagramCommand);
AddItemCommand = new SimpleCommand(ExecuteEnable, ExecuteAddItemCommand);
DirectAddItemCommand = new SimpleCommand(ExecuteEnable, ExecuteDirectAddItemCommand);
RemoveItemCommand = new SimpleCommand(ExecuteEnable, ExecuteRemoveItemCommand);
DirectRemoveItemCommand = new SimpleCommand(ExecuteEnable, ExecuteDirectRemoveItemCommand);
ClearSelectedItemsCommand = new SimpleCommand(ExecuteEnable, ExecuteClearSelectedItemsCommand);
AlignTopCommand = new SimpleCommand(ExecuteAlignTopCommand);
AlignVerticalCentersCommand = new SimpleCommand(ExecuteAlignVerticalCentersCommand);
AlignBottomCommand = new SimpleCommand(ExecuteAlignBottomCommand);
AlignLeftCommand = new SimpleCommand(ExecuteAlignLeftCommand);
AlignHorizontalCentersCommand = new SimpleCommand(ExecuteAlignHorizontalCentersCommand);
AlignRightCommand = new SimpleCommand(ExecuteAlignRightCommand);
BringForwardCommand = new SimpleCommand(ExecuteBringForwardCommand);
BringToFrontCommand = new SimpleCommand(ExecuteBringToFrontCommand);
SendBackwardCommand = new SimpleCommand(ExecuteSendBackwardCommand);
SendToBackCommand = new SimpleCommand(ExecuteSendToBackCommand);
DistributeHorizontalCommand = new SimpleCommand(ExecuteDistributeHorizontalCommand);
DistributeVerticalCommand = new SimpleCommand(ExecuteDistributeVerticalCommand);
SelectAllCommand = new SimpleCommand(ExecuteSelectAllCommand);
AlignTopCommand = new SimpleCommand(ExecuteEnable, ExecuteAlignTopCommand);
AlignVerticalCentersCommand = new SimpleCommand(ExecuteEnable, ExecuteAlignVerticalCentersCommand);
AlignBottomCommand = new SimpleCommand(ExecuteEnable, ExecuteAlignBottomCommand);
AlignLeftCommand = new SimpleCommand(ExecuteEnable, ExecuteAlignLeftCommand);
AlignHorizontalCentersCommand = new SimpleCommand(ExecuteEnable, ExecuteAlignHorizontalCentersCommand);
AlignRightCommand = new SimpleCommand(ExecuteEnable, ExecuteAlignRightCommand);
BringForwardCommand = new SimpleCommand(ExecuteEnable, ExecuteBringForwardCommand);
BringToFrontCommand = new SimpleCommand(ExecuteEnable, ExecuteBringToFrontCommand);
SendBackwardCommand = new SimpleCommand(ExecuteEnable, ExecuteSendBackwardCommand);
SendToBackCommand = new SimpleCommand(ExecuteEnable, ExecuteSendToBackCommand);
DistributeHorizontalCommand = new SimpleCommand(ExecuteEnable, ExecuteDistributeHorizontalCommand);
DistributeVerticalCommand = new SimpleCommand(ExecuteEnable, ExecuteDistributeVerticalCommand);
SelectAllCommand = new SimpleCommand(ExecuteEnable, ExecuteSelectAllCommand);
CopyCommand = new SimpleCommand(ExecuteEnable, ExecuteCopyCommand);
PasteCommand = new SimpleCommand(ExecuteEnable, ExecutePasteCommand);
CutCommand = new SimpleCommand(ExecuteEnable, ExecuteCutCommand);
DeleteCommand = new SimpleCommand(ExecuteEnable, ExecuteDeleteCommand);
LeftMoveCommand = new SimpleCommand(ExecuteEnable, ExecuteLeftMoveCommand);
RightMoveCommand = new SimpleCommand(ExecuteEnable, ExecuteRightMoveCommand);
UpMoveCommand = new SimpleCommand(ExecuteEnable, ExecuteUpMoveCommand);
DownMoveCommand = new SimpleCommand(ExecuteEnable, ExecuteDownMoveCommand);
CenterMoveCommand = new SimpleCommand(ExecuteEnable, ExecuteCenterMoveCommand);
SameSizeCommand = new SimpleCommand(ExecuteEnable, ExecuteSameSizeCommand);
SameWidthCommand = new SimpleCommand(ExecuteEnable, ExecuteSameWidthCommand);
SameHeightCommand = new SimpleCommand(ExecuteEnable, ExecuteSameHeightCommand);
SameAngleCommand = new SimpleCommand(ExecuteEnable, ExecuteSameAngleCommand);
GroupCommand = new SimpleCommand(ExecuteEnable, ExecuteGroupCommand);
UngroupCommand = new SimpleCommand(ExecuteEnable, ExecuteUngroupCommand);
LockCommand = new SimpleCommand(ExecuteEnable, ExecuteLockCommand);
UnlockCommand = new SimpleCommand(ExecuteEnable, ExecuteUnlockCommand);
Mediator.Instance.Register(this);
Items.CollectionChanged += Items_CollectionChanged;
}
public bool ExecuteEnable(object para)
{
return IsReadOnly == false;
}
#region UnDo ReDo
private void Do(object sender, string propertyName, object newvalue)
@@ -550,6 +578,83 @@ namespace AIStudio.Wpf.DiagramDesigner
get; private set;
}
public SimpleCommand CopyCommand
{
get; private set;
}
public SimpleCommand PasteCommand
{
get; private set;
}
public SimpleCommand CutCommand
{
get; private set;
}
public SimpleCommand DeleteCommand
{
get; private set;
}
public SimpleCommand LeftMoveCommand
{
get; private set;
}
public SimpleCommand RightMoveCommand
{
get; private set;
}
public SimpleCommand UpMoveCommand
{
get; private set;
}
public SimpleCommand DownMoveCommand
{
get; private set;
}
public SimpleCommand CenterMoveCommand
{
get; private set;
}
public SimpleCommand SameSizeCommand
{
get; private set;
}
public SimpleCommand SameWidthCommand
{
get; private set;
}
public SimpleCommand SameHeightCommand
{
get; private set;
}
public SimpleCommand SameAngleCommand
{
get; private set;
}
public SimpleCommand GroupCommand
{
get; private set;
}
public SimpleCommand UngroupCommand
{
get; private set;
}
public SimpleCommand LockCommand
{
get; private set;
}
public SimpleCommand UnlockCommand
{
get; private set;
}
private SimpleCommand _undoCommand;
public SimpleCommand UndoCommand
{
@@ -597,6 +702,7 @@ namespace AIStudio.Wpf.DiagramDesigner
get; set;
}
public bool AddVerify(SelectableDesignerItemViewModelBase item)
{
if (item.InitData() == false)
@@ -638,7 +744,10 @@ namespace AIStudio.Wpf.DiagramDesigner
{
item.Parent = this;
item.ZIndex = Items.Count;
item.Id = Guid.NewGuid();
if (item.Id == Guid.Empty)
{
item.Id = Guid.NewGuid();
}
//item.LineColor = this.LineColor;
//item.FillColor = this.FillColor;
var logical = item as LogicalGateItemViewModelBase;
@@ -1267,6 +1376,363 @@ namespace AIStudio.Wpf.DiagramDesigner
}
}
private void ExecuteCopyCommand(object parameter)
{
List<DesignerItemViewModelBase> selectedDesignerItems =
SelectedItems.OfType<DesignerItemViewModelBase>().ToList();
List<ConnectorViewModel> selectedConnections =
SelectedItems.OfType<ConnectorViewModel>().ToList();
foreach (ConnectorViewModel connection in Items.OfType<ConnectorViewModel>())
{
if (!selectedConnections.Contains(connection))
{
DesignerItemViewModelBase sourceItem = (from item in selectedDesignerItems
where item.Id == connection.SourceConnectorInfo.DataItem.Id
select item).FirstOrDefault();
DesignerItemViewModelBase sinkItem = (from item in selectedDesignerItems
where item.Id == ((connection.SinkConnectorInfo as FullyCreatedConnectorInfo).DataItem).Id
select item).FirstOrDefault();
if (sourceItem != null &&
sinkItem != null &&
BelongToSameGroup(sourceItem, sinkItem))
{
selectedConnections.Add(connection);
}
}
}
//连线上的文本
foreach (var selectedConnection in selectedConnections)
{
if (selectedConnection.OutTextItem != null)
{
selectedDesignerItems.Add(selectedConnection.OutTextItem);
}
}
string json = new SerializableObject
{
DesignerItems = selectedDesignerItems.Select(p => p.ToSerializabObject()).Where(p => p != null).ToList(),
Connections = selectedConnections.Select(p => p.ToSerializabObject()).Where(p => p != null).ToList(),
}.ToJson();
OffsetX = 10;
OffsetY = 10;
Clipboard.Clear();
Clipboard.SetData(DataFormats.Serializable, json);
}
private void ExecutePasteCommand(object parameter)
{
if (Clipboard.ContainsData(DataFormats.Serializable))
{
String clipboardData = Clipboard.GetData(DataFormats.Serializable) as String;
if (String.IsNullOrEmpty(clipboardData))
return;
try
{
List<SelectableDesignerItemViewModelBase> items = new List<SelectableDesignerItemViewModelBase>();
SerializableObject copyitem = JsonConvert.DeserializeObject<SerializableObject>(clipboardData);
Dictionary<Guid, Guid> mappingOldToNewIDs = new Dictionary<Guid, Guid>();
foreach (var diagramItemData in copyitem.DesignerItems)
{
DesignerItemViewModelBase newItem = null;
Type type = TypeHelper.GetType(diagramItemData.TypeName);
DesignerItemViewModelBase itemBase = (DesignerItemViewModelBase)Activator.CreateInstance(type, this, diagramItemData.ObjectJson);
Guid newID = Guid.NewGuid();
mappingOldToNewIDs.Add(itemBase.Id, newID);
itemBase.Id = newID;
itemBase.Left += OffsetX;
itemBase.Top += OffsetY;
newItem = itemBase;
if (newItem != null)
{
items.Add(newItem);
}
}
DirectAddItemCommand.Execute(items);
OffsetX += 10;
OffsetY += 10;
List<SelectableDesignerItemViewModelBase> connectors = new List<SelectableDesignerItemViewModelBase>();
foreach (var connection in copyitem.Connections)
{
var connectionItem = JsonConvert.DeserializeObject<ConnectionItem>(connection.ObjectJson);
Guid newID = Guid.NewGuid();
mappingOldToNewIDs.Add(connectionItem.Id, newID);
connectionItem.SourceId = mappingOldToNewIDs[connectionItem.SourceId];
connectionItem.SinkId = mappingOldToNewIDs[connectionItem.SinkId];
connectionItem.SourceType = System.Type.GetType(connectionItem.SourceTypeName);
connectionItem.SinkType = System.Type.GetType(connectionItem.SinkTypeName);
DesignerItemViewModelBase sourceItem = GetConnectorDataItem(this, connectionItem.SourceId, connectionItem.SourceType);
ConnectorOrientation sourceConnectorOrientation = connectionItem.SourceOrientation;
FullyCreatedConnectorInfo sourceConnectorInfo = GetFullConnectorInfo(connectionItem.Id, sourceItem, sourceConnectorOrientation, connectionItem.SourceXRatio, connectionItem.SourceYRatio, connectionItem.SourceInnerPoint);
DesignerItemViewModelBase sinkItem = GetConnectorDataItem(this, connectionItem.SinkId, connectionItem.SinkType);
ConnectorOrientation sinkConnectorOrientation = connectionItem.SinkOrientation;
FullyCreatedConnectorInfo sinkConnectorInfo = GetFullConnectorInfo(connectionItem.Id, sinkItem, sinkConnectorOrientation, connectionItem.SinkXRatio, connectionItem.SinkYRatio, connectionItem.SinkInnerPoint);
ConnectorViewModel connectionVM = new ConnectorViewModel(this, sourceConnectorInfo, sinkConnectorInfo, connectionItem, connectionItem.VectorLineDrawMode);
connectors.Add(connectionVM);
DesignerItemViewModelBase textItem = items.OfType<DesignerItemViewModelBase>().FirstOrDefault(x => x.ParentId == connectionItem.Id);
if (textItem != null)
{
connectionVM.OutTextItem = textItem;
}
}
DirectAddItemCommand.Execute(connectors);
//修复父级的引用
foreach (var item in items)
{
if (item.ParentId != Guid.Empty)
{
if (mappingOldToNewIDs.ContainsKey(item.ParentId))
item.ParentId = mappingOldToNewIDs[item.ParentId];
}
}
}
catch (Exception e)
{
MessageBox.Show(e.StackTrace, e.Message, MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
private DesignerItemViewModelBase GetConnectorDataItem(IDiagramViewModel diagramViewModel, Guid conectorDataItemId, Type connectorDataItemType)
{
DesignerItemViewModelBase dataItem = diagramViewModel.Items.OfType<DesignerItemViewModelBase>().Single(x => x.Id == conectorDataItemId);
return dataItem;
}
private FullyCreatedConnectorInfo GetFullConnectorInfo(Guid connectorId, DesignerItemViewModelBase dataItem, ConnectorOrientation connectorOrientation, double xRatio, double yRatio, bool isInnerPoint)
{
if (isInnerPoint)
{
return dataItem.Connectors.Where(p => p.XRatio == xRatio && p.YRatio == yRatio).FirstOrDefault();
}
else
{
switch (connectorOrientation)
{
case ConnectorOrientation.Top:
return dataItem.TopConnector;
case ConnectorOrientation.Left:
return dataItem.LeftConnector;
case ConnectorOrientation.Right:
return dataItem.RightConnector;
case ConnectorOrientation.Bottom:
return dataItem.BottomConnector;
default:
throw new InvalidOperationException(
string.Format("Found invalid persisted Connector Orientation for Connector Id: {0}", connectorId));
}
}
}
private bool ItemsToDeleteHasConnector(List<SelectableDesignerItemViewModelBase> itemsToRemove, ConnectorInfoBase connector)
{
if (connector is FullyCreatedConnectorInfo fully)
{
return itemsToRemove.Contains(fully.DataItem);
}
return false;
}
private void ExecuteCutCommand(object parameter)
{
ExecutePasteCommand(null);
OffsetX = 0;
OffsetY = 0;
ExecuteDeleteCommand(null);
}
private void ExecuteDeleteCommand(object parameter)
{
List<SelectableDesignerItemViewModelBase> itemsToRemove = SelectedItems.OfType<SelectableDesignerItemViewModelBase>().ToList();
List<SelectableDesignerItemViewModelBase> connectionsToAlsoRemove = new List<SelectableDesignerItemViewModelBase>();
foreach (var connector in Items.OfType<ConnectorViewModel>())
{
if (ItemsToDeleteHasConnector(itemsToRemove, connector.SourceConnectorInfo))
{
connectionsToAlsoRemove.Add(connector);
}
if (ItemsToDeleteHasConnector(itemsToRemove, connector.SinkConnectorInfo))
{
connectionsToAlsoRemove.Add(connector);
}
}
itemsToRemove.AddRange(connectionsToAlsoRemove);
RemoveItemCommand.Execute(itemsToRemove);
}
private void ExecuteLeftMoveCommand(object parameter)
{
foreach (var item in SelectedItems.OfType<DesignerItemViewModelBase>())
{
item.Left -= 0.5;
}
}
private void ExecuteRightMoveCommand(object parameter)
{
foreach (var item in SelectedItems.OfType<DesignerItemViewModelBase>())
{
item.Left += 0.5;
}
}
private void ExecuteUpMoveCommand(object parameter)
{
foreach (var item in SelectedItems.OfType<DesignerItemViewModelBase>())
{
item.Top -= 0.5;
}
}
private void ExecuteDownMoveCommand(object parameter)
{
foreach (var item in SelectedItems.OfType<DesignerItemViewModelBase>())
{
item.Top += 0.5;
}
}
private void ExecuteCenterMoveCommand(object parameter)
{
foreach (var item in SelectedItems.OfType<DesignerItemViewModelBase>())
{
item.Left = (PageSize.Width - item.ItemWidth) / 2;
item.Top = (PageSize.Height - item.ItemHeight) / 2;
}
}
private void ExecuteSameSizeCommand(object parameter)
{
if (parameter is DesignerItemViewModelBase designerItem)
{
foreach (var item in SelectedItems.OfType<DesignerItemViewModelBase>())
{
item.ItemWidth = designerItem.ItemWidth;
item.ItemHeight = designerItem.ItemHeight;
}
}
}
private void ExecuteSameWidthCommand(object parameter)
{
if (parameter is DesignerItemViewModelBase designerItem)
{
foreach (var item in SelectedItems.OfType<DesignerItemViewModelBase>())
{
item.ItemWidth = designerItem.ItemWidth;
}
}
}
private void ExecuteSameHeightCommand(object parameter)
{
if (parameter is DesignerItemViewModelBase designerItem)
{
foreach (var item in SelectedItems.OfType<DesignerItemViewModelBase>())
{
item.ItemHeight = designerItem.ItemHeight;
}
}
}
private void ExecuteSameAngleCommand(object parameter)
{
if (parameter is DesignerItemViewModelBase designerItem)
{
foreach (var item in SelectedItems.OfType<DesignerItemViewModelBase>())
{
item.Angle = designerItem.Angle;
}
}
}
private void ExecuteGroupCommand(object parameter)
{
var items = from item in SelectedItems.OfType<DesignerItemViewModelBase>()
where item.ParentId == Guid.Empty
select item;
Rect rect = GetBoundingRectangle(items);
GroupDesignerItemViewModel groupItem = new GroupDesignerItemViewModel();
groupItem.IsGroup = true;
groupItem.ItemWidth = rect.Width;
groupItem.ItemHeight = rect.Height;
groupItem.Left = rect.Left;
groupItem.Top = rect.Top;
groupItem.ZIndex = Items.Count;
DirectAddItemCommand.Execute(groupItem);
foreach (DesignerItemViewModelBase item in items)
item.ParentId = groupItem.Id;
ClearSelectedItemsCommand.Execute(null);
//groupItem.IsSelected = true;
SelectionService.AddToSelection(groupItem);
}
private void ExecuteUngroupCommand(object parameter)
{
var groups = (from item in SelectedItems.OfType<DesignerItemViewModelBase>()
where item.IsGroup && item.ParentId == Guid.Empty
select item).ToArray();
foreach (DesignerItemViewModelBase groupRoot in groups)
{
var children = from child in SelectedItems.OfType<DesignerItemViewModelBase>()
where child.ParentId == groupRoot.Id
select child;
foreach (DesignerItemViewModelBase child in children)
child.ParentId = Guid.Empty;
RemoveItemCommand.Execute(groupRoot);
UpdateZIndex();
}
}
private void ExecuteLockCommand(object parameter)
{
}
private void ExecuteUnlockCommand(object parameter)
{
}
public bool BelongToSameGroup(IGroupable item1, IGroupable item2)
{
IGroupable root1 = SelectionService.GetGroupRoot(item1);