ningshuxia
2025-03-31 36f1be9860b303afac93f93a15f0c62cba57c3c5
WinFrmUI/PBS.WinFrmUI.Hydro/99-map-view/propertyform.cs
@@ -1,41 +1,55 @@
using System.Data;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraVerticalGrid.Events;
using System.Data;
namespace HydroUI
    public partial class PropertyForm : UserControl
    {
        public PropertyForm()
        {
            InitializeComponent();
            this.propertyGridControl1.OptionsBehavior.PropertySort = DevExpress.XtraVerticalGrid.PropertySort.NoSort;
            this.propertyGridControl1.OptionsView.AllowReadOnlyRowAppearance = DevExpress.Utils.DefaultBoolean.True;
            this.propertyGridControl1.OptionsView.ShowFocusedFrame = false;
            this.propertyGridControl1.OptionsView.ShowRootLevelIndent = false;
            this.propertyGridControl1.RecordWidth = 120;
            this.propertyGridControl1.RowHeaderWidth = 100;
            propertyGridControl1.CustomRecordCellEdit += propertyGridControl1_CustomRecordCellEdit;
            propertyGridControl1.CustomDrawRowHeaderCell += propertyGridControl1_CustomDrawRowHeaderCell;
            propertyGridControl1.CustomDrawRowValueCell += propertyGridControl1_CustomDrawRowValueCell;
            propertyGridControl1.ShowingEditor += propertyGridControl1_ShowingEditor;
            this.comboBox_type.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;
        }
        private IBaseViewModel _selectedObject;
        private MapObjectType _selectedType=  MapObjectType.全部;
        private List<SelectionSet> selectionSets;
        public SelectionSet selectionSet=new SelectionSet();
        private Form fr = null;
        public PropertyForm()
        {
            InitializeComponent();
        }
        public void SetEnabled(bool isReadOnly)
        {
            _isEnable = isReadOnly;
        }
        public void SetWindows(Form mg)
        {
            if (mg.WindowState == FormWindowState.Maximized)
            {
                //this.StartPosition = FormStartPosition.Manual;   //设置窗体位置由程序控制
                this.Height = mg.Height-50;
                this.Location = new Point(mg.Left + mg.Width -this.Width, mg.Top+50);   //设置新窗体位置,当前窗体的右侧(X轴方向)
                this.Location = new Point(mg.Left + mg.Width - this.Width, mg.Top + 50);
            }
            else
            {
                //this.StartPosition = FormStartPosition.Manual;   //设置窗体位置由程序控制
                this.Height = mg.Height;
                this.Location = new Point(mg.Left + mg.Width - 15, mg.Top);   //设置新窗体位置,当前窗体的右侧(X轴方向)
                this.Location = new Point(mg.Left + mg.Width - 15, mg.Top);
            }
            fr = mg;
            //GlobalObject.PropertyForm.Show();
        }
        public Form GetWindow()
        {
@@ -51,7 +65,6 @@
            lock(selectedObject)
            {
                _selectedObject = selectedObject;
                
                if (_selectedObject is JunctionViewModel junc)
@@ -101,7 +114,8 @@
                {
                    _selectedType = MapObjectType.喷头;
                    propertyGridControl1.SelectedObjects = new NozzleViewModel[] { nozzle };
                } else
                }
                else
                {
                    _selectedType = MapObjectType.全部;
                    propertyGridControl1.SelectedObjects =new IBaseViewModel[] { selectedObject };
@@ -127,15 +141,15 @@
                        Text = "重复器 Properties";
                        break;
                }
            }
        }
        public void SetNet(MapViewNetWork net)
        {
            selectionSet.net = net;
            RefreshListBox();
        }
        public void SetObjs(List<IBaseViewModel> selectedObjects)
        {
            if (selectedObjects == null) return;
@@ -163,13 +177,12 @@
        {
            foreach (var item in Enum.GetValues(typeof(MapObjectType)))
            {
                comboBox_type.Items.Add(item);
                comboBox_type.Properties.Items.Add(item);
            }
            comboBox_type.SelectedIndex = 0;
            selectionSets = new List<SelectionSet>();
            selectionSet = new SelectionSet();
            listBox1.DataSource = selectionSet.View;
            //listBox1.DisplayMember = "IDType";
            this.listBoxControl1.DataSource = selectionSet.View;
        }
        private void comboBox_type_SelectedIndexChanged(object sender, EventArgs e)
@@ -179,11 +192,11 @@
        void RefreshListBox()
        {
            if (selectionSet == null) return;
            if (comboBox_type.Items.Count<1)
            if (comboBox_type.Properties.Items.Count < 1)
            {
                foreach (var item in Enum.GetValues(typeof(MapObjectType)))
                {
                    comboBox_type.Items.Add(item);
                    comboBox_type.Properties.Items.Add(item);
                }
                comboBox_type.SelectedIndex = 0;
            }
@@ -193,34 +206,24 @@
            selectionSet.filterType = selectedType;
            selectionSet.FilterString = textBox_search.Text;
            this.listBoxControl1.DataSource = null;
            this.listBoxControl1.DataSource = selectionSet.View;
            listBox1.SelectedIndexChanged -= listBox1_SelectedIndexChanged;
            //listBox1.DisplayMember = "ID";
            //// 重新绑定选择集列表
            //listBox1.DataSource = selectionSet.View;
            listBox1.DataSource = null;
            //listBox1.DisplayMember = "IDType";
            listBox1.DataSource = selectionSet.View;
            listBox1.SelectedIndexChanged += listBox1_SelectedIndexChanged;
        }
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedItems.Count == 0)
            if (this.listBoxControl1.SelectedItems.Count == 0)
            {
                return;
            }
            else if (listBox1.SelectedItems.Count == 1)
            else if (this.listBoxControl1.SelectedItems.Count == 1)
            {
                var objID = listBox1.SelectedItems[0];
                var objID = this.listBoxControl1.SelectedItems[0];
                var findlist = selectionSet.net.MapObjects.FindAll(o => o.ID == objID);
                if (findlist.Count <= 0) return;
                IBaseViewModel mapObject = findlist[0];// selectionSet.selectedObjects.Find(obj => obj.ID == ((MapObject)listBox1.SelectedItems[0]).ID);
                IBaseViewModel mapObject = findlist[0];// selectionSet.selectedObjects.Find(obj => obj.ID == ((MapObject)this.listBoxControl1.SelectedItems[0]).ID);
                if (mapObject != null) SetObj(mapObject);
                GlobalObject.map.selectedObjs.ForEach(o => o.Selected = false); ;
                GlobalObject.map.selectedObjs.Clear();
@@ -231,9 +234,9 @@
            else
            {
                List<IBaseViewModel> list = new List<IBaseViewModel>();
                foreach (var o in listBox1.SelectedItems)
                foreach (var o in this.listBoxControl1.SelectedItems)
                {
                    var objID = listBox1.SelectedItems[0];
                    var objID = this.listBoxControl1.SelectedItems[0];
                    IBaseViewModel mapObject = selectionSet. net.MapObjects.FindAll(obj => obj.ID == objID)[0];
                    list.Add(mapObject);
                }
@@ -245,70 +248,17 @@
                GlobalObject.map.SetMapInvalidate();
            }
           
        }
        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            if (listBox1.SelectedItems.Count == 1)
            if (this.listBoxControl1.SelectedItems.Count == 1)
            {
                //IBaseViewModel mapObject = (IBaseViewModel)listBox1.SelectedItems[0];
                var objID = listBox1.SelectedItems[0];
                var objID = this.listBoxControl1.SelectedItems[0];
                IBaseViewModel mapObject = selectionSet.net.MapObjects.FindAll(obj => obj.ID == objID)[0];
                GlobalObject.map.setCenter (mapObject);
                GlobalObject.map.SetMapInvalidate();
            }
        }
        private void PropertyForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }
        bool _isEnable = true;
        private static Dictionary<object, List<object>> _selectedObjectsDict = new Dictionary<object, List<object>>();
        private void propertyGridControl1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            //foreach (var item in listBox1.SelectedItems)
            //{
            //    var mapObject = selectionSet.selectedObjects.Find(obj => obj.ID == ((MapObject)item).ID);
            //    if (e.ChangedItem.Label == " 批次编号 ")
            //    {
            //        mapObject.Level = (int)e.ChangedItem.Value;
            //    }
            //}
            if (!_isEnable)
            {
                MessageBox.Show("当前状态的修改将不会被保存","重复器展开中");
                _isEnable = true;
            }
            //object firstObj = selectionSet.selectedObjects.First();
            //if (_selectedObjectsDict[firstObj].Contains(e.ChangedItem.Label))
            //{
            //    foreach (var obj in selectionSet.selectedObjects)
            //    {
            //        var props = TypeDescriptor.GetProperties(obj);
            //        foreach (PropertyDescriptor prop in props)
            //        {
            //            if (prop.Name == e.ChangedItem.Label)
            //            {
            //                prop.SetValue(obj, e.ChangedItem.Value);
            //                break;
            //            }
            //        }
            //    }
            //}
            GlobalObject.map.SetMapInvalidate();
        }
        
        public void ShowProperties(IEnumerable<IBaseViewModel> selectedObjects)
@@ -324,9 +274,6 @@
                return;
            }
            propertyGridControl1.SelectedObjects = objs;
        }
        private void textBox_search_TextChanged(object sender, EventArgs e)
@@ -393,7 +340,6 @@
                RefreshListBox();
            }
            
        }
        private void PropertyForm_KeyDown(object sender, KeyEventArgs e)
@@ -401,49 +347,187 @@
            //Ctrl+A,则选中当前listbox中的所有项
            if (e.Control && e.KeyCode == Keys.A)
            {
                for (int i = 0; i < listBox1.Items.Count; i++)
                for (int i = 0; i < this.listBoxControl1.Items.Count; i++)
                {
                    listBox1.SetSelected(i, true);
                    this.listBoxControl1.SetSelected(i, true);
                }
            }
        }
        private void 全选ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listBox1.SelectedIndexChanged -= listBox1_SelectedIndexChanged;
            for (int i = 0; i < listBox1.Items.Count; i++)
            {
                listBox1.SetSelected(i, true);
            }
            listBox1.SelectedIndexChanged += listBox1_SelectedIndexChanged;
            this.propertyGridControl1.SelectedObjects =selectionSet. CurrentSelectedObjects?.ToArray();
            GlobalObject.map.selectedObjs.ForEach(o => o.Selected = false);
            if (selectionSet.CurrentSelectedObjects!=null)
            {
                GlobalObject.map.selectedObjs.AddRange(selectionSet.CurrentSelectedObjects);
                selectionSet.CurrentSelectedObjects.ForEach(o =>
                {
                    o.Selected = true;
                });
            }
            GlobalObject.map.SetMapInvalidate();
        }
        private void listBox1_MouseDown(object sender, MouseEventArgs e)
        private void listBoxControl1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (listBox1.SelectedItems.Count > 0)
                this.popupMenuRight.ShowPopup(MousePosition);
            }
        }
        private void barBtnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
                {
                    contextMenuStrip1.Show(listBox1, e.Location);
            if (this.listBoxControl1.SelectedItems.Count == 1)
            {
                GlobalObject.map.DeleteChoosedObj();
                RefreshListBox();
            }
        }
        #region Property
        //自定义属性Header显示
        private void propertyGridControl1_CustomDrawRowHeaderCell(object sender, CustomDrawRowHeaderCellEventArgs e)
        {
            //字段名称
            var fieldName = e.Row.Properties.FieldName.Split(new char[] { '.' }).Last();
            if (string.IsNullOrEmpty(fieldName))
            {
                return;
            }
            var realFieldName = fieldName;
            //属性描述器
            var descriptor = this.propertyGridControl1.GetPropertyDescriptor(e.Row);
            if (descriptor != null)
            {
                //名称
                var displayNameAttri = (DisplayNameAttribute)descriptor.Attributes[typeof(DisplayNameAttribute)];
                if (displayNameAttri != null && !string.IsNullOrEmpty(displayNameAttri.DisplayName))
                {
                    e.Caption = displayNameAttri.DisplayName;
                }
            }
        }
        //自定义属性值显示
        private void propertyGridControl1_CustomDrawRowValueCell(object sender, CustomDrawRowValueCellEventArgs e)
        {
            //行字段名称
            var fieldName = e.Row.Properties.FieldName.Split(new char[] { '.' }).Last();
            if (string.IsNullOrEmpty(fieldName))
            {
                return;
            }
            //行类型全名称
            var fullTypeName = e.Row.Properties.RowType.FullName;
            if (fullTypeName == typeof(DateTime).FullName)
            {
                e.CellText = ((DateTime)e.Properties.Value).ToString("yyyy-MM-dd HH:mm:ss");
            }
            else if (fullTypeName == typeof(string[]).FullName)
            {
                var stringValue = (string[])e.Properties.Value;
                e.CellText = stringValue?.Length.ToString();
            }
            else if (fullTypeName == typeof(DictionaryPropertyAdapter).FullName)
            {
                e.CellText = string.Empty;
            }
            else
            {
                var descriptor = this.propertyGridControl1.GetPropertyDescriptor(e.Row);
                if (descriptor != null)
                {
                    var displayUnitAttri = (DisplayUnitAttribute)descriptor.Attributes[typeof(DisplayUnitAttribute)];
                    if (displayUnitAttri != null)
                    {
                        if (e.Properties.Value != null)
                        {
                            e.CellText = e.Properties.Value.ToString() + " " + displayUnitAttri.Unit;
                        }
                    }
                }
            }
        }
        //属性编辑框的显示与取消
        private void propertyGridControl1_ShowingEditor(object sender, CancelEventArgs e)
        {
            var rowTypeFullName = this.propertyGridControl1.FocusedRow.Properties.RowType.FullName;
            var fieldName = this.propertyGridControl1.FocusedRow.Properties.FieldName.Split(new char[] { '.' }).Last();
            if (rowTypeFullName == typeof(Image).FullName)
            {
                e.Cancel = true;
                return;
            }
            var descriptor = this.propertyGridControl1.GetPropertyDescriptor(this.propertyGridControl1.FocusedRow);
            var showEditor = (ShowEditorAttribute)descriptor.Attributes[typeof(ShowEditorAttribute)];
            if (showEditor != null)
            {
                if (!showEditor.ShowEditor)
                {
                    e.Cancel = true;
                    return;
                }
            }
        }
        //编辑框
        private void propertyGridControl1_CustomRecordCellEdit(object sender, GetCustomRowCellEditEventArgs e)
        {
            var fieldName = e.Row.Properties.FieldName.Split(new char[] { '.' }).Last();
            if (string.IsNullOrEmpty(fieldName))
            {
                return;
            }
            var descriptor = this.propertyGridControl1.GetPropertyDescriptor(e.Row);
            var rowTypeFullName = e.Row.Properties.RowType.FullName;
            #region bool
            if (rowTypeFullName == typeof(bool).FullName)
            {
                var ckEdit = new RepositoryItemCheckEdit();
                ckEdit.CheckStyle = DevExpress.XtraEditors.Controls.CheckStyles.Standard;
                if (e.Row.Properties.ReadOnly == true)
                {
                    ckEdit.ReadOnly = true;
                }
                e.RepositoryItem = ckEdit;
            }
            #endregion
            #region 富文本
            if (descriptor != null)
            {
                var attri_multi = (MultiTextAttribute)descriptor.Attributes[typeof(MultiTextAttribute)];
                if (attri_multi != null)
                {
                    var memoEdit = new RepositoryItemMemoEdit();
                    if (e.Row.Properties.ReadOnly == true)
                    {
                        memoEdit.ReadOnly = true;
                    }
                    e.RepositoryItem = memoEdit;
                }
            }
            #endregion
            #region 图片
            if (rowTypeFullName == typeof(Image).FullName)
            {
                var picEdit = new RepositoryItemPictureEdit();
                picEdit.ReadOnly = true;
                picEdit.NullText = "空";
                e.RepositoryItem = picEdit;
                e.Row.Expanded = true;
            }
            #endregion
        }
        #endregion
    }
    public class SelectionSet