添加节点可删除

This commit is contained in:
艾竹
2023-01-23 15:43:44 +08:00
parent bb2882c618
commit 2c76aacc66
13 changed files with 158 additions and 45 deletions

View File

@@ -78,14 +78,14 @@ namespace AIStudio.Wpf.Flowchart
ConnectorViewModel connector3 = new ConnectorViewModel(decide.RightConnector, middle2.TopConnector, _service.DrawModeViewModel.LineDrawMode, _service.DrawModeViewModel.LineRouterMode);
DiagramViewModel.DirectAddItemCommand.Execute(connector3);
connector3.AddText(">=3");
connector3.AddLabel(">=3");
ConnectorViewModel connector4 = new ConnectorViewModel(middle2.BottomConnector, cobegin.TopConnector, _service.DrawModeViewModel.LineDrawMode, _service.DrawModeViewModel.LineRouterMode);
DiagramViewModel.DirectAddItemCommand.Execute(connector4);
ConnectorViewModel connector5 = new ConnectorViewModel(decide.BottomConnector, cobegin.TopConnector, _service.DrawModeViewModel.LineDrawMode, _service.DrawModeViewModel.LineRouterMode);
DiagramViewModel.DirectAddItemCommand.Execute(connector5);
connector5.AddText("<3");
connector5.AddLabel("<3");
ConnectorViewModel connector6 = new ConnectorViewModel(cobegin.BottomConnector, middle3.TopConnector, _service.DrawModeViewModel.LineDrawMode, _service.DrawModeViewModel.LineRouterMode);
DiagramViewModel.DirectAddItemCommand.Execute(connector6);

View File

@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
namespace AIStudio.Wpf.DiagramDesigner
{
public static class ConnectorStyleProps
{
public static readonly DependencyProperty StyleProperty =
DependencyProperty.RegisterAttached("Style", typeof(Style), typeof(ConnectorStyleProps),
new FrameworkPropertyMetadata(default(Style)));
public static Style GetStyle(DependencyObject d)
{
return (Style)d.GetValue(StyleProperty);
}
public static void SetStyle(DependencyObject d, Style value)
{
d.SetValue(StyleProperty, value);
}
}
}

View File

@@ -12,6 +12,7 @@ namespace AIStudio.Wpf.DiagramDesigner
public class Connector : Control
{
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);

View File

@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Windows;
using System.Windows.Data;
namespace AIStudio.Wpf.DiagramDesigner
{
public class ConectorStyleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
{
if (parameter is Style style)
{
return style;
}
else if (parameter is string str)
{
return Application.Current.FindResource(str) as Style;
}
else
{
return Application.Current.FindResource("DefaultConnectorStyle") as Style;
}
}
return value;
}
public object ConvertBack(object value, Type targetTypes, object parameter, CultureInfo culture)
{
return DependencyProperty.UnsetValue;
}
}
}

View File

@@ -23,7 +23,8 @@
<s:LineDashConverter x:Key="LineDashConverter"/>
<s:ClipConverter x:Key="ClipConverter"/>
<s:InvertBoolConverter x:Key="InvertBoolConverter"/>
<s:ConectorStyleConverter x:Key="ConectorStyleConverter"/>
<!-- ResizeDecorator Default Template -->
<!--
<ControlTemplate x:Key="ResizeDecoratorTemplate" TargetType="{x:Type Control}">
@@ -155,7 +156,7 @@
</Style>
<!-- Connector Style -->
<Style x:Key="normalConnector" TargetType="{x:Type s:Connector}">
<Style x:Key="DefaultConnectorStyle" TargetType="{x:Type s:Connector}">
<Setter Property="Cursor" Value="Cross" />
<Setter Property="SnapsToDevicePixels" Value="true" />
<Setter Property="Template">
@@ -187,7 +188,7 @@
</Style>
<!-- Connector Style -->
<Style x:Key="logicalConnector" TargetType="{x:Type s:Connector}">
<Style x:Key="LogicalConnectorStyle" TargetType="{x:Type s:Connector}">
<Setter Property="Cursor" Value="Cross" />
<Setter Property="SnapsToDevicePixels" Value="true" />
<Setter Property="Template">
@@ -274,22 +275,6 @@
</Setter>
</Style>
<!-- Connector Style -->
<Style x:Key="pointConnector1" TargetType="{x:Type s:PointConnector}">
<Setter Property="SnapsToDevicePixels" Value="true" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type s:PointConnector}">
<Grid Width="{Binding ConnectorWidth}" Height="{Binding ConnectorHeight}">
<!-- transparent extra space makes connector easier to hit -->
<Ellipse Fill="Transparent" Margin="-2" />
<Ellipse Fill="{Binding ColorViewModel.FillColor,Converter={StaticResource ColorBrushConverter}}" StrokeThickness="1" Stroke="{Binding ColorViewModel.LineColor,Converter={StaticResource ColorBrushConverter}}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="normalConnectorContainer" TargetType="{x:Type s:ConnectorContainer}">
<Setter Property="Template">
<Setter.Value>
@@ -326,22 +311,22 @@
<ControlTemplate x:Key="ConnectorDecoratorTemplate"
TargetType="{x:Type Control}">
<Grid Margin="-5">
<s:Connector DataContext="{Binding LeftConnector}" Style="{StaticResource normalConnector}"
<s:Connector DataContext="{Binding LeftConnector}" Style="{Binding Style, Converter={StaticResource ConectorStyleConverter},ConverterParameter={StaticResource DefaultConnectorStyle}}"
Orientation="Left"
VerticalAlignment="Center"
HorizontalAlignment="Left"
Visibility="{Binding Path=ShowConnectors, Converter={x:Static s:BoolToVisibilityConverter.Instance}}" />
<s:Connector DataContext="{Binding TopConnector}" Style="{StaticResource normalConnector}"
<s:Connector DataContext="{Binding TopConnector}" Style="{Binding Style, Converter={StaticResource ConectorStyleConverter},ConverterParameter={StaticResource DefaultConnectorStyle}}"
Orientation="Top"
VerticalAlignment="Top"
HorizontalAlignment="Center"
Visibility="{Binding Path=ShowConnectors, Converter={x:Static s:BoolToVisibilityConverter.Instance}}" />
<s:Connector DataContext="{Binding RightConnector}" Style="{StaticResource normalConnector}"
<s:Connector DataContext="{Binding RightConnector}" Style="{Binding Style, Converter={StaticResource ConectorStyleConverter},ConverterParameter={StaticResource DefaultConnectorStyle}}"
Orientation="Right"
VerticalAlignment="Center"
HorizontalAlignment="Right"
Visibility="{Binding Path=ShowConnectors, Converter={x:Static s:BoolToVisibilityConverter.Instance}}" />
<s:Connector DataContext="{Binding BottomConnector}" Style="{StaticResource normalConnector}"
<s:Connector DataContext="{Binding BottomConnector}" Style="{Binding Style, Converter={StaticResource ConectorStyleConverter},ConverterParameter={StaticResource DefaultConnectorStyle}}"
Orientation="Bottom"
VerticalAlignment="Bottom"
HorizontalAlignment="Center"
@@ -355,7 +340,7 @@
<s:ConnectorContainer.ItemTemplate>
<DataTemplate>
<Grid>
<s:Connector Style="{StaticResource normalConnector}"/>
<s:Connector Style="{StaticResource DefaultConnectorStyle}"/>
</Grid>
</DataTemplate>
</s:ConnectorContainer.ItemTemplate>
@@ -373,6 +358,8 @@
<ItemsControl ItemsSource="{Binding Items}"
ItemContainerStyleSelector="{x:Static s:DesignerItemsControlItemStyleSelector.Instance}">
<ItemsControl.Resources>
<!--基础类型-->
<Style x:Key="designerItemStyle"
TargetType="{x:Type ContentPresenter}">
<Setter Property="Canvas.Left"
@@ -509,6 +496,7 @@
</Style.Triggers>
</Style>
<!--连接线-->
<Style x:Key="connectorItemStyle"
TargetType="{x:Type ContentPresenter}">
<Setter Property="Width"
@@ -530,7 +518,7 @@
<Grid.ContextMenu>
<ContextMenu>
<MenuItem Header="删除" Command="{Binding DeleteConnectionCommand}" CommandParameter="{Binding}"/>
<MenuItem Header="添加文本" Command="{Binding AddTextCommand}" CommandParameter="{Binding}"/>
<MenuItem Header="添加文本" Command="{Binding AddLabelCommand}" CommandParameter="{Binding}"/>
<MenuItem Header="插入点(按住ctrl可一直插入)" IsCheckable="True" IsChecked="{Binding ShouldInsertAnchor}" />
</ContextMenu>
</Grid.ContextMenu>
@@ -600,7 +588,12 @@
<s:PointContainer.ItemTemplate>
<DataTemplate>
<Grid>
<s:PointConnector Style="{StaticResource pointConnector1}"/>
<Grid.ContextMenu>
<ContextMenu>
<MenuItem Header="删除" Command="{Binding DeleteVertexCommand}" CommandParameter="{Binding}"/>
</ContextMenu>
</Grid.ContextMenu>
<s:PointConnector Style="{StaticResource pointConnector}"/>
<c:PointDragThumb Cursor="SizeAll" Opacity="0"/>
</Grid>
</DataTemplate>
@@ -617,6 +610,11 @@
<s:PointContainer.ItemTemplate>
<DataTemplate>
<Grid s:SelectionProps.EnabledForSelection="True" Background="{Binding ColorViewModel.FillColor,Converter={StaticResource ColorBrushConverter}}">
<Grid.ContextMenu>
<ContextMenu>
<MenuItem Header="删除" Command="{Binding DeleteLabelCommand}" CommandParameter="{Binding}"/>
</ContextMenu>
</Grid.ContextMenu>
<c:PointDragThumb Cursor="SizeAll" Opacity="0"/>
<s:TextControl Margin="5" />
</Grid>
@@ -691,6 +689,7 @@
</Setter>
</Style>
<!--自带的Gif样式-->
<Style x:Key="gifimageItemStyle"
TargetType="{x:Type ContentPresenter}">
<Setter Property="Canvas.Top"
@@ -736,7 +735,7 @@
<s:ConnectorContainer x:Name="PART_ConnectorContainer" Visibility="Hidden" Style="{StaticResource normalConnectorContainer}" ItemsSource="{Binding Connectors}">
<s:ConnectorContainer.ItemTemplate>
<DataTemplate>
<s:Connector Style="{StaticResource normalConnector}"/>
<s:Connector Style="{StaticResource DefaultConnectorStyle}"/>
</DataTemplate>
</s:ConnectorContainer.ItemTemplate>
</s:ConnectorContainer>
@@ -768,6 +767,7 @@
</Setter>
</Style>
<!--自带的logical样式-->
<Style x:Key="logicalItemStyle"
TargetType="{x:Type ContentPresenter}">
<Setter Property="Canvas.Top"
@@ -811,7 +811,7 @@
<s:ConnectorContainer.ItemTemplate>
<DataTemplate>
<Grid>
<s:Connector Style="{StaticResource logicalConnector}"/>
<s:Connector Style="{StaticResource LogicalConnectorStyle}"/>
</Grid>
</DataTemplate>
</s:ConnectorContainer.ItemTemplate>
@@ -890,7 +890,7 @@
</Setter.Value>
</Setter>
</Style>
<Style x:Key="linkpointItemStyle"
TargetType="{x:Type ContentPresenter}">
<Setter Property="Canvas.Top"
@@ -917,7 +917,7 @@
<!-- PART_ConnectorDecorator -->
<Grid Margin="-5"
x:Name="PART_ConnectorDecorator">
<s:Connector DataContext="{Binding TopConnector}" Style="{StaticResource normalConnector}"
<s:Connector DataContext="{Binding TopConnector}" Style="{StaticResource DefaultConnectorStyle}"
Orientation="Right"
VerticalAlignment="Center"
HorizontalAlignment="Center"

View File

@@ -22,6 +22,9 @@ namespace AIStudio.Wpf.DiagramDesigner
public DiagramControl()
{
InitializeComponent();
//把资源加入全局资源
Application.Current.Resources.Add("DefaultConnectorStyle", this.FindResource("DefaultConnectorStyle"));
}
public static readonly DependencyProperty ZoomValueProperty = DependencyProperty.Register(nameof(ZoomValue), typeof(double), typeof(DiagramControl), new UIPropertyMetadata(1d));

View File

@@ -61,7 +61,7 @@
</Setter>
</Style>
</UserControl.Resources>
<Grid IsHitTestVisible="{Binding IsHitTestVisible}">
<Grid IsHitTestVisible="{Binding IsHitTestVisible}">
<TextBox x:Name="PART_TextBlock"
Text="{Binding Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Foreground="{Binding FontViewModel.FontColor,Converter={StaticResource ColorBrushConverter}}"

View File

@@ -14,6 +14,8 @@ namespace AIStudio.Wpf.DiagramDesigner
Offset = offset ?? new PointBase();
FontViewModel = Parent.FontViewModel;
ColorViewModel = Parent.ColorViewModel;
DeleteLabelCommand = new SimpleCommand(DeleteLabel);
}
public ConnectorViewModel Parent
@@ -78,16 +80,11 @@ namespace AIStudio.Wpf.DiagramDesigner
}
}
public override PointBase Position
public SimpleCommand DeleteLabelCommand
{
get
{
return new PointBase(Parent.Area.Left + Left, Parent.Area.Top + Top);
}
get; set;
}
public override PointBase MiddlePosition => new PointBase(Parent.Area.Left + Left + ConnectorWidth / 2, Parent.Area.Top + Top + ConnectorHeight / 2);
private bool updating = false;
public void UpdatePosition(SvgPath[] paths)
@@ -167,5 +164,13 @@ namespace AIStudio.Wpf.DiagramDesigner
}
}
private void DeleteLabel(object parameter)
{
if (parameter is ConnectorLabelModel label)
{
Parent.Labels.Remove(label);
}
}
}
}

View File

@@ -9,6 +9,8 @@ namespace AIStudio.Wpf.DiagramDesigner
Parent = parent;
X = position?.X ?? 0;
Y = position?.Y ?? 0;
DeleteVertexCommand = new SimpleCommand(DeleteVertex);
}
public ConnectorViewModel Parent
@@ -26,5 +28,18 @@ namespace AIStudio.Wpf.DiagramDesigner
public override PointBase MiddlePosition => new PointBase(Parent.Area.Left + Left + ConnectorWidth / 2, Parent.Area.Top + Top + ConnectorHeight / 2);
public SimpleCommand DeleteVertexCommand
{
get; set;
}
private void DeleteVertex(object parameter)
{
if (parameter is ConnectorVertexModel vertice)
{
Parent.Vertices.Remove(vertice);
}
}
}
}

View File

@@ -61,7 +61,7 @@ namespace AIStudio.Wpf.DiagramDesigner
this.SinkConnectorInfo = sinkConnectorInfo;
DeleteConnectionCommand = new SimpleCommand(DeleteConnection);
AddVertexCommand = new SimpleCommand(AddVertex);
AddTextCommand = new SimpleCommand(AddText);
AddLabelCommand = new SimpleCommand(AddLabel);
}
protected void LoadDesignerItemViewModel(SelectableDesignerItemBase designerbase)
@@ -339,7 +339,7 @@ namespace AIStudio.Wpf.DiagramDesigner
get; set;
}
public SimpleCommand AddTextCommand
public SimpleCommand AddLabelCommand
{
get; set;
}
@@ -643,10 +643,10 @@ namespace AIStudio.Wpf.DiagramDesigner
protected override void ExecuteEditCommand(object param)
{
AddText();
AddLabel();
}
public void AddText(object text = null)
public void AddLabel(object text = null)
{
var label = new ConnectorLabelModel(this, text?.ToString());
label.PropertyChanged += new WeakINPCEventHandler(ConnectorViewModel_PropertyChanged).Handler;
@@ -656,6 +656,8 @@ namespace AIStudio.Wpf.DiagramDesigner
var paths = Labels.Count > 0 ? PathGeneratorResult.Paths.Select(p => new SvgPath(p)).ToArray() : Array.Empty<SvgPath>();
label.UpdatePosition(paths);
}
#endregion
}
}

View File

@@ -69,6 +69,7 @@ namespace AIStudio.Wpf.DiagramDesigner
connectors.Add(new FullyCreatedConnectorInfo(this, ConnectorOrientation.Right));
}
#region
public FullyCreatedConnectorInfo TopConnector
{
get { return (connectors != null && connectors.Count >= 1) ? connectors[0] : null; }
@@ -89,6 +90,11 @@ namespace AIStudio.Wpf.DiagramDesigner
get { return (connectors != null && connectors.Count >= 4) ? connectors[3] : null; }
}
public Style ConnectorStyle
{
get; set;
}
public ShapeDefiner ShapeDefiner
{
get;
@@ -348,6 +354,9 @@ namespace AIStudio.Wpf.DiagramDesigner
}
}
#endregion
#region
public void AddConnector(FullyCreatedConnectorInfo connector)
{
if (!connectors.Contains(connector))
@@ -449,5 +458,7 @@ namespace AIStudio.Wpf.DiagramDesigner
}
public IShape GetShape() => ShapeDefiner(this);
#endregion
}
}

View File

@@ -136,6 +136,19 @@ namespace AIStudio.Wpf.DiagramDesigner
}
}
private Style _style;
public Style Style
{
get
{
return _style;
}
set
{
SetProperty(ref _style, value);
}
}
public SimpleCommand DeleteCommand
{
get; private set;

View File

@@ -231,7 +231,7 @@ namespace AIStudio.Wpf.Flowchart.Models
linkModel.ColorViewModel.LineWidth = diagramLink.Width;
if (!string.IsNullOrEmpty(diagramLink.Label))
{
linkModel.AddText(diagramLink.Label);
linkModel.AddLabel(diagramLink.Label);
}
//线条形状与箭头待处理
//switch (diagramLink.Router)