// Copyright (C) Stichting Deltares 2016. All rights reserved.
//
// This file is part of the D-Soil Model application.
//
// The D-Soil Model application 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 System;
using System.Collections;
using System.Collections.Generic;
using Deltares.DSoilModel.Data;
using Deltares.Geotechnics;
using Deltares.Geotechnics.ConePenetrationTest;
using Deltares.Probabilistic;
using Deltares.Standard.Attributes;
using Deltares.Standard.Forms.DExpress;
using Deltares.Standard.Reflection;
namespace Deltares.DSoilModel.Forms
{
public class DSoilModelContext : GeotechnicsContext
{
#region Properties for user column filters
private readonly HashSet alwaysVisibleProperties = new HashSet
{
StaticReflection.GetMemberName(s => s.Name),
StaticReflection.GetMemberName(s => s.Description),
StaticReflection.GetMemberName(s => s.Color),
StaticReflection.GetMemberName(s => s.SoilType),
};
private readonly HashSet macrostabilitySoilProperties = new HashSet
{
StaticReflection.GetMemberName(s => s.AbovePhreaticLevel),
StaticReflection.GetMemberName(s => s.AbovePhreaticLevelStochast),
StaticReflection.GetMemberName(s => s.BelowPhreaticLevel),
StaticReflection.GetMemberName(s => s.BelowPhreaticLevelStochast),
StaticReflection.GetMemberName(s => s.ShearStrengthModel),
StaticReflection.GetMemberName(s => s.Cohesion),
StaticReflection.GetMemberName(s => s.CohesionStochast),
StaticReflection.GetMemberName(s => s.FrictionAngle),
StaticReflection.GetMemberName(s => s.FrictionAngleStochast),
StaticReflection.GetMemberName(s => s.UsePop),
StaticReflection.GetMemberName(s => s.POP),
StaticReflection.GetMemberName(s => s.POPStochast),
StaticReflection.GetMemberName(s => s.RatioCuPc),
StaticReflection.GetMemberName(s => s.RatioCuPcStochast),
StaticReflection.GetMemberName(s => s.StrengthIncreaseExponent),
StaticReflection.GetMemberName(s => s.StrengthIncreaseExponentStochast)
};
private readonly HashSet pipingSoilProperties = new HashSet
{
StaticReflection.GetMemberName(s => s.BelowPhreaticLevelStochast),
StaticReflection.GetMemberName(s => s.BelowPhreaticLevel),
StaticReflection.GetMemberName(s => s.DiameterD70),
StaticReflection.GetMemberName(s => s.DiameterD70Stochast),
StaticReflection.GetMemberName(s => s.PermeabKx),
StaticReflection.GetMemberName(s => s.PermeabKxStochast),
};
private readonly HashSet flowslideSoilProperties = new HashSet
{
StaticReflection.GetMemberName(s => s.DiameterD15),
StaticReflection.GetMemberName(s => s.DiameterD50),
StaticReflection.GetMemberName(s => s.FrictionAngle),
StaticReflection.GetMemberName(s => s.Porosity),
StaticReflection.GetMemberName(s => s.MinPorosity),
StaticReflection.GetMemberName(s => s.MaxPorosity),
StaticReflection.GetMemberName(s => s.Evoldm),
StaticReflection.GetMemberName(s => s.Ks0),
StaticReflection.GetMemberName(s => s.GammaKorrel),
StaticReflection.GetMemberName(s => s.ParM),
StaticReflection.GetMemberName(s => s.ParU),
StaticReflection.GetMemberName(s => s.ParV),
StaticReflection.GetMemberName(s => s.ParR),
StaticReflection.GetMemberName(s => s.ParS2),
};
private readonly HashSet combinedSoilProperties = null;
#endregion
public DSoilModelContext()
{
combinedSoilProperties = new HashSet();
combinedSoilProperties.UnionWith(macrostabilitySoilProperties);
combinedSoilProperties.UnionWith(pipingSoilProperties);
combinedSoilProperties.UnionWith(flowslideSoilProperties);
}
///
/// The current filter to display all parameters, stochastic parameters only or as-is parameters only.
///
public ParameterViewSettings ParameterView { get; set; }
///
/// Method indicating a visibility override value for a given instance object.
///
/// Object being evaluated.
/// Name of the member which is part of .
///
/// True if visible; False if hidden; Null if no override.
///
public override bool? IsVisible(object source, string member)
{
if (source is BaseControlBinding || source is GridViewControl)
{
// Make sure popup menu item for adaption of units is disabled in gridview.
if (member == "EditUnitCommand")
{
return false;
}
}
var grid = source as GridViewControl;
if (grid != null)
{
if (member == "AddRowCommand" && (grid.Name == "GeometryPointsGridViewControl" || grid.Name == "GeometryCurvesGridViewControl"))
{
return false;
}
}
if (source is MainForm)
{
// hide units command from tools menu
if (member == "SelectUnits")
{
return false;
}
if (member == "ExecuteDeleteCommand")
{
return false;
}
}
if (source is SosSoilLayer1D && member == ((SosSoilLayer1D)source).GetMemberName(x => x.BottomLevel))
{
return false;
}
if (source is SoilLayer1D && member == ((SoilLayer1D)source).GetMemberName(x => x.BottomLevel))
{
return false;
}
var boring = source as Boring;
if (boring != null)
{
if (member == boring.GetMemberName(b => b.TopVoid) ||
member == boring.GetMemberName(b => b.BottomVoid) ||
member == boring.GetMemberName(b => b.MedianVoid) ||
member == boring.GetMemberName(b => b.GammaSatVoid) ||
member == boring.GetMemberName(b => b.GammaUnsatVoid))
{
return false;
}
}
var layer = source as BoringLayer;
if (layer != null)
{
if (member == layer.GetMemberName(x => x.Name))
{
return false;
}
if (member == layer.GetMemberName(x => x.Description))
{
return false;
}
if (member == layer.GetMemberName(x => x.IsAquifer))
{
return false;
}
if (member == layer.GetMemberName(x => x.Height))
{
return false;
}
if (member == layer.GetMemberName(x => x.SandMedian))
{
return layer.IsSandMedianUsed;
}
if (member == layer.GetMemberName(x => x.SandMedian))
{
return layer.IsSandMedianUsed;
}
if (member == layer.GetMemberName(x => x.GammaSat))
{
return layer.IsGammaSatUsed;
}
if (member == layer.GetMemberName(x => x.GammaUnsat))
{
return layer.IsGammaUnsatUsed;
}
}
if (source is BoringDatarow || (source is Boring && member == ((Boring)source).GetMemberName(x => x.BoringDatarows)))
{
return false;
}
var profile1D = source as SoilProfile1D;
if (profile1D != null)
{
if (member == profile1D.GetMemberName(sp1 => sp1.Location) ||
member == profile1D.GetMemberName(x => x.ProbabilityOfOccurrence) ||
member == profile1D.GetMemberName(x => x.X) ||
member == profile1D.GetMemberName(x => x.Y))
return false;
}
var profile2D = source as SoilProfile2D;
if (profile2D != null)
{
if (member == profile2D.GetMemberName(sp1 => sp1.Location) ||
member == profile2D.GetMemberName(sp1 => sp1.XBegin) ||
member == profile2D.GetMemberName(sp1 => sp1.XEnd) ||
member == profile2D.GetMemberName(sp1 => sp1.YBegin) ||
member == profile2D.GetMemberName(sp1 => sp1.YEnd) ||
member == profile2D.GetMemberName(sp1 => sp1.Length))
{
return false;
}
}
var soil = source as Soil;
if (soil != null)
{
// For Wti version, stresstables are not to be used (DSB-506)
if (member == soil.GetMemberName(s => s.UseDefaultShearStrengthModel) ||
member == soil.GetMemberName(s => s.StressTable) ||
member == soil.GetMemberName(s => s.StressTableStochast) ||
member == soil.GetMemberName(s => s.BondStressTable))
{
return false;
}
bool? visibleAccordingParameterView = VisibleAccordingParameterView(member);
if (visibleAccordingParameterView != null && !(bool)visibleAccordingParameterView)
{
return false;
}
bool? visibleAccordingSoilUserFilter = VisibleAccordingSoilUserFilter(member);
if (visibleAccordingSoilUserFilter != null)
{
return visibleAccordingSoilUserFilter;
}
if (member == "StressTableStochast") // DSB-299, used only in the "old" stability kernel
{
return false;
}
}
if (source is SurfaceLine2)
{
var surfaceLine = (SurfaceLine2)source;
if (member == surfaceLine.GetMemberName(x => x.GeometryMustIncludePoint_Xml))
{
return false;
}
if (member == surfaceLine.GetMemberName(x => x.LandwardDirection))
{
return false;
}
if (member == surfaceLine.GetMemberName(x => x.Geometry))
{
return false;
}
}
else if (source is SoilProfile1D || source is SoilProfile2D)
{
if (member == "ChangeLog")
{
return true;
}
}
if (source is PreConsolidationStress)
{
if (member == ((PreConsolidationStress)source).GetMemberName(x => x.StressStochast))
{
return ParameterView != ParameterViewSettings.AsIsParameters;
}
else if (member == ((PreConsolidationStress)source).GetMemberName(x => x.StressValue))
{
return ParameterView != ParameterViewSettings.ProbabilisticParameters;
}
}
//See DSB-538: for now do not use zones, old value = true
var segment = source as SoilSegment;
if (segment != null)
{
if (member == segment.GetMemberName(seg => seg.CreateSettlementZones))
{
return false;
}
}
// hide import of old project database (Geobase), see DSB-715
if (source is DSoilModelPlugin && member == "OldProjectImport")
{
return false;
}
return base.IsVisible(source, member);
}
///
/// Method indicating if the enabled override value for a given instance object.
///
/// Object being evaluated.
/// Name of the member which is part of .
///
/// True if enabled; False if disabled; Null if no override.
///
public override bool? IsEnabled(object source, string member)
{
if (source is ConePenetrationTestData)
{
return false;
}
if (source is Boring || source is BoringLayer)
{
return false;
}
if (source is ConePenetrationTestPerSegment && member == ((ConePenetrationTestPerSegment)source).GetMemberName(x => x.Xlocal))
{
return false;
}
if (source is BoringPerSegment && member == ((BoringPerSegment)source).GetMemberName(x => x.Xlocal))
{
return false;
}
var soil = source as Soil;
if (soil != null)
{
BindPropertyInfo propertyInfo = PropertyInfoSupport.GetPropertyInfo(typeof(Soil), member);
if (propertyInfo != null)
{
if (!propertyInfo.CanWrite)
{
return false;
}
}
if (member.EndsWith("Stochast"))
{
if (ParameterView == ParameterViewSettings.AsIsParameters)
{
return false;
}
}
else if (!alwaysVisibleProperties.Contains(member))
{
if (ParameterView == ParameterViewSettings.ProbabilisticParameters)
{
return false;
}
}
return true; // all other parameters should be editable
}
var cp = source as CharacteristicPoint;
if (member == cp.GetMemberName(p => p.X) || member == cp.GetMemberName(p => p.Z))
return false;
var grid = source as GridViewControl;
if (grid != null)
{
switch (member)
{
case "AddRowCommand":
case "InsertRowCommand":
switch (grid.Name)
{
case "SoilSegmentsTable":
case "SurfaceLinePointsGridControl":
case "SurfaceLinesTable":
return false;
}
break;
case "DeleteRowCommand":
switch (grid.Name)
{
case "SurfaceLinePointsGridControl":
return false;
}
break;
case "CopyCommand":
switch (grid.Name)
{
case "CptsGridViewControl":
case "BoringsGridViewControl":
case "SurfaceLinePointsGridControl":
return false;
}
break;
case "PasteCommand":
case "EditCommand":
switch (grid.Name)
{
case "CptsGridViewControl":
case "BoringsGridViewControl":
case "SurfaceLinePointsGridControl":
case "CPTsTable":
case "BoringsTable":
return false;
}
break;
}
}
var geometryEditor = source as GeometryEditor;
if (geometryEditor != null)
{
if (member == "DeleteSelectedObjects")
{
var selection = geometryEditor.SpatialEditor.EmptySelection;
if (selection != null && (selection.DataObject is ConePenetrationTestData || selection.DataObject is Boring))
{
return false;
}
}
}
return base.IsEnabled(source, member);
}
///
/// Method returning a collection of domain object for list or enum type members;
///
/// Object being evaluated.
/// Name of the member which is part of .
/// A collection of domain object; Null if no override.
public override ICollection GetDomain(object source, string member)
{
if (source is Soil && member == StaticReflection.GetMemberName(s => s.ShearStrengthModel))
{
return new List
{
ShearStrengthModel.CPhi,
ShearStrengthModel.CuCalculated,
ShearStrengthModel.None
};
}
if (source is Stochast && member == StaticReflection.GetMemberName(s => s.DistributionType))
{
return new List
{
DistributionType.Deterministic,
DistributionType.Normal,
DistributionType.LogNormal
};
}
return null;
}
///
/// Gets the format.
///
/// The type.
/// The source.
/// The member.
///
public override string GetFormat(Type type, object source, string member)
{
if (type == typeof(ConePenetrationTestPerSegment) || source is ConePenetrationTestPerSegment)
{
if (member == StaticReflection.GetMemberName(x => x.Xlocal))
{
return "F3";
}
}
if (type == typeof(BoringPerSegment) || source is BoringPerSegment)
{
if (member == StaticReflection.GetMemberName(x => x.Xlocal))
{
return "F3";
}
}
return base.GetFormat(type, source, member);
}
protected override HashSet GetFilteredProperties(object soilUserFilter)
{
if (soilUserFilter is UserColumnFilters)
{
switch ((UserColumnFilters)soilUserFilter)
{
case UserColumnFilters.MacrostabilityWti:
{
return macrostabilitySoilProperties;
}
case UserColumnFilters.PipingWti:
{
return pipingSoilProperties;
}
case UserColumnFilters.FlowSlideWti:
{
return flowslideSoilProperties;
}
}
}
return combinedSoilProperties;
}
private bool? VisibleAccordingSoilUserFilter(string member)
{
if (alwaysVisibleProperties.Contains(member))
{
return true;
}
var filteredProperties = GetFilteredProperties(SoilUserFilter);
if (filteredProperties == null)
{
return null;
}
return filteredProperties.Contains(member);
}
private bool? VisibleAccordingParameterView(string member)
{
if (member.EndsWith("Popup"))
{
return false;
}
if (member.EndsWith("Stochast"))
{
return ParameterView != ParameterViewSettings.AsIsParameters;
}
if (!alwaysVisibleProperties.Contains(member))
{
return ParameterView != ParameterViewSettings.ProbabilisticParameters;
}
return null;
}
}
}