FileGridListWithPageRangeControl.xaml.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. using ComPDFKit.PDFDocument;
  2. using Compdfkit_Tools.Common;
  3. using Compdfkit_Tools.Helper;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Collections.ObjectModel;
  7. using System.ComponentModel;
  8. using System.Linq;
  9. using System.Runtime.CompilerServices;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Data;
  15. using System.Windows.Documents;
  16. using System.Windows.Forms;
  17. using System.Windows.Input;
  18. using System.Windows.Media;
  19. using System.Windows.Media.Imaging;
  20. using System.Windows.Navigation;
  21. using System.Windows.Shapes;
  22. using static Compdfkit_Tools.PDFControl.FileGridListControl;
  23. using DataGrid = System.Windows.Controls.DataGrid;
  24. using DataGridCell = System.Windows.Controls.DataGridCell;
  25. using UserControl = System.Windows.Controls.UserControl;
  26. namespace Compdfkit_Tools.PDFControl
  27. {
  28. public class FileInfoWithRange : INotifyPropertyChanged
  29. {
  30. public CPDFDocument Document;
  31. public string Name { get; set; }
  32. public string Path { get; set; }
  33. public string Size { get; set; }
  34. private string _pageRange;
  35. public string PageRange { get => _pageRange; set => UpdateProper(ref _pageRange, value); }
  36. private List<int> _pageRangeList = new List<int>();
  37. public List<int> PageRangeList
  38. {
  39. get => _pageRangeList;
  40. set
  41. {
  42. _pageRangeList = value;
  43. PageRange = CommonHelper.GetPageParmFromList(PageRangeList);
  44. }
  45. }
  46. public event PropertyChangedEventHandler PropertyChanged;
  47. protected virtual void OnPropertyChanged(string propertyName = null)
  48. {
  49. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  50. }
  51. protected bool UpdateProper<T>(ref T properValue,
  52. T newValue,
  53. [CallerMemberName] string properName = "")
  54. {
  55. if (object.Equals(properValue, newValue))
  56. return false;
  57. properValue = newValue;
  58. OnPropertyChanged(properName);
  59. return true;
  60. }
  61. }
  62. public partial class FileGridListWithPageRangeControl : UserControl, INotifyPropertyChanged
  63. {
  64. public static readonly DependencyProperty IsEnsureProperty =
  65. DependencyProperty.Register(nameof(IsEnsure), typeof(bool), typeof(FileGridListWithPageRangeControl), new PropertyMetadata(false));
  66. public bool IsEnsure
  67. {
  68. get => (bool)GetValue(IsEnsureProperty);
  69. private set
  70. {
  71. SetValue(IsEnsureProperty, value);
  72. }
  73. }
  74. private int _fileNumText;
  75. public int FileNumText
  76. {
  77. get => _fileNumText;
  78. set
  79. {
  80. if (UpdateProper(ref _fileNumText, value))
  81. IsEnsure = _fileNumText > 0;
  82. }
  83. }
  84. public FileInfoWithRange FileForDisplay
  85. {
  86. get
  87. {
  88. if (FileDataGrid.Items.Count > 0)
  89. {
  90. if (FileDataGrid.SelectedItems.Count > 0)
  91. {
  92. return FileDataGrid.SelectedItems[0] as FileInfoWithRange;
  93. }
  94. else
  95. {
  96. return FileDataGrid.Items[0] as FileInfoWithRange;
  97. }
  98. }
  99. else
  100. {
  101. return null;
  102. }
  103. }
  104. }
  105. private ObservableCollection<FileInfoWithRange> _fileInfoDataList;
  106. public ObservableCollection<FileInfoWithRange> FileInfoDataList
  107. {
  108. get { return _fileInfoDataList; }
  109. set
  110. {
  111. _fileInfoDataList = value;
  112. _fileInfoDataList.CollectionChanged += (sender, args) =>
  113. {
  114. FileNumText = _fileInfoDataList.Count;
  115. };
  116. }
  117. }
  118. public FileGridListWithPageRangeControl()
  119. {
  120. this.DataContext = this;
  121. InitializeComponent();
  122. }
  123. public event PropertyChangedEventHandler PropertyChanged;
  124. protected virtual void OnPropertyChanged(string propertyName = null)
  125. {
  126. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  127. }
  128. protected bool UpdateProper<T>(ref T properValue,
  129. T newValue,
  130. [CallerMemberName] string properName = "")
  131. {
  132. if (object.Equals(properValue, newValue))
  133. return false;
  134. properValue = newValue;
  135. OnPropertyChanged(properName);
  136. return true;
  137. }
  138. private void AddFilesBtn_Click(object sender, RoutedEventArgs e)
  139. {
  140. var openFileDialog = new OpenFileDialog();
  141. openFileDialog.Multiselect = true;
  142. openFileDialog.Filter = "PDF Files (*.pdf)|*.pdf";
  143. if (openFileDialog.ShowDialog() == DialogResult.OK)
  144. {
  145. foreach (string filePath in openFileDialog.FileNames)
  146. {
  147. CPDFDocument document = CPDFDocument.InitWithFilePath(filePath);
  148. if(document == null)
  149. {
  150. continue;
  151. }
  152. if (FileInfoDataList.Any(item => item.Path == document.FilePath))
  153. {
  154. continue;
  155. }
  156. if (document.IsLocked)
  157. {
  158. PasswordWindow passwordWindow = new PasswordWindow();
  159. passwordWindow.InitWithDocument(document);
  160. passwordWindow.Owner = Window.GetWindow(this);
  161. passwordWindow.PasswordDialog.SetShowText(filePath + " is encrypted.");
  162. passwordWindow.ShowDialog();
  163. if (document.IsLocked)
  164. {
  165. document.Release();
  166. continue;
  167. }
  168. }
  169. List<int> pageRangeList = new List<int>();
  170. for (int i = 0; i < document.PageCount; i++)
  171. {
  172. pageRangeList.Add(i + 1);
  173. }
  174. FileInfoDataList.Add(new FileInfoWithRange
  175. {
  176. Document = document,
  177. Name = document.FileName,
  178. Size = CommonHelper.GetFileSize(filePath),
  179. Path = document.FilePath,
  180. PageRangeList = pageRangeList
  181. });
  182. }
  183. }
  184. }
  185. private void RemoveBtn_Click(object sender, RoutedEventArgs e)
  186. {
  187. if (FileDataGrid.SelectedItems.Count == 0)
  188. {
  189. foreach (var item in FileInfoDataList)
  190. {
  191. item.Document.Release();
  192. }
  193. FileInfoDataList.Clear();
  194. return;
  195. }
  196. var selectedItems = FileDataGrid.SelectedItems;
  197. var selectedItemsList = selectedItems.Cast<FileInfoWithRange>().ToList();
  198. foreach (var item in selectedItemsList)
  199. {
  200. FileInfoDataList.Remove(item);
  201. }
  202. }
  203. private void PageRangeBtn_Click(object sender, RoutedEventArgs e)
  204. {
  205. PageRangeDialog pageRangeDialog = new PageRangeDialog();
  206. if (FileDataGrid.SelectedItems[0] is FileInfoWithRange fileInfo)
  207. {
  208. pageRangeDialog.InitWithFileInfo(fileInfo);
  209. }
  210. pageRangeDialog.Owner = Window.GetWindow(this);
  211. pageRangeDialog.WindowClosed += PageRangeDialog_WindowClosed;
  212. pageRangeDialog.ShowDialog();
  213. }
  214. private void PageRangeDialog_WindowClosed(object sender, List<int> result)
  215. {
  216. if (result != null)
  217. {
  218. (FileDataGrid.SelectedItems[0] as FileInfoWithRange).PageRangeList = result;
  219. }
  220. }
  221. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  222. {
  223. FileInfoDataList = new ObservableCollection<FileInfoWithRange>();
  224. FileDataGrid.ItemsSource = FileInfoDataList;
  225. }
  226. private void FileDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
  227. {
  228. var dataGrid = sender as DataGrid;
  229. if (dataGrid != null)
  230. {
  231. RemoveBtn.Content = dataGrid.SelectedItems.Count > 0 ? LanguageHelper.SecurityManager.GetString("Button_RemoveSelected") : LanguageHelper.SecurityManager.GetString("Button_RemoveAll");
  232. PageRangeBtn.Visibility = dataGrid.SelectedItems.Count == 1 ? Visibility.Visible : Visibility.Collapsed;
  233. }
  234. }
  235. private void FileDataGrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  236. {
  237. FileDataGrid.UnselectAll();
  238. }
  239. }
  240. }