OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / Functions / PresetLoader.cs
1 /*  PresetLoader.cs $\r
2         \r
3            This file is part of the HandBrake source code.\r
4            Homepage: <http://handbrake.fr>.\r
5            It may be used under the terms of the GNU General Public License. */\r
6 \r
7 using System;\r
8 using System.Windows.Forms;\r
9 \r
10 namespace Handbrake.Functions\r
11 {\r
12     class PresetLoader\r
13     {\r
14         /// <summary>\r
15         /// This function takes in a Query which has been parsed by QueryParser and\r
16         /// set's all the GUI widgets correctly.\r
17         /// </summary>\r
18         /// <param name="mainWindow"></param>\r
19         /// <param name="presetQuery">The Parsed CLI Query</param>\r
20         /// <param name="name">Name of the preset</param>\r
21         /// <param name="pictureSettings">Save picture settings in the preset</param>\r
22         public static void presetLoader(frmMain mainWindow, QueryParser presetQuery, string name, Boolean pictureSettings)\r
23         {\r
24             // ---------------------------\r
25             // Setup the GUI\r
26             // ---------------------------\r
27 \r
28             #region Source\r
29             // Reset some vaules to stock first to prevent errors.\r
30             mainWindow.check_iPodAtom.CheckState = CheckState.Unchecked;\r
31 \r
32             // Now load all the new settings onto the main window\r
33             if (presetQuery.Format != null)\r
34             {\r
35                 string destination = mainWindow.text_destination.Text;\r
36                 destination = destination.Replace(".mp4", "." + presetQuery.Format);\r
37                 destination = destination.Replace(".m4v", "." + presetQuery.Format);\r
38                 destination = destination.Replace(".mkv", "." + presetQuery.Format);\r
39                 mainWindow.text_destination.Text = destination;\r
40             }\r
41 \r
42             #endregion\r
43 \r
44             #region Destination and Output Settings\r
45 \r
46             if (presetQuery.Format != null)\r
47             {\r
48                 if (presetQuery.Format == "mp4" || presetQuery.Format == "m4v")\r
49                     mainWindow.drop_format.SelectedIndex = 0;\r
50                 else if (presetQuery.Format == "mkv")\r
51                     mainWindow.drop_format.SelectedIndex = 1;\r
52             }\r
53 \r
54             mainWindow.check_iPodAtom.CheckState = presetQuery.IpodAtom ? CheckState.Checked : CheckState.Unchecked;\r
55 \r
56             mainWindow.check_optimiseMP4.CheckState = presetQuery.OptimizeMP4 ? CheckState.Checked : CheckState.Unchecked;\r
57 \r
58             mainWindow.check_largeFile.CheckState = presetQuery.LargeMP4 ? CheckState.Checked : CheckState.Unchecked;\r
59 \r
60             mainWindow.setContainerOpts(); // select the container options according to the selected format\r
61 \r
62             #endregion\r
63 \r
64             #region Picture\r
65 \r
66             if (pictureSettings) // only Load picture settings if the perset requires it\r
67             {\r
68                 mainWindow.PictureSettings.check_autoCrop.Checked = true;\r
69 \r
70                 if (presetQuery.CropValues != null)\r
71                 {\r
72                     int top, bottom, left, right;\r
73                     int.TryParse(presetQuery.CropTop, out top);\r
74                     int.TryParse(presetQuery.CropBottom, out bottom);\r
75                     int.TryParse(presetQuery.CropLeft, out left);\r
76                     int.TryParse(presetQuery.CropRight, out right);\r
77 \r
78                     mainWindow.PictureSettings.check_customCrop.Checked = true;\r
79                     mainWindow.PictureSettings.crop_top.Value = top;\r
80                     mainWindow.PictureSettings.crop_bottom.Value = bottom;\r
81                     mainWindow.PictureSettings.crop_left.Value = left;\r
82                     mainWindow.PictureSettings.crop_right.Value = right;\r
83                 }\r
84             }\r
85 \r
86             // Set the anamorphic mode 0,1,2,3\r
87             mainWindow.PictureSettings.drp_anamorphic.SelectedIndex = presetQuery.AnamorphicMode;\r
88 \r
89             // Aspect Ratio\r
90             mainWindow.PictureSettings.check_KeepAR.CheckState = presetQuery.keepDisplayAsect ? CheckState.Checked : CheckState.Unchecked;\r
91                 \r
92             // Set the Width and height as Required.\r
93             if (presetQuery.Width != 0)\r
94                 mainWindow.PictureSettings.text_width.Value = presetQuery.Width;\r
95 \r
96             if (presetQuery.Height != 0)\r
97                 mainWindow.PictureSettings.text_height.Value = presetQuery.Height;\r
98 \r
99             // Max Width/Height override Width/Height\r
100             if (presetQuery.MaxWidth != 0)\r
101             {\r
102                 mainWindow.PictureSettings.text_width.Value = presetQuery.MaxWidth;\r
103                 mainWindow.PictureSettings.MaximumWidth = presetQuery.MaxWidth;\r
104             }\r
105 \r
106             if (presetQuery.MaxHeight != 0)\r
107             {\r
108                 mainWindow.PictureSettings.text_height.Value = presetQuery.MaxHeight;\r
109                 mainWindow.PictureSettings.MaximumHeight = presetQuery.MaxHeight;\r
110             }\r
111 \r
112             // Case where both height and max height are 0 - For built-in presets\r
113             if (presetQuery.MaxHeight == 0 && presetQuery.Height == 0)\r
114                 mainWindow.PictureSettings.text_height.Value = 0;\r
115 \r
116             if (presetQuery.MaxWidth == 0 && presetQuery.Width == 0)\r
117                 if (mainWindow.selectedTitle != null && mainWindow.selectedTitle.Resolution.Width != 0)\r
118                     mainWindow.PictureSettings.text_width.Value = mainWindow.selectedTitle.Resolution.Width;\r
119 \r
120             mainWindow.PictureSettings.setMax();\r
121 \r
122             // Custom Anamorphic Controls\r
123             mainWindow.PictureSettings.updownDisplayWidth.Text = presetQuery.displayWidthValue.ToString();\r
124             mainWindow.PictureSettings.updownParHeight.Text = presetQuery.pixelAspectWidth.ToString();\r
125             mainWindow.PictureSettings.updownParWidth.Text = presetQuery.pixelAspectHeight.ToString();\r
126             mainWindow.PictureSettings.drp_modulus.SelectedItem = presetQuery.AnamorphicModulus;\r
127 \r
128             #endregion\r
129 \r
130             #region Filters\r
131             mainWindow.Filters.setDecomb(presetQuery.Decomb);\r
132             mainWindow.Filters.setDeInterlace(presetQuery.DeInterlace);\r
133             mainWindow.Filters.setDeNoise(presetQuery.DeNoise);\r
134             mainWindow.Filters.setDeTelecine(presetQuery.DeTelecine);\r
135             mainWindow.Filters.setDeBlock(presetQuery.DeBlock);\r
136             mainWindow.Filters.setGrayScale(presetQuery.Grayscale);\r
137             #endregion\r
138 \r
139             #region Video\r
140             mainWindow.drp_videoEncoder.Text = presetQuery.VideoEncoder;\r
141 \r
142             if (presetQuery.AverageVideoBitrate != null)\r
143             {\r
144                 mainWindow.radio_avgBitrate.Checked = true;\r
145                 mainWindow.text_bitrate.Text = presetQuery.AverageVideoBitrate;\r
146             }\r
147             if (presetQuery.VideoTargetSize != null)\r
148             {\r
149                 mainWindow.radio_targetFilesize.Checked = true;\r
150                 mainWindow.text_filesize.Text = presetQuery.VideoTargetSize;\r
151             }\r
152 \r
153             // Quality\r
154             if (presetQuery.VideoQuality != 0)\r
155             {\r
156                 mainWindow.radio_cq.Checked = true;\r
157                 if (presetQuery.VideoEncoder == "H.264 (x264)")\r
158                 {\r
159                     int value;\r
160                     double x264step = Properties.Settings.Default.x264cqstep;\r
161                     double presetValue = presetQuery.VideoQuality;\r
162 \r
163                     double x = 51 / x264step;\r
164 \r
165                     double calculated = presetValue / x264step;\r
166                     calculated = x - calculated;\r
167 \r
168                     int.TryParse(calculated.ToString(), out value);\r
169 \r
170                     // This will sometimes occur when the preset was generated \r
171                     // with a different granularity, so, round and try again.\r
172                     if (value == 0)\r
173                     {\r
174                         double val = Math.Round(calculated, 0);\r
175                         int.TryParse(val.ToString(), out value);\r
176                     }\r
177                     mainWindow.slider_videoQuality.Value = value;\r
178                 }\r
179                 else\r
180                 {\r
181                     int presetVal;\r
182                     int.TryParse(presetQuery.VideoQuality.ToString(), out presetVal);\r
183                     mainWindow.slider_videoQuality.Value = presetVal;\r
184                 }\r
185             }\r
186 \r
187             mainWindow.check_2PassEncode.CheckState = presetQuery.TwoPass ? CheckState.Checked : CheckState.Unchecked;\r
188 \r
189 \r
190             mainWindow.drp_videoFramerate.Text = presetQuery.VideoFramerate;\r
191 \r
192             mainWindow.check_turbo.CheckState = presetQuery.TurboFirstPass ? CheckState.Checked : CheckState.Unchecked;\r
193 \r
194             #endregion\r
195 \r
196             #region Chapter Markers\r
197 \r
198             if (presetQuery.ChapterMarkers)\r
199             {\r
200                 mainWindow.Check_ChapterMarkers.CheckState = CheckState.Checked;\r
201                 mainWindow.Check_ChapterMarkers.Enabled = true;\r
202             }\r
203             else\r
204                 mainWindow.Check_ChapterMarkers.CheckState = CheckState.Unchecked;\r
205 \r
206             #endregion\r
207 \r
208             #region Audio\r
209             // Clear the audio listing\r
210             mainWindow.AudioSettings.clearAudioList();\r
211 \r
212             if (presetQuery.AudioInformation != null)\r
213                 foreach (AudioTrack track in presetQuery.AudioInformation)\r
214                 {\r
215                     ListViewItem newTrack = new ListViewItem(mainWindow.AudioSettings.getNewID().ToString());\r
216 \r
217                     newTrack.SubItems.Add("Automatic");\r
218                     newTrack.SubItems.Add(track.Encoder);\r
219                     newTrack.SubItems.Add(track.MixDown);\r
220                     newTrack.SubItems.Add(track.SampleRate);\r
221                     if (track.Encoder.Contains("AC3"))\r
222                         newTrack.SubItems.Add("Auto");\r
223                     else\r
224                         newTrack.SubItems.Add(track.Bitrate);\r
225                     newTrack.SubItems.Add(track.DRC);\r
226                     mainWindow.AudioSettings.addTrackForPreset(newTrack);\r
227                 }\r
228             #endregion\r
229 \r
230             #region Other\r
231             mainWindow.x264Panel.x264Query = presetQuery.H264Query;\r
232 \r
233             // Set the preset name\r
234             mainWindow.labelPreset.Text = "Output Settings (Preset: " + name + ")";\r
235             #endregion\r
236         }\r
237     }\r
238 }