// Copyright (C) Stichting Deltares 2025. All rights reserved.
//
// This file is part of the application DAM - UI.
//
// DAM - UI is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
//
// All names, logos, and references to "Deltares" are registered trademarks of
// Stichting Deltares and remain full property of Stichting Deltares at all times.
// All rights reserved.
using Deltares.Dam.Data;
using Deltares.Standard.EventPublisher;
using Deltares.Standard.EventPublisher.Enum;
using Deltares.Standard.Forms;
using Deltares.Standard.Forms.DExpress;
using System;
using System.Windows.Forms;
namespace Deltares.Dam.Forms
{
public partial class DamProjectCalculationSpecificationPropertyControl : UserControl, IPropertyControl
{
private DamProjectCalculationSpecification damProjectCalculationSpecification;
private DamProjectData damProjectData;
private bool firstEdit;
public DamProjectCalculationSpecificationPropertyControl()
{
InitializeComponent();
Name = "Calculation";
BindSupport.BindTextAndValue(CalculationDefinitionPanelControl, FailureMechanismLabel, FailureMechanismCombobox, p => p.FailureMechanismSystemType);
BindSupport.BindTextAndValue(CalculationDefinitionPanelControl, CalculationModelLabel, CalculationModelComboBox, p => p.CalculationModel);
DataEventPublisher.OnSelectionChanged += DataEventPublisher_OnSubSelectionChanged;
DataEventPublisher.OnAfterChange += DataEventPublisher_OnAfterChange;
FormsSupport.RepairRightAnchoredControls(this);
FormsSupport.AdjustSizeToContents(CalculationDefinitionPanelControl);
FormsSupport.AdjustSizeToContents(damProjectCalculationOptionsPropertyControl1);
firstEdit = true;
}
private void DataEventPublisher_OnAfterChange(object sender, PublishEventArgs e)
{
var specification = sender as DamProjectCalculationSpecification;
if (specification != null && damProjectCalculationSpecification != null)
{
damProjectCalculationSpecification = specification;
SetSelectedDamCalculationSpecification();
}
if (sender is DamProject pd)
{
damProjectCalculationSpecification = pd.DamProjectData.DamProjectCalculationSpecification;
//currentSpecification = pd.DamProjectData.DamProjectCalculationSpecification.DamStabilityParameters;
SetSelectedDamCalculationSpecification();
}
}
private void SetSelectedDamCalculationSpecification()
{
DamProjectCalculationSpecification selection = damProjectCalculationSpecification;
if (selection != null)
{
selection.DamStabilityParameters.SlipCircleDefinition.Specification = selection;
BindSupport.Assign(damProjectCalculationOptionsPropertyControl1, selection);
if (firstEdit)
{
// Ensure that all edits of the damProjectCalculationOptionsPropertyControl1 are updated at start
damProjectCalculationOptionsPropertyControl1.SelectedObject = selection;
}
if (CalculationDefinitionPanelControl.InvokeRequired)
{
Action action = () => BindSupport.Assign(CalculationDefinitionPanelControl, selection);
CalculationDefinitionPanelControl.Invoke(action);
}
else
{
BindSupport.Assign(CalculationDefinitionPanelControl, selection);
}
if (selection.FailureMechanismSystemType != FailureMechanismSystemType.Piping && firstEdit)
{
// this is a hack to force update on ill placed edits.
Width += 1;
firstEdit = false;
}
}
}
private void DataEventPublisher_OnSubSelectionChanged(object sender, PublishEventArgs e)
{
if (((SelectionEventArgs)e).PropertyEditorReactionType == PropertyEditorReactionType.Ignore &&
sender is DamProjectCalculationSpecification item &&
damProjectData.DamProjectCalculationSpecifications.Contains(item))
{
damProjectCalculationSpecification = item;
SetSelectedDamCalculationSpecification();
}
DamProjectData data = sender as DamProjectData;
if (data != null && ((SelectionEventArgs)e).PropertyEditorReactionType == PropertyEditorReactionType.Update &&
data.DamProjectCalculationSpecifications.Contains(data.DamProjectCalculationSpecification))
{
damProjectData = data;
damProjectCalculationSpecification = data.DamProjectCalculationSpecification;
SetSelectedDamCalculationSpecification();
SelectedObject = data;
}
}
#region IPropertyControl Members
public bool IsVisible => damProjectData is { DamProjectType: DamProjectType.Design };
public object SelectedObject
{
get => damProjectData;
set
{
var data = value as DamProjectData;
if (data != null)
{
damProjectData = data;
damProjectCalculationSpecification = damProjectData.DamProjectCalculationSpecification;
BindSupport.Assign(this, damProjectCalculationSpecification);
BindSupport.Assign(CalculationDefinitionPanelControl, data.DamProjectCalculationSpecifications);
SetSelectedDamCalculationSpecification();
CalculationSpecificationsGroupControl.Visible = true;
}
}
}
#endregion
}
}