OutLineControl.xaml.cs 18 KB

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