EditSingleConfig.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. using MeterVision.db;
  2. using MeterVision.model;
  3. using MeterVision.Util;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Data;
  14. using System.Windows.Documents;
  15. using System.Windows.Input;
  16. using System.Windows.Media;
  17. using System.Windows.Media.Imaging;
  18. using System.Windows.Shapes;
  19. namespace MeterVision.Dlg
  20. {
  21. /// <summary>
  22. /// EditSingleConfig.xaml 的交互逻辑
  23. /// </summary>
  24. public partial class EditSingleConfig : Window
  25. {
  26. public SingleDetailItem mDetailItem { get; private set; }
  27. private bool _can_mark;
  28. public EditSingleConfig(SingleDetailItem detailItem)
  29. {
  30. InitializeComponent();
  31. cmbMeterType.ItemsSource = StationItem.MeterTypeList;
  32. cmbFlowRate.ItemsSource = StationItem.FlowRateList;
  33. cmbLastUnit.ItemsSource = StationItem.UnitList;
  34. cmbNumCount.ItemsSource = StationItem.NumList;
  35. cmbIndCount.ItemsSource = StationItem.NumList;
  36. mDetailItem = detailItem;
  37. Init_Load(detailItem);
  38. if (File.Exists(detailItem.SrcImage))
  39. {
  40. ucMark.Visibility = Visibility.Visible;
  41. ucMark.SetImagePath(detailItem.SrcImage);
  42. ucMark.MeterRegion_MarkFinished += UcMark_MeterRegion_MarkFinished;
  43. ucMark.FeatureRegion_MarkFinished += UcMark_FeatureRegion_MarkFinished;
  44. _can_mark = true;
  45. }
  46. else
  47. {
  48. ucMark.Visibility = Visibility.Hidden;
  49. _can_mark = false;
  50. }
  51. if (_can_mark)
  52. {
  53. ucMark.MeterType = detailItem.MeterType;
  54. }
  55. }
  56. private void UcMark_FeatureRegion_MarkFinished(string regions, int meterType)
  57. {
  58. //throw new NotImplementedException();
  59. this.txtFeatureRegion.Text = regions;
  60. }
  61. private void UcMark_MeterRegion_MarkFinished(string regions, int meterType)
  62. {
  63. //throw new NotImplementedException();
  64. this.txtDialRegion.Text = regions;
  65. }
  66. private void Init_Load(SingleDetailItem detailItem)
  67. {
  68. this.ChangeInfoByMeterType(detailItem.MeterType);
  69. cmbMeterType.SelectedValue = detailItem.MeterType;
  70. cmbFlowRate.Text = detailItem.FlowRate.ToString();
  71. //txtBrightVal.Text = detailItem.BrightVal.ToString();
  72. txtNumInUpper.Text = detailItem.NumInUpper.ToString();
  73. cmbNumCount.Text = detailItem.DigitCount == 0 ? "" : detailItem.DigitCount.ToString();
  74. cmbIndCount.Text = detailItem.PointerCount == 0 ? "" : detailItem.PointerCount.ToString();
  75. txtDialRegion.Text = detailItem.MeterRegion;
  76. cmbLastUnit.SelectedValue = detailItem.LastUnit;
  77. if (detailItem.MeterType == 1 || detailItem.MeterType == 3 || detailItem.MeterType == 2)
  78. {
  79. //cmbLastUnit.SelectedValue = double.Parse(detailItem.LastUnit);
  80. txtFeatureRegion.Text = detailItem.FeatureRegion;
  81. }
  82. txtLastValue.Text = detailItem.LastValue.ToString();
  83. //string.IsNullOrEmpty(detailItem.LastTime) ? "" : detailItem.LastValue.ToString();
  84. txtLastTime.Text = detailItem.LastTime;
  85. }
  86. private bool Get_Load()
  87. {
  88. bool trySuccess = false;
  89. int meterType = -1;
  90. if (cmbMeterType.SelectedItem != null)
  91. {
  92. meterType = (int)cmbMeterType.SelectedValue;
  93. }
  94. if (meterType == 0)
  95. {
  96. MessageBox.Show("请选择正确的表类型");
  97. return false;
  98. }
  99. int flowRate = -1;
  100. if (cmbFlowRate.SelectedItem != null)
  101. {
  102. flowRate = (int)cmbFlowRate.SelectedValue;
  103. }
  104. else
  105. {
  106. trySuccess = int.TryParse(cmbFlowRate.Text.Trim(), out flowRate);
  107. }
  108. if (flowRate == 0)
  109. {
  110. MessageBox.Show("请输入每小时最大流量");
  111. return false;
  112. }
  113. //double brightVal = -1;
  114. //trySuccess = double.TryParse(txtBrightVal.Text.Trim(), out brightVal);
  115. //if (brightVal == 0)
  116. //{
  117. double brightVal = 1.2;
  118. //}
  119. int numCount = -1;
  120. trySuccess = int.TryParse(cmbNumCount.Text.Trim(), out numCount);
  121. if ((meterType == 1 || meterType == 3) && numCount < 4)
  122. {
  123. MessageBox.Show("请输入正确的数字个数");
  124. return false;
  125. }
  126. int indCount = -1;
  127. trySuccess = int.TryParse(cmbIndCount.Text.Trim(), out indCount);
  128. if (meterType == 2 && indCount < 5)
  129. {
  130. MessageBox.Show("请输入正确的指针个数");
  131. return false;
  132. }
  133. double lastUnit = 0;
  134. if (cmbLastUnit.SelectedValue == null)
  135. {
  136. MessageBox.Show("请选择尾数单位");
  137. return false;
  138. }
  139. lastUnit = (double)cmbLastUnit.SelectedValue;
  140. int numInUpper = -1;
  141. if (meterType == 1 || meterType == 3)
  142. {
  143. if (!int.TryParse(txtNumInUpper.Text.Trim(), out numInUpper) || (numInUpper != 0 && numInUpper != 1))
  144. {
  145. MessageBox.Show("请输入正确的数字在上的值");
  146. return false;
  147. }
  148. }
  149. else
  150. {
  151. numInUpper = 1; //这个应该是无所谓的
  152. }
  153. string dialRegion = txtDialRegion.Text.Trim();
  154. //if (string.IsNullOrEmpty(dialRegion))
  155. //{
  156. // MessageBox.Show("请输入表盘坐标");
  157. // return false;
  158. //}
  159. string featureRegion = txtFeatureRegion.Text.Trim();
  160. //if ((meterType == 1 || meterType == 3) && string.IsNullOrEmpty(featureRegion))
  161. //{
  162. // MessageBox.Show("请输入数字区域 \\ 首位指针坐标");
  163. // return false;
  164. //}
  165. double lastValue;
  166. trySuccess = double.TryParse(txtLastValue.Text.Trim(), out lastValue);
  167. string lastTime = txtLastTime.Text.Trim();
  168. mDetailItem .MeterType = meterType;
  169. mDetailItem.FlowRate = flowRate;
  170. mDetailItem.BrightVal = brightVal;
  171. mDetailItem.DigitCount = numCount;
  172. mDetailItem.PointerCount = indCount;
  173. mDetailItem.LastUnit = 0;
  174. mDetailItem.FeatureRegion = "";
  175. if (meterType == 1 || meterType == 3 || meterType == 2)
  176. {
  177. mDetailItem.LastUnit = lastUnit;
  178. mDetailItem.FeatureRegion = featureRegion;
  179. }
  180. mDetailItem.NumInUpper = numInUpper;
  181. mDetailItem.MeterRegion = dialRegion;
  182. mDetailItem.LastValue = lastValue;
  183. if (string.IsNullOrEmpty(lastTime))
  184. {
  185. lastTime = DateTime.Now.ToString("yyyyMMddHHmmss");
  186. }
  187. mDetailItem.LastTime = lastTime;
  188. return true;
  189. }
  190. private void BtnOK_Click(object sender, RoutedEventArgs e)
  191. {
  192. bool blGet = Get_Load();
  193. if (!blGet)
  194. {
  195. return;
  196. }
  197. TSingleDetail singleDetail = new TSingleDetail();
  198. ObjectHelper.CopyMatchingFields(mDetailItem,singleDetail);
  199. try
  200. {
  201. bool updateSuccess = DBSingle.UpdateSingleDetailByConfig(singleDetail);
  202. if (updateSuccess)
  203. {
  204. DialogResult = true;
  205. }
  206. else
  207. {
  208. MessageBox.Show(Application.Current.MainWindow, "修改失败", "错误",
  209. MessageBoxButton.OK, MessageBoxImage.Error);
  210. }
  211. }
  212. catch (Exception ex)
  213. {
  214. MessageBox.Show(Application.Current.MainWindow, $"{ex.Message}", "错误",
  215. MessageBoxButton.OK, MessageBoxImage.Error);
  216. }
  217. }
  218. private void BtnClose_Click(object sender, RoutedEventArgs e)
  219. {
  220. DialogResult = false;
  221. this.Close();
  222. }
  223. private void CmbMeterType_SelectionChanged(object sender, SelectionChangedEventArgs e)
  224. {
  225. int meterType = -1; //= int.Parse(cmbMeterType.SelectedValue.ToString());
  226. //((KeyValuePair<int, string>)cmbMeterType.SelectedItem).Key;
  227. //int.TryParse(cmbMeterType.SelectedValue.ToString(),out meterType);
  228. if (cmbMeterType.SelectedValue != null)
  229. {
  230. meterType = (int)cmbMeterType.SelectedValue;
  231. }
  232. this.ChangeInfoByMeterType(meterType);
  233. //改变仪表的类型
  234. ucMark.MeterType = meterType;
  235. }
  236. private void ChangeInfoByMeterType(int meterType)
  237. {
  238. if (meterType == 1 || meterType == 3)
  239. {
  240. txtLastUnitTitle.Text = "最后一位数字的单位:";
  241. txtFeatureRegionTitle.Text = "数字区域坐标:";
  242. if (meterType == 1)
  243. {
  244. grdIndCount.Width = new GridLength(1, GridUnitType.Star);
  245. grdNumCount.Width = new GridLength(1, GridUnitType.Star);
  246. }
  247. else
  248. {
  249. grdIndCount.Width = new GridLength(0, GridUnitType.Pixel);
  250. grdNumCount.Width = new GridLength(1, GridUnitType.Star);
  251. }
  252. grdFlowRate.Width = new GridLength(1, GridUnitType.Star);
  253. grdNumInUpper.Width = new GridLength(1, GridUnitType.Star);
  254. }
  255. else if (meterType == 2)
  256. {
  257. txtLastUnitTitle.Text = "最后一个指针的单位:";
  258. txtFeatureRegionTitle.Text = "首位指针同刻度坐标:";
  259. grdNumCount.Width = new GridLength(0, GridUnitType.Pixel);
  260. grdIndCount.Width = new GridLength(1, GridUnitType.Star);
  261. grdFlowRate.Width = new GridLength(1, GridUnitType.Star);
  262. grdNumInUpper.Width = new GridLength(0, GridUnitType.Pixel);
  263. }
  264. }
  265. private void CmbFlowRate_SelectionChanged(object sender, SelectionChangedEventArgs e)
  266. {
  267. }
  268. private void CmbFlowRate_PreviewTextInput(object sender, TextCompositionEventArgs e)
  269. {
  270. // 只允许数字输入
  271. e.Handled = !Regex.IsMatch(e.Text, @"^\d$");
  272. }
  273. private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
  274. {
  275. // 如果输入法启用(例如中文输入法),则阻止输入
  276. //if (InputMethod.GetIsInputMethodEnabled)
  277. //{
  278. // e.Handled = true;
  279. // return;
  280. //}
  281. // 只允许数字输入
  282. //e.Handled = !Regex.IsMatch(e.Text, @"^\d$");
  283. // 判断当前输入的字符是否是数字
  284. if (e.Text.All(char.IsDigit))
  285. {
  286. e.Handled = false; // 如果是数字,允许输入
  287. }
  288. else
  289. {
  290. // 如果当前输入的字符不是数字,并且是中文输入法的拼音或候选字,禁止输入
  291. e.Handled = true;
  292. }
  293. }
  294. private void BtnWholeMeter_Click(object sender, RoutedEventArgs e)
  295. {
  296. txtDialRegion.Text = "0,0 319,239";
  297. }
  298. //---------------------------------------------------
  299. }
  300. }