using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
namespace IStation.WinFrmUI.Basic
{
///
///
///
public partial class TestTreeByStationView : XtraUserControl
{
public TestTreeByStationView()
{
InitializeComponent();
this.treeList1.InitialDefaultSettings();
this.treeList1.SelectImageList = ImageLib.Lib;
this.treeList1.MouseDoubleClick += TreeList1_MouseDoubleClick;
this.layoutControl1.SetupLayoutControl();
}
public Action, IStation.Model.PumpCurve> OnAddTestItem;
private void TreeList1_MouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (_pumpCurves == null)
return;
var node = this.treeList1.GetNodeAt(e.X, e.Y);
if (node == null)
return;
if (!(this.treeList1.GetDataRecordByNode(node) is CurrentViewModel vm))
return;
if (vm.IsGroup)
return;
var mapping = vm.Model as Model.PumpCurveMapping;
var vmParent = _allBindingList.Where(x => x.ID == vm.ParentID).First();
var station = _allBindingList.Where(x => x.ID == vmParent.ParentID).First();
var pump = vmParent.Model as Model.Product;
var curve = _pumpCurves.Find(x => x.ID == mapping.CurveID);
OnAddTestItem?.Invoke(station.Name, pump, curve);
}
public class CurrentViewModel
{
public long ID { get; set; }
public long ParentID { get; set; }
public string Name { get; set; }
public string ObjectType { get; set; }
public object Model { get; set; }
public bool IsGroup { get; set; }
public int ImageIndex { get; set; }
}
///
/// 聚焦改变事件
///
public event Action FocusedChangedEvent;
private BindingList _allBindingList = null;//所有绑定列表
private List _pumpCurves = null;
///
/// 绑定数据
///
public void Clear()
{
_pumpCurves = null;
_allBindingList = new BindingList();
this.treeList1.DataSource = _allBindingList;
this.FocusedChangedEvent?.Invoke(null);
}
///
/// 绑定数据
///
public void SetBindingData()
{
WaitFrmHelper.ShowWaitForm("正在加载数据...");
_allBindingList = new BindingList();
_pumpCurves = new BLL.PumpCurve().GetAll();
var stations = new BLL.Station().GetAll();
if (stations != null && stations.Any())
{
var imgIndex = ImageLib.Station;
foreach (var station in stations)
{
var vm = new CurrentViewModel()
{
ID = station.ID,
Name = station.Name,
ObjectType = IStation.ObjectType.Station,
Model = station,
IsGroup = true,
ImageIndex = imgIndex
};
_allBindingList.Add(vm);
}
}
var pumps = new BLL.Product().GetAllPump();
if (pumps != null && pumps.Any())
{
var imgIndex = ImageLib.Pump;
foreach (var pump in pumps)
{
var vm = new CurrentViewModel()
{
ID = pump.ID,
Name = pump.Name,
ParentID = pump.BelongID,
ObjectType = IStation.ObjectType.Product,
Model = pump,
IsGroup = true,
ImageIndex = imgIndex
};
_allBindingList.Add(vm);
}
var pumpIds = pumps.Select(x => x.ID).ToList();
var pumpCurveMappings = new BLL.PumpCurveMapping().GetByPumpIds(pumpIds);
if (pumpCurveMappings != null && pumpCurveMappings.Any())
{
var pumpCurveIds = pumpCurveMappings.Select(x => x.CurveID).Distinct().ToList();
var curveImgIndex = ImageLib.PumpCurve;
var workCurveImgIndex = ImageLib.WorkPumpCurve;
foreach (var mapping in pumpCurveMappings)
{
var vm = new CurrentViewModel()
{
ID = mapping.ID,
Name = mapping.OtherName,
ParentID = mapping.PumpID,
ObjectType = IStation.ObjectType.PumpCurveMapping,
Model = mapping,
IsGroup = false,
ImageIndex = mapping.IsWorking ? workCurveImgIndex : curveImgIndex
};
_allBindingList.Add(vm);
}
}
}
this.treeList1.DataSource = _allBindingList;
this.treeList1.ForceInitialize();
WaitFrmHelper.HideWaitForm();
}
public void ExpandAll()
{
this.treeList1.ExpandAll();
}
///
///
///
public void UpdateOtherName(string name)
{
var row = this.treeList1.GetCurrentViewModel(_allBindingList);
if (row == null)
return;
row.Name = name;
this.treeList1.RefreshNode(this.treeList1.FocusedNode);
}
//全部展开
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();
}
//检索
private void barCkSearch_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
if (this.barCkSearch.Checked)
this.treeList1.ShowFindPanel();
else
this.treeList1.HideFindPanel();
}
//详细信息
private void barBtnDetail_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
XtraMessageBox.Show("待补充!");
}
//泵信息
private void barBtnPumpInfo_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
var vm = this.treeList1.GetCurrentViewModel(_allBindingList);
if (vm == null)
return;
var parentVm = _allBindingList?.Where(x => x.ID == vm.ParentID).FirstOrDefault();
if (parentVm == null)
return;
var pump = parentVm.Model as Model.Product;
WaitFrmHelper.ShowWaitForm();
var dlg = new PumpInfoDlg();
dlg.Shown += delegate { WaitFrmHelper.HideWaitForm(); };
dlg.SetBindingData(pump);
dlg.ShowDialog();
}
//聚焦前
private void treeList1_BeforeFocusNode(object sender, DevExpress.XtraTreeList.BeforeFocusNodeEventArgs e)
{
if (this.treeList1.GetDataRecordByNode(e.Node) is CurrentViewModel row)
{
if (row.IsGroup)
{
e.CanFocus = false;
}
}
}
//聚焦改变
private void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
{
var vm = this.treeList1.GetCurrentViewModel(_allBindingList);
if (vm == null)
return;
if (vm.IsGroup)
return;
var mapping = vm.Model as Model.PumpCurveMapping;
this.FocusedChangedEvent?.Invoke(mapping);
}
}
}