123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468 |
- using ComPDFKitViewer;
- using ComPDFKitViewer.AnnotEvent;
- using PDF_Office.CustomControl.CompositeControl;
- using PDF_Office.Helper;
- using PDF_Office.Model;
- using PDF_Office.Model.AnnotPanel;
- using PDF_Office.Model.PropertyPanel.AnnotPanel;
- using PDF_Office.ViewModels.Tools;
- using PDF_Office.ViewModels.Tools.AnnotManager;
- 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 void InitSting()
- {
- }
- #endregion
- #region 属性
- //手绘画笔橡皮擦的大小列表
- public List<ComboDataItem> PenSizeItems { get; protected set; }
- public List<ComboDataItem> EraserSizeItems { get; protected set; }
- private AnnotCommon _basicVm = new AnnotCommon();
- public AnnotCommon 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 = 10;
- public double EraseThicknessLine
- {
- get { return _erasethicknessLine; }
- set => SetProperty(ref _erasethicknessLine, value);
- }
- private DoubleCollection _strokeDashArray = new DoubleCollection();
- public DoubleCollection StrokeDashArray
- {
- get { return _strokeDashArray; }
- set => SetProperty(ref _strokeDashArray, value);
- }
- #endregion 属性
- public AnnotAttribEvent AnnotEvent { get; set; }
- private AnnotHandlerEventArgs Annot;
- private AnnotTransfer 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);
- InitSting();
- InitVariable();
- }
- private void InitVariable()
- {
- InitPenSizeItems();
- InitEraserSizeItems();
- InitColorItems();
- }
- private void InitPenSizeItems()
- {
- PenSizeItems = new List<ComboDataItem>();
- ComboDataItem item = new ComboDataItem(1, "pt");
- PenSizeItems.Add(item);
- item = new ComboDataItem(2, "pt");
- PenSizeItems.Add(item);
- item = new ComboDataItem(4, "pt");
- PenSizeItems.Add(item);
- item = new ComboDataItem(6, "pt");
- PenSizeItems.Add(item);
- item = new ComboDataItem(8, "pt");
- PenSizeItems.Add(item);
- }
- private void InitEraserSizeItems()
- {
- EraserSizeItems = new List<ComboDataItem>();
- ComboDataItem item = new ComboDataItem(5, "pt");
- EraserSizeItems.Add(item);
- item = new ComboDataItem(10, "pt");
- EraserSizeItems.Add(item);
- item = new ComboDataItem(15, "pt");
- EraserSizeItems.Add(item);
- item = new ComboDataItem(20, "pt");
- EraserSizeItems.Add(item);
-
- }
- private void InitColorItems()
- {
- BasicVm.ColorItems = AnnotColorList.GetColorList(ColorSelectorType.Border);
- }
- //设置颜色
- 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();
- StrokeDashArray = new DoubleCollection();
- switch (tag)
- {
- case "Dashed":
- newDash.Dashes.Add(2);
- newDash.Dashes.Add(2);
- PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.LineStyle, newDash);
- StrokeDashArray.Add(1);
- StrokeDashArray.Add(1);
- 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)
- {
- //属性面板,切换橡皮擦后,再回到画笔时,需要恢复最近画笔的属性值
- CurrenFreeHandAnnot = PropertyPanel.annot as FreehandAnnotArgs;
- PropertyPanel.InvokeToMyTools(AnnotArgsType.AnnotErase, btn);
- IsPen = false;
- }
- }
- private FreehandAnnotArgs CurrenFreeHandAnnot;//用来切换橡皮擦时,保存当前的手绘;
- //选择画笔
- private void Pen_Command(object obj)
- {
- var btn = obj as ToggleButton;
- if (btn.IsChecked == true)
- {
- PropertyPanel.InvokeToMyTools(AnnotArgsType.AnnotFreehand, CurrenFreeHandAnnot);
- IsPen = true;;
- if (CurrenFreeHandAnnot is FreehandAnnotArgs)
- {
- BasicVm.AnnotTypeTitle = "画笔";
- }
- else
- {
- BasicVm.AnnotTypeTitle = "橡皮擦";
- }
- }
- }
- public bool IsNavigationTarget(NavigationContext navigationContext)
- {
- return true;
- }
- public void OnNavigatedFrom(NavigationContext navigationContext)
- {
- BasicVm.IsMultiSelected = false;
- }
- public void OnNavigatedTo(NavigationContext navigationContext)
- {
- navigationContext.Parameters.TryGetValue<AnnotTransfer>(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.IsDashLine = BasicVm.IsSolidLine = false;
- }
- 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.IsSolidLine = isSolid;
- BasicVm.IsDashLine = !isSolid;
- }
- else
- {
- BasicVm.IsSolidLine = BasicVm.IsDashLine = false;
- }
- }
- }
- //外部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.IsSolidLine = isSolid;
- BasicVm.IsDashLine = !isSolid;
-
- IsPen = true;
- StrokeDashArray = new DoubleCollection();
- if (isSolid == false)
- {
- StrokeDashArray.Add(1);
- StrokeDashArray.Add(1);
- }
- }
- }
- if (Annot is EraseArgs)
- {
- var annot = Annot as EraseArgs;
- if (annot != null)
- {
- EraseThicknessLine = annot.Thickness;
- IsPen = false;
- }
- }
- }
- }
- }
|