using ComPDFKitViewer.AnnotEvent;
using PDF_Office.DataConvert;
using PDF_Office.Helper;
using PDF_Office.Model.BOTA;
using PDF_Office.ViewModels.BOTA;
using System;
using System.Collections.Generic;
using System.ComponentModel;
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.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
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_Office.Views.BOTA
{
///
/// AnnotationContent.xaml 的交互逻辑
///
public partial class AnnotationContent : UserControl
{
private AnnotationContentViewModel viewModel;
public AnnotationContent()
{
InitializeComponent();
viewModel = this.DataContext as AnnotationContentViewModel;
if (AnnotationList.Items.Count < 0)
{
MenuExpandAll.IsEnabled = false;
}
}
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 = false;
copyText.IsEnabled = false;
}
else
{
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;
}
}
}
}
}
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;
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(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(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(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 (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 pagelist = new List();
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);
}
}
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)
{
if (AnnotationList.SelectedItems.Count > 1)
{
viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute(AnnotationList.SelectedItems);
}
else if (AnnotationList.SelectedItems.Count == 1)
{
viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute(AnnotationList.SelectedItem);
}
}
private void AnnotationList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var a1 = (sender as ListBox).SelectedItems.Count;
var a2 = (e.OriginalSource as ListBox).SelectedItems.Count;
var a3 = (e.Source as ListBox).SelectedItems.Count;
if (a1 == 1)
{
viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute((sender as ListBox).SelectedItem);
}
else if (a1 > 1)
{
#region 阅读视图 多选
//Dictionary> selectedItemDics = new Dictionary>();
//List eventArgs = new List();
//List ints = new List();
//foreach (var item in (sender as ListBox).SelectedItems)
//{
// if (item is AnnotationHandlerEventArgs annotation)
// {
// if (!selectedItemDics.ContainsKey(annotation.PageIndex))
// {
// ints.Clear();
// ints.Add(annotation.AnnotIndex);
// selectedItemDics.Add(annotation.PageIndex, ints);
// }
// else
// {
// ints.Add(annotation.AnnotIndex);
// selectedItemDics[annotation.PageIndex] = ints;
// }
// }
//}
#endregion 阅读视图 多选
viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute((sender as ListBox).SelectedItems[0]);
}
}
private void AnnotationList_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (AnnotationList.SelectedItems == null)
{
return;
}
if (e.Key == Key.Escape)
{
AnnotationList.SelectedItems.Clear();
}
}
}
}