OutLineControl.xaml.cs 21 KB

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