-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMacrocycleViewModel.cs
93 lines (83 loc) · 2.64 KB
/
MacrocycleViewModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System.ComponentModel;
using ChemSharp.Molecules;
using PorphyStruct.Core;
using PorphyStruct.Core.Analysis.Properties;
using TinyMVVM;
namespace PorphyStruct.ViewModel;
public class MacrocycleViewModel : ListingViewModel<AnalysisViewModel>
{
/// <summary>
/// The Path to open from
/// </summary>
public string Filename { get; }
private Atom? _selectedAtom;
/// <summary>
/// Gets or Sets the selected Atom
/// </summary>
public Atom? SelectedAtom
{
get => _selectedAtom;
set => Set(ref _selectedAtom, value, () => HandleAtomSelect(_selectedAtom));
}
/// <summary>
/// The opened Macrocycle
/// </summary>
public Macrocycle Macrocycle { get; }
public MacrocycleViewModel(string path)
{
Filename = path;
Macrocycle = new Macrocycle(Filename);
}
public MacrocycleViewModel(Macrocycle cycle)
{
Macrocycle = cycle;
Filename = cycle.Title;
}
public override string Title => Path.GetFileNameWithoutExtension(Filename);
/// <summary>
/// Runs Detection Algorithm
/// </summary>
/// <returns></returns>
public async Task Analyze()
{
Items.Clear();
await Task.Run(Macrocycle.Detect);
Validate();
foreach (var part in Macrocycle.DetectedParts)
{
var analysis = new AnalysisViewModel(this, part);
part.Properties ??= new MacrocycleProperties(part);
Items.Add(analysis);
SelectedIndex = Items.IndexOf(analysis);
analysis.PropertyChanged += Child_Changed;
}
SelectedIndexChanged += (sender, args) => SelectedAtom = null;
}
private void Child_Changed(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName != nameof(SelectedItem.Analysis)) return;
var tmp = SelectedIndex;
SelectedIndex = -1;
OnSelectedIndexChanged();
SelectedIndex = tmp;
OnSelectedIndexChanged();
}
protected virtual void Validate() { }
public virtual void HandleAtomSelect(Atom? selectedAtom)
{
foreach (var viewModel in Items)
{
if (!viewModel.Analysis.Atoms.Contains(selectedAtom))
{
viewModel.ExperimentalSeries.ClearSelection();
viewModel.Model.InvalidatePlot(true);
continue;
}
SelectedIndex = Items.IndexOf(viewModel);
var index = viewModel.Analysis.GetMappingIndex(selectedAtom);
viewModel.ExperimentalSeries.SelectItem(index);
viewModel.Model.InvalidatePlot(true);
break;
}
}
}