MergeDialog.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. using PDF_Master.CustomControl;
  2. using PDF_Master.Helper;
  3. using PDF_Master.Model.Dialog.ToolsDialogs;
  4. using PDF_Master.ViewModels.Dialog.ToolsDialogs;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Data;
  14. using System.Windows.Documents;
  15. using System.Windows.Input;
  16. using System.Windows.Media;
  17. using System.Windows.Media.Imaging;
  18. using System.Windows.Navigation;
  19. using System.Windows.Shapes;
  20. namespace PDF_Master.Views.Dialog.ToolsDialogs
  21. {
  22. /// <summary>
  23. /// MergeDialog.xaml 的交互逻辑
  24. /// </summary>
  25. public partial class MergeDialog : UserControl
  26. {
  27. /// <summary>
  28. /// 当前显示了线段的Item
  29. /// </summary>
  30. private ListViewItem listViewItem = null;
  31. private bool IsFile = false;
  32. public MergeDialog()
  33. {
  34. InitializeComponent();
  35. }
  36. private void Delete_Click(object sender, RoutedEventArgs e)
  37. {
  38. (DataContext as MergeDialogViewModel).DeleteItem((sender as Button).DataContext as MergeObject);
  39. }
  40. private void MergeView_PreviewMouseMove(object sender, MouseEventArgs e)
  41. {
  42. if (e.LeftButton == MouseButtonState.Pressed)
  43. {
  44. var pos = e.GetPosition(MergeView);
  45. HitTestResult result = VisualTreeHelper.HitTest(MergeView, pos);
  46. if (result == null)
  47. {
  48. return;
  49. }
  50. //点击删除按钮
  51. Button Button = CommonHelper.FindVisualParent<Button>(result.VisualHit);
  52. if (Button != null)
  53. {
  54. return;
  55. }
  56. //点击下拉框
  57. WritableComboBox writableComboBox = CommonHelper.FindVisualParent<WritableComboBox>(result.VisualHit);
  58. if (writableComboBox != null)
  59. {
  60. return;
  61. }
  62. var treeViewItem = CommonHelper.FindVisualParent<ListViewItem>(result.VisualHit);
  63. if (treeViewItem == null)
  64. {
  65. return;
  66. }
  67. DataObject dataObj = new DataObject(treeViewItem);
  68. DragDrop.DoDragDrop(MergeView, dataObj, DragDropEffects.Move);
  69. return;
  70. }
  71. }
  72. private void MergeView_Drop(object sender, DragEventArgs e)
  73. {
  74. if (IsFile)
  75. {
  76. DragDropHelper.Drop(this, e);
  77. string[] file = (string[])e.Data.GetData(DataFormats.FileDrop);
  78. (DataContext as MergeDialogViewModel).AddFiles(file);
  79. }
  80. ListViewItem souredata = e.Data.GetData(typeof(ListViewItem)) as ListViewItem;
  81. if (souredata == null)
  82. {
  83. return;
  84. }
  85. MergeObject soureNode = souredata.DataContext as MergeObject;
  86. Point pos = e.GetPosition(MergeView);
  87. HitTestResult result = VisualTreeHelper.HitTest(MergeView, pos);
  88. if (result == null)
  89. {
  90. HiddenaAllLine();
  91. return;
  92. }
  93. ListViewItem targetitem = CommonHelper.FindVisualParent<ListViewItem>(result.VisualHit);
  94. if (targetitem == null)
  95. {
  96. HiddenaAllLine();
  97. return;
  98. }
  99. MergeObject targetNode = targetitem.DataContext as MergeObject;
  100. if (soureNode.Equals(targetNode))
  101. {
  102. HiddenaAllLine();
  103. return;
  104. }
  105. (DataContext as MergeDialogViewModel).MoveMerge(targetNode, soureNode);
  106. HiddenaAllLine();
  107. }
  108. private void MergeView_DragOver(object sender, DragEventArgs e)
  109. {
  110. if (IsFile)
  111. {
  112. DragDropHelper.DragOver(this, e);
  113. }
  114. ListViewItem sourceitem = e.Data.GetData(typeof(ListViewItem)) as ListViewItem;
  115. if (sourceitem == null)
  116. {
  117. return;
  118. }
  119. Point pos = e.GetPosition(MergeView);
  120. HitTestResult result = VisualTreeHelper.HitTest(MergeView, pos);
  121. if (result != null)
  122. {
  123. ListViewItem treeviewitem = CommonHelper.FindVisualParent<ListViewItem>(result.VisualHit);
  124. if (treeviewitem != null)
  125. {
  126. if (listViewItem == null)
  127. {
  128. listViewItem = treeviewitem;
  129. }
  130. else if (!listViewItem.Equals(treeviewitem))
  131. {
  132. (listViewItem.DataContext as MergeObject).IsBackwards = false;
  133. (listViewItem.DataContext as MergeObject).IsForward = false;
  134. listViewItem = treeviewitem;
  135. }
  136. else
  137. {
  138. (listViewItem.DataContext as MergeObject).IsBackwards = false;
  139. (listViewItem.DataContext as MergeObject).IsForward = false;
  140. }
  141. if (listViewItem.Equals(sourceitem))
  142. {
  143. return;
  144. }
  145. //鼠标位于上半部分显示实线,下半部分显示虚线
  146. Point p = treeviewitem.TranslatePoint(new Point(0, 0), MergeView);
  147. if (pos.Y < p.Y + 16)
  148. {
  149. (treeviewitem.DataContext as MergeObject).IsBackwards = true;
  150. }
  151. else
  152. {
  153. (treeviewitem.DataContext as MergeObject).IsForward = true;
  154. }
  155. }
  156. }
  157. }
  158. private void MergeView_DragLeave(object sender, DragEventArgs e)
  159. {
  160. if (IsFile)
  161. {
  162. DragDropHelper.DragLeave();
  163. }
  164. HiddenaAllLine();
  165. }
  166. /// <summary>
  167. /// 清理线条显示,以及相关缓存对象
  168. /// </summary>
  169. private void HiddenaAllLine()
  170. {
  171. if (listViewItem != null)
  172. {
  173. (listViewItem.DataContext as MergeObject).IsBackwards = false;
  174. (listViewItem.DataContext as MergeObject).IsForward = false;
  175. listViewItem = null;
  176. }
  177. }
  178. private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
  179. {
  180. Regex regex = new Regex("[^0-9.]+");
  181. e.Handled = regex.IsMatch(e.Text);
  182. }
  183. private void TextBoxEx_LostFocus(object sender, RoutedEventArgs e)
  184. {
  185. try
  186. {
  187. double Tag = Double.Parse((sender as TextBox).Text);
  188. if (Tag <= 0)
  189. {
  190. (sender as TextBox).Text = "";
  191. }
  192. }
  193. catch (Exception)
  194. {
  195. (sender as TextBox).Text = "";
  196. }
  197. }
  198. private void TextBoxEx_PreviewKeyDown(object sender, KeyEventArgs e)
  199. {
  200. if (e.Key == Key.Enter)
  201. {
  202. BtnMerge.Focus();
  203. e.Handled = true;
  204. }
  205. }
  206. private void MergeView_DragEnter(object sender, DragEventArgs e)
  207. {
  208. IsFile = false;
  209. var file = (System.Array)e.Data.GetData(DataFormats.FileDrop);
  210. if (file == null)//为null 表示内部拖动 触发的
  211. {
  212. return;
  213. }
  214. foreach (string f in file)
  215. {
  216. System.IO.FileInfo info = new System.IO.FileInfo(f);
  217. //只要拖拽进来的文件里包含有pdf格式文件,就允许拖入
  218. if (System.IO.Path.GetExtension(f).ToLower() == ".pdf" && info.Length > 0)
  219. {
  220. DragDropHelper.DragEnter(this, e);
  221. IsFile = true;
  222. }
  223. //图片格式
  224. else if (Properties.Resources.imageex.Contains(System.IO.Path.GetExtension(f).ToLower()) && info.Length > 0)
  225. {
  226. DragDropHelper.DragEnter(this, e);
  227. IsFile = true;
  228. }
  229. }
  230. }
  231. private void Grid_Drop(object sender, DragEventArgs e)
  232. {
  233. if (IsFile)
  234. {
  235. DragDropHelper.Drop(this, e);
  236. string[] file = (string[])e.Data.GetData(DataFormats.FileDrop);
  237. (DataContext as MergeDialogViewModel).AddFiles(file);
  238. }
  239. }
  240. private void Grid_DragEnter(object sender, DragEventArgs e)
  241. {
  242. IsFile = false;
  243. var file = (System.Array)e.Data.GetData(DataFormats.FileDrop);
  244. if (file == null)//为null 表示内部拖动 触发的
  245. {
  246. return;
  247. }
  248. foreach (string f in file)
  249. {
  250. System.IO.FileInfo info = new System.IO.FileInfo(f);
  251. //只要拖拽进来的文件里包含有pdf格式文件,就允许拖入
  252. if (System.IO.Path.GetExtension(f).ToLower() == ".pdf" && info.Length > 0)
  253. {
  254. DragDropHelper.DragEnter(this, e);
  255. IsFile = true;
  256. }
  257. //图片格式
  258. else if (Properties.Resources.imageex.Contains(System.IO.Path.GetExtension(f).ToLower()) && info.Length > 0)
  259. {
  260. DragDropHelper.DragEnter(this, e);
  261. IsFile = true;
  262. }
  263. }
  264. }
  265. private void Grid_DragOver(object sender, DragEventArgs e)
  266. {
  267. if (IsFile)
  268. {
  269. DragDropHelper.DragOver(this, e);
  270. }
  271. }
  272. private void Grid_DragLeave(object sender, DragEventArgs e)
  273. {
  274. if (IsFile)
  275. {
  276. DragDropHelper.DragLeave();
  277. }
  278. }
  279. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  280. {
  281. NoFileText.Text = App.MainPageLoader.GetString("Merge_Hint");
  282. TitleFileName.Text = App.MainPageLoader.GetString("Merge_TitleFileName");
  283. TitlePageRange.Text = App.MainPageLoader.GetString("Merge_TitlePageRange");
  284. TitleSize.Text = App.MainPageLoader.GetString("Merge_TitleSize");
  285. AddComboBoxItemText.Text = App.MainPageLoader.GetString("Merge_AddFiles");
  286. AddFolderComboBoxItemText.Text = App.MainPageLoader.GetString("Merge_AddFolder");
  287. AddOpenFileComboBoxItem.Text = App.MainPageLoader.GetString("Merge_AddOpenFile");
  288. BtnMerge.Content = App.MainPageLoader.GetString("Merge_Yes");
  289. BtnCancel.Content = App.MainPageLoader.GetString("Merge_No");
  290. }
  291. }
  292. }