using DevExpress.XtraEditors;
using DevExpress.XtraTreeList;
using IStation.Untity;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
namespace IStation.WinFrmUI.Monitor
{
///
///
///
public partial class MultiMonitorPointTreeList : XtraUserControl
{
public MultiMonitorPointTreeList()
{
InitializeComponent();
treeList1.InitialDefaultMultiSelectSettings();
treeList1.SelectImageList = ImageLib.Lib;
layoutControl1.SetupLayoutControl();
}
public class CurrentViewModel
{
public CurrentViewModel() { }
public CurrentViewModel(Model.Station rhs)
{
ID = rhs.ID;
Name = rhs.Name;
ObjectType = IStation.ObjectType.Station;
SortCode = rhs.SortCode;
ImageIndex = ImageLib.Station;
IsGroup = true;
Model = rhs;
}
public CurrentViewModel(Model.MonitorPointGroup rhs)
{
ID = rhs.ID;
ParentID = rhs.ParentIds == null || !rhs.ParentIds.Any()
? rhs.BelongID : TreeParentIdsHelper.GetLastParentID(rhs.ParentIds);
Name = rhs.Name;
ObjectType = IStation.ObjectType.MonitorPointGroup;
SortCode = rhs.SortCode;
IsGroup = true;
ImageIndex = ImageLib.Group;
Model = rhs;
}
public CurrentViewModel(Model.MonitorPointExSignalWithSignalType rhs)
{
ID = rhs.MonitorPointID;
ParentID = rhs.GroupID;
Name = rhs.Name;
ObjectType = IStation.ObjectType.MonitorPoint;
SortCode = rhs.SortCode;
ImageIndex = ImageLib.MonitorPoint;
Model = rhs;
}
public long ID { get; set; }
public long ParentID { get; set; }
public string Name { get; set; }
public string ObjectType { get; set; }
public int SortCode { get; set; }
public int ImageIndex { get; set; }
public bool IsGroup { get; set; }
public object Model { get; set; }
public bool? Checked { get; set; } = false;
public Color Color { get; set; }
}
///
/// 选择改变事件
///
public event Action CheckChangedEvent;
private List _allBindingList = null;//所有绑定列表
///
/// 绑定数据
///
public void SetBindingData()
{
WaitFrmHelper.ShowWaitForm("正在加载数据...");
_allBindingList = new List();
var stations = new BLL.Station().GetAll();
if (stations != null && stations.Any())
{
var _bll = new BLL.MonitorPoint();
var _bllGroup = new BLL.MonitorPointGroup();
foreach (var station in stations)
{
var vmStation = new CurrentViewModel(station);
_allBindingList.Add(vmStation);
var monitorPointGroups = _bllGroup.GetByBelongTypeAndBelongID(IStation.ObjectType.Station, station.ID);
if (monitorPointGroups == null || !monitorPointGroups.Any())
continue;
foreach (var group in monitorPointGroups)
{
var vmGroup = new CurrentViewModel(group);
_allBindingList.Add(vmGroup);
var monitorPoints = _bll.GetExSignalWithSignalTypeByBelongTypeAndBelongIDAndGroupID(group.BelongType, group.BelongID, group.ID);
if (monitorPoints == null || !monitorPoints.Any())
continue;
foreach (var item in monitorPoints)
{
var vm = new CurrentViewModel(item);
_allBindingList.Add(vm);
}
}
}
}
treeList1.DataSource = _allBindingList;
treeList1.ForceInitialize();
WaitFrmHelper.HideWaitForm();
}
//全部展开
private void barBtnExpandAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
treeList1.ExpandAll();
}
//全部折叠
private void barBtnCollapseAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
treeList1.CollapseAll();
}
//检索
private void barCkSearch_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
if (barCkSearch.Checked)
treeList1.ShowFindPanel();
else
treeList1.HideFindPanel();
}
//详细信息
private void barBtnDetail_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
XtraMessageBox.Show("待补充!");
}
//节点选中前
private void treeList1_BeforeCheckNode(object sender, CheckNodeEventArgs e)
{
var vm = treeList1.GetDataRecordByNode(e.Node) as CurrentViewModel;
if (vm == null)
return;
if (vm.IsGroup)
{
e.CanCheck = false;
}
}
//节点选中后
private void treeList1_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
{
if (_allBindingList == null)
return;
var count = _allBindingList.Where(x => !x.IsGroup && x.Checked == true).Count();
if (count > 11)
{
XtraMessageBox.Show("目前只支持十个测点展示!");
e.Node.Checked = false;
return;
}
var vm = treeList1.GetDataRecordByNode(e.Node) as CurrentViewModel;
if (vm == null)
return;
if (vm.IsGroup)
return;
var monitorPoint = vm.Model as Model.MonitorPointExSignalWithSignalType;
if (monitorPoint == null)
return;
vm.Color = GetRandomColor(count);
CheckChangedEvent?.Invoke(monitorPoint, vm.Color, e.Node.Checked);
}
#region Color
private List ColorArray = new List()
{ Color.Red, Color.Blue, Color.Green, Color.DodgerBlue,
Color.Fuchsia, Color.MidnightBlue, Color.Maroon,
Color.Aquamarine, Color.Bisque, Color.BurlyWood
};
///
/// 获取随机颜色
///
///
private Color GetRandomColor(int count)
{
if (count < ColorArray.Count)
{
return ColorArray[count];
}
return ColorHelper.GetRandomColor();
}
#endregion
//绘制单元格
private void treeList1_CustomDrawNodeCell(object sender, DevExpress.XtraTreeList.CustomDrawNodeCellEventArgs e)
{
/* var node = this.treeList1.GetDataRecordByNode(e.Node) as CurrentViewModel;
if (node == null)
return;
if (node.Checked.HasValue && node.Checked.Value)
{
e.Appearance.ForeColor = node.Color;
}*/
}
//取消绘制单元格
private void treeList1_CustomDrawNodeCheckBox(object sender, CustomDrawNodeCheckBoxEventArgs e)
{
var node = treeList1.GetDataRecordByNode(e.Node) as CurrentViewModel;
if (node == null)
return;
if (node.IsGroup)
{
e.ObjectArgs.State = DevExpress.Utils.Drawing.ObjectState.Disabled;
}
}
//清空选中项
private void barBtnClearChecked_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
foreach (var vm in _allBindingList)
{
if (vm.Checked == true)
{
vm.Checked = false;
if (vm.IsGroup)
continue;
var monitorPoint = vm.Model as Model.MonitorPointExSignalWithSignalType;
CheckChangedEvent?.Invoke(monitorPoint, vm.Color, false);
}
}
treeList1.DataSource = _allBindingList;
treeList1.RefreshDataSource();
}
}
}