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