PageEditContent.xaml.cs 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  1. using PDF_Master.EventAggregators;
  2. using PDF_Master.Helper;
  3. using PDF_Master.Model.PageEdit;
  4. using PDF_Master.ViewModels.PageEdit;
  5. using Prism.Events;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Controls.Primitives;
  14. using System.Windows.Data;
  15. using System.Windows.Documents;
  16. using System.Windows.Input;
  17. using System.Windows.Media;
  18. using System.Windows.Media.Imaging;
  19. using System.Windows.Navigation;
  20. using System.Windows.Shapes;
  21. using System.Windows.Threading;
  22. using WpfToolkit.Controls;
  23. namespace PDF_Master.Views.PageEdit
  24. {
  25. /// <summary>
  26. /// PageEditContent.xaml 的交互逻辑
  27. /// </summary>
  28. public partial class PageEditContent : UserControl
  29. {
  30. /// <summary>
  31. /// 用于通知刷新图片的事件
  32. /// 因为目前的图片刷新策略依赖很多UI事件判断,因为将主要判断逻辑放在UI层,VM负责图片刷新,非必要情况尽量不要采用这种形式
  33. /// </summary>
  34. private IEventAggregator eventor;
  35. /// <summary>
  36. /// 暂存的唯一索引值,用于区分多页签
  37. /// </summary>
  38. private string unicode;
  39. /// <summary>
  40. /// 是否是加载时第一次触发滚动
  41. /// </summary>
  42. private bool isFirstScrollChange = true;
  43. /// <summary>
  44. /// 用于判断滚轮停止的计时器
  45. /// </summary>
  46. private DispatcherTimer timer = new DispatcherTimer();
  47. /// <summary>
  48. /// 判断是否开始框选
  49. /// </summary>
  50. private bool startChoose = false;
  51. /// <summary>
  52. /// 框选的起始位置
  53. /// </summary>
  54. private Point starPosition = new Point();
  55. /// <summary>
  56. /// 记录当前滑块的状态
  57. /// </summary>
  58. private ScrollEventType scrolltype = ScrollEventType.EndScroll;
  59. //鼠标点击时在item中的位置 实现类似点哪拖哪的细节
  60. private double item_x;
  61. private double item_y;
  62. //插入标记代表的插入位置
  63. private int InsertIndex = -1;
  64. //拖动的Item
  65. private PageEditItem tempItem;
  66. /// <summary>
  67. /// 是否正在拖拽排序中,通过该变量避免单击触发拖动
  68. /// </summary>
  69. private bool isDraging = false;
  70. //是否正在从外部拖入文件
  71. public bool isDragingEnter { get; set; } = false;
  72. /// <summary>
  73. /// 是否需要自动滚动
  74. /// </summary>
  75. private bool needScroll = false;
  76. //自动滚动速度
  77. private int speed = 0;
  78. private PageEditContentViewModel viewModel;
  79. public PageEditContent()
  80. {
  81. InitializeComponent();
  82. }
  83. private void PageEditItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  84. {
  85. //BOTA缩略图里 插入子项时,刷新子项大小
  86. if (GridBOTAHeader.Visibility == Visibility.Visible&&e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
  87. {
  88. ItemSuitAcutalWidth(this.ActualWidth);
  89. }
  90. }
  91. public PageEditContent(IEventAggregator eventAggregator) : this()
  92. {
  93. eventor = eventAggregator;
  94. unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  95. timer.Interval = TimeSpan.FromSeconds(0.3);
  96. timer.Tick += Timer_Tick;
  97. viewModel = this.DataContext as PageEditContentViewModel;
  98. viewModel.PageEditItems.CollectionChanged += PageEditItems_CollectionChanged;
  99. //订阅页面刷新事件
  100. eventor.GetEvent<PageEditNotifyEvent>().Subscribe(OneNotifyEvent, e => e.Unicode == unicode);
  101. eventAggregator.GetEvent<CleanSelectAllEvent>().Subscribe(CleanSelectAll, e => e.Unicode == unicode);
  102. }
  103. private void CleanSelectAll(CleanSelectAllArgs obj)
  104. {
  105. ListPageEdit.SelectedItems.Clear();
  106. ListPageEdit.SelectedIndex = viewModel.PDFViewer.CurrentIndex;
  107. }
  108. private void Timer_Tick(object sender, EventArgs e)
  109. {
  110. PulishEvent();
  111. timer.Stop();
  112. }
  113. private void OneNotifyEvent(PageEditNotifyEventArgs e)
  114. {
  115. switch (e.Type)
  116. {
  117. case NotifyType.RefreshPage:
  118. PulishEvent();
  119. break;
  120. case NotifyType.SelectItems:
  121. if (e.PageRange.Count == 1)
  122. {
  123. ListPageEdit.SelectedIndex = e.PageRange[0] - 1;
  124. ListPageEdit.ScrollIntoView(ListPageEdit.SelectedItem);
  125. }
  126. else
  127. {
  128. ListPageEdit.SelectedItems.Clear();
  129. for (int i = 0; i < e.PageRange.Count; i++)
  130. {
  131. ListPageEdit.SelectedItems.Add(ListPageEdit.Items[e.PageRange[i] - 1]);
  132. }
  133. }
  134. break;
  135. }
  136. }
  137. #region UI事件
  138. /// <summary>
  139. /// 每次显示的时候就触发事件,刷新所有图片
  140. /// </summary>
  141. /// <param name="sender"></param>
  142. /// <param name="e"></param>
  143. private void PageEdit_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  144. {
  145. if ((bool)e.NewValue)
  146. {
  147. //当前页面没有发生变化时,刷新图片 这种情况下会拿两次图,需要留意
  148. PulishEvent();
  149. //当前页面发生变化时通过ScrollChanged事件来刷新图片
  150. isFirstScrollChange = true;
  151. if (GridBOTAHeader.Visibility == Visibility.Visible)
  152. {
  153. ItemSuitAcutalWidth(this.ActualWidth);
  154. }
  155. }
  156. }
  157. /// <summary>
  158. /// 鼠标滚轮滚动时触发的事件
  159. /// </summary>
  160. /// <param name="sender"></param>
  161. /// <param name="e"></param>
  162. private void ListPageEdit_ScrollChanged(object sender, ScrollChangedEventArgs e)
  163. {
  164. if (isFirstScrollChange)
  165. {
  166. PulishEvent();//第一次加载时触发的Scollchange 直接刷新界面,减少白板显示时间
  167. isFirstScrollChange = false;
  168. return;
  169. }
  170. else
  171. {
  172. if (e.VerticalChange != 0)
  173. timer.Start();//暂时找不到比较好的 判断Scroller停止的方法,先用计时器粗略判断
  174. }
  175. }
  176. /// <summary>
  177. /// 拖动右侧滑块时触发的事件
  178. /// </summary>
  179. /// <param name="sender"></param>
  180. /// <param name="e"></param>
  181. private void ListPageEdit_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)
  182. {
  183. scrolltype = e.ScrollEventType;
  184. if (scrolltype != System.Windows.Controls.Primitives.ScrollEventType.EndScroll)
  185. {
  186. timer.Stop();
  187. return;
  188. }
  189. PulishEvent();
  190. }
  191. #endregion
  192. #region 方法
  193. /// <summary>
  194. /// 发布事件
  195. /// </summary>
  196. private void PulishEvent()
  197. {
  198. var itemSize = new Size((ListPageEdit.Items[0] as PageEditItem).ItemSize.Width+32,(ListPageEdit.Items[0] as PageEditItem).ItemSize.Height+30);
  199. var range = GetRoughFromView(ListPageEdit, itemSize, new Thickness(5,10,5,10));
  200. if((bool)TbnTwoLine.IsChecked)
  201. {
  202. ///双列模式下要计算两列item
  203. range = new Tuple<int, int, int>(range.Item1,range.Item2*2,range.Item3);
  204. }
  205. eventor.GetEvent<PageEditRefreshEvent>().Publish(new PageEditRefreshEventArgs() { Unicode = unicode, PageRange = range });
  206. }
  207. /// <summary>
  208. /// 获取滑轨的垂直偏移量,结合item总数和Item尺寸以及间隔,来估算实际展示的item范围
  209. /// 返回值为页面范围 从1开始
  210. /// </summary>
  211. /// <param name="view"></param>
  212. /// <param name="itemSize"></param>
  213. /// <param name="itemMargin"></param>
  214. /// <returns></returns>
  215. private Tuple<int, int, int> GetRoughFromView(ListBox view, Size itemSize, Thickness itemMargin)
  216. {
  217. //var scrollViewer = GetScrollHost(view);
  218. var scrollViewer = CommonHelper.FindVisualChild<ScrollViewer>(view);
  219. if (scrollViewer == null || scrollViewer.ActualHeight == 0 || scrollViewer.ActualWidth == 0)//视图展开
  220. return new Tuple<int, int, int>(0, 0, 0);
  221. try
  222. {
  223. var currentHeight = scrollViewer.ActualHeight - view.Padding.Top;
  224. var currentWidth = scrollViewer.ActualWidth;
  225. //计算当前窗口大小能显示的行数和列数
  226. var columnCount = (int)(currentWidth / (itemSize.Width + itemMargin.Left));
  227. var rowCount = (int)Math.Ceiling(currentHeight / (itemSize.Height + itemMargin.Bottom));
  228. var preItemCount = (int)((scrollViewer.VerticalOffset / scrollViewer.ExtentHeight) * ((view.Items.Count + columnCount - 1) / columnCount));//滑动百分比*行数 = 大概的垂直位置
  229. preItemCount = preItemCount * columnCount;
  230. var preEnd = (int)(((scrollViewer.VerticalOffset + scrollViewer.ActualHeight) / scrollViewer.ExtentHeight) * ((view.Items.Count + columnCount - 1) / columnCount));
  231. preEnd = preEnd * columnCount + columnCount - 1;
  232. var middle = (int)Math.Ceiling(preItemCount + preEnd / 2d);
  233. return new Tuple<int, int, int>(
  234. Math.Max(preItemCount, 0),
  235. Math.Min(view.Items.Count, preEnd),
  236. middle);
  237. }
  238. catch { }
  239. return new Tuple<int, int, int>(0, 0, 0);
  240. }
  241. /// <summary>
  242. /// 获取listbox里的ScrollViewer对象
  243. /// 留意如果有重写ListBox对象,该方法可能无效
  244. /// </summary>
  245. /// <param name="listBox"></param>
  246. /// <returns></returns>
  247. private ScrollViewer GetScrollHost(ListBox listBox)
  248. {
  249. if (VisualTreeHelper.GetChildrenCount(listBox) > 0)
  250. {
  251. int s = VisualTreeHelper.GetChildrenCount(listBox);
  252. Border border = VisualTreeHelper.GetChild(listBox, 0) as Border;
  253. if (border != null)
  254. {
  255. return VisualTreeHelper.GetChild(border, 0) as ScrollViewer;
  256. }
  257. }
  258. return null;
  259. }
  260. #endregion
  261. /// <summary>
  262. /// 输入框显示时,自动获取焦点
  263. /// </summary>
  264. /// <param name="sender"></param>
  265. /// <param name="e"></param>
  266. private void TextBox_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  267. {
  268. if ((bool)e.NewValue)
  269. {
  270. (sender as Control).Focus();
  271. }
  272. }
  273. private void ListPageEdit_PreviewMouseMove(object sender, MouseEventArgs e)
  274. {
  275. try
  276. {
  277. if (e.LeftButton == MouseButtonState.Pressed&&!isDragingEnter)
  278. {
  279. //鼠标框选逻辑
  280. if (startChoose)
  281. {
  282. var position = e.GetPosition(ListPageEdit);
  283. if (position.X < 5 || position.X > ListPageEdit.ActualWidth - 5 || position.Y < 5 || position.Y > ListPageEdit.ActualHeight - 5)
  284. {
  285. startChoose = false;
  286. RectChoose.Visibility = Visibility.Collapsed;
  287. //暂时未想到靠近顶部和底部自动翻滚的好方法,只能先屏蔽这部分功能
  288. Mouse.Capture(null);
  289. return;
  290. }
  291. //矩形框内的item设为选中
  292. DoSelectItems();
  293. return;
  294. }
  295. //拖拽排序的逻辑
  296. var pos = e.GetPosition(ListPageEdit);
  297. if (pos.Y < 0 || pos.Y > ListPageEdit.ActualHeight)
  298. {
  299. LineInset.Visibility = Visibility.Collapsed;
  300. ImgPicture.Visibility = Visibility.Collapsed;
  301. return;
  302. }
  303. HitTestResult result = VisualTreeHelper.HitTest(ListPageEdit, pos);
  304. if (result == null)
  305. {
  306. return;
  307. }
  308. var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  309. if (listBoxItem == null)
  310. {
  311. return;
  312. }
  313. isDragingEnter = false;
  314. tempItem = listBoxItem.DataContext as PageEditItem;
  315. var item_pos = e.GetPosition(listBoxItem);
  316. if (item_pos != null)
  317. {
  318. item_x = item_pos.X;
  319. item_y = item_pos.Y;
  320. }
  321. var scroll = GetScrollHost(ListPageEdit);
  322. string filename = Guid.NewGuid().ToString() + ".pdf";
  323. string tempPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), filename);
  324. System.IO.File.Create(tempPath);
  325. string[] files = new string[1];
  326. files[0] = tempPath;
  327. //禁用从窗体拖拽到桌面的方法
  328. //DataObject dataObj = new DataObject(DataFormats.FileDrop, files);
  329. DataObject dataObj = new DataObject(listBoxItem);
  330. DragDrop.DoDragDrop(ListPageEdit, dataObj, DragDropEffects.Copy);
  331. Mouse.Capture(ListPageEdit);
  332. return;
  333. }
  334. RectChoose.Visibility = Visibility.Collapsed;
  335. startChoose = false;
  336. Mouse.Capture(null);
  337. }
  338. catch
  339. {
  340. LineInset.Visibility = Visibility.Collapsed;
  341. ImgPicture.Visibility = Visibility.Collapsed;
  342. }
  343. }
  344. /// <summary>
  345. /// 判断是否开始框选、记录框选起始位置
  346. /// </summary>
  347. /// <param name="sender"></param>
  348. /// <param name="e"></param>
  349. private void ListPageEdit_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  350. {
  351. var pos = e.GetPosition(ListPageEdit);
  352. HitTestResult result = VisualTreeHelper.HitTest(ListPageEdit, pos);
  353. if (result == null)
  354. {
  355. return;
  356. }
  357. //未选中item 并且不是点击滑轨时 开始框选
  358. var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  359. var scroller = CommonHelper.FindVisualParent<ScrollBar>(result.VisualHit);
  360. if (listBoxItem == null)
  361. {
  362. if (scroller != null)
  363. {
  364. startChoose = false;
  365. return;
  366. }
  367. //点击空白处时开始框选
  368. startChoose = true;
  369. if (ListPageEdit.SelectedItems.Count > 0)
  370. {
  371. ListPageEdit.SelectedItems.Clear();
  372. }
  373. starPosition = e.GetPosition(ListPageEdit);
  374. starPosition = new Point(starPosition.X, starPosition.Y + GetWrapPanel(ListPageEdit).VerticalOffset);
  375. Mouse.Capture(ListPageEdit);
  376. return;
  377. }
  378. //选中了item 时,不能框选
  379. startChoose = false;
  380. //更改系统的选中规则,选中状态下,鼠标松开后取消选中
  381. //方便实现多选拖拽功能
  382. if (listBoxItem.IsSelected == true && !Keyboard.IsKeyDown(Key.LeftCtrl))
  383. {
  384. e.Handled = true;
  385. }
  386. }
  387. private void ListPageEdit_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  388. {
  389. var pos = e.GetPosition(ListPageEdit);
  390. HitTestResult result = VisualTreeHelper.HitTest(ListPageEdit, pos);
  391. if (result == null)
  392. {
  393. return;
  394. }
  395. var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  396. if (listBoxItem == null)
  397. {
  398. return;
  399. }
  400. //更改系统默认的选中规则,多选后,鼠标单击抬起后再选中单个
  401. //拖拽框选之后的抬起鼠标不进入处理
  402. if (!startChoose && !Keyboard.IsKeyDown(Key.LeftCtrl) && !Keyboard.IsKeyDown(Key.LeftShift))
  403. {
  404. ListPageEdit.SelectedItems.Clear();
  405. ListPageEdit.SelectedItem = listBoxItem;
  406. listBoxItem.IsSelected = true;
  407. return;
  408. }
  409. Mouse.Capture(null);
  410. //结束鼠标框选
  411. startChoose = false;
  412. RectChoose.Visibility = Visibility.Collapsed;
  413. }
  414. /// <summary>
  415. /// 获取Listobox的Wrappanel容器
  416. /// </summary>
  417. /// <param name="listBox"></param>
  418. /// <returns></returns>
  419. public VirtualizingWrapPanel GetWrapPanel(ListBox listBox)
  420. {
  421. Border border = VisualTreeHelper.GetChild(listBox, 0) as Border;
  422. var panel = CommonHelper.FindVisualChild<VirtualizingWrapPanel>(border);
  423. return panel;
  424. }
  425. /// <summary>
  426. ///根据鼠标拖选的框 选中矩形框里面的Item
  427. /// </summary>
  428. private void DoSelectItems()
  429. {
  430. var s = GetScrollHost(ListPageEdit);
  431. Point start = new Point();
  432. //通过 实时的垂直偏移量和第一次的偏移量抵消,来获取准确的垂直偏移值。
  433. start = new Point(starPosition.X, starPosition.Y - s.VerticalOffset);
  434. var rec = new Rect(start, Mouse.GetPosition(ListPageEdit));
  435. RectChoose.Margin = new Thickness(rec.Left, rec.Top, 0, 0);
  436. RectChoose.Width = rec.Width;
  437. RectChoose.Height = rec.Height;
  438. RectChoose.Visibility = Visibility.Visible;
  439. //检测遍历所有项,筛选在矩形框中的Item
  440. for (int i = 0; i < ListPageEdit.Items.Count; i++)
  441. {
  442. var _item = ListPageEdit.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
  443. //通过这一步来避免重复误选中
  444. var parent = CommonHelper.FindVisualParent<VirtualizingWrapPanel>(_item);
  445. if (parent == null)
  446. continue;
  447. var v = VisualTreeHelper.GetOffset(_item);
  448. if (rec.IntersectsWith(new Rect(v.X, v.Y, _item.ActualWidth, _item.ActualHeight)))
  449. {
  450. ListPageEdit.SelectedItems.Add(ListPageEdit.Items[i]);
  451. }
  452. else
  453. {
  454. ListPageEdit.SelectedItems.Remove(ListPageEdit.Items[i]);
  455. }
  456. }
  457. return;
  458. }
  459. /// <summary>
  460. /// 退出拖拽模式
  461. /// </summary>
  462. private void ExitDraging()
  463. {
  464. LineInset.Visibility = Visibility.Collapsed;
  465. ImgPicture.Visibility = Visibility.Collapsed;
  466. isDraging = false;
  467. }
  468. /// <summary>
  469. /// 拖拽释放后的处理逻辑
  470. /// </summary>
  471. /// <param name="sender"></param>
  472. /// <param name="e"></param>
  473. private async void ListPageEdit_Drop(object sender, DragEventArgs e)
  474. {
  475. needScroll = false;
  476. if (!isDraging)
  477. {
  478. //未拖拽时隐藏插入标记和虚影
  479. LineInset.Visibility = Visibility.Collapsed;
  480. ImgPicture.Visibility = Visibility.Collapsed;
  481. return;
  482. }
  483. //拖入非法格式释放时 返回
  484. string[] file = (string[])e.Data.GetData(DataFormats.FileDrop);
  485. if (file != null)
  486. {
  487. return;
  488. }
  489. #region 功能付费锁
  490. // //if (!App.IsActive())
  491. // //{
  492. // // MidLane.Visibility = Visibility.Collapsed;
  493. // // IAPFunctionDialog dialog = new IAPFunctionDialog("PageEdit");
  494. // // dialog.ShowDialog();
  495. // // return;
  496. // //}
  497. #endregion
  498. #region 从外部拖拽插入文件
  499. if (isDragingEnter)
  500. {
  501. //注释从外部拖拽插入文件的功能
  502. //var files = (string[])e.Data.GetData(DataFormats.FileDrop);
  503. //Array.Reverse(files);
  504. //foreach(string file in files)
  505. //{
  506. // System.IO.FileInfo info = new System.IO.FileInfo(file);
  507. // if(System.IO.Path.GetExtension(file).ToLower()==".pdf"&&info.Length>0)
  508. // {
  509. // int index = InsertIndex == -1 ? 0 : InsertIndex;
  510. // viewModel.InsertFromFile(index, file);
  511. // viewModel.ReloadAfterOption(true,true,new Tuple<int, int>(0,ListPageEdit.Items.Count));
  512. // }
  513. //}
  514. ////其他文件 则新增一个页签打开
  515. //// DragAddTab.Invoke(dragingEnterPath, new RoutedEventArgs());//底层库需要加一个 Load(TPDFDocument)的接口
  516. //LineInset.Visibility = Visibility.Collapsed;
  517. //ImgPicture.Visibility = Visibility.Collapsed;
  518. //isDragingEnter = false;
  519. return;
  520. }
  521. #endregion
  522. var pos = e.GetPosition(ListPageEdit);
  523. var result = VisualTreeHelper.HitTest(ListPageEdit, pos);
  524. //if (result == null)
  525. //{
  526. // //超出当前可控区域
  527. // ExitDraging();
  528. // return;
  529. //}
  530. ////查找元数据
  531. //var sourcePerson = e.Data.GetData(typeof(StackPanel)) as StackPanel;
  532. //if (sourcePerson == null)
  533. //{
  534. // ExitDraging();
  535. // return;
  536. //}
  537. //查找目标数据
  538. int targetindex = 0;//目标插入位置
  539. if (InsertIndex != -1)
  540. {
  541. //往前移动时 此index 不是准确的,需要处理++
  542. targetindex = InsertIndex;
  543. }
  544. //else//基本不会命中 仅作为保险措施
  545. //{
  546. // var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  547. // if (listBoxItem == null)
  548. // {
  549. // ////鼠标停留在两个item之间或其他无效区域 暂时不做处理(比较麻烦)
  550. // ExitDraging();
  551. // return;
  552. // }
  553. // var targetPerson = listBoxItem;
  554. // targetPerson.Opacity = 1;
  555. // sourcePerson.Opacity = 1;
  556. // if (ReferenceEquals(targetPerson, sourcePerson))
  557. // {
  558. // ExitDraging();
  559. // return;
  560. // }
  561. // targetindex = ListPageEdit.Items.IndexOf(targetPerson);
  562. //}
  563. List<ListBoxItem> list = new List<ListBoxItem>();
  564. List<int> sourceindex = new List<int>();//需要保存每个页面对应的位置
  565. //开始排序
  566. List<int> pages = new List<int>();
  567. //要先对所有选中项 根据页码排序
  568. for (int i = 0; i < ListPageEdit.SelectedItems.Count; i++)
  569. {
  570. var pageindex = ListPageEdit.Items.IndexOf(ListPageEdit.SelectedItems[i] as PageEditItem);
  571. pages.Add(pageindex);//存入的为页码索引值
  572. }
  573. if (pages.Count <= 0)
  574. {
  575. ExitDraging();
  576. return;
  577. }
  578. viewModel.DragToSort(targetindex,pages);
  579. ExitDraging();
  580. isDraging = false;
  581. }
  582. private void MidLane_Drop(object sender, DragEventArgs e)
  583. {
  584. ListPageEdit_Drop(sender, e);
  585. }
  586. private void ImgPicture_Drop(object sender, DragEventArgs e)
  587. {
  588. ListPageEdit_Drop(sender, e);
  589. }
  590. /// <summary>
  591. /// 拖拽事件写在外部的Grid里,会更加流畅
  592. /// </summary>
  593. /// <param name="sender"></param>
  594. /// <param name="e"></param>
  595. private void Grid_DragOver(object sender, DragEventArgs e)
  596. {
  597. try
  598. {
  599. //ctrl建按下 不显示插入标记和虚影 因为释放不会响应drop事件
  600. if (e.KeyStates == (DragDropKeyStates.ControlKey | DragDropKeyStates.LeftMouseButton) || e.KeyStates == (DragDropKeyStates.ShiftKey | DragDropKeyStates.LeftMouseButton | DragDropKeyStates.ControlKey))
  601. return;
  602. //从外部拖入文件时 返回
  603. string[] file = (string[])e.Data.GetData(DataFormats.FileDrop);
  604. if(file!=null)
  605. {
  606. return;
  607. }
  608. //滚动后有 位置不准确 要减去滚动偏移量
  609. //控制线的位置
  610. var pos = e.GetPosition(ListPageEdit);
  611. var result = VisualTreeHelper.HitTest(ListPageEdit, pos);
  612. if (result == null)
  613. {
  614. return;
  615. //MidLane.Visibility = Visibility.Collapsed;
  616. //return;
  617. }
  618. //获取当前鼠标指针下的容器
  619. var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  620. if (listBoxItem == null)
  621. {
  622. //MidLane.Visibility = Visibility.Collapsed;
  623. //return;
  624. }
  625. #region 计算虚影位置
  626. //xaml层 要设置 虚影控件为左上
  627. double xPos = 0;
  628. double yPos = 0;
  629. //内部拖动
  630. if (!isDragingEnter)
  631. {
  632. if(tempItem!=null)
  633. {
  634. ImgPicture.Width = tempItem.ItemSize.Width;
  635. ImgPicture.Height = tempItem.ItemSize.Height;
  636. ImgPicture.Source = tempItem.Image;
  637. xPos = e.GetPosition(ListPageEdit).X - item_x;
  638. yPos = e.GetPosition(ListPageEdit).Y - item_y;
  639. }
  640. }
  641. else
  642. {
  643. DragDropHelper.DragOver(this, e);
  644. //从外部拖入的逻辑
  645. //var pic = ToBitmapSource(dragingEnterPath);
  646. //ShadowPicture.Width = pic.Width;
  647. //ShadowPicture.Height = pic.Height;
  648. //ShadowPicture.Source = pic;
  649. //xPos = e.GetPosition(ListPageEdit).X - pic.Width / 2;
  650. //yPos = e.GetPosition(ListPageEdit).Y - pic.Height / 2;
  651. }
  652. ImgPicture.Margin = new Thickness(xPos, yPos, 0, 0);
  653. #endregion
  654. #region 计算插入标记位置
  655. var scroll = GetScrollHost(ListPageEdit);
  656. if (listBoxItem != null)
  657. {
  658. //虚拟化影响到该值计算
  659. var p = VisualTreeHelper.GetOffset(listBoxItem);//计算控件在容器中的偏移(位置)
  660. LineInset.Visibility = Visibility.Visible;
  661. if(!isDragingEnter)
  662. {
  663. ImgPicture.Visibility = Visibility.Visible;
  664. }
  665. else
  666. {
  667. ImgPicture.Visibility = Visibility.Collapsed;
  668. }
  669. var panel = GetWrapPanel(ListPageEdit);
  670. //var item = panel.ItemSize.Width;
  671. //var item = (ListPageEdit.ItemContainerGenerator.ContainerFromIndex(0) as ListBoxItem).DesiredSize.Width;
  672. var item = listBoxItem.DesiredSize.Width;
  673. int count = (int)(panel.ViewportWidth / item);
  674. var gap = (panel.ViewportWidth - count * item) / (count + 1) * 1.0;
  675. LineInset.X2 = LineInset.X1 = p.X + gap / 2 + listBoxItem.DesiredSize.Width;
  676. if (pos.X < p.X + gap / 2 + listBoxItem.ActualWidth / 2)
  677. {
  678. LineInset.X2 = LineInset.X1 = p.X - gap / 2;
  679. InsertIndex = ListPageEdit.Items.IndexOf(listBoxItem.DataContext as PageEditItem);
  680. }
  681. else
  682. {
  683. InsertIndex = ListPageEdit.Items.IndexOf(listBoxItem.DataContext as PageEditItem) + 1;
  684. }
  685. Console.WriteLine("InsertIndex:{0}\r\n",InsertIndex);
  686. //MidLane.Y1 = p.Y - scroll.VerticalOffset;//向下滑动后要减去滑动值
  687. LineInset.Y1 = p.Y;
  688. if (LineInset.Y1 < 0)//避免超出上边界
  689. {
  690. LineInset.Y1 = 0;
  691. }
  692. //MidLane.Y2 = p.Y + listBoxItem.ActualHeight - scroll.VerticalOffset;//仿智能滚动后可能会导致 垂直滚动偏量不准确
  693. LineInset.Y2 = p.Y + listBoxItem.ActualHeight;
  694. if (LineInset.Y2 < 0)
  695. {
  696. LineInset.Y2 = 0;
  697. }
  698. }
  699. #endregion
  700. //暂时处理 鼠标移出边框时,虚影的显示问题
  701. if (pos.Y <= 30 || pos.Y >= ListPageEdit.ActualHeight - 10)
  702. {
  703. LineInset.Visibility = Visibility.Collapsed;
  704. ImgPicture.Visibility = Visibility.Collapsed;
  705. needScroll = false;
  706. }
  707. if (pos.X <= 40 || pos.X >= scroll.ViewportWidth - 50)
  708. {
  709. LineInset.Visibility = Visibility.Collapsed;
  710. ImgPicture.Visibility = Visibility.Collapsed;
  711. needScroll = false;
  712. }
  713. #region 靠近上下边界时,自动滚动,离边界越近,滚动速度越快
  714. speed = 0;
  715. if (pos.Y >= ListPageEdit.ActualHeight - 30)
  716. {
  717. speed = 30 - (int)(ListPageEdit.ActualHeight - pos.Y);
  718. needScroll = true;
  719. }
  720. else if (pos.Y <= 30)
  721. {
  722. speed = (int)(pos.Y - 30);
  723. needScroll = true;
  724. }
  725. else
  726. needScroll = false;
  727. var v = scroll.VerticalOffset;
  728. scroll.ScrollToVerticalOffset(v + speed);//触发连续滚动
  729. #endregion
  730. }
  731. catch
  732. {
  733. }
  734. }
  735. private void ListBoxItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  736. {
  737. if(GridBOTAHeader.Visibility == Visibility.Visible&&viewModel.PDFViewer!=null)
  738. {
  739. if(Keyboard.Modifiers == ModifierKeys.Shift||Keyboard.Modifiers== ModifierKeys.Control)
  740. {
  741. return;
  742. }
  743. var item = (sender as ListBoxItem).DataContext as PageEditItem;
  744. if(item != null)
  745. {
  746. viewModel.PDFViewer.GoToPage(item.PageNumber-1);
  747. }
  748. }
  749. }
  750. private void ListBoxItem_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
  751. {
  752. if (e.ChangedButton == MouseButton.Left)
  753. {
  754. //双击回到PDFViewer界面
  755. var item = sender as ListBoxItem;
  756. if (item != null)
  757. {
  758. var index = ListPageEdit.ItemContainerGenerator.IndexFromContainer(item);
  759. //双击事件无法绑定Command 暂时采用这种方式调用VM里的方法
  760. viewModel.BackToPDFViewer(index);
  761. }
  762. }
  763. }
  764. private void PageEdit_SizeChanged(object sender, SizeChangedEventArgs e)
  765. {
  766. //BOTA缩略图模式下需要调整Item宽度
  767. if (GridBOTAHeader.Visibility == Visibility.Visible)
  768. {
  769. ItemSuitAcutalWidth(e.NewSize.Width);
  770. }
  771. }
  772. private void ItemSuitAcutalWidth(double width)
  773. {
  774. //缩略图模式下,保持一列或者两列显示
  775. if ((bool)TbnTwoLine.IsChecked)
  776. {
  777. var itemwidth = (width - 140) / 2;
  778. if(itemwidth<0)
  779. {
  780. itemwidth = width / 4;
  781. }
  782. var itemHeight = 294 * itemwidth / 208.0;
  783. viewModel.ChangeItemSize(new Size(itemwidth, itemHeight));
  784. }
  785. else
  786. {
  787. var itemwidth = 0.6 * width;
  788. var itemheight = 294 * itemwidth / 208.0;
  789. viewModel.ChangeItemSize(new Size(itemwidth, itemheight));
  790. }
  791. }
  792. private void TbnTwoLine_Click(object sender, RoutedEventArgs e)
  793. {
  794. ItemSuitAcutalWidth(this.ActualWidth);
  795. PulishEvent();
  796. }
  797. /// <summary>
  798. /// 用来判断是否有外界拖入的事件
  799. /// </summary>
  800. /// <param name="sender"></param>
  801. /// <param name="e"></param>
  802. private void Grid_PreviewDragEnter(object sender, DragEventArgs e)
  803. {
  804. var file = (System.Array)e.Data.GetData(DataFormats.FileDrop);
  805. if (file == null)//为null 表示内部拖动 触发的
  806. {
  807. return;
  808. }
  809. isDragingEnter = false;
  810. foreach (string f in file)
  811. {
  812. System.IO.FileInfo info = new System.IO.FileInfo(f);
  813. //只要拖拽进来的文件里包含有pdf格式文件,就允许拖入
  814. if (System.IO.Path.GetExtension(f).ToLower() == ".pdf" && info.Length > 0)
  815. {
  816. isDragingEnter = true;
  817. }
  818. }
  819. DragDropHelper.DragEnter(this, e);
  820. }
  821. private void Grid_DragLeave(object sender, DragEventArgs e)
  822. {
  823. DragDropHelper.DragLeave();
  824. }
  825. private void Grid_Drop(object sender, DragEventArgs e)
  826. {
  827. DragDropHelper.Drop(this,e);
  828. }
  829. private void ListBoxItem_DragLeave(object sender, DragEventArgs e)
  830. {
  831. //增加辅助判断,防止误触发 拖动排序
  832. //较大幅度拖动才能触发排序
  833. isDraging = true;
  834. }
  835. private void Border_Drop(object sender, DragEventArgs e)
  836. {
  837. }
  838. private async void TextBoxEx_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  839. {
  840. var text = sender as TextBox;
  841. if(text!=null)
  842. {
  843. await Task.Delay(50);
  844. text.Focus();
  845. }
  846. }
  847. private void TextBoxEx_TextChanged(object sender, TextChangedEventArgs e)
  848. {
  849. var text = sender as TextBox;
  850. if(text.Visibility==Visibility.Visible&&string.IsNullOrEmpty(text.Text))
  851. {
  852. text.Focus();
  853. }
  854. }
  855. private void TextBoxEx_LostFocus(object sender, RoutedEventArgs e)
  856. {
  857. //因为需要处理点击按钮或者点击列表失去焦点是 不进行弹窗 因此写在cs里,方便获取UI控件
  858. var element = FocusManager.GetFocusedElement(this);
  859. var visual = VisualTreeHelper.HitTest(this, Mouse.GetPosition(this));
  860. //点击其他区域失去焦点时 不响应页码判断逻辑
  861. if (visual != null)
  862. {
  863. var listbox = CommonHelper.FindVisualParent<ListBox>(visual.VisualHit);
  864. if (listbox != null)
  865. {
  866. //点击在listbox列表内
  867. return;
  868. }
  869. var image = CommonHelper.FindVisualParent<Image>(visual.VisualHit);
  870. if (image != null)
  871. {
  872. //点击在listbox列表内,具体某一项时,上面的判断调整可能失效,需要补充条件
  873. return;
  874. }
  875. var button =CommonHelper.FindVisualParent<Button>(visual.VisualHit);
  876. if(button!=null)
  877. {
  878. //点击在按钮时
  879. return;
  880. }
  881. var combox = CommonHelper.FindVisualParent<ComboBox>(visual.VisualHit);
  882. if(combox!=null)
  883. {
  884. //点击在下拉框时
  885. return;
  886. }
  887. viewModel.lostfocus(e);
  888. }
  889. }
  890. private void Grid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  891. {
  892. var visual = VisualTreeHelper.HitTest(this, Mouse.GetPosition(this));
  893. if (visual!=null&&(visual.VisualHit as Grid)!=null)
  894. {
  895. //点击工具栏空白处时 失去焦点
  896. ListPageEdit.Focus();
  897. }
  898. }
  899. private void MenuItem_Click(object sender, RoutedEventArgs e)
  900. {
  901. //因为需要获取准确的选中页面,因此把入口放在.cs代码里
  902. List<int> lists = new List<int>();
  903. foreach(PageEditItem item in ListPageEdit.SelectedItems)
  904. {
  905. lists.Add(item.PageNumber);
  906. }
  907. viewModel.print(lists);
  908. }
  909. }
  910. }