123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433 |
- using ImTools;
- using PDF_Master.CustomControl;
- using PDF_Master.Helper;
- using PDF_Master.Model.Dialog.ToolsDialogs;
- using PDF_Master.ViewModels.Dialog.ToolsDialogs;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- namespace PDF_Master.Views.Dialog.ToolsDialogs
- {
- /// <summary>
- /// MergeDialog.xaml 的交互逻辑
- /// </summary>
- public partial class MergeDialog : UserControl
- {
- /// <summary>
- /// 当前显示了线段的Item
- /// </summary>
- private ListViewItem listViewItem = null;
- private bool IsFile = false;
- public MergeDialog()
- {
- InitializeComponent();
- }
- private void Delete_Click(object sender, RoutedEventArgs e)
- {
- (DataContext as MergeDialogViewModel).DeleteItem((sender as Button).DataContext as MergeObject);
- }
- private void MergeView_PreviewMouseMove(object sender, MouseEventArgs e)
- {
- if (e.LeftButton == MouseButtonState.Pressed)
- {
- var pos = e.GetPosition(MergeView);
- HitTestResult result = VisualTreeHelper.HitTest(MergeView, pos);
- if (result == null)
- {
- return;
- }
- //点击删除按钮
- Button Button = CommonHelper.FindVisualParent<Button>(result.VisualHit);
- if (Button != null)
- {
- return;
- }
- //点击下拉框
- WritableComboBox writableComboBox = CommonHelper.FindVisualParent<WritableComboBox>(result.VisualHit);
- if (writableComboBox != null)
- {
- return;
- }
- var treeViewItem = CommonHelper.FindVisualParent<ListViewItem>(result.VisualHit);
- if (treeViewItem == null)
- {
- return;
- }
- DataObject dataObj = new DataObject(treeViewItem);
- DragDrop.DoDragDrop(MergeView, dataObj, DragDropEffects.Move);
- return;
- }
- }
- private void MergeView_Drop(object sender, DragEventArgs e)
- {
- //避免和Grid_Drop事件重复触发,V1.2.0测试完成后可以删除
- //if (IsFile)
- //{
- // DragDropHelper.Drop(this, e);
- // string[] file = (string[])e.Data.GetData(DataFormats.FileDrop);
- // (DataContext as MergeDialogViewModel).AddFiles(file);
- //}
- ListViewItem souredata = e.Data.GetData(typeof(ListViewItem)) as ListViewItem;
- if (souredata == null)
- {
- return;
- }
- MergeObject soureNode = souredata.DataContext as MergeObject;
- //防止在外部点击拖入异常
- if (soureNode == null)
- {
- return;
- }
- Point pos = e.GetPosition(MergeView);
- HitTestResult result = VisualTreeHelper.HitTest(MergeView, pos);
- if (result == null)
- {
- HiddenaAllLine();
- return;
- }
- ListViewItem targetitem = CommonHelper.FindVisualParent<ListViewItem>(result.VisualHit);
- if (targetitem == null)
- {
- HiddenaAllLine();
- return;
- }
- MergeObject targetNode = targetitem.DataContext as MergeObject;
- if (soureNode.Equals(targetNode))
- {
- HiddenaAllLine();
- return;
- }
- (DataContext as MergeDialogViewModel).MoveMerge(targetNode, soureNode);
- HiddenaAllLine();
- }
- private void MergeView_DragOver(object sender, DragEventArgs e)
- {
- if (IsFile)
- {
- DragDropHelper.DragOver(this, e);
- }
- ListViewItem sourceitem = e.Data.GetData(typeof(ListViewItem)) as ListViewItem;
- if (sourceitem == null)
- {
- return;
- }
- Point pos = e.GetPosition(MergeView);
- HitTestResult result = VisualTreeHelper.HitTest(MergeView, pos);
- if (result != null)
- {
- ListViewItem treeviewitem = CommonHelper.FindVisualParent<ListViewItem>(result.VisualHit);
- if (treeviewitem != null)
- {
- if (listViewItem == null)
- {
- listViewItem = treeviewitem;
- }
- else if (!listViewItem.Equals(treeviewitem))
- {
- (listViewItem.DataContext as MergeObject).IsBackwards = false;
- (listViewItem.DataContext as MergeObject).IsForward = false;
- listViewItem = treeviewitem;
- }
- else
- {
- (listViewItem.DataContext as MergeObject).IsBackwards = false;
- (listViewItem.DataContext as MergeObject).IsForward = false;
- }
- if (listViewItem.Equals(sourceitem))
- {
- return;
- }
- //鼠标位于上半部分显示实线,下半部分显示虚线
- Point p = treeviewitem.TranslatePoint(new Point(0, 0), MergeView);
- if (pos.Y < p.Y + 16)
- {
- (treeviewitem.DataContext as MergeObject).IsBackwards = true;
- }
- else
- {
- (treeviewitem.DataContext as MergeObject).IsForward = true;
- }
- }
- }
- }
- private void MergeView_DragLeave(object sender, DragEventArgs e)
- {
- if (IsFile)
- {
- DragDropHelper.DragLeave();
- }
- HiddenaAllLine();
- }
- /// <summary>
- /// 清理线条显示,以及相关缓存对象
- /// </summary>
- private void HiddenaAllLine()
- {
- if (listViewItem != null)
- {
- (listViewItem.DataContext as MergeObject).IsBackwards = false;
- (listViewItem.DataContext as MergeObject).IsForward = false;
- listViewItem = null;
- }
- }
- private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
- {
- Regex regex = new Regex("[^0-9.]+");
- e.Handled = regex.IsMatch(e.Text);
- }
- private void TextBoxEx_LostFocus(object sender, RoutedEventArgs e)
- {
- try
- {
- double Tag = Double.Parse((sender as TextBox).Text);
- if (Tag <= 0)
- {
- (sender as TextBox).Text = "";
- }
- }
- catch (Exception)
- {
- (sender as TextBox).Text = "";
- }
- }
- private void TextBoxEx_PreviewKeyDown(object sender, KeyEventArgs e)
- {
- if (e.Key == Key.Enter)
- {
- BtnMerge.Focus();
- e.Handled = true;
- }
- }
- private void MergeView_DragEnter(object sender, DragEventArgs e)
- {
- IsFile = false;
- var file = (System.Array)e.Data.GetData(DataFormats.FileDrop);
- if (file == null)//为null 表示内部拖动 触发的
- {
- return;
- }
- foreach (string f in file)
- {
- bool isFolder = Directory.Exists(f);
- if (isFolder)
- {
- DragDropHelper.DragEnter(this, e);
- IsFile = true;
- TraverseFiles(f, null, e);
- }
- else
- {
- System.IO.FileInfo info = new System.IO.FileInfo(f);
- //只要拖拽进来的文件里包含有pdf格式文件,就允许拖入
- if (System.IO.Path.GetExtension(f).ToLower() == ".pdf" && info.Length > 0)
- {
- DragDropHelper.DragEnter(this, e);
- IsFile = true;
- }
- //图片格式
- else if (Properties.Resources.imageex.Contains(System.IO.Path.GetExtension(f).ToLower()) && info.Length > 0)
- {
- DragDropHelper.DragEnter(this, e);
- IsFile = true;
- }
- }
- }
- }
- private void Grid_Drop(object sender, DragEventArgs e)
- {
- if (IsFile)
- {
- DragDropHelper.Drop(this, e);
- string[] file = (string[])e.Data.GetData(DataFormats.FileDrop);
- bool isFolder = Directory.Exists(file[0]);
- if (isFolder)
- {
- string file1 = file[0];
- List<string> names = new List<string>();
- TraverseFiles(file1, names);
- (DataContext as MergeDialogViewModel).AddFiles(names.ToArray());
- }
- else
- {
- (DataContext as MergeDialogViewModel).AddFiles(file);
- }
- }
- }
- private void Grid_DragEnter(object sender, DragEventArgs e)
- {
- IsFile = false;
- var file = (System.Array)e.Data.GetData(DataFormats.FileDrop);
- if (file == null)//为null 表示内部拖动 触发的
- {
- return;
- }
- foreach (string f in file)
- {
- bool isFolder = Directory.Exists(f);
- if (isFolder)
- {
- DragDropHelper.DragEnter(this, e);
- IsFile = true;
- TraverseFiles(f, null, e);
- }
- else
- {
- System.IO.FileInfo info = new System.IO.FileInfo(f);
- //只要拖拽进来的文件里包含有pdf格式文件,就允许拖入
- if (System.IO.Path.GetExtension(f).ToLower() == ".pdf" && info.Length > 0)
- {
- DragDropHelper.DragEnter(this, e);
- IsFile = true;
- }
- //图片格式
- else if (Properties.Resources.imageex.Contains(System.IO.Path.GetExtension(f).ToLower()) && info.Length > 0)
- {
- DragDropHelper.DragEnter(this, e);
- IsFile = true;
- }
- }
- }
- }
- private void Grid_DragOver(object sender, DragEventArgs e)
- {
- if (IsFile)
- {
- DragDropHelper.DragOver(this, e);
- }
- }
- private void Grid_DragLeave(object sender, DragEventArgs e)
- {
- if (IsFile)
- {
- DragDropHelper.DragLeave();
- }
- }
- private void UserControl_Loaded(object sender, RoutedEventArgs e)
- {
- NoFileText.Text = App.MainPageLoader.GetString("Merge_Hint");
- TitleFileName.Text = App.MainPageLoader.GetString("Merge_TitleFileName");
- TitlePageRange.Text = App.MainPageLoader.GetString("Merge_TitlePageRange");
- TitleSize.Text = App.MainPageLoader.GetString("Merge_TitleSize");
- TxbAddFileText.Text = App.MainPageLoader.GetString("Merge_AddFiles");
- MenuAddComboBoxItem.Header = App.MainPageLoader.GetString("Merge_AddFiles");
- MenuAddFolderComboBoxItem.Header = App.MainPageLoader.GetString("Merge_AddFolder");
- MenuAddOpenFileComboBoxItem.Header = App.MainPageLoader.GetString("Merge_AddOpenFile");
- BtnMerge.Content = App.MainPageLoader.GetString("Merge_Yes");
- BtnCancel.Content = App.MainPageLoader.GetString("Merge_No");
- }
- private void PageRangeWritableComboBox_LostFocus(object sender, RoutedEventArgs e)
- {
- WritableComboBox writableComboBox = sender as WritableComboBox;
- var ls = writableComboBox.DataContext as MergeObject;
- if (ls != null)
- {
- foreach (var a in ls.SetPageRange)
- {
- Trace.WriteLine(a);
- }
- }
- }
- private void BtnAddFile_Initialized(object sender, EventArgs e)
- {
- var btn = sender as Button;
- if (btn != null)
- {
- btn.ContextMenu = null;
- }
- }
- private void BtnAddFile_Click(object sender, RoutedEventArgs e)
- {
- CmAddFile.PlacementTarget = BtnAddFile;
- CmAddFile.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
- CmAddFile.IsOpen = true;
- }
- /// <summary>
- /// 文件夹遍历
- /// </summary>
- /// <param name="folderPath"></param>
- /// <param name="names"></param>
- public void TraverseFiles(string folderPath, List<string> names, DragEventArgs e = null)
- {
- // 获取当前文件夹下的所有文件
- string[] files = Directory.GetFiles(folderPath);
- foreach (string file in files)
- {
- if (names != null) { names.Add(file); }
- // 处理文件
- if (e != null)
- {
- System.IO.FileInfo info = new System.IO.FileInfo(file);
- //只要拖拽进来的文件里包含有pdf格式文件,就允许拖入
- if (System.IO.Path.GetExtension(file).ToLower() == ".pdf" && info.Length > 0)
- {
- DragDropHelper.DragEnter(this, e);
- IsFile = true;
- }
- //图片格式
- else if (Properties.Resources.imageex.Contains(System.IO.Path.GetExtension(file).ToLower()) && info.Length > 0)
- {
- DragDropHelper.DragEnter(this, e);
- IsFile = true;
- }
- }
- }
- // 获取当前文件夹下的所有子文件夹
- string[] subFolders = Directory.GetDirectories(folderPath);
- foreach (string subFolder in subFolders)
- {
- // 递归遍历子文件夹中的文件
- TraverseFiles(subFolder, names);
- }
- }
- }
- }
|