123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386 |
- using ComPDFKitViewer;
- using ComPDFKitViewer.AnnotEvent;
- using PDF_Office.Helper;
- using PDF_Office.Model;
- using PDF_Office.Model.PropertyPanel.AnnotPanel;
- using PDF_Office.ViewModels.Tools;
- using PDFSettings;
- 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<object> EraseCommand { get; set; }
- public DelegateCommand<object> PenCommand { get; set; }
- public DelegateCommand<object> SelectedColorChangedCommand { get; set; }
- public DelegateCommand<object> SelectPenThickChangedCommand { get; set; }
- public DelegateCommand<object> SetEraserThickCommand { get; set; }
- public DelegateCommand<object> LineModeCheckedCommand { get; set; }
- public DelegateCommand<object> SelectedOpacityValueCommand { get; set; }
- public FreehandAnnotPropertyViewModel()
- {
- EraseCommand = new DelegateCommand<object>(Erase_Command);
- PenCommand = new DelegateCommand<object>(Pen_Command);
- SelectedColorChangedCommand = new DelegateCommand<object>(SelectedColorChanged);
- SelectPenThickChangedCommand = new DelegateCommand<object>(SelectPenThickChanged);
- SetEraserThickCommand = new DelegateCommand<object>(SelectEraserThickChanged);
- LineModeCheckedCommand = new DelegateCommand<object>(LineMode_Checked);
- SelectedOpacityValueCommand = new DelegateCommand<object>(SelectedOpacityValue);
- InitVariable();
- }
- private void InitVariable()
- {
- }
- //设置颜色
- private void SelectedColorChanged(object obj)
- {
- if (obj != null)
- {
- var colorValue = (Color)obj;
- if (colorValue != null)
- {
- BasicVm.FontColor = new SolidColorBrush(colorValue);
- BasicVm.FontColor.Opacity = BasicVm.FillOpacity;
- PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Color, colorValue);
- if (BasicVm.IsMultiSelected == false)
- {
- PropertyPanel.InvokeToMyTools(AnnotArgsType.AnnotFreehand, colorValue);
- }
- }
- }
- }
- //设置线条大小
- private void SelectPenThickChanged(object obj)
- {
- if (obj != null && obj is double)
- {
- var thick = (double)obj;
- PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Thickness, thick);
- }
- }
- //设置橡皮擦大小
- 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);
- PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.LineStyle, newDash);
-
- break;
- case "Solid":
- PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.LineStyle, DashStyles.Solid);
- break;
- }
-
- AnnotEvent?.UpdateAnnot();
- }
- }
- //设置不透明度
- private void SelectedOpacityValue(object obj)
- {
- if (obj != null)
- {
- BasicVm.FillOpacity = (double)obj;
- BasicVm.FontColor.Opacity = BasicVm.FillOpacity;
- PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Transparency, BasicVm.FillOpacity);
- 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<AnnotPropertyPanel>(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel);
- if (PropertyPanel != null)
- {
- AnnotEvent = PropertyPanel.AnnotEvent;
- Annot = PropertyPanel.annot;
- BasicVm.IsMultiSelected = PropertyPanel.IsMultiSelected;
- if(Annot is FreehandAnnotArgs)
- {
- BasicVm.AnnotTypeTitle = "画笔";
- }
- else
- {
- BasicVm.AnnotTypeTitle = "橡皮擦";
- }
- if (BasicVm.IsMultiSelected)
- {
- IsAttributeEquals();
- BasicVm.SetStrDashStyle("None");
- }
- else
- {
- GetAnnotProperty();
- }
-
- }
- }
- private List<FreehandAnnotArgs> ConvertLists()
- {
- List<FreehandAnnotArgs> Lists = new List<FreehandAnnotArgs>();
- foreach (var item in PropertyPanel.annotlists)
- {
- var selecteditem = item as FreehandAnnotArgs;
- if (selecteditem != null)
- {
- Lists.Add(selecteditem);
- }
- }
- if (Lists.Count != PropertyPanel.annotlists.Count)
- return null;
- else
- return Lists;
- }
- private void IsAttributeEquals()
- {
- var list = ConvertLists();
- if (list != null)
- {
- var temp = list[0];
- Dictionary<string, bool> isNoEqualsDir = new Dictionary<string, bool>();
- isNoEqualsDir.Add("Color", false);
- isNoEqualsDir.Add("Thickness", false);
- isNoEqualsDir.Add("FontStyleFontWeight", false);
- isNoEqualsDir.Add("ThickSolidDashStyle", false);
- foreach (var item in list)
- {
- if (item == list[0])
- continue;
- if (isNoEqualsDir["Color"] == false)
- {
- if (temp.InkColor.A != item.InkColor.A || temp.InkColor.R != item.InkColor.R || temp.InkColor.G != item.InkColor.G || temp.InkColor.B != item.InkColor.B)
- {
- BasicVm.FontColor = new SolidColorBrush(Colors.Transparent);
- isNoEqualsDir["Color"] = true;
- }
- }
- if (isNoEqualsDir["Thickness"] == false)
- {
- isNoEqualsDir["Thickness"] = true;
- if (temp.LineWidth > item.LineWidth)
- {
- BasicVm.AnnotThickness = temp.LineWidth;
- }
- else
- {
- BasicVm.AnnotThickness = item.LineWidth;
- }
- }
- if(isNoEqualsDir["ThickSolidDashStyle"] == false)
- {
- if(isSolidStyle(temp) != isSolidStyle(item))
- {
- isNoEqualsDir["ThickSolidDashStyle"] = true;
- }
- }
- }
- if (isNoEqualsDir["Color"] == false)
- {
- BasicVm.FontColor = new SolidColorBrush(temp.InkColor);
- }
- if (isNoEqualsDir["Thickness"] == false)
- {
- BasicVm.AnnotThickness = temp.LineWidth;
- }
- if(isNoEqualsDir["ThickSolidDashStyle"] == true)
- {
- var isSolid = isSolidStyle(temp);
- BasicVm.SetStrDashStyle(isSolid ? "Solid" : "Dash");
- }
- else
- {
- BasicVm.SetStrDashStyle("None");
- }
- }
- }
- //外部UI控件选中状态
- private bool isSolidStyle(FreehandAnnotArgs annot)
- {
- 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;
- }
- }
- }
- return isSolid;
-
- }
- 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;
- var isSolid = isSolidStyle(annot);
- BasicVm.SetStrDashStyle(isSolid ? "Solid" : "Dash");
- IsPen = true;
- }
- }
- if (Annot is EraseArgs)
- {
- var annot = Annot as EraseArgs;
- if (annot != null)
- {
- EraseThicknessLine = annot.Thickness;
- IsPen = false;
- }
- }
- }
- }
- }
|