using MeterVision.db;
using MeterVision.model;
using MeterVision.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
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.Shapes;
namespace MeterVision.Dlg
{
///
/// EditSingleConfig.xaml 的交互逻辑
///
public partial class EditSingleConfig : Window
{
public SingleDetailItem mDetailItem { get; private set; }
private bool _can_mark;
public EditSingleConfig(SingleDetailItem detailItem)
{
InitializeComponent();
cmbMeterType.ItemsSource = StationItem.MeterTypeList;
cmbFlowRate.ItemsSource = StationItem.FlowRateList;
cmbLastUnit.ItemsSource = StationItem.UnitList;
cmbNumCount.ItemsSource = StationItem.NumList;
cmbIndCount.ItemsSource = StationItem.NumList;
mDetailItem = detailItem;
Init_Load(detailItem);
if (File.Exists(detailItem.SrcImage))
{
ucMark.Visibility = Visibility.Visible;
ucMark.SetImagePath(detailItem.SrcImage);
ucMark.MeterRegion_MarkFinished += UcMark_MeterRegion_MarkFinished;
ucMark.FeatureRegion_MarkFinished += UcMark_FeatureRegion_MarkFinished;
_can_mark = true;
}
else
{
ucMark.Visibility = Visibility.Hidden;
_can_mark = false;
}
if (_can_mark)
{
ucMark.MeterType = detailItem.MeterType;
}
}
private void UcMark_FeatureRegion_MarkFinished(string regions, int meterType)
{
//throw new NotImplementedException();
this.txtFeatureRegion.Text = regions;
}
private void UcMark_MeterRegion_MarkFinished(string regions, int meterType)
{
//throw new NotImplementedException();
this.txtDialRegion.Text = regions;
}
private void Init_Load(SingleDetailItem detailItem)
{
this.ChangeInfoByMeterType(detailItem.MeterType);
cmbMeterType.SelectedValue = detailItem.MeterType;
cmbFlowRate.Text = detailItem.FlowRate.ToString();
txtBrightVal.Text = detailItem.BrightVal.ToString();
cmbNumCount.Text = detailItem.DigitCount == 0 ? "" : detailItem.DigitCount.ToString();
cmbIndCount.Text = detailItem.PointerCount == 0 ? "" : detailItem.PointerCount.ToString();
txtDialRegion.Text = detailItem.MeterRegion;
cmbLastUnit.SelectedValue = detailItem.LastUnit;
if (detailItem.MeterType == 1 || detailItem.MeterType == 3 || detailItem.MeterType == 2)
{
//cmbLastUnit.SelectedValue = double.Parse(detailItem.LastUnit);
txtFeatureRegion.Text = detailItem.FeatureRegion;
}
txtLastValue.Text = detailItem.LastValue.ToString();
//string.IsNullOrEmpty(detailItem.LastTime) ? "" : detailItem.LastValue.ToString();
txtLastTime.Text = detailItem.LastTime;
}
private bool Get_Load()
{
bool trySuccess = false;
int meterType = -1;
if (cmbMeterType.SelectedItem != null)
{
meterType = (int)cmbMeterType.SelectedValue;
}
if (meterType == 0)
{
MessageBox.Show("请选择正确的表类型");
return false;
}
int flowRate = -1;
if (cmbFlowRate.SelectedItem != null)
{
flowRate = (int)cmbFlowRate.SelectedValue;
}
else
{
trySuccess = int.TryParse(cmbFlowRate.Text.Trim(), out flowRate);
}
if (flowRate == 0)
{
MessageBox.Show("请输入每小时最大流量");
return false;
}
double brightVal = -1;
trySuccess = double.TryParse(txtBrightVal.Text.Trim(), out brightVal);
if (brightVal == 0)
{
brightVal = 1.2;
}
int numCount = -1;
trySuccess = int.TryParse(cmbNumCount.Text.Trim(), out numCount);
if ((meterType == 1 || meterType == 3) && numCount < 4)
{
MessageBox.Show("请输入正确的数字个数");
return false;
}
int indCount = -1;
trySuccess = int.TryParse(cmbIndCount.Text.Trim(), out indCount);
if (meterType == 2 && indCount < 5)
{
MessageBox.Show("请输入正确的指针个数");
return false;
}
double lastUnit = 0;
if (cmbLastUnit.SelectedValue == null)
{
MessageBox.Show("请选择尾数单位");
return false;
}
lastUnit = (double)cmbLastUnit.SelectedValue;
string dialRegion = txtDialRegion.Text.Trim();
if (string.IsNullOrEmpty(dialRegion))
{
MessageBox.Show("请输入表盘坐标");
return false;
}
string featureRegion = txtFeatureRegion.Text.Trim();
if ((meterType == 1 || meterType == 3) && string.IsNullOrEmpty(featureRegion))
{
MessageBox.Show("请输入数字区域 \\ 首位指针坐标");
return false;
}
double lastValue;
trySuccess = double.TryParse(txtLastValue.Text.Trim(), out lastValue);
string lastTime = txtLastTime.Text.Trim();
mDetailItem .MeterType = meterType;
mDetailItem.FlowRate = flowRate;
mDetailItem.BrightVal = brightVal;
mDetailItem.DigitCount = numCount;
mDetailItem.PointerCount = indCount;
mDetailItem.LastUnit = 0;
mDetailItem.FeatureRegion = "";
if (meterType == 1 || meterType == 3 || meterType == 2)
{
mDetailItem.LastUnit = lastUnit;
mDetailItem.FeatureRegion = featureRegion;
}
mDetailItem.MeterRegion = dialRegion;
mDetailItem.LastValue = lastValue;
if (string.IsNullOrEmpty(lastTime))
{
lastTime = DateTime.Now.ToString("yyyyMMddHHmmss");
}
mDetailItem.LastTime = lastTime;
return true;
}
private void BtnOK_Click(object sender, RoutedEventArgs e)
{
bool blGet = Get_Load();
if (!blGet)
{
return;
}
TSingleDetail singleDetail = new TSingleDetail();
ObjectHelper.CopyMatchingFields(mDetailItem,singleDetail);
try
{
bool updateSuccess = DBSingle.UpdateSingleDetailByConfig(singleDetail);
if (updateSuccess)
{
DialogResult = true;
}
else
{
MessageBox.Show(Application.Current.MainWindow, "修改失败", "错误",
MessageBoxButton.OK, MessageBoxImage.Error);
}
}
catch (Exception ex)
{
MessageBox.Show(Application.Current.MainWindow, $"{ex.Message}", "错误",
MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void BtnClose_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
this.Close();
}
private void CmbMeterType_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int meterType = -1; //= int.Parse(cmbMeterType.SelectedValue.ToString());
//((KeyValuePair)cmbMeterType.SelectedItem).Key;
//int.TryParse(cmbMeterType.SelectedValue.ToString(),out meterType);
if (cmbMeterType.SelectedValue != null)
{
meterType = (int)cmbMeterType.SelectedValue;
}
this.ChangeInfoByMeterType(meterType);
//改变仪表的类型
ucMark.MeterType = meterType;
}
private void ChangeInfoByMeterType(int meterType)
{
if (meterType == 1 || meterType == 3)
{
txtLastUnitTitle.Text = "最后一位数字的单位:";
txtFeatureRegionTitle.Text = "数字区域坐标:";
if (meterType == 1)
{
grdIndCount.Width = new GridLength(1, GridUnitType.Star);
grdNumCount.Width = new GridLength(1, GridUnitType.Star);
}
else
{
grdIndCount.Width = new GridLength(0, GridUnitType.Pixel);
grdNumCount.Width = new GridLength(1, GridUnitType.Star);
}
}
else if (meterType == 2)
{
txtLastUnitTitle.Text = "最后一个指针的单位:";
txtFeatureRegionTitle.Text = "首位指针同刻度坐标:";
grdNumCount.Width = new GridLength(0, GridUnitType.Pixel);
grdIndCount.Width = new GridLength(1, GridUnitType.Star);
}
}
private void CmbFlowRate_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
private void CmbFlowRate_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
// 只允许数字输入
e.Handled = !Regex.IsMatch(e.Text, @"^\d$");
}
private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
// 如果输入法启用(例如中文输入法),则阻止输入
//if (InputMethod.GetIsInputMethodEnabled)
//{
// e.Handled = true;
// return;
//}
// 只允许数字输入
//e.Handled = !Regex.IsMatch(e.Text, @"^\d$");
// 判断当前输入的字符是否是数字
if (e.Text.All(char.IsDigit))
{
e.Handled = false; // 如果是数字,允许输入
}
else
{
// 如果当前输入的字符不是数字,并且是中文输入法的拼音或候选字,禁止输入
e.Handled = true;
}
}
private void BtnWholeMeter_Click(object sender, RoutedEventArgs e)
{
txtDialRegion.Text = "0,0 319,239";
}
//---------------------------------------------------
}
}