DashboardContainerPanel.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using AvalonDock.Controls;
  2. using FastReport.Export.Dxf.Sections;
  3. using InABox.Configuration;
  4. using InABox.Core;
  5. using InABox.Wpf;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Media;
  15. namespace PRSDesktop;
  16. /// <summary>
  17. /// An <see cref="IPanel{T}"/> which wraps a <typeparamref name="TDashboard"/>, with a header. <typeparamref name="TDashboard"/>
  18. /// must be an <see cref="IBasePanel"/>, so that it can act as a normal PRS panel.
  19. /// </summary>
  20. /// <remarks>
  21. /// <see cref="IActionsDashboard"/> has no effect here, and instead, since <typeparamref name="TDashboard"/> must implement <see cref="IBasePanel"/>,
  22. /// one should use <see cref="IBasePanel.CreateToolbarButtons(PRSDesktop.IPanelHost)"/> to create actions.
  23. /// </remarks>
  24. /// <typeparam name="TDashboard"></typeparam>
  25. /// <typeparam name="TGroup"></typeparam>
  26. /// <typeparam name="TProperties"></typeparam>
  27. public class DashboardContainerPanel<TDashboard, TGroup, TProperties> : UserControl, IBasePanel
  28. where TDashboard : IDashboardWidget<TGroup, TProperties>, IBasePanel, new()
  29. where TGroup : DashboardWidgetGroup
  30. where TProperties : IUserConfigurationSettings, IDashboardProperties, new()
  31. {
  32. public TDashboard Dashboard { get; set; }
  33. private StackPanel HeaderItemsLeft;
  34. private StackPanel HeaderItemsRight;
  35. public DashboardContainerPanel()
  36. {
  37. Dashboard = new TDashboard
  38. {
  39. Properties = new UserConfiguration<TProperties>().Load()
  40. };
  41. var border = new Border
  42. {
  43. BorderBrush = new SolidColorBrush(Colors.Gray),
  44. BorderThickness = new Thickness(0)
  45. };
  46. var grid = new Grid();
  47. grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  48. grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
  49. var headerBorder = new Border
  50. {
  51. BorderBrush = new SolidColorBrush(Colors.Gray),
  52. BorderThickness = new Thickness(0.75),
  53. CornerRadius = new CornerRadius(5, 5, 0, 0),
  54. Background = new SolidColorBrush(Colors.WhiteSmoke)
  55. };
  56. var dock = new DockPanel
  57. {
  58. Background = new SolidColorBrush(Colors.Transparent)
  59. };
  60. HeaderItemsRight = new StackPanel
  61. {
  62. Margin = new Thickness(5),
  63. Orientation = Orientation.Horizontal
  64. };
  65. HeaderItemsLeft = new StackPanel
  66. {
  67. Margin = new Thickness(5),
  68. Orientation = Orientation.Horizontal
  69. };
  70. DockPanel.SetDock(HeaderItemsRight, Dock.Right);
  71. dock.Children.Add(HeaderItemsRight);
  72. DockPanel.SetDock(HeaderItemsLeft, Dock.Left);
  73. dock.Children.Add(HeaderItemsLeft);
  74. headerBorder.Child = dock;
  75. var content = new ContentControl
  76. {
  77. Margin = new Thickness(0, 2, 0, 0),
  78. Content = Dashboard
  79. };
  80. Grid.SetRow(headerBorder, 0);
  81. Grid.SetRow(content, 1);
  82. grid.Children.Add(headerBorder);
  83. grid.Children.Add(content);
  84. border.Child = grid;
  85. Content = border;
  86. if (Dashboard is IHeaderDashboard headerDashboard)
  87. {
  88. RefreshHeader(headerDashboard.Header);
  89. headerDashboard.Header.HeaderChanged += Header_HeaderChanged;
  90. }
  91. }
  92. private void RefreshHeader(DashboardHeader header)
  93. {
  94. if (HeaderItemsLeft.Children.Count > 0)
  95. {
  96. HeaderItemsLeft.Children.Clear();
  97. }
  98. if (HeaderItemsRight.Children.Count > 0)
  99. {
  100. HeaderItemsRight.Children.Clear();
  101. }
  102. foreach (var item in header.GetLeftElements())
  103. {
  104. HeaderItemsLeft.Children.Add(item);
  105. }
  106. foreach (var item in header.GetRightElements())
  107. {
  108. HeaderItemsRight.Children.Add(item);
  109. }
  110. }
  111. private void Header_HeaderChanged(DashboardHeader header)
  112. {
  113. RefreshHeader(header);
  114. }
  115. #region IBasePanel
  116. public bool IsReady { get => Dashboard.IsReady; set => Dashboard.IsReady = true; }
  117. public string SectionName => Dashboard.SectionName;
  118. public event DataModelUpdateEvent? OnUpdateDataModel
  119. {
  120. add => Dashboard.OnUpdateDataModel += value;
  121. remove => Dashboard.OnUpdateDataModel -= value;
  122. }
  123. public void CreateToolbarButtons(IPanelHost host)
  124. {
  125. Dashboard.CreateToolbarButtons(host);
  126. }
  127. public DataModel DataModel(Selection selection)
  128. {
  129. return Dashboard.DataModel(selection);
  130. }
  131. public void Heartbeat(TimeSpan time)
  132. {
  133. Dashboard.Heartbeat(time);
  134. }
  135. public void Refresh()
  136. {
  137. Dashboard.Refresh();
  138. }
  139. public Dictionary<string, object[]> Selected()
  140. {
  141. return Dashboard.Selected();
  142. }
  143. public void Setup()
  144. {
  145. Dashboard.Setup();
  146. }
  147. public void Shutdown(CancelEventArgs? cancel)
  148. {
  149. Dashboard.Shutdown(cancel);
  150. if(cancel?.Cancel != true)
  151. {
  152. new UserConfiguration<TProperties>().Save(Dashboard.Properties);
  153. }
  154. }
  155. #endregion
  156. }