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
{
///
/// MergeDialog.xaml 的交互逻辑
///
public partial class MergeDialog : UserControl
{
///
/// 当前显示了线段的Item
///
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(result.VisualHit);
if (Button != null)
{
return;
}
//点击下拉框
WritableComboBox writableComboBox = CommonHelper.FindVisualParent(result.VisualHit);
if (writableComboBox != null)
{
return;
}
var treeViewItem = CommonHelper.FindVisualParent(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(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(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();
}
///
/// 清理线条显示,以及相关缓存对象
///
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 names = new List();
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;
}
///
/// 文件夹遍历
///
///
///
public void TraverseFiles(string folderPath, List 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);
}
}
}
}