using ComPDFKitViewer; using ComPDFKitViewer.AnnotEvent; using PDF_Office.Model; using PDF_Office.Model.PropertyPanel.AnnotPanel; using PDF_Office.ViewModels.Tools; using Prism.Commands; using Prism.Mvvm; using Prism.Regions; using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Data; using System.Windows.Media; namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel { public class EraseThicknessConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value is double) { return (double)value * 6; } return value; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } public class FreehandAnnotPropertyViewModel : BindableBase, INavigationAware { #region 属性 private AnnotBasePropertyVM _basicVm = new AnnotBasePropertyVM(); public AnnotBasePropertyVM BasicVm { get { return _basicVm; } set => SetProperty(ref _basicVm, value); } private bool _isPen = true; public bool IsPen { get { return _isPen; } set => SetProperty(ref _isPen, value); } private double _erasethicknessLine = 1; public double EraseThicknessLine { get { return _erasethicknessLine; } set => SetProperty(ref _erasethicknessLine, value); } #endregion 属性 public AnnotAttribEvent AnnotEvent { get; set; } private AnnotHandlerEventArgs Annot; private AnnotPropertyPanel PropertyPanel; public DelegateCommand EraseCommand { get; set; } public DelegateCommand PenCommand { get; set; } public DelegateCommand SelectedColorChangedCommand { get; set; } public DelegateCommand SelectPenThickChangedCommand { get; set; } public DelegateCommand SetEraserThickCommand { get; set; } public DelegateCommand LineModeCheckedCommand { get; set; } public DelegateCommand SelectedOpacityValueCommand { get; set; } public FreehandAnnotPropertyViewModel() { EraseCommand = new DelegateCommand(Erase_Command); PenCommand = new DelegateCommand(Pen_Command); SelectedColorChangedCommand = new DelegateCommand(SelectedColorChanged); SelectPenThickChangedCommand = new DelegateCommand(SelectPenThickChanged); SetEraserThickCommand = new DelegateCommand(SelectEraserThickChanged); LineModeCheckedCommand = new DelegateCommand(LineMode_Checked); SelectedOpacityValueCommand = new DelegateCommand(SelectedOpacityValue); InitVariable(); } private void InitVariable() { } //设置颜色 private void SelectedColorChanged(object obj) { if (obj != null) { var colorValue = (Color)obj; if (colorValue != null) { if (BasicVm.IsMultiSelected) { foreach (var item in PropertyPanel.AnnotEvents) { item?.UpdateAttrib(AnnotAttrib.Color, colorValue); item?.UpdateAnnot(); } } else { BasicVm.FontColor = new SolidColorBrush(colorValue); BasicVm.FontColor.Opacity = BasicVm.FillOpacity; AnnotEvent?.UpdateAttrib(AnnotAttrib.Color, colorValue); AnnotEvent?.UpdateAnnot(); PropertyPanel.InvokeToMyTools(AnnotArgsType.AnnotFreehand, colorValue); } } } } //设置线条大小 private void SelectPenThickChanged(object obj) { if (obj != null && obj is double) { var thick = (double)obj; if (BasicVm.IsMultiSelected) { foreach (var itemEvent in PropertyPanel.AnnotEvents) { itemEvent?.UpdateAttrib(AnnotAttrib.Thickness, thick); itemEvent?.UpdateAnnot(); } } else { AnnotEvent?.UpdateAttrib(AnnotAttrib.Thickness, thick); AnnotEvent?.UpdateAnnot(); } } } //设置橡皮擦大小 private void SelectEraserThickChanged(object obj) { if (obj != null && obj is double) { var eraser = (double)obj; AnnotEvent?.UpdateAttrib(AnnotAttrib.Thickness, eraser); AnnotEvent?.UpdateAnnot(); } } //设置线条样式 private void LineMode_Checked(object obj) { if (obj != null) { var tag = ((string)obj); DashStyle newDash = new DashStyle(); switch (tag) { case "Dashed": newDash.Dashes.Add(2); newDash.Dashes.Add(2); // AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, DashStyles.Dash); AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, newDash); break; case "Solid": AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, DashStyles.Solid); break; } AnnotEvent?.UpdateAnnot(); } } //设置不透明度 private void SelectedOpacityValue(object obj) { if (obj != null) { BasicVm.FillOpacity = (double)obj; BasicVm.FontColor.Opacity = BasicVm.FillOpacity; AnnotEvent?.UpdateAttrib(AnnotAttrib.Transparency, BasicVm.FillOpacity); AnnotEvent?.UpdateAnnot(); PropertyPanel.InvokeToMyTools(AnnotArgsType.AnnotFreehand, BasicVm.FillOpacity); } } //选择橡皮擦 private void Erase_Command(object obj) { var btn = obj as ToggleButton; if(btn.IsChecked == true) { PropertyPanel.InvokeToMyTools(AnnotArgsType.AnnotErase, btn); IsPen = false; } } //选择画笔 private void Pen_Command(object obj) { var btn = obj as ToggleButton; if (btn.IsChecked == true) { PropertyPanel.InvokeToMyTools(AnnotArgsType.AnnotErase, btn); IsPen = true; } } public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { BasicVm.IsMultiSelected = false; } public void OnNavigatedTo(NavigationContext navigationContext) { navigationContext.Parameters.TryGetValue(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel); if (PropertyPanel != null) { AnnotEvent = PropertyPanel.AnnotEvent; Annot = PropertyPanel.annot; if (PropertyPanel.annotlists != null && PropertyPanel.annotlists.Count > 1) { BasicVm.IsMultiSelected = true; } else { BasicVm.IsMultiSelected = false; } if (BasicVm.IsMultiSelected) { double thickness = 0; foreach(var item in PropertyPanel.annotlists) { if ((item as FreehandAnnotArgs).LineWidth >= thickness) thickness = (item as FreehandAnnotArgs).LineWidth; } BasicVm.AnnotThickness = thickness; BasicVm.SetStrDashStyle("None"); } else { GetAnnotProperty(); } } } private void GetAnnotProperty() { if (Annot is FreehandAnnotArgs) { var annot = Annot as FreehandAnnotArgs; if (annot != null) { BasicVm.FillOpacity = annot.Transparency; BasicVm.FontColor = new SolidColorBrush(annot.InkColor); BasicVm.AnnotThickness = annot.LineWidth; BasicVm.Dash = annot.LineDash; bool isSolid = true; if(annot.LineDash != null && annot.LineDash.Dashes.Count > 0) { foreach(var item in annot.LineDash.Dashes) { if(item > 0) { isSolid = false; break; } } } BasicVm.SetStrDashStyle(isSolid ? "Solid" : "Dash"); IsPen = true; } } if (Annot is EraseArgs) { var annot = Annot as EraseArgs; if (annot != null) { EraseThicknessLine = annot.Thickness; IsPen = false; } } } } }