using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using TProduct.ProcessDrawing.Components;
using TProduct.ProcessDrawing.Models;
namespace TProduct.ProcessDrawing.ViewModels
{
///
/// 对应窗口的业务逻辑
///
public class DisplayViewModel : ViewModelBase
{
public ObservableCollection DeviceList { get; set; } = new ObservableCollection();
private DeviceItem4Config _currentDevice;
public DeviceItem4Config CurrentDevice
{
get => _currentDevice;
set => Set(ref _currentDevice, value);
}
private string _saveFailedMessage;
public string SaveFailedMessage
{
get { return _saveFailedMessage; }
set { Set(ref _saveFailedMessage, value); }
}
public RelayCommand DeviceSelectedCommand { get; set; }
int _pipeIndex = -1;
public void SetCurrentPipeIndex(int pipeIndex)
{
this._pipeIndex = pipeIndex;
if (DeviceList == null)
return;
foreach (var d in DeviceList)
{
if (d.IsHavePipeIndex(pipeIndex))
{
//(d.UiComponent).IsShowFlowAnimation = 1;
d.UiComponent .SetRunStatus(true );
}
else
{
d.UiComponent.SetRunStatus(false );
}
}
}
public void SetContaiterSize(int width, int height)
{
double left_x = (from x in DeviceList select x.X).Min();
double right_x = (from x in DeviceList select x.X).Max();
double top_y = (from x in DeviceList select x.Y).Min();
double bottom_y = (from x in DeviceList select x.Y).Max();
double offset_y = 0;
if ( bottom_y > height)
{
offset_y = 5 - top_y;
}
foreach (var d in DeviceList)
{
d.Y = d.Y + offset_y;
}
}
public DisplayViewModel()
{
if (!IsInDesignMode)
{
// 初始化组态组件
InititialDevices();
//
DeviceSelectedCommand = new RelayCommand(model2 =>
{
// 记录一个当前选中组件
// Model = DeviceItemModel
// 对当前组件进行选中
// 进行属性、点位编辑
if (CurrentDevice != null)
CurrentDevice.IsSelected = false;
if (model2 != null)
{
model2.IsSelected = true;
}
CurrentDevice = model2;
});
}
}
//初始化组态组件
private void InititialDevices()
{
if(DeviceList == null)
{
DeviceList = new ObservableCollection();
}
List all_DeviceItemModels = null;
if (!ProcessDrawingConfigFileHelper.ReadFile(out all_DeviceItemModels))
{
return;
}
foreach (var dim in all_DeviceItemModels)
{
DeviceList.Add(dim);
}
}
public void InitialRunStatus()
{
if (DeviceList == null)
return;
foreach (var d in DeviceList)
{
if (d.IsHavePipeIndex(_pipeIndex))
{
// d.UiComponent.StartAnimation();
d.UiComponent.SetRunStatus(true );
}
else
{
d.UiComponent.SetRunStatus(false );
}
}
}
}
}