using CaeGlobals; using DynamicTypeDescriptor; using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing.Design; using System.IO; using System.Linq; using System.Windows.Forms; using System.Windows.Forms.Design; using CPSO.Settings; namespace CPSO.Forms { [Serializable] public class ViewRBFMorphing : IViewSettings, IReset { private PythonSettings _pythonSettings; // ReSharper disable once IdentifierTypo private readonly DynamicCustomTypeDescriptor _dctd = null; [Category("Python环境")] [OrderedDisplayName(0, 10, "Conda目录")] [Description("指定Anaconda或者Minicoda本地安装中activate.bat所在目录.")] [Editor(typeof(BatFileNameEditor), typeof(UITypeEditor))] public string CondaActivateBat { get => _pythonSettings.CondaPath; set => _pythonSettings.CondaPath = value; } [Category("Python环境")] [OrderedDisplayName(1, 10, "虚拟环境")] [Description("选择要激活的虚拟环境.")] [TypeConverter(typeof(DynamicListConverter))] public string Environment { get => _pythonSettings.CondaEnvironment; set => _pythonSettings.CondaEnvironment = value; } [Category("Python环境")] [OrderedDisplayName(2, 10, "脚本文件")] [Description("指定Python脚本文件.")] [Editor(typeof(PythonFileNameEditor), typeof(UITypeEditor))] public string Script { get; set; } private class BatFileNameEditor : FileNameEditor { protected override void InitializeDialog(OpenFileDialog openFileDialog) { base.InitializeDialog(openFileDialog); openFileDialog.FileName = "activate.bat"; openFileDialog.Filter = @"批处理文件(*.bat)|*.bat"; } } private class PythonFileNameEditor : FileNameEditor { protected override void InitializeDialog(OpenFileDialog openFileDialog) { base.InitializeDialog(openFileDialog); openFileDialog.InitialDirectory = Path.Combine(Application.StartupPath, "Scripts"); openFileDialog.Filter = @"Python脚本(*.py)|*.py"; } } protected class DynamicListConverter : StringConverter { public override bool GetStandardValuesSupported(ITypeDescriptorContext context) { // Indicate that standard values are supported return true; } public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) { // This is where you dynamically generate your list of items // You can fetch these from a database, file, or other source var dynamicItems = new List(); if (context.Instance is ViewRBFMorphing rbfMorphing && File.Exists(rbfMorphing.CondaActivateBat)) { var path = Path.GetDirectoryName(rbfMorphing.CondaActivateBat); if (path != null) { var pythonName = Directory.GetParent(path); if (pythonName != null) { var envDirs = Path.Combine(pythonName.FullName, "envs"); dynamicItems.AddRange( from dir in Directory.GetDirectories(envDirs) select new DirectoryInfo(dir) into info where info.Exists select info.Name ); } } } return new StandardValuesCollection(dynamicItems); } public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) { // Set to true if the user can only select from the provided list, false for free text entry return false; } } public ViewRBFMorphing(PythonSettings settings) { _pythonSettings = settings; _dctd = ProviderInstaller.Install(this); } public ISettings GetBase() { return _pythonSettings; } public void Reset() { _pythonSettings.Reset(); } } }