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; 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; 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(); new_STDdata.Hardness = item["Hardness"].ToString(); new_STDdata.Density = item["Density"].ToString(); new_STDdata.Electrical_conductivity = item["Electrical_conductivity"].ToString(); new_STDdata.BSE = item["BSE"].ToString(); new_STDdata.Formula = item["Formula"].ToString(); new_STDdata.Element = item["Formula"].ToString(); new_STDdata.StrName = item["StrName"].ToString(); new_STDdata.Expression = item["Expression"].ToString(); new_STDdata.Color = item["Color"].ToString(); new_STDdata.KeyElementList = item["KeyElementList"].ToString(); new_STDdata.SubElementList = item["SubElementList"].ToString(); new_STDdata.GroupId = item["GroupId"].ToString(); 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.Key; PropGrid[i, 1] = new SourceGrid.Cells.Cell(kv.Value.StrName, typeof(string)); PropGrid[i, 1].Tag = kv.Key; PropGrid[i, 2] = new SourceGrid.Cells.Cell("", typeof(string)); SourceGrid.Cells.Views.Cell view = new SourceGrid.Cells.Views.Cell(); view.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[int.Parse(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 { int r = PropGrid.Selection.ActivePosition.Row; MakeCheckboxUnchecked(); PropGrid[PropGrid.Selection.ActivePosition.Row, 0].Value = true; } PropGrid.Refresh(); } } private void TSLoad_Click(object sender, EventArgs e) { OpenFileDialog fileSel = new OpenFileDialog(); fileSel.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 = (-1 == m_SubMidWindow.m_MainForm.m_STDRuleslist.PreRow) ? false : true; this.insertToolStripMenuItem.Enabled = bEnable; } } catch (Exception/* ex*/) { } } private void insertToolStripMenuItem_Click(object sender, EventArgs e) { 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() != "") { m_SubMidWindow.m_MainForm.SaveDataOfSelRule(i, j); PropGrid.Focus(true); int selrow = PropGrid.Selection.ActivePosition.Row; int STDnum = (int)PropGrid[selrow, 1].Tag; int STDId = 1; foreach (KeyValuePair kv in m_SubMidWindow.m_MainForm.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; m_SubMidWindow.m_MainForm.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.PreRow = i; } } 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) { 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, j].Value.ToString().Replace(" ", "").Trim() != "") { m_SubMidWindow.m_MainForm.SaveDataOfSelRule(i, j); AddIntoMainLab(); } } else if(i==-1&&j==-1) { AddIntoMainLab(); } } void AddIntoMainLab() { PropGrid.Focus(true); int selrow = PropGrid.Selection.ActivePosition.Row; int STDnum = (int)PropGrid[selrow, 1].Tag; int STDId = 1; foreach (KeyValuePair kv in m_SubMidWindow.m_MainForm.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; if (m_SubMidWindow.m_MainForm.GroupIdDictionaryFromId.ContainsKey(int.Parse(ReferenceDictionary[STDnum].GroupId))) { sT.GroupId = ReferenceDictionary[STDnum].GroupId; } else { MessageBox.Show("As this classification group is not defined, it has been adjusted to the default grouping!"); } m_SubMidWindow.m_MainForm.STDDictionary.Add(STDId + 1, sT); m_SubMidWindow.m_MainForm.m_STDRuleslist.AddNewRow(STDId + 1, sT.StrName, STDRuleslist.colorHx16toRGB(sT.Color)); m_SubMidWindow.m_MainForm.m_STDRuleslist.Refresh(); m_SubMidWindow.m_MainForm.m_STDRuleslist.PreRow = m_SubMidWindow.m_MainForm.m_STDRuleslist.Grid_Minerals.RowsCount - 1; } } }