From ae04ccae7a92468d5c12a738001c794543ca5787 Mon Sep 17 00:00:00 2001 From: Nikolaj Olsson Date: Sun, 28 Feb 2021 14:16:05 +0100 Subject: [PATCH] Add export to csv of bookmarks - thx kjaerlige :) Fix #4854 --- LanguageMaster.xml | 7 +-- src/ui/Forms/BookmarksGoTo.Designer.cs | 25 ++++++++-- src/ui/Forms/BookmarksGoTo.cs | 65 +++++++++++++++++++++++++ src/ui/Forms/Main.cs | 5 ++ src/ui/Forms/Options/SettingsProfile.cs | 2 +- src/ui/Logic/LanguageDeserializer.cs | 3 ++ 6 files changed, 98 insertions(+), 9 deletions(-) diff --git a/LanguageMaster.xml b/LanguageMaster.xml index eb1b30b6d..a99e97e08 100644 --- a/LanguageMaster.xml +++ b/LanguageMaster.xml @@ -1281,7 +1281,7 @@ To use an API key go to "Options -> Settings -> Tools" to enter your Googl Remove voices New actor... Remove actors - Edit bookmark + Edit bookmark... Remove bookmark Go to source view Go to list view @@ -1960,7 +1960,8 @@ can edit in same subtitle file (collaboration) Toggle bookmarks Toggle bookmarks - add comment Clear bookmarks - Go to bookmark + Export bookmarks... + Go to bookmark... Go to previous bookmark Go to next bookmark Choose profile @@ -1982,7 +1983,7 @@ can edit in same subtitle file (collaboration) Single line max. length Optimal chars/sec Max. chars/sec - Max. words/min + Max. words/min Auto-wrap while typing Min. duration, milliseconds Max. duration, milliseconds diff --git a/src/ui/Forms/BookmarksGoTo.Designer.cs b/src/ui/Forms/BookmarksGoTo.Designer.cs index f09a8e429..350d63f3b 100644 --- a/src/ui/Forms/BookmarksGoTo.Designer.cs +++ b/src/ui/Forms/BookmarksGoTo.Designer.cs @@ -34,6 +34,7 @@ this.columnHeaderNumber = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.columnHeaderStartTime = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.columnHeaderText = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); + this.buttonExport = new System.Windows.Forms.Button(); this.SuspendLayout(); // // buttonCancel @@ -44,7 +45,7 @@ this.buttonCancel.Location = new System.Drawing.Point(555, 308); this.buttonCancel.Name = "buttonCancel"; this.buttonCancel.Size = new System.Drawing.Size(75, 23); - this.buttonCancel.TabIndex = 0; + this.buttonCancel.TabIndex = 30; this.buttonCancel.Text = "C&ancel"; this.buttonCancel.UseVisualStyleBackColor = true; // @@ -55,7 +56,7 @@ this.buttonOK.Location = new System.Drawing.Point(474, 308); this.buttonOK.Name = "buttonOK"; this.buttonOK.Size = new System.Drawing.Size(75, 23); - this.buttonOK.TabIndex = 2; + this.buttonOK.TabIndex = 20; this.buttonOK.Text = "&OK"; this.buttonOK.UseVisualStyleBackColor = true; this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click); @@ -75,7 +76,7 @@ this.listViewBookmarks.Location = new System.Drawing.Point(13, 12); this.listViewBookmarks.Name = "listViewBookmarks"; this.listViewBookmarks.Size = new System.Drawing.Size(617, 289); - this.listViewBookmarks.TabIndex = 1; + this.listViewBookmarks.TabIndex = 0; this.listViewBookmarks.UseCompatibleStateImageBehavior = false; this.listViewBookmarks.View = System.Windows.Forms.View.Details; this.listViewBookmarks.DoubleClick += new System.EventHandler(this.listViewBookmarks_DoubleClick); @@ -92,11 +93,24 @@ // this.columnHeaderText.Width = 480; // + // buttonExport + // + this.buttonExport.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonExport.ImeMode = System.Windows.Forms.ImeMode.NoControl; + this.buttonExport.Location = new System.Drawing.Point(371, 308); + this.buttonExport.Name = "buttonExport"; + this.buttonExport.Size = new System.Drawing.Size(97, 23); + this.buttonExport.TabIndex = 10; + this.buttonExport.Text = "Export..."; + this.buttonExport.UseVisualStyleBackColor = true; + this.buttonExport.Click += new System.EventHandler(this.buttonExport_Click); + // // BookmarksGoTo // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(642, 341); + this.Controls.Add(this.buttonExport); this.Controls.Add(this.listViewBookmarks); this.Controls.Add(this.buttonOK); this.Controls.Add(this.buttonCancel); @@ -109,9 +123,9 @@ this.ShowInTaskbar = false; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; this.Text = "BookmarksGoTo"; - this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.BookmarksGoTo_KeyDown); - this.ResizeEnd += new System.EventHandler(this.BookmarksGoTo_ResizeEnd); this.Shown += new System.EventHandler(this.BookmarksGoTo_Shown); + this.ResizeEnd += new System.EventHandler(this.BookmarksGoTo_ResizeEnd); + this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.BookmarksGoTo_KeyDown); this.ResumeLayout(false); } @@ -124,5 +138,6 @@ private System.Windows.Forms.ColumnHeader columnHeaderNumber; private System.Windows.Forms.ColumnHeader columnHeaderStartTime; private System.Windows.Forms.ColumnHeader columnHeaderText; + private System.Windows.Forms.Button buttonExport; } } \ No newline at end of file diff --git a/src/ui/Forms/BookmarksGoTo.cs b/src/ui/Forms/BookmarksGoTo.cs index 592251e3a..3d0bff6a8 100644 --- a/src/ui/Forms/BookmarksGoTo.cs +++ b/src/ui/Forms/BookmarksGoTo.cs @@ -2,6 +2,9 @@ using Nikse.SubtitleEdit.Core.Common; using Nikse.SubtitleEdit.Logic; using System; +using System.IO; +using System.Linq; +using System.Text; using System.Windows.Forms; namespace Nikse.SubtitleEdit.Forms @@ -16,11 +19,14 @@ namespace Nikse.SubtitleEdit.Forms InitializeComponent(); UiUtil.FixFonts(this); Text = LanguageSettings.Current.Bookmarks.GoToBookmark; + buttonExport.Text = LanguageSettings.Current.MultipleReplace.Export; + buttonOK.Text = LanguageSettings.Current.General.Ok; buttonCancel.Text = LanguageSettings.Current.General.Cancel; columnHeaderNumber.Text = LanguageSettings.Current.General.NumberSymbol; columnHeaderStartTime.Text = LanguageSettings.Current.General.StartTime; columnHeaderText.Text = LanguageSettings.Current.General.Text; + _subtitle = subtitle; foreach (var p in subtitle.Paragraphs) { @@ -81,6 +87,65 @@ namespace Nikse.SubtitleEdit.Forms private void BookmarksGoTo_Shown(object sender, EventArgs e) { BookmarksGoTo_ResizeEnd(sender, e); + listViewBookmarks.Focus(); + } + + private void buttonExport_Click(object sender, EventArgs e) + { + ExportBookmarksAsCsv(_subtitle, this); + } + + public static void ExportBookmarksAsCsv(Subtitle subtitle, Form form) + { + using (var saveDialog = new SaveFileDialog { FileName = string.Empty, Filter = "CSV|*.csv" }) + { + if (saveDialog.ShowDialog(form) != DialogResult.OK) + { + return; + } + + var sb = new StringBuilder(); + foreach (var p in subtitle.Paragraphs.Where(p => p.Bookmark != null)) + { + sb.AppendLine(MakeParagraphCsvLine(p)); + } + + File.WriteAllText(saveDialog.FileName, sb.ToString()); + } + } + + private static string MakeParagraphCsvLine(Paragraph paragraph) + { + const string separator = ";"; + var sb = new StringBuilder(); + sb.Append(paragraph.Number + separator); + sb.Append(ToCsvText(paragraph.StartTime.ToDisplayString()) + separator); + sb.Append(ToCsvText(paragraph.EndTime.ToDisplayString()) + separator); + sb.Append(ToCsvText(paragraph.Duration.ToShortDisplayString()) + separator); + sb.Append(ToCsvText(paragraph.Text) + separator); + sb.Append(ToCsvText(paragraph.Bookmark) + separator); + return sb.ToString(); + } + + private static string ToCsvText(string input) + { + if (string.IsNullOrEmpty(input)) + { + return string.Empty; + } + + var sb = new StringBuilder(); + sb.Append("\""); + foreach (var nextChar in input) + { + sb.Append(nextChar); + if (nextChar == '"') + { + sb.Append("\""); + } + } + sb.Append("\""); + return sb.ToString(); } } } diff --git a/src/ui/Forms/Main.cs b/src/ui/Forms/Main.cs index c7f63c3e6..53ede88c4 100644 --- a/src/ui/Forms/Main.cs +++ b/src/ui/Forms/Main.cs @@ -29035,6 +29035,11 @@ namespace Nikse.SubtitleEdit.Forms menuItem.Click += (sender2, e2) => { ClearBookmarks(); }; _bookmarkContextMenu.Items.Add(menuItem); + // export bookmarks + menuItem = new ToolStripMenuItem(LanguageSettings.Current.Settings.ExportBookmarks); + menuItem.Click += (sender2, e2) => { BookmarksGoTo.ExportBookmarksAsCsv(_subtitle, this); }; + _bookmarkContextMenu.Items.Add(menuItem); + UiUtil.FixFonts(_bookmarkContextMenu); pictureBoxBookmark.ContextMenuStrip = _bookmarkContextMenu; } diff --git a/src/ui/Forms/Options/SettingsProfile.cs b/src/ui/Forms/Options/SettingsProfile.cs index 3d41fb233..5194da1d6 100644 --- a/src/ui/Forms/Options/SettingsProfile.cs +++ b/src/ui/Forms/Options/SettingsProfile.cs @@ -28,7 +28,7 @@ namespace Nikse.SubtitleEdit.Forms.Options labelSubMaxLen.Text = language.SubtitleLineMaximumLength; labelOptimalCharsPerSecond.Text = language.OptimalCharactersPerSecond; labelMaxCharsPerSecond.Text = language.MaximumCharactersPerSecond; - labelMaxWordsPerMin.Text = language.MaximumWordssPerMinute; + labelMaxWordsPerMin.Text = language.MaximumWordsPerMinute; labelMinDuration.Text = language.DurationMinimumMilliseconds; labelMaxDuration.Text = language.DurationMaximumMilliseconds; labelMinGapMs.Text = language.MinimumGapMilliseconds; diff --git a/src/ui/Logic/LanguageDeserializer.cs b/src/ui/Logic/LanguageDeserializer.cs index bba3ae6d7..ad44e6e07 100644 --- a/src/ui/Logic/LanguageDeserializer.cs +++ b/src/ui/Logic/LanguageDeserializer.cs @@ -5290,6 +5290,9 @@ namespace Nikse.SubtitleEdit.Logic case "Settings/ClearBookmarks": language.Settings.ClearBookmarks = reader.Value; break; + case "Settings/ExportBookmarks": + language.Settings.ExportBookmarks = reader.Value; + break; case "Settings/GoToBookmark": language.Settings.GoToBookmark = reader.Value; break;