ningshuxia
2023-02-24 8c0434431e2c77de997ffbf27e65fdcc7ab915e3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.Utils; 
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
using IStation.Untity;
 
namespace IStation.WinFormUI.MonitorDataSet
{
    /// <summary>
    /// 测点树列表控件
    /// </summary>
    public partial class CurveCompareMultiSelectMonitorPointTreeListCtrl : XtraUserControl
    {
        public CurveCompareMultiSelectMonitorPointTreeListCtrl()
        {
            InitializeComponent();
            this.treeList1.InitialDefaultSettings();
            this.treeList1.SelectImageList = CurrentViewModel.Image16Store;
            this.layoutControl1.SetupLayoutControl();
        }
 
        #region 当前视图
 
        public class CurrentViewModel
        {
            public CurrentViewModel() { }
            public CurrentViewModel(Model.MonitorPointGroup rhs)
            {
                this.ID = $"{IStation.ObjectType.MonitorPointGroup}_{rhs.Id}";
                this.ParentID = $"{IStation.ObjectType.MonitorPointGroup}_{TreeParentIdsHelper.GetLastParentID(rhs.ParentIds)}";
                this.Name = rhs.Name;
                this.ObjectType = "监测点组";
                this.ObjectID = rhs.Id;
                this.SortCode = rhs.SortCode;
                this.Description = rhs.Description;
                this.Checked = false;
                this.ImageIndex = 0;
                this.Model = rhs;
            }
            public CurrentViewModel(Model.MonitorPointExSignalList rhs)
            {
                this.ID = $"{IStation.ObjectType.MonitorPoint}_{rhs.Id}";
                this.ParentID = $"{IStation.ObjectType.MonitorPointGroup}_{rhs.GroupId}";
                this.Name = rhs.Name;
                this.ObjectType = "监测点";
                this.ObjectID = rhs.Id;
                this.SortCode = rhs.SortCode;
                this.Description = rhs.Description;
                this.Checked = false;
                this.ImageIndex = 1;
                this.Model = rhs;
            }
 
            public string ID { get; set; }
            public string ParentID { get; set; }
            public string Name { get; set; }
            public string ObjectType { get; set; }
            public long ObjectID { get; set; }
            public int SortCode { get; set; }
            public string Description { get; set; }
            public bool? Checked { get; set; }
            public int ImageIndex { get; set; }
            public object Model { get; set; }
            public Color? Color { get; set; }
            /// <summary>
            /// 图标仓库
            /// </summary>
            public static ImageCollection Image16Store
            {
                get
                {
                    if (_image16Store == null)
                    {
                        _image16Store = new ImageCollection();
                        _image16Store.ImageSize = new Size(16, 16);
                        _image16Store.Images.Add(WinFormUI.Properties.Resource.Group, "Group");
                        _image16Store.Images.Add(WinFormUI.Properties.Resource.MonitorPoint, "MonitorPoint");
                    }
                    return _image16Store;
                }
            }
            private static ImageCollection _image16Store;
        }
 
        #endregion
 
        /// <summary>
        /// check 改变后
        /// </summary>  
        public event Action<Model.MonitorPointExSignalList,System.Drawing.Color> SelectedEvent;
        /// <summary>
        /// 
        /// </summary>
        public event Action<Model.MonitorPointExSignalList> UncheckEvent;
 
        private long _projectId;//项目标识
        private List<CurrentViewModel> _allBindingList = null;//所有绑定列表
         
        /// <summary>
        /// 绑定数据
        /// </summary>
        public void SetBindingData()
        {
            _projectId = 0; 
            this.SetBindingData(null, null);
        }
 
 
        /// <summary>
        /// 绑定数据
        /// </summary>
        public void SetBindingData(long projectId)
        {
            _projectId = projectId;
            var group_list = new BLL.MonitorPointGroup().QueryAll(_projectId);
            var point_list = new BLL.MonitorPoint().QueryExSignalList(_projectId);
            this.SetBindingData(group_list, point_list);
        }
 
        /// <summary>
        /// 绑定数据
        /// </summary>
        public void SetBindingData(long projectId, string belongType, long belongId)
        {
            _projectId = projectId;
            var group_list = new BLL.MonitorPointGroup().QueryByBelongTypeAndBelongId(_projectId, belongType, belongId);
            var point_list = new BLL.MonitorPoint().QueryExSignalListByBelongTypeAndBelongId(_projectId, belongType, belongId);
            this.SetBindingData(group_list, point_list);
        }
 
        /// <summary>
        /// 绑定数据
        /// </summary>
        public void SetBindingData(List<Model.MonitorPointGroup> group_list, List<Model.MonitorPointExSignalList> point_list)
        {
            _allBindingList = new List<CurrentViewModel>();
            if (group_list != null && group_list.Count > 0)
            {
                group_list = group_list.OrderBy(x => x.SortCode).ToList();
                group_list.ForEach(x => _allBindingList.Add(new CurrentViewModel(x)));
            }
            var group = new Model.MonitorPointGroup();
            group.Id = -1;
            group.Name = "未分组";
            group.SortCode = int.MaxValue;
            group.Description = "虚拟分组";
            _allBindingList.Add(new CurrentViewModel(group));
            if (point_list != null && point_list.Count > 0)
            {
                point_list = point_list.OrderBy(x => x.SortCode).ToList();
                point_list.ForEach(x => _allBindingList.Add(new CurrentViewModel(x)));
            }
            this.treeList1.DataSource = _allBindingList;
            this.treeList1.ForceInitialize();
            this.treeList1.ExpandAll();
        }
 
        //全部展开
        private void barBtnExpandAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.treeList1.ExpandAll();
        }
 
        //全部折叠
        private void barBtnCollpseAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.treeList1.CollapseAll();
        }
 
        //检索(menu)
        private void barBtnSearch_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this.layoutControlItem1.Visibility == DevExpress.XtraLayout.Utils.LayoutVisibility.Always)
                this.layoutControlItem1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            else
                this.layoutControlItem1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
        }
 
        //树线
        private void barCkTreeLine_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.treeList1.OptionsView.ShowTreeLines = this.barCkTreeLine.Checked ? DefaultBoolean.True : DefaultBoolean.False;
        }
 
        private List<Color> _colorArray = new List<Color>() { Color.Blue, Color.DarkCyan,Color.MediumTurquoise,Color.MediumSeaGreen, Color.GreenYellow
        , Color.IndianRed,Color.Chocolate,Color.Orange,Color.Bisque,Color.SlateBlue,Color.Violet,Color.SkyBlue,Color.LightGreen};
        //Check改变
        private void treeList1_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
        {
            if (_allBindingList == null)
                return;
            if (_allBindingList.Where(x => x.ObjectType != "监测点组" && x.Checked == true).Count() > 11)
            {
                XtraMessageBox.Show("目前最多支持十个展示!");
                return;
            }
            var node = this.treeList1.GetDataRecordByNode(e.Node) as CurrentViewModel;
            if (node == null)
                return;
            if (node.ObjectType != "监测点")
                return;
            var monitorPoint = node.Model as Model.MonitorPointExSignalList;
            if (monitorPoint == null)
                return;
            if (e.Node.Checked)
            {
                node.Color = QueryColor(); 
                this.SelectedEvent?.Invoke(monitorPoint, node.Color.Value);
            }
            else
            {
                node.Color = null;
                this.UncheckEvent?.Invoke(monitorPoint);
            }
        }
 
        Color QueryColor()
        {
            foreach(var c in _colorArray)
            {
                var f = _allBindingList.Find(x => x.Color == c);
                if (f == null)
                    return c;
            }
            return Color.Black;
        }
 
        //隐藏父节点多选框
        private void treeList1_CustomDrawNodeCheckBox(object sender, DevExpress.XtraTreeList.CustomDrawNodeCheckBoxEventArgs e)
        {
            TreeList _curTree = (TreeList)sender;
            HideCheckBox(n => (this.treeList1.GetDataRecordByNode(n) as CurrentViewModel)?.ObjectType == "监测点组", e);
        }
 
        /// <summary>
         
        public static void HideCheckBox(Predicate<TreeListNode> conditionHanlder, CustomDrawNodeCheckBoxEventArgs e)
        {
            if (conditionHanlder(e.Node))
            {
                e.Handled = true;
            }
        }
        /// <summary>
        /// /
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barClearSelect_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (_allBindingList == null)
                return;
     
 
            foreach (var item in _allBindingList)
            {
                if(item.Checked == true)
                {
                    item.Checked = false; 
                    
                    var monitorPoint = item.Model as Model.MonitorPointExSignalList;
                    if (monitorPoint == null)
                        continue;
                    this.UncheckEvent?.Invoke(monitorPoint );
                }
            }
            this.treeList1.DataSource = _allBindingList;
            this.treeList1.RefreshDataSource();
        }
 
        private void treeList1_CustomDrawNodeCell(object sender, CustomDrawNodeCellEventArgs e)
        {
            var node = this.treeList1.GetDataRecordByNode(e.Node) as CurrentViewModel;
            if (node == null)
                return;
            if (node.Color == null)
                return;
            e.Appearance.ForeColor = node.Color.Value;
        }
    }
}