OSDN Git Service

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