using ComPDFKit.PDFDocument; using ComPDFKitViewer.PdfViewer; using ImTools; using PDF_Master.CustomControl; using PDF_Master.Helper; using PDF_Master.Model; using PDF_Master.Model.PageEdit; using Prism.Commands; using Prism.Mvvm; using Prism.Regions; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Reflection; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Forms; using System.Windows.Input; using System.Windows.Markup; using System.Windows.Media; using static Dropbox.Api.Files.SearchMatchType; using static Dropbox.Api.TeamLog.AdminAlertSeverityEnum; using static System.Net.Mime.MediaTypeNames; using static System.Windows.Forms.VisualStyles.VisualStyleElement; using KeyEventArgs = System.Windows.Input.KeyEventArgs; using ListViewItem = System.Windows.Controls.ListViewItem; using TextBox = System.Windows.Controls.TextBox; using Task = System.Threading.Tasks.Task; using Microsoft.Office.Interop.Word; using System.Threading.Tasks; using static Dropbox.Api.TeamLog.TimeUnit; using System.Threading; using static Dropbox.Api.Files.SaveUrlError; using System.Windows.Documents; using System.Runtime.Remoting.Messaging; using ListView = System.Windows.Controls.ListView; using PDF_Master.EventAggregators; using Prism.Events; namespace PDF_Master.ViewModels.BOTA { public class BookmarkContentViewModel : BindableBase, INavigationAware { #region 文案 private string T_emptyTitle; public string T_EmptyTitle { get { return T_emptyTitle; } set { SetProperty(ref T_emptyTitle, value); } } private string T_emptyContext; public string T_EmptyContext { get { return T_emptyContext; } set { SetProperty(ref T_emptyContext, value); } } private void InitString() { T_EmptyTitle = App.MainPageLoader.GetString("Bookmark_EmptyTitle"); T_EmptyContext = App.MainPageLoader.GetString("Bookmark_EmptyContext"); } #endregion 文案 #region 属性 public bool isAddBookMark = false; private IRegionManager region; private IDialogService dialogs; public CPDFViewer PDFViewer; public IEventAggregator events; /// /// 书签ItemSouce /// private ObservableCollection bookmarklist; public ObservableCollection Bookmarklist { get { return bookmarklist; } set { SetProperty(ref bookmarklist, value); } } /// /// 书签列表为空时,显示状态 /// private Visibility isEmpty; public Visibility IsEmptyPanelVisibility { get { return isEmpty; } set { SetProperty(ref isEmpty, value); } } public CPDFBookmark AddCPDFBookmark = null; public bool IsHasBookmark = true; #endregion 属性 #region 命令 public DelegateCommand KeyDownCommand { get; set; } public DelegateCommand LostFocusCommand { get; set; } public DelegateCommand AddBookmarkCommand { get; set; } public DelegateCommand ListViewItemMouseDownCommand { get; set; } public DelegateCommand EditPageIndexCommand { get; set; } public DelegateCommand DeleteCommand { get; set; } #endregion 命令 public BookmarkContentViewModel(IRegionManager regionManager, IDialogService dialogService, IEventAggregator eventAggregator) { region = regionManager; dialogs = dialogService; events = eventAggregator; Bookmarklist = new ObservableCollection(); LostFocusCommand = new DelegateCommand(LostFocusEvent); AddBookmarkCommand = new DelegateCommand(AddBookmarkEvent); ListViewItemMouseDownCommand = new DelegateCommand(ListViewItemMouseLeftButtonDownEvent); DeleteCommand = new DelegateCommand(DelegateEvent); EditPageIndexCommand = new DelegateCommand(EditPageIndexEvent); KeyDownCommand = new DelegateCommand(KeyDownEvent); InitString(); } /// /// 检测ObservableCollection的数据变更 /// /// /// private void Bookmarklist_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { ObservableCollection obsSender = sender as ObservableCollection; if (obsSender != null) { if (obsSender.Count < 1) { IsEmptyPanelVisibility = Visibility.Visible; } else { IsEmptyPanelVisibility = Visibility.Collapsed; } } else { IsEmptyPanelVisibility = Visibility.Visible; } } /// /// 文本框按下Enter键 /// /// private void KeyDownEvent(object obj) { if (obj is KeyEventArgs) { KeyEventArgs keyEventArgs = (KeyEventArgs)obj; if (keyEventArgs.Key == Key.Enter) { if (keyEventArgs.OriginalSource is TextBox) { TextBox textBox = (TextBox)keyEventArgs.OriginalSource; ListViewItem listViewItem = CommonHelper.FindVisualParent(textBox); UpdateTitle(listViewItem, textBox); TextBlock textBlock = CommonHelper.FindVisualChild(listViewItem); textBlock.Text = textBox.Text; textBlock.Visibility = Visibility.Visible; } } } } /// /// 重新定位 /// /// private void EditPageIndexEvent(object obj) { CPDFBookmark bookmark = obj as CPDFBookmark; if (bookmark != null) { AlertsMessage alertsMessage = new AlertsMessage(); alertsMessage.ShowDialog("提示", "确定要将选定大纲的目标位置设置为当前位置吗?", "Cancel", "OK"); if (alertsMessage.result == ContentResult.Ok) { if (RemoveBookMark(bookmark)) { bookmarklist.Remove(bookmark); CPDFBookmark cPDFBookmark = new CPDFBookmark(); bookmark.Title = bookmark.Title; bookmark.Date = DateTime.Now.ToString(@"yyyyMMddHHmmsszzz\'").Replace(':', '\'') + "\n"; bookmark.PageIndex = PDFViewer.CurrentIndex; if (PDFViewer.Document.AddBookmark(bookmark)) { PDFViewer.UndoManager.CanSave = true; Bookmarklist.Add(bookmark); } } } } } /// /// 右键菜单-删除 /// /// private void DelegateEvent(object obj) { CPDFBookmark bookmark = obj as CPDFBookmark; if (bookmark != null) { if (RemoveBookMark(bookmark)) { Bookmarklist.Remove(bookmark); isAddBookMark = true; PDFViewer.UndoManager.CanSave = true; } } } /// /// 删除书签 /// /// /// private bool RemoveBookMark(CPDFBookmark item) { var data = item; if (data == null) { return false; } return PDFViewer.Document.RemoveBookmark(data.PageIndex); } /// /// ListViewItem失去焦点 /// /// private void LostFocusEvent(object obj) { TextBox textBox = null; ListViewItem listViewItem = null; if (obj is CompositeCommandParameter) { CompositeCommandParameter parameter = (CompositeCommandParameter)obj; if (parameter.Parameter is TextBox) { textBox = (TextBox)parameter.Parameter; listViewItem = CommonHelper.FindVisualParent(textBox); } } if (obj is ListBoxItem) { listViewItem = (ListViewItem)(obj); textBox = CommonHelper.FindVisualChild(listViewItem); } CPDFBookmark bookmark = (CPDFBookmark)listViewItem.DataContext; UpdateTitle(listViewItem, textBox); } /// /// 修改书签标题 /// /// /// private void UpdateTitle(ListViewItem listViewItem, TextBox textBox) { if (listViewItem != null) { var data = listViewItem.DataContext as CPDFBookmark; if (data == null) { return; } var result = PDFViewer.Document.EditBookmark(data.PageIndex, textBox.Text.Trim()); if (result) { data.Title = textBox.Text.Trim(); PDFViewer.UndoManager.CanSave = true; } } } /// /// ListBoxItem左键单击,页面跳转 /// /// private void ListViewItemMouseLeftButtonDownEvent(object obj) { ListBoxItem listBoxItem = (obj as ListBoxItem); if (listBoxItem != null) { if (!(listBoxItem.DataContext is CPDFBookmark)) { return; } int index = (listBoxItem.DataContext as CPDFBookmark).PageIndex; PDFViewer.GoToPage(index); } } /// /// 添加书签 /// /// private void AddBookmarkEvent(object obj) { string info = App.MainPageLoader.GetString("Bookmark_PageN"); int index = PDFViewer.CurrentIndex; string mark = string.Format(info, (index + 1).ToString()); System.Windows.Controls.ListView listView = obj as System.Windows.Controls.ListView; listView.SelectedItems.Clear(); IsHasBookmark = IsExistBookmark(listView);// await Task.Run(() => IsExistBookmark(listView)); if (IsHasBookmark == false) { CPDFBookmark bookmark = new CPDFBookmark(); bookmark.Title = mark; bookmark.Date = DateTime.Now.ToString(@"yyyyMMddHHmmsszzz\'").Replace(':', '\'') + "\n"; bookmark.PageIndex = PDFViewer.CurrentIndex; AddCPDFBookmark = bookmark; if (PDFViewer.Document.AddBookmark(bookmark)) { PDFViewer.UndoManager.CanSave = true; Bookmarklist.Add(bookmark); Bookmarklist = new ObservableCollection(Bookmarklist.OrderBy(item => item.PageIndex)); Bookmarklist.CollectionChanged += Bookmarklist_CollectionChanged; isAddBookMark = false; } //DialogParameters value = new DialogParameters(); //value.Add(ParameterNames.Bookmark, mark); //value.Add(ParameterNames.Title, "创建一个新的书签"); //dialogs.ShowDialog(DialogNames.AddBookmarkDialog, value, e => //{ // if (e.Result == ButtonResult.OK && e.Parameters != null) // { // if (e.Parameters.ContainsKey(ParameterNames.Bookmark)) // { // mark = e.Parameters.GetValue(ParameterNames.Bookmark).ToString(); // CPDFBookmark bookmark = new CPDFBookmark(); // bookmark.Title = mark; // bookmark.Date = DateTime.Now.ToString(@"yyyyMMddHHmmsszzz\'").Replace(':', '\'') + "\n"; // bookmark.PageIndex = PDFViewer.CurrentIndex; // AddCPDFBookmark = bookmark; // if (PDFViewer.Document.AddBookmark(bookmark)) // { // PDFViewer.UndoManager.CanSave = true; // Bookmarklist.Add(bookmark); // Bookmarklist = new ObservableCollection(Bookmarklist.OrderBy(item => item.PageIndex)); // Bookmarklist.CollectionChanged += Bookmarklist_CollectionChanged; // } // } // isAddBookMark = false; // } // if (e.Result == ButtonResult.Cancel) // { // isAddBookMark = true; // } //}); } } private bool IsExistBookmark(System.Windows.Controls.ListView listView) { bool isExistBookmark = false; int index = PDFViewer.CurrentIndex; if (PDFViewer.Document == null) { isExistBookmark = true; return isExistBookmark; } //检测是否已存在相同数据 var list = PDFViewer.Document.GetBookmarkList().FindAll(q => q.PageIndex == index); if (list.Count > 0) { int i = PDFViewer.Document.GetBookmarkList().FindIndex(q => q.PageIndex == index); isExistBookmark = true; if (listView != null) { listView.ScrollIntoView(list[0]); //ListBoxItem myListBoxItem = await Task.Run(() => GetListViewItem(listView, list[0])); ListViewItem myListBoxItem = (ListViewItem)(listView.ItemContainerGenerator.ContainerFromItem(list[0])); List listItems = new List(); foreach (var item in listView.SelectedItems) { listView.ScrollIntoView(item); ListViewItem listViewItem = (ListViewItem)(listView.ItemContainerGenerator.ContainerFromItem(item)); if (listItems.IndexOf(listViewItem) == -1) { listItems.Add(listViewItem); } } if (myListBoxItem.IsSelected == false) { myListBoxItem.IsSelected = true; myListBoxItem.Focus(); isAddBookMark = false; foreach (var item in listItems) { item.IsSelected = false; } } return isExistBookmark; } } return isExistBookmark; } public void OnNavigatedTo(NavigationContext navigationContext) { KeyEventsHelper.KeyDown -= ShortCut_KeyDown; KeyEventsHelper.KeyDown += ShortCut_KeyDown; navigationContext.Parameters.TryGetValue(ParameterNames.PDFViewer, out PDFViewer); if (PDFViewer == null) { IsEmptyPanelVisibility = Visibility.Visible; return; } Bookmarklist = new ObservableCollection(PDFViewer.Document.GetBookmarkList().OrderBy(d => d.PageIndex)); Bookmarklist.CollectionChanged += Bookmarklist_CollectionChanged; if (Bookmarklist.Count < 1) { IsEmptyPanelVisibility = Visibility.Visible; return; } else { IsEmptyPanelVisibility = Visibility.Hidden; return; } } public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { KeyEventsHelper.KeyDown -= ShortCut_KeyDown; } private void ShortCut_KeyDown(object sender, KeyEventArgs e) { if (KeyEventsHelper.IsSingleKey(System.Windows.Input.Key.Escape)) { this.events.GetEvent().Publish(new CleanSelectAllArgs() { Unicode = App.mainWindowViewModel.SelectedItem.Unicode, IsCleanSelectAll = true }); } } } }