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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
|
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PresetManagerViewModel.cs" company="HandBrake Project (http://handbrake.fr)">
// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
// </copyright>
// <summary>
// Defines the PresetManagerViewModel type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace HandBrakeWPF.ViewModels
{
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using Caliburn.Micro;
using HandBrakeWPF.Factories;
using HandBrakeWPF.Model.Picture;
using HandBrakeWPF.Properties;
using HandBrakeWPF.Services.Interfaces;
using HandBrakeWPF.Services.Presets.Interfaces;
using HandBrakeWPF.Services.Presets.Model;
using HandBrakeWPF.Utilities;
using HandBrakeWPF.ViewModels.Interfaces;
using HandBrakeWPF.Views;
using Microsoft.Win32;
using Action = System.Action;
public class PresetManagerViewModel : ViewModelBase, IPresetManagerViewModel
{
private readonly IPresetService presetService;
private readonly IErrorService errorService;
private readonly IWindowManager windowManager;
private readonly PresetDisplayCategory addNewCategory = new PresetDisplayCategory(Resources.AddPresetView_AddNewCategory, true, null);
private IPresetObject selectedPresetCategory;
private Preset selectedPreset;
private PictureSettingsResLimitModes selectedPictureSettingsResLimitMode;
private Action mainWindowCallback;
public PresetManagerViewModel(IPresetService presetService, IErrorService errorService, IWindowManager windowManager)
{
this.presetService = presetService;
this.errorService = errorService;
this.windowManager = windowManager;
this.Title = Resources.PresetManger_Title;
}
public bool IsOpen { get; set; }
public IEnumerable<IPresetObject> PresetsCategories { get; set; }
public List<PresetDisplayCategory> UserPresetCategories { get; set; }
public PresetDisplayCategory SelectedUserPresetCategory
{
get
{
if (this.selectedPreset != null && this.PresetsCategories != null)
{
return this.PresetsCategories.FirstOrDefault(s => s.Category == this.selectedPreset.Category) as PresetDisplayCategory;
}
return null;
}
set
{
if (this.selectedPreset != null && value != null && value.Category != this.selectedPreset.Category)
{
this.presetService.ChangePresetCategory(this.selectedPreset, value.Category);
}
}
}
public string SelectedItem { get; set; }
public IPresetObject SelectedPresetCategory
{
get => this.selectedPresetCategory;
set
{
if (!object.Equals(this.selectedPresetCategory, value))
{
this.selectedPresetCategory = value;
this.NotifyOfPropertyChange(() => this.SelectedPresetCategory);
this.SelectedItem = value?.Category;
this.NotifyOfPropertyChange(() => this.SelectedItem);
this.selectedPreset = null;
this.NotifyOfPropertyChange(() => this.SelectedPreset);
}
}
}
public Preset SelectedPreset
{
get => this.selectedPreset;
set
{
if (!object.Equals(this.selectedPreset, value))
{
this.selectedPreset = value;
this.NotifyOfPropertyChange(() => this.SelectedPreset);
if (value != null)
{
this.SelectedItem = value.Name;
this.NotifyOfPropertyChange(() => this.SelectedItem);
this.IsBuildIn = value.IsBuildIn;
this.CustomWidth = value.Task.MaxWidth;
this.CustomHeight = value.Task.MaxHeight;
this.SetSelectedPictureSettingsResLimitMode();
}
else
{
this.selectedPresetCategory = null;
this.NotifyOfPropertyChange(() => this.SelectedPresetCategory);
}
}
this.NotifyOfPropertyChange(() => this.IsBuildIn);
this.NotifyOfPropertyChange(() => this.SelectedUserPresetCategory);
this.NotifyOfPropertyChange(() => this.IsPresetSelected);
this.NotifyOfPropertyChange(() => this.UserPresetCategories);
}
}
public bool IsBuildIn { get; private set; }
public bool IsPresetSelected => this.SelectedPreset != null;
public BindingList<PictureSettingsResLimitModes> ResolutionLimitModes => new BindingList<PictureSettingsResLimitModes>
{
PictureSettingsResLimitModes.None,
PictureSettingsResLimitModes.Size8K,
PictureSettingsResLimitModes.Size4K,
PictureSettingsResLimitModes.Size1080p,
PictureSettingsResLimitModes.Size720p,
PictureSettingsResLimitModes.Size576p,
PictureSettingsResLimitModes.Size480p,
PictureSettingsResLimitModes.Custom,
};
public PictureSettingsResLimitModes SelectedPictureSettingsResLimitMode
{
get => this.selectedPictureSettingsResLimitMode;
set
{
if (value == this.selectedPictureSettingsResLimitMode)
{
return;
}
this.selectedPictureSettingsResLimitMode = value;
this.NotifyOfPropertyChange(() => this.SelectedPictureSettingsResLimitMode);
this.IsCustomMaxRes = value == PictureSettingsResLimitModes.Custom;
this.NotifyOfPropertyChange(() => this.IsCustomMaxRes);
// Enforce the new limit
ResLimit limit = EnumHelper<PictureSettingsResLimitModes>.GetAttribute<ResLimit, PictureSettingsResLimitModes>(value);
if (limit != null)
{
this.CustomWidth = limit.Width;
this.CustomHeight = limit.Height;
this.NotifyOfPropertyChange(() => this.CustomWidth);
this.NotifyOfPropertyChange(() => this.CustomHeight);
}
if (value == PictureSettingsResLimitModes.None)
{
this.CustomWidth = null;
this.CustomHeight = null;
}
}
}
public int? CustomWidth
{
get => this.selectedPreset?.Task.MaxWidth ?? null;
set
{
if (value == this.selectedPreset.Task.MaxWidth)
{
return;
}
this.selectedPreset.Task.MaxWidth = value;
this.NotifyOfPropertyChange(() => this.CustomWidth);
}
}
public int? CustomHeight
{
get => this.selectedPreset?.Task.MaxHeight ?? null;
set
{
if (value == this.selectedPreset.Task.MaxHeight)
{
return;
}
this.selectedPreset.Task.MaxHeight = value;
this.NotifyOfPropertyChange(() => this.CustomHeight);
}
}
public bool IsCustomMaxRes { get; private set; }
public void SetupWindow(Action mainwindowCallback)
{
this.mainWindowCallback = mainwindowCallback;
this.PresetsCategories = this.presetService.Presets;
this.NotifyOfPropertyChange(() => this.PresetsCategories);
this.presetService.LoadCategoryStates();
this.UserPresetCategories = presetService.GetPresetCategories(true).ToList(); // .Union(new List<PresetDisplayCategory> { addNewCategory }).ToList();
this.presetService.PresetCollectionChanged += this.PresetService_PresetCollectionChanged;
}
public void DeletePreset()
{
if (this.selectedPreset != null)
{
if (this.selectedPreset.IsDefault)
{
this.errorService.ShowMessageBox(
Resources.MainViewModel_CanNotDeleteDefaultPreset,
Resources.Warning,
MessageBoxButton.OK,
MessageBoxImage.Information);
return;
}
MessageBoxResult result =
this.errorService.ShowMessageBox(
Resources.MainViewModel_PresetRemove_AreYouSure + this.selectedPreset.Name + " ?",
Resources.Question,
MessageBoxButton.YesNo,
MessageBoxImage.Question);
if (result == MessageBoxResult.No)
{
return;
}
this.presetService.Remove(this.selectedPreset);
this.NotifyOfPropertyChange(() => this.PresetsCategories);
this.SelectedPreset = this.presetService.DefaultPreset;
}
else
{
this.errorService.ShowMessageBox(Resources.Main_SelectPreset, Resources.Warning, MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
public void SetDefault()
{
if (this.selectedPreset != null)
{
this.presetService.SetDefault(this.selectedPreset);
this.errorService.ShowMessageBox(string.Format(Resources.Main_NewDefaultPreset, this.selectedPreset.Name), Resources.Main_Presets, MessageBoxButton.OK, MessageBoxImage.Information);
}
else
{
this.errorService.ShowMessageBox(Resources.Main_SelectPreset, Resources.Warning, MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
public void Import()
{
OpenFileDialog dialog = new OpenFileDialog { Filter = "Preset Files|*.json;*.plist", CheckFileExists = true };
bool? dialogResult = dialog.ShowDialog();
if (dialogResult.HasValue && dialogResult.Value)
{
this.presetService.Import(dialog.FileName);
this.NotifyOfPropertyChange(() => this.PresetsCategories);
}
}
public void Export()
{
if (this.selectedPreset != null && !this.selectedPreset.IsBuildIn)
{
SaveFileDialog savefiledialog = new SaveFileDialog
{
Filter = "json|*.json",
CheckPathExists = true,
AddExtension = true,
DefaultExt = ".json",
OverwritePrompt = true,
FilterIndex = 0
};
savefiledialog.ShowDialog();
string filename = savefiledialog.FileName;
if (!string.IsNullOrEmpty(filename))
{
this.presetService.Export(savefiledialog.FileName, this.selectedPreset, HBConfigurationFactory.Create());
}
}
else
{
this.errorService.ShowMessageBox(Resources.Main_SelectPreset, Resources.Warning, MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
public void ExportUserPresets()
{
SaveFileDialog savefiledialog = new SaveFileDialog
{
Filter = "json|*.json",
CheckPathExists = true,
AddExtension = true,
DefaultExt = ".json",
OverwritePrompt = true,
FilterIndex = 0
};
savefiledialog.ShowDialog();
string filename = savefiledialog.FileName;
if (!string.IsNullOrEmpty(filename))
{
IList<PresetDisplayCategory> userPresets = this.presetService.GetPresetCategories(true);
this.presetService.ExportCategories(savefiledialog.FileName, userPresets, HBConfigurationFactory.Create());
}
}
public void DeleteBuiltInPresets()
{
List<Preset> allPresets = this.presetService.FlatPresetList;
bool foundDefault = false;
foreach (Preset preset in allPresets)
{
if (preset.IsBuildIn)
{
if (preset.IsDefault)
{
foundDefault = true;
}
this.presetService.Remove(preset);
}
}
if (foundDefault)
{
Preset preset = this.presetService.FlatPresetList.FirstOrDefault();
if (preset != null)
{
this.presetService.SetDefault(preset);
this.SelectedPreset = preset;
}
}
this.NotifyOfPropertyChange(() => this.PresetsCategories);
}
public void ResetBuiltInPresets()
{
this.presetService.UpdateBuiltInPresets();
this.NotifyOfPropertyChange(() => this.PresetsCategories);
this.SetDefaultPreset();
this.errorService.ShowMessageBox(Resources.Presets_ResetComplete, Resources.Presets_ResetHeader, MessageBoxButton.OK, MessageBoxImage.Information);
}
public void EditAudioDefaults()
{
if (this.selectedPreset == null)
{
return;
}
IAudioDefaultsViewModel audioDefaultsViewModel = new AudioDefaultsViewModel(this.selectedPreset.Task);
audioDefaultsViewModel.ResetApplied();
audioDefaultsViewModel.Setup(this.selectedPreset, this.selectedPreset.Task);
this.windowManager.ShowDialogAsync(audioDefaultsViewModel);
if (audioDefaultsViewModel.IsApplied)
{
this.SelectedPreset.AudioTrackBehaviours = audioDefaultsViewModel.AudioBehaviours.Clone();
}
}
public void EditSubtitleDefaults()
{
if (this.selectedPreset == null)
{
return;
}
ISubtitlesDefaultsViewModel subtitlesDefaultsViewModel = new SubtitlesDefaultsViewModel();
subtitlesDefaultsViewModel.ResetApplied();
SubtitlesDefaultsView view = new SubtitlesDefaultsView();
view.DataContext = subtitlesDefaultsViewModel;
view.ShowDialog();
if (subtitlesDefaultsViewModel.IsApplied)
{
this.SelectedPreset.SubtitleTrackBehaviours = subtitlesDefaultsViewModel.SubtitleBehaviours.Clone();
}
}
public void Close()
{
this.presetService.Save();
this.IsOpen = false;
this.presetService.PresetCollectionChanged -= this.PresetService_PresetCollectionChanged;
if (this.mainWindowCallback != null)
{
mainWindowCallback();
}
}
public void SetCurrentPresetAsDefault()
{
if (this.SelectedPreset != null)
{
this.presetService.SetDefault(this.SelectedPreset);
}
}
public void LaunchHelp()
{
Process.Start("explorer.exe", "https://handbrake.fr/docs/en/latest/advanced/custom-presets.html");
}
private void SetDefaultPreset()
{
// Preset Selection
if (this.presetService.DefaultPreset != null)
{
PresetDisplayCategory category =
(PresetDisplayCategory)this.PresetsCategories.FirstOrDefault(
p => p.Category == this.presetService.DefaultPreset.Category);
this.SelectedPresetCategory = category;
this.SelectedPreset = this.presetService.DefaultPreset;
}
}
private void SetSelectedPictureSettingsResLimitMode()
{
// Look for a matching resolution.
foreach (PictureSettingsResLimitModes limit in EnumHelper<PictureSettingsResLimitModes>.GetEnumList())
{
ResLimit resLimit = EnumHelper<PictureSettingsResLimitModes>.GetAttribute<ResLimit, PictureSettingsResLimitModes>(limit);
if (resLimit != null)
{
if (resLimit.Width == this.CustomWidth && resLimit.Height == this.CustomHeight)
{
this.SelectedPictureSettingsResLimitMode = limit;
return;
}
}
}
if (this.CustomWidth.HasValue || this.CustomHeight.HasValue)
{
this.SelectedPictureSettingsResLimitMode = PictureSettingsResLimitModes.Custom;
}
else
{
this.SelectedPictureSettingsResLimitMode = PictureSettingsResLimitModes.None;
}
}
private void PresetService_PresetCollectionChanged(object sender, System.EventArgs e)
{
string presetName = this.selectedPreset?.Name; // Recording such that we can re-select
this.PresetsCategories = this.presetService.Presets;
this.UserPresetCategories = presetService.GetPresetCategories(true).ToList(); // .Union(new List<PresetDisplayCategory> { addNewCategory }).ToList();
this.NotifyOfPropertyChange(() => this.PresetsCategories);
this.NotifyOfPropertyChange(() => this.UserPresetCategories);
this.NotifyOfPropertyChange(() => this.SelectedUserPresetCategory);
// Reselect the preset as the object has changed due to the reload that occurred.
if (!string.IsNullOrEmpty(presetName))
{
this.SelectedPreset = this.presetService.FlatPresetList.FirstOrDefault(s => s.Name == presetName);
}
}
}
}
|