Choose where to replace in translation mode - thx Leon :)

This commit is contained in:
niksedk 2023-04-16 09:37:27 +02:00
parent eed3ffff81
commit 5c824f5855
6 changed files with 107 additions and 37 deletions

View File

@ -4,5 +4,7 @@
{ {
public bool WholeWord { get; set; } public bool WholeWord { get; set; }
public FindType FindType { get; set; } public FindType FindType { get; set; }
public bool SearchTranslation { get; set; }
public bool SearchOriginal { get; set; }
} }
} }

View File

@ -6247,6 +6247,8 @@ namespace Nikse.SubtitleEdit.Forms
{ {
_findHelper.InProgress = false; _findHelper.InProgress = false;
} }
Focus();
} }
public void FindDialogFindPrevious(string findText) public void FindDialogFindPrevious(string findText)
@ -6462,7 +6464,7 @@ namespace Nikse.SubtitleEdit.Forms
_findHelper = _findHelper ?? _replaceDialog.GetFindDialogHelper(_subtitleListViewIndex); _findHelper = _findHelper ?? _replaceDialog.GetFindDialogHelper(_subtitleListViewIndex);
} }
_replaceDialog.Initialize(selectedText, _findHelper); _replaceDialog.Initialize(selectedText, _findHelper, IsOriginalEditable && SubtitleListview1.IsOriginalTextColumnVisible);
if (!_replaceDialog.Visible) if (!_replaceDialog.Visible)
{ {
_replaceDialog.Show(this); _replaceDialog.Show(this);
@ -6721,6 +6723,8 @@ namespace Nikse.SubtitleEdit.Forms
{ {
RestartHistory(); RestartHistory();
} }
Focus();
} }
public void ListViewReplaceAll(ReplaceDialog replaceDialog, FindReplaceDialogHelper findHelper) 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) if (!_replaceDialog.Visible)
{ {
_replaceDialog.Show(this); _replaceDialog.Show(this);

View File

@ -39,12 +39,15 @@
this.labelFindWhat = new System.Windows.Forms.Label(); this.labelFindWhat = new System.Windows.Forms.Label();
this.buttonFind = new System.Windows.Forms.Button(); this.buttonFind = new System.Windows.Forms.Button();
this.checkBoxWholeWord = new System.Windows.Forms.CheckBox(); this.checkBoxWholeWord = new System.Windows.Forms.CheckBox();
this.comboBoxFindReplaceIn = new System.Windows.Forms.ComboBox();
this.labelFindReplaceIn = new System.Windows.Forms.Label();
this.SuspendLayout(); this.SuspendLayout();
// //
// radioButtonRegEx // radioButtonRegEx
// //
this.radioButtonRegEx.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.radioButtonRegEx.AutoSize = true; 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.Name = "radioButtonRegEx";
this.radioButtonRegEx.Size = new System.Drawing.Size(56, 17); this.radioButtonRegEx.Size = new System.Drawing.Size(56, 17);
this.radioButtonRegEx.TabIndex = 5; this.radioButtonRegEx.TabIndex = 5;
@ -54,8 +57,9 @@
// //
// radioButtonCaseSensitive // radioButtonCaseSensitive
// //
this.radioButtonCaseSensitive.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.radioButtonCaseSensitive.AutoSize = true; 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.Name = "radioButtonCaseSensitive";
this.radioButtonCaseSensitive.Size = new System.Drawing.Size(94, 17); this.radioButtonCaseSensitive.Size = new System.Drawing.Size(94, 17);
this.radioButtonCaseSensitive.TabIndex = 4; this.radioButtonCaseSensitive.TabIndex = 4;
@ -65,9 +69,10 @@
// //
// radioButtonNormal // radioButtonNormal
// //
this.radioButtonNormal.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.radioButtonNormal.AutoSize = true; this.radioButtonNormal.AutoSize = true;
this.radioButtonNormal.Checked = 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.Name = "radioButtonNormal";
this.radioButtonNormal.Size = new System.Drawing.Size(58, 17); this.radioButtonNormal.Size = new System.Drawing.Size(58, 17);
this.radioButtonNormal.TabIndex = 3; this.radioButtonNormal.TabIndex = 3;
@ -78,7 +83,7 @@
// //
// buttonReplace // 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.Location = new System.Drawing.Point(266, 54);
this.buttonReplace.Name = "buttonReplace"; this.buttonReplace.Name = "buttonReplace";
this.buttonReplace.Size = new System.Drawing.Size(119, 23); this.buttonReplace.Size = new System.Drawing.Size(119, 23);
@ -98,7 +103,7 @@
// //
// buttonReplaceAll // 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.Location = new System.Drawing.Point(266, 83);
this.buttonReplaceAll.Name = "buttonReplaceAll"; this.buttonReplaceAll.Name = "buttonReplaceAll";
this.buttonReplaceAll.Size = new System.Drawing.Size(119, 23); this.buttonReplaceAll.Size = new System.Drawing.Size(119, 23);
@ -135,7 +140,7 @@
// //
// buttonFind // 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.Location = new System.Drawing.Point(266, 25);
this.buttonFind.Name = "buttonFind"; this.buttonFind.Name = "buttonFind";
this.buttonFind.Size = new System.Drawing.Size(119, 23); this.buttonFind.Size = new System.Drawing.Size(119, 23);
@ -146,19 +151,40 @@
// //
// checkBoxWholeWord // checkBoxWholeWord
// //
this.checkBoxWholeWord.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.checkBoxWholeWord.AutoSize = true; 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.Name = "checkBoxWholeWord";
this.checkBoxWholeWord.Size = new System.Drawing.Size(83, 17); this.checkBoxWholeWord.Size = new System.Drawing.Size(83, 17);
this.checkBoxWholeWord.TabIndex = 2; this.checkBoxWholeWord.TabIndex = 2;
this.checkBoxWholeWord.Text = "Whole word"; this.checkBoxWholeWord.Text = "Whole word";
this.checkBoxWholeWord.UseVisualStyleBackColor = true; 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 // ReplaceDialog
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 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.checkBoxWholeWord);
this.Controls.Add(this.buttonFind); this.Controls.Add(this.buttonFind);
this.Controls.Add(this.labelFindWhat); this.Controls.Add(this.labelFindWhat);
@ -199,5 +225,7 @@
private System.Windows.Forms.Label labelFindWhat; private System.Windows.Forms.Label labelFindWhat;
private System.Windows.Forms.Button buttonFind; private System.Windows.Forms.Button buttonFind;
private System.Windows.Forms.CheckBox checkBoxWholeWord; private System.Windows.Forms.CheckBox checkBoxWholeWord;
private System.Windows.Forms.ComboBox comboBoxFindReplaceIn;
private System.Windows.Forms.Label labelFindReplaceIn;
} }
} }

View File

@ -43,6 +43,12 @@ namespace Nikse.SubtitleEdit.Forms
UiUtil.FixLargeFonts(this, buttonReplace); UiUtil.FixLargeFonts(this, buttonReplace);
_findNextShortcut = UiUtil.GetKeys(Configuration.Settings.Shortcuts.MainEditFindNext); _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; } public bool ReplaceAll { get; set; }
@ -64,6 +70,14 @@ namespace Nikse.SubtitleEdit.Forms
result.FindType = FindType.RegEx; 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; result.WholeWord = checkBoxWholeWord.Checked;
return result; 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; _findHelper = findHelper;
textBoxFind.Text = selectedText; textBoxFind.Text = selectedText;
@ -123,6 +137,9 @@ namespace Nikse.SubtitleEdit.Forms
{ {
checkBoxWholeWord.Checked = findHelper.FindReplaceType.WholeWord; checkBoxWholeWord.Checked = findHelper.FindReplaceType.WholeWord;
} }
labelFindReplaceIn.Visible = replaceInOriginal;
comboBoxFindReplaceIn.Visible = replaceInOriginal;
} }
private void ButtonReplaceClick(object sender, EventArgs e) private void ButtonReplaceClick(object sender, EventArgs e)
@ -133,6 +150,7 @@ namespace Nikse.SubtitleEdit.Forms
Validate(textBoxFind.Text); Validate(textBoxFind.Text);
if (DialogResult == DialogResult.OK) if (DialogResult == DialogResult.OK)
{ {
var findType = GetFindType();
_findAndReplaceMethods.ReplaceDialogReplace(); _findAndReplaceMethods.ReplaceDialogReplace();
} }
@ -147,6 +165,7 @@ namespace Nikse.SubtitleEdit.Forms
Validate(textBoxFind.Text); Validate(textBoxFind.Text);
if (DialogResult == DialogResult.OK) if (DialogResult == DialogResult.OK)
{ {
var findType = GetFindType();
_findAndReplaceMethods.ReplaceDialogReplaceAll(); _findAndReplaceMethods.ReplaceDialogReplaceAll();
} }
@ -203,6 +222,7 @@ namespace Nikse.SubtitleEdit.Forms
Validate(textBoxFind.Text); Validate(textBoxFind.Text);
if (DialogResult == DialogResult.OK) if (DialogResult == DialogResult.OK)
{ {
var findType = GetFindType();
_findAndReplaceMethods.ReplaceDialogFind(); _findAndReplaceMethods.ReplaceDialogFind();
} }
} }

View File

@ -134,7 +134,7 @@ namespace Nikse.SubtitleEdit.Logic
} }
int pos; int pos;
if (!MatchInOriginal) if (!MatchInOriginal && FindReplaceType.SearchTranslation)
{ {
pos = FindPositionInText(p.Text, position); pos = FindPositionInText(p.Text, position);
if (pos >= 0) if (pos >= 0)
@ -154,7 +154,7 @@ namespace Nikse.SubtitleEdit.Logic
MatchInOriginal = false; MatchInOriginal = false;
} }
if (originalSubtitle != null && allowEditOfOriginalSubtitle) if (originalSubtitle != null && allowEditOfOriginalSubtitle && FindReplaceType.SearchOriginal)
{ {
var o = Utilities.GetOriginalParagraph(index, p, originalSubtitle.Paragraphs); var o = Utilities.GetOriginalParagraph(index, p, originalSubtitle.Paragraphs);
if (o != null) if (o != null)
@ -193,7 +193,7 @@ namespace Nikse.SubtitleEdit.Logic
if (originalSubtitle != null && allowEditOfOriginalSubtitle) if (originalSubtitle != null && allowEditOfOriginalSubtitle)
{ {
if (!first || MatchInOriginal) if ((!first || MatchInOriginal) && FindReplaceType.SearchTranslation)
{ {
var o = Utilities.GetOriginalParagraph(index, p, originalSubtitle.Paragraphs); var o = Utilities.GetOriginalParagraph(index, p, originalSubtitle.Paragraphs);
if (o != null) if (o != null)
@ -232,32 +232,37 @@ namespace Nikse.SubtitleEdit.Logic
position = p.Text.Length - 1; 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); if (position - j >= 0 && position < p.Text.Length)
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 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) if (pos >= 0 && startWholeWorkOkay)
{ {
pos += position - j; pos += position - j;
MatchInOriginal = false; MatchInOriginal = false;
SelectedLineIndex = index; SelectedLineIndex = index;
SelectedPosition = pos; SelectedPosition = pos;
ReplaceFromPosition = pos; ReplaceFromPosition = pos;
Success = true; Success = true;
return true; return true;
}
} }
} }
} }
position = 0; position = 0;
first = false; first = false;
index--; index--;

View File

@ -11,8 +11,14 @@ namespace Nikse.SubtitleEdit.Logic
{ {
if (findHelper.FindReplaceType.FindType == FindType.RegEx) if (findHelper.FindReplaceType.FindType == FindType.RegEx)
{ {
var count = ReplaceAllRegEx(findHelper, subtitle, stopAtIndex); var count = 0;
if (allowEditOfOriginalSubtitle && subtitleOriginal?.Paragraphs.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); count += ReplaceAllRegEx(findHelper, subtitleOriginal, stopAtIndex);
} }
@ -20,8 +26,13 @@ namespace Nikse.SubtitleEdit.Logic
return count; return count;
} }
var replaceCount = ReplaceAllNonRegEx(findHelper, subtitle, stopAtIndex); var replaceCount = 0;
if (allowEditOfOriginalSubtitle && subtitleOriginal?.Paragraphs.Count > 0) if (findHelper.FindReplaceType.SearchTranslation)
{
replaceCount += ReplaceAllNonRegEx(findHelper, subtitle, stopAtIndex);
}
if (allowEditOfOriginalSubtitle && findHelper.FindReplaceType.SearchOriginal && subtitleOriginal?.Paragraphs.Count > 0)
{ {
replaceCount += ReplaceAllNonRegEx(findHelper, subtitleOriginal, stopAtIndex); replaceCount += ReplaceAllNonRegEx(findHelper, subtitleOriginal, stopAtIndex);
} }
@ -55,7 +66,7 @@ namespace Nikse.SubtitleEdit.Logic
private static int ReplaceAllRegEx(FindReplaceDialogHelper findHelper, Subtitle subtitle, int stopAtIndex) private static int ReplaceAllRegEx(FindReplaceDialogHelper findHelper, Subtitle subtitle, int stopAtIndex)
{ {
var replaceCount = 0; 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) if (i >= stopAtIndex)
{ {