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