123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474 |
- using AIPractice.LabelEditor.Annotations;
- using AIPractice.Shared.Labels;
- using System.Collections.ObjectModel;
- using System.Collections.Specialized;
- using System.ComponentModel;
- using System.Linq;
- using System.Runtime.CompilerServices;
- namespace AIPractice.LabelEditor
- {
- public class ViewModel : INotifyPropertyChanged
- {
- public event PropertyChangedEventHandler PropertyChanged;
- [NotifyPropertyChangedInvocator]
- protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- public class LabelObjectViewModel : ViewModel
- {
- public string Id { get; }
- public virtual string Title { get; set; }
- public LabelObjectViewModel(string id, string title)
- {
- Id = id;
- Title = title;
- }
- public override string ToString()
- {
- return Title;
- }
- }
- public class ImageLabelViewModel : LabelObjectViewModel
- {
- private ImageLabelViewModel _linked;
- public RootLabelViewModel Root { get; set; }
- public ImageLabelViewModel Linked
- {
- get => _linked;
- set
- {
- if(_linked != value)
- {
- _linked = value;
- OnPropertyChanged();
- }
- }
- }
- public ImageLabelViewModel(string id, string title)
- : base(id, title)
- {
- }
- }
- public class DescriptionLabelViewModel : ImageLabelViewModel
- {
- public override string Title
- {
- get { return Description.Title; }
- set
- {
- if (Description != null)
- {
- Description.Title = value;
- }
- }
- }
- public DescriptionLabel Description { get; }
- public DescriptionLabelViewModel(DescriptionLabel description)
- : base(description.Id, description.Title)
- {
- Description = description;
- }
- }
- public class ConclusionLabelViewModel : ImageLabelViewModel
- {
- public override string Title
- {
- get => Conclusion.Title;
- set
- {
- if (Conclusion != null)
- {
- Conclusion.Title = value;
- }
- }
- }
- public bool IsParts
- {
- get => Conclusion.IsParts;
- set
- {
- if (Conclusion.IsParts != value)
- {
- Conclusion.IsParts = value;
- OnPropertyChanged();
- }
- }
- }
- public bool IsUnique
- {
- get => Conclusion.IsUnique;
- set
- {
- if (Conclusion.IsUnique != value)
- {
- Conclusion.IsUnique = value;
- OnPropertyChanged();
- }
- }
- }
- public ConclusionLabel Conclusion { get; }
- public int Level
- {
- get => (int)Conclusion.Level;
- set
- {
- if (Conclusion != null)
- {
- Conclusion.Level = (MalignantLevel) value;
- }
- OnPropertyChanged();
- }
- }
- public ConclusionLabelViewModel(ConclusionLabel conclusion)
- : base(conclusion.Id, conclusion.Title)
- {
- Conclusion = conclusion;
- }
- }
- public class LabelGroupViewModel : LabelObjectViewModel
- {
- private RootLabelViewModel _root;
- public RootLabelViewModel Root
- {
- get => _root;
- set
- {
- if (_root != value)
- {
- _root = value;
- foreach(var label in Labels)
- {
- label.Root = _root;
- }
- }
- }
- }
- public LabelGroup Group { get; }
- public override string Title
- {
- get => Group.Title;
- set
- {
- if (Group != null)
- {
- Group.Title = value;
- }
- }
- }
- public int Type
- {
- get { return (int)Group.Type; }
- set { Group.Type = (GroupType)value; }
- }
- public ObservableCollection<ImageLabelViewModel> Labels { get; protected set; }
- public LabelGroupViewModel(LabelGroup group)
- : base(group.Id, group.Title)
- {
- Group = group;
- Labels = new ObservableCollection<ImageLabelViewModel>(from x in Group.Labels
- select (x is DescriptionLabel)
- ? ((ImageLabelViewModel)new DescriptionLabelViewModel((DescriptionLabel)x))
- : new ConclusionLabelViewModel((ConclusionLabel)x));
- Labels.CollectionChanged += OnLabelsCollectionChanged;
- }
- private void OnLabelsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
- {
- if (e.OldItems != null)
- {
- foreach (var item in e.OldItems)
- {
- if (item is ImageLabelViewModel label)
- {
- var exists = Group.Labels.FirstOrDefault(x => x.Id == label.Id);
- if (exists != null)
- {
- Group.RemoveLabel(exists);
- }
- }
- }
- }
- if (e.NewItems != null)
- {
- foreach (var item in e.NewItems)
- {
- if (item is ImageLabelViewModel label)
- {
- var exists = Group.Labels.FirstOrDefault(x => x.Id == label.Id);
- if (exists == null)
- {
- if (label is DescriptionLabelViewModel description)
- {
- Group.AddLabel(description.Description);
- }
- else if (label is ConclusionLabelViewModel conclusion)
- {
- Group.AddLabel(conclusion.Conclusion);
- }
- }
- }
- }
- }
- }
- }
- public class MultiSelectionLabelGroupViewModel : LabelGroupViewModel
- {
- public MultiSelectionLabelGroupViewModel(MultiSelectionLabelGroup group)
- : base(group)
- {
- }
- }
- public class SingleSelectionLabelGroupViewModel : LabelGroupViewModel
- {
- public SingleSelectionLabelGroupViewModel(SingleSelectionLabelGroup group)
- : base(group)
- {
- }
- }
- public class RootLabelViewModel : LabelObjectViewModel
- {
- public override string Title
- {
- get => RootLabel.Title;
- set
- {
- if (RootLabel != null)
- {
- RootLabel.Title = value;
- }
- }
- }
- public bool NeedPartsConclusion
- {
- get => RootLabel.NeedPartsConclusion;
- set
- {
- if (RootLabel.NeedPartsConclusion != value)
- {
- RootLabel.NeedPartsConclusion = value;
- OnPropertyChanged();
- }
- }
- }
- public RootLabel RootLabel { get; }
- public ObservableCollection<LabelGroupViewModel> Descriptions { get; }
- public ObservableCollection<LabelGroupViewModel> Conclusions { get; }
- public RootLabelViewModel(RootLabel rootLabel)
- : base(rootLabel.Id, rootLabel.Title)
- {
- RootLabel = rootLabel;
- Descriptions = new ObservableCollection<LabelGroupViewModel>(from x in RootLabel.Descriptions
- select (x is MultiSelectionLabelGroup)
- ? new MultiSelectionLabelGroupViewModel((MultiSelectionLabelGroup)x) { Root = this }
- : ((LabelGroupViewModel)new SingleSelectionLabelGroupViewModel((SingleSelectionLabelGroup)x) { Root = this }));
- Conclusions = new ObservableCollection<LabelGroupViewModel>(from x in RootLabel.Conclusions
- select (x is MultiSelectionLabelGroup)
- ? new MultiSelectionLabelGroupViewModel((MultiSelectionLabelGroup)x) { Root = this }
- : ((LabelGroupViewModel)new SingleSelectionLabelGroupViewModel((SingleSelectionLabelGroup)x) { Root = this }));
- Descriptions.CollectionChanged += OnDescriptionsCollectionChanged;
- Conclusions.CollectionChanged += OnConclusionsCollectionChanged;
- foreach(var descGroup in Descriptions)
- {
- foreach(var descLabel in descGroup.Labels)
- {
- var association = rootLabel.LabelAssociations.FirstOrDefault(x => x.Left.Id == descLabel.Id);
- if(association != null)
- {
- var conclusionId = association.Right.Id;
- foreach (var conGroup in Conclusions)
- {
- foreach (var conLabel in conGroup.Labels)
- {
- if(conLabel.Id == conclusionId)
- {
- descLabel.Linked = conLabel;
- }
- }
- }
- }
- }
- }
- }
- private void OnConclusionsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
- {
- if (e.OldItems != null)
- {
- foreach (var item in e.OldItems)
- {
- if (item is LabelGroupViewModel group)
- {
- var exists = RootLabel.Conclusions.FirstOrDefault(x => x.Id == group.Id);
- if (exists != null)
- {
- RootLabel.RemoveConclusionGroup(exists);
- }
- }
- }
- }
- if (e.NewItems != null)
- {
- var index = e.NewStartingIndex;
- foreach (var item in e.NewItems)
- {
- if (item is LabelGroupViewModel group)
- {
- var exists = RootLabel.Conclusions.FirstOrDefault(x => x.Id == group.Id);
- if (exists == null)
- {
- RootLabel.InsertConclusionGroup(index, group.Group);
- }
- }
- index++;
- }
- }
- }
- private void OnDescriptionsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
- {
- if (e.OldItems != null)
- {
- foreach (var item in e.OldItems)
- {
- if (item is LabelGroupViewModel group)
- {
- var exists = RootLabel.Descriptions.FirstOrDefault(x => x.Id == group.Id);
- if (exists != null)
- {
- RootLabel.RemoveDescriptionGroup(exists);
- }
- }
- }
- }
- if (e.NewItems != null)
- {
- var index = e.NewStartingIndex;
- foreach (var item in e.NewItems)
- {
- if (item is LabelGroupViewModel group)
- {
- var exists = RootLabel.Descriptions.FirstOrDefault(x => x.Id == group.Id);
- if (exists == null)
- {
- RootLabel.InsertDescriptionGroup(index,group.Group);
- }
- }
- index++;
- }
- }
- }
- }
- public class LabelContainerViewModel : ViewModel
- {
- private RootLabelViewModel _selectedRootLabel;
- public LabelContainer Container { get; }
- public RootLabelViewModel SelectedRootLabel
- {
- get => _selectedRootLabel;
- set
- {
- if (_selectedRootLabel != value)
- {
- _selectedRootLabel = value;
- OnPropertyChanged();
- }
- }
- }
- public ObservableCollection<RootLabelViewModel> Labels { get; }
- public LabelContainerViewModel(LabelContainer container)
- {
- Container = container;
- Labels = new ObservableCollection<RootLabelViewModel>(from x in container.Labels
- select new RootLabelViewModel(x));
- Labels.CollectionChanged += OnLabelsCollectionChanged;
- }
- private void OnLabelsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
- {
- if (e.OldItems != null)
- {
- foreach (var item in e.OldItems)
- {
- if (item is RootLabelViewModel label)
- {
- var exists = Container.Labels.FirstOrDefault(x => x.Id == label.Id);
- if (exists != null)
- {
- Container.Labels.Remove(exists);
- }
- }
- }
- }
- if (e.NewItems != null)
- {
- foreach (var item in e.NewItems)
- {
- if (item is RootLabelViewModel label)
- {
- var exists = Container.Labels.FirstOrDefault(x => x.Id == label.Id);
- if (exists == null)
- {
- Container.Labels.Add(label.RootLabel);
- }
- }
- }
- }
- }
- }
- }
|