PageRenderTime 21ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Forms/Split.cs

https://gitlab.com/minaz922/subtitleedit
C# | 353 lines | 309 code | 44 blank | 0 comment | 57 complexity | 81b7597f7ec36fec954625cc841630e3 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 Split : Form
  11. {
  12. Subtitle _subtitle;
  13. SubtitleFormat _format;
  14. Encoding _encoding;
  15. public bool ShowBasic { get; private set; }
  16. int _totalNumberOfCharacters;
  17. bool _loading = true;
  18. List<Subtitle> _parts;
  19. string _fileName;
  20. public Split()
  21. {
  22. InitializeComponent();
  23. var l = Configuration.Settings.Language.Split;
  24. Text = l.Title;
  25. groupBoxSplitOptions.Text = l.SplitOptions;
  26. RadioButtonLines.Text = l.Lines;
  27. radioButtonCharacters.Text = l.Characters;
  28. labelNumberOfParts.Text = l.NumberOfEqualParts;
  29. groupBoxSubtitleInfo.Text = l.SubtitleInfo;
  30. groupBoxOutput.Text = l.Output;
  31. labelFileName.Text = l.FileName;
  32. labelChooseOutputFolder.Text = l.OutputFolder;
  33. labelOutputFormat.Text = Configuration.Settings.Language.Main.Controls.SubtitleFormat;
  34. labelEncoding.Text = Configuration.Settings.Language.Main.Controls.FileEncoding;
  35. groupBoxPreview.Text = Configuration.Settings.Language.General.Preview;
  36. buttonOpenOutputFolder.Text = Configuration.Settings.Language.Main.Menu.File.Open;
  37. listViewParts.Columns[0].Text = l.Lines;
  38. listViewParts.Columns[1].Text = l.Characters;
  39. listViewParts.Columns[2].Text = l.FileName;
  40. buttonSplit.Text = l.DoSplit;
  41. buttonBasic.Text = l.Basic;
  42. buttonCancel.Text = Configuration.Settings.Language.General.Cancel;
  43. comboBoxSubtitleFormats.Left = labelOutputFormat.Left + labelOutputFormat.Width + 3;
  44. comboBoxEncoding.Left = labelEncoding.Left + labelEncoding.Width + 3;
  45. FixLargeFonts();
  46. }
  47. private void FixLargeFonts()
  48. {
  49. Graphics graphics = this.CreateGraphics();
  50. SizeF textSize = graphics.MeasureString(buttonSplit.Text, this.Font);
  51. if (textSize.Height > buttonSplit.Height - 4)
  52. {
  53. int newButtonHeight = (int)(textSize.Height + 7 + 0.5);
  54. Utilities.SetButtonHeight(this, newButtonHeight, 1);
  55. }
  56. }
  57. public void Initialize(Subtitle subtitle, string fileName, SubtitleFormat format, Encoding encoding, double lengthInSeconds)
  58. {
  59. ShowBasic = false;
  60. _subtitle = subtitle;
  61. if (string.IsNullOrEmpty(fileName))
  62. textBoxFileName.Text = Configuration.Settings.Language.SplitSubtitle.Untitled;
  63. else
  64. textBoxFileName.Text = fileName;
  65. _fileName = fileName;
  66. _format = format;
  67. _encoding = encoding;
  68. foreach (Paragraph p in _subtitle.Paragraphs)
  69. _totalNumberOfCharacters += p.Text.Length;
  70. labelLines.Text = string.Format(Configuration.Settings.Language.Split.NumberOfLinesX, _subtitle.Paragraphs.Count);
  71. labelCharacters.Text = string.Format(Configuration.Settings.Language.Split.NumberOfCharactersX, _totalNumberOfCharacters);
  72. try
  73. {
  74. numericUpDownParts.Value = Configuration.Settings.Tools.SplitNumberOfParts;
  75. }
  76. catch
  77. {
  78. }
  79. if (Configuration.Settings.Tools.SplitVia.Trim().ToLower() == "lines")
  80. RadioButtonLines.Checked = true;
  81. else
  82. radioButtonCharacters.Checked = true;
  83. foreach (SubtitleFormat f in SubtitleFormat.AllSubtitleFormats)
  84. {
  85. if (!f.IsVobSubIndexFile)
  86. comboBoxSubtitleFormats.Items.Add(f.FriendlyName);
  87. if (f.FriendlyName == format.FriendlyName)
  88. comboBoxSubtitleFormats.SelectedIndex = comboBoxSubtitleFormats.Items.Count - 1;
  89. }
  90. comboBoxEncoding.Items.Clear();
  91. int encodingSelectedIndex = 0;
  92. comboBoxEncoding.Items.Add(Encoding.UTF8.EncodingName);
  93. foreach (EncodingInfo ei in Encoding.GetEncodings())
  94. {
  95. if (ei.Name != Encoding.UTF8.BodyName && ei.CodePage >= 949 && !ei.DisplayName.Contains("EBCDIC") && ei.CodePage != 1047)
  96. {
  97. comboBoxEncoding.Items.Add(ei.CodePage + ": " + ei.DisplayName);
  98. if (ei.Name == Configuration.Settings.General.DefaultEncoding)
  99. encodingSelectedIndex = comboBoxEncoding.Items.Count - 1;
  100. }
  101. }
  102. comboBoxEncoding.SelectedIndex = encodingSelectedIndex;
  103. if (numericUpDownParts.Maximum > _subtitle.Paragraphs.Count)
  104. numericUpDownParts.Maximum = _subtitle.Paragraphs.Count / 2;
  105. if (!string.IsNullOrEmpty(_fileName))
  106. textBoxOutputFolder.Text = System.IO.Path.GetDirectoryName(_fileName);
  107. else if (string.IsNullOrEmpty(Configuration.Settings.Tools.SplitOutputFolder) || !System.IO.Directory.Exists(Configuration.Settings.Tools.SplitOutputFolder))
  108. textBoxOutputFolder.Text = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
  109. else
  110. textBoxOutputFolder.Text = Configuration.Settings.Tools.SplitOutputFolder;
  111. }
  112. private void CalculateParts()
  113. {
  114. if (_loading)
  115. return;
  116. _loading = true;
  117. _parts = new List<Subtitle>();
  118. if (string.IsNullOrEmpty(textBoxOutputFolder.Text) || !System.IO.Directory.Exists(textBoxOutputFolder.Text))
  119. textBoxOutputFolder.Text = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
  120. var format = Utilities.GetSubtitleFormatByFriendlyName(comboBoxSubtitleFormats.SelectedItem.ToString());
  121. string fileNameNoExt = System.IO.Path.GetFileNameWithoutExtension(textBoxFileName.Text);
  122. if (fileNameNoExt.Trim().Length == 0)
  123. fileNameNoExt = Configuration.Settings.Language.SplitSubtitle.Untitled;
  124. listViewParts.Items.Clear();
  125. int startNumber = 0;
  126. if (RadioButtonLines.Checked)
  127. {
  128. int partSize = (int)(_subtitle.Paragraphs.Count / numericUpDownParts.Value);
  129. for (int i = 0; i < numericUpDownParts.Value; i++)
  130. {
  131. int noOfLines = (int) partSize;
  132. if (i == numericUpDownParts.Value -1)
  133. noOfLines = (int) (_subtitle.Paragraphs.Count - ((numericUpDownParts.Value-1) * partSize));
  134. Subtitle temp = new Subtitle();
  135. temp.Header = _subtitle.Header;
  136. int size = 0;
  137. for (int number = 0; number < noOfLines; number++)
  138. {
  139. Paragraph p = _subtitle.Paragraphs[startNumber + number];
  140. temp.Paragraphs.Add(new Paragraph(p));
  141. size += p.Text.Length;
  142. }
  143. startNumber += noOfLines;
  144. _parts.Add(temp);
  145. ListViewItem lvi = new ListViewItem(string.Format("{0:#,###,###}", noOfLines));
  146. lvi.SubItems.Add(string.Format("{0:#,###,###}", size));
  147. lvi.SubItems.Add(fileNameNoExt + ".Part" + (i + 1) + format.Extension);
  148. listViewParts.Items.Add(lvi);
  149. }
  150. }
  151. else if (radioButtonCharacters.Checked)
  152. {
  153. int partSize = (int)(_totalNumberOfCharacters / numericUpDownParts.Value);
  154. int nextLimit = partSize;
  155. int currentSize = 0;
  156. Subtitle temp = new Subtitle();
  157. temp.Header = _subtitle.Header;
  158. for (int i = 0; i < _subtitle.Paragraphs.Count; i++)
  159. {
  160. Paragraph p = _subtitle.Paragraphs[i];
  161. int size = p.Text.Length;
  162. if (currentSize + size > nextLimit + 4 && _parts.Count < numericUpDownParts.Value-1)
  163. {
  164. _parts.Add(temp);
  165. ListViewItem lvi = new ListViewItem(string.Format("{0:#,###,###}", temp.Paragraphs.Count));
  166. lvi.SubItems.Add(string.Format("{0:#,###,###}", currentSize));
  167. lvi.SubItems.Add(fileNameNoExt + ".Part" + _parts.Count + format.Extension);
  168. listViewParts.Items.Add(lvi);
  169. currentSize = size;
  170. temp = new Subtitle();
  171. temp.Header = _subtitle.Header;
  172. temp.Paragraphs.Add(new Paragraph(p));
  173. }
  174. else
  175. {
  176. currentSize += size;
  177. temp.Paragraphs.Add(new Paragraph(p));
  178. }
  179. }
  180. _parts.Add(temp);
  181. ListViewItem lvi2 = new ListViewItem(string.Format("{0:#,###,###}", temp.Paragraphs.Count));
  182. lvi2.SubItems.Add(string.Format("{0:#,###,###}", currentSize));
  183. lvi2.SubItems.Add(fileNameNoExt + ".Part" + numericUpDownParts.Value + ".srt");
  184. listViewParts.Items.Add(lvi2);
  185. }
  186. _loading = false;
  187. }
  188. private void FormSplitSubtitle_KeyDown(object sender, KeyEventArgs e)
  189. {
  190. if (e.KeyCode == Keys.Escape)
  191. DialogResult = DialogResult.Cancel;
  192. }
  193. private void buttonBasic_Click(object sender, EventArgs e)
  194. {
  195. ShowBasic = true;
  196. DialogResult = DialogResult.Cancel;
  197. }
  198. private void buttonSplit_Click(object sender, EventArgs e)
  199. {
  200. bool overwrite = false;
  201. var format = Utilities.GetSubtitleFormatByFriendlyName(comboBoxSubtitleFormats.SelectedItem.ToString());
  202. string fileNameNoExt = System.IO.Path.GetFileNameWithoutExtension(textBoxFileName.Text);
  203. if (fileNameNoExt.Trim().Length == 0)
  204. fileNameNoExt = Configuration.Settings.Language.SplitSubtitle.Untitled;
  205. int number = 1;
  206. try
  207. {
  208. foreach (Subtitle sub in _parts)
  209. {
  210. string fileName = System.IO.Path.Combine(textBoxOutputFolder.Text, fileNameNoExt + ".Part" + number + format.Extension);
  211. string allText = sub.ToText(format);
  212. if (System.IO.File.Exists(fileName) && !overwrite)
  213. {
  214. if (MessageBox.Show(Configuration.Settings.Language.SplitSubtitle.OverwriteExistingFiles, "", MessageBoxButtons.YesNo) == DialogResult.No)
  215. return;
  216. overwrite = true;
  217. }
  218. System.IO.File.WriteAllText(fileName, allText, GetCurrentEncoding());
  219. number++;
  220. }
  221. }
  222. catch (Exception exception)
  223. {
  224. MessageBox.Show(exception.Message);
  225. return;
  226. }
  227. Configuration.Settings.Tools.SplitNumberOfParts = (int)numericUpDownParts.Value;
  228. Configuration.Settings.Tools.SplitOutputFolder = textBoxOutputFolder.Text;
  229. if (RadioButtonLines.Checked)
  230. Configuration.Settings.Tools.SplitVia = "Lines";
  231. else
  232. Configuration.Settings.Tools.SplitVia = "Characters";
  233. DialogResult = DialogResult.OK;
  234. }
  235. private void buttonCancel_Click(object sender, EventArgs e)
  236. {
  237. DialogResult = DialogResult.Cancel;
  238. }
  239. private void numericUpDownParts_ValueChanged(object sender, EventArgs e)
  240. {
  241. CalculateParts();
  242. }
  243. private void radioButtonCharacters_CheckedChanged(object sender, EventArgs e)
  244. {
  245. CalculateParts();
  246. }
  247. private void RadioButtonLines_CheckedChanged(object sender, EventArgs e)
  248. {
  249. CalculateParts();
  250. }
  251. private void textBoxOutputFolder_TextChanged(object sender, EventArgs e)
  252. {
  253. CalculateParts();
  254. }
  255. private void Split_ResizeEnd(object sender, EventArgs e)
  256. {
  257. columnHeaderFileName.Width = -2;
  258. }
  259. private void buttonChooseFolder_Click(object sender, EventArgs e)
  260. {
  261. if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
  262. {
  263. textBoxOutputFolder.Text = folderBrowserDialog1.SelectedPath;
  264. }
  265. }
  266. private Encoding GetCurrentEncoding()
  267. {
  268. if (comboBoxEncoding.Text == Encoding.UTF8.BodyName || comboBoxEncoding.Text == Encoding.UTF8.EncodingName || comboBoxEncoding.Text == "utf-8")
  269. {
  270. return Encoding.UTF8;
  271. }
  272. foreach (EncodingInfo ei in Encoding.GetEncodings())
  273. {
  274. if (ei.CodePage + ": " + ei.DisplayName == comboBoxEncoding.Text)
  275. return ei.GetEncoding();
  276. }
  277. return Encoding.UTF8;
  278. }
  279. private void Split_Shown(object sender, EventArgs e)
  280. {
  281. _loading = false;
  282. CalculateParts();
  283. }
  284. private void Split_Resize(object sender, EventArgs e)
  285. {
  286. columnHeaderFileName.Width = -2;
  287. }
  288. private void Split_KeyDown(object sender, KeyEventArgs e)
  289. {
  290. if (e.KeyCode == Keys.Escape)
  291. DialogResult = DialogResult.Cancel;
  292. }
  293. private void textBoxFileName_TextChanged(object sender, EventArgs e)
  294. {
  295. CalculateParts();
  296. }
  297. private void comboBoxSubtitleFormats_SelectedIndexChanged(object sender, EventArgs e)
  298. {
  299. CalculateParts();
  300. }
  301. private void buttonOpenOutputFolder_Click(object sender, EventArgs e)
  302. {
  303. if (System.IO.Directory.Exists(textBoxOutputFolder.Text))
  304. System.Diagnostics.Process.Start(textBoxOutputFolder.Text);
  305. else
  306. MessageBox.Show(string.Format(Configuration.Settings.Language.SplitSubtitle.FolderNotFoundX, textBoxOutputFolder.Text));
  307. }
  308. }
  309. }