using BeDesignerSCADA.Common;
using BeDesignerSCADA.Controls;
using BeDesignerSCADA.View;
using BPASmartClient.Compiler;
using BPASmartClient.SCADAControl.Converters;
using ICSharpCode.AvalonEdit;
using Microsoft.Toolkit.Mvvm.ComponentModel;
using Microsoft.Toolkit.Mvvm.Input;
using Microsoft.Win32;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup;
namespace BeDesignerSCADA.ViewModel
{
public class MainViewModelNew : ObservableObject
{
public MainViewModelNew()
{
IsRunning = false;
//启动 或者 停止
RunUiCommand = new RelayCommand(() =>
{
IsRunning = !IsRunning;
});
}
#region 变量
///
/// 变量管理器地址
///
public string VariablePath = string.Empty;
///
/// 布局路径
///
public string LayoutsPath = string.Empty;
///
/// 是否正在运行状态
///
private bool _IsRunning = false;
public bool IsRunning
{
get
{
return _IsRunning;
}
set
{
_IsRunning = value;
if (value)
{
RunCanvasVisibility = Visibility.Visible;
CanvasPanelVisibility = Visibility.Collapsed;
}
else
{
RunCanvasVisibility = Visibility.Collapsed;
CanvasPanelVisibility = Visibility.Visible;
}
OnPropertyChanged("IsRunning");
}
}
///
/// 画布是否显示
///
private Visibility _CanvasPanelVisibility;
public Visibility CanvasPanelVisibility
{
get
{
return _CanvasPanelVisibility;
}
set
{
if (_CanvasPanelVisibility == value)
return;
_CanvasPanelVisibility = value;
OnPropertyChanged("CanvasPanelVisibility");
}
}
///
/// 运行代码是否显示
///
private Visibility _RunCanvasVisibility;
public Visibility RunCanvasVisibility
{
get
{
return _RunCanvasVisibility;
}
set
{
if (_RunCanvasVisibility == value)
return;
_RunCanvasVisibility = value;
OnPropertyChanged("RunCanvasVisibility");
}
}
///
/// 当前代码Xaml
///
private string _XamlCode;
public string XamlCode
{
get
{
return _XamlCode;
}
set
{
if (_XamlCode == value)
return;
_XamlCode = value;
OnPropertyChanged("XamlCode");
}
}
///
/// 选中控件
///
private FrameworkElement _CanSelectedItem;
public FrameworkElement CanSelectedItem
{
get
{
return _CanSelectedItem;
}
set
{
if (_CanSelectedItem == value)
return;
_CanSelectedItem = value;
OnPropertyChanged("CanSelectedItem");
}
}
///
/// 菜单Model
///
private MenuModel _MenuModel;
public MenuModel MenuModel
{
get
{
return _MenuModel;
}
set
{
_MenuModel = value;
OnPropertyChanged("MenuModel");
}
}
///
/// 控件集合名称映射
///
public Dictionary ControlsNameValues = new Dictionary() {
{"TheButton","按钮" },
{"TheTimer","计时器" },
{"TheCheckBox","勾选框" },
{"DigitalNumber","液晶数字" },
{"TheComboBox","下拉框" },
{"TheImage","图片" },
{"TheTextBlock","文本块" },
{"ArcGauge","仪表盘" },
{"StatusLight","状态灯" },
{"TheToggleButton","开关按钮" },
{"TheGroupBox","分组" },
{"TheTextBox","文本框" },
{"NumberBox","数值框" },
{"GraphArrow","箭头" },
{"GraphStar","五角星" },
{"TheSlider","滑块" },
{"TheRadioButton","单选按钮" },
{"SwitchButton","开关" },
{"KnobButton","旋钮" },
{"Silos","物料仓" },
{"NewConveyorBelt","滚动线" },
{"TheDataGrid","表格" },
{"TheListBox","列表控件" },
{"TheRedis","Redis控件" },
{"WaveProgressBar","进度条波浪" },
{"TheProgressBar","进度条正常" },
{"TheRedProgressBar","进度条圆形红" },
{"TheBlueProgressBar","进度条圆形蓝" },
{"TheGreenProgressBar","进度条圆形绿" },
{"TheAPI","API接口" },
{"TheMQTT","MQTT" },
{"TheTabControl","选项卡控件" },
{"TheCylinder","气缸控件" },
{"TheRectangle","矩形" },
{"TheEllipse","圆形" },
{"TheWuLiaoControl","物料集合控件" },
{"FYFTheListBox","列表控件-拖拽" },
{"ThePopMessage","消息弹窗" },
{"TheMessage","消息提示" },
};
#endregion
#region 命令
///
/// 启动或者停止
///
public RelayCommand RunUiCommand { get; set; }
///
/// 编辑
///
public RelayCommand EditCommand { get; set; }
#endregion
#region 常用函数
///
/// 显示当前xaml代码
///
public void ShowCode(TextEditor textEditor)
{
textEditor.Text = canvasPanel.Save();
}
///
/// 更新页面
///
public void UpdatePageBase(PageModel _pageModel)
{
try
{
List ChildrenStr = new List();
foreach (FrameworkElement element in _pageModel.visual.Children)
{
string xamlText = XamlWriter.Save(element);
ChildrenStr.Add(xamlText);
}
MenuModel.SelectPageModels.ChildrenStr= ChildrenStr;
}
catch (Exception ex)
{
}
}
#endregion
#region 脚本编辑数据
///
/// 当前编辑界面
///
public CanvasPanel canvasPanel;
///
/// 当前运行界面
///
public object runCanvas;
///
/// 加载
///
///
public void Loaded(object objCan, object objRun)
{
canvasPanel = objCan as CanvasPanel;
runCanvas = objRun;// as RunCanvas;
InitCommand();
}
///
/// 编辑
///
///
public void Edit(object obj)
{
string result = JsEditWindow.ShowEdit(((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value?.ToString(), canvasPanel.GetAllExecChildren());
((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value = result;
}
///
/// 子控件编辑
///
///
public void ChildEdit(object obj)
{
string xml = ChildEditWindow.ShowEdit(((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value?.ToString());
((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value = xml;
}
///
/// 绑定编辑
///
///
public void BingEdit(object obj)
{
//父控件
Xceed.Wpf.Toolkit.PropertyGrid.PropertyGrid _property = (Xceed.Wpf.Toolkit.PropertyGrid.PropertyGrid)((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).ParentElement;
//选中控件
string controlname = _property.SelectedObjectName;//名称
Type type = _property.SelectedObjectType;//类型
object selectpro = _property.SelectedProperty;
object selectControl = _property.SelectedObject;//对象
string selectProName = _property.SelectedProperty.ToString();
//选中属性
DependencyProperty dependencyProperty = PropertyHelper.GetDependencyProperties(selectControl)?.ToList().Find(x => x.Name == selectProName);
if (dependencyProperty != null)
{
string _binding = PropertyBindingWindow.ShowEdit(dependencyProperty, (selectControl as FrameworkElement), VariablePath);
if (!string.IsNullOrEmpty(_binding))
{
Binding bindingText = new Binding()
{
Mode = BindingMode.TwoWay,
Converter = new JTokenToBoolConverter(),
RelativeSource = new RelativeSource { Mode = RelativeSourceMode.Self },
Path = new PropertyPath(_binding)
};
(selectControl as FrameworkElement).SetBinding(dependencyProperty, bindingText);
}
else
{
BindingOperations.ClearBinding((selectControl as FrameworkElement), dependencyProperty);
}
}
}
///
/// 清除绑定
///
///
public void ClearBingEdit(object obj)
{
//父控件
Xceed.Wpf.Toolkit.PropertyGrid.PropertyGrid _property = (Xceed.Wpf.Toolkit.PropertyGrid.PropertyGrid)((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).ParentElement;
//选中控件
string controlname = _property.SelectedObjectName;//名称
Type type = _property.SelectedObjectType;//类型
object selectpro = _property.SelectedProperty;
object selectControl = _property.SelectedObject;//对象
string selectProName = _property.SelectedProperty.ToString();
//选中属性
DependencyProperty dependencyProperty = PropertyHelper.GetDependencyProperties(selectControl)?.ToList().Find(x => x.Name == selectProName);
if (dependencyProperty != null)
{
BindingOperations.ClearBinding((selectControl as FrameworkElement), dependencyProperty);
}
}
///
/// 图片选择路径
///
///
public void SelectPath(object obj)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "图片|*.jpg;*.png;*.gif;*.jpeg;*.bmp";
if (ofd.ShowDialog() == true)
{
if (!string.IsNullOrEmpty(LayoutsPath))
{
string qz = Path.GetDirectoryName(LayoutsPath);
if (!string.IsNullOrEmpty(qz) && qz.Contains("Layouts"))
{
string path = ofd.FileName;
string path2 = $"{qz.Replace("Layouts", "Images")}/{ofd.SafeFileName}";
FileInfo fi1 = new FileInfo(path);
FileInfo fi2 = new FileInfo(path2);
try
{
if (!Directory.Exists(qz.Replace("Layouts", "Images"))) // 返回bool类型,存在返回true,不存在返回false
{
Directory.CreateDirectory(qz.Replace("Layouts", "Images")); //不存在则创建路径
}
if (File.Exists(path2))
{
fi2.Delete();
}
fi1.CopyTo(path2);
((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value = System.IO.Path.GetFileName(ofd.FileName);
}
catch (IOException ioex)
{
Console.WriteLine(ioex.Message);
}
}
}
}
}
///
/// 选择路径
///
///
public void SelectValuePath(object obj)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "请选择|*.*";
if (ofd.ShowDialog() == true)
{
((Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem)obj).Value = ofd.SafeFileName;
}
}
#endregion
#region 命令事件,页面编辑 Command
///
/// 设置首页
///
public RelayCommand SetHomePageCommand { get; set; }
///
/// 向上移动
///
public RelayCommand SetTopPageCommand { get; set; }
///
/// 向下移动
///
public RelayCommand SetBottomPageCommand { get; set; }
///
/// 增加页-该项下增加
///
public RelayCommand