ViewModels.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. using AIPractice.LabelEditor.Annotations;
  2. using AIPractice.Shared.Labels;
  3. using System.Collections.ObjectModel;
  4. using System.Collections.Specialized;
  5. using System.ComponentModel;
  6. using System.Linq;
  7. using System.Runtime.CompilerServices;
  8. namespace AIPractice.LabelEditor
  9. {
  10. public class ViewModel : INotifyPropertyChanged
  11. {
  12. public event PropertyChangedEventHandler PropertyChanged;
  13. [NotifyPropertyChangedInvocator]
  14. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  15. {
  16. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  17. }
  18. }
  19. public class LabelObjectViewModel : ViewModel
  20. {
  21. public string Id { get; }
  22. public virtual string Title { get; set; }
  23. public LabelObjectViewModel(string id, string title)
  24. {
  25. Id = id;
  26. Title = title;
  27. }
  28. public override string ToString()
  29. {
  30. return Title;
  31. }
  32. }
  33. public class ImageLabelViewModel : LabelObjectViewModel
  34. {
  35. private ImageLabelViewModel _linked;
  36. public RootLabelViewModel Root { get; set; }
  37. public ImageLabelViewModel Linked
  38. {
  39. get => _linked;
  40. set
  41. {
  42. if(_linked != value)
  43. {
  44. _linked = value;
  45. OnPropertyChanged();
  46. }
  47. }
  48. }
  49. public ImageLabelViewModel(string id, string title)
  50. : base(id, title)
  51. {
  52. }
  53. }
  54. public class DescriptionLabelViewModel : ImageLabelViewModel
  55. {
  56. public override string Title
  57. {
  58. get { return Description.Title; }
  59. set
  60. {
  61. if (Description != null)
  62. {
  63. Description.Title = value;
  64. }
  65. }
  66. }
  67. public DescriptionLabel Description { get; }
  68. public DescriptionLabelViewModel(DescriptionLabel description)
  69. : base(description.Id, description.Title)
  70. {
  71. Description = description;
  72. }
  73. }
  74. public class ConclusionLabelViewModel : ImageLabelViewModel
  75. {
  76. public override string Title
  77. {
  78. get => Conclusion.Title;
  79. set
  80. {
  81. if (Conclusion != null)
  82. {
  83. Conclusion.Title = value;
  84. }
  85. }
  86. }
  87. public bool IsParts
  88. {
  89. get => Conclusion.IsParts;
  90. set
  91. {
  92. if (Conclusion.IsParts != value)
  93. {
  94. Conclusion.IsParts = value;
  95. OnPropertyChanged();
  96. }
  97. }
  98. }
  99. public bool IsUnique
  100. {
  101. get => Conclusion.IsUnique;
  102. set
  103. {
  104. if (Conclusion.IsUnique != value)
  105. {
  106. Conclusion.IsUnique = value;
  107. OnPropertyChanged();
  108. }
  109. }
  110. }
  111. public ConclusionLabel Conclusion { get; }
  112. public int Level
  113. {
  114. get => (int)Conclusion.Level;
  115. set
  116. {
  117. if (Conclusion != null)
  118. {
  119. Conclusion.Level = (MalignantLevel) value;
  120. }
  121. OnPropertyChanged();
  122. }
  123. }
  124. public ConclusionLabelViewModel(ConclusionLabel conclusion)
  125. : base(conclusion.Id, conclusion.Title)
  126. {
  127. Conclusion = conclusion;
  128. }
  129. }
  130. public class LabelGroupViewModel : LabelObjectViewModel
  131. {
  132. private RootLabelViewModel _root;
  133. public RootLabelViewModel Root
  134. {
  135. get => _root;
  136. set
  137. {
  138. if (_root != value)
  139. {
  140. _root = value;
  141. foreach(var label in Labels)
  142. {
  143. label.Root = _root;
  144. }
  145. }
  146. }
  147. }
  148. public LabelGroup Group { get; }
  149. public override string Title
  150. {
  151. get => Group.Title;
  152. set
  153. {
  154. if (Group != null)
  155. {
  156. Group.Title = value;
  157. }
  158. }
  159. }
  160. public int Type
  161. {
  162. get { return (int)Group.Type; }
  163. set { Group.Type = (GroupType)value; }
  164. }
  165. public ObservableCollection<ImageLabelViewModel> Labels { get; protected set; }
  166. public LabelGroupViewModel(LabelGroup group)
  167. : base(group.Id, group.Title)
  168. {
  169. Group = group;
  170. Labels = new ObservableCollection<ImageLabelViewModel>(from x in Group.Labels
  171. select (x is DescriptionLabel)
  172. ? ((ImageLabelViewModel)new DescriptionLabelViewModel((DescriptionLabel)x))
  173. : new ConclusionLabelViewModel((ConclusionLabel)x));
  174. Labels.CollectionChanged += OnLabelsCollectionChanged;
  175. }
  176. private void OnLabelsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
  177. {
  178. if (e.OldItems != null)
  179. {
  180. foreach (var item in e.OldItems)
  181. {
  182. if (item is ImageLabelViewModel label)
  183. {
  184. var exists = Group.Labels.FirstOrDefault(x => x.Id == label.Id);
  185. if (exists != null)
  186. {
  187. Group.RemoveLabel(exists);
  188. }
  189. }
  190. }
  191. }
  192. if (e.NewItems != null)
  193. {
  194. foreach (var item in e.NewItems)
  195. {
  196. if (item is ImageLabelViewModel label)
  197. {
  198. var exists = Group.Labels.FirstOrDefault(x => x.Id == label.Id);
  199. if (exists == null)
  200. {
  201. if (label is DescriptionLabelViewModel description)
  202. {
  203. Group.AddLabel(description.Description);
  204. }
  205. else if (label is ConclusionLabelViewModel conclusion)
  206. {
  207. Group.AddLabel(conclusion.Conclusion);
  208. }
  209. }
  210. }
  211. }
  212. }
  213. }
  214. }
  215. public class MultiSelectionLabelGroupViewModel : LabelGroupViewModel
  216. {
  217. public MultiSelectionLabelGroupViewModel(MultiSelectionLabelGroup group)
  218. : base(group)
  219. {
  220. }
  221. }
  222. public class SingleSelectionLabelGroupViewModel : LabelGroupViewModel
  223. {
  224. public SingleSelectionLabelGroupViewModel(SingleSelectionLabelGroup group)
  225. : base(group)
  226. {
  227. }
  228. }
  229. public class RootLabelViewModel : LabelObjectViewModel
  230. {
  231. public override string Title
  232. {
  233. get => RootLabel.Title;
  234. set
  235. {
  236. if (RootLabel != null)
  237. {
  238. RootLabel.Title = value;
  239. }
  240. }
  241. }
  242. public bool NeedPartsConclusion
  243. {
  244. get => RootLabel.NeedPartsConclusion;
  245. set
  246. {
  247. if (RootLabel.NeedPartsConclusion != value)
  248. {
  249. RootLabel.NeedPartsConclusion = value;
  250. OnPropertyChanged();
  251. }
  252. }
  253. }
  254. public RootLabel RootLabel { get; }
  255. public ObservableCollection<LabelGroupViewModel> Descriptions { get; }
  256. public ObservableCollection<LabelGroupViewModel> Conclusions { get; }
  257. public RootLabelViewModel(RootLabel rootLabel)
  258. : base(rootLabel.Id, rootLabel.Title)
  259. {
  260. RootLabel = rootLabel;
  261. Descriptions = new ObservableCollection<LabelGroupViewModel>(from x in RootLabel.Descriptions
  262. select (x is MultiSelectionLabelGroup)
  263. ? new MultiSelectionLabelGroupViewModel((MultiSelectionLabelGroup)x) { Root = this }
  264. : ((LabelGroupViewModel)new SingleSelectionLabelGroupViewModel((SingleSelectionLabelGroup)x) { Root = this }));
  265. Conclusions = new ObservableCollection<LabelGroupViewModel>(from x in RootLabel.Conclusions
  266. select (x is MultiSelectionLabelGroup)
  267. ? new MultiSelectionLabelGroupViewModel((MultiSelectionLabelGroup)x) { Root = this }
  268. : ((LabelGroupViewModel)new SingleSelectionLabelGroupViewModel((SingleSelectionLabelGroup)x) { Root = this }));
  269. Descriptions.CollectionChanged += OnDescriptionsCollectionChanged;
  270. Conclusions.CollectionChanged += OnConclusionsCollectionChanged;
  271. foreach(var descGroup in Descriptions)
  272. {
  273. foreach(var descLabel in descGroup.Labels)
  274. {
  275. var association = rootLabel.LabelAssociations.FirstOrDefault(x => x.Left.Id == descLabel.Id);
  276. if(association != null)
  277. {
  278. var conclusionId = association.Right.Id;
  279. foreach (var conGroup in Conclusions)
  280. {
  281. foreach (var conLabel in conGroup.Labels)
  282. {
  283. if(conLabel.Id == conclusionId)
  284. {
  285. descLabel.Linked = conLabel;
  286. }
  287. }
  288. }
  289. }
  290. }
  291. }
  292. }
  293. private void OnConclusionsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
  294. {
  295. if (e.OldItems != null)
  296. {
  297. foreach (var item in e.OldItems)
  298. {
  299. if (item is LabelGroupViewModel group)
  300. {
  301. var exists = RootLabel.Conclusions.FirstOrDefault(x => x.Id == group.Id);
  302. if (exists != null)
  303. {
  304. RootLabel.RemoveConclusionGroup(exists);
  305. }
  306. }
  307. }
  308. }
  309. if (e.NewItems != null)
  310. {
  311. var index = e.NewStartingIndex;
  312. foreach (var item in e.NewItems)
  313. {
  314. if (item is LabelGroupViewModel group)
  315. {
  316. var exists = RootLabel.Conclusions.FirstOrDefault(x => x.Id == group.Id);
  317. if (exists == null)
  318. {
  319. RootLabel.InsertConclusionGroup(index, group.Group);
  320. }
  321. }
  322. index++;
  323. }
  324. }
  325. }
  326. private void OnDescriptionsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
  327. {
  328. if (e.OldItems != null)
  329. {
  330. foreach (var item in e.OldItems)
  331. {
  332. if (item is LabelGroupViewModel group)
  333. {
  334. var exists = RootLabel.Descriptions.FirstOrDefault(x => x.Id == group.Id);
  335. if (exists != null)
  336. {
  337. RootLabel.RemoveDescriptionGroup(exists);
  338. }
  339. }
  340. }
  341. }
  342. if (e.NewItems != null)
  343. {
  344. var index = e.NewStartingIndex;
  345. foreach (var item in e.NewItems)
  346. {
  347. if (item is LabelGroupViewModel group)
  348. {
  349. var exists = RootLabel.Descriptions.FirstOrDefault(x => x.Id == group.Id);
  350. if (exists == null)
  351. {
  352. RootLabel.InsertDescriptionGroup(index,group.Group);
  353. }
  354. }
  355. index++;
  356. }
  357. }
  358. }
  359. }
  360. public class LabelContainerViewModel : ViewModel
  361. {
  362. private RootLabelViewModel _selectedRootLabel;
  363. public LabelContainer Container { get; }
  364. public RootLabelViewModel SelectedRootLabel
  365. {
  366. get => _selectedRootLabel;
  367. set
  368. {
  369. if (_selectedRootLabel != value)
  370. {
  371. _selectedRootLabel = value;
  372. OnPropertyChanged();
  373. }
  374. }
  375. }
  376. public ObservableCollection<RootLabelViewModel> Labels { get; }
  377. public LabelContainerViewModel(LabelContainer container)
  378. {
  379. Container = container;
  380. Labels = new ObservableCollection<RootLabelViewModel>(from x in container.Labels
  381. select new RootLabelViewModel(x));
  382. Labels.CollectionChanged += OnLabelsCollectionChanged;
  383. }
  384. private void OnLabelsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
  385. {
  386. if (e.OldItems != null)
  387. {
  388. foreach (var item in e.OldItems)
  389. {
  390. if (item is RootLabelViewModel label)
  391. {
  392. var exists = Container.Labels.FirstOrDefault(x => x.Id == label.Id);
  393. if (exists != null)
  394. {
  395. Container.Labels.Remove(exists);
  396. }
  397. }
  398. }
  399. }
  400. if (e.NewItems != null)
  401. {
  402. foreach (var item in e.NewItems)
  403. {
  404. if (item is RootLabelViewModel label)
  405. {
  406. var exists = Container.Labels.FirstOrDefault(x => x.Id == label.Id);
  407. if (exists == null)
  408. {
  409. Container.Labels.Add(label.RootLabel);
  410. }
  411. }
  412. }
  413. }
  414. }
  415. }
  416. }