123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599 |
- using ComPDFKit.PDFAnnotation;
- using ComPDFKit.PDFPage;
- using ComPDFKitViewer;
- using ComPDFKitViewer.AnnotEvent;
- using ImTools;
- using PDF_Master.DataConvert;
- using PDF_Master.EventAggregators;
- using PDF_Master.Helper;
- using PDF_Master.Model.BOTA;
- using PDF_Master.ViewModels;
- using PDF_Master.ViewModels.BOTA;
- using Prism.Events;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Diagnostics;
- using System.Linq;
- using System.Runtime.InteropServices.Expando;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Annotations;
- using System.Windows.Controls;
- using System.Windows.Controls.Primitives;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Forms;
- 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.Paper.ListPaperDocsSortBy;
- using static Dropbox.Api.TeamLog.PaperDownloadFormat;
- using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
- using ListBox = System.Windows.Controls.ListBox;
- using MenuItem = System.Windows.Controls.MenuItem;
- using UserControl = System.Windows.Controls.UserControl;
- namespace PDF_Master.Views.BOTA
- {
- /// <summary>
- /// AnnotationContent.xaml 的交互逻辑
- /// </summary>
- public partial class AnnotationContent : UserControl
- {
- public AnnotationContentViewModel viewModel;
- private string unicode;
- public AnnotationContent(IEventAggregator eventAggregator)
- {
- InitializeComponent();
- unicode = App.mainWindowViewModel.SelectedItem.Unicode;
- viewModel = this.DataContext as AnnotationContentViewModel;
- if (AnnotationList.Items.Count < 0)
- {
- MenuExpandAll.IsEnabled = false;
- }
- eventAggregator.GetEvent<CleanSelectAllEvent>().Subscribe(CleanSelectAll, e => e.Unicode == unicode);
- eventAggregator.GetEvent<RefreshAnnotationEvent>().Subscribe(ExpandGroupHeader, e => e.Unicode == unicode);
- }
- public async void ExpandGroupHeader(RefreshAnnotationArgs obj)
- {
- if (obj.IsAll)
- {
- ExpandAllGroupHeader(obj);
- }
- else
- {
- //需要时间加载完 listbox所有的控件模板
- await Task.Delay(200);
- ExpandOneGroupHeader(obj);
- }
- }
- private async void ExpandAllGroupHeader(RefreshAnnotationArgs obj)
- {
- try
- {
- foreach (var item in AnnotationList.Items)
- {
- var groups = AnnotationList.Items.Groups;
- for (int i = 0; i < groups.Count; i++)
- {
- if (groups[i] is CollectionViewGroup group)
- {
- if (group.Items.Contains(item))
- {
- var scroller = GetScrollHost(AnnotationList);
- var stackpanel = CommonHelper.FindVisualChild<StackPanel>(scroller);
- if (stackpanel == null)
- {
- await Task.Delay(1);
- }
- else
- {
- GroupItemExpanded(stackpanel, i);
- }
- }
- }
- }
- }
- }
- catch (Exception ex) { }
- }
- public async void ExpandOneGroupHeader(RefreshAnnotationArgs obj)
- {
- if (obj.annotHandlerArgs == null)
- {
- if (obj.Pageindex != -1 && obj.AnnotIndex != -1)
- {
- List<AnnotationHandlerEventArgs> annotations = AnnotationList.Items.Cast<AnnotationHandlerEventArgs>().ToList();
- obj.annotHandlerArgs = annotations.FirstOrDefault(x => x.PageIndex == obj.Pageindex && x.AnnotIndex == obj.AnnotIndex);
- }
- }
- if (obj.annotHandlerArgs != null)
- {
- try
- {
- var groups = AnnotationList.Items.Groups;
- if (groups != null && groups.Count > 0)
- {
- for (int i = 0; i < groups.Count; i++)
- {
- if (groups[i] is CollectionViewGroup group)
- {
- if (group.Items.Contains(obj.annotHandlerArgs))
- {
- var scroller = GetScrollHost(AnnotationList);
- var stackpanel = CommonHelper.FindVisualChild<StackPanel>(scroller);
- if (stackpanel != null)
- {
- GroupItemExpanded(stackpanel, i);
- }
- else
- {
- ItemsPresenter itemsPresenter = CommonHelper.FindVisualChild<ItemsPresenter>(scroller);
- if (itemsPresenter != null)
- {
- var tt = VisualTreeHelper.GetChild(itemsPresenter, i);
- int count = VisualTreeHelper.GetChildrenCount(tt);
- }
- }
- }
- }
- }
- }
- if (obj.IsDel)
- {
- viewModel.IsModified = true;
- }
- await Task.Delay(10);//不加延时 会有不滚动的现象
- SelectedItems(obj);
- }
- catch (Exception ex) { }
- }
- }
- private void GroupItemExpanded(StackPanel stackpanel, int i)
- {
- int count = VisualTreeHelper.GetChildrenCount(stackpanel);
- if (count > 0)
- {
- if (VisualTreeHelper.GetChild(stackpanel, i) is GroupItem groupItem)
- {
- var expander = CommonHelper.FindVisualChild<Expander>(groupItem);
- if (expander != null)
- {
- if (expander.IsExpanded == false)
- {
- expander.IsExpanded = true;
- }
- }
- }
- }
- }
- private async void SelectedItems(RefreshAnnotationArgs obj)
- {
- if (obj.IsSelect && obj.annotHandlerArgs != null)
- {
- if (viewModel.ViewContentViewModel.IsMultiSelected == false)
- {
- if (AnnotationList.SelectedItems.IndexOf(obj.annotHandlerArgs) == -1)
- {
- viewModel.IsModified = false;
- AnnotationList.SelectedItem = obj.annotHandlerArgs;
- AnnotationList.SelectedIndex = AnnotationList.Items.IndexOf(obj.annotHandlerArgs);
- }
- await Task.Delay(10);//不加延时 会有不滚动的现象
- AnnotationList.ScrollIntoView(AnnotationList.SelectedItem);
- }
- }
- }
- private void CleanSelectAll(CleanSelectAllArgs obj)
- {
- AnnotationList.SelectedIndex = -1;
- }
- private void ListBoxItem_PreviewMouseDown(object sender, MouseButtonEventArgs e)
- {
- if (e.ClickCount == 1)
- {
- //if (Mouse.LeftButton == e.ButtonState)
- //{
- // viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute(sender);
- //}
- if (Mouse.RightButton == e.ButtonState)
- {
- var pos = e.GetPosition(AnnotationList);
- var result = VisualTreeHelper.HitTest(AnnotationList, pos);
- if (result != null)
- {
- ListBoxItem myListBoxItem = sender as System.Windows.Controls.ListBoxItem;
- MenuItem copyText = myListBoxItem.ContextMenu.Items[0] as MenuItem;
- MenuItem import = myListBoxItem.ContextMenu.Items[1] as MenuItem;
- MenuItem export = myListBoxItem.ContextMenu.Items[2] as MenuItem;
- if (AnnotationList.SelectedItems.Count > 1)
- {
- import.IsEnabled = false;
- export.IsEnabled = true;
- export.Command = viewModel.ExportCommentsCommand;
- copyText.IsEnabled = false;
- }
- else
- {
- //直接右键菜单不需要选中Viewer中注释
- //viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute(sender);
- if (myListBoxItem.DataContext is AnnotationHandlerEventArgs annotation)
- {
- //文本、高亮、下划线、删除线、便签
- if (annotation.EventType == AnnotArgsType.AnnotFreeText ||
- annotation.EventType == AnnotArgsType.AnnotHighlight ||
- annotation.EventType == AnnotArgsType.AnnotUnderline ||
- annotation.EventType == AnnotArgsType.AnnotStrikeout ||
- annotation.EventType == AnnotArgsType.AnnotSticky)
- {
- copyText.IsEnabled = true;
- }
- else
- {
- copyText.IsEnabled = false;
- }
- }
- import.IsEnabled = true;
- import.Command = viewModel.ImportCommentsCommand;
- export.IsEnabled = true;
- export.Command = viewModel.ExportCommentsCommand;
- }
- }
- //拦截响应选中事件
- e.Handled = true;
- }
- }
- }
- private void ListBoxItem_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
- {
- if (Mouse.LeftButton == e.ButtonState)
- {
- viewModel.AddNotesCommand.Execute(sender);
- }
- }
- private async void MenuExpandAll_Click(object sender, RoutedEventArgs e)
- {
- SetAllExpander(true);
- await Task.Delay(1);
- for (int i = 0; i < AnnotationList.Items.Count; i++)
- {
- ListBoxItem item = AnnotationList.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
- if (item != null)
- {
- ToggleButton button = GetExpandButton(item);
- if (button != null && button.Visibility == Visibility.Visible)
- {
- button.IsChecked = true;
- }
- }
- }
- }
- private void MenuCollapseAll_Click(object sender, RoutedEventArgs e)
- {
- for (int i = 0; i < AnnotationList.Items.Count; i++)
- {
- ListBoxItem item = AnnotationList.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
- ToggleButton button = GetExpandButton(item);
- if (button != null && button.Visibility == Visibility.Visible)
- {
- button.IsChecked = false;
- }
- }
- SetAllExpander(false);
- }
- private void SetAllExpander(bool isexpand)
- {
- AnnotationList.Dispatcher.Invoke(() =>
- {
- //开启虚拟化之后 全部展开和折叠会有问题
- var scroller = GetScrollHost(AnnotationList);
- var stackpanel = CommonHelper.FindVisualChild<StackPanel>(scroller);
- int count = VisualTreeHelper.GetChildrenCount(stackpanel);
- for (int i = 0; i < count; i++)
- {
- var item = VisualTreeHelper.GetChild(stackpanel, i) as GroupItem;
- var g = CommonHelper.FindVisualChild<Expander>(item);
- if (g != null)
- g.IsExpanded = isexpand;
- }
- });
- }
- private ScrollViewer GetScrollHost(ListBox listBox)
- {
- if (VisualTreeHelper.GetChildrenCount(listBox) > 0)
- {
- int s = VisualTreeHelper.GetChildrenCount(listBox);
- Border border = VisualTreeHelper.GetChild(listBox, 0) as Border;
- if (border != null)
- {
- return VisualTreeHelper.GetChild(border, 0) as ScrollViewer;
- }
- }
- return null;
- }
- private ToggleButton GetExpandButton(ListBoxItem item)
- {
- if (item == null) return null;
- Border border = VisualTreeHelper.GetChild(item, 0) as Border;
- var btn = CommonHelper.FindVisualChild<ToggleButton>(border);
- return btn;
- }
- private void MenuTimeRightSort_Click(object sender, RoutedEventArgs e)
- {
- ICollectionView v = CollectionViewSource.GetDefaultView(AnnotationList.ItemsSource);
- CreateTimeToDate createTimeToDate = new CreateTimeToDate();
- v.GroupDescriptions.Clear();
- v.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotHandlerEventArgs.CreateTime), createTimeToDate));
- v.SortDescriptions.Clear();
- v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.CreateTime), ListSortDirection.Ascending));
- v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.AnnotIndex), ListSortDirection.Ascending));
- }
- private void MenuTimeBackSort_Click(object sender, RoutedEventArgs e)
- {
- ICollectionView v = CollectionViewSource.GetDefaultView(AnnotationList.ItemsSource);
- CreateTimeToDate createTimeToDate = new CreateTimeToDate();
- v.GroupDescriptions.Clear();
- v.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotHandlerEventArgs.CreateTime), createTimeToDate));
- v.SortDescriptions.Clear();
- v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.CreateTime), ListSortDirection.Descending));
- v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.AnnotIndex), ListSortDirection.Descending));
- }
- private void MenuPageSort_Click(object sender, RoutedEventArgs e)
- {
- ICollectionView v = CollectionViewSource.GetDefaultView(AnnotationList.ItemsSource);
- v.GroupDescriptions.Clear();
- v.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotHandlerEventArgs.PageIndex)));
- v.SortDescriptions.Clear();
- v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.PageIndex), ListSortDirection.Ascending));
- v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.AnnotIndex), ListSortDirection.Ascending));
- }
- private void MenuItemCopyText_Click(object sender, RoutedEventArgs e)
- {
- //没有权限解密时,拦截复制文本操作
- if (!viewModel.ViewContentViewModel.CheckPermissionBeforeOption())
- {
- return;
- }
- if (AnnotationList.SelectedItems.Count == 1)
- {
- if (AnnotationList.SelectedItem is AnnotationHandlerEventArgs annotation)
- {
- //文本、高亮、下划线、删除线、便签
- if (annotation.EventType == AnnotArgsType.AnnotFreeText ||
- annotation.EventType == AnnotArgsType.AnnotHighlight ||
- annotation.EventType == AnnotArgsType.AnnotUnderline ||
- annotation.EventType == AnnotArgsType.AnnotStrikeout ||
- annotation.EventType == AnnotArgsType.AnnotSticky)
- {
- StringBuilder Copystr = new StringBuilder();
- if (annotation.MarkupContent != null)
- {
- Copystr.Append(annotation.MarkupContent);
- Copystr.Append(Environment.NewLine);
- }
- if (annotation.Content != null)
- {
- Copystr.Append(annotation.Content);
- }
- System.Windows.Clipboard.SetText(Copystr.ToString());
- //CustomControl.MessageBoxEx.Show("数据复制成功");
- }
- }
- }
- }
- private void MenuItemDelete_Click(object sender, RoutedEventArgs e)
- {
- List<int> pagelist = new List<int>();
- if (AnnotationList.SelectedItems.Count > 1)
- {
- //for (int i = 0; i < AnnotationList.SelectedItems.Count; i++)
- //{
- // AnnotationHandlerEventArgs annotation = AnnotationList.SelectedItems[i] as AnnotationHandlerEventArgs;
- // pagelist.Add(AnnotationList.Items.IndexOf(annotation));
- //}
- //pagelist.Sort();
- //for (int i = 0; i < pagelist.Count; i++)
- //{
- // AnnotationHandlerEventArgs annotation = AnnotationList.Items[pagelist[pagelist.Count - i - 1]] as AnnotationHandlerEventArgs;
- // if (annotation == null)
- // {
- // continue;
- // }
- // viewModel.DeleteCommand.Execute(annotation);
- //}
- Dictionary<int, List<int>> selectedItemDics = new Dictionary<int, List<int>>();
- List<AnnotationHandlerEventArgs> eventArgs = new List<AnnotationHandlerEventArgs>();
- foreach (var item in AnnotationList.SelectedItems)
- {
- if (item is AnnotationHandlerEventArgs annotation)
- {
- if (eventArgs.Contains(annotation) == false)
- {
- eventArgs.Add(annotation);
- }
- }
- }
- var result = eventArgs.GroupBy(d => d.PageIndex);
- foreach (var item in result)
- {
- if (selectedItemDics.ContainsKey(item.Key) == false)
- {
- var ai = item.Select(w => w.AnnotIndex).ToList();
- selectedItemDics.Add(item.Key, ai);
- }
- }
- viewModel.DeleteCommand.Execute(selectedItemDics);
- }
- else
- {
- AnnotationHandlerEventArgs annotation = (AnnotationHandlerEventArgs)(sender as MenuItem).DataContext;
- if (annotation == null)
- {
- return;
- }
- viewModel.DeleteCommand.Execute(annotation);
- }
- }
- private void BtnMore_Initialized(object sender, EventArgs e)
- {
- BtnMore.ContextMenu = null;
- }
- private void BtnMore_Click(object sender, RoutedEventArgs e)
- {
- MenuMore.PlacementTarget = BtnMore;
- MenuMore.IsOpen = true;
- }
- private void AnnotationList_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- var pos = e.GetPosition(AnnotationList);
- var result = VisualTreeHelper.HitTest(AnnotationList, pos);
- if (result != null)
- {
- //获取当前鼠标指针下的容器
- var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
- if (listBoxItem == null)
- {
- viewModel.ViewContentViewModel.mainViewModel.AnnotMouseLeftButtonDown = false;
- }
- }
- }
- private void AnnotationList_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- var selectedItems = (sender as ListBox).SelectedItems.Cast<AnnotationHandlerEventArgs>().ToList();
- if (selectedItems.Count == 1)
- {
- if (viewModel.IsModified)
- {
- viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute(selectedItems[0]);
- }
- viewModel.IsModified = true;
- }
- else if (selectedItems.Count > 1)
- {
- #region 阅读视图 多选
- Dictionary<int, List<int>> selectedItemDics = new Dictionary<int, List<int>>();
- List<AnnotationHandlerEventArgs> eventArgs = new List<AnnotationHandlerEventArgs>();
- foreach (var item in selectedItems)
- {
- if (item is AnnotationHandlerEventArgs annotation)
- {
- if (eventArgs.Contains(annotation) == false)
- {
- eventArgs.Add(annotation);
- }
- }
- }
- var result = eventArgs.GroupBy(d => d.PageIndex);
- foreach (var item in result)
- {
- if (selectedItemDics.ContainsKey(item.Key) == false)
- {
- var ai = item.Select(w => w.AnnotIndex).ToList();
- selectedItemDics.Add(item.Key, ai);
- }
- }
- #endregion 阅读视图 多选
- viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute(selectedItemDics);
- }
- }
- private void AnnotationList_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
- {
- if (AnnotationList.SelectedItems == null)
- {
- return;
- }
- if (e.Key == Key.Escape)
- {
- //Windows:BOTA列表,移除Esc取消选中逻辑
- //AnnotationList.SelectedItems.Clear();
- }
- }
- private void Grid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- var pos = e.GetPosition(this);
- var result = VisualTreeHelper.HitTest(this, pos);
- if (result != null)
- {
- //点击按钮和listboxitem时 不清空多选 其余区域取消多选
- var item = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
- if (item != null)
- {
- return;
- }
- var btn = CommonHelper.FindVisualParent<System.Windows.Controls.Button>(result.VisualHit);
- if (btn != null)
- {
- return;
- }
- }
- }
- private void ListBox_MouseDown(object sender, MouseButtonEventArgs e)
- {
- AnnotationList.SelectedIndex = -1;
- }
- private void ContextMenu_Loaded(object sender, RoutedEventArgs e)
- {
- var menu = sender as System.Windows.Controls.ContextMenu;
- if (menu != null)
- {
- (menu.Items[0] as MenuItem).Header = viewModel.T_CopyText;
- (menu.Items[1] as MenuItem).Header = viewModel.T_Import;
- (menu.Items[2] as MenuItem).Header = viewModel.T_Export;
- (menu.Items[3] as MenuItem).Header = viewModel.T_Delete;
- if (AnnotationList.SelectedItems.Count > 1)
- {
- (menu.Items[3] as MenuItem).Header = viewModel.T_DeleteAll;
- }
- }
- }
- }
- }
|