StateInfo.xaml.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using NEIntelligentControl2.Models.Windturbine;
  2. using NEIntelligentControl2.Views.Basic;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. namespace NEIntelligentControl2.Views.Infos
  18. {
  19. /// <summary>
  20. /// 状态信息
  21. /// </summary>
  22. public partial class StateInfo : UserControl
  23. {
  24. public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(List<StatusTimeInfo>), typeof(StateInfo), new PropertyMetadata(ItemsSourceChanged));
  25. public static readonly DependencyProperty ItemsTitleProperty = DependencyProperty.Register("ItemsTitle", typeof(string), typeof(StateInfo), new PropertyMetadata(ItemsTitleChanged));
  26. /// <summary>
  27. /// 内容集合
  28. /// </summary>
  29. public List<StatusTimeInfo> ItemsSource { get => GetValue(ItemsSourceProperty) as List<StatusTimeInfo>; set => SetValue(ItemsSourceProperty, value); }
  30. public string ItemsTitle { get => GetValue(ItemsTitleProperty) as string; set => SetValue(ItemsTitleProperty, value); }
  31. /// <summary>
  32. /// 框选开始坐标
  33. /// </summary>
  34. private Point _StartPoint;
  35. /// <summary>
  36. /// 选择的时间
  37. /// </summary>
  38. private double _SelectedTime = 0;
  39. private bool _IsWidth;
  40. public StateInfo()
  41. {
  42. InitializeComponent();
  43. }
  44. private static void ItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  45. {
  46. var si = d as StateInfo;
  47. if (si == null) return;
  48. si.SetStateValue();
  49. }
  50. private static void ItemsTitleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  51. {
  52. var si = d as StateInfo;
  53. if (si == null) return;
  54. if (!string.IsNullOrWhiteSpace(e.NewValue as string))
  55. {
  56. si._tbtitle.Visibility = Visibility.Visible;
  57. }
  58. si._tbtitle.Text = e.NewValue as string;
  59. }
  60. /// <summary>
  61. /// 设置数值
  62. /// </summary>
  63. private void SetStateValue()
  64. {
  65. _SelectedTime = 0;
  66. _tb.Text = Math.Round(_SelectedTime, 2).ToString();
  67. _sp.Children.Clear();
  68. _IsWidth = this.ActualWidth == 0;
  69. double width = _IsWidth ? this.Width - 70 : _sp.ActualWidth;
  70. Console.WriteLine(width);
  71. var itemsSource = this.ItemsSource;
  72. if (itemsSource == null) return;
  73. double totalTime = itemsSource.Sum(i => i.Duration);
  74. foreach (var v in itemsSource)
  75. {
  76. StateInfoPoint sip = new StateInfoPoint(v) { TotalWidth = width, TotalTime = totalTime, SelectionChanged = ItemSelectionChanged };
  77. _sp.Children.Add(sip);
  78. }
  79. }
  80. private void ItemSelectionChanged(bool arg1, double arg2)
  81. {
  82. _SelectedTime = arg1 ? _SelectedTime + arg2 : _SelectedTime - arg2;
  83. _tb.Text = Math.Round(_SelectedTime, 2).ToString();
  84. }
  85. private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  86. {
  87. _StartPoint = e.GetPosition(sender as Grid);
  88. }
  89. private void Grid_MouseMove(object sender, MouseEventArgs e)
  90. {
  91. if (e.LeftButton != MouseButtonState.Pressed) return;
  92. var ct2 = sender as Grid;
  93. var ponit = e.GetPosition(ct2);
  94. if (_RTMain.Visibility != Visibility.Visible)
  95. {
  96. _RTMain.Visibility = Visibility.Visible;
  97. }
  98. Point p1 = new Point();
  99. p1.X = Math.Min(_StartPoint.X, ponit.X);
  100. p1.Y = Math.Min(_StartPoint.Y, ponit.Y);
  101. Point p2 = new Point();
  102. p2.X = Math.Max(_StartPoint.X, ponit.X);
  103. p2.Y = Math.Max(_StartPoint.Y, ponit.Y);
  104. _RTMain.Margin = new Thickness(p1.X, p1.Y, ct2.ActualWidth - p2.X, ct2.ActualHeight - p2.Y);
  105. if (_IsWidth)
  106. {
  107. p1.X -= 70;
  108. p2.X -= 70;
  109. }
  110. double d = 0;
  111. foreach (var v in _sp.Children)
  112. {
  113. var ct = v as StateInfoPoint;
  114. var left = ct.TranslatePoint(default(Point), _sp).X;
  115. var right = left + ct.ActualWidth;
  116. var b = right >= p1.X && left <= p2.X;
  117. if (Keyboard.Modifiers == ModifierKeys.Control)
  118. {
  119. if (b) ct.IsSelected = true;
  120. }
  121. else
  122. {
  123. ct.IsSelected = b;
  124. }
  125. if (ct.IsSelected)
  126. {
  127. d += ct.Time;
  128. }
  129. }
  130. _SelectedTime = d;
  131. _tb.Text = Math.Round(d, 2).ToString();
  132. }
  133. private void Grid_MouseLeave(object sender, MouseEventArgs e)
  134. {
  135. _RTMain.Visibility = Visibility.Hidden;
  136. }
  137. private void Grid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  138. {
  139. _RTMain.Visibility = Visibility.Hidden;
  140. }
  141. }
  142. }