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.Windows.Forms;\r
9 \r
10 namespace Handbrake.Functions\r
11 {\r
12     class PresetLoader\r
13     {\r
14         /// <summary>\r
15         /// This function takes in a Query which has been parsed by QueryParser and\r
16         /// set's all the GUI widgets correctly.\r
17         /// </summary>\r
18         /// <param name="mainWindow"></param>\r
19         /// <param name="presetQuery">The Parsed CLI Query</param>\r
20         /// <param name="name">Name of the preset</param>\r
21         /// <param name="pictureSettings">Save picture settings in the preset</param>\r
22         public static void presetLoader(frmMain mainWindow, QueryParser presetQuery, string name, Boolean pictureSettings)\r
23         {\r
24             // ---------------------------\r
25             // Setup the GUI\r
26             // ---------------------------\r
27 \r
28             #region Source\r
29             // Reset some vaules to stock first to prevent errors.\r
30             mainWindow.check_iPodAtom.CheckState = CheckState.Unchecked;\r
31 \r
32             // Now load all the new settings onto the main window\r
33             if (presetQuery.Format != null)\r
34             {\r
35                 string destination = mainWindow.text_destination.Text;\r
36                 destination = destination.Replace(".mp4", "." + presetQuery.Format);\r
37                 destination = destination.Replace(".m4v", "." + presetQuery.Format);\r
38                 destination = destination.Replace(".mkv", "." + presetQuery.Format);\r
39                 mainWindow.text_destination.Text = destination;\r
40             }\r
41 \r
42             #endregion\r
43 \r
44             #region Destination and Output Settings\r
45 \r
46             if (presetQuery.Format != null)\r
47             {\r
48                 if (presetQuery.Format == "mp4" || presetQuery.Format == "m4v")\r
49                     mainWindow.drop_format.SelectedIndex = 0;\r
50                 else if (presetQuery.Format == "mkv")\r
51                     mainWindow.drop_format.SelectedIndex = 1;\r
52             }\r
53 \r
54             mainWindow.check_iPodAtom.CheckState = presetQuery.IpodAtom ? CheckState.Checked : CheckState.Unchecked;\r
55 \r
56             mainWindow.check_optimiseMP4.CheckState = presetQuery.OptimizeMP4 ? CheckState.Checked : CheckState.Unchecked;\r
57 \r
58             mainWindow.check_largeFile.CheckState = presetQuery.LargeMP4 ? CheckState.Checked : CheckState.Unchecked;\r
59 \r
60             mainWindow.setContainerOpts(); // select the container options according to the selected format\r
61 \r
62             #endregion\r
63 \r
64             #region Picture\r
65 \r
66             if (pictureSettings) // only Load picture settings if the perset requires it\r
67             {\r
68                 mainWindow.PictureSettings.check_autoCrop.Checked = true;\r
69 \r
70                 if (presetQuery.CropValues != null)\r
71                 {\r
72                     int top, bottom, left, right;\r
73                     int.TryParse(presetQuery.CropTop, out top);\r
74                     int.TryParse(presetQuery.CropBottom, out bottom);\r
75                     int.TryParse(presetQuery.CropLeft, out left);\r
76                     int.TryParse(presetQuery.CropRight, out right);\r
77 \r
78                     mainWindow.PictureSettings.check_customCrop.Checked = true;\r
79                     mainWindow.PictureSettings.crop_top.Value = top;\r
80                     mainWindow.PictureSettings.crop_bottom.Value = bottom;\r
81                     mainWindow.PictureSettings.crop_left.Value = left;\r
82                     mainWindow.PictureSettings.crop_right.Value = right;\r
83                 }\r
84             }\r
85 \r
86             // Set the anamorphic mode 0,1,2,3\r
87             mainWindow.PictureSettings.drp_anamorphic.SelectedIndex = presetQuery.AnamorphicMode;\r
88 \r
89             // Aspect Ratio\r
90             mainWindow.PictureSettings.check_KeepAR.CheckState = presetQuery.keepDisplayAsect ? CheckState.Checked : CheckState.Unchecked;\r
91 \r
92             // Reset maxWidth and MaxHeight to 0\r
93             mainWindow.PictureSettings.maxWidth = 0;\r
94             mainWindow.PictureSettings.maxHeight = 0;\r
95                 \r
96             // Set the Width and height as Required.\r
97             if (presetQuery.Width != 0)\r
98                 mainWindow.PictureSettings.text_width.Value = presetQuery.Width;\r
99 \r
100             if (presetQuery.Height != 0)\r
101                 mainWindow.PictureSettings.text_height.Value = presetQuery.Height;\r
102 \r
103             // Max Width/Height override Width/Height\r
104             if (presetQuery.MaxWidth != 0)\r
105             {\r
106                 mainWindow.PictureSettings.text_width.Value = presetQuery.MaxWidth;\r
107                 mainWindow.PictureSettings.maxWidth = presetQuery.MaxWidth;\r
108             }\r
109 \r
110             if (presetQuery.MaxHeight != 0)\r
111             {\r
112                 mainWindow.PictureSettings.text_height.Value = presetQuery.MaxHeight;\r
113                 mainWindow.PictureSettings.maxHeight = presetQuery.MaxHeight;\r
114             }\r
115 \r
116             // Case where both height and max height are 0 - For built-in presets\r
117             if (presetQuery.MaxHeight == 0 && presetQuery.Height == 0)\r
118                 mainWindow.PictureSettings.text_height.Value = 0;\r
119 \r
120             if (presetQuery.MaxWidth == 0 && presetQuery.Width == 0)\r
121                 if (mainWindow.selectedTitle != null && mainWindow.selectedTitle.Resolution.Width != 0)\r
122                     mainWindow.PictureSettings.text_width.Value = mainWindow.selectedTitle.Resolution.Width;\r
123 \r
124             mainWindow.PictureSettings.setMax();\r
125 \r
126             \r
127 \r
128             // Custom Anamorphic Controls\r
129             mainWindow.PictureSettings.txt_displayWidth.Text = presetQuery.displayWidthValue.ToString();\r
130             mainWindow.PictureSettings.txt_parWidth.Text = presetQuery.pixelAspectWidth.ToString();\r
131             mainWindow.PictureSettings.txt_parHeight.Text = presetQuery.pixelAspectHeight.ToString();\r
132             mainWindow.PictureSettings.drop_modulus.SelectedItem = presetQuery.AnamorphicModulus;\r
133 \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             if (presetQuery.VideoQuality != 0)\r
162             {\r
163                 mainWindow.radio_cq.Checked = true;\r
164                 if (presetQuery.VideoEncoder == "H.264 (x264)")\r
165                 {\r
166                     int value;\r
167                     double x264step = Properties.Settings.Default.x264cqstep;\r
168                     double presetValue = presetQuery.VideoQuality;\r
169 \r
170                     double x = 51 / x264step;\r
171 \r
172                     double calculated = presetValue / x264step;\r
173                     calculated = x - calculated;\r
174 \r
175                     int.TryParse(calculated.ToString(), out value);\r
176 \r
177                     // This will sometimes occur when the preset was generated \r
178                     // with a different granularity, so, round and try again.\r
179                     if (value == 0)\r
180                     {\r
181                         double val = Math.Round(calculated, 0);\r
182                         int.TryParse(val.ToString(), out value);\r
183                     }\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 \r
197             mainWindow.drp_videoFramerate.Text = presetQuery.VideoFramerate;\r
198 \r
199             mainWindow.check_turbo.CheckState = presetQuery.TurboFirstPass ? CheckState.Checked : CheckState.Unchecked;\r
200 \r
201             #endregion\r
202 \r
203             #region Chapter Markers\r
204 \r
205             if (presetQuery.ChapterMarkers)\r
206             {\r
207                 mainWindow.Check_ChapterMarkers.CheckState = CheckState.Checked;\r
208                 mainWindow.Check_ChapterMarkers.Enabled = true;\r
209             }\r
210             else\r
211                 mainWindow.Check_ChapterMarkers.CheckState = CheckState.Unchecked;\r
212 \r
213             #endregion\r
214 \r
215             #region Audio\r
216             // Clear the audio listing\r
217             mainWindow.AudioSettings.clearAudioList();\r
218 \r
219             if (presetQuery.AudioInformation != null)\r
220                 foreach (AudioTrack track in presetQuery.AudioInformation)\r
221                 {\r
222                     ListViewItem newTrack = new ListViewItem(mainWindow.AudioSettings.getNewID().ToString());\r
223 \r
224                     newTrack.SubItems.Add("Automatic");\r
225                     newTrack.SubItems.Add(track.Encoder);\r
226                     newTrack.SubItems.Add(track.MixDown);\r
227                     newTrack.SubItems.Add(track.SampleRate);\r
228                     if (track.Encoder.Contains("AC3"))\r
229                         newTrack.SubItems.Add("Auto");\r
230                     else\r
231                         newTrack.SubItems.Add(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.groupBox_output.Text = "Output Settings (Preset: " + name + ")";\r
242             #endregion\r
243         }\r
244     }\r
245 }