-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
LayoutAnalyzerViewModel.cs
163 lines (131 loc) · 4.82 KB
/
LayoutAnalyzerViewModel.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using System.Windows.Input;
using GraphShape.Sample.MVVM;
using GraphShape.Sample.Properties;
using GraphShape.Sample.Utils;
using JetBrains.Annotations;
namespace GraphShape.Sample.ViewModels
{
internal partial class LayoutAnalyzerViewModel : CommandSink, INotifyPropertyChanged
{
#region Commands
[NotNull]
public static readonly RoutedCommand AddLayoutCommand = new RoutedCommand("AddLayout", typeof(LayoutAnalyzerViewModel));
[NotNull]
public static readonly RoutedCommand RemoveLayoutCommand = new RoutedCommand("RemoveLayout", typeof(LayoutAnalyzerViewModel));
[NotNull]
public static readonly RoutedCommand RelayoutCommand = new RoutedCommand("Relayout", typeof(LayoutAnalyzerViewModel));
[NotNull]
public static readonly RoutedCommand ContinueLayoutCommand = new RoutedCommand("ContinueLayout", typeof(LayoutAnalyzerViewModel));
[NotNull]
public static readonly RoutedCommand OpenGraphCommand = new RoutedCommand("OpenGraph", typeof(LayoutAnalyzerViewModel));
[NotNull]
public static readonly RoutedCommand SaveGraphsCommand = new RoutedCommand("SaveGraphs", typeof(LayoutAnalyzerViewModel));
#endregion
public LayoutAnalyzerViewModel()
{
AnalyzedLayout = new GraphLayoutViewModel
{
LayoutAlgorithmType = "Sugiyama"
};
RegisterCommand(
ContinueLayoutCommand,
_ => true,
_ => ContinueLayout());
RegisterCommand(
RelayoutCommand,
_ => true,
_ => Relayout());
RegisterCommand(
OpenGraphCommand,
_ => true,
_ => OpenGraphs());
RegisterCommand(
SaveGraphsCommand,
_ => GraphModels.Count > 0,
_ => SaveGraphs());
CreateSampleGraph();
}
[NotNull, ItemNotNull]
public ObservableCollection<GraphViewModel> GraphModels { get; } = new ObservableCollection<GraphViewModel>();
[CanBeNull]
private GraphViewModel _selectedGraphModel;
[CanBeNull]
public GraphViewModel SelectedGraphModel
{
get => _selectedGraphModel;
set
{
if (_selectedGraphModel == value)
return;
_selectedGraphModel = value;
SelectedGraphChanged();
OnPropertyChanged(nameof(SelectedGraphModel));
}
}
private void SelectedGraphChanged()
{
AnalyzedLayout.Graph = SelectedGraphModel?.Graph;
}
[NotNull]
public GraphLayoutViewModel AnalyzedLayout { get; }
public void ContinueLayout()
{
LayoutManager.Instance.ContinueLayout();
}
public void Relayout()
{
LayoutManager.Instance.Relayout();
}
public void OpenGraphs()
{
var dialog = new OpenFileDialog
{
CheckPathExists = true
};
if (dialog.ShowDialog() == DialogResult.OK)
{
// Open the file and load the graphs
PocGraph graph = PocSerializeHelper.LoadGraph(dialog.FileName);
GraphModels.Add(
new GraphViewModel(
Path.GetFileNameWithoutExtension(dialog.FileName),
graph));
}
}
public void SaveGraphs()
{
var dialog = new FolderBrowserDialog
{
ShowNewFolderButton = true
};
if (dialog.ShowDialog() == DialogResult.OK)
{
foreach (GraphViewModel model in GraphModels)
{
PocSerializeHelper.SaveGraph(
model.Graph,
Path.Combine(dialog.SelectedPath, $"{model.Name}.{Settings.Default.GraphMLExtension}"));
}
}
}
#region INotifyPropertyChanged
/// <inheritdoc />
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Raises a <see cref="PropertyChanged"/> event for the given <paramref name="propertyName"/>.
/// </summary>
/// <param name="propertyName">Property name.</param>
[NotifyPropertyChangedInvocator]
private void OnPropertyChanged(string propertyName)
{
Debug.Assert(propertyName != null);
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
}