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