BookmarkContentViewModel.cs 21 KB

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