using DataRWDAL; using Microsoft.Win32; using System; using System.Collections.ObjectModel; using System.Configuration; using System.IO; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using XCommon; using XCore; using XHandler.Class; using XHandler.Class.DataEx; using XHandler.View.MethodProperty; using XImagingXhandler.XDAL; using static XHandler.Controls.MultiComboBox; namespace XHandler.View { /// /// BacteriaManagement.xaml 的交互逻辑 /// public partial class BacteriaManagement : UserControl { ObservableCollection bacteriaShapeList = new ObservableCollection(); ObservableCollection bacteriaEdgeList = new ObservableCollection(); ObservableCollection bacteriaColorList = new ObservableCollection(); ObservableCollection bacterias = new ObservableCollection(); BacteriaBll bacteriaBll = new BacteriaBll(); Bacteria copyBacteria = null; public Bacteria selectedBacteria { get; set; } public BacteriaManagement() { InitializeComponent(); bacterias = DataModule.getInstance().GetBacterias(); listboxBacteria.ItemsSource = bacterias; ObservableCollection bacteriaShapes = BacteriaShapeDB.GetBacteriaShapeList(); foreach (BacteriaShape shape in bacteriaShapes) { MultiCbxBaseData data = new MultiCbxBaseData(); data.ID = shape.bacteria_shape_id; data.ViewName = shape.bacteria_shape_name; data.IsCheck = false; bacteriaShapeList.Add(data); } mcbShape.ItemsSource = bacteriaShapeList; ObservableCollection bacteriaEdges = BacteriaEdgeDB.GetBacteriaEdgeList(); foreach (BacteriaEdge edge in bacteriaEdges) { MultiCbxBaseData data = new MultiCbxBaseData(); data.ID = edge.bacteria_edge_id; data.ViewName = edge.bacteria_edge_name; data.IsCheck = false; bacteriaEdgeList.Add(data); } mcbEdge.ItemsSource = bacteriaEdgeList; ObservableCollection bacteriaColors = BacteriaColorDB.GetBacteriaColorList(); foreach (BacteriaColor color in bacteriaColors) { MultiCbxBaseData data = new MultiCbxBaseData(); data.ID = color.bacteria_color_id; data.ViewName = color.bacteria_color_name; data.IsCheck = false; bacteriaColorList.Add(data); } mcbColor.ItemsSource = bacteriaColorList; ObservableCollection pickPositionList = DataModule.getInstance().GetPickPosition(); cbBacteriaPosition.ItemsSource = pickPositionList; ObservableCollection coatingPositionList = DataModule.getInstance().GetCoatingPosition(); cbCoatingPosition.ItemsSource = coatingPositionList; ObservableCollection dropdownNamesOfLogic = bacteriaBll.GetBacteriaLogicValue(); cbMergemasks.ItemsSource = dropdownNamesOfLogic; cbDraw_label.ItemsSource = dropdownNamesOfLogic; cbMergemasks.SelectedIndex = 0; cbDraw_label.SelectedIndex = 0; // 模型类型: 1-微牧酵母;2-普通酵母;3-酵母背光01;4-链霉菌 ObservableCollection choiceModelList = ComUtility.GetDropDownList(ConfigurationManager.AppSettings["ChoiceModelType"].ToString()); cbModeltype.ItemsSource = choiceModelList; cbModeltype.SelectedIndex = 0; // 通道颜色: 0-灰;1-红;2-绿;3-蓝 ObservableCollection chanelColorList = ComUtility.GetDropDownList(ConfigurationManager.AppSettings["ChanelColor"].ToString()); cbChanel1Color.ItemsSource = chanelColorList; cbChanel1Color.SelectedIndex = 0; cbChanel2Color.ItemsSource = chanelColorList; cbChanel2Color.SelectedIndex = 0; tbxMark_color1.Text = "255"; tbxMark_color2.Text = "0"; tbxMark_color3.Text = "0"; tbxCaptureDs_ratio.Text = "0.25"; tbxMean_diam.Text = "120"; tbxFont_Size.Text = "30"; tbxDistToEdge.Text = "500"; } private void btnUpload_Click(object sender, RoutedEventArgs e) { if (selectedBacteria == null) return; OpenFileDialog dlg = new OpenFileDialog(); dlg.Filter = "Image files(*.jpg,*.gif,*.png)|*.jpg;*.gif;*.png|All files|*.*"; dlg.FilterIndex = 1; dlg.RestoreDirectory = true; if (dlg.ShowDialog() == true) { string imgFile = dlg.FileNames[0]; string folder = ConfigurationManager.AppSettings["ImgPath"].ToString(); if (!Directory.Exists(folder)) Directory.CreateDirectory(folder); string ext = System.IO.Path.GetExtension(imgFile); string destName = DateTime.Now.ToString("yyyyMMddTHHmmssms") + ext; string destFile = folder + "\\" + destName; File.Copy(imgFile, destFile, true); imageBacteria.Source = BitmapFrame.Create(new Uri(destFile), BitmapCreateOptions.None, BitmapCacheOption.OnLoad); selectedBacteria.bacteria_picture = destName; } } private void btnDelete_Click(object sender, RoutedEventArgs e) { if (selectedBacteria == null) return; selectedBacteria.bacteria_picture = ""; imageBacteria.Source = BitmapFrame.Create(new Uri("pack://application:,,,./Assets/Consumables/无图片.png"), BitmapCreateOptions.None, BitmapCacheOption.OnLoad); } private void mcbShape_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (selectedBacteria == null) return; if (mcbShape.ChekedItems.Count > 0) { string selection = ""; ObservableCollection selectedList = mcbShape.ChekedItems; foreach (var data in selectedList) { if (string.IsNullOrEmpty(selection)) selection = data.ViewName; else selection += "," + data.ViewName; } selectedBacteria.bacteria_shape = selection; } } private void mcbEdge_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (selectedBacteria == null) return; if (mcbEdge.ChekedItems.Count > 0) { string selection = ""; ObservableCollection selectedList = mcbEdge.ChekedItems; foreach (var data in selectedList) { if (string.IsNullOrEmpty(selection)) selection = data.ViewName; else selection += "," + data.ViewName; } selectedBacteria.bacteria_edge = selection; } } private void mcbColor_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (selectedBacteria == null) return; if (mcbColor.ChekedItems.Count > 0) { string selection = ""; ObservableCollection selectedList = mcbColor.ChekedItems; foreach (var data in selectedList) { if (string.IsNullOrEmpty(selection)) selection = data.ViewName; else selection += "," + data.ViewName; } selectedBacteria.bacteria_color = selection; } } private void cbBacteriaPosition_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (selectedBacteria == null) return; selectedBacteria.choice_position = cbBacteriaPosition.SelectedIndex + 1; } private void listboxBacteria_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (listboxBacteria.SelectedIndex < 0) return; Bacteria item = listboxBacteria.SelectedItem as Bacteria; selectedBacteria = DeepCopyByReflection.Copy(item); this.DataContext = selectedBacteria; string folder = ConfigurationManager.AppSettings["ImgPath"].ToString(); string imgPath = folder + "\\" + selectedBacteria.bacteria_picture; if (File.Exists(imgPath)) imageBacteria.Source = BitmapFrame.Create(new Uri(imgPath), BitmapCreateOptions.None, BitmapCacheOption.OnLoad); else imageBacteria.Source = BitmapFrame.Create(new Uri("pack://application:,,,./Assets/Consumables/无图片.png"), BitmapCreateOptions.None, BitmapCacheOption.OnLoad); if (string.IsNullOrEmpty(selectedBacteria.bacteria_shape)) mcbShape.ChekedItems.Clear(); else { mcbShape.ChekedItems.Clear(); string[] ary = selectedBacteria.bacteria_shape.Split(','); foreach (var str in ary) { MultiCbxBaseData data = bacteriaShapeList.Where(s => s.ViewName == str).FirstOrDefault(); if (data != null) mcbShape.AddSelectedItem(data); } } if (string.IsNullOrEmpty(selectedBacteria.bacteria_edge)) mcbEdge.ChekedItems.Clear(); else { mcbEdge.ChekedItems.Clear(); string[] ary = selectedBacteria.bacteria_edge.Split(','); foreach (var str in ary) { MultiCbxBaseData data = bacteriaEdgeList.Where(s => s.ViewName == str).FirstOrDefault(); if (data != null) mcbEdge.AddSelectedItem(data); } } if (string.IsNullOrEmpty(selectedBacteria.bacteria_color)) mcbColor.ChekedItems.Clear(); else { mcbColor.ChekedItems.Clear(); string[] ary = selectedBacteria.bacteria_color.Split(','); foreach (var str in ary) { MultiCbxBaseData data = bacteriaColorList.Where(s => s.ViewName == str).FirstOrDefault(); if (data != null) mcbColor.AddSelectedItem(data); } } if (selectedBacteria.choice_position == 2) cbBacteriaPosition.SelectedIndex = 1; else cbBacteriaPosition.SelectedIndex = 0; cbCoatingPosition.SelectedIndex = 0; //if (selectedBacteria.model_type == 1) //{ // cbModeltype.SelectedIndex = 0; //} //else if (selectedBacteria.model_type == 1) //{ // cbModeltype.SelectedIndex = 1; //} cbDraw_label.Text = selectedBacteria.draw_label; cbMergemasks.Text = selectedBacteria.merge_masks; if (!string.IsNullOrEmpty(selectedBacteria.mark_color)) { string[] markColors = selectedBacteria.mark_color.Split(','); tbxMark_color1.Text = markColors[0].Substring(1, markColors[0].Length - 1); tbxMark_color2.Text = markColors[1]; tbxMark_color3.Text = markColors[2].Substring(0, markColors[2].Length - 1); } else { tbxMark_color1.Text = "0"; tbxMark_color2.Text = "0"; tbxMark_color3.Text = "0"; } // 模型类型 cbModeltype.SelectedValue = selectedBacteria.model_type.ToString(); // 通道1、通道2 cbChanel1Color.SelectedValue = selectedBacteria.chanel1_color.ToString(); cbChanel2Color.SelectedValue = selectedBacteria.chanel2_color.ToString(); } public void SaveBacteria() { if (selectedBacteria == null) return; if (selectedBacteria.is_default_type == 1) return; Bacteria bacteria = listboxBacteria.SelectedItem as Bacteria; bacteria.Copy(selectedBacteria); bacteria.mark_color = "(" + tbxMark_color1.Text + "," + tbxMark_color2.Text + "," + tbxMark_color3.Text + ")"; bacteria.bacteria_content = tbbacteria_content.Text; DropdownName ddn = cbModeltype.SelectedItem as DropdownName; bacteria.model_type = Convert.ToInt32(ddn.dropdown_id.ToString()); DropdownName ddn1 = cbMergemasks.SelectedItem as DropdownName; bacteria.merge_masks = ddn1.dropdown_name.ToString(); DropdownName ddn2 = cbDraw_label.SelectedItem as DropdownName; bacteria.draw_label = ddn2.dropdown_name.ToString(); // 通道1、通道2 DropdownName chanel1Color = cbChanel1Color.SelectedItem as DropdownName; bacteria.chanel1_color = Convert.ToInt32(chanel1Color.dropdown_id); DropdownName chanel2Color = cbChanel2Color.SelectedItem as DropdownName; bacteria.chanel2_color = Convert.ToInt32(chanel2Color.dropdown_id); ; int ret = BacteriaDB.UpdateBacteriaIntodb(bacteria); if (ret > 0) { PlsSetProperty plsSetProperty = new PlsSetProperty($"菌落:{bacteria.bacteria_name} 保存成功", false); plsSetProperty.closeEvent += PlsSetProperty_closeEvent; System.Windows.Window wnd = Application.Current.MainWindow; Grid parent = Utilities.FindVisualChild(wnd); parent.Children.Add(plsSetProperty); } else { PlsSetProperty plsSetProperty = new PlsSetProperty($"菌落:{bacteria.bacteria_name} 保存失败", false); plsSetProperty.closeEvent += PlsSetProperty_closeEvent; System.Windows.Window wnd = Application.Current.MainWindow; Grid parent = Utilities.FindVisualChild(wnd); parent.Children.Add(plsSetProperty); } } public Bacteria AddNewBacteria(string name) { Bacteria newBacteria = new Bacteria(); newBacteria.bacteria_id = Guid.NewGuid().ToString(); newBacteria.bacteria_name = name; newBacteria.is_default_type = 0; newBacteria.bacteria_status = 1; newBacteria.timestamp = DateTime.Now; // 模型类型 if (cbModeltype.ItemsSource != null) { ObservableCollection dropdownNames = (ObservableCollection)cbModeltype.ItemsSource; newBacteria.model_type = Convert.ToInt32(dropdownNames[0].dropdown_id.ToString()); } // 合并识别 if (cbMergemasks.ItemsSource != null) { ObservableCollection dropdownNames = (ObservableCollection)cbMergemasks.ItemsSource; newBacteria.merge_masks = dropdownNames[0].dropdown_name.ToString(); } // 菌标记序号 if (cbDraw_label.ItemsSource != null) { ObservableCollection dropdownNames = (ObservableCollection)cbDraw_label.ItemsSource; newBacteria.draw_label = dropdownNames[0].dropdown_name.ToString(); } int ret = BacteriaDB.AddBacteriaIntodb(newBacteria); if (ret > 0) { bacterias.Add(newBacteria); PlsSetProperty plsSetProperty = new PlsSetProperty($"菌落:{name} 添加成功", false); plsSetProperty.closeEvent += PlsSetProperty_closeEvent; System.Windows.Window wnd = Application.Current.MainWindow; Grid parent = Utilities.FindVisualChild(wnd); parent.Children.Add(plsSetProperty); } else { newBacteria = null; PlsSetProperty plsSetProperty = new PlsSetProperty($"菌落:{name} 添加失败", false); plsSetProperty.closeEvent += PlsSetProperty_closeEvent; System.Windows.Window wnd = Application.Current.MainWindow; Grid parent = Utilities.FindVisualChild(wnd); parent.Children.Add(plsSetProperty); } return newBacteria; } private void menuCopy_Click(object sender, RoutedEventArgs e) { if (listboxBacteria.SelectedItem == null) return; copyBacteria = listboxBacteria.SelectedItem as Bacteria; ContextMenu menu = FindResource("consumableMenu") as ContextMenu; System.Windows.Controls.MenuItem mi = menu.Items[1] as System.Windows.Controls.MenuItem; mi.IsEnabled = true; } private void menuPaste_Click(object sender, RoutedEventArgs e) { if (copyBacteria == null) return; Bacteria newBacteria = copyBacteria.Copy() as Bacteria; newBacteria.bacteria_name = copyBacteria.bacteria_name + " " + Properties.Resources.strCopy; newBacteria.is_default_type = 0; newBacteria.bacteria_id = Guid.NewGuid().ToString(); bacterias.Add(newBacteria); BacteriaDB.AddBacteriaIntodb(newBacteria); } private void menuDelete_Click(object sender, RoutedEventArgs e) { if (listboxBacteria.SelectedItem == null) return; PlsToolTipWin plsToolTipWin = new PlsToolTipWin("Do you want to delete this bacteria? "); plsToolTipWin.Height = ActualHeight; plsToolTipWin.Width = ActualWidth; plsToolTipWin.btnClearData.Visibility = Visibility.Hidden; plsToolTipWin.lauchView = null; SolidColorBrush mybtn1_Brush = new SolidColorBrush(System.Windows.Media.Color.FromArgb(0, 0, 0, 0)); plsToolTipWin.Background = (System.Windows.Media.Brush)mybtn1_Brush; plsToolTipWin.ShowDialog(); if (plsToolTipWin.DialogResult == true) { Bacteria lab = listboxBacteria.SelectedItem as Bacteria; bacterias.Remove(lab); lab.bacteria_status = 0; int flag = BacteriaDB.UpdateBacteriaIntodb(lab); if (flag > 0) { PlsSetProperty plsSetProperty = new PlsSetProperty($"菌落:{lab.bacteria_name} 删除成功", false); plsSetProperty.closeEvent += PlsSetProperty_closeEvent; System.Windows.Window wnd = Application.Current.MainWindow; Grid parent = Utilities.FindVisualChild(wnd); parent.Children.Add(plsSetProperty); } else { PlsSetProperty plsSetProperty = new PlsSetProperty($"菌落:{lab.bacteria_name} 删除失败", false); plsSetProperty.closeEvent += PlsSetProperty_closeEvent; System.Windows.Window wnd = Application.Current.MainWindow; Grid parent = Utilities.FindVisualChild(wnd); parent.Children.Add(plsSetProperty); } } else { } } private void listboxBacteria_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e) { System.Windows.Controls.ListBoxItem listBoxItem = Utilities.FindVisualParent(e.OriginalSource as DependencyObject) as System.Windows.Controls.ListBoxItem; if (listBoxItem != null) { listBoxItem.Focus(); Bacteria bacteria = listBoxItem.DataContext as Bacteria; listboxBacteria.SelectedItem = bacteria; ContextMenu menu = FindResource("BacteriaMenu") as ContextMenu; System.Windows.Controls.MenuItem mi = menu.Items[2] as System.Windows.Controls.MenuItem; if (bacteria.is_default_type == 1) mi.IsEnabled = false; else mi.IsEnabled = true; e.Handled = true; } } private void color_TextChanged(object sender, TextChangedEventArgs e) { System.Windows.Controls.TextBox text = ((System.Windows.Controls.TextBox)sender); if (int.TryParse(text.Text, out int flag)) { if (flag >= 0 && flag <= 255) { colorError.Text = ""; text.BorderThickness = new Thickness(0, 0, 0, 0); return; } } colorError.Text = "输入范围应在[0-255]的整数"; text.BorderThickness = new Thickness(1, 1, 1, 1); text.BorderBrush = Brushes.Red; } private void PlsSetProperty_closeEvent(object sender, EventArgs e) { System.Windows.Window wnd = Application.Current.MainWindow; Grid grid = Utilities.FindVisualChild(wnd); UIElement element = sender as UIElement; grid.Children.Remove(element); } } }