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 : FontBoard, INavigationAware { private bool isSelectedEmpty; public bool IsSelectedEmpty { get { return isSelectedEmpty; } set { SetProperty(ref isSelectedEmpty, value); } } private double fillOpacity = 1; public double FillOpacity { get { return fillOpacity; } set { SetProperty(ref fillOpacity, value); } } private Brush fillColor = new SolidColorBrush(Colors.Transparent); public Brush FillColor { get { return fillColor; } set { SetProperty(ref fillColor, value); AnnotEvent?.UpdateAttrib(AnnotAttrib.FillColor, (fillColor as SolidColorBrush).Color); AnnotEvent?.UpdateAnnot(); } } //private FontFamily fontFamily = new FontFamily("Courier"); //public FontFamily TextFontFamily //{ // get { return fontFamily; } // set // { // SetProperty(ref fontFamily, value); // } //} //private FontWeight fontWeights = FontWeights.Normal; //public FontWeight TextFontWeights //{ // get { return fontWeights; } // set // { // SetProperty(ref fontWeights, value); // } //} //private FontStyle fontStyle = FontStyles.Normal; //public FontStyle TextFontStyle //{ // get { return fontStyle; } // set // { // SetProperty(ref fontStyle, value); // } //} //private ComboDataItem _fontWeightStyleItem; //public ComboDataItem FontWeightStyleItem //{ // get { return _fontWeightStyleItem; } // set // { // SetProperty(ref _fontWeightStyleItem, value); // if (_fontWeightStyleItem.ValueStr != null && string.IsNullOrEmpty((string)_fontWeightStyleItem.ValueStr) == false) // { // switch ((string)_fontWeightStyleItem.ValueStr) // { // case "Regular": // FontStyleItem = FontStyles.Normal; // FontWeightItem = FontWeights.Normal; // break; // case "Bold": // FontStyleItem = FontStyles.Normal; // FontWeightItem = FontWeights.Bold; // break; // case "Italic": // FontStyleItem = FontStyles.Italic; // FontWeightItem = FontWeights.Normal; // break; // case "Bold Italic": // FontStyleItem = FontStyles.Italic; // FontWeightItem = FontWeights.Bold; // break; // } // } // } //} public DelegateCommand SelectedFillOpacityCommand { get; set; } public DelegateCommand SelectedFontStyleCommand { get; set; } public DelegateCommand SelectedColorCommand { get; set; } public DelegateCommand SelectedFillColorCommand { get; set; } public DelegateCommand FontFamilyChangedCommand { get; set; } public DelegateCommand FontStyleChangedCommand { get; set; } public DelegateCommand FontSizeChangedCommand { get; set; } public DelegateCommand TextAlignCheckedCommand { get; set; } public DelegateCommand LineModeCheckedCommand { get; set; } public DelegateCommand SelectedOpacityValueCommand { get; set; } public DelegateCommand CustomFontStyleCommand { get; set; } public DelegateCommand ReDefineFontStyleCommand { get; set; } public DelegateCommand RestoreDefaultStyleCommand { get; set; } public event EventHandler LoadPropertyHandler; public FreetextAnnotPropertyViewModel() { SelectedFillOpacityCommand = new DelegateCommand(SelectedFillOpacity); SelectedFontStyleCommand = new DelegateCommand(SelectedFontStyle); SelectedColorCommand = new DelegateCommand(SelectedColor_Command); SelectedFillColorCommand = new DelegateCommand(SelectedFillColor_Command); FontFamilyChangedCommand = new DelegateCommand(FontFamilyChanged_Command); FontStyleChangedCommand = new DelegateCommand(FontStyleChanged_Command); FontSizeChangedCommand = new DelegateCommand(FontSizeChanged_Command); TextAlignCheckedCommand = new DelegateCommand(TextAlignChecked); LineModeCheckedCommand = new DelegateCommand(LineMode_Checked); SelectedOpacityValueCommand = new DelegateCommand(SelectedOpacityValue); CustomFontStyleCommand = new DelegateCommand(CustomFontStyle); ReDefineFontStyleCommand = new DelegateCommand(ReDefineFontStyle); RestoreDefaultStyleCommand = new DelegateCommand(RestoreDefaultStyle); InitVariable(); } private void InitVariable() { InitFontStyles(); InitFontFamilyComboBox(); InitFontStyleComboBox(); } private void InitFontFamilyComboBox() { FontFamilyItems = TextFont.GetFamily(); } private void InitFontStyleComboBox() { FontStyleItems = TextFont.GetFontStyle(); } private void InitFontStyles() { PresetFontItems = new List(); PresetFontList = TextFont.GetCachePresetFontList(); foreach (var item in PresetFontList) { ComboDataItem itemData = new ComboDataItem(item.mTag, item.mTagContent); PresetFontItems.Add(itemData); } } private void TextAlignChecked(object obj) { if ((string)obj != null /*&& TextEditEvent != null*/) { switch ((string)obj) { case "AlignLeft": AnnotEvent?.UpdateAttrib(AnnotAttrib.TextAlign, TextAlignment.Left); break; case "AlignCenter": AnnotEvent?.UpdateAttrib(AnnotAttrib.TextAlign, TextAlignment.Center); break; case "AlignRight": AnnotEvent?.UpdateAttrib(AnnotAttrib.TextAlign, TextAlignment.Right); break; case "Align": break; } AnnotEvent?.UpdateAnnot(); } } 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 SelectedFontStyle(object obj) { if (obj != null && (PresetFontItem)obj != null) { var item = (PresetFontItem)obj; AnnotEvent?.UpdateAttrib(AnnotAttrib.FontSize, item.mFontSize); AnnotEvent?.UpdateAnnot(); } } private void SelectedFillOpacity(object obj) { if (obj != null) { FillOpacity = (double)obj; SelectColor.Opacity = FillOpacity; AnnotEvent?.UpdateAttrib(AnnotAttrib.Transparency, FillOpacity); AnnotEvent?.UpdateAnnot(); } } private void SelectedOpacityValue(object obj) { if (obj != null) { FillOpacity = (double)obj; SelectColor.Opacity = FillOpacity; AnnotEvent?.UpdateAttrib(AnnotAttrib.Transparency, FillOpacity); AnnotEvent?.UpdateAnnot(); } } private void FontSizeChanged_Command(object obj) { if (obj != null) { var item = (ComboBoxItem)obj; var content = (string)item.Content; if (content != null) { var intData = int.Parse(content); AnnotEvent?.UpdateAttrib(AnnotAttrib.FontSize, intData); AnnotEvent?.UpdateAnnot(); } } } private void FontStyleChanged_Command(object obj) { if (obj != null) { var item = (ComboBoxItem)obj; var content = (string)item.Content; if (content != null) { if (content == "Regular") { AnnotEvent?.UpdateAttrib(AnnotAttrib.FontStyle, FontStyles.Normal); AnnotEvent?.UpdateAttrib(AnnotAttrib.FontWeight, FontWeights.Normal); // TextFontWeights = FontWeights.Normal; //TextFontStyle = FontStyles.Normal; } if (content == "Bold") { AnnotEvent?.UpdateAttrib(AnnotAttrib.FontStyle, FontStyles.Normal); AnnotEvent?.UpdateAttrib(AnnotAttrib.FontWeight, FontWeights.Bold); //TextFontWeights = FontWeights.Bold; // TextFontStyle = FontStyles.Normal; } if (content == "Italic") { AnnotEvent?.UpdateAttrib(AnnotAttrib.FontStyle, FontStyles.Italic); AnnotEvent?.UpdateAttrib(AnnotAttrib.FontWeight, FontWeights.Normal); // TextFontWeights = FontWeights.Normal; // TextFontStyle = FontStyles.Italic; } if (content == "Bold Italic") { AnnotEvent?.UpdateAttrib(AnnotAttrib.FontStyle, FontStyles.Italic); AnnotEvent?.UpdateAttrib(AnnotAttrib.FontWeight, FontWeights.Bold); //TextFontWeights = FontWeights.Bold; // TextFontStyle = FontStyles.Italic; } AnnotEvent?.UpdateAnnot(); } } } private void FontFamilyChanged_Command(object obj) { } private void SelectedFillColor_Command(object obj) { if (obj != null) { var colorValue = (Color)obj; if (colorValue != null) { FillColor = new SolidColorBrush(colorValue); FillColor.Opacity = FillOpacity; Dictionary changeData = new Dictionary(); changeData[AnnotArgsType.AnnotFreehand] = obj; PropertyPanel.DataChangedInvoke(this, changeData); } } } private void SelectedColor_Command(object obj) { if (obj != null) { var colorValue = (Color)obj; if (colorValue != null) { SelectColor = new SolidColorBrush(colorValue); SelectColor.Opacity = FillOpacity; Dictionary changeData = new Dictionary(); changeData[AnnotArgsType.AnnotFreehand] = obj; PropertyPanel.DataChangedInvoke(this, changeData); } } } private void CustomFontStyle() { if (CurrentPresetFont != null) { ContextMenu menu; if (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 FontStyleList = new List(); private void ReDefineFontStyle() { var item = FontStyleList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr); if (CurrentFontFamily.ValueStr == "Bold") { item.mFontStyle = FontStyles.Normal; item.mFontWeight = FontWeights.Bold; } else if (CurrentFontFamily.ValueStr == "Regular") { item.mFontStyle = FontStyles.Normal; item.mFontWeight = FontWeights.Normal; } else { item.mFontStyle = FontStyles.Italic; item.mFontWeight = FontWeights.Bold; } item.mFontSize = (int)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) { IsCanSave = false; ChangedValue -= FontMode_ChangedValue; } public AnnotAttribEvent AnnotEvent { get; set; } private FreeTextAnnotArgs Annot; private AnnotPropertyPanel PropertyPanel; public void OnNavigatedTo(NavigationContext navigationContext) { navigationContext.Parameters.TryGetValue(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel); if (PropertyPanel != null) { AnnotEvent = PropertyPanel.AnnotEvent; Annot = PropertyPanel.annot as FreeTextAnnotArgs; LoadPropertyHandler?.Invoke(this, Annot); ChangedValue -= FontMode_ChangedValue; ChangedValue += FontMode_ChangedValue; IsCanSave = true; } } private void FontMode_ChangedValue(object sender, FontSetModeType e) { if (sender != null) { switch (e) { case FontSetModeType.PresetFontStyes: break; case FontSetModeType.FontFamilys: break; case FontSetModeType.FontSizes: break; case FontSetModeType.FontWeight_Style: UpdateFontWeight_Style(); break; case FontSetModeType.FontColor: if (sender is Color == true) { AnnotEvent.UpdateAttrib(AnnotAttrib.FontColor, CurrentFontColor); AnnotEvent.UpdateAnnot(); } break; case FontSetModeType.TextAlignment: break; } } } } }