BookmarkContentViewModel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKitViewer.PdfViewer;
  3. using ImTools;
  4. using Microsoft.Office.Interop.Word;
  5. using PDF_Office.CustomControl;
  6. using PDF_Office.Helper;
  7. using PDF_Office.Model;
  8. using PDF_Office.Model.PageEdit;
  9. using Prism.Commands;
  10. using Prism.Mvvm;
  11. using Prism.Regions;
  12. using Prism.Services.Dialogs;
  13. using System;
  14. using System.Collections.ObjectModel;
  15. using System.Linq;
  16. using System.Reflection;
  17. using System.Windows;
  18. using System.Windows.Controls;
  19. using System.Windows.Controls.Primitives;
  20. using System.Windows.Forms;
  21. using System.Windows.Input;
  22. using System.Windows.Markup;
  23. using System.Windows.Media;
  24. using static Dropbox.Api.TeamLog.AdminAlertSeverityEnum;
  25. using static System.Net.Mime.MediaTypeNames;
  26. using static System.Windows.Forms.VisualStyles.VisualStyleElement;
  27. using KeyEventArgs = System.Windows.Input.KeyEventArgs;
  28. using ListViewItem = System.Windows.Controls.ListViewItem;
  29. using TextBox = System.Windows.Controls.TextBox;
  30. namespace PDF_Office.ViewModels.BOTA
  31. {
  32. public class BookmarkContentViewModel : BindableBase, INavigationAware
  33. {
  34. #region 属性
  35. private static string hoverColor = "#D9D9D9";
  36. private static string defaultColor = "#F2F2F2";
  37. private static string selectColcr = "#97D7FB";
  38. private static string borderBackground = "#FFFFFF";
  39. private static string borderBrush = "#000000";
  40. private bool isSelete = false;
  41. private IRegionManager region;
  42. private IDialogService dialogs;
  43. public CPDFViewer PDFViewer;
  44. private ObservableCollection<CPDFBookmark> bookmarklist;
  45. public ObservableCollection<CPDFBookmark> Bookmarklist
  46. {
  47. get
  48. {
  49. return bookmarklist;
  50. }
  51. set
  52. {
  53. SetProperty(ref bookmarklist, value);
  54. }
  55. }
  56. private Visibility isEmpty;
  57. public Visibility IsEmptyPanelVisibility
  58. {
  59. get
  60. {
  61. return isEmpty;
  62. }
  63. set
  64. {
  65. SetProperty(ref isEmpty, value);
  66. }
  67. }
  68. #endregion 属性
  69. #region 命令
  70. public DelegateCommand<object> KeyDownCommand { get; set; }
  71. public DelegateCommand<object> LostFocusCommand { get; set; }
  72. public DelegateCommand<object> AddBookmarkCommand { get; set; }
  73. public DelegateCommand<object> ListViewItemMouseDownCommand { get; set; }
  74. public DelegateCommand<object> RenameCommand { get; set; }
  75. public DelegateCommand<object> EditPageIndexCommand { get; set; }
  76. public DelegateCommand<object> DeleteCommand { get; set; }
  77. #endregion 命令
  78. public BookmarkContentViewModel(IRegionManager regionManager, IDialogService dialogService)
  79. {
  80. region = regionManager;
  81. dialogs = dialogService;
  82. //MouseClickCommand = new DelegateCommand<object>(MouseClickEvent);
  83. //MouseEnterCommand = new DelegateCommand<object>(MouseEnterEvent);
  84. LostFocusCommand = new DelegateCommand<object>(LostFocusEvent);
  85. //MouseLeaveCommand = new DelegateCommand<object>(MouseLeaveEvent);
  86. //GotFocusCommand = new DelegateCommand<object>(GotFocusEvent);
  87. AddBookmarkCommand = new DelegateCommand<object>(AddBookmarkEvent);
  88. ListViewItemMouseDownCommand = new DelegateCommand<object>(ListViewItemMouseLeftButtonDownEvent);
  89. RenameCommand = new DelegateCommand<object>(RenameEvent);
  90. DeleteCommand = new DelegateCommand<object>(DelegateEvent);
  91. EditPageIndexCommand = new DelegateCommand<object>(EditPageIndexEvent);
  92. KeyDownCommand = new DelegateCommand<object>(KeyDownEvent);
  93. }
  94. private void KeyDownEvent(object obj)
  95. {
  96. if (obj is KeyEventArgs)
  97. {
  98. KeyEventArgs keyEventArgs = (KeyEventArgs)obj;
  99. if (keyEventArgs.Key == Key.Enter)
  100. {
  101. if (keyEventArgs.OriginalSource is TextBox)
  102. {
  103. TextBox textBox = (TextBox)keyEventArgs.OriginalSource;
  104. ListViewItem listViewItem = CommonHelper.FindVisualParent<ListViewItem>(textBox);
  105. UpdateTitle(listViewItem, textBox);
  106. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listViewItem);
  107. textBlock.Text = textBox.Text;
  108. textBlock.Visibility = Visibility.Visible;
  109. }
  110. }
  111. }
  112. }
  113. private void EditPageIndexEvent(object obj)
  114. {
  115. CPDFBookmark bookmark = obj as CPDFBookmark;
  116. if (bookmark != null)
  117. {
  118. //var list = PDFViewer.Document.GetBookmarkList().FindAll(q => q.PageIndex == bookmark.PageIndex && q.Title == bookmark.Title);
  119. //ListBoxItem myListBoxItem = (ListBoxItem)(listView.ItemContainerGenerator.ContainerFromItem(list[0]));
  120. //if (myListBoxItem.IsSelected == false)
  121. //{
  122. // myListBoxItem.IsSelected = true;
  123. // myListBoxItem.Focus();
  124. //}
  125. DialogParameters value = new DialogParameters();
  126. value.Add(ParameterNames.BookmarkInfo, "确定要将选定大纲的目标位置设置为当前位置吗?");
  127. dialogs.ShowDialog(DialogNames.BookmarkInfoDialog, value, e =>
  128. {
  129. if (e.Result == ButtonResult.OK)
  130. {
  131. if (RemoveBookMark(bookmark))
  132. {
  133. bookmarklist.Remove(bookmark);
  134. CPDFBookmark cPDFBookmark = new CPDFBookmark();
  135. bookmark.Title = bookmark.Title;
  136. bookmark.Date = DateTime.Now.ToString(@"yyyyMMddHHmmsszzz\'").Replace(':', '\'') + "\n";
  137. bookmark.PageIndex = PDFViewer.CurrentIndex;
  138. if (PDFViewer.Document.AddBookmark(bookmark))
  139. {
  140. PDFViewer.UndoManager.CanSave = true;
  141. Bookmarklist = new ObservableCollection<CPDFBookmark>(PDFViewer.Document.GetBookmarkList().OrderBy(d => d.Title));
  142. }
  143. }
  144. }
  145. });
  146. }
  147. }
  148. private void DelegateEvent(object obj)
  149. {
  150. CPDFBookmark bookmark = obj as CPDFBookmark;
  151. if (bookmark != null)
  152. {
  153. if (RemoveBookMark(bookmark))
  154. bookmarklist.Remove(bookmark);
  155. //ListBoxItem myListBoxItem = (ListBoxItem)(myListBox.ItemContainerGenerator.ContainerFromItem(myListBox.Items.CurrentItem));
  156. }
  157. }
  158. private void RenameEvent(object obj)
  159. {
  160. CPDFBookmark bookmark = obj as CPDFBookmark;
  161. if (bookmark != null)
  162. {
  163. //var result = PDFViewer.Document.EditBookmark(bookmark.PageIndex, textBox.Text.Trim());
  164. //if (result)
  165. //{
  166. // bookmark.Title = textBox.Text.Trim();
  167. // PDFViewer.UndoManager.CanSave = true;
  168. //}
  169. }
  170. }
  171. private bool RemoveBookMark(CPDFBookmark item)
  172. {
  173. var data = item;
  174. if (data == null)
  175. return false;
  176. return PDFViewer.Document.RemoveBookmark(data.PageIndex);
  177. }
  178. private void LostFocusEvent(object obj)
  179. {
  180. TextBox textBox = null;
  181. ListViewItem listViewItem = null;
  182. if (obj is CompositeCommandParameter)
  183. {
  184. CompositeCommandParameter parameter = (CompositeCommandParameter)obj;
  185. if (parameter.Parameter is TextBox)
  186. {
  187. textBox = (TextBox)parameter.Parameter;
  188. //textBox.PreviewMouseDown += new MouseButtonEventHandler(TxtTitleInput_PreviewMouseLeftButtonDown);
  189. listViewItem = CommonHelper.FindVisualParent<ListViewItem>(textBox);
  190. }
  191. }
  192. if (obj is ListBoxItem)
  193. {
  194. listViewItem = (ListViewItem)(obj);
  195. textBox = CommonHelper.FindVisualChild<TextBox>(listViewItem);
  196. }
  197. UpdateTitle(listViewItem, textBox);
  198. }
  199. private void UpdateTitle(ListViewItem listViewItem, TextBox textBox)
  200. {
  201. if (listViewItem != null)
  202. {
  203. var data = listViewItem.DataContext as CPDFBookmark;
  204. if (data == null)
  205. return;
  206. var result = PDFViewer.Document.EditBookmark(data.PageIndex, textBox.Text.Trim());
  207. if (result)
  208. {
  209. data.Title = textBox.Text.Trim();
  210. PDFViewer.UndoManager.CanSave = true;
  211. }
  212. }
  213. }
  214. private void TxtTitleInput_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  215. {
  216. TextBox textBox = sender as TextBox;
  217. textBox.Focus();
  218. e.Handled = true;
  219. }
  220. private void ListViewItemMouseLeftButtonDownEvent(object obj)
  221. {
  222. ListBoxItem listBoxItem = (obj as ListBoxItem);
  223. if (listBoxItem != null)
  224. {
  225. //box.Background = listBoxItem.Background;
  226. //box.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(selectColcr));
  227. if (!(listBoxItem.DataContext is CPDFBookmark))
  228. {
  229. return;
  230. }
  231. int index = (listBoxItem.DataContext as CPDFBookmark).PageIndex;
  232. PDFViewer.GoToPage(index);
  233. isSelete = false;
  234. }
  235. //MessageBox.Show("dsafa");
  236. }
  237. private void AddBookmarkEvent(object obj)
  238. {
  239. int index = PDFViewer.CurrentIndex;
  240. string mark = string.Format($"第{index + 1}页");
  241. var list = PDFViewer.Document.GetBookmarkList().FindAll(q => q.PageIndex == index);
  242. if (list.Count > 0)
  243. {
  244. System.Windows.Controls.ListView listView = obj as System.Windows.Controls.ListView;
  245. if (listView != null)
  246. {
  247. //listView.Items.CurrentItem = list[0];
  248. ListBoxItem myListBoxItem = (ListBoxItem)(listView.ItemContainerGenerator.ContainerFromItem(list[0]));
  249. //ContentPresenter myContentPresenter = CommonHelper.FindVisualChild<ContentPresenter>(myListBoxItem);
  250. //DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
  251. //Grid item = (Grid)myDataTemplate.FindName("Grid", myContentPresenter);
  252. //Grid grid = CommonHelper.FindVisualChild<Grid>(myListBoxItem);
  253. if (myListBoxItem.IsSelected == false)
  254. {
  255. myListBoxItem.IsSelected = true;
  256. myListBoxItem.Focus();
  257. }
  258. //listView.SelectedItem = myListBoxItem;
  259. //myListBoxItem.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(selectColcr));
  260. return;
  261. }
  262. }
  263. DialogParameters value = new DialogParameters();
  264. value.Add(ParameterNames.Bookmark, mark);
  265. value.Add(ParameterNames.Title, "创建一个新的书签");
  266. dialogs.ShowDialog(DialogNames.AddBookmarkDialog, value, e =>
  267. {
  268. if (e.Result == ButtonResult.OK && e.Parameters != null)
  269. {
  270. if (e.Parameters.ContainsKey(ParameterNames.Bookmark))
  271. {
  272. mark = e.Parameters.GetValue<string>(ParameterNames.Bookmark).ToString();
  273. CPDFBookmark bookmark = new CPDFBookmark();
  274. bookmark.Title = mark;
  275. bookmark.Date = DateTime.Now.ToString(@"yyyyMMddHHmmsszzz\'").Replace(':', '\'') + "\n";
  276. bookmark.PageIndex = PDFViewer.CurrentIndex;
  277. if (PDFViewer.Document.AddBookmark(bookmark))
  278. {
  279. PDFViewer.UndoManager.CanSave = true;
  280. Bookmarklist = new ObservableCollection<CPDFBookmark>(PDFViewer.Document.GetBookmarkList().OrderBy(d => d.Title));
  281. }
  282. }
  283. }
  284. });
  285. }
  286. public void OnNavigatedTo(NavigationContext navigationContext)
  287. {
  288. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  289. if (PDFViewer == null)
  290. {
  291. return;
  292. }
  293. Bookmarklist = new ObservableCollection<CPDFBookmark>(PDFViewer.Document.GetBookmarkList().OrderBy(d => d.Title));
  294. if (Bookmarklist.Count < 1)
  295. {
  296. IsEmptyPanelVisibility = Visibility.Visible;
  297. return;
  298. }
  299. else
  300. {
  301. IsEmptyPanelVisibility = Visibility.Hidden;
  302. return;
  303. }
  304. }
  305. public bool IsNavigationTarget(NavigationContext navigationContext)
  306. {
  307. return true;
  308. }
  309. public void OnNavigatedFrom(NavigationContext navigationContext)
  310. {
  311. }
  312. }
  313. }