123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436 |
- using ComPDFKitViewer;
- using ComPDFKitViewer.AnnotEvent;
- using PDF_Office.CustomControl.CompositeControl;
- 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.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
- {
- public class FreetextAnnotPropertyViewModel : BindableBase, INavigationAware
- {
- private FontBoardVm _fontVm = new FontBoardVm(true);
- public FontBoardVm FontVm
- {
- get { return _fontVm; }
- set { SetProperty(ref _fontVm, value); }
- }
- private AnnotBasePropertyVM _basicVm = new AnnotBasePropertyVM();
- public AnnotBasePropertyVM BasicVm
- {
- get { return _basicVm; }
- set => SetProperty(ref _basicVm, value);
- }
- public AnnotAttribEvent AnnotEvent { get; set; }
- private FreeTextAnnotArgs Annot;
- private AnnotPropertyPanel PropertyPanel;
- public DelegateCommand SelectedPresetFontCommand { get; set; }
- public DelegateCommand FontFamilyChangedCommand { get; set; }
- public DelegateCommand FontStyleWeightChangedCommand { get; set; }
- public DelegateCommand<object> SelectedFillOpacityCommand { get; set; }
-
- public DelegateCommand<object> SelectedColorCommand { get; set; }
- public DelegateCommand<object> SelectedFillColorCommand { get; set; }
-
- public DelegateCommand FontSizeChangedCommand { get; set; }
- public DelegateCommand<object> TextAlignCheckedCommand { get; set; }
- public DelegateCommand<object> LineModeCheckedCommand { get; set; }
- public DelegateCommand<object> SelectedOpacityValueCommand { get; set; }
- public DelegateCommand CustomFontStyleCommand { get; set; }
- public DelegateCommand ReDefineFontStyleCommand { get; set; }
- public DelegateCommand RestoreDefaultStyleCommand { get; set; }
- public event EventHandler<object> LoadPropertyHandler;
- public FreetextAnnotPropertyViewModel()
- {
- //文本设置
- //预设文本样式
- SelectedPresetFontCommand = new DelegateCommand(SelectedPresetFont);
- //文本字重、字体
- FontStyleWeightChangedCommand = new DelegateCommand(FontStyleWeightChanged);
- //字体
- FontFamilyChangedCommand = new DelegateCommand(FontFamilyChanged);
- //自定义
- CustomFontStyleCommand = new DelegateCommand(CustomFontStyle);
- //重新定义
- ReDefineFontStyleCommand = new DelegateCommand(ReDefineFontStyle);
- //恢复默认
- RestoreDefaultStyleCommand = new DelegateCommand(RestoreDefaultStyle);
- //颜色
- SelectedColorCommand = new DelegateCommand<object>(SelectedColor_Command);
- //填充颜色
- SelectedFillColorCommand = new DelegateCommand<object>(SelectedFillColorChange);
- //填充颜色不透明度
- SelectedFillOpacityCommand = new DelegateCommand<object>(SelectedFillOpacity);
- //大小
- FontSizeChangedCommand = new DelegateCommand(FontSizeChanged);
- //内容对齐
- TextAlignCheckedCommand = new DelegateCommand<object>(TextAlignChecked);
- //MVP不上
- LineModeCheckedCommand = new DelegateCommand<object>(LineMode_Checked);
- SelectedOpacityValueCommand = new DelegateCommand<object>(SelectedOpacityValue);
- InitVariable();
- }
- private void InitVariable()
- {
-
- }
-
- private void TextAlignChecked(object obj)
- {
- if ((string)obj != null)
- {
- switch ((string)obj)
- {
- case "AlignLeft":
- PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.TextAlign, TextAlignment.Left);
- break;
- case "AlignCenter":
- PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.TextAlign, TextAlignment.Center);
- break;
- case "AlignRight":
- PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.TextAlign, TextAlignment.Right);
- break;
- case "Justify":
- PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.TextAlign, TextAlignment.Justify);
- break;
- }
- }
- }
- private void LineMode_Checked(object obj)
- {
- if(obj != null)
- {
- var tag = ((string)obj);
- switch (tag)
- {
- case "Dashed":
- AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, DashStyles.Dash);
- break;
- case "Solid":
- AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, DashStyles.Solid);
- break;
- }
- AnnotEvent?.UpdateAnnot();
- }
- }
-
- //从预设样式设置文本
- private void SelectedPresetFont()
- {
- if (FontVm.CurrentPresetFont != null)
- {
- var itemComboxItem = FontVm.CurrentPresetFont;
- var item = FontVm.PresetFontList.FirstOrDefault(temp => temp.mTag == itemComboxItem.ValueStr);
- if (item != null)
- {
- FontVm.GetCurrentFontSize(item.mFontSize);
- FontVm.GetCurrentFontFamily(item.mFontFamily.ToString(), item.mFontFamily.ToString());
- FontVm.GetFontWeights_Style(item.mFontStyle, item.mFontWeight);
- Dictionary<AnnotAttrib, object> AnnotAttribDir = new Dictionary<AnnotAttrib, object>();
- AnnotAttribDir.Add(AnnotAttrib.FontSize, item.mFontSize);
- AnnotAttribDir.Add(AnnotAttrib.FontFamily, item.mFontFamily);
- AnnotAttribDir.Add(AnnotAttrib.FontStyle, item.mFontStyle);
- AnnotAttribDir.Add(AnnotAttrib.FontWeight, item.mFontWeight);
- PropertyPanel.UpdateAnnotAllAttribs(AnnotAttribDir);
- }
- }
- }
- //设置文本字重、样式
- private void FontStyleWeightChanged()
- {
- if(FontVm.CurrrentFontWeightStyle != null)
- {
- FontVm.UpdateFontWeight_Style();
- Dictionary<AnnotAttrib, object> AnnotAttribDir = new Dictionary<AnnotAttrib, object>();
- AnnotAttribDir.Add(AnnotAttrib.FontStyle, FontVm.FontStyleItem);
- AnnotAttribDir.Add(AnnotAttrib.FontWeight, FontVm.FontWeightItem);
- PropertyPanel.UpdateAnnotAllAttribs(AnnotAttribDir);
- }
- }
-
- private void SelectedFillOpacity(object obj)
- {
- if (obj != null)
- {
- BasicVm.FillOpacity = (double)obj;
- BasicVm.FillColor.Opacity = BasicVm.FillOpacity;
- AnnotEvent?.UpdateAttrib(AnnotAttrib.Transparency, BasicVm.FillOpacity);
- AnnotEvent?.UpdateAnnot();
- }
- }
- private void SelectedOpacityValue(object obj)
- {
- if (obj != null && obj is double)
- {
- BasicVm.FillOpacity = (double)obj;
- BasicVm.FillColor.Opacity = BasicVm.FillOpacity;
- AnnotEvent?.UpdateAttrib(AnnotAttrib.Transparency, BasicVm.FillOpacity);
- AnnotEvent?.UpdateAnnot();
- }
- }
-
- //设置字体大小
- private void FontSizeChanged()
- {
- if (FontVm.CurrentFontSize != null)
- {
- PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.FontSize, FontVm.CurrentFontSize.Value);
- }
- }
- //设置字体样式
- private void FontFamilyChanged()
- {
- if (string.IsNullOrEmpty(FontVm.CurrentFontFamily.ValueStr) == false)
- {
- PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.FontFamily, new FontFamily(FontVm.CurrentFontFamily.ValueStr));
- }
- }
- private void SelectedFillColorChange(object obj)
- {
- if (obj != null)
- {
- var colorValue = (Color)obj;
- if (colorValue != null)
- {
- BasicVm.FillColor = new SolidColorBrush(colorValue);
- PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.FillColor, colorValue);
- // BasicVm.FillColor.Opacity = BasicVmFillOpacity;
- }
- }
- }
- private void SelectedColor_Command(object obj)
- {
- if (obj != null)
- {
- var colorValue = (Color)obj;
- if (colorValue != null)
- {
- FontVm.FontColor = new SolidColorBrush(colorValue);
- AnnotEvent?.UpdateAttrib(AnnotAttrib.FontColor, colorValue);
- AnnotEvent?.UpdateAnnot();
- }
- }
- }
- private void CustomFontStyle()
- {
- if (FontVm.CurrentPresetFont != null)
- {
- ContextMenu menu;
- if (FontVm.CurrentPresetFont.ValueStr == "custom")
- {
- menu = SelectAnnotContextMenu(false);
- }
- else
- {
- menu = SelectAnnotContextMenu(true);
- }
- if (menu != null)
- {
- menu.IsOpen = true;
- }
- }
- }
- private ContextMenu SelectAnnotContextMenu(bool isEnable)
- {
- var popMenu = App.Current.FindResource("CustomFontStyleFlyoutMenu") as ContextMenu;
- if (popMenu != null && popMenu.Items.Count == 2)
- {
- //用所选部分重新定义
- MenuItem menuItem = popMenu.Items[0] as MenuItem;
- menuItem.IsEnabled = isEnable;
- menuItem.Command = ReDefineFontStyleCommand;
- //恢复默认预设样式
- menuItem = popMenu.Items[1] as MenuItem;
- menuItem.IsEnabled = isEnable;
- menuItem.Command = RestoreDefaultStyleCommand;
- }
- return popMenu;
- }
- public List<PresetFontItem> FontStyleList = new List<PresetFontItem>();
- private void ReDefineFontStyle()
- {
- var item = FontStyleList.FirstOrDefault(temp => temp.mTag == FontVm.CurrentPresetFont.ValueStr);
- if (FontVm.CurrentFontFamily.ValueStr == "Bold")
- {
- item.mFontStyle = FontStyles.Normal;
- item.mFontWeight = FontWeights.Bold;
- }
- else if (FontVm.CurrentFontFamily.ValueStr == "Regular")
- {
- item.mFontStyle = FontStyles.Normal;
- item.mFontWeight = FontWeights.Normal;
- }
- else
- {
- item.mFontStyle = FontStyles.Italic;
- item.mFontWeight = FontWeights.Bold;
- }
- item.mFontSize = (int)FontVm.CurrentFontSize.Value;
- }
- private void RestoreDefaultStyle()
- {
- // var item = FontStyleList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
- }
- 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 as FreeTextAnnotArgs;
- LoadPropertyHandler?.Invoke(this, Annot);
- BasicVm.IsMultiSelected = PropertyPanel.IsMultiSelected;
- if(BasicVm.IsMultiSelected)
- {
- bool isDifferentFontFamily = false;
- bool isDifferentFontSize = false;
- bool isDifferentFontStyle = false;
- FreeTextAnnotArgs tempAnnot = null;
- foreach (var item in PropertyPanel.annotlists)
- {
- var itemFreeText = item as FreeTextAnnotArgs;
- if(itemFreeText != null)
- {
- if (tempAnnot == null)
- {
- tempAnnot = itemFreeText;
- }
- else
- {
- if (itemFreeText.FontFamily != tempAnnot.FontFamily)
- {
- isDifferentFontFamily = true;
- }
- if (itemFreeText.FontSize != tempAnnot.FontSize)
- {
- isDifferentFontSize = true;
- }
- if (itemFreeText.FontStyle != tempAnnot.FontStyle || itemFreeText.FontWeight != tempAnnot.FontWeight)
- {
- isDifferentFontStyle = true;
- }
- tempAnnot = itemFreeText;
- }
-
- }
-
- }
- if (isDifferentFontFamily || isDifferentFontSize || isDifferentFontStyle)
- FontVm.CurrentPresetFont = null;
- if(isDifferentFontFamily)
- FontVm.CurrentFontFamily = null;
- if (isDifferentFontSize)
- FontVm.CurrentFontSize = null;
- if (isDifferentFontStyle)
- FontVm.CurrrentFontWeightStyle = null;
- BasicVm.SetStrDashStyle("None");
- }
- else
- {
- GetAnnotProperty();
- }
- }
- }
- private void GetAnnotProperty()
- {
- BasicVm.FillColor = new SolidColorBrush(Annot.BgColor);
- BasicVm.FillOpacity = Annot.Transparency;
- FontVm.FontColor = new SolidColorBrush(Annot.FontColor);
- FontVm.GetCurrentFontSize((int)Annot.FontSize);
- FontVm.GetCurrentFontFamily(Annot.FontFamily.ToString(), Annot.FontFamily.ToString());
- FontVm.GetFontWeights_Style(Annot.FontStyle, Annot.FontWeight);
- string str = "";
- if (Annot.Align == TextAlignment.Left)
- str = "AlignLeft";
- else if (Annot.Align == TextAlignment.Center)
- str = "AlignCenter";
- else if (Annot.Align == TextAlignment.Right)
- str = "AlignRight";
- else if(Annot.Align == TextAlignment.Justify)
- str = "AlignJustify";
- else
- str = "None";
- BasicVm.SetStrDashStyle(str);
- }
- }
- }
|