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             // 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 ? CheckState.Checked : CheckState.Unchecked;\r
74 \r
75             mainWindow.check_largeFile.CheckState = presetQuery.LargeMP4 ? CheckState.Checked : CheckState.Unchecked;\r
76 \r
77             mainWindow.setContainerOpts(); // select the container options according to the selected format\r
78 \r
79             #endregion\r
80 \r
81             #region Picture\r
82             mainWindow.PictureSettings.check_autoCrop.Checked = true;\r
83             if (pictureSettings) // only Load picture settings if the perset requires it\r
84             {\r
85                 if (presetQuery.CropValues != null)\r
86                 {\r
87                     int top, bottom, left, right;\r
88                     int.TryParse(presetQuery.CropTop, out top);\r
89                     int.TryParse(presetQuery.CropBottom, out bottom);\r
90                     int.TryParse(presetQuery.CropLeft, out left);\r
91                     int.TryParse(presetQuery.CropRight, out right);\r
92 \r
93                     mainWindow.PictureSettings.check_customCrop.Checked = true;\r
94                     mainWindow.PictureSettings.crop_top.Value = top;\r
95                     mainWindow.PictureSettings.crop_bottom.Value = bottom;\r
96                     mainWindow.PictureSettings.crop_left.Value = left;\r
97                     mainWindow.PictureSettings.crop_right.Value = right;\r
98                 }\r
99             }\r
100 \r
101             // Set the anamorphic mode 0,1,2,3\r
102             mainWindow.PictureSettings.drp_anamorphic.SelectedIndex = presetQuery.AnamorphicMode;\r
103 \r
104             // Keep Aspect Ration Anamorphic Setting.\r
105             mainWindow.PictureSettings.check_KeepAR.CheckState = presetQuery.KeepDisplayAsect ? CheckState.Checked : CheckState.Unchecked;\r
106 \r
107             // Set the Width and height as Required.\r
108             if (presetQuery.Width != 0)\r
109                 mainWindow.PictureSettings.text_width.Value = presetQuery.Width;\r
110 \r
111             if (presetQuery.Height != 0)\r
112                 mainWindow.PictureSettings.text_height.Value = presetQuery.Height;\r
113 \r
114             // Max Width/Height override Width/Height\r
115             if (presetQuery.MaxWidth != 0)\r
116                 mainWindow.PictureSettings.text_width.Value = presetQuery.MaxWidth;\r
117 \r
118             if (presetQuery.MaxHeight != 0)\r
119                 mainWindow.PictureSettings.text_height.Value = presetQuery.MaxHeight;\r
120 \r
121             mainWindow.PictureSettings.PresetMaximumResolution = new Size(presetQuery.MaxWidth, presetQuery.MaxHeight);\r
122 \r
123             // Case where both height and max height are 0 - For built-in presets\r
124             if (presetQuery.MaxHeight == 0 && presetQuery.Height == 0)\r
125                 mainWindow.PictureSettings.text_height.Value = 0;\r
126 \r
127             if (presetQuery.MaxWidth == 0 && presetQuery.Width == 0)\r
128                 if (mainWindow.selectedTitle != null && mainWindow.selectedTitle.Resolution.Width != 0)\r
129                     mainWindow.PictureSettings.text_width.Value = mainWindow.selectedTitle.Resolution.Width;\r
130 \r
131             // Aspect Ratio for non anamorphic sources\r
132             if (presetQuery.AnamorphicMode == 0)\r
133                 mainWindow.PictureSettings.check_KeepAR.CheckState = presetQuery.Height == 0 ? CheckState.Checked : CheckState.Unchecked;\r
134 \r
135             // Custom Anamorphic Controls\r
136             mainWindow.PictureSettings.updownDisplayWidth.Text = presetQuery.DisplayWidthValue.ToString();\r
137             mainWindow.PictureSettings.updownParHeight.Text = presetQuery.PixelAspectWidth.ToString();\r
138             mainWindow.PictureSettings.updownParWidth.Text = presetQuery.PixelAspectHeight.ToString();\r
139             mainWindow.PictureSettings.drp_modulus.SelectedItem = presetQuery.AnamorphicModulus;\r
140 \r
141             #endregion\r
142 \r
143             #region Filters\r
144             mainWindow.Filters.SetDecomb(presetQuery.Decomb);\r
145             mainWindow.Filters.SetDeInterlace(presetQuery.DeInterlace);\r
146             mainWindow.Filters.SetDeNoise(presetQuery.DeNoise);\r
147             mainWindow.Filters.SetDeTelecine(presetQuery.DeTelecine);\r
148             mainWindow.Filters.SetDeBlock(presetQuery.DeBlock);\r
149             mainWindow.Filters.SetGrayScale(presetQuery.Grayscale);\r
150             #endregion\r
151 \r
152             #region Video\r
153             mainWindow.drp_videoEncoder.Text = presetQuery.VideoEncoder;\r
154 \r
155             if (presetQuery.AverageVideoBitrate != null)\r
156             {\r
157                 mainWindow.radio_avgBitrate.Checked = true;\r
158                 mainWindow.text_bitrate.Text = presetQuery.AverageVideoBitrate;\r
159             }\r
160             if (presetQuery.VideoTargetSize != null)\r
161             {\r
162                 mainWindow.radio_targetFilesize.Checked = true;\r
163                 mainWindow.text_filesize.Text = presetQuery.VideoTargetSize;\r
164             }\r
165 \r
166             // Quality\r
167 \r
168             if (presetQuery.VideoQuality != -1)\r
169             {\r
170                 mainWindow.radio_cq.Checked = true;\r
171                 if (presetQuery.VideoEncoder == "H.264 (x264)")\r
172                 {\r
173                     double cqStep = Properties.Settings.Default.x264cqstep;\r
174                     int value;\r
175                     double x264Step = cqStep;\r
176                     double presetValue = presetQuery.VideoQuality;\r
177 \r
178                     double x = 51 / x264Step;\r
179 \r
180                     double calculated = presetValue / x264Step;\r
181                     calculated = x - calculated;\r
182 \r
183                     int.TryParse(calculated.ToString(), out value);\r
184 \r
185                     // This will sometimes occur when the preset was generated \r
186                     // with a different granularity, so, round and try again.\r
187                     if (value == 0)\r
188                     {\r
189                         double val = Math.Round(calculated, 0);\r
190                         int.TryParse(val.ToString(), out value);\r
191                     }\r
192                     if (value <= mainWindow.slider_videoQuality.Maximum)\r
193                         mainWindow.slider_videoQuality.Value = value;\r
194                 }\r
195                 else\r
196                 {\r
197                     int presetVal;\r
198                     int.TryParse(presetQuery.VideoQuality.ToString(), out presetVal);\r
199                     if (presetVal > mainWindow.slider_videoQuality.Minimum)\r
200                         mainWindow.slider_videoQuality.Value = presetVal;\r
201                 }\r
202             }\r
203 \r
204             mainWindow.check_2PassEncode.CheckState = presetQuery.TwoPass ? CheckState.Checked : CheckState.Unchecked;\r
205 \r
206             mainWindow.drp_videoFramerate.Text = presetQuery.VideoFramerate;\r
207 \r
208             mainWindow.check_turbo.CheckState = presetQuery.TurboFirstPass ? CheckState.Checked : CheckState.Unchecked;\r
209 \r
210             #endregion\r
211 \r
212             #region Chapter Markers\r
213 \r
214             if (presetQuery.ChapterMarkers)\r
215             {\r
216                 mainWindow.Check_ChapterMarkers.CheckState = CheckState.Checked;\r
217                 mainWindow.Check_ChapterMarkers.Enabled = true;\r
218             }\r
219             else\r
220                 mainWindow.Check_ChapterMarkers.CheckState = CheckState.Unchecked;\r
221 \r
222             #endregion\r
223 \r
224             #region Audio\r
225             mainWindow.AudioSettings.LoadTracks(presetQuery.AudioInformation);\r
226             #endregion\r
227 \r
228             #region Other\r
229             mainWindow.x264Panel.X264Query = presetQuery.H264Query;\r
230 \r
231             // Set the preset name\r
232             mainWindow.labelPreset.Text = "Output Settings (Preset: " + name + ")";\r
233             #endregion\r
234         }\r
235     }\r
236 }