Added translation progress to sections

This commit is contained in:
Thorsten Sommer 2022-09-26 22:07:01 +02:00
parent ebc7a5708b
commit 6c7b5dbade
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108

View File

@ -1,4 +1,5 @@
using Processor; using DataModel.Database;
using Processor;
using UI_WinForms.Dialogs; using UI_WinForms.Dialogs;
using UI_WinForms.Resources; using UI_WinForms.Resources;
@ -6,6 +7,8 @@ namespace UI_WinForms.Components;
public partial class SectionTree : UserControl public partial class SectionTree : UserControl
{ {
private static readonly Dictionary<TreeNode, EventHandler<DataModel.Database.Translation?>> NODE_PROGRESS_HANDLERS = new();
public SectionTree() public SectionTree()
{ {
this.InitializeComponent(); this.InitializeComponent();
@ -46,17 +49,23 @@ public partial class SectionTree : UserControl
{ {
foreach (var section in await SectionProcessor.LoadLayer(i)) foreach (var section in await SectionProcessor.LoadLayer(i))
{ {
var translationProgress = await TranslationProcessor.CalculateTranslationProgress(section);
// Create the tree node: // Create the tree node:
var node = new TreeNode var node = new TreeNode
{ {
Name = section.DataKey, // [sic] name is the key Name = section.DataKey, // [sic] name is the key
Text = section.Name, Text = $"{section.Name} [{translationProgress.Result}%]",
StateImageIndex = 1, StateImageIndex = 1,
}; };
// Cache the node: // Cache the node:
treeNodes.Add(section.DataKey, node); treeNodes.Add(section.DataKey, node);
// Wire each node to the translation change event to update the translation progress:
NODE_PROGRESS_HANDLERS.Add(node, async (_, translation) => await this.UpdateTranslationProgress(node, section, translation));
AppEvents.WhenTranslationChanged += NODE_PROGRESS_HANDLERS[node];
// Is this a root node? // Is this a root node?
if (section.Depth is 0) if (section.Depth is 0)
@ -107,6 +116,18 @@ public partial class SectionTree : UserControl
this.treeView.ExpandAll(); this.treeView.ExpandAll();
} }
private async Task UpdateTranslationProgress(TreeNode node, Section section, DataModel.Database.Translation? translation)
{
if (translation is null || translation.TextElement.Section.DataKey == section.DataKey)
{
var currentTranslationProgress = await TranslationProcessor.CalculateTranslationProgress(section);
if(this.treeView.InvokeRequired)
this.treeView.Invoke(() => node.Text = $"{section.Name} [{currentTranslationProgress.Result}%]");
else
node.Text = $"{section.Name} [{currentTranslationProgress.Result}%]";
}
}
private async void buttonAdd_Click(object sender, EventArgs e) private async void buttonAdd_Click(object sender, EventArgs e)
{ {
if(this.DesignMode) if(this.DesignMode)
@ -135,14 +156,20 @@ public partial class SectionTree : UserControl
if(!addedSection.Successful) if(!addedSection.Successful)
return; return;
var translationProgress = await TranslationProcessor.CalculateTranslationProgress(addedSection.Result!);
// Add the new section to the tree control: // Add the new section to the tree control:
var node = new TreeNode var node = new TreeNode
{ {
Name = addedSection.Result!.DataKey, // [sic] name is the key Name = addedSection.Result!.DataKey, // [sic] name is the key
Text = addedSection.Result.Name, Text = $"{addedSection.Result.Name} [{translationProgress.Result}%]",
StateImageIndex = 1, StateImageIndex = 1,
}; };
// Wire each node to the translation change event to update the translation progress:
NODE_PROGRESS_HANDLERS.Add(node, async (_, translation) => await this.UpdateTranslationProgress(node, addedSection.Result, translation));
AppEvents.WhenTranslationChanged += NODE_PROGRESS_HANDLERS[node];
if(!addRootNode && selectedNode is not null) if(!addRootNode && selectedNode is not null)
selectedNode.Nodes.Add(node); selectedNode.Nodes.Add(node);
else else
@ -178,6 +205,10 @@ public partial class SectionTree : UserControl
// Remove all nodes from the tree control: // Remove all nodes from the tree control:
this.treeView.Nodes.Clear(); this.treeView.Nodes.Clear();
foreach (var (treeNode, handler) in NODE_PROGRESS_HANDLERS)
AppEvents.WhenTranslationChanged -= handler;
NODE_PROGRESS_HANDLERS.Clear();
// Reload the tree: // Reload the tree:
this.LoadNodes(this, EventArgs.Empty); this.LoadNodes(this, EventArgs.Empty);
@ -187,6 +218,8 @@ public partial class SectionTree : UserControl
// Fire the click event: // Fire the click event:
this.treeView_NodeMouseClick(this, new TreeNodeMouseClickEventArgs(this.treeView.SelectedNode!, MouseButtons.Left, 1, 0, 0)); this.treeView_NodeMouseClick(this, new TreeNodeMouseClickEventArgs(this.treeView.SelectedNode!, MouseButtons.Left, 1, 0, 0));
AppEvents.TranslationChanged(null);
} }
private async void treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) private async void treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)