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 using Handbrake.Model;\r
11 \r
12 namespace Handbrake.Functions\r
13 {\r
14     class PresetLoader\r
15     {\r
16         /// <summary>\r
17         /// This function takes in a Query which has been parsed by QueryParser and\r
18         /// set's all the GUI widgets correctly.\r
19         /// </summary>\r
20         /// <param name="mainWindow"></param>\r
21         /// <param name="presetQuery">The Parsed CLI Query</param>\r
22         /// <param name="name">Name of the preset</param>\r
23         /// <param name="pictureSettings">Save picture settings in the preset</param>\r
24         public static void LoadPreset(frmMain mainWindow, QueryParser presetQuery, string name, Boolean pictureSettings)\r
25         {\r
26             #region Source\r
27             // Reset some vaules to stock first to prevent errors.\r
28             mainWindow.check_iPodAtom.CheckState = CheckState.Unchecked;\r
29 \r
30             // Now load all the new settings onto the main window\r
31             if (presetQuery.Format != null)\r
32             {\r
33                 string destination = mainWindow.text_destination.Text;\r
34                 destination = destination.Replace(".mp4", "." + presetQuery.Format);\r
35                 destination = destination.Replace(".m4v", "." + presetQuery.Format);\r
36                 destination = destination.Replace(".mkv", "." + presetQuery.Format);\r
37                 mainWindow.text_destination.Text = destination;\r
38             }\r
39 \r
40             #endregion\r
41 \r
42             #region Destination and Output Settings\r
43 \r
44             if (presetQuery.Format != null)\r
45             {\r
46                 if (presetQuery.Format == "mp4" || presetQuery.Format == "m4v")\r
47                 {\r
48                     if (mainWindow.drop_format.SelectedIndex == 0)\r
49                         mainWindow.SetExtension(".mp4");\r
50                     else\r
51                         mainWindow.drop_format.SelectedIndex = 0;\r
52                 }\r
53                 else if (presetQuery.Format == "mkv")\r
54                 {\r
55                     if (mainWindow.drop_format.SelectedIndex == 1)\r
56                         mainWindow.SetExtension(".mkv");\r
57                     else\r
58                         mainWindow.drop_format.SelectedIndex = 1;\r
59                 }\r
60             }\r
61 \r
62             mainWindow.check_iPodAtom.CheckState = presetQuery.IpodAtom ? CheckState.Checked : CheckState.Unchecked;\r
63 \r
64             mainWindow.check_optimiseMP4.CheckState = presetQuery.OptimizeMP4 ? CheckState.Checked : CheckState.Unchecked;\r
65 \r
66             mainWindow.check_largeFile.CheckState = presetQuery.LargeMP4 ? CheckState.Checked : CheckState.Unchecked;\r
67 \r
68             mainWindow.setContainerOpts(); // select the container options according to the selected format\r
69 \r
70             #endregion\r
71 \r
72             #region Picture\r
73             mainWindow.PictureSettings.check_autoCrop.Checked = true;\r
74             if (pictureSettings) // only Load picture settings if the perset requires it\r
75             {\r
76                 if (presetQuery.CropValues != null)\r
77                 {\r
78                     int top, bottom, left, right;\r
79                     int.TryParse(presetQuery.CropTop, out top);\r
80                     int.TryParse(presetQuery.CropBottom, out bottom);\r
81                     int.TryParse(presetQuery.CropLeft, out left);\r
82                     int.TryParse(presetQuery.CropRight, out right);\r
83 \r
84                     mainWindow.PictureSettings.check_customCrop.Checked = true;\r
85                     mainWindow.PictureSettings.crop_top.Value = top;\r
86                     mainWindow.PictureSettings.crop_bottom.Value = bottom;\r
87                     mainWindow.PictureSettings.crop_left.Value = left;\r
88                     mainWindow.PictureSettings.crop_right.Value = right;\r
89                 }\r
90             }\r
91 \r
92             // Set the anamorphic mode 0,1,2,3\r
93             mainWindow.PictureSettings.drp_anamorphic.SelectedIndex = presetQuery.AnamorphicMode;\r
94 \r
95             // Keep Aspect Ration Anamorphic Setting.\r
96             mainWindow.PictureSettings.check_KeepAR.CheckState = presetQuery.KeepDisplayAsect ? CheckState.Checked : CheckState.Unchecked;\r
97 \r
98             // Set the Width and height as Required.\r
99             if (presetQuery.Width != 0)\r
100                 mainWindow.PictureSettings.text_width.Value = presetQuery.Width;\r
101 \r
102             if (presetQuery.Height != 0)\r
103                 mainWindow.PictureSettings.text_height.Value = presetQuery.Height;\r
104 \r
105             // Max Width/Height override Width/Height\r
106             if (presetQuery.MaxWidth != 0)\r
107                 mainWindow.PictureSettings.text_width.Value = presetQuery.MaxWidth;\r
108 \r
109             if (presetQuery.MaxHeight != 0)\r
110                 mainWindow.PictureSettings.text_height.Value = presetQuery.MaxHeight;\r
111 \r
112             mainWindow.PictureSettings.PresetMaximumResolution = new Size(presetQuery.MaxWidth, presetQuery.MaxHeight);\r
113 \r
114             // Case where both height and max height are 0 - For built-in presets\r
115             if (presetQuery.MaxHeight == 0 && presetQuery.Height == 0)\r
116                 mainWindow.PictureSettings.text_height.Value = 0;\r
117 \r
118             if (presetQuery.MaxWidth == 0 && presetQuery.Width == 0)\r
119                 if (mainWindow.selectedTitle != null && mainWindow.selectedTitle.Resolution.Width != 0)\r
120                     mainWindow.PictureSettings.text_width.Value = mainWindow.selectedTitle.Resolution.Width;\r
121 \r
122             // Aspect Ratio for non anamorphic sources\r
123             if (presetQuery.AnamorphicMode == 0)\r
124                 mainWindow.PictureSettings.check_KeepAR.CheckState = presetQuery.Height == 0 ? CheckState.Checked : CheckState.Unchecked;\r
125 \r
126             // Custom Anamorphic Controls\r
127             mainWindow.PictureSettings.updownDisplayWidth.Text = presetQuery.DisplayWidthValue.ToString();\r
128             mainWindow.PictureSettings.updownParHeight.Text = presetQuery.PixelAspectWidth.ToString();\r
129             mainWindow.PictureSettings.updownParWidth.Text = presetQuery.PixelAspectHeight.ToString();\r
130             mainWindow.PictureSettings.drp_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 \r
159             if (presetQuery.VideoQuality != -1)\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                     if (presetVal > mainWindow.slider_videoQuality.Minimum)\r
191                         mainWindow.slider_videoQuality.Value = presetVal;\r
192                 }\r
193             }\r
194 \r
195             mainWindow.check_2PassEncode.CheckState = presetQuery.TwoPass ? CheckState.Checked : CheckState.Unchecked;\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             mainWindow.AudioSettings.LoadTracks(presetQuery.AudioInformation);\r
217             #endregion\r
218 \r
219             #region Other\r
220             mainWindow.x264Panel.x264Query = presetQuery.H264Query;\r
221 \r
222             // Set the preset name\r
223             mainWindow.labelPreset.Text = "Output Settings (Preset: " + name + ")";\r
224             #endregion\r
225         }\r
226     }\r
227 }