-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHocrDocumentViewModel.cs
More file actions
243 lines (188 loc) · 6.59 KB
/
HocrDocumentViewModel.cs
File metadata and controls
243 lines (188 loc) · 6.59 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Data;
using CommunityToolkit.Mvvm.Input;
using HocrEditor.Controls;
using HocrEditor.Core;
using HocrEditor.Helpers;
using HocrEditor.Models;
using Rect = HocrEditor.Models.Rect;
namespace HocrEditor.ViewModels;
public class HocrDocumentViewModel : ViewModelBase, IUndoRedoCommandsService
{
public UndoRedoManager UndoRedoManager { get; } = new();
public string? Filename { get; set; }
public string Name => Filename ?? "New document";
public string Title
{
get
{
var sb = new StringBuilder();
if (IsChanged)
{
sb.Append('*');
}
sb.Append(Name);
return sb.ToString();
}
}
private HocrDocument HocrDocument { get; set; }
public ObservableCollection<HocrPageViewModel> Pages { get; }
public ICollectionView PagesCollectionView { get; }
public string OcrSystem
{
get => HocrDocument.OcrSystem;
set => HocrDocument.OcrSystem = value;
}
public ICollection<string> Capabilities => HocrDocument.Capabilities;
public HocrPageViewModel? CurrentPage
{
get => (HocrPageViewModel?)PagesCollectionView.CurrentItem;
set => PagesCollectionView.MoveCurrentTo(value);
}
public bool ShowThresholdedImage { get; set; }
public bool ShowText { get; set; }
public bool ShowNumbering { get; set; }
public Rect SelectionBounds
{
get => CurrentPage?.SelectionBounds ?? Rect.Empty;
set
{
if (CurrentPage != null)
{
CurrentPage.SelectionBounds = value;
}
}
}
public ICanvasTool CanvasTool { get; set; }
public ReadOnlyObservableCollection<NodeVisibility> NodeVisibility { get; } = new(
new ObservableCollection<NodeVisibility>(
HocrNodeTypeViewModel.NodeTypes.Select(k => new NodeVisibility(k))
)
);
public IRelayCommand<HocrPageViewModel> DeletePageCommand { get; }
public IRelayCommand NextPageCommand { get; }
public IRelayCommand PreviousPageCommand { get; }
public IRelayCommand<ICanvasTool> SelectToolCommand { get; }
public HocrDocumentViewModel() : this(
new HocrDocument(Enumerable.Empty<HocrPage>()),
Enumerable.Empty<HocrPageViewModel>()
)
{
}
public HocrDocumentViewModel(HocrDocument hocrDocument, IEnumerable<HocrPageViewModel> pages)
{
HocrDocument = hocrDocument;
CanvasTool = DocumentCanvasTools.SelectionTool;
Pages = new ObservableCollection<HocrPageViewModel>(pages);
Pages.SubscribeItemPropertyChanged(PagesChanged);
PagesCollectionView = CollectionViewSource.GetDefaultView(Pages);
PagesCollectionView.CurrentChanging += PagesCollectionViewOnCurrentChanging;
PagesCollectionView.CurrentChanged += PagesCollectionViewOnCurrentChanged;
DeletePageCommand = new RelayCommand<HocrPageViewModel>(DeletePage, CanDeletePage);
NextPageCommand = new RelayCommand(
() => PagesCollectionView.MoveCurrentToNext(),
() => !PagesCollectionView.IsCurrentLast()
);
PreviousPageCommand = new RelayCommand(
() => PagesCollectionView.MoveCurrentToPrevious(),
() => !PagesCollectionView.IsCurrentFirst()
);
SelectToolCommand = new RelayCommand<ICanvasTool>(
tool =>
{
tool ??= DocumentCanvasTools.SelectionTool;
if (CanvasTool != tool)
{
CanvasTool = tool;
}
},
tool =>
{
tool ??= DocumentCanvasTools.SelectionTool;
if (CurrentPage == null)
{
return false;
}
return tool.CanMount(CurrentPage);
}
);
IsChanged = false;
}
private void PagesChanged(object? sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case nameof(IsChanged):
{
IsChanged = Pages.Any(p => p.IsChanged);
break;
}
}
}
public bool CanDeletePage(HocrPageViewModel? page) => page != null && Pages.Contains(page);
public void DeletePage(HocrPageViewModel? page)
{
if (page == null)
{
return;
}
UndoRedoManager.ExecuteCommand(Pages.ToCollectionRemoveCommand(page));
}
public override void MarkAsUnchanged()
{
foreach (var page in Pages)
{
page.MarkAsUnchanged();
}
base.MarkAsUnchanged();
}
public HocrDocument BuildDocumentModel()
{
HocrDocument.Pages.Clear();
if (Pages.Any(page => page.HocrPage == null))
{
throw new InvalidOperationException("Expected all HocrPages to not be null");
}
HocrDocument.Pages.AddRange(Pages.Select(page => page.HocrPage!));
return HocrDocument;
}
private void PagesCollectionViewOnCurrentChanging(object sender, CurrentChangingEventArgs e)
{
if (sender is ICollectionView { CurrentItem: HocrPageViewModel page })
{
page.SelectedNodes.CollectionChanged -= CurrentPageSelectedNodesOnCollectionChanged;
}
}
private void PagesCollectionViewOnCurrentChanged(object? sender, EventArgs e)
{
if (sender is ICollectionView { CurrentItem: HocrPageViewModel page })
{
page.SelectedNodes.CollectionChanged += CurrentPageSelectedNodesOnCollectionChanged;
}
SelectToolCommand.NotifyCanExecuteChanged();
OnPropertyChanged(nameof(CurrentPage));
}
private void CurrentPageSelectedNodesOnCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
SelectToolCommand.NotifyCanExecuteChanged();
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
Pages.UnsubscribeItemPropertyChanged(PagesChanged);
if (CurrentPage != null)
{
CurrentPage.SelectedNodes.CollectionChanged -= CurrentPageSelectedNodesOnCollectionChanged;
}
PagesCollectionView.CurrentChanging -= PagesCollectionViewOnCurrentChanging;
PagesCollectionView.CurrentChanged -= PagesCollectionViewOnCurrentChanged;
Pages.Dispose();
NodeVisibility.Dispose();
}
}