BookmarkContentViewModel.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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 ListViewItem = System.Windows.Controls.ListViewItem;
  28. using TextBox = System.Windows.Controls.TextBox;
  29. namespace PDF_Office.ViewModels.BOTA
  30. {
  31. public class BookmarkContentViewModel : BindableBase, INavigationAware
  32. {
  33. #region 属性
  34. private static string hoverColor = "#D9D9D9";
  35. private static string defaultColor = "#F2F2F2";
  36. private static string selectColcr = "#97D7FB";
  37. private static string borderBackground = "#FFFFFF";
  38. private static string borderBrush = "#000000";
  39. private bool isSelete = false;
  40. private IRegionManager region;
  41. private IDialogService dialogs;
  42. public CPDFViewer PDFViewer;
  43. private ObservableCollection<CPDFBookmark> bookmarklist;
  44. public ObservableCollection<CPDFBookmark> Bookmarklist
  45. {
  46. get
  47. {
  48. return bookmarklist;
  49. }
  50. set
  51. {
  52. SetProperty(ref bookmarklist, value);
  53. }
  54. }
  55. private Visibility isEmpty;
  56. public Visibility IsEmptyPanelVisibility
  57. {
  58. get
  59. {
  60. return isEmpty;
  61. }
  62. set
  63. {
  64. SetProperty(ref isEmpty, value);
  65. }
  66. }
  67. #endregion 属性
  68. #region 命令
  69. public DelegateCommand<object> MouseClickCommand { get; set; }
  70. public DelegateCommand<object> MouseEnterCommand { get; set; }
  71. public DelegateCommand<object> MouseLeaveCommand { get; set; }
  72. public DelegateCommand<object> LostFocusCommand { get; set; }
  73. public DelegateCommand<object> GotFocusCommand { get; set; }
  74. public DelegateCommand<object> AddBookmarkCommand { get; set; }
  75. public DelegateCommand<object> ListViewItemMouseDownCommand { get; set; }
  76. public DelegateCommand<object> RenameCommand { get; set; }
  77. public DelegateCommand<object> EditPageIndexCommand { get; set; }
  78. public DelegateCommand<object> DeleteCommand { get; set; }
  79. #endregion 命令
  80. public BookmarkContentViewModel(IRegionManager regionManager, IDialogService dialogService)
  81. {
  82. region = regionManager;
  83. dialogs = dialogService;
  84. //MouseClickCommand = new DelegateCommand<object>(MouseClickEvent);
  85. //MouseEnterCommand = new DelegateCommand<object>(MouseEnterEvent);
  86. LostFocusCommand = new DelegateCommand<object>(LostFocusEvent);
  87. //MouseLeaveCommand = new DelegateCommand<object>(MouseLeaveEvent);
  88. //GotFocusCommand = new DelegateCommand<object>(GotFocusEvent);
  89. AddBookmarkCommand = new DelegateCommand<object>(AddBookmarkEvent);
  90. ListViewItemMouseDownCommand = new DelegateCommand<object>(ListViewItemMouseLeftButtonDownEvent);
  91. RenameCommand = new DelegateCommand<object>(RenameEvent);
  92. DeleteCommand = new DelegateCommand<object>(DelegateEvent);
  93. EditPageIndexCommand = new DelegateCommand<object>(EditPageIndexEvent);
  94. }
  95. private void EditPageIndexEvent(object obj)
  96. {
  97. CPDFBookmark bookmark = obj as CPDFBookmark;
  98. if (bookmark != null)
  99. {
  100. if (RemoveBookMark(bookmark))
  101. {
  102. bookmarklist.Remove(bookmark);
  103. CPDFBookmark cPDFBookmark = new CPDFBookmark();
  104. bookmark.Title = bookmark.Title;
  105. bookmark.Date = DateTime.Now.ToString(@"yyyyMMddHHmmsszzz\'").Replace(':', '\'') + "\n";
  106. bookmark.PageIndex = PDFViewer.CurrentIndex;
  107. if (PDFViewer.Document.AddBookmark(bookmark))
  108. {
  109. PDFViewer.UndoManager.CanSave = true;
  110. Bookmarklist = new ObservableCollection<CPDFBookmark>(PDFViewer.Document.GetBookmarkList().OrderBy(d => d.Title));
  111. }
  112. }
  113. //var list = PDFViewer.Document.GetBookmarkList().FindAll(q => q.PageIndex == bookmark.PageIndex && q.Title==bookmark.Title);
  114. //CPDFBookmark cPDF = list[0];
  115. //cPDF.PageIndex = PDFViewer.CurrentIndex;
  116. }
  117. }
  118. private void DelegateEvent(object obj)
  119. {
  120. CPDFBookmark bookmark = obj as CPDFBookmark;
  121. if (bookmark != null)
  122. {
  123. if (RemoveBookMark(bookmark))
  124. bookmarklist.Remove(bookmark);
  125. //ListBoxItem myListBoxItem = (ListBoxItem)(myListBox.ItemContainerGenerator.ContainerFromItem(myListBox.Items.CurrentItem));
  126. }
  127. }
  128. private void RenameEvent(object obj)
  129. {
  130. CPDFBookmark bookmark = obj as CPDFBookmark;
  131. if (bookmark != null)
  132. {
  133. //var result = PDFViewer.Document.EditBookmark(bookmark.PageIndex, textBox.Text.Trim());
  134. //if (result)
  135. //{
  136. // bookmark.Title = textBox.Text.Trim();
  137. // PDFViewer.UndoManager.CanSave = true;
  138. //}
  139. }
  140. }
  141. private bool RemoveBookMark(CPDFBookmark item)
  142. {
  143. var data = item;
  144. if (data == null)
  145. return false;
  146. return PDFViewer.Document.RemoveBookmark(data.PageIndex);
  147. }
  148. private void GotFocusEvent(object obj)
  149. {
  150. if (obj is CompositeCommandParameter)
  151. {
  152. CompositeCommandParameter parameter = (CompositeCommandParameter)obj;
  153. if (parameter.Parameter is TextBox)
  154. {
  155. TextBox textBox = (TextBox)parameter.Parameter;
  156. textBox.SelectAll();
  157. textBox.PreviewMouseDown -= new MouseButtonEventHandler(TxtTitleInput_PreviewMouseLeftButtonDown);
  158. }
  159. }
  160. }
  161. private void LostFocusEvent(object obj)
  162. {
  163. TextBox textBox = null;
  164. ListViewItem listViewItem = null;
  165. if (obj is CompositeCommandParameter)
  166. {
  167. CompositeCommandParameter parameter = (CompositeCommandParameter)obj;
  168. if (parameter.Parameter is TextBox)
  169. {
  170. textBox = (TextBox)parameter.Parameter;
  171. //textBox.PreviewMouseDown += new MouseButtonEventHandler(TxtTitleInput_PreviewMouseLeftButtonDown);
  172. listViewItem = CommonHelper.FindVisualParent<ListViewItem>(textBox);
  173. }
  174. }
  175. if (obj is ListBoxItem)
  176. {
  177. listViewItem = (ListViewItem)(obj);
  178. textBox = CommonHelper.FindVisualChild<TextBox>(listViewItem);
  179. }
  180. if (listViewItem != null)
  181. {
  182. var data = listViewItem.DataContext as CPDFBookmark;
  183. if (data == null)
  184. return;
  185. var result = PDFViewer.Document.EditBookmark(data.PageIndex, textBox.Text.Trim());
  186. if (result)
  187. {
  188. data.Title = textBox.Text.Trim();
  189. PDFViewer.UndoManager.CanSave = true;
  190. }
  191. }
  192. }
  193. private void TxtTitleInput_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  194. {
  195. TextBox textBox = sender as TextBox;
  196. textBox.Focus();
  197. e.Handled = true;
  198. }
  199. private void ListViewItemMouseLeftButtonDownEvent(object obj)
  200. {
  201. ListBoxItem listBoxItem = (obj as ListBoxItem);
  202. if (listBoxItem != null)
  203. {
  204. //box.Background = listBoxItem.Background;
  205. //box.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(selectColcr));
  206. if (!(listBoxItem.DataContext is CPDFBookmark))
  207. {
  208. return;
  209. }
  210. int index = (listBoxItem.DataContext as CPDFBookmark).PageIndex;
  211. PDFViewer.GoToPage(index);
  212. isSelete = false;
  213. }
  214. //MessageBox.Show("dsafa");
  215. }
  216. private void MouseLeaveEvent(object obj)
  217. {
  218. if (obj is CompositeCommandParameter)
  219. {
  220. CompositeCommandParameter parameter = (CompositeCommandParameter)obj;
  221. if (parameter.Parameter is Grid)
  222. {
  223. Grid grid = parameter.Parameter as Grid;
  224. if (grid != null)
  225. {
  226. TextBlock box = CommonHelper.FindVisualChild<TextBlock>(grid);
  227. ListBoxItem myListBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(grid);
  228. box.Visibility = Visibility.Visible;
  229. //if (myListBoxItem.IsSelected == true)
  230. //{
  231. // box.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(defaultColor));
  232. //}
  233. //if (myListBoxItem.IsMouseOver == true)
  234. //{
  235. // box.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(defaultColor));
  236. //}
  237. //else
  238. //{
  239. // box.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(defaultColor));
  240. //}
  241. //myListBoxItem.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(defaultColor));
  242. //grid.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(defaultColor));
  243. }
  244. }
  245. }
  246. }
  247. public void MouseEnterEvent(object obj)
  248. {
  249. if (obj is CompositeCommandParameter)
  250. {
  251. CompositeCommandParameter parameter = (CompositeCommandParameter)obj;
  252. if (parameter.Parameter is Grid)
  253. {
  254. Grid grid = parameter.Parameter as Grid;
  255. if (grid != null)
  256. {
  257. TextBlock box = CommonHelper.FindVisualChild<TextBlock>(grid);
  258. ListBoxItem listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(grid);
  259. if (box != null)
  260. {
  261. box.Visibility = Visibility.Visible;
  262. //box.Background = listBoxItem.Background;
  263. //box.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(hoverColor));
  264. }
  265. //myListBoxItem.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(hoverColor));
  266. //grid.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(hoverColor));
  267. }
  268. }
  269. }
  270. }
  271. private void AddBookmarkEvent(object obj)
  272. {
  273. int index = PDFViewer.CurrentIndex;
  274. string mark = string.Format($"第{index + 1}页");
  275. var list = PDFViewer.Document.GetBookmarkList().FindAll(q => q.PageIndex == index);
  276. if (list.Count > 0)
  277. {
  278. System.Windows.Controls.ListView listView = obj as System.Windows.Controls.ListView;
  279. if (listView != null)
  280. {
  281. //listView.Items.CurrentItem = list[0];
  282. ListBoxItem myListBoxItem = (ListBoxItem)(listView.ItemContainerGenerator.ContainerFromItem(list[0]));
  283. //ContentPresenter myContentPresenter = CommonHelper.FindVisualChild<ContentPresenter>(myListBoxItem);
  284. //DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
  285. //Grid item = (Grid)myDataTemplate.FindName("Grid", myContentPresenter);
  286. //Grid grid = CommonHelper.FindVisualChild<Grid>(myListBoxItem);
  287. if (myListBoxItem.IsSelected == false)
  288. {
  289. myListBoxItem.IsSelected = true;
  290. myListBoxItem.Focus();
  291. }
  292. //listView.SelectedItem = myListBoxItem;
  293. //myListBoxItem.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(selectColcr));
  294. return;
  295. }
  296. }
  297. DialogParameters value = new DialogParameters();
  298. value.Add(ParameterNames.Bookmark, mark);
  299. value.Add(ParameterNames.Title, "创建一个新的书签");
  300. dialogs.ShowDialog(DialogNames.AddBookmarkDialog, value, e =>
  301. {
  302. if (e.Result == ButtonResult.OK && e.Parameters != null)
  303. {
  304. if (e.Parameters.ContainsKey(ParameterNames.Bookmark))
  305. {
  306. mark = e.Parameters.GetValue<string>(ParameterNames.Bookmark).ToString();
  307. CPDFBookmark bookmark = new CPDFBookmark();
  308. bookmark.Title = mark;
  309. bookmark.Date = DateTime.Now.ToString(@"yyyyMMddHHmmsszzz\'").Replace(':', '\'') + "\n";
  310. bookmark.PageIndex = PDFViewer.CurrentIndex;
  311. if (PDFViewer.Document.AddBookmark(bookmark))
  312. {
  313. PDFViewer.UndoManager.CanSave = true;
  314. Bookmarklist = new ObservableCollection<CPDFBookmark>(PDFViewer.Document.GetBookmarkList().OrderBy(d => d.Title));
  315. }
  316. }
  317. }
  318. });
  319. }
  320. public void MouseClickEvent(object obj)
  321. {
  322. if (obj is MouseButtonEventArgs)
  323. {
  324. var mouse = (MouseButtonEventArgs)obj;
  325. if (mouse.LeftButton == MouseButtonState.Pressed)
  326. {
  327. TextBlock box = null;
  328. Grid grid = null;
  329. if (mouse.ClickCount >= 2)
  330. {
  331. if (mouse.Source is Grid)
  332. {
  333. grid = mouse.Source as Grid;
  334. //grid.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(selectColcr));
  335. if (grid != null)
  336. {
  337. box = CommonHelper.FindVisualChild<TextBlock>(grid);
  338. //grid.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(selectColcr));
  339. if (box != null)
  340. {
  341. box.Visibility = Visibility.Collapsed;
  342. //TextBox textBox = CommonHelper.FindVisualChild<TextBox>((box.Parent as StackPanel));
  343. //FocusManager.SetFocusedElement(box.Parent, textBox);
  344. //Keyboard.Focus(textBox);
  345. //textBox.IsHitTestVisible = true;
  346. //textBox.Focus();
  347. }
  348. }
  349. }
  350. if (mouse.Source is TextBlock)
  351. {
  352. box = (TextBlock)mouse.Source;
  353. grid = (box.Parent as StackPanel).Parent as Grid;
  354. //grid.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(selectColcr));
  355. if (box != null)
  356. {
  357. box.Visibility = Visibility.Collapsed;
  358. //TextBox textBox = CommonHelper.FindVisualChild<TextBox>((box.Parent as StackPanel));
  359. //FocusManager.SetFocusedElement(box.Parent, textBox);
  360. //Keyboard.Focus(textBox);
  361. //textBox.IsHitTestVisible = true;
  362. //textBox.Focus();
  363. }
  364. }
  365. }
  366. //MessageBox.Show("dsafa");
  367. }
  368. }
  369. }
  370. public void OnNavigatedTo(NavigationContext navigationContext)
  371. {
  372. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  373. if (PDFViewer == null)
  374. {
  375. return;
  376. }
  377. Bookmarklist = new ObservableCollection<CPDFBookmark>(PDFViewer.Document.GetBookmarkList().OrderBy(d => d.Title));
  378. if (Bookmarklist.Count < 1)
  379. {
  380. IsEmptyPanelVisibility = Visibility.Visible;
  381. return;
  382. }
  383. else
  384. {
  385. IsEmptyPanelVisibility = Visibility.Hidden;
  386. return;
  387. }
  388. }
  389. public bool IsNavigationTarget(NavigationContext navigationContext)
  390. {
  391. return true;
  392. }
  393. public void OnNavigatedFrom(NavigationContext navigationContext)
  394. {
  395. }
  396. }
  397. }