mirror of
https://gitee.com/akwkevin/aistudio.-wpf.-diagram
synced 2026-04-12 12:16:37 +08:00
支持多个node保存到工具栏,显示还有点问题。
This commit is contained in:
@@ -161,6 +161,8 @@ namespace AIStudio.Wpf.DiagramApp.ViewModels
|
||||
{
|
||||
if (e.PropertyName == "IsSelected")
|
||||
{
|
||||
_service.SelectedItems = DiagramViewModel?.SelectedItems;
|
||||
|
||||
_service.SelectedItem = DiagramViewModel?.SelectedItems?.FirstOrDefault();
|
||||
}
|
||||
|
||||
@@ -266,13 +268,13 @@ namespace AIStudio.Wpf.DiagramApp.ViewModels
|
||||
|
||||
connectionItem.SourceType = System.Type.GetType(connectionItem.SourceTypeName);
|
||||
connectionItem.SinkType = System.Type.GetType(connectionItem.SinkTypeName);
|
||||
DesignerItemViewModelBase sourceItem = GetConnectorDataItem(viewModel, connectionItem.SourceId, connectionItem.SourceType);
|
||||
DesignerItemViewModelBase sourceItem = DiagramViewModelHelper.GetConnectorDataItem(viewModel.Items, connectionItem.SourceId, connectionItem.SourceType);
|
||||
ConnectorOrientation sourceConnectorOrientation = connectionItem.SourceOrientation;
|
||||
FullyCreatedConnectorInfo sourceConnectorInfo = GetFullConnectorInfo(connectionItem.Id, sourceItem, sourceConnectorOrientation, connectionItem.SourceXRatio, connectionItem.SourceYRatio, connectionItem.SourceInnerPoint, connectionItem.SourceInnerPoint);
|
||||
FullyCreatedConnectorInfo sourceConnectorInfo = sourceItem.GetFullConnectorInfo(connectionItem.Id, sourceConnectorOrientation, connectionItem.SourceXRatio, connectionItem.SourceYRatio, connectionItem.SourceInnerPoint, connectionItem.SourceInnerPoint);
|
||||
|
||||
DesignerItemViewModelBase sinkItem = GetConnectorDataItem(viewModel, connectionItem.SinkId, connectionItem.SinkType);
|
||||
DesignerItemViewModelBase sinkItem = DiagramViewModelHelper.GetConnectorDataItem(viewModel.Items, connectionItem.SinkId, connectionItem.SinkType);
|
||||
ConnectorOrientation sinkConnectorOrientation = connectionItem.SinkOrientation;
|
||||
FullyCreatedConnectorInfo sinkConnectorInfo = GetFullConnectorInfo(connectionItem.Id, sinkItem, sinkConnectorOrientation, connectionItem.SinkXRatio, connectionItem.SinkYRatio, connectionItem.SinkInnerPoint, connectionItem.SinkInnerPoint);
|
||||
FullyCreatedConnectorInfo sinkConnectorInfo = sinkItem.GetFullConnectorInfo(connectionItem.Id, sinkConnectorOrientation, connectionItem.SinkXRatio, connectionItem.SinkYRatio, connectionItem.SinkInnerPoint, connectionItem.SinkInnerPoint);
|
||||
|
||||
ConnectionViewModel connectionVM = new ConnectionViewModel(viewModel, sourceConnectorInfo, sinkConnectorInfo, connectionItem);
|
||||
viewModel.Items.Add(connectionVM);
|
||||
@@ -348,61 +350,6 @@ namespace AIStudio.Wpf.DiagramApp.ViewModels
|
||||
return true;
|
||||
}
|
||||
|
||||
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,bool isPortless)
|
||||
{
|
||||
if (isInnerPoint)
|
||||
{
|
||||
return dataItem.Connectors.Where(p => p.XRatio == xRatio && p.YRatio == yRatio).FirstOrDefault();
|
||||
}
|
||||
else if(isPortless)
|
||||
{
|
||||
return dataItem.PortlessConnector;
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (connectorOrientation)
|
||||
{
|
||||
case ConnectorOrientation.Left:
|
||||
return dataItem.LeftConnector;
|
||||
case ConnectorOrientation.TopLeft:
|
||||
return dataItem.TopLeftConnector;
|
||||
case ConnectorOrientation.Top:
|
||||
return dataItem.TopConnector;
|
||||
case ConnectorOrientation.TopRight:
|
||||
return dataItem.TopRightConnector;
|
||||
case ConnectorOrientation.Right:
|
||||
return dataItem.RightConnector;
|
||||
case ConnectorOrientation.BottomRight:
|
||||
return dataItem.BottomRightConnector;
|
||||
case ConnectorOrientation.Bottom:
|
||||
return dataItem.BottomConnector;
|
||||
case ConnectorOrientation.BottomLeft:
|
||||
return dataItem.BottomLeftConnector;
|
||||
default:
|
||||
throw new InvalidOperationException(
|
||||
string.Format("Found invalid persisted Connector Orientation for Connector Id: {0}", connectorId));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private ConnectionViewModel GetSourceItem(FullyCreatedConnectorInfo sinkConnector)
|
||||
{
|
||||
foreach (var connector in DiagramViewModel.Items.OfType<ConnectionViewModel>())
|
||||
{
|
||||
if (connector.SinkConnectorInfo == sinkConnector)
|
||||
{
|
||||
return connector;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private bool ItemsToDeleteHasConnector(List<SelectableDesignerItemViewModelBase> itemsToRemove, ConnectorInfoBase connector)
|
||||
{
|
||||
if (connector is FullyCreatedConnectorInfo fully)
|
||||
@@ -548,13 +495,13 @@ namespace AIStudio.Wpf.DiagramApp.ViewModels
|
||||
|
||||
connectionItem.SourceType = System.Type.GetType(connectionItem.SourceTypeName);
|
||||
connectionItem.SinkType = System.Type.GetType(connectionItem.SinkTypeName);
|
||||
DesignerItemViewModelBase sourceItem = GetConnectorDataItem(viewModel, connectionItem.SourceId, connectionItem.SourceType);
|
||||
DesignerItemViewModelBase sourceItem = DiagramViewModelHelper.GetConnectorDataItem(viewModel.Items, connectionItem.SourceId, connectionItem.SourceType);
|
||||
ConnectorOrientation sourceConnectorOrientation = connectionItem.SourceOrientation;
|
||||
FullyCreatedConnectorInfo sourceConnectorInfo = GetFullConnectorInfo(connectionItem.Id, sourceItem, sourceConnectorOrientation, connectionItem.SourceXRatio, connectionItem.SourceYRatio, connectionItem.SourceInnerPoint, connectionItem.SourceIsPortless);
|
||||
FullyCreatedConnectorInfo sourceConnectorInfo = sourceItem.GetFullConnectorInfo(connectionItem.Id,sourceConnectorOrientation, connectionItem.SourceXRatio, connectionItem.SourceYRatio, connectionItem.SourceInnerPoint, connectionItem.SourceIsPortless);
|
||||
|
||||
DesignerItemViewModelBase sinkItem = GetConnectorDataItem(viewModel, connectionItem.SinkId, connectionItem.SinkType);
|
||||
DesignerItemViewModelBase sinkItem = DiagramViewModelHelper.GetConnectorDataItem(viewModel.Items, connectionItem.SinkId, connectionItem.SinkType);
|
||||
ConnectorOrientation sinkConnectorOrientation = connectionItem.SinkOrientation;
|
||||
FullyCreatedConnectorInfo sinkConnectorInfo = GetFullConnectorInfo(connectionItem.Id, sinkItem, sinkConnectorOrientation, connectionItem.SinkXRatio, connectionItem.SinkYRatio, connectionItem.SinkInnerPoint, connectionItem.SinkIsPortless);
|
||||
FullyCreatedConnectorInfo sinkConnectorInfo = sinkItem.GetFullConnectorInfo(connectionItem.Id, sinkConnectorOrientation, connectionItem.SinkXRatio, connectionItem.SinkYRatio, connectionItem.SinkInnerPoint, connectionItem.SinkIsPortless);
|
||||
|
||||
ConnectionViewModel connectionVM = new ConnectionViewModel(viewModel, sourceConnectorInfo, sinkConnectorInfo, connectionItem);
|
||||
viewModel.Items.Add(connectionVM);
|
||||
|
||||
@@ -249,31 +249,41 @@ namespace AIStudio.Wpf.DiagramApp.ViewModels
|
||||
|
||||
private void LoadMyItems()
|
||||
{
|
||||
if (!Directory.Exists(_custom))
|
||||
{
|
||||
Directory.CreateDirectory(_custom);
|
||||
}
|
||||
|
||||
if (Directory.Exists(_custom))
|
||||
{
|
||||
var files = Directory.GetFiles(_custom);
|
||||
foreach (var filename in files.Where(p => p.ToLower().EndsWith(".json")))
|
||||
{
|
||||
var itemBase = ReadMyItem(filename);
|
||||
if (itemBase != null)
|
||||
{
|
||||
MyToolBoxCategory.ToolBoxItems.Add(new DesignerItemToolBoxData(itemBase, filename, TypeHelper.GetType(itemBase.ItemTypeName)));
|
||||
}
|
||||
AddMyItem(filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private DesignerItemBase ReadMyItem(string filename)
|
||||
private object ReadMyItem(string filename)
|
||||
{
|
||||
try
|
||||
{
|
||||
var xmlobject = JsonConvert.DeserializeObject<DiagramDocument>(File.ReadAllText(filename));
|
||||
|
||||
Type type = TypeHelper.GetType(xmlobject.DiagramItems[0].DesignerItems[0].SerializableTypeName);
|
||||
if (xmlobject.DiagramItems[0].DesignerItems.Count > 1)
|
||||
{
|
||||
var itemBase = new MultipleDesignerItemToolBoxData(xmlobject.DiagramItems[0], filename);
|
||||
|
||||
var itemBase = JsonConvert.DeserializeObject(xmlobject.DiagramItems[0].DesignerItems[0].SerializableString, type) as DesignerItemBase;
|
||||
return itemBase;
|
||||
}
|
||||
else
|
||||
{
|
||||
Type type = TypeHelper.GetType(xmlobject.DiagramItems[0].DesignerItems[0].SerializableTypeName);
|
||||
|
||||
return itemBase;
|
||||
var itemBase = JsonConvert.DeserializeObject(xmlobject.DiagramItems[0].DesignerItems[0].SerializableString, type) as DesignerItemBase;
|
||||
|
||||
return itemBase;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
@@ -285,22 +295,44 @@ namespace AIStudio.Wpf.DiagramApp.ViewModels
|
||||
|
||||
private void AddMyItem()
|
||||
{
|
||||
if (_service.SelectedItem is DesignerItemViewModelBase designer)
|
||||
if (_service.SelectedItems != null)
|
||||
{
|
||||
DiagramDocument diagramDocument = new DiagramDocument();
|
||||
diagramDocument.DiagramItems = new List<DiagramItem>();
|
||||
DiagramItem diagramItem = new DiagramItem();
|
||||
diagramItem.DesignerItems = new List<DiagramDesigner.Models.SerializableItem> { designer.ToSerializableItem(".json") };
|
||||
|
||||
var selectedDesignerItems = _service.SelectedItems.OfType<DesignerItemViewModelBase>();
|
||||
var selectedConnections = _service.SelectedItems.OfType<ConnectionViewModel>();
|
||||
|
||||
diagramItem.DesignerItems = selectedDesignerItems.Select(p => p.ToSerializableItem(".json")).Where(p => p != null).ToList();
|
||||
diagramItem.Connections = selectedConnections.Select(p => p.ToSerializableItem(".json")).Where(p => p != null).ToList();
|
||||
|
||||
diagramDocument.DiagramItems.Add(diagramItem);
|
||||
string newname = NewNameHelper.GetNewName(MyToolBoxCategory.ToolBoxItems.OfType<DesignerItemToolBoxData>().Select(p => Path.GetFileNameWithoutExtension(p.FileName)), "");
|
||||
var filename = $"{_custom}\\{newname}.json";
|
||||
File.WriteAllText(filename, JsonConvert.SerializeObject(diagramDocument));
|
||||
|
||||
AddMyItem(filename);
|
||||
}
|
||||
}
|
||||
|
||||
private void AddMyItem(string filename)
|
||||
{
|
||||
try
|
||||
{
|
||||
var itemBase = ReadMyItem(filename);
|
||||
if (itemBase != null)
|
||||
if (itemBase is DesignerItemBase designer)
|
||||
{
|
||||
MyToolBoxCategory.ToolBoxItems.Add(new DesignerItemToolBoxData(itemBase, filename, TypeHelper.GetType(itemBase.ItemTypeName)));
|
||||
MyToolBoxCategory.ToolBoxItems.Add(new DesignerItemToolBoxData(designer, filename, TypeHelper.GetType(designer.ItemTypeName)));
|
||||
}
|
||||
else if (itemBase is MultipleDesignerItemToolBoxData multiple)
|
||||
{
|
||||
MyToolBoxCategory.ToolBoxItems.Add(multiple);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user