OSDN Git Service

LinGui: make Help->Guide work on windows/mingw
[handbrake-jp/handbrake-jp-git.git] / win / C# / Functions / PresetLoader.cs
index f0c5223..f10bd00 100644 (file)
@@ -1,31 +1,38 @@
 /*  PresetLoader.cs $\r
-       \r
-          This file is part of the HandBrake source code.\r
-          Homepage: <http://handbrake.fr>.\r
-          It may be used under the terms of the GNU General Public License. */\r
-\r
-using System;\r
-using System.Windows.Forms;\r
+    This file is part of the HandBrake source code.\r
+    Homepage: <http://handbrake.fr>.\r
+    It may be used under the terms of the GNU General Public License. */\r
 \r
 namespace Handbrake.Functions\r
 {\r
-    class PresetLoader\r
+    using System.Drawing;\r
+    using System.Windows.Forms;\r
+\r
+    /// <summary>\r
+    /// Load a preset into the main Window\r
+    /// </summary>\r
+    public class PresetLoader\r
     {\r
         /// <summary>\r
         /// This function takes in a Query which has been parsed by QueryParser and\r
         /// set's all the GUI widgets correctly.\r
         /// </summary>\r
-        /// <param name="mainWindow"></param>\r
-        /// <param name="presetQuery">The Parsed CLI Query</param>\r
-        /// <param name="name">Name of the preset</param>\r
-        /// <param name="pictureSettings">Save picture settings in the preset</param>\r
-        public static void presetLoader(frmMain mainWindow, QueryParser presetQuery, string name, Boolean pictureSettings)\r
+        /// <param name="mainWindow">\r
+        /// FrmMain window\r
+        /// </param>\r
+        /// <param name="presetQuery">\r
+        /// The Parsed CLI Query\r
+        /// </param>\r
+        /// <param name="name">\r
+        /// Name of the preset\r
+        /// </param>\r
+        /// <param name="pictureSettings">\r
+        /// Save picture settings in the preset\r
+        /// </param>\r
+        public static void LoadPreset(frmMain mainWindow, QueryParser presetQuery, string name, bool pictureSettings)\r
         {\r
-            // ---------------------------\r
-            // Setup the GUI\r
-            // ---------------------------\r
-\r
             #region Source\r
+\r
             // Reset some vaules to stock first to prevent errors.\r
             mainWindow.check_iPodAtom.CheckState = CheckState.Unchecked;\r
 \r
@@ -45,20 +52,27 @@ namespace Handbrake.Functions
 \r
             if (presetQuery.Format != null)\r
             {\r
-                if (presetQuery.Format == "mp4")\r
-                    mainWindow.drop_format.SelectedIndex = 0;\r
-                else if (presetQuery.Format == "m4v")\r
-                    mainWindow.drop_format.SelectedIndex = 1;\r
+                if (presetQuery.Format == "mp4" || presetQuery.Format == "m4v")\r
+                {\r
+                    if (mainWindow.drop_format.SelectedIndex == 0)\r
+                        mainWindow.SetExtension(".mp4");\r
+                    else\r
+                        mainWindow.drop_format.SelectedIndex = 0;\r
+                }\r
                 else if (presetQuery.Format == "mkv")\r
-                    mainWindow.drop_format.SelectedIndex = 2;\r
-\r
-                if (presetQuery.ChapterMarkers && presetQuery.Format == "mp4")\r
-                    mainWindow.drop_format.SelectedIndex = 1;\r
+                {\r
+                    if (mainWindow.drop_format.SelectedIndex == 1)\r
+                        mainWindow.SetExtension(".mkv");\r
+                    else\r
+                        mainWindow.drop_format.SelectedIndex = 1;\r
+                }\r
             }\r
 \r
             mainWindow.check_iPodAtom.CheckState = presetQuery.IpodAtom ? CheckState.Checked : CheckState.Unchecked;\r
 \r
-            mainWindow.check_optimiseMP4.CheckState = presetQuery.OptimizeMP4 ? CheckState.Checked : CheckState.Unchecked;\r
+            mainWindow.check_optimiseMP4.CheckState = presetQuery.OptimizeMP4\r
+                                                          ? CheckState.Checked\r
+                                                          : CheckState.Unchecked;\r
 \r
             mainWindow.check_largeFile.CheckState = presetQuery.LargeMP4 ? CheckState.Checked : CheckState.Unchecked;\r
 \r
@@ -67,14 +81,11 @@ namespace Handbrake.Functions
             #endregion\r
 \r
             #region Picture\r
-            mainWindow.pictureSettings.check_autoCrop.Checked = true;\r
-            if (presetQuery.CropBottom == "0" && presetQuery.CropTop == "0")\r
-                if (presetQuery.CropLeft == "0" && presetQuery.CropRight == "0")\r
-                    mainWindow.pictureSettings.check_customCrop.Checked = true;\r
 \r
-            if (pictureSettings)\r
+            mainWindow.PictureSettings.check_autoCrop.Checked = true;\r
+            if (pictureSettings) // only Load picture settings if the perset requires it\r
             {\r
-                if (presetQuery.CropTop != null)\r
+                if (presetQuery.CropValues != null)\r
                 {\r
                     int top, bottom, left, right;\r
                     int.TryParse(presetQuery.CropTop, out top);\r
@@ -82,75 +93,73 @@ namespace Handbrake.Functions
                     int.TryParse(presetQuery.CropLeft, out left);\r
                     int.TryParse(presetQuery.CropRight, out right);\r
 \r
-                    mainWindow.pictureSettings.check_customCrop.Checked = true;\r
-                    mainWindow.pictureSettings.crop_top.Value = top;\r
-                    mainWindow.pictureSettings.crop_bottom.Value = bottom;\r
-                    mainWindow.pictureSettings.crop_left.Value = left;\r
-                    mainWindow.pictureSettings.crop_right.Value = right;\r
+                    mainWindow.PictureSettings.check_customCrop.Checked = true;\r
+                    mainWindow.PictureSettings.crop_top.Value = top;\r
+                    mainWindow.PictureSettings.crop_bottom.Value = bottom;\r
+                    mainWindow.PictureSettings.crop_left.Value = left;\r
+                    mainWindow.PictureSettings.crop_right.Value = right;\r
                 }\r
-\r
-                if (presetQuery.Width != 0)\r
-                    mainWindow.pictureSettings.text_width.Text = presetQuery.Width.ToString();\r
-\r
-                if (presetQuery.Height != 0)\r
-                    mainWindow.pictureSettings.text_height.Text = presetQuery.Height.ToString();\r
             }\r
 \r
             // Set the anamorphic mode 0,1,2,3\r
-            mainWindow.pictureSettings.drp_anamorphic.SelectedIndex = presetQuery.AnamorphicMode;\r
+            mainWindow.PictureSettings.drp_anamorphic.SelectedIndex = presetQuery.AnamorphicMode;\r
 \r
+            // Keep Aspect Ration Anamorphic Setting.\r
+            mainWindow.PictureSettings.check_KeepAR.CheckState = presetQuery.KeepDisplayAsect\r
+                                                                     ? CheckState.Checked\r
+                                                                     : CheckState.Unchecked;\r
 \r
-            // Custom Anamorphic Controls\r
-            mainWindow.pictureSettings.check_KeepAR.CheckState = presetQuery.keepDisplayAsect ? CheckState.Checked : CheckState.Unchecked;\r
+            // Set the Width and height as Required.\r
+            if (presetQuery.Width != 0)\r
+                mainWindow.PictureSettings.text_width.Value = presetQuery.Width;\r
 \r
-            mainWindow.pictureSettings.txt_displayWidth.Text = presetQuery.displayWidthValue.ToString();\r
-            mainWindow.pictureSettings.txt_parWidth.Text = presetQuery.pixelAspectWidth.ToString();\r
-            mainWindow.pictureSettings.txt_parHeight.Text = presetQuery.pixelAspectHeight.ToString();\r
-            mainWindow.pictureSettings.drop_modulus.SelectedItem = presetQuery.AnamorphicModulus;\r
+            if (presetQuery.Height != 0)\r
+                mainWindow.PictureSettings.text_height.Value = presetQuery.Height;\r
 \r
\r
-            // Set the public max width and max height varibles in frmMain\r
-            // These are used by the query generator to determine if it should use -X or -w  / -Y or -h\r
+            // Max Width/Height override Width/Height\r
             if (presetQuery.MaxWidth != 0)\r
-            {\r
-                mainWindow.pictureSettings.text_width.Text = presetQuery.MaxWidth.ToString();\r
-                mainWindow.maxWidth = presetQuery.MaxWidth;\r
-            }\r
+                mainWindow.PictureSettings.text_width.Value = presetQuery.MaxWidth;\r
 \r
             if (presetQuery.MaxHeight != 0)\r
-            {\r
-                mainWindow.pictureSettings.text_height.Text = presetQuery.MaxHeight.ToString();\r
-                mainWindow.maxHeight = presetQuery.MaxHeight;\r
-            }\r
+                mainWindow.PictureSettings.text_height.Value = presetQuery.MaxHeight;\r
 \r
+            mainWindow.PictureSettings.PresetMaximumResolution = new Size(presetQuery.MaxWidth, presetQuery.MaxHeight);\r
 \r
-            #endregion\r
+            // Case where both height and max height are 0 - For built-in presets\r
+            if (presetQuery.MaxHeight == 0 && presetQuery.Height == 0)\r
+                mainWindow.PictureSettings.text_height.Value = 0;\r
 \r
-            #region Filters\r
+            if (presetQuery.MaxWidth == 0 && presetQuery.Width == 0)\r
+                if (mainWindow.selectedTitle != null && mainWindow.selectedTitle.Resolution.Width != 0)\r
+                    mainWindow.PictureSettings.text_width.Value = mainWindow.selectedTitle.Resolution.Width;\r
 \r
-            mainWindow.ctl_decomb.setOption(presetQuery.Decomb);\r
+            // Aspect Ratio for non anamorphic sources\r
+            if (presetQuery.AnamorphicMode == 0)\r
+                mainWindow.PictureSettings.check_KeepAR.CheckState = presetQuery.Height == 0\r
+                                                                         ? CheckState.Checked\r
+                                                                         : CheckState.Unchecked;\r
 \r
-            if (mainWindow.ctl_decomb.getDropValue == "Off")\r
-                mainWindow.ctl_deinterlace.setOption(presetQuery.DeInterlace);\r
-            else\r
-                mainWindow.ctl_deinterlace.setOption("None"); // Don't want decomb and deinterlace on at the same time\r
+            // Custom Anamorphic Controls\r
+            mainWindow.PictureSettings.updownDisplayWidth.Text = presetQuery.DisplayWidthValue.ToString();\r
+            mainWindow.PictureSettings.updownParHeight.Text = presetQuery.PixelAspectHeight.ToString();\r
+            mainWindow.PictureSettings.updownParWidth.Text = presetQuery.PixelAspectWidth.ToString();\r
+            mainWindow.PictureSettings.drp_modulus.SelectedItem = presetQuery.AnamorphicModulus.ToString();\r
 \r
-            mainWindow.ctl_denoise.setOption(presetQuery.DeNoise);\r
-            mainWindow.ctl_detelecine.setOption(presetQuery.DeTelecine);\r
+            #endregion\r
+\r
+            #region Filters\r
+\r
+            mainWindow.Filters.SetDecomb(presetQuery.Decomb);\r
+            mainWindow.Filters.SetDeInterlace(presetQuery.DeInterlace);\r
+            mainWindow.Filters.SetDeNoise(presetQuery.DeNoise);\r
+            mainWindow.Filters.SetDeTelecine(presetQuery.DeTelecine);\r
+            mainWindow.Filters.SetDeBlock(presetQuery.DeBlock);\r
+            mainWindow.Filters.SetGrayScale(presetQuery.Grayscale);\r
 \r
-            if (presetQuery.DeBlock != 0)\r
-            {\r
-                mainWindow.slider_deblock.Value = presetQuery.DeBlock;\r
-                mainWindow.lbl_deblockVal.Text = presetQuery.DeBlock.ToString();\r
-            }\r
-            else\r
-            {\r
-                mainWindow.slider_deblock.Value = 4;\r
-                mainWindow.lbl_deblockVal.Text = "Off";\r
-            }\r
             #endregion\r
 \r
             #region Video\r
+\r
             mainWindow.drp_videoEncoder.Text = presetQuery.VideoEncoder;\r
 \r
             if (presetQuery.AverageVideoBitrate != null)\r
@@ -165,50 +174,14 @@ namespace Handbrake.Functions
             }\r
 \r
             // Quality\r
-            if (presetQuery.VideoQuality != 0)\r
+            if (presetQuery.VideoQuality != -1)\r
             {\r
                 mainWindow.radio_cq.Checked = true;\r
-                if (presetQuery.VideoEncoder == "H.264 (x264)")\r
-                {\r
-                    int value;\r
-                    System.Globalization.CultureInfo culture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");\r
-\r
-                    double x264step;\r
-                    double presetValue = presetQuery.VideoQuality;\r
-                    double.TryParse(Properties.Settings.Default.x264cqstep,\r
-                                    System.Globalization.NumberStyles.Number,\r
-                                    culture,\r
-                                    out x264step);\r
-\r
-                    double x = 51 / x264step;\r
-\r
-                    double calculated = presetValue / x264step;\r
-                    calculated = x - calculated;\r
-\r
-                    int.TryParse(calculated.ToString(), out value);\r
-\r
-                    // This will sometimes occur when the preset was generated \r
-                    // with a different granularity, so, round and try again.\r
-                    if (value == 0)\r
-                    {\r
-                        double val = Math.Round(calculated, 0);\r
-                        int.TryParse(val.ToString(), out value);\r
-                    }\r
-\r
-                    mainWindow.slider_videoQuality.Value = value;\r
-                }\r
-                else\r
-                {\r
-                    int presetVal;\r
-                    int.TryParse(presetQuery.VideoQuality.ToString(), out presetVal);\r
-                    mainWindow.slider_videoQuality.Value = presetVal;\r
-                }\r
+                mainWindow.slider_videoQuality.Value = QualityToSliderValue(presetQuery.VideoEncoder, presetQuery.VideoQuality);\r
             }\r
 \r
             mainWindow.check_2PassEncode.CheckState = presetQuery.TwoPass ? CheckState.Checked : CheckState.Unchecked;\r
 \r
-            mainWindow.check_grayscale.CheckState = presetQuery.Grayscale ? CheckState.Checked : CheckState.Unchecked;\r
-\r
             mainWindow.drp_videoFramerate.Text = presetQuery.VideoFramerate;\r
 \r
             mainWindow.check_turbo.CheckState = presetQuery.TurboFirstPass ? CheckState.Checked : CheckState.Unchecked;\r
@@ -228,42 +201,45 @@ namespace Handbrake.Functions
             #endregion\r
 \r
             #region Audio\r
-            // Clear the audio listing\r
-            mainWindow.audioPanel.clearAudioList();\r
-\r
-            if (presetQuery.AudioInformation != null)\r
-                foreach (AudioTrack track in presetQuery.AudioInformation)\r
-                {\r
-                    ListViewItem newTrack = new ListViewItem(mainWindow.audioPanel.getNewID().ToString());\r
-\r
-                    newTrack.SubItems.Add("Automatic");\r
-                    newTrack.SubItems.Add(track.Encoder);\r
-                    newTrack.SubItems.Add(track.MixDown);\r
-                    newTrack.SubItems.Add(track.SampleRate);\r
-                    newTrack.SubItems.Add(track.Bitrate);\r
-                    newTrack.SubItems.Add(track.DRC);\r
-                    mainWindow.audioPanel.addTrackForPreset(newTrack);\r
-                }\r
 \r
-            // Subtitle Stuff\r
-            mainWindow.drp_subtitle.Text = presetQuery.Subtitles;\r
-\r
-            if (presetQuery.ForcedSubtitles)\r
-            {\r
-                mainWindow.check_forced.CheckState = CheckState.Checked;\r
-                mainWindow.check_forced.Enabled = true;\r
-            }\r
-            else\r
-                mainWindow.check_forced.CheckState = CheckState.Unchecked;\r
+            mainWindow.AudioSettings.LoadTracks(presetQuery.AudioInformation);\r
 \r
             #endregion\r
 \r
             #region Other\r
-            mainWindow.x264Panel.x264Query = presetQuery.H264Query;\r
+\r
+            mainWindow.x264Panel.X264Query = presetQuery.H264Query;\r
 \r
             // Set the preset name\r
-            mainWindow.groupBox_output.Text = "Output Settings (Preset: " + name + ")";\r
+            mainWindow.labelPreset.Text = "Output Settings (Preset: " + name + ")";\r
+\r
             #endregion\r
         }\r
+\r
+        /// <summary>\r
+        /// Convert a Quality Value to a position value for the Video Quality slider\r
+        /// </summary>\r
+        /// <param name="videoEncoder">The selected video encoder</param>\r
+        /// <param name="value">The Quality value</param>\r
+        /// <returns>The position on the video quality slider</returns>\r
+        private static int QualityToSliderValue(string videoEncoder, float value)\r
+        {\r
+            int sliderValue = 0;\r
+            switch (videoEncoder)\r
+            {\r
+                case "MPEG-4 (FFmpeg)":\r
+                    sliderValue = 32 - (int)value;\r
+                    break;\r
+                case "H.264 (x264)":\r
+                    double cqStep = Properties.Settings.Default.x264cqstep;\r
+                    sliderValue = (int)((51.0 / cqStep) - (value / cqStep));\r
+                    break;\r
+                case "VP3 (Theora)":\r
+                    sliderValue = (int)value;\r
+                    break;\r
+            }\r
+\r
+            return sliderValue;\r
+        }\r
     }\r
 }
\ No newline at end of file