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             // Aspect Ratio\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             // Custom Anamorphic Controls\r
126             mainWindow.PictureSettings.updownDisplayWidth.Text = presetQuery.displayWidthValue.ToString();\r
127             mainWindow.PictureSettings.updownParHeight.Text = presetQuery.pixelAspectWidth.ToString();\r
128             mainWindow.PictureSettings.updownParWidth.Text = presetQuery.pixelAspectHeight.ToString();\r
129             mainWindow.PictureSettings.drp_modulus.SelectedItem = presetQuery.AnamorphicModulus;\r
130 \r
131             #endregion\r
132 \r
133             #region Filters\r
134             mainWindow.Filters.setDecomb(presetQuery.Decomb);\r
135             mainWindow.Filters.setDeInterlace(presetQuery.DeInterlace);\r
136             mainWindow.Filters.setDeNoise(presetQuery.DeNoise);\r
137             mainWindow.Filters.setDeTelecine(presetQuery.DeTelecine);\r
138             mainWindow.Filters.setDeBlock(presetQuery.DeBlock);\r
139             mainWindow.Filters.setGrayScale(presetQuery.Grayscale);\r
140             #endregion\r
141 \r
142             #region Video\r
143             mainWindow.drp_videoEncoder.Text = presetQuery.VideoEncoder;\r
144 \r
145             if (presetQuery.AverageVideoBitrate != null)\r
146             {\r
147                 mainWindow.radio_avgBitrate.Checked = true;\r
148                 mainWindow.text_bitrate.Text = presetQuery.AverageVideoBitrate;\r
149             }\r
150             if (presetQuery.VideoTargetSize != null)\r
151             {\r
152                 mainWindow.radio_targetFilesize.Checked = true;\r
153                 mainWindow.text_filesize.Text = presetQuery.VideoTargetSize;\r
154             }\r
155 \r
156             // Quality\r
157 \r
158             if (presetQuery.VideoQuality != -1)\r
159             {\r
160                 mainWindow.radio_cq.Checked = true;\r
161                 if (presetQuery.VideoEncoder == "H.264 (x264)")\r
162                 {\r
163                     double cqStep = Properties.Settings.Default.x264cqstep;\r
164                     int value;\r
165                     double x264step = cqStep;\r
166                     double presetValue = presetQuery.VideoQuality;\r
167 \r
168                     double x = 51/x264step;\r
169 \r
170                     double calculated = presetValue/x264step;\r
171                     calculated = x - calculated;\r
172 \r
173                     int.TryParse(calculated.ToString(), out value);\r
174 \r
175                     // This will sometimes occur when the preset was generated \r
176                     // with a different granularity, so, round and try again.\r
177                     if (value == 0)\r
178                     {\r
179                         double val = Math.Round(calculated, 0);\r
180                         int.TryParse(val.ToString(), out value);\r
181                     }\r
182                     if (value <= mainWindow.slider_videoQuality.Maximum)\r
183                         mainWindow.slider_videoQuality.Value = value;\r
184                 }\r
185                 else\r
186                 {\r
187                     int presetVal;\r
188                     int.TryParse(presetQuery.VideoQuality.ToString(), out presetVal);\r
189                     if (presetVal > mainWindow.slider_videoQuality.Minimum)\r
190                         mainWindow.slider_videoQuality.Value = presetVal;\r
191                 }\r
192             }\r
193 \r
194             mainWindow.check_2PassEncode.CheckState = presetQuery.TwoPass ? CheckState.Checked : CheckState.Unchecked;\r
195 \r
196             mainWindow.drp_videoFramerate.Text = presetQuery.VideoFramerate;\r
197 \r
198             mainWindow.check_turbo.CheckState = presetQuery.TurboFirstPass ? CheckState.Checked : CheckState.Unchecked;\r
199 \r
200             #endregion\r
201 \r
202             #region Chapter Markers\r
203 \r
204             if (presetQuery.ChapterMarkers)\r
205             {\r
206                 mainWindow.Check_ChapterMarkers.CheckState = CheckState.Checked;\r
207                 mainWindow.Check_ChapterMarkers.Enabled = true;\r
208             }\r
209             else\r
210                 mainWindow.Check_ChapterMarkers.CheckState = CheckState.Unchecked;\r
211 \r
212             #endregion\r
213 \r
214             #region Audio\r
215             // Clear the audio listing\r
216             mainWindow.AudioSettings.ClearAudioList();\r
217 \r
218             if (presetQuery.AudioInformation != null)\r
219                 foreach (AudioTrack track in presetQuery.AudioInformation)\r
220                 {\r
221                     ListViewItem newTrack = new ListViewItem(mainWindow.AudioSettings.GetNewID().ToString());\r
222 \r
223                     newTrack.SubItems.Add("Automatic");\r
224                     newTrack.SubItems.Add(track.Encoder);\r
225                     newTrack.SubItems.Add(track.MixDown);\r
226                     newTrack.SubItems.Add(track.SampleRate);\r
227                     if (track.Encoder.Contains("AC3"))\r
228                         newTrack.SubItems.Add("Auto");\r
229                     else\r
230                         newTrack.SubItems.Add(track.Bitrate);\r
231                     newTrack.SubItems.Add(track.DRC);\r
232                     mainWindow.AudioSettings.AddTrackForPreset(newTrack);\r
233                 }\r
234             #endregion\r
235 \r
236             #region Other\r
237             mainWindow.x264Panel.x264Query = presetQuery.H264Query;\r
238 \r
239             // Set the preset name\r
240             mainWindow.labelPreset.Text = "Output Settings (Preset: " + name + ")";\r
241             #endregion\r
242         }\r
243     }\r
244 }