FileGridListWithPageRangeControl.xaml.cs 8.1 KB

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