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