Files
wg_cpso/CPSO/Forms/61_Settings/FrmSettings.cs
2026-03-25 18:20:24 +08:00

221 lines
8.5 KiB
C#

using CaeGlobals;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using CPSO.Forms._61_Settings;
using CPSO.Settings;
#pragma warning disable IDE0130
namespace CPSO.Forms
{
public partial class FrmSettings : UserControls.PrePoMaxChildForm
{
// Variables
private string _previousSettings;
private Dictionary<string, IViewSettings> _viewSettings;
private readonly double _labelRatio = 2.3;
// Properties
public Dictionary<string, ISettings> Settings
{
get
{
Dictionary<string, ISettings> result = new Dictionary<string, ISettings>();
foreach (var entry in _viewSettings)
{
result.Add(entry.Key, entry.Value.GetBase());
}
return result;
}
set
{
//create a clone
_viewSettings = new Dictionary<string, IViewSettings>();
foreach (var entry in value)
{
if (entry.Value is GeneralSettings ges)
_viewSettings.Add(entry.Key, new ViewGeneralSettings(ges.DeepClone()));
else if (entry.Value is GraphicsSettings grs)
_viewSettings.Add(entry.Key, new ViewGraphicsSettings(grs.DeepClone()));
else if (entry.Value is ColorSettings cos)
_viewSettings.Add(entry.Key, new ViewColorSettings(cos.DeepClone()));
else if (entry.Value is AnnotationSettings wis)
_viewSettings.Add(entry.Key, new ViewAnnotationSettings(wis.DeepClone()));
else if (entry.Value is MeshingSettings ms)
_viewSettings.Add(entry.Key, new ViewMeshingSettings(ms.DeepClone()));
else if (entry.Value is PreSettings prs)
_viewSettings.Add(entry.Key, new ViewPreSettings(prs.DeepClone()));
else if (entry.Value is CalculixSettings cas)
_viewSettings.Add(entry.Key, new ViewCalculixSettings(cas.DeepClone()));
else if (entry.Value is AbaqusSettings abs)
_viewSettings.Add(entry.Key, new ViewAbaqusSettings(abs.DeepClone()));
else if (entry.Value is PostSettings pos)
_viewSettings.Add(entry.Key, new ViewPostSettings(pos.DeepClone()));
else if (entry.Value is LegendSettings les)
_viewSettings.Add(entry.Key, new ViewLegendSettings(les.DeepClone()));
else if (entry.Value is StatusBlockSettings sbs)
_viewSettings.Add(entry.Key, new ViewStatusBlockSettings(sbs.DeepClone()));
else if (entry.Value is PythonSettings py)
{
_viewSettings.Add(entry.Key, new ViewPythonSettings(py));
}
else if (entry.Value is CuttingForceSettings cfs)
{
_viewSettings.Add(entry.Key, new ViewCuttingForceSettings(cfs));
}
else
throw new NotSupportedException();
}
}
}
// Events
public event Action<SettingsContainer> UpdateSettings;
// Constructors
public FrmSettings()
{
InitializeComponent();
_previousSettings = null;
_viewSettings = null;
//
propertyGrid.SetLabelColumnWidth(_labelRatio);
}
// Event handlers
private void lvSettings_SelectedIndexChanged(object sender, EventArgs e)
{
if (lvSettings.SelectedItems.Count > 0)
{
propertyGrid.SelectedObject = lvSettings.SelectedItems[0].Tag;
propertyGrid.Select();
_previousSettings = lvSettings.SelectedItems[0].Text;
}
}
private void propertyGrid_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
{
propertyGrid.Refresh();
_propertyItemChanged = true;
}
private void cmsPropertyGrid_Opening(object sender, CancelEventArgs e)
{
if (propertyGrid.SelectedObject is IReset)
tsmiResetAll.Enabled = true;
else
tsmiResetAll.Enabled = false;
}
private void tsmiResetAll_Click(object sender, EventArgs e)
{
if (propertyGrid.SelectedObject is IReset resetObject)
{
resetObject.Reset();
propertyGrid.Refresh();
_propertyItemChanged = true;
}
}
private void btnApply_Click(object sender, EventArgs e)
{
Apply();
}
private void btnOK_Click(object sender, EventArgs e)
{
try
{
if (Apply())
{
//
DialogResult = DialogResult.OK;
Hide();
}
}
catch (Exception ex)
{
ExceptionTools.Show(this, ex);
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
Hide();
}
private void FrmSettings_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
Hide();
}
}
// Methods
public void PrepareForm(Controller controller)
{
_propertyItemChanged = false;
_viewSettings = null;
lvSettings.Items.Clear();
propertyGrid.SelectedObject = null;
//
if (controller.Settings == null)
{
throw new Exception("There are no settings to show.");
}
else
{
Settings = controller.Settings.ToDictionary();
//
ListViewItem lvi;
foreach (var entry in _viewSettings)
{
lvi = lvSettings.Items.Add(entry.Key);
lvi.Tag = entry.Value;
}
}
// Open previously shown settings
if (_previousSettings != null)
{
foreach (ListViewItem item in lvSettings.Items)
{
if (item.Text == _previousSettings)
{
item.Selected = true;
break;
}
}
}
else
{
if (lvSettings.Items.Count > 0) lvSettings.Items[0].Selected = true;
}
//
controller.SetSelectByToOff();
}
public void SetSettingsToShow(string name)
{
_previousSettings = name;
}
public bool Apply()
{
Dictionary<string, ISettings> settings = Settings;
//
foreach (var entry in settings) entry.Value.CheckValues();
//
SettingsContainer settingsContainer = new SettingsContainer(settings);
if (settingsContainer.GetWorkDirectory().ContainsNonEnglishCharacters())
{
if (MessageBoxes.ShowWarningQuestionOKCancel(CalculixSettings.NonEnglishDirectoryWarning) ==
DialogResult.Cancel)
return false;
}
//
if (_propertyItemChanged)
{
UpdateSettings?.Invoke(settingsContainer);
_propertyItemChanged = false;
}
return true;
}
}
}