793 lines
33 KiB
C#
793 lines
33 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Input;
|
|
using Elwig.Controls;
|
|
using Elwig.Helpers;
|
|
using Elwig.Helpers.Billing;
|
|
using Elwig.Models.Entities;
|
|
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
|
using ScottPlot.Plottables;
|
|
using ScottPlot;
|
|
using Xceed.Wpf.Toolkit.Primitives;
|
|
using ScottPlot.Control;
|
|
|
|
namespace Elwig.Windows {
|
|
public partial class ChartWindow : ContextWindow {
|
|
|
|
public static readonly Color ColorUngebunden = Colors.Blue;
|
|
public static readonly Color ColorGebunden = Colors.Gold;
|
|
|
|
public readonly int Year;
|
|
public readonly int AvNr;
|
|
public Season Season;
|
|
private PaymentVar PaymentVar;
|
|
private bool HasChanged = false;
|
|
|
|
private Scatter DataPlot;
|
|
private Scatter? GebundenPlot;
|
|
private Marker HighlightedPointPlot;
|
|
private Marker PrimaryMarkedPointPlot;
|
|
private Marker SecondaryMarkedPointPlot;
|
|
private Text TooltipPlot;
|
|
private LegendItem UngebundenLegend;
|
|
private LegendItem GebundenLegend;
|
|
private LegendItem LDWLegend;
|
|
private LegendItem QUWLegend;
|
|
private LegendItem KABLegend;
|
|
|
|
private (Graph? graph, int index) LastHighlighted = (null, -1);
|
|
private (Graph? graph, int index) Highlighted = (null, -1);
|
|
private Graph? ActiveGraph = null;
|
|
private int PrimaryMarkedPoint = -1;
|
|
private int SecondaryMarkedPoint = -1;
|
|
private bool HoverChanged = false;
|
|
private bool HoverActive = false;
|
|
private bool FillingInputs = false;
|
|
|
|
private List<GraphEntry> GraphEntries = [];
|
|
private GraphEntry? SelectedGraphEntry => (GraphEntry)GraphList.SelectedItem;
|
|
|
|
public ChartWindow(int year, int avnr) {
|
|
InitializeComponent();
|
|
Year = year;
|
|
AvNr = avnr;
|
|
Season = Context.Seasons.Find(year) ?? throw new ArgumentException("Season not found");
|
|
PaymentVar = Context.PaymentVariants.Find(year, avnr) ?? throw new ArgumentException("PaymentVar not found");
|
|
Title = $"{PaymentVar?.Name} - Lese {year} - Elwig";
|
|
LockContext = true;
|
|
}
|
|
|
|
private void Window_Loaded(object sender, RoutedEventArgs evt) {
|
|
}
|
|
|
|
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
|
|
if (HasChanged) {
|
|
var r = MessageBox.Show("Soll das Fenster wirklich geschlossen werden? Nicht gespeicherte Änderungen werden NICHT übernommen!", "Schließen bestätigen",
|
|
MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No);
|
|
if (r != MessageBoxResult.Yes) {
|
|
e.Cancel = true;
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void SetHasChanged(bool hasChanged = true) {
|
|
HasChanged = hasChanged;
|
|
SaveButton.IsEnabled = hasChanged;
|
|
}
|
|
|
|
private async Task RefreshGraphList() {
|
|
PaymentVar = await Context.PaymentVariants.FindAsync(Year, AvNr) ?? throw new ArgumentException("PaymentVar not found");
|
|
Season = await Context.Seasons.FindAsync(Year) ?? throw new ArgumentException("Season not found");
|
|
|
|
var data = EditBillingData.FromJson(PaymentVar.Data, Utils.GetAttributeVarieties(Context, Year));
|
|
var paymentEntries = data.GetPaymentGraphEntries(Context, Season);
|
|
GraphEntries = [
|
|
..paymentEntries,
|
|
..data.GetQualityGraphEntries(Context, Season, paymentEntries.Max(e => e.Id))
|
|
];
|
|
|
|
var contracts = Utils.GetContractsForYear(Context, Year);
|
|
FillingInputs = true;
|
|
ControlUtils.RenewItemsSource(ContractInput, contracts, g => (g as ContractSelection)?.Listing);
|
|
FillingInputs = false;
|
|
ControlUtils.RenewItemsSource(GraphList, GraphEntries, g => (g as GraphEntry)?.ContractsStringChange, GraphList_SelectionChanged, ControlUtils.RenewSourceDefault.First);
|
|
|
|
RefreshInputs();
|
|
}
|
|
|
|
private void RefreshInputs() {
|
|
ResetPlot();
|
|
if (SelectedGraphEntry != null) {
|
|
CopyButton.IsEnabled = true;
|
|
DeleteButton.IsEnabled = true;
|
|
GebundenTypeFixed.IsEnabled = true;
|
|
GebundenTypeGraph.IsEnabled = true;
|
|
GebundenTypeNone.IsEnabled = true;
|
|
ContractInput.IsEnabled = true;
|
|
AbgewertetInput.IsEnabled = true;
|
|
EnableOptionButtons();
|
|
FillInputs();
|
|
} else {
|
|
CopyButton.IsEnabled = false;
|
|
DeleteButton.IsEnabled = false;
|
|
DisableUnitTextBox(OechsleInput);
|
|
DisableOptionButtons();
|
|
}
|
|
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;
|
|
AbgewertetInput.IsEnabled = false;
|
|
}
|
|
GC.Collect();
|
|
}
|
|
|
|
private void FillInputs() {
|
|
FillingInputs = true;
|
|
|
|
AbgewertetInput.IsChecked = SelectedGraphEntry?.Abgewertet;
|
|
if (SelectedGraphEntry?.GebundenFlatBonus is double bonus) {
|
|
GebundenTypeFixed.IsChecked = true;
|
|
GebundenFlatBonus.Text = $"{bonus}";
|
|
} else if (SelectedGraphEntry?.GebundenGraph != null) {
|
|
GebundenTypeGraph.IsChecked = true;
|
|
GebundenFlatBonus.Text = "";
|
|
} else {
|
|
GebundenTypeNone.IsChecked = true;
|
|
GebundenFlatBonus.Text = "";
|
|
}
|
|
|
|
ControlUtils.SelectCheckComboBoxItems(ContractInput, SelectedGraphEntry?.Contracts ?? [], i => (i as ContractSelection)?.Listing);
|
|
|
|
InitPlot();
|
|
OechslePricePlot.IsEnabled = true;
|
|
FillingInputs = false;
|
|
}
|
|
|
|
protected override async Task OnRenewContext() {
|
|
await RefreshGraphList();
|
|
}
|
|
|
|
private void InitPlot() {
|
|
UngebundenLegend = new LegendItem() {
|
|
Label = "Ungebunden",
|
|
LineWidth = 1,
|
|
LineColor = ColorUngebunden,
|
|
Marker = new MarkerStyle(MarkerShape.FilledCircle, 5, ColorUngebunden)
|
|
};
|
|
|
|
GebundenLegend = new LegendItem() {
|
|
Label = "Gebunden",
|
|
LineWidth = 1,
|
|
LineColor = ColorGebunden,
|
|
Marker = new MarkerStyle(MarkerShape.FilledCircle, 5, ColorGebunden)
|
|
};
|
|
|
|
LDWLegend = new LegendItem() {
|
|
Label = "68 °Oe (LDW)",
|
|
LineWidth = 2,
|
|
LineColor = Colors.Red,
|
|
Marker = MarkerStyle.None
|
|
};
|
|
|
|
QUWLegend = new LegendItem() {
|
|
Label = "73 °Oe (QUW)",
|
|
LineWidth = 2,
|
|
LineColor = Colors.Orange,
|
|
Marker = MarkerStyle.None
|
|
};
|
|
|
|
KABLegend = new LegendItem() {
|
|
Label = "84 °Oe (KAB)",
|
|
LineWidth = 2,
|
|
LineColor = Colors.Green,
|
|
Marker = MarkerStyle.None
|
|
};
|
|
|
|
RefreshGradationLines();
|
|
|
|
if (SelectedGraphEntry?.GebundenGraph != null) {
|
|
GebundenPlot = OechslePricePlot.Plot.Add.Scatter(SelectedGraphEntry.GebundenGraph.DataX, SelectedGraphEntry.GebundenGraph.DataY);
|
|
GebundenPlot.LineStyle.Color = ColorGebunden;
|
|
GebundenPlot.Color = ColorGebunden;
|
|
GebundenPlot.MarkerStyle = new MarkerStyle(MarkerShape.FilledCircle, 9, ColorGebunden);
|
|
}
|
|
|
|
DataPlot = OechslePricePlot.Plot.Add.Scatter(SelectedGraphEntry!.DataGraph.DataX, SelectedGraphEntry!.DataGraph.DataY);
|
|
DataPlot.LineStyle.Color = ColorUngebunden;
|
|
DataPlot.Color = ColorUngebunden;
|
|
DataPlot.MarkerStyle = new MarkerStyle(MarkerShape.FilledCircle, 9, ColorUngebunden);
|
|
|
|
if (SelectedGraphEntry?.GebundenGraph == null) {
|
|
ChangeActiveGraph(SelectedGraphEntry?.DataGraph);
|
|
}
|
|
|
|
OechslePricePlot.Interaction.Enable(new PlotActions() {
|
|
ZoomIn = StandardActions.ZoomIn,
|
|
ZoomOut = StandardActions.ZoomOut,
|
|
PanUp = StandardActions.PanUp,
|
|
PanDown = StandardActions.PanDown,
|
|
PanLeft = StandardActions.PanLeft,
|
|
PanRight = StandardActions.PanRight,
|
|
DragPan = StandardActions.DragPan,
|
|
DragZoom = StandardActions.DragZoom,
|
|
DragZoomRectangle = StandardActions.DragZoomRectangle,
|
|
ZoomRectangleClear = StandardActions.ZoomRectangleClear,
|
|
ZoomRectangleApply = StandardActions.ZoomRectangleApply,
|
|
AutoScale = StandardActions.AutoScale,
|
|
});
|
|
|
|
//OechslePricePlot.Plot.XAxis.ManualTickSpacing(1);
|
|
//OechslePricePlot.Plot.YAxis.ManualTickSpacing(0.1);
|
|
OechslePricePlot.Plot.Axes.SetLimits(Math.Min(GraphEntry.MinX, GraphEntry.MinXGeb) - 1, GraphEntry.MaxX + 1, -0.1, 2);
|
|
|
|
//OechslePricePlot.Plot.Layout(padding: 0);
|
|
//OechslePricePlot.Plot.XAxis2.Layout(padding: 0);
|
|
//OechslePricePlot.Plot.YAxis.Layout(padding: 0);
|
|
//OechslePricePlot.Plot.YAxis2.Layout(padding: 0);
|
|
|
|
HighlightedPointPlot = OechslePricePlot.Plot.Add.Marker(0, 0, MarkerShape.OpenCircle, 10, Colors.Red);
|
|
HighlightedPointPlot.IsVisible = false;
|
|
|
|
PrimaryMarkedPointPlot = OechslePricePlot.Plot.Add.Marker(0, 0, MarkerShape.FilledCircle, 6, Colors.Red);
|
|
PrimaryMarkedPointPlot.IsVisible = false;
|
|
|
|
SecondaryMarkedPointPlot = OechslePricePlot.Plot.Add.Marker(0, 0, MarkerShape.FilledCircle, 6, Colors.Red);
|
|
SecondaryMarkedPointPlot.IsVisible = false;
|
|
|
|
|
|
|
|
OechslePricePlot.Refresh();
|
|
|
|
RefreshFreeZoom();
|
|
OechslePricePlot.Refresh();
|
|
}
|
|
|
|
private void ResetPlot() {
|
|
PrimaryMarkedPoint = -1;
|
|
SecondaryMarkedPoint = -1;
|
|
ChangeActiveGraph(null);
|
|
HideGradationLines();
|
|
OechslePricePlot.Plot.Remove(DataPlot);
|
|
if (GebundenPlot != null) {
|
|
OechslePricePlot.Plot.Remove(GebundenPlot);
|
|
GebundenPlot = null;
|
|
}
|
|
OechslePricePlot.Plot.Clear();
|
|
OechslePricePlot.Reset();
|
|
OechslePricePlot.Refresh();
|
|
}
|
|
|
|
private void ChangeMarker(Marker point, bool visible, double x = 0, double y = 0) {
|
|
point.Location = new Coordinates(x, y);
|
|
point.IsVisible = visible;
|
|
}
|
|
|
|
private void EnableActionButtons() {
|
|
if (PaymentVar.TestVariant) {
|
|
LeftFlatButton.IsEnabled = true;
|
|
RightFlatButton.IsEnabled = true;
|
|
LinearIncreaseButton.IsEnabled = true;
|
|
}
|
|
}
|
|
|
|
private void DisableActionButtons() {
|
|
LeftFlatButton.IsEnabled = false;
|
|
RightFlatButton.IsEnabled = false;
|
|
InterpolateButton.IsEnabled = false;
|
|
LinearIncreaseButton.IsEnabled = false;
|
|
}
|
|
|
|
private void FreeZoomInput_Changed(object sender, RoutedEventArgs evt) {
|
|
RefreshFreeZoom();
|
|
}
|
|
|
|
private void RefreshFreeZoom() {
|
|
if (FreeZoomInput.IsChecked == true) {
|
|
UnlockZoom();
|
|
} else {
|
|
LockZoom();
|
|
}
|
|
OechslePricePlot.Refresh();
|
|
}
|
|
|
|
private void LockZoom() {
|
|
ScottPlot.AxisRules.MaximumBoundary BoundaryRule = new(
|
|
xAxis: OechslePricePlot.Plot.Axes.Bottom,
|
|
yAxis: OechslePricePlot.Plot.Axes.Left,
|
|
limits: new AxisLimits(GraphEntry.MinX - 1, GraphEntry.MaxX + 1, -0.1, 2));
|
|
|
|
ScottPlot.AxisRules.MaximumSpan SpanRule = new(
|
|
xAxis: OechslePricePlot.Plot.Axes.Bottom,
|
|
yAxis: OechslePricePlot.Plot.Axes.Left,
|
|
xSpan: GraphEntry.MaxX - GraphEntry.MinX + 2,
|
|
ySpan: 2.1);
|
|
|
|
OechslePricePlot.Plot.Axes.Rules.Clear();
|
|
OechslePricePlot.Plot.Axes.Rules.Add(BoundaryRule);
|
|
OechslePricePlot.Plot.Axes.Rules.Add(SpanRule);
|
|
OechslePricePlot.Plot.Axes.SetLimits(GraphEntry.MinX - 1, GraphEntry.MaxX + 1, -0.1, 2);
|
|
}
|
|
|
|
private void UnlockZoom() {
|
|
ScottPlot.AxisRules.MaximumSpan SpanRule = new(
|
|
xAxis: OechslePricePlot.Plot.Axes.Bottom,
|
|
yAxis: OechslePricePlot.Plot.Axes.Left,
|
|
xSpan: (GraphEntry.MaxX - GraphEntry.MinX) * 1.5,
|
|
ySpan: 3.5);
|
|
|
|
OechslePricePlot.Plot.Axes.Rules.Clear();
|
|
OechslePricePlot.Plot.Axes.Rules.Add(SpanRule);
|
|
}
|
|
|
|
private void EnableOptionButtons() {
|
|
FreeZoomInput.IsEnabled = true;
|
|
GradationLinesInput.IsEnabled = true;
|
|
TooltipInput.IsEnabled = true;
|
|
}
|
|
|
|
private void DisableOptionButtons() {
|
|
FreeZoomInput.IsEnabled = false;
|
|
GradationLinesInput.IsEnabled = false;
|
|
TooltipInput.IsEnabled = false;
|
|
}
|
|
|
|
private void GradationLinesInput_Changed(object sender, RoutedEventArgs evt) {
|
|
RefreshGradationLines();
|
|
}
|
|
|
|
private void RefreshGradationLines() {
|
|
if (GradationLinesInput.IsChecked == true && SelectedGraphEntry != null && !OechslePricePlot.Plot.PlottableList.OfType<VerticalLine>().Any()) {
|
|
ShowGradationLines();
|
|
ShowLegend();
|
|
} else if (GradationLinesInput.IsChecked == false) {
|
|
HideGradationLines();
|
|
HideLegend();
|
|
}
|
|
OechslePricePlot.Refresh();
|
|
}
|
|
|
|
private void ShowGradationLines() {
|
|
OechslePricePlot.Plot.Add.VerticalLine(68, 2, Colors.Red);
|
|
OechslePricePlot.Plot.Add.VerticalLine(73, 2, Colors.Orange);
|
|
OechslePricePlot.Plot.Add.VerticalLine(84, 2, Colors.Green);
|
|
}
|
|
|
|
private void HideGradationLines() {
|
|
OechslePricePlot.Plot.PlottableList.RemoveAll(p => p is VerticalLine);
|
|
}
|
|
|
|
private void ShowLegend() {
|
|
OechslePricePlot.Plot.Legend.Location = Alignment.UpperLeft;
|
|
OechslePricePlot.Plot.Legend.IsVisible = true;
|
|
|
|
OechslePricePlot.Plot.Legend.ManualItems.Add(LDWLegend);
|
|
OechslePricePlot.Plot.Legend.ManualItems.Add(QUWLegend);
|
|
OechslePricePlot.Plot.Legend.ManualItems.Add(KABLegend);
|
|
OechslePricePlot.Plot.Legend.ManualItems.Add(UngebundenLegend);
|
|
if (SelectedGraphEntry?.GebundenGraph != null) OechslePricePlot.Plot.Legend.ManualItems.Add(GebundenLegend);
|
|
}
|
|
|
|
private void HideLegend() {
|
|
OechslePricePlot.Plot.Legend.IsVisible = false;
|
|
OechslePricePlot.Plot.Legend.ManualItems.Clear();
|
|
}
|
|
|
|
private void OechsleInput_TextChanged(object sender, TextChangedEventArgs evt) {
|
|
if (ActiveGraph == null || SelectedGraphEntry == null) {
|
|
return;
|
|
}
|
|
|
|
bool success = int.TryParse(OechsleInput.Text, out int oechsle);
|
|
|
|
SecondaryMarkedPoint = -1;
|
|
ChangeMarker(SecondaryMarkedPointPlot, false);
|
|
|
|
if (success) {
|
|
if (oechsle >= ActiveGraph.MinX && oechsle <= ActiveGraph.MaxX) {
|
|
PrimaryMarkedPoint = oechsle - ActiveGraph.MinX;
|
|
ChangeMarker(PrimaryMarkedPointPlot, true, ActiveGraph.GetOechsleAt(PrimaryMarkedPoint), ActiveGraph.GetPriceAt(PrimaryMarkedPoint));
|
|
|
|
PriceInput.Text = Math.Round(ActiveGraph.GetPriceAt(PrimaryMarkedPoint), Season.Precision).ToString();
|
|
|
|
EnableActionButtons();
|
|
OechslePricePlot.Refresh();
|
|
EnableUnitTextBox(PriceInput);
|
|
return;
|
|
}
|
|
}
|
|
|
|
PrimaryMarkedPoint = -1;
|
|
ChangeMarker(PrimaryMarkedPointPlot, false);
|
|
DisableActionButtons();
|
|
PriceInput.Text = "";
|
|
DisableUnitTextBox(PriceInput);
|
|
OechslePricePlot.Refresh();
|
|
DisableUnitTextBox(PriceInput);
|
|
}
|
|
|
|
private void PriceInput_TextChanged(object sender, TextChangedEventArgs evt) {
|
|
if (PrimaryMarkedPoint != -1 && ActiveGraph != null && PriceInput.IsKeyboardFocusWithin == true) {
|
|
var res = Validator.CheckDecimal(PriceInput.TextBox, true, 2, Season.Precision);
|
|
if (res.IsValid && double.TryParse(PriceInput.Text, out double price)) {
|
|
ActiveGraph.SetPriceAt(PrimaryMarkedPoint, price);
|
|
PrimaryMarkedPointPlot.Location = new Coordinates(PrimaryMarkedPointPlot.Location.X, price);
|
|
SetHasChanged();
|
|
OechslePricePlot.Refresh();
|
|
CheckGebundenTypeFixed();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void LeftFlatButton_Click(object sender, RoutedEventArgs evt) {
|
|
if (PrimaryMarkedPoint == -1 || ActiveGraph == null) {
|
|
return;
|
|
}
|
|
ActiveGraph.FlattenGraphLeft(PrimaryMarkedPoint);
|
|
SetHasChanged();
|
|
OechslePricePlot.Refresh();
|
|
CheckGebundenTypeFixed();
|
|
}
|
|
|
|
private void RightFlatButton_Click(object sender, RoutedEventArgs evt) {
|
|
if (PrimaryMarkedPoint == -1 || ActiveGraph == null) {
|
|
return;
|
|
}
|
|
ActiveGraph.FlattenGraphRight(PrimaryMarkedPoint);
|
|
SetHasChanged();
|
|
OechslePricePlot.Refresh();
|
|
CheckGebundenTypeFixed();
|
|
}
|
|
|
|
private void InterpolateButton_Click(object sender, RoutedEventArgs evt) {
|
|
if (PrimaryMarkedPoint == SecondaryMarkedPoint || PrimaryMarkedPoint == -1 || SecondaryMarkedPoint == -1 || ActiveGraph == null) {
|
|
return;
|
|
}
|
|
ActiveGraph.InterpolateGraph(PrimaryMarkedPoint, SecondaryMarkedPoint);
|
|
SetHasChanged();
|
|
OechslePricePlot.Refresh();
|
|
CheckGebundenTypeFixed();
|
|
}
|
|
|
|
private void LinearIncreaseButton_Click(object sender, RoutedEventArgs e) {
|
|
if (PrimaryMarkedPoint == -1 || ActiveGraph == null) {
|
|
return;
|
|
}
|
|
double? priceIncrease = Utils.ShowLinearPriceIncreaseDialog();
|
|
if (priceIncrease == null) {
|
|
return;
|
|
}
|
|
ActiveGraph.LinearIncreaseGraphToEnd(PrimaryMarkedPoint, priceIncrease.Value);
|
|
SetHasChanged();
|
|
OechslePricePlot.Refresh();
|
|
CheckGebundenTypeFixed();
|
|
}
|
|
|
|
private void OechslePricePlot_MouseDown(object sender, MouseEventArgs e) {
|
|
if (GraphList.SelectedItem == null) {
|
|
return;
|
|
}
|
|
|
|
if (HoverActive) {
|
|
if (PaymentVar.TestVariant && Keyboard.IsKeyDown(System.Windows.Input.Key.LeftCtrl)) {
|
|
if (PrimaryMarkedPoint == -1 || ActiveGraph == null || ActiveGraph != Highlighted.graph) {
|
|
return;
|
|
}
|
|
SecondaryMarkedPoint = Highlighted.index;
|
|
|
|
ChangeMarker(SecondaryMarkedPointPlot, true, ActiveGraph.GetOechsleAt(SecondaryMarkedPoint), ActiveGraph.GetPriceAt(SecondaryMarkedPoint));
|
|
|
|
InterpolateButton.IsEnabled = true;
|
|
|
|
return;
|
|
}
|
|
|
|
PrimaryMarkedPoint = Highlighted.index;
|
|
if (ActiveGraph != Highlighted.graph) ChangeActiveGraph(Highlighted.graph);
|
|
|
|
ChangeMarker(PrimaryMarkedPointPlot, true, ActiveGraph.GetOechsleAt(PrimaryMarkedPoint), ActiveGraph.GetPriceAt(PrimaryMarkedPoint));
|
|
|
|
OechsleInput.Text = Highlighted.graph.GetOechsleAt(Highlighted.index).ToString();
|
|
PriceInput.Text = Highlighted.graph.GetPriceAt(Highlighted.index).ToString();
|
|
|
|
EnableActionButtons();
|
|
} else {
|
|
PrimaryMarkedPoint = -1;
|
|
SecondaryMarkedPoint = -1;
|
|
if (SelectedGraphEntry!.GebundenGraph != null) {
|
|
ChangeActiveGraph(null);
|
|
}
|
|
ChangeMarker(PrimaryMarkedPointPlot, false);
|
|
ChangeMarker(SecondaryMarkedPointPlot, false);
|
|
|
|
OechsleInput.Text = "";
|
|
PriceInput.Text = "";
|
|
DisableUnitTextBox(PriceInput);
|
|
|
|
DisableActionButtons();
|
|
}
|
|
}
|
|
|
|
private void OechslePricePlot_MouseMove(object sender, MouseEventArgs e) {
|
|
MouseChange(e);
|
|
}
|
|
|
|
private void OechslePricePlot_MouseWheel(object sender, MouseWheelEventArgs e) {
|
|
MouseChange(e);
|
|
}
|
|
|
|
private void MouseChange(MouseEventArgs e) {
|
|
if (GraphList.SelectedItem == null) {
|
|
return;
|
|
}
|
|
|
|
(double x, double y, int index)? mouseOnData = MouseOnPlot(DataPlot, e.GetPosition(OechslePricePlot));
|
|
(double x, double y, int index)? mouseOnGebunden = MouseOnPlot(GebundenPlot, e.GetPosition(OechslePricePlot));
|
|
|
|
Highlighted = LastHighlighted;
|
|
|
|
if (mouseOnData != null) {
|
|
ChangeMarker(HighlightedPointPlot, true, mouseOnData.Value.x, mouseOnData.Value.y);
|
|
HighlightedPointPlot.IsVisible = true;
|
|
HoverChanged = true ^ HoverActive;
|
|
HoverActive = true;
|
|
HandleTooltip(mouseOnData.Value.x, mouseOnData.Value.y, mouseOnData.Value.index, SelectedGraphEntry!.DataGraph, e.GetPosition(OechslePricePlot), e is MouseWheelEventArgs);
|
|
} else if (mouseOnGebunden != null) {
|
|
ChangeMarker(HighlightedPointPlot, true, mouseOnGebunden.Value.x, mouseOnGebunden.Value.y);
|
|
HighlightedPointPlot.IsVisible = true;
|
|
HoverChanged = true ^ HoverActive;
|
|
HoverActive = true;
|
|
HandleTooltip(mouseOnGebunden.Value.x, mouseOnGebunden.Value.y, mouseOnGebunden.Value.index, SelectedGraphEntry!.GebundenGraph!, e.GetPosition(OechslePricePlot), e is MouseWheelEventArgs);
|
|
} else {
|
|
ChangeMarker(HighlightedPointPlot, false);
|
|
HoverChanged = false ^ HoverActive;
|
|
HoverActive = false;
|
|
OechslePricePlot.Plot.PlottableList.Remove(TooltipPlot);
|
|
OechslePricePlot.Refresh();
|
|
}
|
|
}
|
|
|
|
private (double, double, int)? MouseOnPlot(Scatter? plot, Point p) {
|
|
if (plot == null) {
|
|
return null;
|
|
}
|
|
|
|
OechslePricePlot.Refresh();
|
|
Pixel mousePixel = new(p.X, p.Y);
|
|
Coordinates mouseLocation = OechslePricePlot.Plot.GetCoordinates(mousePixel);
|
|
DataPoint nearestPoint = plot.Data.GetNearest(mouseLocation, OechslePricePlot.Plot.LastRender, 3);
|
|
|
|
if (nearestPoint.IsReal) {
|
|
return (nearestPoint.X, nearestPoint.Y, nearestPoint.Index);
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private void HandleTooltip(double pointX, double pointY, int pointIndex, Graph g, Point p, bool force) {
|
|
if (force || LastHighlighted != Highlighted || HoverChanged) {
|
|
OechslePricePlot.Plot.PlottableList.Remove(TooltipPlot);
|
|
if (TooltipInput.IsChecked == true) {
|
|
Pixel mousePixel = new(p.X, p.Y - 30);
|
|
Coordinates mouseLocation = OechslePricePlot.Plot.GetCoordinates(mousePixel);
|
|
TooltipPlot = OechslePricePlot.Plot.Add.Text($"Oechsle: {pointX:N2}, Preis: {Math.Round(pointY, Season.Precision)}€/kg", mouseLocation.X, mouseLocation.Y);
|
|
TooltipPlot.Label.FontSize = 12;
|
|
TooltipPlot.Label.Bold = true;
|
|
TooltipPlot.Label.BorderColor = Colors.Black;
|
|
TooltipPlot.Label.BorderWidth = 2;
|
|
TooltipPlot.Label.BackColor = Colors.White;
|
|
TooltipPlot.Label.Padding = 10;
|
|
TooltipPlot.Label.Alignment = Alignment.MiddleLeft;
|
|
}
|
|
LastHighlighted = (g, pointIndex);
|
|
HoverChanged = false;
|
|
OechslePricePlot.Refresh();
|
|
}
|
|
}
|
|
|
|
private int GetMaxGraphId() {
|
|
return GraphEntries.Count == 0 ? 0 : GraphEntries.Select(g => g.Id).Max();
|
|
}
|
|
|
|
private void AddButton_Click(object sender, RoutedEventArgs e) {
|
|
GraphEntry newGraphEntry = new(GetMaxGraphId() + 1, Season.Precision, BillingData.CurveMode.Oe);
|
|
GraphEntries.Add(newGraphEntry);
|
|
SetHasChanged();
|
|
GraphList.Items.Refresh();
|
|
GraphList.SelectedItem = newGraphEntry;
|
|
}
|
|
|
|
private void CopyButton_Click(object sender, RoutedEventArgs e) {
|
|
if (SelectedGraphEntry == null) return;
|
|
|
|
GraphEntry newGraphEntry = SelectedGraphEntry.Copy(GetMaxGraphId() + 1);
|
|
GraphEntries.Add(newGraphEntry);
|
|
SetHasChanged();
|
|
GraphList.Items.Refresh();
|
|
GraphList.SelectedItem = newGraphEntry;
|
|
}
|
|
|
|
private void DeleteButton_Click(object sender, RoutedEventArgs e) {
|
|
if (SelectedGraphEntry == null) return;
|
|
|
|
var r = MessageBox.Show(
|
|
$"Soll der Graph {SelectedGraphEntry.Id} (verwendet in folgenden Verträgen: {SelectedGraphEntry.ContractsStringSimple}) wirklich gelöscht werden?",
|
|
"Graph löschen", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No);
|
|
|
|
if (r == MessageBoxResult.Yes) {
|
|
GraphEntries.Remove(SelectedGraphEntry);
|
|
SetHasChanged();
|
|
GraphList.Items.Refresh();
|
|
OechslePricePlot.IsEnabled = false;
|
|
}
|
|
}
|
|
|
|
private async void SaveButton_Click(object sender, RoutedEventArgs e) {
|
|
var origData = BillingData.FromJson(PaymentVar.Data);
|
|
var data = BillingData.FromGraphEntries(GraphEntries, origData, Utils.GetAttributeVarieties(Context, Year, true));
|
|
|
|
EntityEntry<PaymentVar>? tr = null;
|
|
try {
|
|
PaymentVar.Data = data.ToJsonString();
|
|
tr = Context.Update(PaymentVar);
|
|
await Context.SaveChangesAsync();
|
|
LockContext = false;
|
|
tr = null;
|
|
await App.HintContextChange();
|
|
} catch (Exception exc) {
|
|
if (tr != null) await tr.ReloadAsync();
|
|
var str = "Der Eintrag konnte nicht in der Datenbank gespeichert werden!\n\n" + exc.Message;
|
|
if (exc.InnerException != null) str += "\n\n" + exc.InnerException.Message;
|
|
MessageBox.Show(str, "Auszahlungsvariante speichern", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
LockContext = true;
|
|
SetHasChanged(false);
|
|
}
|
|
|
|
private void EnableUnitTextBox(UnitTextBox u) {
|
|
if (PaymentVar.TestVariant) {
|
|
u.IsEnabled = true;
|
|
u.TextBox.IsReadOnly = false;
|
|
}
|
|
}
|
|
|
|
private void DisableUnitTextBox(UnitTextBox u) {
|
|
u.IsEnabled = false;
|
|
u.TextBox.IsReadOnly = true;
|
|
}
|
|
|
|
private void ChangeActiveGraph(Graph? g) {
|
|
if (g != null && g == SelectedGraphEntry?.DataGraph) {
|
|
EnableUnitTextBox(OechsleInput);
|
|
if (SelectedGraphEntry?.GebundenGraph != null) ChangeLineWidth(DataPlot, 4);
|
|
ChangeLineWidth(GebundenPlot, 1);
|
|
} else if (g != null && g == SelectedGraphEntry?.GebundenGraph) {
|
|
EnableUnitTextBox(OechsleInput);
|
|
ChangeLineWidth(GebundenPlot, 4);
|
|
ChangeLineWidth(DataPlot, 1);
|
|
} else {
|
|
DisableUnitTextBox(OechsleInput);
|
|
DisableUnitTextBox(PriceInput);
|
|
OechsleInput.Text = "";
|
|
PriceInput.Text = "";
|
|
ChangeLineWidth(DataPlot, 1);
|
|
ChangeLineWidth(GebundenPlot, 1);
|
|
}
|
|
ActiveGraph = g;
|
|
}
|
|
|
|
private void ChangeLineWidth(Scatter? p, double lineWidth) {
|
|
if (p != null) {
|
|
p.LineWidth = (float)lineWidth;
|
|
}
|
|
}
|
|
|
|
private void GraphList_SelectionChanged(object sender, SelectionChangedEventArgs e) {
|
|
RefreshInputs();
|
|
}
|
|
|
|
private void PriceInput_LostFocus(object sender, RoutedEventArgs e) {
|
|
|
|
}
|
|
|
|
private void OechsleInput_LostFocus(object sender, RoutedEventArgs e) {
|
|
|
|
}
|
|
|
|
private void GebundenFlatBonus_TextChanged(object sender, TextChangedEventArgs e) {
|
|
if (FillingInputs) return;
|
|
var r = Validator.CheckDecimal(GebundenFlatBonus.TextBox, true, 2, Season.Precision);
|
|
if (r.IsValid && SelectedGraphEntry != null) {
|
|
SelectedGraphEntry.GebundenFlatBonus = double.Parse(GebundenFlatBonus.Text);
|
|
ResetPlot();
|
|
InitPlot();
|
|
}
|
|
}
|
|
|
|
private void ContractInput_Changed(object sender, ItemSelectionChangedEventArgs e) {
|
|
if (FillingInputs) return;
|
|
if (e.IsSelected == true) {
|
|
bool success = RemoveContractFromOtherGraphEntries(e.Item.ToString());
|
|
if (!success) {
|
|
ContractInput.SelectedItems.Remove(e.Item);
|
|
}
|
|
}
|
|
var r = ContractInput.SelectedItems.Cast<ContractSelection>();
|
|
SelectedGraphEntry!.Contracts = r.ToList();
|
|
SetHasChanged();
|
|
GraphList.Items.Refresh();
|
|
}
|
|
|
|
private bool RemoveContractFromOtherGraphEntries(string? contract) {
|
|
if (contract == null) return true;
|
|
foreach (var ge in GraphEntries) {
|
|
if (ge != SelectedGraphEntry && ge.Abgewertet == SelectedGraphEntry?.Abgewertet) {
|
|
var toRemove = ge.Contracts.Where(c => c.Listing.Equals(contract)).ToList();
|
|
if (toRemove.Count == 0) continue;
|
|
var r = MessageBox.Show($"Achtung: {string.Join(", ", toRemove)} ist bereits in Graph {ge.Id} in Verwendung!\nSoll die Zuweisung dort entfernt werden?", "Entfernen bestätigen",
|
|
MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No);
|
|
if (r != MessageBoxResult.Yes) {
|
|
return false;
|
|
}
|
|
ge.Contracts.RemoveAll(c => c.Listing.Equals(contract));
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private void AbgewertetInput_Changed(object sender, RoutedEventArgs e) {
|
|
if (FillingInputs) return;
|
|
if (SelectedGraphEntry == null) return;
|
|
SelectedGraphEntry.Abgewertet = AbgewertetInput.IsChecked == true;
|
|
SetHasChanged();
|
|
GraphList.Items.Refresh();
|
|
}
|
|
|
|
private void GebundenType_Checked(object sender, RoutedEventArgs e) {
|
|
if (FillingInputs) return;
|
|
if (SelectedGraphEntry == null) {
|
|
DisableUnitTextBox(GebundenFlatBonus);
|
|
return;
|
|
}
|
|
if (GebundenTypeNone.IsChecked == true) {
|
|
SelectedGraphEntry.RemoveGebundenGraph();
|
|
DisableUnitTextBox(GebundenFlatBonus);
|
|
} else if (GebundenTypeFixed.IsChecked == true) {
|
|
SelectedGraphEntry.GebundenFlatBonus = double.TryParse(GebundenFlatBonus.Text, out var val) ? val : 0.1;
|
|
SelectedGraphEntry.AddGebundenGraph();
|
|
EnableUnitTextBox(GebundenFlatBonus);
|
|
} else if (GebundenTypeGraph.IsChecked == true) {
|
|
SelectedGraphEntry.AddGebundenGraph();
|
|
DisableUnitTextBox(GebundenFlatBonus);
|
|
}
|
|
SetHasChanged();
|
|
RefreshInputs();
|
|
}
|
|
|
|
private void CheckGebundenTypeFixed() {
|
|
FillingInputs = true;
|
|
if (SelectedGraphEntry?.GebundenFlatBonus is double bonus) {
|
|
GebundenTypeFixed.IsChecked = true;
|
|
GebundenFlatBonus.Text = $"{bonus}";
|
|
EnableUnitTextBox(GebundenFlatBonus);
|
|
} else if (SelectedGraphEntry?.GebundenGraph != null) {
|
|
GebundenTypeGraph.IsChecked = true;
|
|
GebundenFlatBonus.Text = "";
|
|
DisableUnitTextBox(GebundenFlatBonus);
|
|
}
|
|
FillingInputs = false;
|
|
}
|
|
}
|
|
}
|