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