FreehandAnnotPropertyViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. using ComPDFKitViewer;
  2. using ComPDFKitViewer.AnnotEvent;
  3. using PDF_Office.Model;
  4. using PDF_Office.Model.PropertyPanel.AnnotPanel;
  5. using PDF_Office.ViewModels.Tools;
  6. using Prism.Commands;
  7. using Prism.Mvvm;
  8. using Prism.Regions;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Globalization;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows.Controls;
  16. using System.Windows.Controls.Primitives;
  17. using System.Windows.Data;
  18. using System.Windows.Media;
  19. namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
  20. {
  21. public class EraseThicknessConverter : IValueConverter
  22. {
  23. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  24. {
  25. if (value is double)
  26. {
  27. return (double)value * 6;
  28. }
  29. return value;
  30. }
  31. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  32. {
  33. throw new NotImplementedException();
  34. }
  35. }
  36. public class FreehandAnnotPropertyViewModel : BindableBase, INavigationAware
  37. {
  38. private bool isPen = true;
  39. public bool IsPen
  40. {
  41. get { return isPen; }
  42. set
  43. {
  44. SetProperty(ref isPen, value);
  45. }
  46. }
  47. private Brush selectColor = new SolidColorBrush(Colors.Transparent);
  48. public Brush SelectColor
  49. {
  50. get { return selectColor; }
  51. set { SetProperty(ref selectColor, value);
  52. AnnotEvent?.UpdateAttrib(AnnotAttrib.Color, (SelectColor as SolidColorBrush).Color);
  53. AnnotEvent?.UpdateAnnot();
  54. }
  55. }
  56. private double annotOpacity = 1;
  57. public double AnnotOpacity
  58. {
  59. get { return annotOpacity; }
  60. set
  61. {
  62. SetProperty(ref annotOpacity, value);
  63. }
  64. }
  65. bool isCanSave = false;
  66. private double thicknessLine = 1;
  67. public double ThicknessLine
  68. {
  69. get { return thicknessLine; }
  70. set
  71. {
  72. SetProperty(ref thicknessLine, value);
  73. if(isCanSave)
  74. {
  75. AnnotEvent?.UpdateAttrib(AnnotAttrib.Thickness, thicknessLine);
  76. AnnotEvent?.UpdateAnnot();
  77. }
  78. }
  79. }
  80. private double erasethicknessLine = 1;
  81. public double EraseThicknessLine
  82. {
  83. get { return erasethicknessLine; }
  84. set
  85. {
  86. SetProperty(ref erasethicknessLine, value);
  87. AnnotEvent?.UpdateAttrib(AnnotAttrib.Thickness, erasethicknessLine);
  88. AnnotEvent?.UpdateAnnot();
  89. }
  90. }
  91. private bool _isMultiSelected = false;
  92. public bool IsMultiSelected
  93. {
  94. get { return _isMultiSelected; }
  95. set => SetProperty(ref _isMultiSelected, value);
  96. }
  97. public AnnotAttribEvent AnnotEvent { get; set; }
  98. private AnnotHandlerEventArgs Annot;
  99. private AnnotPropertyPanel PropertyPanel;
  100. public DelegateCommand<object> EraseCommand { get; set; }
  101. public DelegateCommand<object> PenCommand { get; set; }
  102. public DelegateCommand<object> SelectedColorChangedCommand { get; set; }
  103. public DelegateCommand<object> SelectPenThickChangedCommand { get; set; }
  104. public DelegateCommand<object> SetEraserThickCommand { get; set; }
  105. public DelegateCommand<object> LineModeCheckedCommand { get; set; }
  106. public DelegateCommand<object> SelectedOpacityValueCommand { get; set; }
  107. public event EventHandler<object> LoadPropertyHandler;
  108. public FreehandAnnotPropertyViewModel()
  109. {
  110. EraseCommand = new DelegateCommand<object>(Erase_Command);
  111. PenCommand = new DelegateCommand<object>(Pen_Command);
  112. SelectedColorChangedCommand = new DelegateCommand<object>(SelectedColorChanged_Click);
  113. SelectPenThickChangedCommand = new DelegateCommand<object>(SelectPenThickChanged_Command);
  114. SetEraserThickCommand = new DelegateCommand<object>(SelectEraserThickChanged_Command);
  115. LineModeCheckedCommand = new DelegateCommand<object>(LineMode_Checked);
  116. SelectedOpacityValueCommand = new DelegateCommand<object>(SelectedOpacityValue);
  117. InitVariable();
  118. }
  119. private void InitVariable()
  120. {
  121. }
  122. private void SelectEraserThickChanged_Command(object obj)
  123. {
  124. if (obj != null)
  125. {
  126. var item = (ComboBoxItem)obj;
  127. var content = (string)item.Content;
  128. if (content != null)
  129. {
  130. var intData = double.Parse(content);
  131. AnnotEvent?.UpdateAttrib(AnnotAttrib.Thickness, intData);
  132. AnnotEvent?.UpdateAnnot();
  133. EraseThicknessLine = intData;
  134. }
  135. }
  136. }
  137. private void LineMode_Checked(object obj)
  138. {
  139. if (obj != null)
  140. {
  141. var tag = ((string)obj);
  142. DashStyle newDash = new DashStyle();
  143. switch (tag)
  144. {
  145. case "Dashed":
  146. newDash.Dashes.Add(2);
  147. newDash.Dashes.Add(2);
  148. AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, DashStyles.Dash);
  149. AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, newDash);
  150. break;
  151. case "Solid":
  152. AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, newDash);
  153. break;
  154. }
  155. AnnotEvent?.UpdateAnnot();
  156. }
  157. }
  158. private void SelectPenThickChanged_Command(object obj)
  159. {
  160. if (obj != null)
  161. {
  162. var item = (ComboBoxItem)obj;
  163. var content = (string)item.Content;
  164. if (content != null)
  165. {
  166. var intData = double.Parse(content);
  167. if (IsMultiSelected)
  168. {
  169. foreach (var itemEvent in PropertyPanel.AnnotEvents)
  170. {
  171. itemEvent?.UpdateAttrib(AnnotAttrib.Thickness, intData);
  172. itemEvent?.UpdateAnnot();
  173. }
  174. }
  175. else
  176. {
  177. AnnotEvent?.UpdateAttrib(AnnotAttrib.Thickness, intData);
  178. AnnotEvent?.UpdateAnnot();
  179. }
  180. ThicknessLine = intData;
  181. }
  182. }
  183. }
  184. private void SelectedOpacityValue(object obj)
  185. {
  186. if (obj != null)
  187. {
  188. annotOpacity = (double)obj;
  189. SelectColor.Opacity = annotOpacity;
  190. AnnotEvent?.UpdateAttrib(AnnotAttrib.Transparency, annotOpacity);
  191. AnnotEvent?.UpdateAnnot();
  192. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  193. changeData[AnnotArgsType.AnnotFreehand] = annotOpacity;
  194. PropertyPanel.DataChangedInvoke(this, changeData);
  195. }
  196. }
  197. private void SelectedColorChanged_Click(object obj)
  198. {
  199. if (obj != null)
  200. {
  201. var colorValue = (Color)obj;
  202. if (colorValue != null)
  203. {
  204. if (IsMultiSelected)
  205. {
  206. foreach (var item in PropertyPanel.AnnotEvents)
  207. {
  208. item?.UpdateAttrib(AnnotAttrib.Color, colorValue);
  209. item?.UpdateAnnot();
  210. }
  211. }
  212. else
  213. {
  214. SelectColor = new SolidColorBrush(colorValue);
  215. SelectColor.Opacity = AnnotOpacity;
  216. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  217. changeData[AnnotArgsType.AnnotFreehand] = obj;
  218. PropertyPanel.DataChangedInvoke(this, changeData);
  219. }
  220. }
  221. }
  222. }
  223. private void Erase_Command(object obj)
  224. {
  225. var btn = obj as ToggleButton;
  226. if(btn.IsChecked == true)
  227. {
  228. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  229. changeData[AnnotArgsType.AnnotErase] = btn;
  230. PropertyPanel.DataChangedInvoke(this, changeData);
  231. IsPen = false;
  232. }
  233. }
  234. private void Pen_Command(object obj)
  235. {
  236. var btn = obj as ToggleButton;
  237. if (btn.IsChecked == true)
  238. {
  239. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  240. changeData[AnnotArgsType.AnnotErase] = btn;
  241. PropertyPanel.DataChangedInvoke(this, changeData);
  242. IsPen = true;
  243. }
  244. }
  245. public bool IsNavigationTarget(NavigationContext navigationContext)
  246. {
  247. return true;
  248. }
  249. public void OnNavigatedFrom(NavigationContext navigationContext)
  250. {
  251. IsMultiSelected = false;
  252. isCanSave = false;
  253. }
  254. public void OnNavigatedTo(NavigationContext navigationContext)
  255. {
  256. navigationContext.Parameters.TryGetValue<AnnotPropertyPanel>(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel);
  257. if (PropertyPanel != null)
  258. {
  259. AnnotEvent = PropertyPanel.AnnotEvent;
  260. Annot = PropertyPanel.annot;
  261. if (PropertyPanel.annotlists != null && PropertyPanel.annotlists.Count > 1)
  262. {
  263. IsMultiSelected = true;
  264. }
  265. else
  266. {
  267. IsMultiSelected = false;
  268. }
  269. if (IsMultiSelected)
  270. {
  271. double thickness = 0;
  272. foreach(var item in PropertyPanel.annotlists)
  273. {
  274. if ((item as FreehandAnnotArgs).LineWidth >= thickness)
  275. thickness = (item as FreehandAnnotArgs).LineWidth;
  276. }
  277. ThicknessLine = thickness;
  278. }
  279. else
  280. {
  281. GetAnnotProperty();
  282. if (Annot is FreehandAnnotArgs)
  283. LoadPropertyHandler?.Invoke(null, (Annot as FreehandAnnotArgs).InkColor);
  284. }
  285. }
  286. isCanSave = true;
  287. }
  288. private void GetAnnotProperty()
  289. {
  290. if (Annot is FreehandAnnotArgs)
  291. {
  292. var annot = Annot as FreehandAnnotArgs;
  293. if (annot != null)
  294. {
  295. AnnotOpacity = annot.Transparency;
  296. SelectColor = new SolidColorBrush(annot.InkColor);
  297. ThicknessLine = annot.LineWidth;
  298. IsPen = true;
  299. }
  300. }
  301. if (Annot is EraseArgs)
  302. {
  303. var annot = Annot as EraseArgs;
  304. if (annot != null)
  305. {
  306. EraseThicknessLine = annot.Thickness;
  307. IsPen = false;
  308. }
  309. }
  310. }
  311. }
  312. }