-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
381 lines (321 loc) · 11.9 KB
/
MainWindow.xaml.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Speech;
using System.Speech.Synthesis;
using System.Collections.ObjectModel;
using System.Threading;
using System.IO;
using System.Windows.Forms;
using System.Globalization;
namespace Dictee
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
ViewModel _vm;
public MainWindow()
{
InitializeComponent();
_vm = new ViewModel(StackPan);
DataContext = _vm;
}
}
public class ViewModel : NotifyBaseClass
{
//public List<string> Phrases { get; set; }
public ObservableCollection<string> Phrases { get; set; }
public int PhraseId { get; set; }
bool IsChecked { get; set; }
bool IsValidate { get; set; }
bool InProgress { get; set; }
SpeechSynthesizer parole;
StackPanel _stack;
public ViewModel(StackPanel stack)
{
string a = "abcd";
var c = a[1];
IsChecked = false;
InProgress = false;
IsValidate = false;
NbrErrorValidate = 0;
MainIsVisible = false;
parole = new SpeechSynthesizer();
_stack = stack;
Phrases = new ObservableCollection<string>();
var v = parole.GetInstalledVoices();
string t = "";
foreach (InstalledVoice iv in v)
t += iv.VoiceInfo.Name + ", ";
try
{
parole.SelectVoice("Microsoft Hortense Desktop");
}
catch
{
System.Windows.Forms.MessageBox.Show("Error set language Virginie : " + t);
if (v.Count > 0)
parole.SelectVoice(v[0].VoiceInfo.Name);
}
InitializeCommand();
}
public void InitializeCommand()
{
CommandStart = new RoutedUICommand( "Start", "TestMenuClick", typeof(ViewModel) );
CommandBinding commandStartBinding = new CommandBinding(CommandStart, Start, CanStart);
CommandManager.RegisterClassCommandBinding( typeof(UIElement ), commandStartBinding );
CommandNext = new RoutedUICommand("Next", "TestMenuClick", typeof(ViewModel));
CommandBinding commandNextBinding = new CommandBinding(CommandNext, Next, CanNext);
CommandManager.RegisterClassCommandBinding(typeof(UIElement), commandNextBinding);
CommandRead = new RoutedUICommand("Read", "TestMenuClick", typeof(ViewModel));
CommandBinding commandReadBinding = new CommandBinding(CommandRead, Read, CanRead);
CommandManager.RegisterClassCommandBinding(typeof(UIElement), commandReadBinding);
CommandCheck = new RoutedUICommand("Check", "TestMenuClick", typeof(ViewModel));
CommandBinding commandCheckBinding = new CommandBinding(CommandCheck, Check, CanCheck);
CommandManager.RegisterClassCommandBinding(typeof(UIElement), commandCheckBinding);
CommandOpen = new RoutedUICommand("Open", "TestMenuClick", typeof(ViewModel));
CommandBinding commandOpenBinding = new CommandBinding(CommandOpen, Open, CanOpen);
CommandManager.RegisterClassCommandBinding(typeof(UIElement), commandOpenBinding);
CommandValidate = new RoutedUICommand("Validate", "TestMenuClick", typeof(ViewModel));
CommandBinding commandValidateBinding = new CommandBinding(CommandValidate, Validate, CanValidate);
CommandManager.RegisterClassCommandBinding(typeof(UIElement), commandValidateBinding);
CommandShowMain = new RoutedUICommand("ShowMain", "TestMenuClick", typeof(ViewModel));
CommandBinding commandShowMainBinding = new CommandBinding(CommandShowMain, ShowMain, CanShowMain);
CommandManager.RegisterClassCommandBinding(typeof(UIElement), commandShowMainBinding);
}
public RoutedUICommand CommandShowMain { get; set; }
void ShowMain(object param, ExecutedRoutedEventArgs e)
{
MainIsVisible = !MainIsVisible;
}
void CanShowMain(object param, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
public RoutedUICommand CommandValidate { get; set; }
void Validate(object param, ExecutedRoutedEventArgs e)
{
var list = _stack.Children;
int err = 0;
foreach( UIElement p in list )
{
if( p is PhraseControl )
{
PhraseControl cont = p as PhraseControl;
err += cont.Check(true);
}
}
NbrErrorValidate = err;
IsValidate = true;
}
void CanValidate(object param, CanExecuteRoutedEventArgs e)
{
e.CanExecute = IsChecked && !IsValidate;
}
public RoutedUICommand CommandOpen { get; set; }
void Open(object param, ExecutedRoutedEventArgs e)
{
OpenFileDialog of = new OpenFileDialog();
of.InitialDirectory = Directory.GetCurrentDirectory();
of.Filter = "Text Files (.txt)|*.txt";
var res = of.ShowDialog();
if (res == DialogResult.OK)
{
Filename = of.SafeFileName;
int cpt = 0;
Phrases.Clear();
var lines = File.ReadAllLines(of.FileName, Encoding.GetEncoding(1252));
foreach (string l in lines)
{
if (l[0] != '-' && l != "")
{
Phrases.Add(l);
((PhraseControl)_stack.Children[cpt]).SetPhrase(l);
cpt++;
}
}
}
}
void CanOpen(object param, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
public RoutedUICommand CommandCheck { get; set; }
void Check(object param, ExecutedRoutedEventArgs e)
{
var list = _stack.Children;
int err = 0;
foreach( UIElement p in list )
{
if( p is PhraseControl )
{
PhraseControl cont = p as PhraseControl;
err += cont.Check();
}
}
NbrError = err;
IsChecked = true;
SaveText("Corr_" + Filename.Split('.')[0] + "_" + err.ToString() + ".txt");
}
void CanCheck(object param, CanExecuteRoutedEventArgs e)
{
e.CanExecute = !IsChecked;
}
public RoutedUICommand CommandRead { get; set; }
void Read(object param, ExecutedRoutedEventArgs e)
{
OpenFileDialog of = new OpenFileDialog();
of.InitialDirectory = Directory.GetCurrentDirectory();
of.Filter = "Text Files (.txt)|*.txt";
var res = of.ShowDialog();
if (res == DialogResult.OK)
{
Filename = of.SafeFileName;
int cpt = 1;
Phrases.Clear();
var lines = File.ReadAllLines(of.FileName, Encoding.GetEncoding(1252));
foreach (string l in lines)
{
if (l[0] != '-' && l != "")
{
Phrases.Add(l);
_stack.Children.Add(new PhraseControl(cpt, l, parole, this));
cpt++;
}
}
PhraseInProgress = Phrases[0];
PhraseId = 0;
InProgress = true;
}
}
void CanRead(object param, CanExecuteRoutedEventArgs e)
{
e.CanExecute = !InProgress;
}
public RoutedUICommand CommandNext { get; set; }
void Next(object param, ExecutedRoutedEventArgs e)
{
if (PhraseId < Phrases.Count - 1)
PhraseId++;
PhraseInProgress = Phrases[PhraseId];
}
void CanNext(object param, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
public RoutedUICommand CommandStart { get; set; }
void Start(object param, ExecutedRoutedEventArgs e)
{
}
void CanStart(object param, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
public void SaveText(string filename = "tmp.txt")
{
List<string> phrase = new List<string>();
var list = _stack.Children;
foreach (UIElement p in list)
{
if (p is PhraseControl)
{
PhraseControl cont = p as PhraseControl;
phrase.Add(cont.GetPhrase());
}
}
File.WriteAllLines(filename, phrase, Encoding.GetEncoding(1252));
}
public bool MainIsVisible
{
get => _mainIsVisible;
set
{
if (value != _mainIsVisible)
{
_mainIsVisible = value;
NotifyPropertiyChanged(nameof(MainIsVisible));
}
}
}
private bool _mainIsVisible;
public int NbrErrorValidate
{
get => _nbrErrorValidate;
set
{
if (value != _nbrErrorValidate)
{
_nbrErrorValidate = value;
NotifyPropertiyChanged(nameof(NbrErrorValidate));
}
}
}
private int _nbrErrorValidate;
public int NbrError
{
get => _nbrError;
set
{
if (value != _nbrError)
{
_nbrError = value;
NotifyPropertiyChanged(nameof(NbrError));
}
}
}
private int _nbrError;
public string Filename
{
get => _filename;
set
{
if (value != _filename)
{
_filename = value;
NotifyPropertiyChanged(nameof(Filename));
}
}
}
private string _filename;
public string PhraseInProgress
{
get => _phraseInProgress;
set
{
if (value != _phraseInProgress)
{
_phraseInProgress = value;
NotifyPropertiyChanged(nameof(PhraseInProgress));
}
}
}
private string _phraseInProgress;
}
public class BoolToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((bool)value)
return Visibility.Visible;
else
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}