mirror of
https://gitee.com/akwkevin/aistudio.-wpf.-diagram
synced 2026-04-12 04:06:36 +08:00
支持多个node保存到工具栏,显示还有点问题。
This commit is contained in:
@@ -5,6 +5,10 @@ using System.Windows.Media;
|
||||
using AIStudio.Wpf.DiagramDesigner;
|
||||
using AIStudio.Wpf.DiagramDesigner.Helpers;
|
||||
using System.Windows;
|
||||
using System.Collections.Generic;
|
||||
using AIStudio.Wpf.DiagramDesigner.Models;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
|
||||
namespace AIStudio.Wpf.DiagramApp.Models
|
||||
{
|
||||
@@ -47,12 +51,73 @@ namespace AIStudio.Wpf.DiagramApp.Models
|
||||
{
|
||||
public string FileName { get; set; }
|
||||
public DesignerItemViewModelBase DesignerItemViewModel { get; set; }
|
||||
public DesignerItemToolBoxData(DesignerItemBase designerItemBase, string filename, Type type, double width = 32, double height = 32, Size? desiredSize = null, string description = null) : base(null, null, type, width, height, desiredSize, description)
|
||||
public DesignerItemToolBoxData(DesignerItemBase designerItemBase, string filename, Type type, double width = 32, double height = 32, Size? desiredSize = null, string description = null) : base(null, null, type, width, height, desiredSize, description)
|
||||
{
|
||||
Addition = designerItemBase;
|
||||
DesignerItemViewModel = Activator.CreateInstance(type, null, designerItemBase) as DesignerItemViewModelBase;
|
||||
FileName = filename;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public class MultipleDesignerItemToolBoxData : ToolBoxData
|
||||
{
|
||||
public string FileName
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
public List<SelectableDesignerItemViewModelBase> SelectableDesignerItemViewModel
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
public MultipleDesignerItemToolBoxData(DiagramItem designerItemBase, string filename, double width = 32, double height = 32, Size? desiredSize = null, string description = null) : base(null, null, typeof(List<SelectableDesignerItemViewModelBase>), width, height, desiredSize, description)
|
||||
{
|
||||
List<DesignerItemViewModelBase> items = new List<DesignerItemViewModelBase>();
|
||||
foreach (var diagramItemData in designerItemBase.DesignerItems)
|
||||
{
|
||||
Type itemtype = TypeHelper.GetType(diagramItemData.ModelTypeName);
|
||||
DesignerItemViewModelBase itemBase = Activator.CreateInstance(itemtype, null, diagramItemData, ".json") as DesignerItemViewModelBase;
|
||||
items.Add(itemBase);
|
||||
}
|
||||
List<ConnectionViewModel> connects = new List<ConnectionViewModel>();
|
||||
foreach (var connection in designerItemBase.Connections)
|
||||
{
|
||||
Type itemtype = TypeHelper.GetType(connection.SerializableTypeName);
|
||||
var connectionItem = SerializeHelper.DeserializeObject(itemtype, connection.SerializableString, ".json") as ConnectionItem;
|
||||
|
||||
connectionItem.SourceType = System.Type.GetType(connectionItem.SourceTypeName);
|
||||
connectionItem.SinkType = System.Type.GetType(connectionItem.SinkTypeName);
|
||||
DesignerItemViewModelBase sourceItem = DiagramViewModelHelper.GetConnectorDataItem(items, connectionItem.SourceId, connectionItem.SourceType);
|
||||
if (sourceItem == null)
|
||||
continue;
|
||||
ConnectorOrientation sourceConnectorOrientation = connectionItem.SourceOrientation;
|
||||
FullyCreatedConnectorInfo sourceConnectorInfo = sourceItem.GetFullConnectorInfo(connectionItem.Id, sourceConnectorOrientation, connectionItem.SourceXRatio, connectionItem.SourceYRatio, connectionItem.SourceInnerPoint, connectionItem.SourceInnerPoint);
|
||||
|
||||
DesignerItemViewModelBase sinkItem = DiagramViewModelHelper.GetConnectorDataItem(items, connectionItem.SinkId, connectionItem.SinkType);
|
||||
if (sinkItem == null)
|
||||
continue;
|
||||
ConnectorOrientation sinkConnectorOrientation = connectionItem.SinkOrientation;
|
||||
FullyCreatedConnectorInfo sinkConnectorInfo = sinkItem.GetFullConnectorInfo(connectionItem.Id, sinkConnectorOrientation, connectionItem.SinkXRatio, connectionItem.SinkYRatio, connectionItem.SinkInnerPoint, connectionItem.SinkInnerPoint);
|
||||
|
||||
ConnectionViewModel connectionVM = new ConnectionViewModel(null, sourceConnectorInfo, sinkConnectorInfo, connectionItem);
|
||||
connects.Add(connectionVM);
|
||||
}
|
||||
|
||||
var minleft = items.OfType<DesignerItemViewModelBase>().Min(p => p.Left);
|
||||
var mintop = items.OfType<DesignerItemViewModelBase>().Min(p => p.Top);
|
||||
foreach (var item in items)
|
||||
{
|
||||
item.Left -= minleft;
|
||||
item.Top -= mintop;
|
||||
}
|
||||
|
||||
SelectableDesignerItemViewModel = new List<SelectableDesignerItemViewModelBase>();
|
||||
SelectableDesignerItemViewModel.AddRange(items);
|
||||
SelectableDesignerItemViewModel.AddRange(connects);
|
||||
|
||||
Addition = SelectableDesignerItemViewModel;
|
||||
FileName = filename;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,4 +136,6 @@ namespace AIStudio.Wpf.DiagramApp.Models
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -161,6 +161,57 @@
|
||||
</Trigger>
|
||||
</DataTemplate.Triggers>
|
||||
</DataTemplate>
|
||||
<DataTemplate DataType="{x:Type model:MultipleDesignerItemToolBoxData}">
|
||||
<Grid Width="{Binding Width}" Height="{Binding Height}" ToolTip="{Binding Description}">
|
||||
<Grid.ContextMenu>
|
||||
<ContextMenu>
|
||||
<MenuItem Header="删除" Command="{StaticResource DeleteItemCommandReference}" CommandParameter="{Binding .}"/>
|
||||
</ContextMenu>
|
||||
</Grid.ContextMenu>
|
||||
<Rectangle Name="Border"
|
||||
StrokeThickness="1"
|
||||
StrokeDashArray="2"
|
||||
Fill="Transparent"
|
||||
SnapsToDevicePixels="true"/>
|
||||
<Viewbox Stretch="Uniform">
|
||||
<ItemsControl ItemsSource="{Binding SelectableDesignerItemViewModel}">
|
||||
<ItemsControl.ItemsPanel>
|
||||
<ItemsPanelTemplate>
|
||||
<Canvas x:Name="rootCanvas"/>
|
||||
</ItemsPanelTemplate>
|
||||
</ItemsControl.ItemsPanel>
|
||||
<ItemsControl.Template>
|
||||
<ControlTemplate TargetType="{x:Type ItemsControl}">
|
||||
<Grid>
|
||||
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
|
||||
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
|
||||
</Border>
|
||||
</Grid>
|
||||
</ControlTemplate>
|
||||
</ItemsControl.Template>
|
||||
<ItemsControl.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<Grid Width="{Binding ItemWidth}" Height="{Binding ItemHeight}" IsHitTestVisible="False">
|
||||
<ContentControl Content="{Binding .}" Margin="2"/>
|
||||
</Grid>
|
||||
</DataTemplate>
|
||||
</ItemsControl.ItemTemplate>
|
||||
<ItemsControl.Resources>
|
||||
<Style TargetType="{x:Type ContentPresenter}">
|
||||
<Setter Property="Canvas.Left" Value="{Binding Left}" />
|
||||
<Setter Property="Canvas.Top" Value="{Binding Top}" />
|
||||
</Style>
|
||||
</ItemsControl.Resources>
|
||||
</ItemsControl>
|
||||
</Viewbox>
|
||||
|
||||
</Grid>
|
||||
<DataTemplate.Triggers>
|
||||
<Trigger Property="IsMouseOver" Value="true">
|
||||
<Setter TargetName="Border" Property="Stroke" Value="Gray"/>
|
||||
</Trigger>
|
||||
</DataTemplate.Triggers>
|
||||
</DataTemplate>
|
||||
<DataTemplate DataType="{x:Type flowchartmodel:FlowchartToolBoxData}">
|
||||
<Grid Width="{Binding Width}" Height="{Binding Height}" ToolTip="{Binding Description}">
|
||||
<Rectangle Name="Border"
|
||||
|
||||
@@ -79,7 +79,7 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
dataObject.DesiredSize = toolBoxData.DesiredSize;
|
||||
dataObject.Icon = toolBoxData.Icon;
|
||||
dataObject.ColorViewModel = toolBoxData.ColorViewModel;
|
||||
dataObject.DesignerItem = toolBoxData.Addition as DesignerItemBase;
|
||||
dataObject.DesignerItem = toolBoxData.Addition;
|
||||
|
||||
DragDrop.DoDragDrop((DependencyObject)sender, dataObject, DragDropEffects.Copy);
|
||||
e.Handled = true;
|
||||
|
||||
@@ -566,26 +566,38 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
if (dragObject != null)
|
||||
{
|
||||
_viewModel.ClearSelectedItemsCommand.Execute(null);
|
||||
Point position = e.GetPosition(this);
|
||||
DesignerItemViewModelBase itemBase = null;
|
||||
if (dragObject.DesignerItem != null)
|
||||
Point position = e.GetPosition(this);
|
||||
if (dragObject.DesignerItem is List<SelectableDesignerItemViewModelBase> designerItems)
|
||||
{
|
||||
itemBase = Activator.CreateInstance(dragObject.ContentType, _viewModel, dragObject.DesignerItem) as DesignerItemViewModelBase;
|
||||
foreach (var item in designerItems.OfType<DesignerItemViewModelBase>())
|
||||
{
|
||||
item.Left += position.X;
|
||||
item.Top += position.Y;
|
||||
}
|
||||
_viewModel.AddItemCommand.Execute(designerItems);
|
||||
}
|
||||
else
|
||||
{
|
||||
itemBase = Activator.CreateInstance(dragObject.ContentType) as DesignerItemViewModelBase;
|
||||
itemBase.Icon = dragObject.Icon;
|
||||
itemBase.ColorViewModel = CopyHelper.Mapper(dragObject.ColorViewModel);
|
||||
if (dragObject.DesiredSize != null)
|
||||
DesignerItemViewModelBase itemBase = null;
|
||||
if (dragObject.DesignerItem is DesignerItemBase)
|
||||
{
|
||||
itemBase.ItemWidth = dragObject.DesiredSize.Value.Width;
|
||||
itemBase.ItemHeight = dragObject.DesiredSize.Value.Height;
|
||||
itemBase = Activator.CreateInstance(dragObject.ContentType, _viewModel, dragObject.DesignerItem) as DesignerItemViewModelBase;
|
||||
}
|
||||
else
|
||||
{
|
||||
itemBase = Activator.CreateInstance(dragObject.ContentType) as DesignerItemViewModelBase;
|
||||
itemBase.Icon = dragObject.Icon;
|
||||
itemBase.ColorViewModel = CopyHelper.Mapper(dragObject.ColorViewModel);
|
||||
if (dragObject.DesiredSize != null)
|
||||
{
|
||||
itemBase.ItemWidth = dragObject.DesiredSize.Value.Width;
|
||||
itemBase.ItemHeight = dragObject.DesiredSize.Value.Height;
|
||||
}
|
||||
}
|
||||
itemBase.Left = Math.Max(0, position.X - itemBase.ItemWidth / 2);
|
||||
itemBase.Top = Math.Max(0, position.Y - itemBase.ItemHeight / 2);
|
||||
_viewModel.AddItemCommand.Execute(itemBase);
|
||||
}
|
||||
itemBase.Left = Math.Max(0, position.X - itemBase.ItemWidth / 2);
|
||||
itemBase.Top = Math.Max(0, position.Y - itemBase.ItemHeight / 2);
|
||||
_viewModel.AddItemCommand.Execute(itemBase);
|
||||
}
|
||||
var dragFile = e.Data.GetData(DataFormats.FileDrop);
|
||||
if (dragFile != null && dragFile is string[] files)
|
||||
|
||||
@@ -13,6 +13,6 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
public Type ContentType { get; set; }
|
||||
public string Icon { get; set; }
|
||||
public IColorViewModel ColorViewModel { get; set; }
|
||||
public DesignerItemBase DesignerItem { get; set; }
|
||||
public object DesignerItem { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace AIStudio.Wpf.DiagramDesigner
|
||||
@@ -146,6 +147,12 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public List<SelectableDesignerItemViewModelBase> SelectedItems
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
private SelectableDesignerItemViewModelBase _selectedItem;
|
||||
public SelectableDesignerItemViewModelBase SelectedItem
|
||||
{
|
||||
|
||||
@@ -38,6 +38,12 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
List<SelectableDesignerItemViewModelBase> SelectedItems
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
IColorViewModel CopyDefaultColorViewModel();
|
||||
IFontViewModel CopyDefaultFontViewModel();
|
||||
IShapeViewModel CopyDefaultShapeViewModel();
|
||||
|
||||
@@ -512,6 +512,44 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
this.RaisePropertyChanged(nameof(Angle), GetOldValue<double>(nameof(Angle)), Angle);
|
||||
}
|
||||
|
||||
public FullyCreatedConnectorInfo GetFullConnectorInfo(Guid connectorId, ConnectorOrientation connectorOrientation, double xRatio, double yRatio, bool isInnerPoint, bool isPortless)
|
||||
{
|
||||
if (isInnerPoint)
|
||||
{
|
||||
return this.Connectors.Where(p => p.XRatio == xRatio && p.YRatio == yRatio).FirstOrDefault();
|
||||
}
|
||||
else if (isPortless)
|
||||
{
|
||||
return this.PortlessConnector;
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (connectorOrientation)
|
||||
{
|
||||
case ConnectorOrientation.Left:
|
||||
return this.LeftConnector;
|
||||
case ConnectorOrientation.TopLeft:
|
||||
return this.TopLeftConnector;
|
||||
case ConnectorOrientation.Top:
|
||||
return this.TopConnector;
|
||||
case ConnectorOrientation.TopRight:
|
||||
return this.TopRightConnector;
|
||||
case ConnectorOrientation.Right:
|
||||
return this.RightConnector;
|
||||
case ConnectorOrientation.BottomRight:
|
||||
return this.BottomRightConnector;
|
||||
case ConnectorOrientation.Bottom:
|
||||
return this.BottomConnector;
|
||||
case ConnectorOrientation.BottomLeft:
|
||||
return this.BottomLeftConnector;
|
||||
|
||||
default:
|
||||
throw new InvalidOperationException(
|
||||
string.Format("Found invalid persisted Connector Orientation for Connector Id: {0}", connectorId));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public RectangleBase GetBounds(bool includePorts = false)
|
||||
{
|
||||
if (!includePorts)
|
||||
|
||||
@@ -779,8 +779,7 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
{
|
||||
item.Id = Guid.NewGuid();
|
||||
}
|
||||
//item.LineColor = this.LineColor;
|
||||
//item.FillColor = this.FillColor;
|
||||
|
||||
var logical = item as LogicalGateItemViewModelBase;
|
||||
if (logical != null && logical.LogicalType > 0)
|
||||
{
|
||||
@@ -1479,11 +1478,11 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
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, connectionItem.SourceIsPortless);
|
||||
FullyCreatedConnectorInfo sourceConnectorInfo = sourceItem.GetFullConnectorInfo(connectionItem.Id, sourceConnectorOrientation, connectionItem.SourceXRatio, connectionItem.SourceYRatio, connectionItem.SourceInnerPoint, connectionItem.SourceIsPortless);
|
||||
|
||||
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, connectionItem.SinkIsPortless);
|
||||
FullyCreatedConnectorInfo sinkConnectorInfo = sinkItem.GetFullConnectorInfo(connectionItem.Id, sinkConnectorOrientation, connectionItem.SinkXRatio, connectionItem.SinkYRatio, connectionItem.SinkInnerPoint, connectionItem.SinkIsPortless);
|
||||
|
||||
ConnectionViewModel connectionVM = new ConnectionViewModel(this, sourceConnectorInfo, sinkConnectorInfo, connectionItem);
|
||||
connectors.Add(connectionVM);
|
||||
@@ -1512,45 +1511,7 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
{
|
||||
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 bool ItemsToDeleteHasConnector(List<SelectableDesignerItemViewModelBase> itemsToRemove, ConnectorInfoBase connector)
|
||||
{
|
||||
@@ -1782,10 +1743,6 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public RectangleBase GetBoundingRectangle(IEnumerable<DesignerItemViewModelBase> items)
|
||||
{
|
||||
double x1 = Double.MaxValue;
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace AIStudio.Wpf.DiagramDesigner
|
||||
{
|
||||
public class DiagramViewModelHelper
|
||||
{
|
||||
public static DesignerItemViewModelBase GetConnectorDataItem(IEnumerable<SelectableDesignerItemViewModelBase> items, Guid conectorDataItemId, Type connectorDataItemType)
|
||||
{
|
||||
DesignerItemViewModelBase dataItem = items.OfType<DesignerItemViewModelBase>().Single(x => x.Id == conectorDataItemId);
|
||||
return dataItem;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user