Merge pull request #6621 from ivandrofly/develop

NameList: Make namelist more interactive
This commit is contained in:
Nikolaj Olsson 2023-01-28 09:02:40 -05:00 committed by GitHub
commit 04493a17c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 33 deletions

View File

@ -2,6 +2,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Threading.Tasks;
using System.Xml; using System.Xml;
namespace Nikse.SubtitleEdit.Core.Dictionaries namespace Nikse.SubtitleEdit.Core.Dictionaries
@ -316,5 +317,10 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
} }
return false; return false;
} }
public static async Task<NameList> CreateAsync(string dictionaryFolder, string languageName, bool useOnlineNameList, string namesUrl)
{
return await Task.Run(() => new NameList(dictionaryFolder, languageName, useOnlineNameList, namesUrl));
}
} }
} }

View File

@ -126,7 +126,7 @@ namespace Nikse.SubtitleEdit.Forms.Options
} }
} }
private void ComboBoxWordListLanguageSelectedIndexChanged(object sender, EventArgs e) private async void ComboBoxWordListLanguageSelectedIndexChanged(object sender, EventArgs e)
{ {
buttonRemoveNameEtc.Enabled = false; buttonRemoveNameEtc.Enabled = false;
buttonAddNames.Enabled = false; buttonAddNames.Enabled = false;
@ -134,6 +134,11 @@ namespace Nikse.SubtitleEdit.Forms.Options
buttonAddUserWord.Enabled = false; buttonAddUserWord.Enabled = false;
buttonRemoveOcrFix.Enabled = false; buttonRemoveOcrFix.Enabled = false;
buttonAddOcrFix.Enabled = false; buttonAddOcrFix.Enabled = false;
listViewNames.BeginUpdate();
listBoxUserWordLists.BeginUpdate();
listBoxOcrFixList.BeginUpdate();
listViewNames.Items.Clear(); listViewNames.Items.Clear();
listBoxUserWordLists.Items.Clear(); listBoxUserWordLists.Items.Clear();
listBoxOcrFixList.Items.Clear(); listBoxOcrFixList.Items.Clear();
@ -151,8 +156,12 @@ namespace Nikse.SubtitleEdit.Forms.Options
// OCR fix words // OCR fix words
LoadOcrFixList(true); LoadOcrFixList(true);
LoadNames(language, true); await LoadNamesAsync(language, true);
} }
listViewNames.EndUpdate();
listBoxUserWordLists.EndUpdate();
listBoxOcrFixList.EndUpdate();
} }
private void LoadOcrFixList(bool reloadListBox) private void LoadOcrFixList(bool reloadListBox)
@ -167,8 +176,10 @@ namespace Nikse.SubtitleEdit.Forms.Options
{ {
listBoxOcrFixList.BeginUpdate(); listBoxOcrFixList.BeginUpdate();
listBoxOcrFixList.Items.Clear(); listBoxOcrFixList.Items.Clear();
listBoxOcrFixList.Items.AddRange(_ocrFixReplaceList.WordReplaceList.Select(p => p.Key + " --> " + p.Value).ToArray<object>()); // ReSharper disable once CoVariantArrayConversion
listBoxOcrFixList.Items.AddRange(_ocrFixReplaceList.PartialLineWordBoundaryReplaceList.Select(p => p.Key + " --> " + p.Value).ToArray<object>()); listBoxOcrFixList.Items.AddRange(_ocrFixReplaceList.WordReplaceList.Select(p => p.Key + " --> " + p.Value).ToArray());
// ReSharper disable once CoVariantArrayConversion
listBoxOcrFixList.Items.AddRange(_ocrFixReplaceList.PartialLineWordBoundaryReplaceList.Select(p => p.Key + " --> " + p.Value).ToArray());
listBoxOcrFixList.Sorted = true; listBoxOcrFixList.Sorted = true;
listBoxOcrFixList.EndUpdate(); listBoxOcrFixList.EndUpdate();
} }
@ -182,38 +193,38 @@ namespace Nikse.SubtitleEdit.Forms.Options
if (reloadListBox) if (reloadListBox)
{ {
listBoxUserWordLists.BeginUpdate();
listBoxUserWordLists.Items.Clear(); listBoxUserWordLists.Items.Clear();
listBoxUserWordLists.Items.AddRange(_userWordList.ToArray<object>()); listBoxUserWordLists.Items.AddRange(_userWordList.ToArray<object>());
listBoxUserWordLists.EndUpdate();
} }
} }
private void LoadNames(string language, bool reloadListBox) private async Task LoadNamesAsync(string language, bool reloadListBox)
{ {
var task = Task.Factory.StartNew(() => // update all names
{ _wordListNames = await GetNamesSortedFromSourceAsync().ConfigureAwait(true);
// names etc
var nameList = new NameList(Configuration.DictionariesDirectory, language, Configuration.Settings.WordLists.UseOnlineNames, Configuration.Settings.WordLists.NamesUrl);
_wordListNames = nameList.GetAllNames();
_wordListNames.Sort();
return _wordListNames;
});
if (reloadListBox) if (reloadListBox)
{ {
// reload the listbox on a continuation ui thead listViewNames.BeginUpdate();
var uiContext = TaskScheduler.FromCurrentSynchronizationContext(); listViewNames.Items.Clear();
task.ContinueWith(originalTask => var list = new List<ListViewItem>();
foreach (var item in _wordListNames)
{ {
listViewNames.BeginUpdate(); list.Add(new ListViewItem(item));
listViewNames.Items.Clear(); }
var list = new List<ListViewItem>(); listViewNames.Items.AddRange(list.ToArray());
foreach (var item in originalTask.Result) listViewNames.EndUpdate();
{ }
list.Add(new ListViewItem(item));
} async Task<List<string>> GetNamesSortedFromSourceAsync()
listViewNames.Items.AddRange(list.ToArray()); {
listViewNames.EndUpdate(); var nameList = await NameList.CreateAsync(Configuration.DictionariesDirectory, language, Configuration.Settings.WordLists.UseOnlineNames, Configuration.Settings.WordLists.NamesUrl)
}, uiContext); .ConfigureAwait(false);
var names = nameList.GetAllNames();
names.Sort();
return names;
} }
} }
@ -229,7 +240,7 @@ namespace Nikse.SubtitleEdit.Forms.Options
return cb?.CultureInfo.Name.Replace('-', '_'); return cb?.CultureInfo.Name.Replace('-', '_');
} }
private void ButtonAddNamesClick(object sender, EventArgs e) private async void ButtonAddNamesClick(object sender, EventArgs e)
{ {
var languageIndex = comboBoxWordListLanguage.SelectedIndex; var languageIndex = comboBoxWordListLanguage.SelectedIndex;
if (languageIndex < 0) if (languageIndex < 0)
@ -241,9 +252,13 @@ namespace Nikse.SubtitleEdit.Forms.Options
var text = textBoxNameEtc.Text.RemoveControlCharacters().Trim(); var text = textBoxNameEtc.Text.RemoveControlCharacters().Trim();
if (!string.IsNullOrEmpty(language) && text.Length > 1 && !_wordListNames.Contains(text)) if (!string.IsNullOrEmpty(language) && text.Length > 1 && !_wordListNames.Contains(text))
{ {
var nameList = new NameList(Configuration.DictionariesDirectory, language, Configuration.Settings.WordLists.UseOnlineNames, Configuration.Settings.WordLists.NamesUrl); // adds new name
var nameList = await NameList.CreateAsync(Configuration.DictionariesDirectory, language, Configuration.Settings.WordLists.UseOnlineNames, Configuration.Settings.WordLists.NamesUrl);
nameList.Add(text); nameList.Add(text);
LoadNames(language, true);
// reload
await LoadNamesAsync(language, true).ConfigureAwait(true);
labelStatus.Text = string.Format(LanguageSettings.Current.Settings.WordAddedX, text); labelStatus.Text = string.Format(LanguageSettings.Current.Settings.WordAddedX, text);
textBoxNameEtc.Text = string.Empty; textBoxNameEtc.Text = string.Empty;
textBoxNameEtc.Focus(); textBoxNameEtc.Focus();
@ -275,7 +290,7 @@ namespace Nikse.SubtitleEdit.Forms.Options
buttonRemoveNameEtc.Enabled = listViewNames.SelectedItems.Count >= 1; buttonRemoveNameEtc.Enabled = listViewNames.SelectedItems.Count >= 1;
} }
private void ButtonRemoveNameEtcClick(object sender, EventArgs e) private async void ButtonRemoveNameEtcClick(object sender, EventArgs e)
{ {
if (listViewNames.SelectedItems.Count == 0) if (listViewNames.SelectedItems.Count == 0)
{ {
@ -301,7 +316,8 @@ namespace Nikse.SubtitleEdit.Forms.Options
if (result == DialogResult.Yes) if (result == DialogResult.Yes)
{ {
var removeCount = 0; var removeCount = 0;
var namesList = new NameList(Configuration.DictionariesDirectory, language, Configuration.Settings.WordLists.UseOnlineNames, Configuration.Settings.WordLists.NamesUrl); var namesList = await NameList.CreateAsync(Configuration.DictionariesDirectory, language, Configuration.Settings.WordLists.UseOnlineNames, Configuration.Settings.WordLists.NamesUrl)
.ConfigureAwait(true);
for (var idx = listViewNames.SelectedIndices.Count - 1; idx >= 0; idx--) for (var idx = listViewNames.SelectedIndices.Count - 1; idx >= 0; idx--)
{ {
index = listViewNames.SelectedIndices[idx]; index = listViewNames.SelectedIndices[idx];
@ -313,7 +329,7 @@ namespace Nikse.SubtitleEdit.Forms.Options
if (removeCount > 0) if (removeCount > 0)
{ {
LoadNames(language, true); // reload await LoadNamesAsync(language, true).ConfigureAwait(true); // reload
if (index < listViewNames.Items.Count) if (index < listViewNames.Items.Count)
{ {