Selaa lähdekoodia

大纲-添加大纲相关

zhuyi 2 vuotta sitten
vanhempi
commit
cf0abe0de9

+ 102 - 12
PDF Office/ViewModels/BOTA/OutLineControlViewModel.cs

@@ -22,6 +22,7 @@ namespace PDF_Office.ViewModels.BOTA
 {
     class OutLineControlViewModel : BindableBase, INavigationAware
     {
+        private string PageDefaultName = "";
         //缩略图相关全局变量,减少内存申请次数
         private WriteableBitmap WirteBitmap;
         private byte[] bmpData;
@@ -146,13 +147,13 @@ namespace PDF_Office.ViewModels.BOTA
                 return;
             }
             CPDFAction action = outline.Outline.GetAction();
-            //if (action != null && action.ActionType != C_ACTION_TYPE.ACTION_TYPE_UNKNOWN)
-            //    PDFViewer.ProcessAction(action);
-            //else
-            //{
+            if (action != null && action.ActionType != C_ACTION_TYPE.ACTION_TYPE_UNKNOWN)
+                PDFViewer.ProcessAction(action);
+            else
+            {
                 Size size = PDFViewer.Document.GetPageSize(Convert.ToInt32(outline.PageIndex) - 1);
                 PDFViewer.GoToPage(Convert.ToInt32(outline.PageIndex) - 1, new Point(size.Width - outline.PositionX, size.Height - outline.PositionY));
-            //}
+            }
         }
 
         /// <summary>
@@ -194,38 +195,91 @@ namespace PDF_Office.ViewModels.BOTA
             CPDFDestination info = new CPDFDestination();
             info.PageIndex = textSelectNodes[0].PageIndex;
             Size size = PDFViewer.Document.GetPageSize(textSelectNodes[0].PageIndex);
-            info.Position_X = (float)(size.Width-textSelectNodes[0].StartPoint.X);
-            info.Position_Y = (float)(size.Height-textSelectNodes[0].StartPoint.Y);
+            info.Position_X = (float)(size.Width - textSelectNodes[0].StartPoint.X);
+            info.Position_Y = (float)(size.Height - textSelectNodes[0].StartPoint.Y);
             outline.Outline.SetDestination(PDFViewer.Document, info);
-            PDFViewer.Document.ReleaseOutlineList();
             Updata(false);
         }
 
         /// <summary>
         /// 添加大纲
         /// </summary>
-        public void AddOutLine()
+        public int AddOutLine(OutlineNode outline)
         {
+            int ItemIndex = 0;
             List<TextSelectNode> textSelectNodes = PDFViewer.GetCreateOutLineInfo();
             CPDFDestination info = new CPDFDestination();
+            if (textSelectNodes.Count <= 0)
+            {
+                //SDK出错了
+                return -1;
+            }
             info.PageIndex = textSelectNodes[0].PageIndex;
             info.Position_X = (float)textSelectNodes[0].StartPoint.X;
             info.Position_Y = (float)textSelectNodes[0].StartPoint.Y;
             CPDFOutline dto = null;
+            //当前有选中大纲列表
+            if (outline != null)
+            {
+                CPDFOutline parentoutline = outline.Outline.GetParent();
+                if (parentoutline == null)
+                {
+                    //获取父级失败,直接添加在根节点最下方
+                    PDFViewer.Document.GetOutlineList();
+                    CPDFOutline parent = PDFViewer.Document.GetOutlineRoot();
+                    if (!parent.InsertChildAtIndex(PDFViewer.Document, parent.ChildList.Count, ref dto))
+                    {
+                        //SDK出错了
+                        return -1;
+                    }
+                    ItemIndex = parent.ChildList.Count;
+                }
+                else
+                {
+                    //获取父节点成功,添加在父节点中选中项下方
+                    int index = GetOutlinesIndexFormParent(parentoutline, outline.Outline);
+                    parentoutline.InsertChildAtIndex(PDFViewer.Document, index, ref dto);
+                    ItemIndex = index + 1;
+                }
+            }
+            else
+            {
+                //未选中数据,直接添加在根节点最下方
+                PDFViewer.Document.GetOutlineList();
+                CPDFOutline parent = PDFViewer.Document.GetOutlineRoot();
+                if (!parent.InsertChildAtIndex(PDFViewer.Document, parent.ChildList.Count, ref dto))
+                {
+                    //SDK出错了
+                    return -1;
+                }
+                ItemIndex = parent.ChildList.Count;
+            }
+            ///当前没有选中文字
             if (string.IsNullOrEmpty(textSelectNodes[0].SelectText))
             {
-                dto.SetTitle(textSelectNodes[0].PageIndex.ToString());
+                string addPageName = PageDefaultName + (textSelectNodes[0].PageIndex + 1).ToString();
+                if (!dto.SetTitle(addPageName))
+                {
+                    //SDK出错了
+                    return -1;
+                }
             }
             else
             {
-                dto.SetTitle(textSelectNodes[0].SelectText);
+                string addPageName = PageDefaultName + textSelectNodes[0].SelectText;
+                if (!dto.SetTitle(addPageName))
+                {
+                    //SDK出错了
+                    return -1;
+                }
             }
 
             dto.SetDestination(PDFViewer.Document, info);
-            PDFViewer.Document.ReleaseOutlineList();
             Updata(false);
+            return ItemIndex;
         }
 
+
         /// <summary>
         /// 删除所有大纲
         /// </summary>
@@ -496,6 +550,42 @@ namespace PDF_Office.ViewModels.BOTA
             return null;
         }
 
+        /// <summary>
+        /// 从当前列表中找到对应的OutlineNode对象
+        /// </summary>
+        public OutlineNode FindOutlineFromList(ObservableCollection<OutlineNode> list, OutlineNode outline, int outlineindex)
+        {
+            //如果传入比对数据为null,则返回最后一项
+            if (outline == null)
+            {
+                return list.Last();
+            }
+            int index = list.IndexOf(outline);
+            if (index >= 0)
+            {
+                if (outlineindex <= index)
+                {
+                    return list[index];
+                }
+                else
+                {
+                    return list[outlineindex - 1];
+                }
+            }
+            else
+            {
+                foreach (var item in list)
+                {
+                    OutlineNode node = FindOutlineFromList(item.Chlidlist, outline, outlineindex);
+                    if (node != null)
+                    {
+                        return node;
+                    }
+                }
+            }
+            return null;
+        }
+
         private int GetIndexFromParent(List<CPDFOutline> parentlist, CPDFOutline outline)
         {
             for (int i = 0; i < parentlist.Count; i++)

+ 10 - 8
PDF Office/Views/BOTA/OutLineControl.xaml

@@ -27,13 +27,15 @@
                     VerticalAlignment="Center" FontSize="18" FontWeight="SemiBold" Text="Outline" />
             <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
                 <customcontrol:PathButton
+            x:Name="BtnAdd"
+            Click="BtnAdd_Click"
             Height="24" Width="24"  IconHeight="20"  IconWidth="20" 
             Icon="{StaticResource Ic_AddButtonPath}"  IconFill="Red"
             IconPress="{StaticResource Ic_AddButtonPath}" IconPressFill="#C04CF8"
             IconMouseOver="{StaticResource Ic_AddButtonPath}" IconMouseOverFill="#C04CF8" />
                 <customcontrol:PathButton
-                    x:Name="BtnMore"
-                    Click="BtnMore_Click"
+            x:Name="BtnMore"
+            Click="BtnMore_Click"
             Height="24" Width="24"  IconHeight="20"  IconWidth="20" 
             Icon="{StaticResource Ic_MoreButtonPath}"  IconFill="Red"
             IconPress="{StaticResource Ic_MoreButtonPath}" IconPressFill="#C04CF8"
@@ -74,7 +76,7 @@
         <Grid Grid.Row="1"
                DragOver="Grid_DragOver"
               DragLeave="Grid_DragLeave">
-            <TreeView x:Name="OutlineView" Grid.Row="1" ItemsSource="{Binding Outlinelist}"
+            <TreeView x:Name="OutlineView" Grid.Row="1" ItemsSource="{Binding Outlinelist}" 
                 BorderThickness="0" AllowDrop="True"
                 VirtualizingPanel.ScrollUnit="Pixel"
                 Drop="OutlineView_Drop"
@@ -83,14 +85,14 @@
                 PreviewMouseDoubleClick="OutlineView_PreviewMouseDoubleClick"
                 ScrollViewer.HorizontalScrollBarVisibility="Disabled">
                 <TreeView.ItemTemplate>
-                    <HierarchicalDataTemplate  DataType="{x:Type model:OutlineNode}" ItemsSource="{Binding Path=Chlidlist}">
+                    <HierarchicalDataTemplate   DataType="{x:Type model:OutlineNode}" ItemsSource="{Binding Path=Chlidlist}">
                         <Grid>
                             <Grid.ContextMenu>
                                 <ContextMenu>
-                                    <MenuItem Header="添加条目"/>
-                                    <MenuItem Header="添加子条目"/>
-                                    <MenuItem Header="添加上一级条目" IsEnabled="{Binding CanAddParent}"/>
-                                    
+                                    <MenuItem x:Name="AddMenu" Click="AddMenu_Click" Header="添加条目"/>
+                                    <MenuItem x:Name="AddChlidMenu" Click="AddChlidMenu_Click"  Header="添加子条目"/>
+                                    <MenuItem x:Name="AddParentMenu" Click="AddParentMenu_Click" Header="添加上一级条目" IsEnabled="{Binding CanAddParent}"/>
+
                                     <MenuItem x:Name="DeleteMenu" Click="DeleteMenu_Click" Header="删除"/>
 
                                     <MenuItem x:Name="RenameMenu" Click="RenameMenu_Click" Header="重命名"/>

+ 40 - 2
PDF Office/Views/BOTA/OutLineControl.xaml.cs

@@ -35,6 +35,7 @@ namespace PDF_Office.Views.BOTA
         /// 需要重命名的Item的DataContext
         /// </summary>
         private OutlineNode ReNameOutlineNode = null;
+        bool IsReName = false;
         public OutLineControl()
         {
             InitializeComponent();
@@ -48,7 +49,7 @@ namespace PDF_Office.Views.BOTA
         }
         private void OutlineView_PreviewMouseMove(object sender, MouseEventArgs e)
         {
-            if (e.LeftButton == MouseButtonState.Pressed)
+            if (e.LeftButton == MouseButtonState.Pressed && !IsReName)
             {
                 var pos = e.GetPosition(OutlineView);
                 HitTestResult result = VisualTreeHelper.HitTest(OutlineView, pos);
@@ -154,6 +155,7 @@ namespace PDF_Office.Views.BOTA
             {
                 return;
             }
+
             OutlineNode viewitem = tree.SelectedItem as OutlineNode;
             if (viewitem == null)
             {
@@ -234,6 +236,7 @@ namespace PDF_Office.Views.BOTA
                 }
                 ReNameText.Focus();
                 ReNameText.SelectAll();
+                IsReName = true;
             }
         }
 
@@ -242,12 +245,14 @@ namespace PDF_Office.Views.BOTA
             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;
@@ -258,18 +263,20 @@ namespace PDF_Office.Views.BOTA
                 {
                     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))
             {
@@ -363,5 +370,36 @@ namespace PDF_Office.Views.BOTA
                 (DataContext as OutLineControlViewModel).ChangeOutLineDestination((sender as MenuItem).DataContext as OutlineNode);
             }
         }
+
+        private void BtnAdd_Click(object sender, RoutedEventArgs e)
+        {
+            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;
+        }
+
+        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)
+        {
+
+        }
+
+        private void AddChlidMenu_Click(object sender, RoutedEventArgs e)
+        {
+
+        }
     }
 }