RecipeSelectDialogViewModel.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Controls.Primitives;
  8. using System.Windows.Input;
  9. using System.Windows.Interactivity;
  10. using System.Windows.Markup.Localizer;
  11. using Aitex.Core.UI.Control;
  12. using Aitex.Core.UI.MVVM;
  13. using DocumentFormat.OpenXml.Drawing.Charts;
  14. using DocumentFormat.OpenXml.EMMA;
  15. using MECF.Framework.UI.Client.CenterViews.Editors.Sequence;
  16. using OpenSEMI.ClientBase;
  17. using SciChart.Charting.Common.Extensions;
  18. using SciChart.Core.Extensions;
  19. namespace MECF.Framework.UI.Client.CenterViews.Editors.Recipe
  20. {
  21. public class RecipeSelectDialogViewModel : DialogViewModel<string>
  22. {
  23. public ObservableCollection<ProcessTypeFileItem> ProcessTypeFileList { get; set; }
  24. public FileNode CurrentFileNode { get; set; }
  25. public int ProcessTypeIndexSelection { get; set; }
  26. public ObservableCollection<FileNode> Files { get; set; }
  27. private FileNode currentFileNode;
  28. private double viewportWidth = 276;//judge TreeviewItem display Region
  29. private double lastLocation = 0;
  30. public void LinePageUp(MouseEventArgs e)
  31. {
  32. IEnumerable<FileNode> source = ProcessTypeFileList.First()?.FileListByProcessType;
  33. if (source==null|| currentFileNode == null) return;
  34. if (!(e.Source is ScrollViewer)) return;
  35. var scrollViewer = e.Source as ScrollViewer;
  36. viewportWidth = scrollViewer.ViewportWidth;
  37. if (e.GetPosition(scrollViewer).X < viewportWidth) {return; }
  38. var originalSource = e.OriginalSource as FrameworkElement;
  39. var repeatButton = originalSource.FindVisualParent<RepeatButton>();
  40. var scrollBar = repeatButton.FindVisualParent<ScrollBar>();
  41. // Handle only the case when the horizontal scrollbar is clicked
  42. if (repeatButton != null && scrollBar != null && scrollBar.Name == "PART_VerticalScrollBar")
  43. {
  44. double count = Math.Ceiling(scrollViewer.VerticalOffset / 24.0);
  45. switch (repeatButton.Name)
  46. {
  47. case "PageUp":
  48. if (source.First().IsSelected == true) return;
  49. if (lastLocation == count) source.First().IsSelected = true;
  50. else PageUPDown((int)count);
  51. break;
  52. case "PageDown":
  53. if (source.Last().IsSelected == true) return;
  54. if (lastLocation == count) source.Last().IsSelected = true;
  55. else PageUPDown((int)count);
  56. break;
  57. case "LineUp":
  58. if (source.First().IsSelected == true) return;
  59. if (currentFileNode.IsFile)
  60. {
  61. FileNode parentNode = currentFileNode.Parent;
  62. for (int i = 0; i < parentNode.Files.Count; i++)
  63. {
  64. if (parentNode.Files[i] == currentFileNode)
  65. {
  66. if (i == 0) parentNode.IsSelected = true;
  67. else
  68. currentFileNode.Parent.Files[i - 1].IsSelected = true;
  69. break;
  70. }
  71. }
  72. }
  73. else
  74. {
  75. if (currentFileNode.Parent != null)
  76. {
  77. FileNode parentNode = currentFileNode.Parent;
  78. for (int i = 0; i < parentNode.Files.Count; i++)
  79. {
  80. if (parentNode.Files[i] == currentFileNode)
  81. {
  82. if (i !=0)
  83. {
  84. FileNode node = parentNode.Files[i - 1];
  85. if (node.Files.Count > 0&& node.IsExpanded)
  86. {
  87. node.Files[node.Files.Count-1].IsSelected = true;
  88. }
  89. else node.IsSelected = true;
  90. }
  91. break;
  92. }
  93. }
  94. }
  95. }
  96. break;
  97. case "LineDown":
  98. if (source.Last().IsSelected == true) return;
  99. if (currentFileNode.IsFile)
  100. {
  101. FileNode parentNode = currentFileNode.Parent;
  102. for (int i = 0; i < parentNode.Files.Count; i++)
  103. {
  104. if (parentNode.Files[i] == currentFileNode)
  105. {
  106. if (i == parentNode.Files.Count - 1)
  107. {
  108. if (parentNode.Parent != null)
  109. {
  110. for (global::System.Int32 j = 0; j < parentNode.Parent.Files.Count; j++)
  111. {
  112. if (parentNode.Parent.Files[j] == parentNode)
  113. {
  114. if (j != parentNode.Parent.Files.Count - 1)
  115. parentNode.Parent.Files[j + 1].IsSelected = true;
  116. break;
  117. }
  118. }
  119. }
  120. }
  121. else
  122. {
  123. currentFileNode.Parent.Files[i + 1].IsSelected = true;
  124. break;
  125. }
  126. }
  127. }
  128. }
  129. else
  130. {
  131. if (currentFileNode.Files.Count > 0)
  132. {
  133. currentFileNode.IsExpanded = true;
  134. currentFileNode.Files[0].IsSelected = true;
  135. }
  136. else
  137. {
  138. if (currentFileNode.Parent != null)
  139. {
  140. FileNode parentNode = currentFileNode.Parent;
  141. for (int i = 0; i < parentNode.Files.Count; i++)
  142. {
  143. if (parentNode.Files[i] == currentFileNode)
  144. {
  145. if (i != parentNode.Files.Count - 1)
  146. {
  147. currentFileNode.Parent.Files[i + 1].IsSelected = true;
  148. }
  149. break;
  150. }
  151. }
  152. }
  153. }
  154. }
  155. break;
  156. default:
  157. break;
  158. }
  159. lastLocation = count;
  160. }
  161. }
  162. private void PageUPDown(int count)
  163. {
  164. int sum = 0;
  165. IEnumerable<FileNode> roots= ProcessTypeFileList.First()?.FileListByProcessType;
  166. if (roots.Any())
  167. {
  168. foreach(FileNode fileNode in roots)
  169. {
  170. if (GetNode(fileNode, ref sum, count))
  171. break;
  172. }
  173. }
  174. }
  175. private bool GetNode(FileNode node,ref int sum,int count)
  176. {
  177. if (++sum >= count)
  178. {
  179. node.IsSelected = true;
  180. return true;
  181. }
  182. if (node.IsExpanded&&node.Files.Count>0)
  183. {
  184. foreach (FileNode fileNode in node.Files)
  185. {
  186. if (GetNode(fileNode, ref sum, count)) return true;
  187. }
  188. }
  189. return false;
  190. }
  191. public void TreeSelectChanged(FileNode file)
  192. {
  193. this.currentFileNode = file;
  194. }
  195. public void TreeMouseDoubleClick(MouseButtonEventArgs item)
  196. {
  197. item.Handled = true;
  198. if (item.Source is TreeView tree)
  199. {
  200. Point t = item.GetPosition(tree);
  201. if (t.X < viewportWidth)
  202. OK();
  203. }
  204. }
  205. public void OK()
  206. {
  207. if (this.currentFileNode != null)
  208. {
  209. if (this.currentFileNode.IsFile)
  210. {
  211. //this.DialogResult = currentFileNode.PrefixPath + "\\" + currentFileNode.FullPath;
  212. this.DialogResult = currentFileNode.FullPath;
  213. IsCancel = false;
  214. TryClose(true);
  215. }
  216. }
  217. else {
  218. Cancel();
  219. }
  220. }
  221. public void Cancel()
  222. {
  223. IsCancel = true;
  224. TryClose(false);
  225. }
  226. }
  227. }