From fae78265773758d71b22312ef901a17a1085da9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=89=BE=E7=AB=B9?= Date: Sun, 9 Apr 2023 12:38:57 +0800 Subject: [PATCH] =?UTF-8?q?ReDo=20Undo=20Item=20ItemWidth=20=E5=92=8C=20An?= =?UTF-8?q?gle=20=E7=AD=89=E5=B7=A5=E5=85=B7=E6=A0=8F=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ViewModels/MainWindowViewModel.cs | 253 +++--------------- .../ViewModels/PageViewModel.cs | 232 +++++++++------- .../ViewModels/ToolBoxViewModel.cs | 18 +- AIStudio.Wpf.DiagramApp/Views/MainWindow.xaml | 54 ++-- .../Views/MainWindow.xaml.cs | 22 -- .../Views/PropertyControl.xaml | 22 +- .../ViewModels/OutLineTextDesignerItemData.cs | 2 +- .../OutLineTextDesignerItemViewModel.cs | 2 +- .../Helpers/CopyHelper.cs | 46 +++- .../Serializables/SelectableItemBase.cs | 13 +- .../AdditionViewModel/QuickThemeViewModel.cs | 113 ++++---- .../BaseViewModel/DiagramServicesProvider.cs | 12 +- .../BaseViewModel/DiagramViewModel.cs | 133 ++++++++- .../BaseViewModel/SelectableViewModelBase.cs | 6 +- .../ViewModels/IDiagramServiceProvider.cs | 7 +- .../ViewModels/IDiagramViewModel.cs | 16 ++ 16 files changed, 487 insertions(+), 464 deletions(-) diff --git a/AIStudio.Wpf.DiagramApp/ViewModels/MainWindowViewModel.cs b/AIStudio.Wpf.DiagramApp/ViewModels/MainWindowViewModel.cs index 2cbf1fc..92ea1cb 100644 --- a/AIStudio.Wpf.DiagramApp/ViewModels/MainWindowViewModel.cs +++ b/AIStudio.Wpf.DiagramApp/ViewModels/MainWindowViewModel.cs @@ -37,7 +37,7 @@ namespace AIStudio.Wpf.DiagramApp.ViewModels public MainWindowViewModel() { - ToolBoxViewModel = new ToolBoxViewModel(); + ToolBoxViewModel = new ToolBoxViewModel(this); DiagramsViewModels = new ObservableCollection(); DiagramsViewModels.Add(new PageViewModel("新建-1", "*", DiagramType.Normal)); @@ -178,11 +178,11 @@ namespace AIStudio.Wpf.DiagramApp.ViewModels } } - public SelectableDesignerItemViewModelBase SelectedItem + public SelectableDesignerItemViewModelBase SelectedItemViewModel { get { - return _service.SelectedItem; + return _service.SelectedItemViewModel; } } @@ -367,118 +367,7 @@ namespace AIStudio.Wpf.DiagramApp.ViewModels { return this._selectedColorCommand ?? (this._selectedColorCommand = new DelegateCommand(para => this.SelectedColorExecuted(para))); } - } - - private ICommand _addPageCommand; - public ICommand AddPageCommand - { - get - { - return this._addPageCommand ?? (this._addPageCommand = new DelegateCommand(para => this.AddPageExecuted(para))); - } - } - - - private ICommand _addCopyPageCommand; - public ICommand AddCopyPageCommand - { - get - { - return this._addCopyPageCommand ?? (this._addCopyPageCommand = new DelegateCommand(para => this.AddCopyPageExecuted(para))); - } - } - - private ICommand _renamePageCommand; - public ICommand RenamePageCommand - { - get - { - return this._renamePageCommand ?? (this._renamePageCommand = new DelegateCommand(para => this.RenamePageExecuted(para))); - } - } - - private ICommand _endRenamePageCommand; - public ICommand EndRenamePageCommand - { - get - { - return this._endRenamePageCommand ?? (this._endRenamePageCommand = new DelegateCommand(para => this.EndRenamePageExecuted(para))); - } - } - - private ICommand _deletePageCommand; - public ICommand DeletePageCommand - { - get - { - return this._deletePageCommand ?? (this._deletePageCommand = new DelegateCommand(para => this.DeletePageExecuted(para))); - } - } - - - private ICommand _addImageCommand; - public ICommand AddImageCommand - { - get - { - return this._addImageCommand ?? (this._addImageCommand = new DelegateCommand(para => this.AddImageExecuted(para))); - } - } - - private ICommand _editImageCommand; - public ICommand EditImageCommand - { - get - { - return this._editImageCommand ?? (this._editImageCommand = new DelegateCommand(para => this.EditImageExecuted(para))); - } - } - - private ICommand _resizeImageCommand; - public ICommand ResizeImageCommand - { - get - { - return this._resizeImageCommand ?? (this._resizeImageCommand = new DelegateCommand(para => this.ResizeImageExecuted(para))); - } - } - - private ICommand _resetImageCommand; - public ICommand ResetImageCommand - { - get - { - return this._resetImageCommand ?? (this._resetImageCommand = new DelegateCommand(para => this.ResetImageExecuted(para))); - } - } - - private ICommand _addVideoCommand; - public ICommand AddVideoCommand - { - get - { - return this._addVideoCommand ?? (this._addVideoCommand = new DelegateCommand(para => this.AddVideoExectued(para))); - } - } - - - private ICommand _addOutLineTextCommand; - public ICommand AddOutLineTextCommand - { - get - { - return this._addOutLineTextCommand ?? (this._addOutLineTextCommand = new DelegateCommand(para => this.AddOutLineTextExecuted(para))); - } - } - - private ICommand _addBarcodeCommand; - public ICommand AddBarcodeCommand - { - get - { - return this._addBarcodeCommand ?? (this._addBarcodeCommand = new DelegateCommand(para => this.AddBarcodeExecuted(para))); - } - } + } private ICommand _aboutCommand; public ICommand AboutCommand @@ -525,47 +414,42 @@ namespace AIStudio.Wpf.DiagramApp.ViewModels || e.PropertyName == nameof(ShapeViewModel) || e.PropertyName == nameof(QuickThemeViewModel) || e.PropertyName == nameof(LockObjectViewModel) - || e.PropertyName == nameof(SelectedItem)) + || e.PropertyName == nameof(SelectedItemViewModel)) { RaisePropertyChanged(e.PropertyName); } - if (PageViewModel == null) return; + if (PageViewModel == null || PageViewModel.DiagramViewModel == null) return; if (sender is IFontViewModel) - PageViewModel.SetFont(sender as IFontViewModel, e.PropertyName); - - if (sender is IColorViewModel) - PageViewModel.SetColor(sender as IColorViewModel, e.PropertyName); - - if (sender is IShapeViewModel) - PageViewModel.SetSharp(sender as IShapeViewModel, e.PropertyName); - - if (sender is IQuickThemeViewModel) - PageViewModel.SetQuickItem(sender as IQuickThemeViewModel, e.PropertyName); - - if (sender is LockObject) - PageViewModel.LockAction(sender as LockObject, e.PropertyName); - - if (sender is DesignerItemViewModelBase designer - && (e.PropertyName == nameof(designer.Angle) - || e.PropertyName == nameof(designer.ItemWidth) - || e.PropertyName == nameof(designer.ItemHeight) - || e.PropertyName == nameof(designer.ScaleX) - || e.PropertyName == nameof(designer.ScaleY))) { - PageViewModel.SetPropertyValue(designer, e.PropertyName); + PageViewModel.DiagramViewModel.SetFont(sender as IFontViewModel, e.PropertyName, PageViewModel.DiagramViewModel.SelectedItems); + } + else if (sender is IColorViewModel) + { + PageViewModel.DiagramViewModel.SetColor(sender as IColorViewModel, e.PropertyName, PageViewModel.DiagramViewModel.SelectedItems); + } + else if (sender is IShapeViewModel) + { + PageViewModel.DiagramViewModel.SetSharp(sender as IShapeViewModel, e.PropertyName, PageViewModel.DiagramViewModel.SelectedItems); + } + else if (sender is IQuickThemeViewModel) + { + PageViewModel.DiagramViewModel.SetQuickItem(sender as IQuickThemeViewModel, e.PropertyName, PageViewModel.DiagramViewModel.SelectedItems); + } + else if (sender is LockObject) + { + PageViewModel.DiagramViewModel.LockAction(sender as LockObject, e.PropertyName, PageViewModel.DiagramViewModel.SelectedItems); + } + else if (sender is SelectableDesignerItemViewModelBase designer) + { + PageViewModel.DiagramViewModel.SetPropertyValue(designer, e.PropertyName, PageViewModel.DiagramViewModel.SelectedItems); } } public bool KeyExecuted(KeyEventArgs e) { - //if (PageViewModel?.DiagramViewModel?.ExecuteShortcut(e) == true) - //{ - // return true; - //} - var para = e.KeyboardDevice.Modifiers == ModifierKeys.None ? e.Key.ToString() : e.KeyboardDevice.Modifiers.ToString() + "+" + e.Key.ToString(); bool executed = true; switch (para) @@ -577,22 +461,7 @@ namespace AIStudio.Wpf.DiagramApp.ViewModels } return executed; - } - - private void UnDoExecuted() - { - PageViewModel?.DiagramViewModel?.UndoCommand.Execute(null); - } - - private void ReDoExecuted() - { - PageViewModel?.DiagramViewModel?.RedoCommand.Execute(null); - } - - private void SelectedAllExecuted() - { - PageViewModel?.DiagramViewModel?.SelectAllCommand.Execute(null); - } + } private void OpenExecuted(string para = null) { @@ -789,75 +658,15 @@ namespace AIStudio.Wpf.DiagramApp.ViewModels LockObjectViewModel.LockObject.ForEach(p => p.IsChecked = false); } - private void AddPageExecuted(object para) - { - PageViewModel?.AddPageExecuted(para); - } - - private void AddCopyPageExecuted(object para) - { - PageViewModel?.AddCopyPageExecuted(para); - } - - private void RenamePageExecuted(object para) - { - PageViewModel?.RenamePageExecuted(para); - } - - private void EndRenamePageExecuted(object para) - { - PageViewModel?.EndRenamePageExecuted(para); - } - - private void DeletePageExecuted(object para) - { - PageViewModel?.DeletePageExecuted(para); - } - - private void AddImageExecuted(object para) - { - PageViewModel?.AddImageExecuted(para); - } - - private void EditImageExecuted(object para) - { - PageViewModel?.EditImageExecuted(PageViewModel.DiagramViewModel.SelectedItems?.FirstOrDefault()); - } - - private void ResizeImageExecuted(object para) - { - PageViewModel?.ResizeImageExecuted(PageViewModel.DiagramViewModel.SelectedItems?.FirstOrDefault()); - } - - private void ResetImageExecuted(object para) - { - PageViewModel?.ResetImageExecuted(PageViewModel.DiagramViewModel.SelectedItems?.FirstOrDefault()); - } - - private void AddVideoExectued(object para) - { - PageViewModel?.AddVideoExecuted(para); - } - - private void AddOutLineTextExecuted(object para) - { - PageViewModel?.AddOutLineTextExecuted(para); - } - - private void AddBarcodeExecuted(object para) - { - PageViewModel?.AddBarcodeExecuted(para); - } - private void SelectedColorExecuted(object para) { if (para == null) return; switch (ColorType) { - case Models.ColorType.Text: PageViewModel?.SetFont(new FontViewModel() { FontColor = (Color)para }, "FontColor"); break; - case Models.ColorType.Fill: PageViewModel?.SetColor(new ColorViewModel() { FillColor = new ColorObject() { Color = (Color)para } }, "FillColor"); break; - case Models.ColorType.Line: PageViewModel?.SetColor(new ColorViewModel() { LineColor = new ColorObject() { Color = (Color)para } }, "LineColor"); break; + case Models.ColorType.Text: PageViewModel?.DiagramViewModel?.SetFont(new FontViewModel() { FontColor = (Color)para }, "FontColor", PageViewModel.DiagramViewModel.SelectedItems); break; + case Models.ColorType.Fill: PageViewModel?.DiagramViewModel?.SetColor(new ColorViewModel() { FillColor = new ColorObject() { Color = (Color)para } }, "FillColor", PageViewModel.DiagramViewModel.SelectedItems); break; + case Models.ColorType.Line: PageViewModel?.DiagramViewModel?.SetColor(new ColorViewModel() { LineColor = new ColorObject() { Color = (Color)para } }, "LineColor", PageViewModel.DiagramViewModel.SelectedItems); break; } } diff --git a/AIStudio.Wpf.DiagramApp/ViewModels/PageViewModel.cs b/AIStudio.Wpf.DiagramApp/ViewModels/PageViewModel.cs index 09d686a..0260ba3 100644 --- a/AIStudio.Wpf.DiagramApp/ViewModels/PageViewModel.cs +++ b/AIStudio.Wpf.DiagramApp/ViewModels/PageViewModel.cs @@ -25,6 +25,7 @@ namespace AIStudio.Wpf.DiagramApp.ViewModels { public partial class PageViewModel : BindableBase { + #region 初始化 protected IDiagramServiceProvider _service { get @@ -71,14 +72,14 @@ namespace AIStudio.Wpf.DiagramApp.ViewModels InitDiagramViewModel(); } + #endregion + #region 属性 public string FileName { get; set; } - #region 属性 - private string _title; public string Title { @@ -172,15 +173,124 @@ namespace AIStudio.Wpf.DiagramApp.ViewModels } #endregion - #region + #region 命令 + private ICommand _addPageCommand; + public ICommand AddPageCommand + { + get + { + return this._addPageCommand ?? (this._addPageCommand = new DelegateCommand(para => this.AddPageExecuted(para))); + } + } + + private ICommand _addCopyPageCommand; + public ICommand AddCopyPageCommand + { + get + { + return this._addCopyPageCommand ?? (this._addCopyPageCommand = new DelegateCommand(para => this.AddCopyPageExecuted(para))); + } + } + + private ICommand _renamePageCommand; + public ICommand RenamePageCommand + { + get + { + return this._renamePageCommand ?? (this._renamePageCommand = new DelegateCommand(para => this.RenamePageExecuted(para))); + } + } + + private ICommand _endRenamePageCommand; + public ICommand EndRenamePageCommand + { + get + { + return this._endRenamePageCommand ?? (this._endRenamePageCommand = new DelegateCommand(para => this.EndRenamePageExecuted(para))); + } + } + + private ICommand _deletePageCommand; + public ICommand DeletePageCommand + { + get + { + return this._deletePageCommand ?? (this._deletePageCommand = new DelegateCommand(para => this.DeletePageExecuted(para))); + } + } + + private ICommand _addImageCommand; + public ICommand AddImageCommand + { + get + { + return this._addImageCommand ?? (this._addImageCommand = new DelegateCommand(para => this.AddImageExecuted(para))); + } + } + + private ICommand _editImageCommand; + public ICommand EditImageCommand + { + get + { + return this._editImageCommand ?? (this._editImageCommand = new DelegateCommand(para => this.EditImageExecuted(para))); + } + } + + private ICommand _resizeImageCommand; + public ICommand ResizeImageCommand + { + get + { + return this._resizeImageCommand ?? (this._resizeImageCommand = new DelegateCommand(para => this.ResizeImageExecuted(para))); + } + } + + private ICommand _resetImageCommand; + public ICommand ResetImageCommand + { + get + { + return this._resetImageCommand ?? (this._resetImageCommand = new DelegateCommand(para => this.ResetImageExecuted(para))); + } + } + + private ICommand _addVideoCommand; + public ICommand AddVideoCommand + { + get + { + return this._addVideoCommand ?? (this._addVideoCommand = new DelegateCommand(para => this.AddVideoExectued(para))); + } + } + + + private ICommand _addOutLineTextCommand; + public ICommand AddOutLineTextCommand + { + get + { + return this._addOutLineTextCommand ?? (this._addOutLineTextCommand = new DelegateCommand(para => this.AddOutLineTextExecuted(para))); + } + } + + private ICommand _addBarcodeCommand; + public ICommand AddBarcodeCommand + { + get + { + return this._addBarcodeCommand ?? (this._addBarcodeCommand = new DelegateCommand(para => this.AddBarcodeExecuted(para))); + } + } + #endregion + + #region 属性改变 private void DiagramViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { if (e.PropertyName == "IsSelected") { - _service.SelectedItems = DiagramViewModel?.SelectedItems; - - _service.SelectedItem = DiagramViewModel?.SelectedItem; + _service.SelectedItemViewModel = CopyHelper.Mapper(DiagramViewModel?.SelectedItem); } var property = sender.GetType().GetProperty(e.PropertyName); @@ -195,6 +305,7 @@ namespace AIStudio.Wpf.DiagramApp.ViewModels #endregion + #region 方法 protected virtual bool AddVerify(SelectableDesignerItemViewModelBase arg) { return true; @@ -356,84 +467,6 @@ namespace AIStudio.Wpf.DiagramApp.ViewModels return true; } - private bool ItemsToDeleteHasConnector(List itemsToRemove, ConnectorInfoBase connector) - { - if (connector is FullyCreatedConnectorInfo fully) - { - return itemsToRemove.Contains(fully.DataItem); - } - - return false; - } - - #region 主题 - public void SetPropertyValue(SelectableDesignerItemViewModelBase selectable, string propertyName) - { - foreach (var item in DiagramViewModel.SelectedItems) - { - if (item != selectable) - { - CopyHelper.CopyPropertyValue(selectable, item, propertyName); - } - } - } - - public void SetFont(IFontViewModel fontViewModel, string propertyName) - { - foreach (var item in DiagramViewModel.SelectedItems) - { - if (item.FontViewModel != fontViewModel) - { - CopyHelper.CopyPropertyValue(fontViewModel, item.FontViewModel, propertyName); - } - } - } - - public void SetColor(IColorViewModel colorViewModel, string propertyName) - { - foreach (var item in DiagramViewModel.SelectedItems) - { - if (item.ColorViewModel != colorViewModel) - { - CopyHelper.CopyPropertyValue(colorViewModel, item.ColorViewModel, propertyName); - } - } - } - - public void SetSharp(IShapeViewModel shapeViewModel, string propertyName) - { - foreach (var item in DiagramViewModel.SelectedItems) - { - if (item.ShapeViewModel != shapeViewModel) - { - CopyHelper.CopyPropertyValue(shapeViewModel, item.ShapeViewModel, propertyName); - } - } - } - - public void SetQuickItem(IQuickThemeViewModel quickThemeViewModel, string propertyName) - { - if (propertyName == nameof(QuickTheme) && quickThemeViewModel.QuickTheme != null) - { - foreach (var item in DiagramViewModel.SelectedItems) - { - SetFont(quickThemeViewModel.QuickTheme.FontViewModel, "FontColor"); - SetColor(quickThemeViewModel.QuickTheme.ColorViewModel, "FillColor"); - SetColor(quickThemeViewModel.QuickTheme.ColorViewModel, "LineColor"); - SetColor(quickThemeViewModel.QuickTheme.ColorViewModel, "LineWidth"); - } - quickThemeViewModel.QuickTheme = null; - } - } - - public void LockAction(LockObject lockObject, string propertyName) - { - foreach (var item in DiagramViewModel?.SelectedItems) - { - item.LockObjectViewModel.SetValue(lockObject); - } - } - public virtual void AddPageExecuted(object para) { int index = 0; @@ -550,6 +583,11 @@ namespace AIStudio.Wpf.DiagramApp.ViewModels public void EditImageExecuted(object para) { + if (para == null) + { + para = DiagramViewModel.SelectedItem; + } + ImageItemViewModel itemBase = para as ImageItemViewModel; if (itemBase != null) { @@ -559,6 +597,11 @@ namespace AIStudio.Wpf.DiagramApp.ViewModels public void ResizeImageExecuted(object para) { + if (para == null) + { + para = DiagramViewModel.SelectedItem; + } + ImageItemViewModel itemBase = para as ImageItemViewModel; if (itemBase != null) { @@ -568,6 +611,10 @@ namespace AIStudio.Wpf.DiagramApp.ViewModels public void ResetImageExecuted(object para) { + if (para == null) + { + para = DiagramViewModel.SelectedItem; + } ImageItemViewModel itemBase = para as ImageItemViewModel; if (itemBase != null) { @@ -575,7 +622,7 @@ namespace AIStudio.Wpf.DiagramApp.ViewModels } } - public void AddVideoExecuted(object para) + public void AddVideoExectued(object para) { VideoItemViewModel itemBase = new VideoItemViewModel(); DiagramViewModel?.AddItemCommand.Execute(itemBase); @@ -604,20 +651,7 @@ namespace AIStudio.Wpf.DiagramApp.ViewModels _service.DrawModeViewModel.CursorMode = CursorMode.Move; } } - #endregion - - private Size MeasureString(OutLineTextDesignerItemViewModel itemBase) - { - var formattedText = new FormattedText( - itemBase.Text, - CultureInfo.CurrentUICulture, - FlowDirection.LeftToRight, - new Typeface(new FontFamily(itemBase.FontViewModel.FontFamily), itemBase.FontViewModel.FontStyle, itemBase.FontViewModel.FontWeight, itemBase.FontViewModel.FontStretch), - itemBase.FontViewModel.FontSize, - Brushes.Black); - - return new Size(formattedText.Width, formattedText.Height); - } + #endregion public virtual void Dispose() { diff --git a/AIStudio.Wpf.DiagramApp/ViewModels/ToolBoxViewModel.cs b/AIStudio.Wpf.DiagramApp/ViewModels/ToolBoxViewModel.cs index a7bae62..252deb2 100644 --- a/AIStudio.Wpf.DiagramApp/ViewModels/ToolBoxViewModel.cs +++ b/AIStudio.Wpf.DiagramApp/ViewModels/ToolBoxViewModel.cs @@ -28,15 +28,11 @@ namespace AIStudio.Wpf.DiagramApp.ViewModels { public class ToolBoxViewModel : BindableBase { - private IDiagramServiceProvider _service - { - get - { - return DiagramServicesProvider.Instance.Provider; - } - } - public ToolBoxViewModel() + private MainWindowViewModel _mainWindowViewModel; + + public ToolBoxViewModel(MainWindowViewModel mainWindowViewModel) { + _mainWindowViewModel = mainWindowViewModel; Init(); } @@ -298,14 +294,14 @@ namespace AIStudio.Wpf.DiagramApp.ViewModels private void AddMyItem() { - if (_service.SelectedItems != null) + if (_mainWindowViewModel?.PageViewModel?.DiagramViewModel != null) { DiagramDocument diagramDocument = new DiagramDocument(); diagramDocument.DiagramItems = new List(); DiagramItem diagramItem = new DiagramItem(); - var selectedDesignerItems = _service.SelectedItems.OfType(); - var selectedConnections = _service.SelectedItems.OfType(); + var selectedDesignerItems = _mainWindowViewModel?.PageViewModel?.DiagramViewModel.SelectedItems.OfType(); + var selectedConnections = _mainWindowViewModel?.PageViewModel?.DiagramViewModel.SelectedItems.OfType(); 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(); diff --git a/AIStudio.Wpf.DiagramApp/Views/MainWindow.xaml b/AIStudio.Wpf.DiagramApp/Views/MainWindow.xaml index 4ebda94..a4cba06 100644 --- a/AIStudio.Wpf.DiagramApp/Views/MainWindow.xaml +++ b/AIStudio.Wpf.DiagramApp/Views/MainWindow.xaml @@ -1020,21 +1020,21 @@ - + - - - - + @@ -1045,19 +1045,19 @@ - - - - @@ -1328,7 +1328,7 @@ ItemWidth="45" ItemHeight="56" ItemsSource="{Binding QuickThemeViewModel.QuickThemes}" - SelectedItem="{Binding QuickThemeViewModel.QuickTheme}" + SelectedItem="{Binding QuickThemeViewModel.QuickTheme,Delay=100}" ResizeMode="Both" Selectable="False"> @@ -1349,12 +1349,12 @@ - + - - + + - + @@ -1392,7 +1392,7 @@ Width="190" /> - + @@ -1412,7 +1412,7 @@ Width="190" /> - + @@ -1468,7 +1468,7 @@ - + @@ -2523,20 +2523,20 @@ - - - - + + + + - + - +