MergeDialog.xaml.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. using ImTools;
  2. using PDF_Master.CustomControl;
  3. using PDF_Master.Helper;
  4. using PDF_Master.Model.Dialog.ToolsDialogs;
  5. using PDF_Master.ViewModels.Dialog.ToolsDialogs;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Data;
  9. using System.Diagnostics;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Text.RegularExpressions;
  14. using System.Threading.Tasks;
  15. using System.Windows;
  16. using System.Windows.Controls;
  17. using System.Windows.Data;
  18. using System.Windows.Documents;
  19. using System.Windows.Input;
  20. using System.Windows.Media;
  21. using System.Windows.Media.Imaging;
  22. using System.Windows.Navigation;
  23. using System.Windows.Shapes;
  24. namespace PDF_Master.Views.Dialog.ToolsDialogs
  25. {
  26. /// <summary>
  27. /// MergeDialog.xaml 的交互逻辑
  28. /// </summary>
  29. public partial class MergeDialog : UserControl
  30. {
  31. /// <summary>
  32. /// 当前显示了线段的Item
  33. /// </summary>
  34. private ListViewItem listViewItem = null;
  35. private bool IsFile = false;
  36. public MergeDialog()
  37. {
  38. InitializeComponent();
  39. }
  40. private void Delete_Click(object sender, RoutedEventArgs e)
  41. {
  42. (DataContext as MergeDialogViewModel).DeleteItem((sender as Button).DataContext as MergeObject);
  43. }
  44. private void MergeView_PreviewMouseMove(object sender, MouseEventArgs e)
  45. {
  46. if (e.LeftButton == MouseButtonState.Pressed)
  47. {
  48. var pos = e.GetPosition(MergeView);
  49. HitTestResult result = VisualTreeHelper.HitTest(MergeView, pos);
  50. if (result == null)
  51. {
  52. return;
  53. }
  54. //点击删除按钮
  55. Button Button = CommonHelper.FindVisualParent<Button>(result.VisualHit);
  56. if (Button != null)
  57. {
  58. return;
  59. }
  60. //点击下拉框
  61. WritableComboBox writableComboBox = CommonHelper.FindVisualParent<WritableComboBox>(result.VisualHit);
  62. if (writableComboBox != null)
  63. {
  64. return;
  65. }
  66. var treeViewItem = CommonHelper.FindVisualParent<ListViewItem>(result.VisualHit);
  67. if (treeViewItem == null)
  68. {
  69. return;
  70. }
  71. DataObject dataObj = new DataObject(treeViewItem);
  72. DragDrop.DoDragDrop(MergeView, dataObj, DragDropEffects.Move);
  73. return;
  74. }
  75. }
  76. private void MergeView_Drop(object sender, DragEventArgs e)
  77. {
  78. //避免和Grid_Drop事件重复触发,V1.2.0测试完成后可以删除
  79. //if (IsFile)
  80. //{
  81. // DragDropHelper.Drop(this, e);
  82. // string[] file = (string[])e.Data.GetData(DataFormats.FileDrop);
  83. // (DataContext as MergeDialogViewModel).AddFiles(file);
  84. //}
  85. ListViewItem souredata = e.Data.GetData(typeof(ListViewItem)) as ListViewItem;
  86. if (souredata == null)
  87. {
  88. return;
  89. }
  90. MergeObject soureNode = souredata.DataContext as MergeObject;
  91. //防止在外部点击拖入异常
  92. if (soureNode == null)
  93. {
  94. return;
  95. }
  96. Point pos = e.GetPosition(MergeView);
  97. HitTestResult result = VisualTreeHelper.HitTest(MergeView, pos);
  98. if (result == null)
  99. {
  100. HiddenaAllLine();
  101. return;
  102. }
  103. ListViewItem targetitem = CommonHelper.FindVisualParent<ListViewItem>(result.VisualHit);
  104. if (targetitem == null)
  105. {
  106. HiddenaAllLine();
  107. return;
  108. }
  109. MergeObject targetNode = targetitem.DataContext as MergeObject;
  110. if (soureNode.Equals(targetNode))
  111. {
  112. HiddenaAllLine();
  113. return;
  114. }
  115. (DataContext as MergeDialogViewModel).MoveMerge(targetNode, soureNode);
  116. HiddenaAllLine();
  117. }
  118. private void MergeView_DragOver(object sender, DragEventArgs e)
  119. {
  120. if (IsFile)
  121. {
  122. DragDropHelper.DragOver(this, e);
  123. }
  124. ListViewItem sourceitem = e.Data.GetData(typeof(ListViewItem)) as ListViewItem;
  125. if (sourceitem == null)
  126. {
  127. return;
  128. }
  129. Point pos = e.GetPosition(MergeView);
  130. HitTestResult result = VisualTreeHelper.HitTest(MergeView, pos);
  131. if (result != null)
  132. {
  133. ListViewItem treeviewitem = CommonHelper.FindVisualParent<ListViewItem>(result.VisualHit);
  134. if (treeviewitem != null)
  135. {
  136. if (listViewItem == null)
  137. {
  138. listViewItem = treeviewitem;
  139. }
  140. else if (!listViewItem.Equals(treeviewitem))
  141. {
  142. (listViewItem.DataContext as MergeObject).IsBackwards = false;
  143. (listViewItem.DataContext as MergeObject).IsForward = false;
  144. listViewItem = treeviewitem;
  145. }
  146. else
  147. {
  148. (listViewItem.DataContext as MergeObject).IsBackwards = false;
  149. (listViewItem.DataContext as MergeObject).IsForward = false;
  150. }
  151. if (listViewItem.Equals(sourceitem))
  152. {
  153. return;
  154. }
  155. //鼠标位于上半部分显示实线,下半部分显示虚线
  156. Point p = treeviewitem.TranslatePoint(new Point(0, 0), MergeView);
  157. if (pos.Y < p.Y + 16)
  158. {
  159. (treeviewitem.DataContext as MergeObject).IsBackwards = true;
  160. }
  161. else
  162. {
  163. (treeviewitem.DataContext as MergeObject).IsForward = true;
  164. }
  165. }
  166. }
  167. }
  168. private void MergeView_DragLeave(object sender, DragEventArgs e)
  169. {
  170. if (IsFile)
  171. {
  172. DragDropHelper.DragLeave();
  173. }
  174. HiddenaAllLine();
  175. }
  176. /// <summary>
  177. /// 清理线条显示,以及相关缓存对象
  178. /// </summary>
  179. private void HiddenaAllLine()
  180. {
  181. if (listViewItem != null)
  182. {
  183. (listViewItem.DataContext as MergeObject).IsBackwards = false;
  184. (listViewItem.DataContext as MergeObject).IsForward = false;
  185. listViewItem = null;
  186. }
  187. }
  188. private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
  189. {
  190. Regex regex = new Regex("[^0-9.]+");
  191. e.Handled = regex.IsMatch(e.Text);
  192. }
  193. private void TextBoxEx_LostFocus(object sender, RoutedEventArgs e)
  194. {
  195. try
  196. {
  197. double Tag = Double.Parse((sender as TextBox).Text);
  198. if (Tag <= 0)
  199. {
  200. (sender as TextBox).Text = "";
  201. }
  202. }
  203. catch (Exception)
  204. {
  205. (sender as TextBox).Text = "";
  206. }
  207. }
  208. private void TextBoxEx_PreviewKeyDown(object sender, KeyEventArgs e)
  209. {
  210. if (e.Key == Key.Enter)
  211. {
  212. BtnMerge.Focus();
  213. e.Handled = true;
  214. }
  215. }
  216. private void MergeView_DragEnter(object sender, DragEventArgs e)
  217. {
  218. IsFile = false;
  219. var file = (System.Array)e.Data.GetData(DataFormats.FileDrop);
  220. if (file == null)//为null 表示内部拖动 触发的
  221. {
  222. return;
  223. }
  224. foreach (string f in file)
  225. {
  226. bool isFolder = Directory.Exists(f);
  227. if (isFolder)
  228. {
  229. DragDropHelper.DragEnter(this, e);
  230. IsFile = true;
  231. TraverseFiles(f, null, e);
  232. }
  233. else
  234. {
  235. System.IO.FileInfo info = new System.IO.FileInfo(f);
  236. //只要拖拽进来的文件里包含有pdf格式文件,就允许拖入
  237. if (System.IO.Path.GetExtension(f).ToLower() == ".pdf" && info.Length > 0)
  238. {
  239. DragDropHelper.DragEnter(this, e);
  240. IsFile = true;
  241. }
  242. //图片格式
  243. else if (Properties.Resources.imageex.Contains(System.IO.Path.GetExtension(f).ToLower()) && info.Length > 0)
  244. {
  245. DragDropHelper.DragEnter(this, e);
  246. IsFile = true;
  247. }
  248. }
  249. }
  250. }
  251. private void Grid_Drop(object sender, DragEventArgs e)
  252. {
  253. if (IsFile)
  254. {
  255. DragDropHelper.Drop(this, e);
  256. string[] file = (string[])e.Data.GetData(DataFormats.FileDrop);
  257. bool isFolder = Directory.Exists(file[0]);
  258. if (isFolder)
  259. {
  260. string file1 = file[0];
  261. List<string> names = new List<string>();
  262. TraverseFiles(file1, names);
  263. (DataContext as MergeDialogViewModel).AddFiles(names.ToArray());
  264. }
  265. else
  266. {
  267. (DataContext as MergeDialogViewModel).AddFiles(file);
  268. }
  269. }
  270. }
  271. private void Grid_DragEnter(object sender, DragEventArgs e)
  272. {
  273. IsFile = false;
  274. var file = (System.Array)e.Data.GetData(DataFormats.FileDrop);
  275. if (file == null)//为null 表示内部拖动 触发的
  276. {
  277. return;
  278. }
  279. foreach (string f in file)
  280. {
  281. bool isFolder = Directory.Exists(f);
  282. if (isFolder)
  283. {
  284. DragDropHelper.DragEnter(this, e);
  285. IsFile = true;
  286. TraverseFiles(f, null, e);
  287. }
  288. else
  289. {
  290. System.IO.FileInfo info = new System.IO.FileInfo(f);
  291. //只要拖拽进来的文件里包含有pdf格式文件,就允许拖入
  292. if (System.IO.Path.GetExtension(f).ToLower() == ".pdf" && info.Length > 0)
  293. {
  294. DragDropHelper.DragEnter(this, e);
  295. IsFile = true;
  296. }
  297. //图片格式
  298. else if (Properties.Resources.imageex.Contains(System.IO.Path.GetExtension(f).ToLower()) && info.Length > 0)
  299. {
  300. DragDropHelper.DragEnter(this, e);
  301. IsFile = true;
  302. }
  303. }
  304. }
  305. }
  306. private void Grid_DragOver(object sender, DragEventArgs e)
  307. {
  308. if (IsFile)
  309. {
  310. DragDropHelper.DragOver(this, e);
  311. }
  312. }
  313. private void Grid_DragLeave(object sender, DragEventArgs e)
  314. {
  315. if (IsFile)
  316. {
  317. DragDropHelper.DragLeave();
  318. }
  319. }
  320. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  321. {
  322. NoFileText.Text = App.MainPageLoader.GetString("Merge_Hint");
  323. TitleFileName.Text = App.MainPageLoader.GetString("Merge_TitleFileName");
  324. TitlePageRange.Text = App.MainPageLoader.GetString("Merge_TitlePageRange");
  325. TitleSize.Text = App.MainPageLoader.GetString("Merge_TitleSize");
  326. TxbAddFileText.Text = App.MainPageLoader.GetString("Merge_AddFiles");
  327. MenuAddComboBoxItem.Header = App.MainPageLoader.GetString("Merge_AddFiles");
  328. MenuAddFolderComboBoxItem.Header = App.MainPageLoader.GetString("Merge_AddFolder");
  329. MenuAddOpenFileComboBoxItem.Header = App.MainPageLoader.GetString("Merge_AddOpenFile");
  330. BtnMerge.Content = App.MainPageLoader.GetString("Merge_Yes");
  331. BtnCancel.Content = App.MainPageLoader.GetString("Merge_No");
  332. }
  333. private void PageRangeWritableComboBox_LostFocus(object sender, RoutedEventArgs e)
  334. {
  335. WritableComboBox writableComboBox = sender as WritableComboBox;
  336. var ls = writableComboBox.DataContext as MergeObject;
  337. if (ls != null)
  338. {
  339. foreach (var a in ls.SetPageRange)
  340. {
  341. Trace.WriteLine(a);
  342. }
  343. }
  344. }
  345. private void BtnAddFile_Initialized(object sender, EventArgs e)
  346. {
  347. var btn = sender as Button;
  348. if (btn != null)
  349. {
  350. btn.ContextMenu = null;
  351. }
  352. }
  353. private void BtnAddFile_Click(object sender, RoutedEventArgs e)
  354. {
  355. CmAddFile.PlacementTarget = BtnAddFile;
  356. CmAddFile.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
  357. CmAddFile.IsOpen = true;
  358. }
  359. /// <summary>
  360. /// 文件夹遍历
  361. /// </summary>
  362. /// <param name="folderPath"></param>
  363. /// <param name="names"></param>
  364. public void TraverseFiles(string folderPath, List<string> names, DragEventArgs e = null)
  365. {
  366. // 获取当前文件夹下的所有文件
  367. string[] files = Directory.GetFiles(folderPath);
  368. foreach (string file in files)
  369. {
  370. if (names != null) { names.Add(file); }
  371. // 处理文件
  372. if (e != null)
  373. {
  374. System.IO.FileInfo info = new System.IO.FileInfo(file);
  375. //只要拖拽进来的文件里包含有pdf格式文件,就允许拖入
  376. if (System.IO.Path.GetExtension(file).ToLower() == ".pdf" && info.Length > 0)
  377. {
  378. DragDropHelper.DragEnter(this, e);
  379. IsFile = true;
  380. }
  381. //图片格式
  382. else if (Properties.Resources.imageex.Contains(System.IO.Path.GetExtension(file).ToLower()) && info.Length > 0)
  383. {
  384. DragDropHelper.DragEnter(this, e);
  385. IsFile = true;
  386. }
  387. }
  388. }
  389. // 获取当前文件夹下的所有子文件夹
  390. string[] subFolders = Directory.GetDirectories(folderPath);
  391. foreach (string subFolder in subFolders)
  392. {
  393. // 递归遍历子文件夹中的文件
  394. TraverseFiles(subFolder, names);
  395. }
  396. }
  397. }
  398. }