Files
aistudio-wpf-diagram/AIStudio.Wpf.DiagramDesigner/ViewModels/BaseViewModel/SelectableViewModelBase.cs

281 lines
6.8 KiB
C#
Raw Normal View History

2021-07-23 09:42:22 +08:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Windows.Media;
using AIStudio.Wpf.DiagramDesigner.Models;
using Newtonsoft.Json;
2021-07-23 09:42:22 +08:00
2022-10-28 22:45:39 +08:00
namespace AIStudio.Wpf.DiagramDesigner
2021-07-23 09:42:22 +08:00
{
2023-01-24 09:02:40 +08:00
public abstract class SelectableViewModelBase : BindableBase, ISelectable
2021-07-23 09:42:22 +08:00
{
2023-01-24 09:02:40 +08:00
protected IDiagramServiceProvider _service
{
get
{
return DiagramServicesProvider.Instance.Provider;
}
}
2021-07-23 09:42:22 +08:00
2023-01-22 22:10:39 +08:00
public SelectableViewModelBase()
2021-07-23 09:42:22 +08:00
{
Init();
(FontViewModel as FontViewModel).PropertyChanged += FontViewModel_PropertyChanged;
}
2023-01-24 16:20:39 +08:00
public SelectableViewModelBase(IDiagramViewModel root, SelectableItemBase designer)
2021-07-23 09:42:22 +08:00
{
Init();
2023-01-24 16:20:39 +08:00
LoadDesignerItemViewModel(root, designer);
2021-07-23 09:42:22 +08:00
(FontViewModel as FontViewModel).PropertyChanged += FontViewModel_PropertyChanged;
}
2023-01-24 16:20:39 +08:00
public SelectableViewModelBase(IDiagramViewModel root, string json)
{
Init();
2023-01-24 16:20:39 +08:00
LoadDesignerItemViewModel(root, JsonConvert.DeserializeObject(json, ToXmlType()) as SelectableItemBase);
(FontViewModel as FontViewModel).PropertyChanged += FontViewModel_PropertyChanged;
}
2023-01-24 16:20:39 +08:00
public virtual SelectableItemBase ToXmlObject()
{
return null;
}
public virtual SerializableItem ToSerializabObject()
{
var obj = ToXmlObject();
if (obj != null)
{
return new SerializableItem() { TypeName = this.GetType().FullName, ObjectJson = obj.ToJson() };
}
else
{
return null;
}
}
public virtual Type ToXmlType()
{
return null;
}
2021-07-23 09:42:22 +08:00
protected virtual void Init()
{
ColorViewModel = _service.CopyDefaultColorViewModel();
FontViewModel = _service.CopyDefaultFontViewModel();
2021-07-23 09:42:22 +08:00
LockObjectViewModel = new LockObjectViewModel();
}
2023-01-24 16:20:39 +08:00
protected virtual void LoadDesignerItemViewModel(IDiagramViewModel root, SelectableItemBase designerbase)
2021-07-23 09:42:22 +08:00
{
2023-01-24 16:20:39 +08:00
this.Root = root;
2021-07-23 09:42:22 +08:00
this.Id = designerbase.Id;
this.ParentId = designerbase.ParentId;
this.IsGroup = designerbase.IsGroup;
this.ZIndex = designerbase.ZIndex;
this.Text = designerbase.Text;
ColorViewModel = CopyHelper.Mapper(designerbase.ColorItem);
FontViewModel = CopyHelper.Mapper<FontViewModel, FontItem>(designerbase.FontItem);
}
2023-01-24 16:20:39 +08:00
public IDiagramViewModel Root
{
get; set;
}
2023-01-24 16:20:39 +08:00
public SelectableViewModelBase Parent
{
get; set;
}
public Guid Id
{
get; set;
}
2021-07-23 09:42:22 +08:00
private Guid _parentId;
public Guid ParentId
{
get
{
return _parentId;
}
set
{
SetProperty(ref _parentId, value);
}
}
public bool IsGroup
{
get; set;
}
2021-07-23 09:42:22 +08:00
2023-01-24 09:02:40 +08:00
protected bool _isSelected;
2021-07-23 09:42:22 +08:00
[Browsable(false)]
2023-01-24 09:02:40 +08:00
public virtual bool IsSelected
2021-07-23 09:42:22 +08:00
{
get
{
return _isSelected;
}
set
{
2023-01-24 09:02:40 +08:00
SetProperty(ref _isSelected, value);
2021-07-23 09:42:22 +08:00
}
}
private int _zIndex;
[Browsable(true)]
[CanDo]
public int ZIndex
{
get
{
return _zIndex;
}
set
{
SetProperty(ref _zIndex, value);
}
}
private bool _isReadOnly;
[Browsable(false)]
public bool IsReadOnly
{
get
{
2023-01-24 16:20:39 +08:00
if (Root?.IsReadOnly == true && Root?.IsLoading == false)
{
return true;
}
if (Parent?.IsReadOnly == true)
2022-12-04 23:07:20 +08:00
{
return true;
}
2023-01-24 16:20:39 +08:00
2022-12-04 23:07:20 +08:00
if (LockObjectViewModel?.LockObject.FirstOrDefault(p => p.LockFlag == LockFlag.All)?.IsChecked == true)
2021-07-23 09:42:22 +08:00
{
return true;
}
return _isReadOnly;
}
set
{
SetProperty(ref _isReadOnly, value);
}
}
private bool _isHitTestVisible = true;
[Browsable(false)]
public bool IsHitTestVisible
{
get
{
2023-01-24 16:20:39 +08:00
if (Parent?.IsHitTestVisible == false)
{
return false;
}
2021-07-23 09:42:22 +08:00
return _isHitTestVisible;
}
set
2023-01-24 16:20:39 +08:00
{
2021-07-23 09:42:22 +08:00
if (SetProperty(ref _isHitTestVisible, value))
{
RaisePropertyChanged("IsReadOnly");
}
}
}
private IColorViewModel _colorViewModel;
public IColorViewModel ColorViewModel
{
get
{
return _colorViewModel;
}
set
{
SetProperty(ref _colorViewModel, value);
}
}
private IFontViewModel _fontViewModel;
public IFontViewModel FontViewModel
{
get
{
return _fontViewModel;
}
set
{
SetProperty(ref _fontViewModel, value);
}
}
public ILockObjectViewModel LockObjectViewModel
{
get; set;
}
2021-07-23 09:42:22 +08:00
private string _text;
[Browsable(true)]
[CanDo]
2023-01-24 09:02:40 +08:00
public virtual string Text
2021-07-23 09:42:22 +08:00
{
get
{
2021-07-27 21:58:55 +08:00
var text = _text;
2021-07-23 09:42:22 +08:00
if (FontViewModel.FontCase == FontCase.Upper)
{
2021-07-27 21:58:55 +08:00
return text?.ToUpper();
2021-07-23 09:42:22 +08:00
}
else if (FontViewModel.FontCase == FontCase.Lower)
{
2021-07-27 21:58:55 +08:00
return text?.ToLower();
2021-07-23 09:42:22 +08:00
}
else
{
2021-07-27 21:58:55 +08:00
return text;
2021-07-23 09:42:22 +08:00
}
}
set
{
2023-01-24 09:02:40 +08:00
if (SetProperty(ref _text, value))
2021-07-27 21:58:55 +08:00
{
2021-07-23 09:42:22 +08:00
}
}
}
2023-01-24 09:02:40 +08:00
public virtual void AddToSelection(bool selected)
2023-01-22 21:46:59 +08:00
{
}
2021-07-23 09:42:22 +08:00
private void FontViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == "FontCase")
{
RaisePropertyChanged("Text");
}
}
public virtual void Dispose()
{
}
}
}