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 \r
77             if (pictureSettings) // only Load picture settings if the perset requires it\r
78             {\r
79                 mainWindow.PictureSettings.check_autoCrop.Checked = true;\r
80 \r
81                 if (presetQuery.CropValues != null)\r
82                 {\r
83                     int top, bottom, left, right;\r
84                     int.TryParse(presetQuery.CropTop, out top);\r
85                     int.TryParse(presetQuery.CropBottom, out bottom);\r
86                     int.TryParse(presetQuery.CropLeft, out left);\r
87                     int.TryParse(presetQuery.CropRight, out right);\r
88 \r
89                     mainWindow.PictureSettings.check_customCrop.Checked = true;\r
90                     mainWindow.PictureSettings.crop_top.Value = top;\r
91                     mainWindow.PictureSettings.crop_bottom.Value = bottom;\r
92                     mainWindow.PictureSettings.crop_left.Value = left;\r
93                     mainWindow.PictureSettings.crop_right.Value = right;\r
94                 }\r
95             }\r
96             \r
97             // Set the anamorphic mode 0,1,2,3\r
98             mainWindow.PictureSettings.drp_anamorphic.SelectedIndex = presetQuery.AnamorphicMode;\r
99 \r
100             // Aspect Ratio\r
101             mainWindow.PictureSettings.check_KeepAR.CheckState = presetQuery.keepDisplayAsect ? CheckState.Checked : CheckState.Unchecked;\r
102                 \r
103             // Set the Width and height as Required.\r
104             if (presetQuery.Width != 0)\r
105                 mainWindow.PictureSettings.text_width.Value = presetQuery.Width;\r
106 \r
107             if (presetQuery.Height != 0)\r
108                 mainWindow.PictureSettings.text_height.Value = presetQuery.Height;\r
109 \r
110             // Max Width/Height override Width/Height\r
111             if (presetQuery.MaxWidth != 0)\r
112                 mainWindow.PictureSettings.text_width.Value = presetQuery.MaxWidth;\r
113 \r
114             if (presetQuery.MaxHeight != 0)\r
115                 mainWindow.PictureSettings.text_height.Value = presetQuery.MaxHeight;\r
116 \r
117             mainWindow.PictureSettings.PresetMaximumResolution = new Size(presetQuery.MaxWidth, presetQuery.MaxHeight);\r
118 \r
119             // Case where both height and max height are 0 - For built-in presets\r
120             if (presetQuery.MaxHeight == 0 && presetQuery.Height == 0)\r
121                 mainWindow.PictureSettings.text_height.Value = 0;\r
122 \r
123             if (presetQuery.MaxWidth == 0 && presetQuery.Width == 0)\r
124                 if (mainWindow.selectedTitle != null && mainWindow.selectedTitle.Resolution.Width != 0)\r
125                     mainWindow.PictureSettings.text_width.Value = mainWindow.selectedTitle.Resolution.Width;\r
126 \r
127             // Custom Anamorphic Controls\r
128             mainWindow.PictureSettings.updownDisplayWidth.Text = presetQuery.displayWidthValue.ToString();\r
129             mainWindow.PictureSettings.updownParHeight.Text = presetQuery.pixelAspectWidth.ToString();\r
130             mainWindow.PictureSettings.updownParWidth.Text = presetQuery.pixelAspectHeight.ToString();\r
131             mainWindow.PictureSettings.drp_modulus.SelectedItem = presetQuery.AnamorphicModulus;\r
132 \r
133             #endregion\r
134 \r
135             #region Filters\r
136             mainWindow.Filters.setDecomb(presetQuery.Decomb);\r
137             mainWindow.Filters.setDeInterlace(presetQuery.DeInterlace);\r
138             mainWindow.Filters.setDeNoise(presetQuery.DeNoise);\r
139             mainWindow.Filters.setDeTelecine(presetQuery.DeTelecine);\r
140             mainWindow.Filters.setDeBlock(presetQuery.DeBlock);\r
141             mainWindow.Filters.setGrayScale(presetQuery.Grayscale);\r
142             #endregion\r
143 \r
144             #region Video\r
145             mainWindow.drp_videoEncoder.Text = presetQuery.VideoEncoder;\r
146 \r
147             if (presetQuery.AverageVideoBitrate != null)\r
148             {\r
149                 mainWindow.radio_avgBitrate.Checked = true;\r
150                 mainWindow.text_bitrate.Text = presetQuery.AverageVideoBitrate;\r
151             }\r
152             if (presetQuery.VideoTargetSize != null)\r
153             {\r
154                 mainWindow.radio_targetFilesize.Checked = true;\r
155                 mainWindow.text_filesize.Text = presetQuery.VideoTargetSize;\r
156             }\r
157 \r
158             // Quality\r
159             if (presetQuery.VideoQuality != 0)\r
160             {\r
161                 mainWindow.radio_cq.Checked = true;\r
162                 if (presetQuery.VideoEncoder == "H.264 (x264)")\r
163                 {\r
164                     double cqStep = Properties.Settings.Default.x264cqstep;\r
165                     int value;\r
166                     double x264step = cqStep;\r
167                     double presetValue = presetQuery.VideoQuality;\r
168 \r
169                     double x = 51 / x264step;\r
170 \r
171                     double calculated = presetValue / x264step;\r
172                     calculated = x - calculated;\r
173 \r
174                     int.TryParse(calculated.ToString(), out value);\r
175 \r
176                     // This will sometimes occur when the preset was generated \r
177                     // with a different granularity, so, round and try again.\r
178                     if (value == 0)\r
179                     {\r
180                         double val = Math.Round(calculated, 0);\r
181                         int.TryParse(val.ToString(), out value);\r
182                     }\r
183                     if (value < mainWindow.slider_videoQuality.Maximum)\r
184                         mainWindow.slider_videoQuality.Value = value;\r
185                 }\r
186                 else\r
187                 {\r
188                     int presetVal;\r
189                     int.TryParse(presetQuery.VideoQuality.ToString(), out presetVal);\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 }