OutLineControl.xaml.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. using ComPDFKit.PDFDocument;
  2. using PDF_Master.CustomControl;
  3. using PDF_Master.EventAggregators;
  4. using PDF_Master.Helper;
  5. using PDF_Master.Model.BOTA;
  6. using PDF_Master.ViewModels.BOTA;
  7. using Prism.Events;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Collections.ObjectModel;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Controls;
  16. using System.Windows.Controls.Primitives;
  17. using System.Windows.Data;
  18. using System.Windows.Documents;
  19. using System.Windows.Input;
  20. using System.Windows.Media;
  21. using System.Windows.Media.Imaging;
  22. using System.Windows.Navigation;
  23. using System.Windows.Shapes;
  24. using static Dropbox.Api.Files.WriteMode;
  25. using MenuItem = System.Windows.Controls.MenuItem;
  26. namespace PDF_Master.Views.BOTA
  27. {
  28. /// <summary>
  29. /// OutLineControl.xaml 的交互逻辑
  30. /// </summary>
  31. public partial class OutLineControl : UserControl
  32. {
  33. /// <summary>
  34. /// 当前显示了线段的Item
  35. /// </summary>
  36. private TreeViewItem Treeviewitem = null;
  37. /// <summary>
  38. /// 需要跳转页面的Item
  39. /// </summary>
  40. private TreeViewItem GotoPageItem = null;
  41. /// <summary>
  42. /// 需要重命名的Item的DataContext
  43. /// </summary>
  44. public OutlineNode ReNameOutlineNode = null;
  45. /// <summary>
  46. /// 正在拖拽
  47. /// </summary>
  48. private bool onDrop = false;
  49. public bool IsReName = false;
  50. private string unicode = "";
  51. /// <summary>
  52. /// 判断是否拖拽到了新窗口
  53. /// </summary>
  54. /// <returns></returns>
  55. private bool IsInNowWindow()
  56. {
  57. if (App.DropStartWindow != null)
  58. {
  59. Window frm = Window.GetWindow(OutlineView);
  60. if (frm != App.DropStartWindow)
  61. {
  62. return true;
  63. }
  64. return false;
  65. }
  66. return false;
  67. }
  68. public OutLineControl()
  69. {
  70. InitializeComponent();
  71. unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  72. }
  73. public OutLineControl(IEventAggregator eventAggregator) : this()
  74. {
  75. //Windows:BOTA列表,移除点击阅读页取消选中逻辑
  76. //eventAggregator.GetEvent<CleanSelectAllEvent>().Subscribe(CleanSelectAll, e => e.Unicode == unicode);
  77. }
  78. private void CleanSelectAll(CleanSelectAllArgs obj)
  79. {
  80. OutlineNode outlineNode = OutlineView.SelectedItem as OutlineNode;
  81. if (outlineNode != null)
  82. {
  83. outlineNode.IsSelected = false;
  84. }
  85. }
  86. private void BtnMore_Click(object sender, RoutedEventArgs e)
  87. {
  88. MenuMore.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
  89. MenuMore.PlacementTarget = BtnMore;
  90. MenuMore.IsOpen = true;
  91. }
  92. private void OutlineView_PreviewMouseMove(object sender, MouseEventArgs e)
  93. {
  94. if (IsInNowWindow())
  95. {
  96. return;
  97. }
  98. if (e.LeftButton == MouseButtonState.Pressed && !IsReName)
  99. {
  100. var pos = e.GetPosition(OutlineView);
  101. HitTestResult result = VisualTreeHelper.HitTest(OutlineView, pos);
  102. if (result == null)
  103. {
  104. return;
  105. }
  106. var treeViewItem = CommonHelper.FindVisualParent<TreeViewItem>(result.VisualHit);
  107. if (treeViewItem == null)
  108. {
  109. return;
  110. }
  111. DataObject dataObj = new DataObject(treeViewItem);
  112. DragDrop.DoDragDrop(OutlineView, dataObj, DragDropEffects.Move);
  113. return;
  114. }
  115. }
  116. /// <summary>
  117. /// 拖拽判断区域以及显示什么线
  118. /// </summary>
  119. /// <param name="sender"></param>
  120. /// <param name="e"></param>
  121. private void Grid_DragOver(object sender, DragEventArgs e)
  122. {
  123. if (IsInNowWindow())
  124. {
  125. e.Effects = DragDropEffects.None;
  126. e.Handled = true;
  127. return;
  128. }
  129. (DataContext as OutLineControlViewModel).IsOnDrop = true;
  130. TreeViewItem sourceitem = e.Data.GetData(typeof(TreeViewItem)) as TreeViewItem;
  131. if (sourceitem == null)
  132. {
  133. return;
  134. }
  135. Point pos = e.GetPosition(OutlineView);
  136. HitTestResult result = VisualTreeHelper.HitTest(OutlineView, pos);
  137. if (result != null)
  138. {
  139. TreeViewItem treeviewitem = CommonHelper.FindVisualParent<TreeViewItem>(result.VisualHit);
  140. if (treeviewitem != null)
  141. {
  142. //控制最顶端的线
  143. if (pos.Y <= 9)
  144. {
  145. if (Treeviewitem == null)
  146. {
  147. Treeviewitem = treeviewitem;
  148. }
  149. (DataContext as OutLineControlViewModel).IsInsertHead = true;
  150. (Treeviewitem.DataContext as OutlineNode).IsInsertCurrentLayer = false;
  151. (Treeviewitem.DataContext as OutlineNode).IsInsertNextLayer = false;
  152. }
  153. else
  154. {
  155. (DataContext as OutLineControlViewModel).IsInsertHead = false;
  156. if (Treeviewitem == null)
  157. {
  158. Treeviewitem = treeviewitem;
  159. }
  160. else if (!Treeviewitem.Equals(treeviewitem))
  161. {
  162. (Treeviewitem.DataContext as OutlineNode).IsInsertCurrentLayer = false;
  163. (Treeviewitem.DataContext as OutlineNode).IsInsertNextLayer = false;
  164. Treeviewitem = treeviewitem;
  165. }
  166. else
  167. {
  168. (Treeviewitem.DataContext as OutlineNode).IsInsertCurrentLayer = false;
  169. (Treeviewitem.DataContext as OutlineNode).IsInsertNextLayer = false;
  170. }
  171. if (Treeviewitem.Equals(sourceitem))
  172. {
  173. return;
  174. }
  175. //鼠标位于上半部分显示实线,下半部分显示虚线
  176. Point p = treeviewitem.TranslatePoint(new Point(0, 0), OutlineView);
  177. if (pos.Y < p.Y + 16)
  178. {
  179. (treeviewitem.DataContext as OutlineNode).IsInsertCurrentLayer = true;
  180. }
  181. else
  182. {
  183. (treeviewitem.DataContext as OutlineNode).IsInsertNextLayer = true;
  184. }
  185. }
  186. }
  187. }
  188. }
  189. private void Grid_DragLeave(object sender, DragEventArgs e)
  190. {
  191. HiddenaAllLine();
  192. }
  193. private void OutlineView_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
  194. {
  195. TextBlock text = e.OriginalSource as TextBlock;
  196. if (text == null || text.Name == "PageIndex")
  197. {
  198. return;
  199. }
  200. TreeView tree = sender as TreeView;
  201. if (tree == null)
  202. {
  203. return;
  204. }
  205. OutlineNode viewitem = tree.SelectedItem as OutlineNode;
  206. if (viewitem == null)
  207. {
  208. return;
  209. }
  210. ReNameOutlineNode = viewitem;
  211. ReNameOutlineNode.IsReName = Visibility.Collapsed;
  212. e.Handled = true;
  213. }
  214. private void OutlineView_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  215. {
  216. (DataContext as OutLineControlViewModel).IsInsertHead = false;
  217. if (Treeviewitem != null)
  218. {
  219. (Treeviewitem.DataContext as OutlineNode).IsInsertCurrentLayer = false;
  220. (Treeviewitem.DataContext as OutlineNode).IsInsertNextLayer = false;
  221. Treeviewitem = null;
  222. }
  223. }
  224. private void OutlineView_Drop(object sender, DragEventArgs e)
  225. {
  226. if (IsInNowWindow())
  227. {
  228. e.Effects = DragDropEffects.None;
  229. e.Handled = true;
  230. return;
  231. }
  232. onDrop = false;
  233. TreeViewItem souredata = e.Data.GetData(typeof(TreeViewItem)) as TreeViewItem;
  234. if (souredata == null)
  235. {
  236. return;
  237. }
  238. OutlineNode soureoutlineNode = souredata.DataContext as OutlineNode;
  239. Point pos = e.GetPosition(OutlineView);
  240. HitTestResult result = VisualTreeHelper.HitTest(OutlineView, pos);
  241. if (result == null)
  242. {
  243. HiddenaAllLine();
  244. return;
  245. }
  246. TreeViewItem targetitem = CommonHelper.FindVisualParent<TreeViewItem>(result.VisualHit);
  247. if (targetitem == null)
  248. {
  249. HiddenaAllLine();
  250. return;
  251. }
  252. OutlineNode targetoutlineNode = targetitem.DataContext as OutlineNode;
  253. if (soureoutlineNode.Equals(targetoutlineNode))
  254. {
  255. HiddenaAllLine();
  256. return;
  257. }
  258. (DataContext as OutLineControlViewModel).MoveOutLine(targetoutlineNode, soureoutlineNode);
  259. HiddenaAllLine();
  260. (DataContext as OutLineControlViewModel).Updata(true);
  261. }
  262. /// <summary>
  263. /// 清理线条显示,以及相关缓存对象
  264. /// </summary>
  265. private void HiddenaAllLine()
  266. {
  267. (DataContext as OutLineControlViewModel).IsOnDrop = false;
  268. (DataContext as OutLineControlViewModel).IsInsertHead = false;
  269. if (Treeviewitem != null)
  270. {
  271. (Treeviewitem.DataContext as OutlineNode).IsInsertCurrentLayer = false;
  272. (Treeviewitem.DataContext as OutlineNode).IsInsertNextLayer = false;
  273. Treeviewitem = null;
  274. }
  275. }
  276. private void ReName_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  277. {
  278. TextBox ReNameText = sender as TextBox;
  279. if (ReNameText == null)
  280. {
  281. return;
  282. }
  283. if (ReNameText.Visibility == Visibility.Visible)
  284. {
  285. if (ReNameOutlineNode != null)
  286. {
  287. ReNameText.Text = ReNameOutlineNode.Outline.Title;
  288. }
  289. ReNameText.Focus();
  290. ReNameText.SelectAll();
  291. IsReName = true;
  292. }
  293. }
  294. private void RenameGrid_LostFocus(object sender, RoutedEventArgs e)
  295. {
  296. TextBox newdto = e.OriginalSource as TextBox;
  297. if (newdto == null)
  298. {
  299. IsReName = false;
  300. return;
  301. }
  302. string newtitle = string.IsNullOrEmpty(newdto.Text) ? "" : newdto.Text;
  303. Grid senderdto = sender as Grid;
  304. if (senderdto == null)
  305. {
  306. IsReName = false;
  307. return;
  308. }
  309. senderdto.Visibility = Visibility.Collapsed;
  310. if (ReNameOutlineNode != null)
  311. {
  312. CPDFOutline result = ReNameOutlineNode.Outline;
  313. if (result != null)
  314. {
  315. if (newtitle == result.Title)
  316. {
  317. IsReName = false;
  318. return;
  319. }
  320. (DataContext as OutLineControlViewModel).SetTitle(result, newtitle);
  321. }
  322. (DataContext as OutLineControlViewModel).Updata(false);
  323. }
  324. IsReName = false;
  325. }
  326. private void ToolTip_Opened(object sender, RoutedEventArgs e)
  327. {
  328. CustomImageControl imageControl = CommonHelper.FindVisualChild<CustomImageControl>(sender as ToolTip);
  329. OutlineNode outline = imageControl.DataContext as OutlineNode;
  330. if (outline == null || string.IsNullOrEmpty(outline.PageIndex))
  331. {
  332. return;
  333. }
  334. //开始绘制
  335. DrawingContext drawDc = imageControl.OpenDraw();
  336. WriteableBitmap imageData = (DataContext as OutLineControlViewModel).LoadPreview(outline, (int)imageControl.ActualWidth, (int)imageControl.ActualHeight);
  337. if (imageData == null)
  338. {
  339. imageControl.CloseDraw();
  340. return;
  341. }
  342. drawDc.DrawImage(imageData, new Rect(0, 0, imageControl.ActualWidth, imageControl.ActualHeight));
  343. imageControl.CloseDraw();
  344. }
  345. private void ToolTip_Closed(object sender, RoutedEventArgs e)
  346. {
  347. CustomImageControl imageControl = CommonHelper.FindVisualChild<CustomImageControl>(sender as ToolTip);
  348. //清空绘制上下文数据
  349. imageControl.OpenDraw();
  350. imageControl.CloseDraw();
  351. }
  352. private void TreeViewItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  353. {
  354. //每次鼠标按下,更新开始拖拽的窗口对象
  355. MainWindow frm = Window.GetWindow(this) as MainWindow;
  356. if (frm != null)
  357. {
  358. App.DropStartWindow = frm;
  359. }
  360. if (e.ClickCount == 1)
  361. {
  362. TreeViewItem content = sender as TreeViewItem;
  363. if (content == null)
  364. return;
  365. Point p = e.GetPosition(OutlineView);
  366. HitTestResult result = VisualTreeHelper.HitTest(OutlineView, p);
  367. if (result != null)
  368. {
  369. //点击部位为箭头时,不跳转
  370. ToggleButton button = CommonHelper.FindVisualParent<ToggleButton>(result.VisualHit);
  371. if (button != null)
  372. return;
  373. Button btn = CommonHelper.FindVisualParent<Button>(result.VisualHit);
  374. if (btn != null)
  375. return;
  376. GotoPageItem = content;
  377. }
  378. }
  379. }
  380. private void TreeViewItem_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  381. {
  382. if (!onDrop)
  383. {
  384. //因为点击按下事件是按照层级传递下来的,所以取到最后一次按下的对象进行跳转,以免跳转到父级
  385. if (GotoPageItem != null)
  386. {
  387. (DataContext as OutLineControlViewModel).GoToPage(GotoPageItem);
  388. }
  389. }
  390. }
  391. private void DowngradeMenu_Click(object sender, RoutedEventArgs e)
  392. {
  393. (DataContext as OutLineControlViewModel).Downgrade((sender as MenuItem).DataContext as OutlineNode);
  394. }
  395. private void UpgradeMenu_Click(object sender, RoutedEventArgs e)
  396. {
  397. (DataContext as OutLineControlViewModel).Upgrade((sender as MenuItem).DataContext as OutlineNode);
  398. }
  399. private void DeleteMenu_Click(object sender, RoutedEventArgs e)
  400. {
  401. (DataContext as OutLineControlViewModel).RemoveOutline((sender as MenuItem).DataContext as OutlineNode);
  402. }
  403. private void RenameMenu_Click(object sender, RoutedEventArgs e)
  404. {
  405. ReNameOutlineNode = (sender as MenuItem).DataContext as OutlineNode;
  406. ReNameOutlineNode.IsReName = Visibility.Collapsed;
  407. }
  408. private void ChangeDestinationMenu_Click(object sender, RoutedEventArgs e)
  409. {
  410. AlertsMessage alertsMessage = new AlertsMessage();
  411. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("Outline_DialogContent"), App.MainPageLoader.GetString("Outline_DialogNO"), App.MainPageLoader.GetString("Outline_DialogYes"));
  412. if (alertsMessage.result == ContentResult.Ok)
  413. {
  414. (DataContext as OutLineControlViewModel).ChangeOutLineDestination((sender as MenuItem).DataContext as OutlineNode);
  415. }
  416. }
  417. private void BtnAdd_Click(object sender, RoutedEventArgs e)
  418. {
  419. AddOutlineNode();
  420. }
  421. public OutlineNode AddOutlineNode(string str = null)
  422. {
  423. int ItemIndex = (DataContext as OutLineControlViewModel).AddOutLine(OutlineView.SelectedItem as OutlineNode);
  424. OutlineView.UpdateLayout();
  425. OutlineNode node = (DataContext as OutLineControlViewModel).FindOutlineFromList((DataContext as OutLineControlViewModel).Outlinelist, OutlineView.SelectedItem as OutlineNode, ItemIndex);
  426. node.IsSelected = true;
  427. ReNameOutlineNode = node;
  428. node.IsReName = Visibility.Collapsed;
  429. if (!string.IsNullOrEmpty(str))
  430. {
  431. CPDFOutline result = node.Outline;
  432. (DataContext as OutLineControlViewModel).SetTitle(result, str);
  433. //输入框失去焦点后 会更新一次数据,这里可以不用更新
  434. //如果解开注释,会出现阅读页添加大纲后无法进入编辑模式的情况
  435. //(DataContext as OutLineControlViewModel).Updata(false);
  436. }
  437. return node;
  438. }
  439. private void AddMenu_Click(object sender, RoutedEventArgs e)
  440. {
  441. ((sender as MenuItem).DataContext as OutlineNode).IsSelected = true;
  442. int ItemIndex = (DataContext as OutLineControlViewModel).AddOutLine((sender as MenuItem).DataContext as OutlineNode);
  443. OutlineView.UpdateLayout();
  444. OutlineNode node = (DataContext as OutLineControlViewModel).FindOutlineFromList((DataContext as OutLineControlViewModel).Outlinelist, OutlineView.SelectedItem as OutlineNode, ItemIndex);
  445. node.IsSelected = true;
  446. ReNameOutlineNode = node;
  447. node.IsReName = Visibility.Collapsed;
  448. }
  449. private void AddParentMenu_Click(object sender, RoutedEventArgs e)
  450. {
  451. ((sender as MenuItem).DataContext as OutlineNode).IsSelected = true;
  452. int ItemIndex = (DataContext as OutLineControlViewModel).InsertParentOutline((sender as MenuItem).DataContext as OutlineNode);
  453. OutlineView.UpdateLayout();
  454. OutlineNode node = (DataContext as OutLineControlViewModel).FindOutlineFromList((DataContext as OutLineControlViewModel).Outlinelist, (OutlineView.SelectedItem as OutlineNode).Parent, ItemIndex);
  455. node.IsSelected = true;
  456. ReNameOutlineNode = node;
  457. node.IsReName = Visibility.Collapsed;
  458. }
  459. private void AddChlidMenu_Click(object sender, RoutedEventArgs e)
  460. {
  461. ((sender as MenuItem).DataContext as OutlineNode).IsExpanded = true;
  462. ((sender as MenuItem).DataContext as OutlineNode).IsSelected = true;
  463. int ItemIndex = (DataContext as OutLineControlViewModel).InsertChlidOutline((sender as MenuItem).DataContext as OutlineNode);
  464. OutlineView.UpdateLayout();
  465. OutlineNode node = (DataContext as OutLineControlViewModel).FindOutlineFromList((DataContext as OutLineControlViewModel).Outlinelist, (OutlineView.SelectedItem as OutlineNode).Chlidlist[ItemIndex], ItemIndex);
  466. if (node != null)
  467. {
  468. node.IsSelected = true;
  469. ReNameOutlineNode = node;
  470. node.IsReName = Visibility.Collapsed;
  471. }
  472. }
  473. private void OutlineView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
  474. {
  475. onDrop = false;
  476. }
  477. private void ReName_KeyDown(object sender, KeyEventArgs e)
  478. {
  479. if (e.Key == Key.Enter)
  480. {
  481. OutlineView.Focus();
  482. }
  483. }
  484. private void UserControl_KeyDown(object sender, KeyEventArgs e)
  485. {
  486. if (e.Key == Key.Escape)
  487. {
  488. //Windows:BOTA列表,移除Esc取消选中逻辑
  489. //OutlineNode outlineNode = OutlineView.SelectedItem as OutlineNode;
  490. //if (outlineNode != null)
  491. //{
  492. // outlineNode.IsSelected = false;
  493. // e.Handled = true;
  494. //}
  495. }
  496. if (e.Key == Key.Delete)
  497. {
  498. if (OutlineView.SelectedItem != null)
  499. {
  500. if (OutlineView.SelectedItem is OutlineNode outlineNode)
  501. {
  502. (DataContext as OutLineControlViewModel).RemoveOutline(outlineNode);
  503. }
  504. }
  505. }
  506. }
  507. private void OutlineView_PreviewMouseDown(object sender, MouseButtonEventArgs e)
  508. {
  509. if (e.LeftButton == MouseButtonState.Pressed)
  510. {
  511. var pos = e.GetPosition(OutlineView);
  512. var result = VisualTreeHelper.HitTest(OutlineView, pos);
  513. if (result != null)
  514. {
  515. //获取当前鼠标指针下的容器
  516. var listBoxItem = CommonHelper.FindVisualParent<TreeViewItem>(result.VisualHit);
  517. if (listBoxItem == null)
  518. {
  519. OutlineNode outlineNode = OutlineView.SelectedItem as OutlineNode;
  520. if (outlineNode != null)
  521. {
  522. outlineNode.IsSelected = false;
  523. }
  524. }
  525. }
  526. //BookMarkListView.Focus();
  527. }
  528. }
  529. }
  530. }