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                     mainWindow.drop_format.SelectedIndex = 0;\r
51                 else if (presetQuery.Format == "mkv")\r
52                     mainWindow.drop_format.SelectedIndex = 1;\r
53             }\r
54 \r
55             mainWindow.check_iPodAtom.CheckState = presetQuery.IpodAtom ? CheckState.Checked : CheckState.Unchecked;\r
56 \r
57             mainWindow.check_optimiseMP4.CheckState = presetQuery.OptimizeMP4 ? CheckState.Checked : CheckState.Unchecked;\r
58 \r
59             mainWindow.check_largeFile.CheckState = presetQuery.LargeMP4 ? CheckState.Checked : CheckState.Unchecked;\r
60 \r
61             mainWindow.setContainerOpts(); // select the container options according to the selected format\r
62 \r
63             #endregion\r
64 \r
65             #region Picture\r
66 \r
67             if (pictureSettings) // only Load picture settings if the perset requires it\r
68             {\r
69                 mainWindow.PictureSettings.check_autoCrop.Checked = true;\r
70 \r
71                 if (presetQuery.CropValues != null)\r
72                 {\r
73                     int top, bottom, left, right;\r
74                     int.TryParse(presetQuery.CropTop, out top);\r
75                     int.TryParse(presetQuery.CropBottom, out bottom);\r
76                     int.TryParse(presetQuery.CropLeft, out left);\r
77                     int.TryParse(presetQuery.CropRight, out right);\r
78 \r
79                     mainWindow.PictureSettings.check_customCrop.Checked = true;\r
80                     mainWindow.PictureSettings.crop_top.Value = top;\r
81                     mainWindow.PictureSettings.crop_bottom.Value = bottom;\r
82                     mainWindow.PictureSettings.crop_left.Value = left;\r
83                     mainWindow.PictureSettings.crop_right.Value = right;\r
84                 }\r
85             }\r
86             \r
87             // Set the anamorphic mode 0,1,2,3\r
88             mainWindow.PictureSettings.drp_anamorphic.SelectedIndex = presetQuery.AnamorphicMode;\r
89 \r
90             // Aspect Ratio\r
91             mainWindow.PictureSettings.check_KeepAR.CheckState = presetQuery.keepDisplayAsect ? CheckState.Checked : CheckState.Unchecked;\r
92                 \r
93             // Set the Width and height as Required.\r
94             if (presetQuery.Width != 0)\r
95                 mainWindow.PictureSettings.text_width.Value = presetQuery.Width;\r
96 \r
97             if (presetQuery.Height != 0)\r
98                 mainWindow.PictureSettings.text_height.Value = presetQuery.Height;\r
99 \r
100             // Max Width/Height override Width/Height\r
101             if (presetQuery.MaxWidth != 0)\r
102                 mainWindow.PictureSettings.text_width.Value = presetQuery.MaxWidth;\r
103 \r
104             if (presetQuery.MaxHeight != 0)\r
105                 mainWindow.PictureSettings.text_height.Value = presetQuery.MaxHeight;\r
106 \r
107             mainWindow.PictureSettings.PresetMaximumResolution = new Size(presetQuery.MaxWidth, presetQuery.MaxHeight);\r
108 \r
109             // Case where both height and max height are 0 - For built-in presets\r
110             if (presetQuery.MaxHeight == 0 && presetQuery.Height == 0)\r
111                 mainWindow.PictureSettings.text_height.Value = 0;\r
112 \r
113             if (presetQuery.MaxWidth == 0 && presetQuery.Width == 0)\r
114                 if (mainWindow.selectedTitle != null && mainWindow.selectedTitle.Resolution.Width != 0)\r
115                     mainWindow.PictureSettings.text_width.Value = mainWindow.selectedTitle.Resolution.Width;\r
116 \r
117             // Custom Anamorphic Controls\r
118             mainWindow.PictureSettings.updownDisplayWidth.Text = presetQuery.displayWidthValue.ToString();\r
119             mainWindow.PictureSettings.updownParHeight.Text = presetQuery.pixelAspectWidth.ToString();\r
120             mainWindow.PictureSettings.updownParWidth.Text = presetQuery.pixelAspectHeight.ToString();\r
121             mainWindow.PictureSettings.drp_modulus.SelectedItem = presetQuery.AnamorphicModulus;\r
122 \r
123             #endregion\r
124 \r
125             #region Filters\r
126             mainWindow.Filters.setDecomb(presetQuery.Decomb);\r
127             mainWindow.Filters.setDeInterlace(presetQuery.DeInterlace);\r
128             mainWindow.Filters.setDeNoise(presetQuery.DeNoise);\r
129             mainWindow.Filters.setDeTelecine(presetQuery.DeTelecine);\r
130             mainWindow.Filters.setDeBlock(presetQuery.DeBlock);\r
131             mainWindow.Filters.setGrayScale(presetQuery.Grayscale);\r
132             #endregion\r
133 \r
134             #region Video\r
135             mainWindow.drp_videoEncoder.Text = presetQuery.VideoEncoder;\r
136 \r
137             if (presetQuery.AverageVideoBitrate != null)\r
138             {\r
139                 mainWindow.radio_avgBitrate.Checked = true;\r
140                 mainWindow.text_bitrate.Text = presetQuery.AverageVideoBitrate;\r
141             }\r
142             if (presetQuery.VideoTargetSize != null)\r
143             {\r
144                 mainWindow.radio_targetFilesize.Checked = true;\r
145                 mainWindow.text_filesize.Text = presetQuery.VideoTargetSize;\r
146             }\r
147 \r
148             // Quality\r
149             if (presetQuery.VideoQuality != 0)\r
150             {\r
151                 mainWindow.radio_cq.Checked = true;\r
152                 if (presetQuery.VideoEncoder == "H.264 (x264)")\r
153                 {\r
154                     double cqStep;\r
155                     double.TryParse(Properties.Settings.Default.x264cqstep, out cqStep);\r
156                     int value;\r
157                     double x264step = cqStep;\r
158                     double presetValue = presetQuery.VideoQuality;\r
159 \r
160                     double x = 51 / x264step;\r
161 \r
162                     double calculated = presetValue / x264step;\r
163                     calculated = x - calculated;\r
164 \r
165                     int.TryParse(calculated.ToString(), out value);\r
166 \r
167                     // This will sometimes occur when the preset was generated \r
168                     // with a different granularity, so, round and try again.\r
169                     if (value == 0)\r
170                     {\r
171                         double val = Math.Round(calculated, 0);\r
172                         int.TryParse(val.ToString(), out value);\r
173                     }\r
174                     if (value < mainWindow.slider_videoQuality.Maximum)\r
175                         mainWindow.slider_videoQuality.Value = value;\r
176                 }\r
177                 else\r
178                 {\r
179                     int presetVal;\r
180                     int.TryParse(presetQuery.VideoQuality.ToString(), out presetVal);\r
181                     mainWindow.slider_videoQuality.Value = presetVal;\r
182                 }\r
183             }\r
184 \r
185             mainWindow.check_2PassEncode.CheckState = presetQuery.TwoPass ? CheckState.Checked : CheckState.Unchecked;\r
186 \r
187 \r
188             mainWindow.drp_videoFramerate.Text = presetQuery.VideoFramerate;\r
189 \r
190             mainWindow.check_turbo.CheckState = presetQuery.TurboFirstPass ? CheckState.Checked : CheckState.Unchecked;\r
191 \r
192             #endregion\r
193 \r
194             #region Chapter Markers\r
195 \r
196             if (presetQuery.ChapterMarkers)\r
197             {\r
198                 mainWindow.Check_ChapterMarkers.CheckState = CheckState.Checked;\r
199                 mainWindow.Check_ChapterMarkers.Enabled = true;\r
200             }\r
201             else\r
202                 mainWindow.Check_ChapterMarkers.CheckState = CheckState.Unchecked;\r
203 \r
204             #endregion\r
205 \r
206             #region Audio\r
207             // Clear the audio listing\r
208             mainWindow.AudioSettings.ClearAudioList();\r
209 \r
210             if (presetQuery.AudioInformation != null)\r
211                 foreach (AudioTrack track in presetQuery.AudioInformation)\r
212                 {\r
213                     ListViewItem newTrack = new ListViewItem(mainWindow.AudioSettings.GetNewID().ToString());\r
214 \r
215                     newTrack.SubItems.Add("Automatic");\r
216                     newTrack.SubItems.Add(track.Encoder);\r
217                     newTrack.SubItems.Add(track.MixDown);\r
218                     newTrack.SubItems.Add(track.SampleRate);\r
219                     if (track.Encoder.Contains("AC3"))\r
220                         newTrack.SubItems.Add("Auto");\r
221                     else\r
222                         newTrack.SubItems.Add(track.Bitrate);\r
223                     newTrack.SubItems.Add(track.DRC);\r
224                     mainWindow.AudioSettings.AddTrackForPreset(newTrack);\r
225                 }\r
226             #endregion\r
227 \r
228             #region Other\r
229             mainWindow.x264Panel.x264Query = presetQuery.H264Query;\r
230 \r
231             // Set the preset name\r
232             mainWindow.labelPreset.Text = "Output Settings (Preset: " + name + ")";\r
233             #endregion\r
234         }\r
235     }\r
236 }