From 6a5676f916528b04c03e7eefcfa585ca41fd933f Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Sat, 20 Jan 2024 01:53:27 +0100 Subject: [PATCH] ChartWindow: Load GraphEntries correctly from EditBillingData --- Elwig/Helpers/Billing/EditBillingData.cs | 33 +++++++++--- Elwig/Helpers/Billing/GraphEntry.cs | 3 +- Elwig/Windows/ChartWindow.xaml.cs | 65 ++++++++++-------------- 3 files changed, 54 insertions(+), 47 deletions(-) diff --git a/Elwig/Helpers/Billing/EditBillingData.cs b/Elwig/Helpers/Billing/EditBillingData.cs index c3b8f1e..5be462f 100644 --- a/Elwig/Helpers/Billing/EditBillingData.cs +++ b/Elwig/Helpers/Billing/EditBillingData.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using System.Text.Json.Nodes; @@ -16,7 +17,7 @@ namespace Elwig.Helpers.Billing { return new(ParseJson(json), attributeVariants); } - public IEnumerable GetPaymentGraphEntries() { + public IEnumerable GetPaymentGraphEntries(AppDbContext context) { Dictionary> dict1 = []; Dictionary> dict2 = []; var p = GetPaymentEntry(); @@ -40,22 +41,38 @@ namespace Elwig.Helpers.Billing { dict2[idx].Add("default"); } + var virtOffset = dict1.Count; Dictionary curves = GetCurves(); decimal[] virtCurves = [.. dict2.Keys.Order()]; for (int i = 0; i < virtCurves.Length; i++) { var idx = virtCurves[i]; - dict1[1000 + i] = dict2[idx]; - curves[1000 + i] = new Curve(CurveMode.Oe, new() { { 73, idx } }, null); + dict1[i + virtOffset] = dict2[idx]; + curves[i + virtOffset] = new Curve(CurveMode.Oe, new() { { 73, idx } }, null); } - Dictionary> dict3 = []; + Dictionary> dict3 = curves.ToDictionary(c => c.Key, _ => new List()); + foreach (var (selector, value) in GetSelection(p, AttributeVariants)) { + int? idx = null; + if (value.TryGetValue(out var val)) { + idx = Array.IndexOf(virtCurves, val) + virtOffset; + } else if (value.TryGetValue(out var str)) { + idx = int.Parse(str.Split(":")[1]); + } + if (idx != null) + dict3[(int)idx].Add(selector); + } + var vars = context.WineVarieties.ToDictionary(v => v.SortId, v => v); + var attrs = context.WineAttributes.ToDictionary(a => a.AttrId, a => a); - - return dict3.Select(e => new GraphEntry(e.Key, curves[e.Key], 50, 120)).ToList(); + return dict3 + .Select(e => new GraphEntry(e.Key, curves[e.Key], e.Value + .Select(s => new ContractSelection(vars[s[..2]], s.Length > 2 ? attrs[s[2..]] : null)) + .ToList(), 50, 120)) + .ToList(); } - public IEnumerable GetQualityGraphEntries() { + public IEnumerable GetQualityGraphEntries(AppDbContext context) { Dictionary> dict1 = []; Dictionary> dict2 = []; foreach (var (qualid, q) in GetQualityEntry() ?? []) { diff --git a/Elwig/Helpers/Billing/GraphEntry.cs b/Elwig/Helpers/Billing/GraphEntry.cs index 05dae82..eb86887 100644 --- a/Elwig/Helpers/Billing/GraphEntry.cs +++ b/Elwig/Helpers/Billing/GraphEntry.cs @@ -31,11 +31,12 @@ namespace Elwig.Helpers.Billing { if (gebunden != null) GebundenGraph = new Graph(gebunden, minX, maxX); } - public GraphEntry(int id, BillingData.Curve curve, int minX, int maxX) : + public GraphEntry(int id, BillingData.Curve curve, List contracts, int minX, int maxX) : this(id, curve.Mode, minX, maxX) { DataGraph = new Graph(curve.Normal, minX, maxX); if (curve.Gebunden != null) GebundenGraph = new Graph(curve.Gebunden, minX, maxX); + Contracts = contracts; } private GraphEntry(int id, BillingData.CurveMode mode, Graph dataGraph, Graph? gebundenGraph, diff --git a/Elwig/Windows/ChartWindow.xaml.cs b/Elwig/Windows/ChartWindow.xaml.cs index b78e237..3945d89 100644 --- a/Elwig/Windows/ChartWindow.xaml.cs +++ b/Elwig/Windows/ChartWindow.xaml.cs @@ -58,10 +58,7 @@ namespace Elwig.Windows { private async Task RefreshGraphList() { await Context.PaymentVariants.LoadAsync(); - await RefreshGraphListQuery(); - } - private async Task RefreshGraphListQuery() { var attrVariants = Context.DeliveryParts .Where(d => d.Year == Year) .Select(d => $"{d.SortId}{d.AttrId}") @@ -71,37 +68,18 @@ namespace Elwig.Windows { .Order() .ToList(); var data = EditBillingData.FromJson(PaymentVar.Data, attrVariants); - GraphEntries.AddRange(data.GetPaymentGraphEntries()); - GraphEntries.AddRange(data.GetQualityGraphEntries()); + GraphEntries = [ ..data.GetPaymentGraphEntries(Context), ..data.GetQualityGraphEntries(Context)]; - //var contracts = ContractSelection.GetContractsForYear(Context, Year).DistinctBy(c => c.Listing).Order().ToList(); - //ControlUtils.RenewItemsSource(ContractInput, contracts, g => (g as GraphEntry)?.Id); - - ControlUtils.RenewItemsSource(ContractInput, attrVariants, g => g); - ControlUtils.RenewItemsSource(GraphList, GraphEntries, g => (g as GraphEntry)?.Id, null, ControlUtils.RenewSourceDefault.IfOnly); + var contracts = ContractSelection.GetContractsForYear(Context, Year).DistinctBy(c => c.Listing).Order().ToList(); + ControlUtils.RenewItemsSource(ContractInput, contracts, g => (g as ContractSelection)?.ToString()); + ControlUtils.RenewItemsSource(GraphList, GraphEntries, g => (g as GraphEntry)?.Id, null, ControlUtils.RenewSourceDefault.First); RefreshInputs(); } - private string ParseContracts(JsonObject auszahlungsSorten, int num) { - return ""; - } - - private void RefreshInputs(bool validate = false) { + private void RefreshInputs() { ResetPlot(); - if (!PaymentVar.TestVariant) { - AddButton.IsEnabled = false; - CopyButton.IsEnabled = false; - DeleteButton.IsEnabled = false; - DisableUnitTextBox(OechsleInput); - DisableUnitTextBox(PriceInput); - GebundenTypeFixed.IsEnabled = false; - GebundenTypeGraph.IsEnabled = false; - GebundenTypeNone.IsEnabled = false; - ContractInput.IsEnabled = false; - EnableOptionButtons(); - FillInputs(); - } else if (SelectedGraphEntry != null) { + if (SelectedGraphEntry != null) { CopyButton.IsEnabled = true; DeleteButton.IsEnabled = true; //EnableUnitTextBox(OechsleInput); @@ -117,21 +95,31 @@ namespace Elwig.Windows { DisableUnitTextBox(OechsleInput); DisableOptionButtons(); } - GC.Collect(); + if (!PaymentVar.TestVariant) { + AddButton.IsEnabled = false; + CopyButton.IsEnabled = false; + DeleteButton.IsEnabled = false; + DisableUnitTextBox(OechsleInput); + DisableUnitTextBox(PriceInput); + GebundenTypeFixed.IsEnabled = false; + GebundenTypeGraph.IsEnabled = false; + GebundenTypeNone.IsEnabled = false; + ContractInput.IsEnabled = false; + } } private void FillInputs() { - GraphNum.Text = SelectedGraphEntry.Id.ToString(); + GraphNum.Text = SelectedGraphEntry?.Id.ToString(); - if (SelectedGraphEntry.GebundenFlatBonus != null) { + if (SelectedGraphEntry?.GebundenFlatBonus != null) { GebundenTypeFixed.IsChecked = true; - } else if (SelectedGraphEntry.GebundenGraph != null) { + } else if (SelectedGraphEntry?.GebundenGraph != null) { GebundenTypeGraph.IsChecked = true; } else { GebundenTypeNone.IsChecked = true; ; } - ControlUtils.SelectCheckComboBoxItems(ContractInput, SelectedGraphEntry.Contracts, i => (i as ContractSelection)?.Listing); + ControlUtils.SelectCheckComboBoxItems(ContractInput, SelectedGraphEntry?.Contracts ?? [], i => (i as ContractSelection)?.Listing); InitPlot(); OechslePricePlot.IsEnabled = true; @@ -149,13 +137,13 @@ namespace Elwig.Windows { GebundenPlot.MarkerSize = 9; } - DataPlot = OechslePricePlot.Plot.AddScatter(SelectedGraphEntry.DataGraph.DataX, SelectedGraphEntry.DataGraph.DataY); + DataPlot = OechslePricePlot.Plot.AddScatter(SelectedGraphEntry!.DataGraph.DataX, SelectedGraphEntry!.DataGraph.DataY); DataPlot.LineColor = Color.Blue; DataPlot.MarkerColor = Color.Blue; DataPlot.MarkerSize = 9; - if (SelectedGraphEntry.GebundenGraph == null) { - ChangeActiveGraph(SelectedGraphEntry.DataGraph); + if (SelectedGraphEntry?.GebundenGraph == null) { + ChangeActiveGraph(SelectedGraphEntry?.DataGraph); } OechslePricePlot.RightClicked -= OechslePricePlot.DefaultRightClickEvent; @@ -470,7 +458,7 @@ namespace Elwig.Windows { HighlightedPointPlot.IsVisible = true; HoverChanged = true ^ HoverActive; HoverActive = true; - HandleTooltip(mouseOnGebunden.Value.x, mouseOnGebunden.Value.y, mouseOnGebunden.Value.index, SelectedGraphEntry!.GebundenGraph); + HandleTooltip(mouseOnGebunden.Value.x, mouseOnGebunden.Value.y, mouseOnGebunden.Value.index, SelectedGraphEntry!.GebundenGraph!); } else { ChangeMarker(HighlightedPointPlot, false); HoverChanged = false ^ HoverActive; @@ -604,7 +592,7 @@ namespace Elwig.Windows { } private void GraphList_SelectionChanged(object sender, SelectionChangedEventArgs e) { - SelectedGraphEntry = (GraphEntry)GraphList.SelectedItem; + SelectedGraphEntry = GraphList.SelectedItem as GraphEntry; RefreshInputs(); //var x = OechslePricePlot.Plot.GetPlottables().OfType(); @@ -629,6 +617,7 @@ namespace Elwig.Windows { private void ContractInput_Changed(object sender, RoutedEventArgs e) { var r = ContractInput.SelectedItems.Cast(); SelectedGraphEntry!.Contracts = r.ToList(); + // FIXME when using arrow keys, selection does not work nicely GraphList.Items.Refresh(); }