DashboardContainerPanel.cs 5.2 KB

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