TextEditToolContentViewModel.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. using Prism.Commands;
  2. using Prism.Mvvm;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Media;
  9. using System.Windows;
  10. using PDF_Master.Model.PropertyPanel.AnnotPanel;
  11. using System.Windows.Controls;
  12. using Prism.Regions;
  13. using ComPDFKitViewer.PdfViewer;
  14. using PDF_Master.Model;
  15. using Microsoft.Win32;
  16. using PDF_Master.CustomControl;
  17. using ComPDFKitViewer;
  18. using System.Windows.Input;
  19. using PDF_Master.Helper;
  20. namespace PDF_Master.ViewModels.Tools
  21. {
  22. public class TextEditToolContentViewModel: BindableBase, INavigationAware
  23. {
  24. #region 变量
  25. public ViewContentViewModel viewContentViewModel;
  26. private CPDFViewer PDFViewer;
  27. private IRegionManager regions;
  28. private Dictionary<string, string> btnToProperty = new Dictionary<string, string>();
  29. #endregion
  30. #region 属性
  31. private bool _isTextEdit = false;
  32. public bool IsTextEdit
  33. {
  34. get { return _isTextEdit; }
  35. set { SetProperty(ref _isTextEdit, value); }
  36. }
  37. private bool _isImgEdit = false;
  38. public bool IsImgEdit
  39. {
  40. get { return _isImgEdit; }
  41. set { SetProperty(ref _isImgEdit, value); }
  42. }
  43. #endregion
  44. #region Command
  45. // 添加文本、图像
  46. public DelegateCommand<object> AddContentCommand { get; set; }
  47. public DelegateCommand AddTextCommand { get; set; }
  48. public DelegateCommand AddImgCommand { get; set; }
  49. #endregion
  50. #region 初始化
  51. public TextEditToolContentViewModel(IRegionManager regionManager)
  52. {
  53. regions = regionManager;
  54. InitCommand();
  55. InitBtnToProperty();
  56. }
  57. private void InitCommand()
  58. {
  59. AddContentCommand = new DelegateCommand<object>(AddContent);
  60. AddTextCommand = new DelegateCommand(AddText);
  61. AddImgCommand = new DelegateCommand(AddImg);
  62. }
  63. private void InitBtnToProperty()
  64. {
  65. btnToProperty.Add("Text", "TextEditProperty");
  66. btnToProperty.Add("Image", "ImageEditProperty");
  67. btnToProperty.Add("TextAndImage", "ImageTextEditProperty");
  68. btnToProperty.Add("PropertyPanelContent", "PropertyPanelContent");
  69. }
  70. #endregion
  71. public void AddContent(object obj)
  72. {
  73. if (PDFViewer == null || obj == null || obj as CustomIconToggleBtn == null) return;
  74. var btn = obj as CustomIconToggleBtn;
  75. EnterEditMode((bool)btn.IsChecked, btn.Tag.ToString());
  76. }
  77. private void EnterEditMode(bool isCheckMode,string modeType)
  78. {
  79. if (isCheckMode)
  80. {
  81. if (modeType == "Text")
  82. {
  83. IsImgEdit = false;
  84. IsTextEdit = true;
  85. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditText);
  86. }
  87. else
  88. {
  89. IsImgEdit = true;
  90. IsTextEdit = false;
  91. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditImage);
  92. }
  93. }
  94. else
  95. {
  96. IsImgEdit = false;
  97. IsTextEdit = false;
  98. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.None);
  99. PDFViewer.SetMouseMode(MouseModes.PDFEdit);
  100. }
  101. }
  102. #region Command功能
  103. private void AddText()
  104. {
  105. EnterEditMode(true, "Text");
  106. }
  107. private void AddImg()
  108. {
  109. EnterEditMode(true, "Image");
  110. }
  111. #endregion
  112. private void AddToPropertyPanel(string type, List<PDFEditEvent> e)
  113. {
  114. if (btnToProperty.ContainsKey(type))
  115. {
  116. NavigationParameters parameters = new NavigationParameters();
  117. parameters.Add(ParameterNames.PDFViewer, PDFViewer);
  118. parameters.Add(ParameterNames.AnnotEvent, e);
  119. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  120. {
  121. regions.RequestNavigate(RegionNames.PropertyRegionName, btnToProperty[type], parameters);
  122. }));
  123. ShowPropertyPanel(true);
  124. }
  125. }
  126. private void ShowPropertyPanel(bool show = true)
  127. {
  128. viewContentViewModel.IsPropertyOpen = show;
  129. }
  130. private void PDFViewer_PDFEditActiveHandler(object sender, List<PDFEditEvent> e)
  131. {
  132. if (e != null && e.Count > 0)
  133. {
  134. bool isText = false;
  135. bool isImg = false;
  136. foreach (var item in e)
  137. {
  138. if (item.EditType == ComPDFKit.PDFPage.CPDFEditType.EditImage)
  139. {
  140. isImg = true;
  141. }
  142. if (item.EditType == ComPDFKit.PDFPage.CPDFEditType.EditText)
  143. {
  144. isText = true;
  145. }
  146. }
  147. if (isText == true && isImg == false)
  148. {
  149. AddToPropertyPanel("Text", e);
  150. }
  151. if (isText == false && isImg == true)
  152. {
  153. AddToPropertyPanel("Image", e);
  154. }
  155. if (isText == true && isImg == true)
  156. {
  157. AddToPropertyPanel("TextAndImage", e);
  158. }
  159. }
  160. else
  161. {
  162. AddToPropertyPanel("PropertyPanelContent", null);
  163. }
  164. }
  165. public void OnNavigatedTo(NavigationContext navigationContext)
  166. {
  167. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  168. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  169. if (PDFViewer != null)
  170. {
  171. PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler;
  172. PDFViewer.PDFEditActiveHandler += PDFViewer_PDFEditActiveHandler;
  173. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  174. PDFViewer.PDFEditCommandHandler += PDFViewer_PDFEditCommandHandler;
  175. }
  176. }
  177. private void PDFViewer_PDFEditCommandHandler(object sender, PDFEditCommand e)
  178. {
  179. if (e == null)
  180. return;
  181. switch (e.CommandType)
  182. {
  183. case CommandType.Context:
  184. if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.None)//点击空白区域
  185. {
  186. e.PopupMenu = EmptyStateMenu(sender);
  187. }
  188. break;
  189. default:
  190. e.DoCommand();
  191. break;
  192. }
  193. if (e.PopupMenu != null)
  194. {
  195. e.Handle = true;
  196. }
  197. }
  198. //点击空白处时
  199. private ContextMenu EmptyStateMenu(object sender)
  200. {
  201. var popMenu = App.Current.FindResource("NoneMenu") as ContextMenu;
  202. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  203. //粘贴
  204. customMenu.SetMenuBinding(0, ApplicationCommands.Paste);
  205. //添加文本
  206. customMenu.SetMenuBinding(1, AddTextCommand);
  207. //添加图像
  208. customMenu.SetMenuBinding(2, AddImgCommand);
  209. return popMenu;
  210. }
  211. public bool IsNavigationTarget(NavigationContext navigationContext)
  212. {
  213. return true;
  214. }
  215. public void OnNavigatedFrom(NavigationContext navigationContext)
  216. {
  217. IsImgEdit = false;
  218. IsTextEdit = false;
  219. PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler;
  220. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  221. ShowPropertyPanel(false);
  222. }
  223. }
  224. }