123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470 |
- using ComPDFKit.PDFDocument;
- using PDF_Office.CustomControl;
- using PDF_Office.EventAggregators;
- using PDF_Office.Helper;
- using PDF_Office.Model.BOTA;
- using PDF_Office.ViewModels.BOTA;
- using Prism.Events;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Controls.Primitives;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using static Dropbox.Api.Files.WriteMode;
- using MenuItem = System.Windows.Controls.MenuItem;
- namespace PDF_Office.Views.BOTA
- {
- /// <summary>
- /// OutLineControl.xaml 的交互逻辑
- /// </summary>
- public partial class OutLineControl : UserControl
- {
- /// <summary>
- /// 当前显示了线段的Item
- /// </summary>
- private TreeViewItem Treeviewitem = null;
- /// <summary>
- /// 需要跳转页面的Item
- /// </summary>
- private TreeViewItem GotoPageItem = null;
- /// <summary>
- /// 需要重命名的Item的DataContext
- /// </summary>
- public OutlineNode ReNameOutlineNode = null;
- /// <summary>
- /// 正在拖拽
- /// </summary>
- private bool onDrop = false;
- public bool IsReName = false;
- public OutLineControl()
- {
- InitializeComponent();
- }
- private void BtnMore_Click(object sender, RoutedEventArgs e)
- {
- MenuMore.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
- MenuMore.PlacementTarget = BtnMore;
- MenuMore.IsOpen = true;
- }
- private void OutlineView_PreviewMouseMove(object sender, MouseEventArgs e)
- {
- if (e.LeftButton == MouseButtonState.Pressed && !IsReName)
- {
- var pos = e.GetPosition(OutlineView);
- HitTestResult result = VisualTreeHelper.HitTest(OutlineView, pos);
- if (result == null)
- {
- return;
- }
- var treeViewItem = CommonHelper.FindVisualParent<TreeViewItem>(result.VisualHit);
- if (treeViewItem == null)
- {
- return;
- }
- DataObject dataObj = new DataObject(treeViewItem);
- DragDrop.DoDragDrop(OutlineView, dataObj, DragDropEffects.Move);
- return;
- }
- }
- /// <summary>
- /// 拖拽判断区域以及显示什么线
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void Grid_DragOver(object sender, DragEventArgs e)
- {
- (DataContext as OutLineControlViewModel).IsOnDrop = true;
- TreeViewItem sourceitem = e.Data.GetData(typeof(TreeViewItem)) as TreeViewItem;
- if (sourceitem == null)
- {
- return;
- }
- Point pos = e.GetPosition(OutlineView);
- HitTestResult result = VisualTreeHelper.HitTest(OutlineView, pos);
- if (result != null)
- {
- TreeViewItem treeviewitem = CommonHelper.FindVisualParent<TreeViewItem>(result.VisualHit);
- if (treeviewitem != null)
- {
- //控制最顶端的线
- if (pos.Y <= 9)
- {
- if (Treeviewitem == null)
- {
- Treeviewitem = treeviewitem;
- }
- (DataContext as OutLineControlViewModel).IsInsertHead = true;
- (Treeviewitem.DataContext as OutlineNode).IsInsertCurrentLayer = false;
- (Treeviewitem.DataContext as OutlineNode).IsInsertNextLayer = false;
- }
- else
- {
- (DataContext as OutLineControlViewModel).IsInsertHead = false;
- if (Treeviewitem == null)
- {
- Treeviewitem = treeviewitem;
- }
- else if (!Treeviewitem.Equals(treeviewitem))
- {
- (Treeviewitem.DataContext as OutlineNode).IsInsertCurrentLayer = false;
- (Treeviewitem.DataContext as OutlineNode).IsInsertNextLayer = false;
- Treeviewitem = treeviewitem;
- }
- else
- {
- (Treeviewitem.DataContext as OutlineNode).IsInsertCurrentLayer = false;
- (Treeviewitem.DataContext as OutlineNode).IsInsertNextLayer = false;
- }
- if (Treeviewitem.Equals(sourceitem))
- {
- return;
- }
- //鼠标位于上半部分显示实线,下半部分显示虚线
- Point p = treeviewitem.TranslatePoint(new Point(0, 0), OutlineView);
- if (pos.Y < p.Y + 16)
- {
- (treeviewitem.DataContext as OutlineNode).IsInsertCurrentLayer = true;
- }
- else
- {
- (treeviewitem.DataContext as OutlineNode).IsInsertNextLayer = true;
- }
- }
- }
- }
- }
- private void Grid_DragLeave(object sender, DragEventArgs e)
- {
- HiddenaAllLine();
- }
- private void OutlineView_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
- {
- TextBlock text = e.OriginalSource as TextBlock;
- if (text == null || text.Name == "PageIndex")
- {
- return;
- }
- TreeView tree = sender as TreeView;
- if (tree == null)
- {
- return;
- }
- OutlineNode viewitem = tree.SelectedItem as OutlineNode;
- if (viewitem == null)
- {
- return;
- }
- ReNameOutlineNode = viewitem;
- ReNameOutlineNode.IsReName = Visibility.Collapsed;
- e.Handled = true;
- }
- private void OutlineView_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
- {
- (DataContext as OutLineControlViewModel).IsInsertHead = false;
- if (Treeviewitem != null)
- {
- (Treeviewitem.DataContext as OutlineNode).IsInsertCurrentLayer = false;
- (Treeviewitem.DataContext as OutlineNode).IsInsertNextLayer = false;
- Treeviewitem = null;
- }
- }
- private void OutlineView_Drop(object sender, DragEventArgs e)
- {
- onDrop = false;
- TreeViewItem souredata = e.Data.GetData(typeof(TreeViewItem)) as TreeViewItem;
- if (souredata == null)
- {
- return;
- }
- OutlineNode soureoutlineNode = souredata.DataContext as OutlineNode;
- Point pos = e.GetPosition(OutlineView);
- HitTestResult result = VisualTreeHelper.HitTest(OutlineView, pos);
- if (result == null)
- {
- HiddenaAllLine();
- return;
- }
- TreeViewItem targetitem = CommonHelper.FindVisualParent<TreeViewItem>(result.VisualHit);
- if (targetitem == null)
- {
- HiddenaAllLine();
- return;
- }
- OutlineNode targetoutlineNode = targetitem.DataContext as OutlineNode;
- if (soureoutlineNode.Equals(targetoutlineNode))
- {
- HiddenaAllLine();
- return;
- }
- (DataContext as OutLineControlViewModel).MoveOutLine(targetoutlineNode, soureoutlineNode);
- HiddenaAllLine();
- (DataContext as OutLineControlViewModel).Updata(true);
- }
- /// <summary>
- /// 清理线条显示,以及相关缓存对象
- /// </summary>
- private void HiddenaAllLine()
- {
- (DataContext as OutLineControlViewModel).IsOnDrop = false;
- (DataContext as OutLineControlViewModel).IsInsertHead = false;
- if (Treeviewitem != null)
- {
- (Treeviewitem.DataContext as OutlineNode).IsInsertCurrentLayer = false;
- (Treeviewitem.DataContext as OutlineNode).IsInsertNextLayer = false;
- Treeviewitem = null;
- }
- }
- private void ReName_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
- {
- TextBox ReNameText = sender as TextBox;
- if (ReNameText == null)
- {
- return;
- }
- if (ReNameText.Visibility == Visibility.Visible)
- {
- if (ReNameOutlineNode != null)
- {
- ReNameText.Text = ReNameOutlineNode.Outline.Title;
- }
- ReNameText.Focus();
- ReNameText.SelectAll();
- IsReName = true;
- }
- }
- private void RenameGrid_LostFocus(object sender, RoutedEventArgs e)
- {
- TextBox newdto = e.OriginalSource as TextBox;
- if (newdto == null)
- {
- IsReName = false;
- return;
- }
- string newtitle = string.IsNullOrEmpty(newdto.Text) ? "" : newdto.Text;
- Grid senderdto = sender as Grid;
- if (senderdto == null)
- {
- IsReName = false;
- return;
- }
- senderdto.Visibility = Visibility.Collapsed;
- if (ReNameOutlineNode != null)
- {
- CPDFOutline result = ReNameOutlineNode.Outline;
- if (result != null)
- {
- if (newtitle == result.Title)
- {
- IsReName = false;
- return;
- }
- (DataContext as OutLineControlViewModel).SetTitle(result, newtitle);
- }
- (DataContext as OutLineControlViewModel).Updata(false);
- }
- IsReName = false;
- }
- private void ToolTip_Opened(object sender, RoutedEventArgs e)
- {
- CustomImageControl imageControl = CommonHelper.FindVisualChild<CustomImageControl>(sender as ToolTip);
- OutlineNode outline = imageControl.DataContext as OutlineNode;
- if (outline == null || string.IsNullOrEmpty(outline.PageIndex))
- {
- return;
- }
- //开始绘制
- DrawingContext drawDc = imageControl.OpenDraw();
- WriteableBitmap imageData = (DataContext as OutLineControlViewModel).LoadPreview(outline, (int)imageControl.ActualWidth, (int)imageControl.ActualHeight);
- if (imageData == null)
- {
- imageControl.CloseDraw();
- return;
- }
- drawDc.DrawImage(imageData, new Rect(0, 0, imageControl.ActualWidth, imageControl.ActualHeight));
- imageControl.CloseDraw();
- }
- private void ToolTip_Closed(object sender, RoutedEventArgs e)
- {
- CustomImageControl imageControl = CommonHelper.FindVisualChild<CustomImageControl>(sender as ToolTip);
- //清空绘制上下文数据
- imageControl.OpenDraw();
- imageControl.CloseDraw();
- }
- private void TreeViewItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- if (e.ClickCount == 1)
- {
- TreeViewItem content = sender as TreeViewItem;
- if (content == null)
- return;
- Point p = e.GetPosition(OutlineView);
- HitTestResult result = VisualTreeHelper.HitTest(OutlineView, p);
- if (result != null)
- {
- //点击部位为箭头时,不跳转
- ToggleButton button = CommonHelper.FindVisualParent<ToggleButton>(result.VisualHit);
- if (button != null)
- return;
- Button btn = CommonHelper.FindVisualParent<Button>(result.VisualHit);
- if (btn != null)
- return;
- GotoPageItem = content;
- }
- }
- }
- private void TreeViewItem_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
- {
- if (!onDrop)
- {
- //因为点击按下事件是按照层级传递下来的,所以取到最后一次按下的对象进行跳转,以免跳转到父级
- if (GotoPageItem != null)
- {
- (DataContext as OutLineControlViewModel).GoToPage(GotoPageItem);
- }
- }
- }
- private void DowngradeMenu_Click(object sender, RoutedEventArgs e)
- {
- (DataContext as OutLineControlViewModel).Downgrade((sender as MenuItem).DataContext as OutlineNode);
- }
- private void UpgradeMenu_Click(object sender, RoutedEventArgs e)
- {
- (DataContext as OutLineControlViewModel).Upgrade((sender as MenuItem).DataContext as OutlineNode);
- }
- private void DeleteMenu_Click(object sender, RoutedEventArgs e)
- {
- (DataContext as OutLineControlViewModel).RemoveOutline((sender as MenuItem).DataContext as OutlineNode);
- }
- private void RenameMenu_Click(object sender, RoutedEventArgs e)
- {
- ReNameOutlineNode = (sender as MenuItem).DataContext as OutlineNode;
- ReNameOutlineNode.IsReName = Visibility.Collapsed;
- }
- private void ChangeDestinationMenu_Click(object sender, RoutedEventArgs e)
- {
- AlertsMessage alertsMessage = new AlertsMessage();
- alertsMessage.ShowDialog("", App.MainPageLoader.GetString("Outline_DialogContent"), App.MainPageLoader.GetString("Outline_DialogNO"), App.MainPageLoader.GetString("Outline_DialogYes"));
- if (alertsMessage.result == ContentResult.Ok)
- {
- (DataContext as OutLineControlViewModel).ChangeOutLineDestination((sender as MenuItem).DataContext as OutlineNode);
- }
- }
- private void BtnAdd_Click(object sender, RoutedEventArgs e)
- {
- AddOutlineNode();
- }
- public OutlineNode AddOutlineNode(string str = null)
- {
- int ItemIndex = (DataContext as OutLineControlViewModel).AddOutLine(OutlineView.SelectedItem as OutlineNode);
- OutlineView.UpdateLayout();
- OutlineNode node = (DataContext as OutLineControlViewModel).FindOutlineFromList((DataContext as OutLineControlViewModel).Outlinelist, OutlineView.SelectedItem as OutlineNode, ItemIndex);
- node.IsSelected = true;
- ReNameOutlineNode = node;
- node.IsReName = Visibility.Collapsed;
- if (!string.IsNullOrEmpty(str))
- {
- CPDFOutline result = node.Outline;
- (DataContext as OutLineControlViewModel).SetTitle(result, str);
- //输入框失去焦点后 会更新一次数据,这里可以不用更新
- //如果解开注释,会出现阅读页添加大纲后无法进入编辑模式的情况
- //(DataContext as OutLineControlViewModel).Updata(false);
- }
- return node;
- }
- private void AddMenu_Click(object sender, RoutedEventArgs e)
- {
- ((sender as MenuItem).DataContext as OutlineNode).IsSelected = true;
- int ItemIndex = (DataContext as OutLineControlViewModel).AddOutLine((sender as MenuItem).DataContext as OutlineNode);
- OutlineView.UpdateLayout();
- OutlineNode node = (DataContext as OutLineControlViewModel).FindOutlineFromList((DataContext as OutLineControlViewModel).Outlinelist, OutlineView.SelectedItem as OutlineNode, ItemIndex);
- node.IsSelected = true;
- ReNameOutlineNode = node;
- node.IsReName = Visibility.Collapsed;
- }
- private void AddParentMenu_Click(object sender, RoutedEventArgs e)
- {
- ((sender as MenuItem).DataContext as OutlineNode).IsSelected = true;
- int ItemIndex = (DataContext as OutLineControlViewModel).InsertParentOutline((sender as MenuItem).DataContext as OutlineNode);
- OutlineView.UpdateLayout();
- OutlineNode node = (DataContext as OutLineControlViewModel).FindOutlineFromList((DataContext as OutLineControlViewModel).Outlinelist, (OutlineView.SelectedItem as OutlineNode).Parent, ItemIndex);
- node.IsSelected = true;
- ReNameOutlineNode = node;
- node.IsReName = Visibility.Collapsed;
- }
- private void AddChlidMenu_Click(object sender, RoutedEventArgs e)
- {
- ((sender as MenuItem).DataContext as OutlineNode).IsExpanded = true;
- ((sender as MenuItem).DataContext as OutlineNode).IsSelected = true;
- int ItemIndex = (DataContext as OutLineControlViewModel).InsertChlidOutline((sender as MenuItem).DataContext as OutlineNode);
- OutlineView.UpdateLayout();
- OutlineNode node = (DataContext as OutLineControlViewModel).FindOutlineFromList((DataContext as OutLineControlViewModel).Outlinelist, (OutlineView.SelectedItem as OutlineNode).Chlidlist[ItemIndex], ItemIndex);
- node.IsSelected = true;
- ReNameOutlineNode = node;
- node.IsReName = Visibility.Collapsed;
- }
- private void OutlineView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
- {
- onDrop = false;
- }
- private void ReName_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.Key == Key.Enter)
- {
- OutlineView.Focus();
- }
- }
- }
- }
|