Flowchart

This commit is contained in:
艾竹
2022-12-04 23:07:20 +08:00
parent dc42f75610
commit 0487857d7b
30 changed files with 1621 additions and 383 deletions

View File

@@ -1,20 +1,12 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AIStudio.Wpf.DiagramDesigner;
using AIStudio.Wpf.Flowchart.Models;
using AIStudio.Wpf.Flowchart.ViewModels;
namespace AIStudio.Wpf.Flowchart.Controls
{
@@ -26,24 +18,8 @@ namespace AIStudio.Wpf.Flowchart.Controls
{
public const string PART_DiagramControl = "PART_DiagramControl";
private DiagramControl _diagramControl;
private IDiagramServiceProvider _service
{
get
{
return DiagramServicesProvider.Instance.Provider;
}
}
private DiagramViewModel _diagramViewModel = new DiagramViewModel()
{
ShowGrid = true,
GridCellSize = new Size(100, 60),
GridMargin = 0d,
CellHorizontalAlignment = CellHorizontalAlignment.Center,
CellVerticalAlignment = CellVerticalAlignment.Center,
PageSizeType = PageSizeType.Custom,
PageSize = new Size(double.NaN, double.NaN),
};
private IDiagramViewModel _diagramViewModel;
static FlowchartEditor()
{
@@ -51,7 +27,18 @@ namespace AIStudio.Wpf.Flowchart.Controls
}
public FlowchartEditor()
{
{
_diagramViewModel = new DiagramViewModel();
_diagramViewModel.SetScreenScale();
_diagramViewModel.ShowGrid = true;
_diagramViewModel.GridCellSize = new Size(125 / _diagramViewModel.ScreenScale, 125 / _diagramViewModel.ScreenScale);
_diagramViewModel.GridMargin = 0d;
_diagramViewModel.CellHorizontalAlignment = CellHorizontalAlignment.Center;
_diagramViewModel.CellVerticalAlignment = CellVerticalAlignment.Center;
_diagramViewModel.PageSizeType = PageSizeType.Custom;
_diagramViewModel.PageSize = new Size(double.NaN, double.NaN);
_diagramViewModel.VectorLineDrawMode = DrawMode.BoundaryConnectingLine;
_diagramViewModel.PropertyChanged += DiagramViewModel_PropertyChanged;
}
@@ -63,6 +50,8 @@ namespace AIStudio.Wpf.Flowchart.Controls
_diagramControl.HorizontalAlignment = HorizontalAlignment.Stretch;
_diagramControl.VerticalAlignment = VerticalAlignment.Stretch;
_diagramControl.DataContext = _diagramViewModel;
GetDataFunc = GetData;
}
private void DiagramViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
@@ -74,47 +63,95 @@ namespace AIStudio.Wpf.Flowchart.Controls
}
//一点要绑定不为空的FlowchartModel才能用即便为空的也要new一个再来绑定
public static readonly DependencyProperty FlowchartModelProperty =
DependencyProperty.Register(nameof(FlowchartModel),
typeof(FlowchartModel),
public static readonly DependencyProperty DataProperty =
DependencyProperty.Register(nameof(Data),
typeof(string),
typeof(FlowchartEditor),
new FrameworkPropertyMetadata(null, OnFlowchartModelChanged));
new FrameworkPropertyMetadata(null, OnDataChanged));
public FlowchartModel FlowchartModel
public string Data
{
get
{
return (FlowchartModel)GetValue(FlowchartModelProperty);
return (string)GetValue(DataProperty);
}
set
{
SetValue(FlowchartModelProperty, value);
SetValue(DataProperty, value);
}
}
private static void OnFlowchartModelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
private static void OnDataChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var view = d as FlowchartEditor;
var model = e.NewValue as FlowchartModel;
if (model != null)
var json = e.NewValue as string;
if (json != null)
{
view.CreateFlowchartModel(model);
view.CreateFlowchartModel(json);
}
}
private void CreateFlowchartModel(FlowchartModel model)
private void CreateFlowchartModel(string json)
{
_service.DrawModeViewModel.VectorLineDrawMode = DrawMode.BoundaryConnectingLine;
_diagramViewModel.Items.Clear();
if (model != null)
_diagramViewModel.ToObject(json);
}
public static readonly DependencyProperty GetDataFuncProperty =
DependencyProperty.Register(nameof(GetDataFunc),
typeof(Func<string>),
typeof(FlowchartEditor),
new FrameworkPropertyMetadata(null));
public Func<string> GetDataFunc
{
get
{
foreach (var node in model.Nodes)
{
_diagramViewModel.DirectAddItemCommand.Execute(node);
}
foreach (var link in model.Links)
{
_diagramViewModel.DirectAddItemCommand.Execute(link);
}
return (Func<string>)this.GetValue(GetDataFuncProperty);
}
set
{
this.SetValue(GetDataFuncProperty, value);
}
}
public Func<string> GetData
{
get
{
return new Func<string>(() => _diagramViewModel.ToJson());
}
}
public static readonly DependencyProperty ModeProperty =
DependencyProperty.Register(nameof(Mode),
typeof(string),
typeof(FlowchartEditor),
new FrameworkPropertyMetadata("Edit", OnModeChanged));
public string Mode
{
get
{
return (string)GetValue(ModeProperty);
}
set
{
SetValue(ModeProperty, value);
}
}
private static void OnModeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var view = d as FlowchartEditor;
var mode = e.NewValue as string;
if (mode != "Edit")
{
view._diagramViewModel.IsReadOnly = true;
}
else
{
view._diagramViewModel.IsReadOnly = false;
}
}