FactorySectionsGrid.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Windows.Media.Imaging;
  6. using Comal.Classes;
  7. using InABox.Core;
  8. using InABox.DynamicGrid;
  9. using InABox.WPF;
  10. namespace PRSDesktop
  11. {
  12. internal class FactorySectionsGrid : DynamicGrid<FactorySection>
  13. {
  14. private string _CurrentSection = "";
  15. private readonly List<FactorySection> CurrentSections = new();
  16. public BitmapImage QAColor = PRSDesktop.Resources.quality.AsBitmapImage(Color.White);
  17. public BitmapImage QAGray = PRSDesktop.Resources.quality.AsGrayScale().AsBitmapImage(Color.White);
  18. public FactorySectionsGrid()
  19. {
  20. Options.AddRange(DynamicGridOption.RecordCount);
  21. ActionColumns.Add(new DynamicImageColumn(QualityImage, QualityAction));
  22. HiddenColumns.Add(x => x.QualityChecks);
  23. ActionColumns.Add(new DynamicRowMovementColumn(DynamicRowMovement.Up, SwapRows));
  24. ActionColumns.Add(new DynamicRowMovementColumn(DynamicRowMovement.Down, SwapRows));
  25. }
  26. public string CurrentGroup
  27. {
  28. get => _CurrentSection;
  29. set
  30. {
  31. if (!string.IsNullOrEmpty(_CurrentSection))
  32. {
  33. Sections.RemoveAll(x => x.Group.Equals(_CurrentSection));
  34. Sections.AddRange(CurrentSections);
  35. }
  36. _CurrentSection = value;
  37. CurrentSections.Clear();
  38. if (!string.IsNullOrEmpty(_CurrentSection))
  39. CurrentSections.AddRange(Sections.Where(x => x.Group.Equals(_CurrentSection)));
  40. }
  41. }
  42. public List<FactorySection> Sections { get; set; }
  43. private bool QualityAction(CoreRow arg)
  44. {
  45. if (arg != null)
  46. {
  47. var form = new NotesForm
  48. {
  49. Caption = "Please enter the Quality Checks to be performed before starting work on this stage",
  50. Text = CurrentSections[arg.Index].QualityChecks
  51. };
  52. if (form.ShowDialog() == true)
  53. {
  54. CurrentSections[arg.Index].QualityChecks = form.Text;
  55. return true;
  56. }
  57. }
  58. return false;
  59. }
  60. private BitmapImage QualityImage(CoreRow arg)
  61. {
  62. if (arg == null)
  63. return QAColor;
  64. if (string.IsNullOrWhiteSpace(arg.Get<FactorySection, string>(x => x.QualityChecks)))
  65. return QAGray;
  66. return QAColor;
  67. }
  68. private bool SwapRows(int arg1, int arg2)
  69. {
  70. var item = CurrentSections[arg1];
  71. CurrentSections.Remove(item);
  72. CurrentSections.Insert(arg2, item);
  73. return true;
  74. }
  75. //public DynamicGridColumns DefineColumns()
  76. //{
  77. // return LoadColumns();
  78. //}
  79. protected override DynamicGridColumns LoadColumns()
  80. {
  81. var columns = new DynamicGridColumns
  82. {
  83. new() { ColumnName = "Name", /* Type = typeof(String), */ Width = 0 },
  84. new() { ColumnName = "Stations", Caption = "Stns", Width = 40, Alignment = Alignment.MiddleCenter },
  85. new() { ColumnName = "Hidden", Caption = "Hide", Width = 30 }
  86. //new DynamicGridColumn() { ColumnName = "Shared", Width = 30},
  87. };
  88. return columns;
  89. }
  90. #region Save / Load
  91. //protected override DataTable Reload(Dictionary<String, Object> criteria, List<String> columnnames, String sort)
  92. //{
  93. // DataTable result = new DataTable();
  94. // result.LoadColumns(typeof(FactorySection));
  95. // result.LoadRows(CurrentSections);
  96. // return result;
  97. //}
  98. protected override void Reload(Filters<FactorySection> criteria, Columns<FactorySection> columns, ref SortOrder<FactorySection> sort,
  99. Action<CoreTable, Exception> action)
  100. {
  101. var result = new CoreTable();
  102. result.LoadColumns(typeof(FactorySection));
  103. result.LoadRows(CurrentSections);
  104. action.Invoke(result, null);
  105. }
  106. protected override FactorySection LoadItem(CoreRow row)
  107. {
  108. var index = Data.Rows.IndexOf(row);
  109. return CurrentSections[index];
  110. }
  111. public override void SaveItem(FactorySection item)
  112. {
  113. if (!CurrentSections.Contains(item))
  114. CurrentSections.Add(item);
  115. }
  116. protected override void DeleteItems(params CoreRow[] rows)
  117. {
  118. foreach (var row in rows.OrderByDescending(x => x.Index))
  119. CurrentSections.RemoveAt(row.Index);
  120. }
  121. protected override FactorySection CreateItem()
  122. {
  123. var section = base.CreateItem();
  124. section.Group = CurrentGroup;
  125. section.ID = Guid.NewGuid();
  126. return section;
  127. }
  128. #endregion
  129. }
  130. }