1 // --------------------------------------------------------------------------------------------------------------------
2 // <copyright file="ChromaSmoothFilter.cs" company="HandBrake Project (http://handbrake.fr)">
3 //   This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
4 // </copyright>
5 // <summary>
6 //   Defines the Chroma Smooth Filter type.
7 // </summary>
8 // --------------------------------------------------------------------------------------------------------------------
9 
10 namespace HandBrakeWPF.ViewModelItems.Filters
11 {
12     using System.ComponentModel;
13     using System.Linq;
14 
15     using Caliburn.Micro;
16 
17     using HandBrake.Interop.Interop;
18     using HandBrake.Interop.Interop.HbLib;
19     using HandBrake.Interop.Interop.Interfaces.Model.Filters;
20 
21     using HandBrakeWPF.Model.Filters;
22     using HandBrakeWPF.Services.Encode.Model;
23     using HandBrakeWPF.Services.Presets.Model;
24     using HandBrakeWPF.Services.Scan.Model;
25 
26     using Action = System.Action;
27 
28     public class ChromaSmoothFilter : PropertyChangedBase
29     {
30         public static readonly string Off = "off";
31         public static readonly string Custom = "custom";
32 
33         private readonly Action triggerTabChanged;
34 
ChromaSmoothFilter(EncodeTask currentTask, Action triggerTabChanged)35         public ChromaSmoothFilter(EncodeTask currentTask, Action triggerTabChanged)
36         {
37             this.triggerTabChanged = triggerTabChanged;
38             this.CurrentTask = currentTask;
39 
40             this.SetPresets();
41             this.SetTunes();
42         }
43 
44         public EncodeTask CurrentTask { get; private set; }
45 
46         public BindingList<FilterPreset> Presets { get; set; }
47 
48         public BindingList<FilterTune> Tunes { get; set; }
49 
50         public bool ShowTune => this.SelectedPreset != null && this.SelectedPreset.Key != Off && this.SelectedPreset.Key != Custom;
51 
52         public bool ShowCustom => this.SelectedPreset != null && this.SelectedPreset.Key == Custom;
53 
54         public FilterPreset SelectedPreset
55         {
56             get => this.CurrentTask.ChromaSmooth;
57 
58             set
59             {
60                 if (Equals(value, this.CurrentTask.ChromaSmooth))
61                 {
62                     return;
63                 }
64 
65                 this.CurrentTask.ChromaSmooth = value;
66 
67                 this.NotifyOfPropertyChange(() => this.SelectedPreset);
68                 this.NotifyOfPropertyChange(() => this.ShowTune);
69                 this.NotifyOfPropertyChange(() => this.ShowCustom);
70 
71                 if (this.SelectedTune == null)
72                 {
73                     this.SelectedTune = this.Tunes.FirstOrDefault();
74                 }
75 
76                 this.triggerTabChanged();
77             }
78         }
79 
80         public FilterTune SelectedTune
81         {
82             get => this.CurrentTask.ChromaSmoothTune;
83 
84             set
85             {
86                 if (Equals(value, this.CurrentTask.ChromaSmoothTune))
87                 {
88                     return;
89                 }
90 
91                 this.CurrentTask.ChromaSmoothTune = value;
92 
93                 this.NotifyOfPropertyChange(() => this.SelectedTune);
94                 this.triggerTabChanged();
95             }
96         }
97 
98         public string CustomSettings
99         {
100             get => this.CurrentTask.CustomChromaSmooth;
101 
102             set
103             {
104                 if (value == this.CurrentTask.CustomChromaSmooth)
105                 {
106                     return;
107                 }
108 
109                 this.CurrentTask.CustomChromaSmooth = value;
110                 this.NotifyOfPropertyChange(() => this.CustomSettings);
111             }
112         }
113 
SetPreset(Preset preset, EncodeTask task)114         public void SetPreset(Preset preset, EncodeTask task)
115         {
116             this.CurrentTask = task;
117 
118             if (preset == null)
119             {
120                 this.SelectedPreset = new FilterPreset(HandBrakeFilterHelpers.GetFilterPresets((int)hb_filter_ids.HB_FILTER_CHROMA_SMOOTH).FirstOrDefault(s => s.ShortName == "off"));
121                 this.CustomSettings = string.Empty;
122                 this.SelectedTune = null;
123                 return;
124             }
125 
126             this.SelectedPreset = preset.Task.ChromaSmooth;
127             this.SelectedTune = preset.Task.ChromaSmoothTune;
128             this.CustomSettings = preset.Task.CustomChromaSmooth;
129         }
130 
UpdateTask(EncodeTask task)131         public void UpdateTask(EncodeTask task)
132         {
133             this.CurrentTask = task;
134             this.NotifyOfPropertyChange(() => this.SelectedPreset);
135             this.NotifyOfPropertyChange(() => this.SelectedTune);
136             this.NotifyOfPropertyChange(() => this.CustomSettings);
137 
138             this.NotifyOfPropertyChange(() => this.ShowTune);
139             this.NotifyOfPropertyChange(() => this.ShowCustom);
140         }
141 
MatchesPreset(Preset preset)142         public bool MatchesPreset(Preset preset)
143         {
144             if (this.SelectedPreset?.Key != preset.Task?.ChromaSmooth?.Key)
145             {
146                 return false;
147             }
148 
149             if (this.SelectedTune?.Key != preset?.Task?.ChromaSmoothTune?.Key)
150             {
151                 return false;
152             }
153 
154             if (this.CustomSettings != preset?.Task?.CustomChromaSmooth)
155             {
156                 return false;
157             }
158 
159             return true;
160         }
161 
SetSource(Source source, Title title, Preset preset, EncodeTask task)162         public void SetSource(Source source, Title title, Preset preset, EncodeTask task)
163         {
164             this.CurrentTask = task;
165         }
166 
SetPresets()167         private void SetPresets()
168         {
169             BindingList<FilterPreset> presets = new BindingList<FilterPreset>();
170             foreach (HBPresetTune tune in HandBrakeFilterHelpers.GetFilterPresets((int)hb_filter_ids.HB_FILTER_CHROMA_SMOOTH))
171             {
172                 presets.Add(new FilterPreset(tune));
173             }
174 
175             this.Presets = presets;
176             this.NotifyOfPropertyChange(() => this.Presets);
177         }
178 
SetTunes()179         private void SetTunes()
180         {
181             BindingList<FilterTune> tunes = new BindingList<FilterTune>();
182             foreach (HBPresetTune tune in HandBrakeFilterHelpers.GetFilterTunes((int)hb_filter_ids.HB_FILTER_CHROMA_SMOOTH))
183             {
184                 tunes.Add(new FilterTune(tune));
185             }
186 
187             this.Tunes = tunes;
188             this.NotifyOfPropertyChange(() => this.Tunes);
189         }
190     }
191 }
192