using OTS.WinFormsUI.Docking; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using static System.Windows.Forms.VisualStyles.VisualStyleElement; namespace OTSPartA_STDEditor { public partial class ReferenceLibrary : DockContent { public SubMidWindow m_SubMidWindow = null; protected SourceGrid.Cells.Views.Cell m_CellGroupType = null; public Dictionary ReferenceDictionary = new Dictionary(); System.Collections.Hashtable table_ReferenceLibrary; readonly Dictionary ReferenceGroup = new Dictionary(); public ReferenceLibrary(SubMidWindow SubMidWindow) { InitializeComponent(); m_SubMidWindow = SubMidWindow; } private void InitGrid() { int iColumnWidth = 20; // 第一列的宽度 int width = this.Width; PropGrid.Redim(1, 12); PropGrid.Columns.SetWidth(0, iColumnWidth); PropGrid.Columns.AutoSizeColumn(2); // 固定进度条 PropGrid.Columns.SetWidth(2, 1 * (width - iColumnWidth) / 30); PropGrid.Columns.SetWidth(3, 25 * (width - iColumnWidth) / 30); PropGrid.Columns.SetWidth(4, 3 * (width - iColumnWidth) / 30); PropGrid.Columns.SetWidth(5, 1 * (width - iColumnWidth) / 30); PropGrid.Columns.SetWidth(6, 1 * (width - iColumnWidth) / 30); PropGrid.Columns.SetWidth(7, 2 * (width - iColumnWidth) / 30); PropGrid.Columns.SetWidth(8, 1 * (width - iColumnWidth) / 30); PropGrid.Columns.SetWidth(9, 3 * (width - iColumnWidth) / 30); PropGrid.Columns.SetWidth(10, 10 * (width - iColumnWidth) / 30); PropGrid.Columns.SetWidth(11, 1 * (width - iColumnWidth) / 30); PropGrid.AutoStretchColumnsToFitWidth = true; // 创建列标题 if (null == PropGrid[0, 1]) { List listColName = new List(); if (m_SubMidWindow.m_MainForm.lan.GetNameTable("Form_Main")["language"].ToString() == "ZH") { listColName.Add("规则名称"); listColName.Add("颜色"); listColName.Add("表达式"); listColName.Add("必须存在元素"); listColName.Add("硬度"); listColName.Add("密度"); listColName.Add("导电性"); listColName.Add("BSE"); listColName.Add("化学式"); listColName.Add("元素"); listColName.Add("分组名称"); } else { listColName.Add("Rule Name"); listColName.Add("Color"); listColName.Add("Expression"); listColName.Add("Elements must exist"); listColName.Add("Hardness"); listColName.Add("Density"); listColName.Add("Conductivity"); listColName.Add("BSE"); listColName.Add("Formula"); listColName.Add("Element"); listColName.Add("GroupName"); } for (int i = 0; i < listColName.Count; i++) { if (null == PropGrid[0, 1 + i]) { PropGrid[0, 1 + i] = new SourceGrid.Cells.Cell(listColName[i]); } else { PropGrid[0, 1 + i].Value = listColName[i]; } } PropGrid.FixedRows = 1; // 第一行是列标题不可以滚动 PropGrid.Selection.EnableMultiSelection = false; PropGrid.Refresh(); } } private void ComparativeLibrary_Load(object sender, EventArgs e) { m_SubMidWindow.m_MainForm.lan = new Language(this); table_ReferenceLibrary = m_SubMidWindow.m_MainForm.lan.GetNameTable("ReferenceLibrary"); InitGrid(); } bool LoadClassifyToDictionary(string DBAddress, ref Dictionary STDDictionary) { try { System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + DBAddress + "'"); m_dbConnection.Open(); //加载STD信息 System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select * from ClassifySTD", m_dbConnection); DataSet ds = new DataSet(); m_dataAdapter.Fill(ds); DataTable dt = ds.Tables[0]; ReferenceGroup.Clear(); STDDictionary.Clear(); if (dt != null) { if (dt.Rows.Count > 0) { foreach (DataRow item in dt.Rows) { STDdata new_STDdata = new STDdata { STDId = int.Parse(item["STDId"].ToString()), Hardness = item["Hardness"].ToString(), Density = item["Density"].ToString(), Electrical_conductivity = item["Electrical_conductivity"].ToString(), BSE = item["BSE"].ToString(), Formula = item["Formula"].ToString(), Element = item["Formula"].ToString(), StrName = item["StrName"].ToString(), Expression = item["Expression"].ToString(), Color = item["Color"].ToString(), KeyElementList = item["KeyElementList"].ToString(), SubElementList = item["SubElementList"].ToString(), GroupId = (int)item["GroupId"] }; STDDictionary.Add(int.Parse(item["STDId"].ToString()), new_STDdata); } } } System.Data.SQLite.SQLiteDataAdapter m_dataAdapterGroup = new System.Data.SQLite.SQLiteDataAdapter("select Id,name from STDGroups", m_dbConnection); DataSet dsGroup = new DataSet(); m_dataAdapterGroup.Fill(dsGroup); DataTable dtGroup = dsGroup.Tables[0]; if (dtGroup != null) { if (dtGroup.Rows.Count >= 0) { foreach (DataRow item in dtGroup.Rows) { ReferenceGroup.Add(int.Parse(item["Id"].ToString()), item["name"].ToString()); } if(!ReferenceGroup.Keys.Contains(0)) { ReferenceGroup.Add(0, "Default"); } } } return true; } catch (Exception) { MessageBox.Show("Reference library format error, please open the correct reference library!", "Tip"); return false; } } void AddDataToRefereceLiberary(Dictionary RefDictionary) { PropGrid.Redim(RefDictionary.Count+1, 12); int i = 1; foreach (KeyValuePair kv in RefDictionary) { if (null == PropGrid[i, 0]) { PropGrid[i, 0] = new SourceGrid.Cells.CheckBox(null, false); SourceGrid.Cells.Controllers.CheckBox clickEvent = new SourceGrid.Cells.Controllers.CheckBox(); clickEvent.CheckedChanged += new EventHandler(clickEvent_Click); PropGrid[i, 0].AddController(clickEvent); } else { PropGrid[i, 0].Value = false; } PropGrid.Rows[i].Height = 25; PropGrid.Rows[i].Tag = kv.Value; PropGrid[i, 1] = new SourceGrid.Cells.Cell(kv.Value.StrName, typeof(string)) { Tag = kv.Value }; PropGrid[i, 2] = new SourceGrid.Cells.Cell("", typeof(string)); SourceGrid.Cells.Views.Cell view = new SourceGrid.Cells.Views.Cell { BackColor = STDRuleslist.colorHx16toRGB(kv.Value.Color) }; PropGrid[i, 2].View = view; PropGrid[i, 3] = new SourceGrid.Cells.Cell(kv.Value.Expression, typeof(string)); PropGrid[i, 4] = new SourceGrid.Cells.Cell(kv.Value.KeyElementList, typeof(string)); PropGrid[i, 5] = new SourceGrid.Cells.Cell(kv.Value.Hardness, typeof(string)); PropGrid[i, 6] = new SourceGrid.Cells.Cell(kv.Value.Density, typeof(string)); PropGrid[i, 7] = new SourceGrid.Cells.Cell(kv.Value.Electrical_conductivity, typeof(string)); PropGrid[i, 8] = new SourceGrid.Cells.Cell(kv.Value.BSE, typeof(string)); PropGrid[i, 9] = new SourceGrid.Cells.Cell(kv.Value.Formula, typeof(string)); PropGrid[i, 10] = new SourceGrid.Cells.Cell(kv.Value.Element, typeof(string)); PropGrid[i, 11] = new SourceGrid.Cells.Cell(ReferenceGroup[kv.Value.GroupId], typeof(string)); i++; } PropGrid.AutoSizeCells(); int iColumnWidth = 45; // 第一列的宽度 int width = this.Width; PropGrid.Columns.SetWidth(0, iColumnWidth); PropGrid.Columns.SetWidth(2, 1 * (width - iColumnWidth) / 20); //PropGrid.Columns.SetWidth(3, 25 * (width - iColumnWidth) / 30); //PropGrid.Columns.SetWidth(4, 3 * (width - iColumnWidth) / 30); //PropGrid.Columns.SetWidth(5, 1 * (width - iColumnWidth) / 30); //PropGrid.Columns.SetWidth(6, 1 * (width - iColumnWidth) / 30); //PropGrid.Columns.SetWidth(7, 2 * (width - iColumnWidth) / 30); //PropGrid.Columns.SetWidth(8, 1 * (width - iColumnWidth) / 30); //PropGrid.Columns.SetWidth(9, 3 * (width - iColumnWidth) / 30); //PropGrid.Columns.SetWidth(10, 10 * (width - iColumnWidth) / 30); for (int ro = 0; ro < PropGrid.RowsCount; ro++) { PropGrid.Rows.SetHeight(ro,25); } for (int col = 1; col < PropGrid.ColumnsCount; col++) { for (int ro = 0; ro < PropGrid.RowsCount; ro++) { PropGrid[ro,col].AddController(SourceGrid.Cells.Controllers.Unselectable.Default); } } PropGrid.Refresh(); //为修改bug 第一次点击sourcegrid滚动界面时,取数不准问题(载入数据时将第一次点击落在1,0单元格) if (PropGrid[1,0] != null) { SourceGrid.Position pos = new SourceGrid.Position(1, 0); PropGrid.Selection.Focus(pos, true); } } public void MakeCheckboxUnchecked() { for (int i = 1; i < PropGrid.RowsCount; i++) { if ((bool)PropGrid[i, 0].Value == true) { PropGrid[i, 0].Value = false; } } } private void clickEvent_Click(object sender, EventArgs e) { int t = PropGrid.Selection.ActivePosition.Column; if (t == 0)//选择事件 { if ((bool)PropGrid[PropGrid.Selection.ActivePosition.Row, 0].Value == true) { PropGrid[PropGrid.Selection.ActivePosition.Row, 0].Value = false; } else { MakeCheckboxUnchecked(); PropGrid[PropGrid.Selection.ActivePosition.Row, 0].Value = true; } PropGrid.Refresh(); } } private void TSLoad_Click(object sender, EventArgs e) { OpenFileDialog fileSel = new OpenFileDialog { Filter = "(*.db)|*.db" }; if (DialogResult.OK == fileSel.ShowDialog()) { ReferenceDictionary.Clear(); if (LoadClassifyToDictionary(fileSel.FileName, ref ReferenceDictionary)) { AddDataToRefereceLiberary(ReferenceDictionary); this.Text = " " + fileSel.FileName; PropGrid.Refresh(); } } } private void TSClose_Click(object sender, EventArgs e) { if (PropGrid.RowsCount > 1) { PropGrid.Rows.RemoveRange(1, PropGrid.RowsCount - 1); ReferenceDictionary.Clear(); this.Text = this.Text.Split(' ')[0]; PropGrid.Refresh(); } else { ReferenceDictionary.Clear(); this.Text = this.Text.Split(' ')[0]; PropGrid.Refresh(); } } private void PropGrid_MouseClick(object sender, MouseEventArgs e) { SourceGrid.Grid oSender = (SourceGrid.Grid)sender; oSender.Focus(); // 表示右击的是无效区域, 则退出(第0行是标题) if (oSender.MouseCellPosition.Row < 1 || oSender.MouseCellPosition.Column < 0) { return; } // 右击的区域获取焦点 PropGrid.Selection.Focus(new SourceGrid.Position(oSender.MouseCellPosition.Row, oSender.MouseCellPosition.Column), true); try { if (MouseButtons.Right == e.Button && e.Clicks == 1) { // 此处编写在什么情况下弹出右击菜单 contextMenuStrip.Show(PointToScreen(e.Location)); // 判断左侧区域是否已经选中了某个stdmineral bool bEnable = false; bEnable = null != m_SubMidWindow.m_MainForm.m_STDRuleslist.Predata; this.insertToolStripMenuItem.Enabled = bEnable; } } catch (Exception/* ex*/) { } } private void InsertToolStripMenuItem_Click(object sender, EventArgs e) { //if (m_SubMidWindow.m_MainForm.m_STDRuleslist.tabControl1.SelectedIndex == 0) //{ // MessageBox.Show("Please do not operate the reference library in the grouping interface!"); // return; //} //m_SubMidWindow.m_MainForm.m_STDRuleslist.Grid_Minerals.Focus(true); //int i = m_SubMidWindow.m_MainForm.m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Row; //int j = m_SubMidWindow.m_MainForm.m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Column; ///// 保证鼠标点击的GRID行和列是有效的 //if (i >= 0 && j >= 0) //{ // //规则名称不为空 // if (m_SubMidWindow.m_MainForm.m_STDRuleslist.Grid_Minerals[i, 0].Value.ToString().Replace(" ", "").Trim() != "") // { // int id = ((STDdata)m_SubMidWindow.m_MainForm.m_STDRuleslist.Grid_Minerals[i, 0].Tag).STDId; // m_SubMidWindow.m_MainForm.SaveDataOfSelRule(id); // PropGrid.Focus(true); // int selrow = PropGrid.Selection.ActivePosition.Row; // int STDnum = ((STDdata)PropGrid[selrow, 1].Tag).STDId; // int STDId = 1; // foreach (KeyValuePair kv in m_SubMidWindow.m_MainForm._sTDEditor.STDDictionary) // { // if (STDId < kv.Key) // { // STDId = kv.Key; // } // } // STDdata sT = new STDdata(); // sT.BSE = ReferenceDictionary[STDnum].BSE; // sT.StrName = ReferenceDictionary[STDnum].StrName; // sT.Color = ReferenceDictionary[STDnum].Color; // sT.Density = ReferenceDictionary[STDnum].Density; // sT.Expression = ReferenceDictionary[STDnum].Expression; // sT.Formula = ReferenceDictionary[STDnum].Formula; // sT.Hardness = ReferenceDictionary[STDnum].Hardness; // sT.UsingImgPropertyList = ReferenceDictionary[STDnum].UsingImgPropertyList; // sT.UsingOtherPropertyList = ReferenceDictionary[STDnum].UsingOtherPropertyList; // sT.KeyElementList = ReferenceDictionary[STDnum].KeyElementList; // sT.SubElementList = ReferenceDictionary[STDnum].SubElementList; // sT.Electrical_conductivity = ReferenceDictionary[STDnum].Electrical_conductivity; // sT.Element = ReferenceDictionary[STDnum].Element; // sT.GroupId = ReferenceDictionary[STDnum].GroupId; // sT.STDId = STDnum; // m_SubMidWindow.m_MainForm._sTDEditor.STDDictionary.Add(STDId + 1, sT); // m_SubMidWindow.m_MainForm.m_STDRuleslist.InsertNewRow(STDId + 1, sT.StrName, STDRuleslist.colorHx16toRGB(sT.Color)); // m_SubMidWindow.m_MainForm.m_STDRuleslist.Refresh(); // m_SubMidWindow.m_MainForm.m_STDRuleslist.Predata = (STDdata)m_SubMidWindow.m_MainForm.m_STDRuleslist.Grid_Minerals[i, 0].Tag; // } //} //else //{ // AddIntoMainLab(); //} } #region 因为复制容易引起用户误操作,故取消 private void copyDataToolStripMenuItem_Click(object sender, EventArgs e) { //int selrow = PropGrid.Selection.ActivePosition.Row; //int STDnum = (int)PropGrid[selrow, 1].Tag; //int STDid = (int)m_SubMidWindow.m_MainForm.m_STDRuleslist.Grid_Minerals[m_SubMidWindow.m_MainForm.m_STDRuleslist.PreRow, 0].Tag; //m_SubMidWindow.m_MainForm.STDDictionary[STDid] = ReferenceDictionary[STDnum]; //m_SubMidWindow.m_MainForm.m_STDRuleslist.ChangeStrName(m_SubMidWindow.m_MainForm.m_STDRuleslist.PreRow, ReferenceDictionary[STDnum].StrName); //m_SubMidWindow.m_MainForm.ChangeSTDEditorAndGrid_Attributes(STDid); //m_SubMidWindow.m_MainForm.m_STDRuleslist.Grid_Minerals.Refresh(); } #endregion private void addIntoMainLabToolStripMenuItem_Click(object sender, EventArgs e) { foreach (TreeNode treeNode1 in m_SubMidWindow.m_MainForm.m_STDRuleslist.treeView_G.Nodes) { if (((STDGroups)treeNode1.Tag).name == "Default") { if (m_SubMidWindow.m_MainForm.m_STDRuleslist.Predata != null) { m_SubMidWindow.m_MainForm.SaveDataOfSelRule(m_SubMidWindow.m_MainForm.m_STDRuleslist.Predata.STDId); } STDdata ddata = AddIntoMainLab(); int STDId = 40000; foreach (STDGroups grp in m_SubMidWindow.m_MainForm.m_STDRuleslist.GroupViewDic.Values) { foreach (STDdata ddata1 in grp.ContainSTD) { if (STDId < ddata1.STDId) { STDId = ddata1.STDId; } } } ddata.STDId = STDId + 1; TreeNode treeNode = new TreeNode(); treeNode.Text = ddata.StrName; treeNode.Tag = ddata; ddata.ListNum = ((((STDGroups)treeNode1.Tag).ContainSTD).Count - 1).ToString(); ddata.GroupId = ((STDGroups)treeNode1.Tag).id; ((STDGroups)treeNode1.Tag).ContainSTD.Add(ddata); m_SubMidWindow.m_MainForm.m_STDRuleslist.InitGroupView(); foreach (TreeNode node in m_SubMidWindow.m_MainForm.m_STDRuleslist.treeView_G.Nodes) { foreach (TreeNode node1 in node.Nodes) { if (((STDdata)node1.Tag).STDId == ddata.STDId) { m_SubMidWindow.m_MainForm.ChangeSTDEditorAndGrid_Attributes(ddata); m_SubMidWindow.m_MainForm.m_STDRuleslist.Predata = (STDdata)treeNode.Tag; m_SubMidWindow.m_MainForm.m_STDRuleslist.treeView_G.SelectedNode = node1; m_SubMidWindow.m_MainForm.m_STDRuleslist.treeView_G.Focus(); break; } } } } } } STDdata AddIntoMainLab() { PropGrid.Focus(true); int selrow = PropGrid.Selection.ActivePosition.Row; int STDnum = ((STDdata)PropGrid[selrow, 1].Tag).STDId; //int STDId = 40000; //foreach (KeyValuePair kv in m_SubMidWindow.m_MainForm._sTDEditor.STDDictionary) //{ // if (STDId < kv.Key) // { // STDId = kv.Key; // } //} STDdata sT = new STDdata { BSE = ReferenceDictionary[STDnum].BSE, StrName = ReferenceDictionary[STDnum].StrName, Color = ReferenceDictionary[STDnum].Color, Density = ReferenceDictionary[STDnum].Density, Expression = ReferenceDictionary[STDnum].Expression, Formula = ReferenceDictionary[STDnum].Formula, Hardness = ReferenceDictionary[STDnum].Hardness, UsingImgPropertyList = ReferenceDictionary[STDnum].UsingImgPropertyList, UsingOtherPropertyList = ReferenceDictionary[STDnum].UsingOtherPropertyList, KeyElementList = ReferenceDictionary[STDnum].KeyElementList, SubElementList = ReferenceDictionary[STDnum].SubElementList, Electrical_conductivity = ReferenceDictionary[STDnum].Electrical_conductivity, Element = ReferenceDictionary[STDnum].Element }; return sT; } } }