PageRenderTime 43ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Forms/SubStationAlphaProperties.cs

https://gitlab.com/minaz922/subtitleedit
C# | 273 lines | 251 code | 22 blank | 0 comment | 75 complexity | 3b290903c3f5a500f96c7fd33e8a7485 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. using Nikse.SubtitleEdit.Logic;
  7. using Nikse.SubtitleEdit.Logic.SubtitleFormats;
  8. namespace Nikse.SubtitleEdit.Forms
  9. {
  10. public sealed partial class SubStationAlphaProperties : Form
  11. {
  12. private Subtitle _subtitle;
  13. private bool _isSubStationAlpha;
  14. private string _videoFileName;
  15. public SubStationAlphaProperties(Subtitle subtitle, SubtitleFormat format, string videoFileName, string subtitleFileName)
  16. {
  17. InitializeComponent();
  18. _subtitle = subtitle;
  19. _isSubStationAlpha = format.FriendlyName == new SubStationAlpha().FriendlyName;
  20. _videoFileName = videoFileName;
  21. var l = Configuration.Settings.Language.SubStationAlphaProperties;
  22. if (_isSubStationAlpha)
  23. {
  24. Text = l.TitleSubstationAlpha;
  25. labelWrapStyle.Visible = false;
  26. comboBoxWrapStyle.Visible = false;
  27. checkBoxScaleBorderAndShadow.Visible = false;
  28. Height = Height - (comboBoxWrapStyle.Height + checkBoxScaleBorderAndShadow.Height + 8);
  29. }
  30. else
  31. {
  32. Text = l.Title;
  33. }
  34. comboBoxWrapStyle.SelectedIndex = 2;
  35. comboBoxCollision.SelectedIndex = 0;
  36. string header = subtitle.Header;
  37. if (subtitle.Header == null)
  38. {
  39. SubStationAlpha ssa = new SubStationAlpha();
  40. var sub = new Subtitle();
  41. var lines = new List<string>();
  42. foreach (string line in subtitle.ToText(ssa).Replace(Environment.NewLine, "\n").Split('\n'))
  43. lines.Add(line);
  44. string title = "Untitled";
  45. if (!string.IsNullOrEmpty(subtitleFileName))
  46. title = System.IO.Path.GetFileNameWithoutExtension(subtitleFileName);
  47. else if (!string.IsNullOrEmpty(videoFileName))
  48. title = System.IO.Path.GetFileNameWithoutExtension(videoFileName);
  49. ssa.LoadSubtitle(sub, lines, title);
  50. header = sub.Header;
  51. }
  52. if (header != null)
  53. {
  54. foreach (string line in header.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
  55. {
  56. string s = line.ToLower().Trim();
  57. if (s.StartsWith("title:"))
  58. {
  59. textBoxTitle.Text = s.Remove(0, 6).Trim();
  60. }
  61. else if (s.StartsWith("original script:"))
  62. {
  63. textBoxOriginalScript.Text = s.Remove(0, 16).Trim();
  64. }
  65. else if (s.StartsWith("original translation:"))
  66. {
  67. textBoxTranslation.Text = s.Remove(0, 21).Trim();
  68. }
  69. else if (s.StartsWith("original editing:"))
  70. {
  71. textBoxEditing.Text = s.Remove(0, 17).Trim();
  72. }
  73. else if (s.StartsWith("original timing:"))
  74. {
  75. textBoxTiming.Text = s.Remove(0, 16).Trim();
  76. }
  77. else if (s.StartsWith("synch point:"))
  78. {
  79. textBoxSyncPoint.Text = s.Remove(0, 12).Trim();
  80. }
  81. else if (s.StartsWith("script updated by:"))
  82. {
  83. textBoxUpdatedBy.Text = s.Remove(0, 18).Trim();
  84. }
  85. else if (s.StartsWith("update details:"))
  86. {
  87. textBoxUpdateDetails.Text = s.Remove(0, 15).Trim();
  88. }
  89. else if (s.StartsWith("collisions:"))
  90. {
  91. if (s.Remove(0, 11).Trim() == "reverse")
  92. comboBoxCollision.SelectedIndex = 1;
  93. }
  94. else if (s.StartsWith("playresx:"))
  95. {
  96. int number;
  97. if (int.TryParse(s.Remove(0, 9).Trim(), out number))
  98. numericUpDownVideoWidth.Value = number;
  99. }
  100. else if (s.StartsWith("playresy:"))
  101. {
  102. int number;
  103. if (int.TryParse(s.Remove(0, 9).Trim(), out number))
  104. numericUpDownVideoHeight.Value = number;
  105. }
  106. else if (s.StartsWith("scaledborderandshadow:"))
  107. {
  108. checkBoxScaleBorderAndShadow.Checked = s.Remove(0, 22).Trim().ToLower() == "yes";
  109. }
  110. }
  111. }
  112. groupBoxScript.Text = l.Script;
  113. labelTitle.Text = l.ScriptTitle;
  114. labelOriginalScript.Text = l.OriginalScript;
  115. labelTranslation.Text = l.Translation;
  116. labelEditing.Text = l.Editing;
  117. labelTiming.Text = l.Timing;
  118. labelSyncPoint.Text = l.SyncPoint;
  119. labelUpdatedBy.Text = l.UpdatedBy;
  120. labelUpdateDetails.Text = l.UpdateDetails;
  121. groupBoxResolution.Text = l.Resolution;
  122. labelVideoResolution.Text = l.VideoResolution;
  123. groupBoxOptions.Text = l.Options;
  124. labelCollision.Text = l.Collision;
  125. labelWrapStyle.Text = l.WrapStyle;
  126. checkBoxScaleBorderAndShadow.Text = l.ScaleBorderAndShadow;
  127. buttonOK.Text = Configuration.Settings.Language.General.OK;
  128. buttonCancel.Text = Configuration.Settings.Language.General.Cancel;
  129. FixLargeFonts();
  130. }
  131. private void FixLargeFonts()
  132. {
  133. Graphics graphics = CreateGraphics();
  134. SizeF textSize = graphics.MeasureString(buttonCancel.Text, Font);
  135. if (textSize.Height > buttonCancel.Height - 4)
  136. {
  137. int newButtonHeight = (int)(textSize.Height + 7 + 0.5);
  138. Utilities.SetButtonHeight(this, newButtonHeight, 1);
  139. }
  140. }
  141. private void buttonCancel_Click(object sender, EventArgs e)
  142. {
  143. DialogResult = DialogResult.Cancel;
  144. }
  145. private string GetDefaultHeader()
  146. {
  147. SubtitleFormat format;
  148. if (_isSubStationAlpha)
  149. format = new SubStationAlpha();
  150. else
  151. format = new AdvancedSubStationAlpha();
  152. var sub = new Subtitle();
  153. string text = format.ToText(sub, string.Empty);
  154. string[] lineArray = text.Split(Environment.NewLine.ToCharArray());
  155. var lines = new List<string>();
  156. foreach (string line in lineArray)
  157. lines.Add(line);
  158. format.LoadSubtitle(sub, lines, string.Empty);
  159. return sub.Header.Trim();
  160. }
  161. private void buttonOK_Click(object sender, EventArgs e)
  162. {
  163. if (_subtitle.Header == null || !_subtitle.Header.ToLower().Contains("[script info]"))
  164. _subtitle.Header = GetDefaultHeader();
  165. string title = textBoxTitle.Text;
  166. if (title.Trim().Length == 0)
  167. title = "untitled";
  168. UpdateTag("Title", title, title.Trim().Length == 0);
  169. UpdateTag("Original Script", textBoxOriginalScript.Text, textBoxOriginalScript.Text.Trim().Length == 0);
  170. UpdateTag("Original Translation", textBoxTranslation.Text, textBoxTranslation.Text.Trim().Length == 0);
  171. UpdateTag("Original Editing", textBoxEditing.Text, textBoxEditing.Text.Trim().Length == 0);
  172. UpdateTag("Original Timing", textBoxTiming.Text, textBoxTiming.Text.Trim().Length == 0);
  173. UpdateTag("Synch Point", textBoxSyncPoint.Text, textBoxSyncPoint.Text.Trim().Length == 0);
  174. UpdateTag("Script Updated By", textBoxUpdatedBy.Text, textBoxUpdatedBy.Text.Trim().Length == 0);
  175. UpdateTag("Update Details", textBoxUpdateDetails.Text, textBoxUpdateDetails.Text.Trim().Length == 0);
  176. UpdateTag("PlayResX", numericUpDownVideoWidth.Value.ToString(), numericUpDownVideoWidth.Value == 0);
  177. UpdateTag("PlayResY", numericUpDownVideoHeight.Value.ToString(), numericUpDownVideoHeight.Value == 0);
  178. if (comboBoxCollision.SelectedIndex == 0)
  179. UpdateTag("collisions", "Normal", false); // normal
  180. else
  181. UpdateTag("collisions", "Reverse", false); // reverse
  182. if (!_isSubStationAlpha)
  183. {
  184. UpdateTag("wrapstyle", comboBoxWrapStyle.SelectedIndex.ToString(), false);
  185. if (checkBoxScaleBorderAndShadow.Checked)
  186. UpdateTag("ScaledBorderAndShadow", "yes", false);
  187. else
  188. UpdateTag("ScaledBorderAndShadow", "no", false);
  189. }
  190. DialogResult = DialogResult.OK;
  191. }
  192. private void UpdateTag(string tag, string text, bool remove)
  193. {
  194. bool scriptInfoOn = false;
  195. var sb = new StringBuilder();
  196. bool found = false;
  197. foreach (string line in _subtitle.Header.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
  198. {
  199. if (line.ToLower().StartsWith("[script info]"))
  200. {
  201. scriptInfoOn = true;
  202. }
  203. else if (line.StartsWith("["))
  204. {
  205. if (!found && scriptInfoOn && !remove)
  206. sb.AppendLine(tag + ": " + text);
  207. sb.AppendLine();
  208. scriptInfoOn = false;
  209. }
  210. string s = line.ToLower();
  211. if (s.StartsWith(tag.ToLower() + ":"))
  212. {
  213. if (!remove)
  214. sb.AppendLine(line.Substring(0, tag.Length) + ": " + text);
  215. found = true;
  216. }
  217. else
  218. {
  219. sb.AppendLine(line);
  220. }
  221. }
  222. _subtitle.Header = sb.ToString().Trim();
  223. }
  224. private void SubStationAlphaProperties_KeyDown(object sender, KeyEventArgs e)
  225. {
  226. if (e.KeyCode == Keys.Escape)
  227. DialogResult = DialogResult.Cancel;
  228. }
  229. private void buttonGetResolutionFromVideo_Click(object sender, EventArgs e)
  230. {
  231. openFileDialog1.Title = Configuration.Settings.Language.General.OpenVideoFileTitle;
  232. openFileDialog1.FileName = string.Empty;
  233. openFileDialog1.Filter = Utilities.GetVideoFileFilter(false);
  234. openFileDialog1.FileName = string.Empty;
  235. if (string.IsNullOrEmpty(openFileDialog1.InitialDirectory) && !string.IsNullOrEmpty(_videoFileName))
  236. {
  237. openFileDialog1.InitialDirectory = System.IO.Path.GetDirectoryName(_videoFileName);
  238. openFileDialog1.FileName = System.IO.Path.GetFileName(_videoFileName);
  239. }
  240. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  241. {
  242. VideoInfo info = Utilities.GetVideoInfo(openFileDialog1.FileName, delegate { Application.DoEvents(); });
  243. if (info != null && info.Success)
  244. {
  245. numericUpDownVideoWidth.Value = info.Width;
  246. numericUpDownVideoHeight.Value = info.Height;
  247. }
  248. }
  249. }
  250. }
  251. }