FreehandAnnotPropertyViewModel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. PropertyPanel.InvokeToMyTools(AnnotArgsType.AnnotFreehand, annotOpacity);
  193. }
  194. }
  195. private void SelectedColorChanged_Click(object obj)
  196. {
  197. if (obj != null)
  198. {
  199. var colorValue = (Color)obj;
  200. if (colorValue != null)
  201. {
  202. if (IsMultiSelected)
  203. {
  204. foreach (var item in PropertyPanel.AnnotEvents)
  205. {
  206. item?.UpdateAttrib(AnnotAttrib.Color, colorValue);
  207. item?.UpdateAnnot();
  208. }
  209. }
  210. else
  211. {
  212. SelectColor = new SolidColorBrush(colorValue);
  213. SelectColor.Opacity = AnnotOpacity;
  214. PropertyPanel.InvokeToMyTools(AnnotArgsType.AnnotFreehand, obj);
  215. }
  216. }
  217. }
  218. }
  219. private void Erase_Command(object obj)
  220. {
  221. var btn = obj as ToggleButton;
  222. if(btn.IsChecked == true)
  223. {
  224. PropertyPanel.InvokeToMyTools(AnnotArgsType.AnnotErase, btn);
  225. IsPen = false;
  226. }
  227. }
  228. private void Pen_Command(object obj)
  229. {
  230. var btn = obj as ToggleButton;
  231. if (btn.IsChecked == true)
  232. {
  233. PropertyPanel.InvokeToMyTools(AnnotArgsType.AnnotErase, btn);
  234. IsPen = true;
  235. }
  236. }
  237. public bool IsNavigationTarget(NavigationContext navigationContext)
  238. {
  239. return true;
  240. }
  241. public void OnNavigatedFrom(NavigationContext navigationContext)
  242. {
  243. IsMultiSelected = false;
  244. isCanSave = false;
  245. }
  246. public void OnNavigatedTo(NavigationContext navigationContext)
  247. {
  248. navigationContext.Parameters.TryGetValue<AnnotPropertyPanel>(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel);
  249. if (PropertyPanel != null)
  250. {
  251. AnnotEvent = PropertyPanel.AnnotEvent;
  252. Annot = PropertyPanel.annot;
  253. if (PropertyPanel.annotlists != null && PropertyPanel.annotlists.Count > 1)
  254. {
  255. IsMultiSelected = true;
  256. }
  257. else
  258. {
  259. IsMultiSelected = false;
  260. }
  261. if (IsMultiSelected)
  262. {
  263. double thickness = 0;
  264. foreach(var item in PropertyPanel.annotlists)
  265. {
  266. if ((item as FreehandAnnotArgs).LineWidth >= thickness)
  267. thickness = (item as FreehandAnnotArgs).LineWidth;
  268. }
  269. ThicknessLine = thickness;
  270. }
  271. else
  272. {
  273. GetAnnotProperty();
  274. if (Annot is FreehandAnnotArgs)
  275. LoadPropertyHandler?.Invoke(null, (Annot as FreehandAnnotArgs).InkColor);
  276. }
  277. }
  278. isCanSave = true;
  279. }
  280. private void GetAnnotProperty()
  281. {
  282. if (Annot is FreehandAnnotArgs)
  283. {
  284. var annot = Annot as FreehandAnnotArgs;
  285. if (annot != null)
  286. {
  287. AnnotOpacity = annot.Transparency;
  288. SelectColor = new SolidColorBrush(annot.InkColor);
  289. ThicknessLine = annot.LineWidth;
  290. IsPen = true;
  291. }
  292. }
  293. if (Annot is EraseArgs)
  294. {
  295. var annot = Annot as EraseArgs;
  296. if (annot != null)
  297. {
  298. EraseThicknessLine = annot.Thickness;
  299. IsPen = false;
  300. }
  301. }
  302. }
  303. }
  304. }