From 5c824f5855ec04477e82405a82fe6e61d71a2c63 Mon Sep 17 00:00:00 2001 From: niksedk Date: Sun, 16 Apr 2023 09:37:27 +0200 Subject: [PATCH] Choose where to replace in translation mode - thx Leon :) --- src/libse/Enums/ReplaceType.cs | 2 ++ src/ui/Forms/Main.cs | 8 +++-- src/ui/Forms/ReplaceDialog.Designer.cs | 44 ++++++++++++++++++----- src/ui/Forms/ReplaceDialog.cs | 22 +++++++++++- src/ui/Logic/FindReplaceDialogHelper.cs | 47 ++++++++++++++----------- src/ui/Logic/ReplaceAllHelper.cs | 21 ++++++++--- 6 files changed, 107 insertions(+), 37 deletions(-) diff --git a/src/libse/Enums/ReplaceType.cs b/src/libse/Enums/ReplaceType.cs index 62efcb98a..078b7f141 100644 --- a/src/libse/Enums/ReplaceType.cs +++ b/src/libse/Enums/ReplaceType.cs @@ -4,5 +4,7 @@ { public bool WholeWord { get; set; } public FindType FindType { get; set; } + public bool SearchTranslation { get; set; } + public bool SearchOriginal { get; set; } } } diff --git a/src/ui/Forms/Main.cs b/src/ui/Forms/Main.cs index 3904b8f94..a9968a55d 100644 --- a/src/ui/Forms/Main.cs +++ b/src/ui/Forms/Main.cs @@ -6247,6 +6247,8 @@ namespace Nikse.SubtitleEdit.Forms { _findHelper.InProgress = false; } + + Focus(); } public void FindDialogFindPrevious(string findText) @@ -6462,7 +6464,7 @@ namespace Nikse.SubtitleEdit.Forms _findHelper = _findHelper ?? _replaceDialog.GetFindDialogHelper(_subtitleListViewIndex); } - _replaceDialog.Initialize(selectedText, _findHelper); + _replaceDialog.Initialize(selectedText, _findHelper, IsOriginalEditable && SubtitleListview1.IsOriginalTextColumnVisible); if (!_replaceDialog.Visible) { _replaceDialog.Show(this); @@ -6721,6 +6723,8 @@ namespace Nikse.SubtitleEdit.Forms { RestartHistory(); } + + Focus(); } public void ListViewReplaceAll(ReplaceDialog replaceDialog, FindReplaceDialogHelper findHelper) @@ -6977,7 +6981,7 @@ namespace Nikse.SubtitleEdit.Forms } } - _replaceDialog.Initialize(selectedText, _findHelper); + _replaceDialog.Initialize(selectedText, _findHelper, IsOriginalEditable && SubtitleListview1.IsOriginalTextColumnVisible); if (!_replaceDialog.Visible) { _replaceDialog.Show(this); diff --git a/src/ui/Forms/ReplaceDialog.Designer.cs b/src/ui/Forms/ReplaceDialog.Designer.cs index a442e098e..58585a5c5 100644 --- a/src/ui/Forms/ReplaceDialog.Designer.cs +++ b/src/ui/Forms/ReplaceDialog.Designer.cs @@ -39,12 +39,15 @@ this.labelFindWhat = new System.Windows.Forms.Label(); this.buttonFind = new System.Windows.Forms.Button(); this.checkBoxWholeWord = new System.Windows.Forms.CheckBox(); + this.comboBoxFindReplaceIn = new System.Windows.Forms.ComboBox(); + this.labelFindReplaceIn = new System.Windows.Forms.Label(); this.SuspendLayout(); // // radioButtonRegEx // + this.radioButtonRegEx.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.radioButtonRegEx.AutoSize = true; - this.radioButtonRegEx.Location = new System.Drawing.Point(15, 190); + this.radioButtonRegEx.Location = new System.Drawing.Point(15, 235); this.radioButtonRegEx.Name = "radioButtonRegEx"; this.radioButtonRegEx.Size = new System.Drawing.Size(56, 17); this.radioButtonRegEx.TabIndex = 5; @@ -54,8 +57,9 @@ // // radioButtonCaseSensitive // + this.radioButtonCaseSensitive.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.radioButtonCaseSensitive.AutoSize = true; - this.radioButtonCaseSensitive.Location = new System.Drawing.Point(15, 167); + this.radioButtonCaseSensitive.Location = new System.Drawing.Point(15, 212); this.radioButtonCaseSensitive.Name = "radioButtonCaseSensitive"; this.radioButtonCaseSensitive.Size = new System.Drawing.Size(94, 17); this.radioButtonCaseSensitive.TabIndex = 4; @@ -65,9 +69,10 @@ // // radioButtonNormal // + this.radioButtonNormal.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.radioButtonNormal.AutoSize = true; this.radioButtonNormal.Checked = true; - this.radioButtonNormal.Location = new System.Drawing.Point(15, 144); + this.radioButtonNormal.Location = new System.Drawing.Point(15, 189); this.radioButtonNormal.Name = "radioButtonNormal"; this.radioButtonNormal.Size = new System.Drawing.Size(58, 17); this.radioButtonNormal.TabIndex = 3; @@ -78,7 +83,7 @@ // // buttonReplace // - this.buttonReplace.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.buttonReplace.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.buttonReplace.Location = new System.Drawing.Point(266, 54); this.buttonReplace.Name = "buttonReplace"; this.buttonReplace.Size = new System.Drawing.Size(119, 23); @@ -98,7 +103,7 @@ // // buttonReplaceAll // - this.buttonReplaceAll.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.buttonReplaceAll.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.buttonReplaceAll.Location = new System.Drawing.Point(266, 83); this.buttonReplaceAll.Name = "buttonReplaceAll"; this.buttonReplaceAll.Size = new System.Drawing.Size(119, 23); @@ -135,7 +140,7 @@ // // buttonFind // - this.buttonFind.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.buttonFind.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.buttonFind.Location = new System.Drawing.Point(266, 25); this.buttonFind.Name = "buttonFind"; this.buttonFind.Size = new System.Drawing.Size(119, 23); @@ -146,19 +151,40 @@ // // checkBoxWholeWord // + this.checkBoxWholeWord.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.checkBoxWholeWord.AutoSize = true; - this.checkBoxWholeWord.Location = new System.Drawing.Point(15, 114); + this.checkBoxWholeWord.Location = new System.Drawing.Point(15, 159); this.checkBoxWholeWord.Name = "checkBoxWholeWord"; this.checkBoxWholeWord.Size = new System.Drawing.Size(83, 17); this.checkBoxWholeWord.TabIndex = 2; this.checkBoxWholeWord.Text = "Whole word"; this.checkBoxWholeWord.UseVisualStyleBackColor = true; // + // comboBoxFindReplaceIn + // + this.comboBoxFindReplaceIn.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.comboBoxFindReplaceIn.FormattingEnabled = true; + this.comboBoxFindReplaceIn.Location = new System.Drawing.Point(15, 120); + this.comboBoxFindReplaceIn.Name = "comboBoxFindReplaceIn"; + this.comboBoxFindReplaceIn.Size = new System.Drawing.Size(232, 21); + this.comboBoxFindReplaceIn.TabIndex = 15; + // + // labelFindReplaceIn + // + this.labelFindReplaceIn.AutoSize = true; + this.labelFindReplaceIn.Location = new System.Drawing.Point(12, 104); + this.labelFindReplaceIn.Name = "labelFindReplaceIn"; + this.labelFindReplaceIn.Size = new System.Drawing.Size(96, 13); + this.labelFindReplaceIn.TabIndex = 16; + this.labelFindReplaceIn.Text = "Replace/search in:"; + // // ReplaceDialog // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(397, 221); + this.ClientSize = new System.Drawing.Size(397, 266); + this.Controls.Add(this.labelFindReplaceIn); + this.Controls.Add(this.comboBoxFindReplaceIn); this.Controls.Add(this.checkBoxWholeWord); this.Controls.Add(this.buttonFind); this.Controls.Add(this.labelFindWhat); @@ -199,5 +225,7 @@ private System.Windows.Forms.Label labelFindWhat; private System.Windows.Forms.Button buttonFind; private System.Windows.Forms.CheckBox checkBoxWholeWord; + private System.Windows.Forms.ComboBox comboBoxFindReplaceIn; + private System.Windows.Forms.Label labelFindReplaceIn; } } \ No newline at end of file diff --git a/src/ui/Forms/ReplaceDialog.cs b/src/ui/Forms/ReplaceDialog.cs index f7795e077..0b1798e48 100644 --- a/src/ui/Forms/ReplaceDialog.cs +++ b/src/ui/Forms/ReplaceDialog.cs @@ -43,6 +43,12 @@ namespace Nikse.SubtitleEdit.Forms UiUtil.FixLargeFonts(this, buttonReplace); _findNextShortcut = UiUtil.GetKeys(Configuration.Settings.Shortcuts.MainEditFindNext); + + comboBoxFindReplaceIn.Items.Clear(); + comboBoxFindReplaceIn.Items.Add("Translation and original"); + comboBoxFindReplaceIn.Items.Add("Translation only"); + comboBoxFindReplaceIn.Items.Add("Original only"); + comboBoxFindReplaceIn.SelectedIndex = 0; } public bool ReplaceAll { get; set; } @@ -64,6 +70,14 @@ namespace Nikse.SubtitleEdit.Forms result.FindType = FindType.RegEx; } + result.SearchOriginal = !comboBoxFindReplaceIn.Visible || + comboBoxFindReplaceIn.SelectedIndex == 0 || + comboBoxFindReplaceIn.SelectedIndex == 2; + + result.SearchTranslation = !comboBoxFindReplaceIn.Visible || + comboBoxFindReplaceIn.SelectedIndex == 0 || + comboBoxFindReplaceIn.SelectedIndex == 1; + result.WholeWord = checkBoxWholeWord.Checked; return result; } @@ -91,7 +105,7 @@ namespace Nikse.SubtitleEdit.Forms } } - internal void Initialize(string selectedText, FindReplaceDialogHelper findHelper) + internal void Initialize(string selectedText, FindReplaceDialogHelper findHelper, bool replaceInOriginal) { _findHelper = findHelper; textBoxFind.Text = selectedText; @@ -123,6 +137,9 @@ namespace Nikse.SubtitleEdit.Forms { checkBoxWholeWord.Checked = findHelper.FindReplaceType.WholeWord; } + + labelFindReplaceIn.Visible = replaceInOriginal; + comboBoxFindReplaceIn.Visible = replaceInOriginal; } private void ButtonReplaceClick(object sender, EventArgs e) @@ -133,6 +150,7 @@ namespace Nikse.SubtitleEdit.Forms Validate(textBoxFind.Text); if (DialogResult == DialogResult.OK) { + var findType = GetFindType(); _findAndReplaceMethods.ReplaceDialogReplace(); } @@ -147,6 +165,7 @@ namespace Nikse.SubtitleEdit.Forms Validate(textBoxFind.Text); if (DialogResult == DialogResult.OK) { + var findType = GetFindType(); _findAndReplaceMethods.ReplaceDialogReplaceAll(); } @@ -203,6 +222,7 @@ namespace Nikse.SubtitleEdit.Forms Validate(textBoxFind.Text); if (DialogResult == DialogResult.OK) { + var findType = GetFindType(); _findAndReplaceMethods.ReplaceDialogFind(); } } diff --git a/src/ui/Logic/FindReplaceDialogHelper.cs b/src/ui/Logic/FindReplaceDialogHelper.cs index b6d236fe7..f0b355721 100644 --- a/src/ui/Logic/FindReplaceDialogHelper.cs +++ b/src/ui/Logic/FindReplaceDialogHelper.cs @@ -134,7 +134,7 @@ namespace Nikse.SubtitleEdit.Logic } int pos; - if (!MatchInOriginal) + if (!MatchInOriginal && FindReplaceType.SearchTranslation) { pos = FindPositionInText(p.Text, position); if (pos >= 0) @@ -154,7 +154,7 @@ namespace Nikse.SubtitleEdit.Logic MatchInOriginal = false; } - if (originalSubtitle != null && allowEditOfOriginalSubtitle) + if (originalSubtitle != null && allowEditOfOriginalSubtitle && FindReplaceType.SearchOriginal) { var o = Utilities.GetOriginalParagraph(index, p, originalSubtitle.Paragraphs); if (o != null) @@ -193,7 +193,7 @@ namespace Nikse.SubtitleEdit.Logic if (originalSubtitle != null && allowEditOfOriginalSubtitle) { - if (!first || MatchInOriginal) + if ((!first || MatchInOriginal) && FindReplaceType.SearchTranslation) { var o = Utilities.GetOriginalParagraph(index, p, originalSubtitle.Paragraphs); if (o != null) @@ -232,32 +232,37 @@ namespace Nikse.SubtitleEdit.Logic position = p.Text.Length - 1; } - for (var j = 0; j <= position; j++) + if (originalSubtitle != null && allowEditOfOriginalSubtitle && FindReplaceType.SearchOriginal) { - if (position - j >= 0 && position < p.Text.Length) + + for (var j = 0; j <= position; j++) { - var t = p.Text.Substring(position - j, j + 1); - var pos = FindPositionInText(t, 0); - var startWholeWord = position - j < 1; - if (!startWholeWord && position - j - 1 > 0) + if (position - j >= 0 && position < p.Text.Length) { - startWholeWord = SeparatorChars.Contains(p.Text[position - j - 1]); - } + var t = p.Text.Substring(position - j, j + 1); + var pos = FindPositionInText(t, 0); + var startWholeWord = position - j < 1; + if (!startWholeWord && position - j - 1 > 0) + { + startWholeWord = SeparatorChars.Contains(p.Text[position - j - 1]); + } - var startWholeWorkOkay = !FindReplaceType.WholeWord || startWholeWord; + var startWholeWorkOkay = !FindReplaceType.WholeWord || startWholeWord; - if (pos >= 0 && startWholeWorkOkay) - { - pos += position - j; - MatchInOriginal = false; - SelectedLineIndex = index; - SelectedPosition = pos; - ReplaceFromPosition = pos; - Success = true; - return true; + if (pos >= 0 && startWholeWorkOkay) + { + pos += position - j; + MatchInOriginal = false; + SelectedLineIndex = index; + SelectedPosition = pos; + ReplaceFromPosition = pos; + Success = true; + return true; + } } } } + position = 0; first = false; index--; diff --git a/src/ui/Logic/ReplaceAllHelper.cs b/src/ui/Logic/ReplaceAllHelper.cs index 926f39e6e..dd496d312 100644 --- a/src/ui/Logic/ReplaceAllHelper.cs +++ b/src/ui/Logic/ReplaceAllHelper.cs @@ -11,8 +11,14 @@ namespace Nikse.SubtitleEdit.Logic { if (findHelper.FindReplaceType.FindType == FindType.RegEx) { - var count = ReplaceAllRegEx(findHelper, subtitle, stopAtIndex); - if (allowEditOfOriginalSubtitle && subtitleOriginal?.Paragraphs.Count > 0) + var count = 0; + + if (findHelper.FindReplaceType.SearchTranslation) + { + count += ReplaceAllRegEx(findHelper, subtitle, stopAtIndex); + } + + if (allowEditOfOriginalSubtitle && findHelper.FindReplaceType.SearchOriginal && subtitleOriginal?.Paragraphs.Count > 0) { count += ReplaceAllRegEx(findHelper, subtitleOriginal, stopAtIndex); } @@ -20,8 +26,13 @@ namespace Nikse.SubtitleEdit.Logic return count; } - var replaceCount = ReplaceAllNonRegEx(findHelper, subtitle, stopAtIndex); - if (allowEditOfOriginalSubtitle && subtitleOriginal?.Paragraphs.Count > 0) + var replaceCount = 0; + if (findHelper.FindReplaceType.SearchTranslation) + { + replaceCount += ReplaceAllNonRegEx(findHelper, subtitle, stopAtIndex); + } + + if (allowEditOfOriginalSubtitle && findHelper.FindReplaceType.SearchOriginal && subtitleOriginal?.Paragraphs.Count > 0) { replaceCount += ReplaceAllNonRegEx(findHelper, subtitleOriginal, stopAtIndex); } @@ -55,7 +66,7 @@ namespace Nikse.SubtitleEdit.Logic private static int ReplaceAllRegEx(FindReplaceDialogHelper findHelper, Subtitle subtitle, int stopAtIndex) { var replaceCount = 0; - for (int i = Math.Max(0, findHelper.StartLineIndex); i < subtitle.Paragraphs.Count; i++) + for (var i = Math.Max(0, findHelper.StartLineIndex); i < subtitle.Paragraphs.Count; i++) { if (i >= stopAtIndex) {