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 { private bool isPen = true; public bool IsPen { get { return isPen; } set { SetProperty(ref isPen, value); } } private Brush selectColor = new SolidColorBrush(Colors.Transparent); public Brush SelectColor { get { return selectColor; } set { SetProperty(ref selectColor, value); AnnotEvent?.UpdateAttrib(AnnotAttrib.Color, (SelectColor as SolidColorBrush).Color); AnnotEvent?.UpdateAnnot(); } } private double annotOpacity = 1; public double AnnotOpacity { get { return annotOpacity; } set { SetProperty(ref annotOpacity, value); } } bool isCanSave = false; private double thicknessLine = 1; public double ThicknessLine { get { return thicknessLine; } set { SetProperty(ref thicknessLine, value); if(isCanSave) { AnnotEvent?.UpdateAttrib(AnnotAttrib.Thickness, thicknessLine); AnnotEvent?.UpdateAnnot(); } } } private double erasethicknessLine = 1; public double EraseThicknessLine { get { return erasethicknessLine; } set { SetProperty(ref erasethicknessLine, value); AnnotEvent?.UpdateAttrib(AnnotAttrib.Thickness, erasethicknessLine); AnnotEvent?.UpdateAnnot(); } } private bool _isMultiSelected = false; public bool IsMultiSelected { get { return _isMultiSelected; } set => SetProperty(ref _isMultiSelected, value); } 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 event EventHandler LoadPropertyHandler; public FreehandAnnotPropertyViewModel() { EraseCommand = new DelegateCommand(Erase_Command); PenCommand = new DelegateCommand(Pen_Command); SelectedColorChangedCommand = new DelegateCommand(SelectedColorChanged_Click); SelectPenThickChangedCommand = new DelegateCommand(SelectPenThickChanged_Command); SetEraserThickCommand = new DelegateCommand(SelectEraserThickChanged_Command); LineModeCheckedCommand = new DelegateCommand(LineMode_Checked); SelectedOpacityValueCommand = new DelegateCommand(SelectedOpacityValue); InitVariable(); } private void InitVariable() { } private void SelectEraserThickChanged_Command(object obj) { if (obj != null) { var item = (ComboBoxItem)obj; var content = (string)item.Content; if (content != null) { var intData = double.Parse(content); AnnotEvent?.UpdateAttrib(AnnotAttrib.Thickness, intData); AnnotEvent?.UpdateAnnot(); EraseThicknessLine = intData; } } } 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, newDash); break; } AnnotEvent?.UpdateAnnot(); } } private void SelectPenThickChanged_Command(object obj) { if (obj != null) { var item = (ComboBoxItem)obj; var content = (string)item.Content; if (content != null) { var intData = double.Parse(content); if (IsMultiSelected) { foreach (var itemEvent in PropertyPanel.AnnotEvents) { itemEvent?.UpdateAttrib(AnnotAttrib.Thickness, intData); itemEvent?.UpdateAnnot(); } } else { AnnotEvent?.UpdateAttrib(AnnotAttrib.Thickness, intData); AnnotEvent?.UpdateAnnot(); } ThicknessLine = intData; } } } private void SelectedOpacityValue(object obj) { if (obj != null) { annotOpacity = (double)obj; SelectColor.Opacity = annotOpacity; AnnotEvent?.UpdateAttrib(AnnotAttrib.Transparency, annotOpacity); AnnotEvent?.UpdateAnnot(); PropertyPanel.InvokeToMyTools(AnnotArgsType.AnnotFreehand, annotOpacity); } } private void SelectedColorChanged_Click(object obj) { if (obj != null) { var colorValue = (Color)obj; if (colorValue != null) { if (IsMultiSelected) { foreach (var item in PropertyPanel.AnnotEvents) { item?.UpdateAttrib(AnnotAttrib.Color, colorValue); item?.UpdateAnnot(); } } else { SelectColor = new SolidColorBrush(colorValue); SelectColor.Opacity = AnnotOpacity; PropertyPanel.InvokeToMyTools(AnnotArgsType.AnnotFreehand, obj); } } } } 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) { IsMultiSelected = false; isCanSave = 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) { IsMultiSelected = true; } else { IsMultiSelected = false; } if (IsMultiSelected) { double thickness = 0; foreach(var item in PropertyPanel.annotlists) { if ((item as FreehandAnnotArgs).LineWidth >= thickness) thickness = (item as FreehandAnnotArgs).LineWidth; } ThicknessLine = thickness; } else { GetAnnotProperty(); if (Annot is FreehandAnnotArgs) LoadPropertyHandler?.Invoke(null, (Annot as FreehandAnnotArgs).InkColor); } } isCanSave = true; } private void GetAnnotProperty() { if (Annot is FreehandAnnotArgs) { var annot = Annot as FreehandAnnotArgs; if (annot != null) { AnnotOpacity = annot.Transparency; SelectColor = new SolidColorBrush(annot.InkColor); ThicknessLine = annot.LineWidth; IsPen = true; } } if (Annot is EraseArgs) { var annot = Annot as EraseArgs; if (annot != null) { EraseThicknessLine = annot.Thickness; IsPen = false; } } } } }