TextAnnotPropertyViewModel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. using ComPDFKitViewer;
  2. using ComPDFKitViewer.AnnotEvent;
  3. using PDF_Office.Helper;
  4. using PDF_Office.Model;
  5. using PDF_Office.ViewModels.Tools;
  6. using PDFSettings;
  7. using Prism.Commands;
  8. using Prism.Mvvm;
  9. using Prism.Regions;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Globalization;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. using System.Windows;
  17. using System.Windows.Data;
  18. using System.Windows.Media;
  19. namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
  20. {
  21. public class AnnotArgsTypeConverter : IValueConverter
  22. {
  23. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  24. {
  25. if (value is AnnotArgsType)
  26. {
  27. if (parameter is string)
  28. {
  29. var args = (AnnotArgsType)value;
  30. if ((string)parameter == "AnnotHighlight" && args == AnnotArgsType.AnnotHighlight)
  31. {
  32. return Visibility.Visible;
  33. }
  34. if ((string)parameter == "AnnotUnderline" && args == AnnotArgsType.AnnotUnderline)
  35. {
  36. return Visibility.Visible;
  37. }
  38. if ((string)parameter == "AnnotSquiggly" && args == AnnotArgsType.AnnotSquiggly)
  39. {
  40. return Visibility.Visible;
  41. }
  42. if ((string)parameter == "AnnotStrikeout" && args == AnnotArgsType.AnnotStrikeout)
  43. {
  44. return Visibility.Visible;
  45. }
  46. if ((string)parameter == "AnnotSticky" && args == AnnotArgsType.AnnotSticky)
  47. {
  48. return Visibility.Visible;
  49. }
  50. }
  51. }
  52. return Visibility.Collapsed;
  53. }
  54. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  55. {
  56. throw new NotImplementedException();
  57. }
  58. }
  59. public class TextAnnotPropertyViewModel:BindableBase, INavigationAware
  60. {
  61. #region 属性
  62. private AnnotArgsType annotType;
  63. public AnnotArgsType AnnotType
  64. {
  65. get { return annotType; }
  66. set
  67. {
  68. SetProperty(ref annotType, value);
  69. SetAnnotType();
  70. }
  71. }
  72. private string annotTypeTitle;
  73. public string AnnotTypeTitle
  74. {
  75. get { return annotTypeTitle; }
  76. set
  77. {
  78. SetProperty(ref annotTypeTitle, value);
  79. }
  80. }
  81. private double annotOpacity;
  82. public double AnnotOpacity
  83. {
  84. get {return annotOpacity;}
  85. set
  86. {
  87. SetProperty(ref annotOpacity, value);
  88. }
  89. }
  90. /// <summary>
  91. /// 示例背景
  92. /// </summary>
  93. private Brush sampleTextBg = new SolidColorBrush(Colors.Transparent);
  94. public Brush SampleTextBg
  95. {
  96. get { return sampleTextBg; }
  97. set { SetProperty(ref sampleTextBg, value);
  98. if(Annot.EventType == AnnotArgsType.AnnotHighlight)
  99. {
  100. CurrentColor = sampleTextBg;
  101. AnnotEvent?.UpdateAttrib(AnnotAttrib.Color, (sampleTextBg as SolidColorBrush).Color);
  102. AnnotEvent?.UpdateAnnot();
  103. }
  104. }
  105. }
  106. private Brush selectColor = new SolidColorBrush(Colors.Transparent);
  107. public Brush SelectColor
  108. {
  109. get { return selectColor; }
  110. set {
  111. SetProperty(ref selectColor, value);
  112. AnnotEvent?.UpdateAttrib(AnnotAttrib.Color, (selectColor as SolidColorBrush).Color);
  113. AnnotEvent?.UpdateAnnot();
  114. CurrentColor = selectColor;
  115. }
  116. }
  117. private Brush _currentColor = new SolidColorBrush(Colors.Transparent);
  118. public Brush CurrentColor
  119. {
  120. get { return _currentColor; }
  121. set
  122. {
  123. SetProperty(ref _currentColor, value);
  124. }
  125. }
  126. private bool _isMultiSelected = false;
  127. public bool IsMultiSelected
  128. {
  129. get { return _isMultiSelected; }
  130. set => SetProperty(ref _isMultiSelected, value);
  131. }
  132. #endregion
  133. public AnnotAttribEvent AnnotEvent { get; set; }
  134. private AnnotHandlerEventArgs Annot;
  135. private AnnotPropertyPanel PropertyPanel;
  136. public DelegateCommand<object> SelectedColorChangedCommand { get; set; }
  137. public DelegateCommand<object> OpacityItemCommand { get; set; }
  138. public DelegateCommand<object> OpacityValueChangedCommand { get; set; }
  139. public DelegateCommand<object> SelectedValueChangedCommand { get; set; }
  140. public event EventHandler<object> LoadPropertyHandler;
  141. public TextAnnotPropertyViewModel()
  142. {
  143. SelectedColorChangedCommand = new DelegateCommand<object>(SelectedColorChanged_Click);
  144. OpacityItemCommand = new DelegateCommand<object>(OpacityItemCommand_Click);
  145. OpacityValueChangedCommand = new DelegateCommand<object>(OpacityValueChanged_Command);
  146. SelectedValueChangedCommand = new DelegateCommand<object>(SelectedValueChanged_Command);
  147. }
  148. private void SelectedValueChanged_Command(object obj)
  149. {
  150. if (obj != null)
  151. {
  152. annotOpacity = (double)obj;
  153. if (AnnotType == AnnotArgsType.AnnotHighlight)
  154. SampleTextBg.Opacity = annotOpacity;
  155. else
  156. SelectColor.Opacity = annotOpacity;
  157. AnnotEvent?.UpdateAttrib(AnnotAttrib.Transparency, annotOpacity);
  158. AnnotEvent?.UpdateAnnot();
  159. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  160. changeData[AnnotType] = annotOpacity;
  161. PropertyPanel.DataChangedInvoke(this, changeData);
  162. }
  163. }
  164. private void OpacityItemCommand_Click(object obj)
  165. {
  166. if (obj != null)
  167. {
  168. if (AnnotType == AnnotArgsType.AnnotHighlight)
  169. SampleTextBg.Opacity = annotOpacity;
  170. else
  171. SelectColor.Opacity = annotOpacity;
  172. AnnotEvent?.UpdateAttrib(AnnotAttrib.Transparency, annotOpacity);
  173. AnnotEvent?.UpdateAnnot();
  174. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  175. changeData[AnnotType] = annotOpacity;
  176. PropertyPanel.DataChangedInvoke(this, changeData);
  177. }
  178. }
  179. private void OpacityValueChanged_Command(object obj)
  180. {
  181. if (obj != null)
  182. {
  183. if (AnnotType == AnnotArgsType.AnnotHighlight)
  184. SampleTextBg.Opacity = annotOpacity;
  185. else
  186. SelectColor.Opacity = annotOpacity;
  187. AnnotEvent?.UpdateAttrib(AnnotAttrib.Transparency, annotOpacity);
  188. AnnotEvent?.UpdateAnnot();
  189. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  190. changeData[AnnotType] = annotOpacity;
  191. PropertyPanel.DataChangedInvoke(this, changeData);
  192. }
  193. }
  194. public void SelectedColorChanged_Click(object color)
  195. {
  196. if (color != null && PropertyPanel != null)
  197. {
  198. var colorValue = (Color)color;
  199. if (colorValue != null)
  200. {
  201. if(IsMultiSelected)
  202. {
  203. foreach (var item in PropertyPanel.AnnotEvents)
  204. {
  205. item?.UpdateAttrib(AnnotAttrib.Color, colorValue);
  206. item?.UpdateAnnot();
  207. }
  208. }
  209. else
  210. {
  211. switch (AnnotType)
  212. {
  213. case AnnotArgsType.AnnotHighlight:
  214. SampleTextBg = new SolidColorBrush(colorValue);
  215. SampleTextBg.Opacity = AnnotOpacity;
  216. break;
  217. case AnnotArgsType.AnnotUnderline:
  218. case AnnotArgsType.AnnotStrikeout:
  219. case AnnotArgsType.AnnotSquiggly:
  220. case AnnotArgsType.AnnotSticky:
  221. SelectColor = new SolidColorBrush(colorValue);
  222. SelectColor.Opacity = AnnotOpacity;
  223. break;
  224. }
  225. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  226. changeData[AnnotType] = color;
  227. PropertyPanel.DataChangedInvoke(this, changeData);
  228. }
  229. }
  230. }
  231. }
  232. private void SetAnnotType()
  233. {
  234. switch (AnnotType)
  235. {
  236. case AnnotArgsType.AnnotHighlight:
  237. AnnotTypeTitle = "高亮";
  238. break;
  239. case AnnotArgsType.AnnotUnderline:
  240. AnnotTypeTitle = "下划线";
  241. break;
  242. case AnnotArgsType.AnnotStrikeout:
  243. AnnotTypeTitle = "删除线";
  244. break;
  245. case AnnotArgsType.AnnotSquiggly:
  246. AnnotTypeTitle = "波浪线";
  247. break;
  248. case AnnotArgsType.AnnotSticky:
  249. AnnotTypeTitle = "??";
  250. break;
  251. }
  252. }
  253. public bool IsNavigationTarget(NavigationContext navigationContext)
  254. {
  255. return true;
  256. }
  257. public void OnNavigatedFrom(NavigationContext navigationContext)
  258. {
  259. IsMultiSelected = false;
  260. }
  261. public void OnNavigatedTo(NavigationContext navigationContext)
  262. {
  263. navigationContext.Parameters.TryGetValue<AnnotPropertyPanel>(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel);
  264. if(PropertyPanel != null)
  265. {
  266. AnnotEvent = PropertyPanel.AnnotEvent;
  267. Annot = PropertyPanel.annot;
  268. AnnotType = Annot.EventType;
  269. if (PropertyPanel.annotlists != null && PropertyPanel.annotlists.Count > 1)
  270. {
  271. IsMultiSelected = true;
  272. }
  273. else
  274. {
  275. IsMultiSelected = false;
  276. }
  277. if(IsMultiSelected)
  278. {
  279. DefaultAnnotProperty annotProperty = SettingHelper.GetAnnotDefaultProperty(AnnotArgsType.AnnotHighlight);
  280. if (annotProperty != null)
  281. {
  282. SelectColor = new SolidColorBrush(annotProperty.ForgoundColor);
  283. }
  284. }
  285. else
  286. {
  287. GetAnnotProperty();
  288. }
  289. LoadPropertyHandler?.Invoke(null, Annot);
  290. }
  291. }
  292. private void GetAnnotProperty()
  293. {
  294. if (Annot != null)
  295. {
  296. switch (Annot.EventType)
  297. {
  298. case AnnotArgsType.AnnotHighlight:
  299. if (Annot is TextHighlightAnnotArgs)
  300. {
  301. var Hightlight = Annot as TextHighlightAnnotArgs;
  302. AnnotOpacity = Hightlight.Transparency;
  303. SampleTextBg = new SolidColorBrush(Hightlight.Color);
  304. }
  305. break;
  306. case AnnotArgsType.AnnotUnderline:
  307. {
  308. var Underline = Annot as TextUnderlineAnnotArgs;
  309. AnnotOpacity = Underline.Transparency;
  310. SampleTextBg = new SolidColorBrush(Colors.Transparent);
  311. SelectColor = new SolidColorBrush(Underline.Color);
  312. }
  313. break;
  314. case AnnotArgsType.AnnotStrikeout:
  315. {
  316. var Strikeout = Annot as TextStrikeoutAnnotArgs;
  317. AnnotOpacity = Strikeout.Transparency;
  318. SampleTextBg = new SolidColorBrush(Colors.Transparent);
  319. SelectColor = new SolidColorBrush(Strikeout.Color);
  320. }
  321. break;
  322. case AnnotArgsType.AnnotSquiggly:
  323. {
  324. var Squiggly = Annot as TextSquigglyAnnotArgs;
  325. AnnotOpacity = Squiggly.Transparency;
  326. SampleTextBg = new SolidColorBrush(Colors.Transparent);
  327. SelectColor = new SolidColorBrush(Squiggly.Color);
  328. }
  329. break;
  330. }
  331. }
  332. }
  333. }
  334. }