LabelGroup.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System.Collections.Generic;
  2. namespace AIPractice.Shared.Labels
  3. {
  4. public enum GroupType
  5. {
  6. Image = 0,
  7. Roi =1,
  8. All = 2,
  9. }
  10. public abstract class LabelGroup : LabelObject
  11. {
  12. private RootLabel _root;
  13. /// <summary>
  14. /// Gets the group type.
  15. /// </summary>
  16. public GroupType Type { get; set; }
  17. /// <summary>
  18. /// Gets the labels belongs to this group.
  19. /// </summary>
  20. public IReadOnlyList<ImageLabel> Labels { get; }
  21. /// <summary>
  22. /// Gets or sets the root of this group
  23. /// </summary>
  24. public RootLabel Root
  25. {
  26. get => _root;
  27. set
  28. {
  29. if (_root != value)
  30. {
  31. _root = value;
  32. foreach(var label in Labels)
  33. {
  34. label.Root = _root;
  35. }
  36. }
  37. }
  38. }
  39. protected LabelGroup(string id, string title, GroupType type, IEnumerable<ImageLabel> labels) : base(id, title)
  40. {
  41. Type = type;
  42. var list = new List<ImageLabel>(labels);
  43. Labels = list;
  44. }
  45. /// <summary>
  46. /// Add one label into the group.
  47. /// </summary>
  48. /// <param name="label">The label to be added. and the root label will be assigned to the label.</param>
  49. public void AddLabel(ImageLabel label)
  50. {
  51. var labelList = (List<ImageLabel>)Labels;
  52. if(!labelList.Contains(label))
  53. {
  54. labelList.Add(label);
  55. label.Root = _root;
  56. }
  57. }
  58. /// <summary>
  59. /// Remove the lable from the group.
  60. /// </summary>
  61. /// <param name="label">The label to be removed.</param>
  62. public void RemoveLabel(ImageLabel label)
  63. {
  64. var labelList = (List<ImageLabel>)Labels;
  65. if(labelList.Contains(label))
  66. {
  67. labelList.Remove(label);
  68. }
  69. }
  70. /// <summary>
  71. /// Insert one label into the group on given index.
  72. /// </summary>
  73. /// <param name="index">The index to be insert.</param>
  74. /// <param name="label">The label to be added. and the root label will be assigned to the label.</param>
  75. public void InsertLabel(int index, ImageLabel label)
  76. {
  77. var labelList = (List<ImageLabel>)Labels;
  78. if (!labelList.Contains(label))
  79. {
  80. labelList.Insert(index,label);
  81. label.Root = _root;
  82. }
  83. }
  84. }
  85. }