using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Aitex.Core.RT.Log;
using System.Xml;
using Aitex.Core.UI.ControlDataContext;
using System.Collections.ObjectModel;
namespace Aitex.Core.UI.Control
{
///
/// Interaction logic for MOSource.xaml
///
public partial class MOSource : UserControl
{
string _moConfig;
public MOSource()
{
InitializeComponent();
}
public static readonly DependencyProperty MOSourceDataListProperty = DependencyProperty.Register(
"MOSourceDataList", typeof(ObservableCollection), typeof(MOSource),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
public ObservableCollection MOSourceDataList
{
get
{
return (ObservableCollection)this.GetValue(MOSourceDataListProperty);
}
set
{
this.SetValue(MOSourceDataListProperty, value);
}
}
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
UpdateMOLayout(_moConfig);
}
public void SetMOLayout(string moConfig)
{
_moConfig = moConfig;
}
void UpdateMOLayout(string moConfig)
{
double left = 0;
double width = this.Width;
double intervalWidth = 0;
int splitBath = -1;
double bathWidth = 0;
int objIndex = 0;
UserControl bathUc;
double bathLeft = -1;
int bathIntervalCount = 0;
ObservableCollection obj = new ObservableCollection();
if (rootCanvas.Children.Count > 0)
{
return;
}
if (string.IsNullOrEmpty(moConfig))
{
LOG.Error("MO源配置文件为空");
return;
}
//index, name, type
List> items = new List>();
XmlDocument doc = new XmlDocument();
doc.LoadXml(moConfig);
XmlNodeList lstNode = doc.SelectNodes("MOSourceConfig/MOSources/MOSourceItem");
foreach (XmlNode node in lstNode)
{
try
{
int index = int.Parse(node.Attributes["DisplayIndex"].Value) - 1;
string name = node.Attributes["Name"].Value;
string type = node.Attributes["Type"].Value;
int bathindex = int.Parse(node.Attributes["BathIndex"].Value) - 1;
if (!string.IsNullOrEmpty(name))
{
items.Add(new Tuple(index, name, type,bathindex));
}
}
catch (Exception ex)
{
LOG.Error("MO源配置项错误," + node.Name, ex);
}
}
items.Sort((p1, p2) => p1.Item1.CompareTo(p2.Item1));
foreach (var item in items)
{
UserControl uc;
switch (item.Item3)
{
case "WithDilute": uc = new MODilute(); break;
case "WithPush": uc = new MOPush(); break;
case "General": uc = new MOGeneral();break;
default:
uc = new MOGeneral(); break;
}
obj.Add(new UILayoutDataItem());
obj[objIndex].uc = uc;
obj[objIndex].index = item.Item1;
if (splitBath == -1 && item.Item4!=-1) //第一个Bath,如果bathIndex为-1,表示没有水槽
{
bathWidth = uc.Width;
obj[objIndex].bathWidth = bathWidth;
}
else if ((item.Item4 == splitBath) && (item.Item4 != -1)) //一个Bath对应多个MO源
{
bathWidth += uc.Width;
if (obj.Count>1)
obj[objIndex-1].bathWidth = 0;
obj[objIndex].bathWidth = bathWidth;
}
else if (item.Item4 != -1) //一个Bath只有一个对应的MO源
{
bathWidth = uc.Width;
obj[objIndex].bathWidth = bathWidth;
}
else //Bath没有对应的MO源
{
bathWidth = 0;
obj[objIndex].bathWidth = -1;
}
objIndex++;
splitBath = item.Item4;
width -= uc.Width;
}
intervalWidth = width / items.Count;
foreach (var item in obj)
{
//Mo layout
UserControl uc = item.uc as UserControl;
Canvas.SetLeft(uc, left);
uc.DataContext = MOSourceDataList[item.index];
rootCanvas.Children.Add(uc);
bathIntervalCount++;
//bath layout
if(item.bathWidth != -1)
{
if(bathLeft == -1)
{ bathLeft = left; }
if(item.bathWidth>0)
{
bathUc = new Bath();
bathUc.Width = item.bathWidth + ((bathIntervalCount-1) * intervalWidth);
Canvas.SetLeft(bathUc, bathLeft);
Canvas.SetTop(bathUc, 220);
bathUc.SetBinding(Bath.DeviceDataProperty, new Binding("BathData") { Source = MOSourceDataList[item.index] });
//bathUc.DataContext = MOSourceDataList[item.index - 1];
rootCanvas.Children.Add(bathUc);
bathLeft = -1;
bathIntervalCount = 0;
}
}
left += uc.Width + intervalWidth;
}
}
public class UILayoutDataItem
{
public int index;
public UserControl uc;
public double bathWidth;
}
}
}