using OTS.WinFormsUI.Docking; using SpectrumSTDEditor.BaseClass; using System; using System.Collections.Generic; using System.Data; using System.Windows.Forms; namespace SpectrumSTDEditor { 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(); List ReferenceXrayList = null; byte[] ReferencexrayByteData = null; public ReferenceLibrary(SubMidWindow SubMidWindow) { InitializeComponent(); m_SubMidWindow = SubMidWindow; } private void InitGrid() { int iColumnWidth = 20; // 第一列的宽度 int width = this.Width; PropGrid.Redim(1, 10); PropGrid.Columns.SetWidth(0, iColumnWidth); PropGrid.Columns.AutoSizeColumn(2); // 固定进度条 PropGrid.Columns.SetWidth(2, 1 * (width - iColumnWidth) / 30); PropGrid.Columns.SetWidth(3, 1 * (width - iColumnWidth) / 30); PropGrid.Columns.SetWidth(4, 25 * (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, 3 * (width - iColumnWidth) / 30); PropGrid.Columns.SetWidth(9, 5 * (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("BSE"); listColName.Add("化学式"); listColName.Add("元素"); } else { listColName.Add("Rule Name"); listColName.Add("ChineseName"); listColName.Add("Color"); listColName.Add("Hardness"); listColName.Add("Density"); listColName.Add("BSE"); listColName.Add("Formula"); listColName.Add("Element"); } 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 STDMinerals", 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["rigiditymod"].ToString(); new_STDdata.Density = item["density"].ToString(); new_STDdata.BSE = item["BSEValue"].ToString(); new_STDdata.Formula = item["formula"].ToString(); new_STDdata.Element = item["element"].ToString(); new_STDdata.StrName = item["name"].ToString(); new_STDdata.Color = item["color"].ToString(); new_STDdata.ChineseName = item["chinesename"].ToString(); new_STDdata.XrayData = (byte[])item["SPEC"]; STDDictionary.Add(int.Parse(item["id"].ToString()), new_STDdata); } } } //加载Xray信息 System.Data.SQLite.SQLiteDataAdapter m_dataAdapterXray = new System.Data.SQLite.SQLiteDataAdapter("select Id,SPEC from STDMinerals", m_dbConnection); DataSet dsXray = new DataSet(); m_dataAdapterXray.Fill(dsXray); DataTable dtXray = dsXray.Tables[0]; ReferenceXrayList = new List(); if (dtXray != null) { if (dtXray.Rows.Count > 0) { foreach (DataRow item in dtXray.Rows) { STDEditor.STDXray sXray = new STDEditor.STDXray(); sXray.StdID = item["Id"].ToString(); sXray.XrayData = (byte[])item["SPEC"]; ReferenceXrayList.Add(sXray); } } } 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, 9); 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(kv.Value.ChineseName, typeof(string)); PropGrid[i, 3] = new SourceGrid.Cells.Cell("", typeof(string)); SourceGrid.Cells.Views.Cell view = new SourceGrid.Cells.Views.Cell(); view.BackColor = shareFunction.string10ToColor(kv.Value.Color); PropGrid[i, 3].View = view; PropGrid[i, 4] = new SourceGrid.Cells.Cell(kv.Value.Hardness, typeof(string)); PropGrid[i, 5] = new SourceGrid.Cells.Cell(kv.Value.Density, typeof(string)); PropGrid[i, 6] = new SourceGrid.Cells.Cell(kv.Value.BSE, typeof(string)); PropGrid[i, 7] = new SourceGrid.Cells.Cell(kv.Value.Formula, typeof(string)); PropGrid[i, 8] = new SourceGrid.Cells.Cell(kv.Value.Element, typeof(string)); i++; } PropGrid.AutoSizeCells(); int iColumnWidth = 45; // 第一列的宽度 int width = this.Width; PropGrid.Columns.SetWidth(0, iColumnWidth); PropGrid.Columns.SetWidth(3, 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; m_SubMidWindow.m_STDEditor.ShowXrayAtlas(); } else { int r = PropGrid.Selection.ActivePosition.Row; SelReferenceXray((int)PropGrid.Rows[r].Tag, ReferenceDictionary[(int)PropGrid.Rows[r].Tag]); ShowReferenceContent(PropGrid.Rows[r].Tag.ToString()); //m_SubMidWindow.m_MainForm.m_Attributes.SetDatatoGrid_Attributes(ReferenceDictionary[(int)PropGrid.Rows[r].Tag]); 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(); } } } /// /// 根据选择节点显示对应的Xray信息 /// public void SelReferenceXray(int Key, STDdata sT) { if (sT == null) { return; } if (ReferenceXrayList != null) { //if (m_STDXrayList.Count > 0) //{ bool isExists = false; foreach (STDEditor.STDXray item in ReferenceXrayList) { //修改XrayData信息 if (item.StdID == Key.ToString()) { string selColor = sT.Color; int colorValue = Convert.ToInt32(selColor); //显示Xray信息 if (item.XrayData != null) { isExists = true; m_SubMidWindow.m_STDEditor.ShowXrayAtlas(colorValue, item.XrayData); } break; } } if (!isExists) { m_SubMidWindow.m_STDEditor.ShowXrayAtlas(); } //} } } 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; 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.Formula = ReferenceDictionary[STDnum].Formula; sT.Hardness = ReferenceDictionary[STDnum].Hardness; sT.Element = ReferenceDictionary[STDnum].Element; m_SubMidWindow.m_MainForm.STDDictionary.Add(STDId + 1, sT); m_SubMidWindow.m_MainForm.m_STDRuleslist.InsertNewRow(STDId + 1, sT.StrName, shareFunction.string10ToColor(sT.Color)); m_SubMidWindow.m_MainForm.m_STDRuleslist.Refresh(); m_SubMidWindow.m_MainForm.m_STDRuleslist.PreRow = i; for (int k = 0; k < ReferenceXrayList.Count; k++) { if (int.Parse(ReferenceXrayList[k].StdID) == STDnum) { STDEditor.STDXray tDXray = new STDEditor.STDXray(); tDXray.StdID = (STDId + 1).ToString(); tDXray.XrayData = ReferenceXrayList[k].XrayData; tDXray.InfoState = (int)STDEditor.STDXrayInfoState.Add; m_SubMidWindow.m_STDEditor.m_STDXrayList.Add(tDXray); } } } } else { AddIntoMainLab(); } } private void ShowReferenceContent(string stdID) { if (ReferenceXrayList != null) { for (int i = 0; i < ReferenceXrayList.Count; i++) { if (ReferenceXrayList[i].StdID == stdID) { //获取修改行的信息 ReferencexrayByteData = (byte[])ReferenceXrayList[i].XrayData; } } } } #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.Formula = ReferenceDictionary[STDnum].Formula; sT.Hardness = ReferenceDictionary[STDnum].Hardness; sT.ChineseName = ReferenceDictionary[STDnum].ChineseName; sT.Element = ReferenceDictionary[STDnum].Element; sT.XrayData = ReferenceDictionary[STDnum].XrayData; m_SubMidWindow.m_MainForm.STDDictionary.Add(STDId + 1, sT); m_SubMidWindow.m_MainForm.m_STDRuleslist.AddNewRow(STDId + 1, sT.StrName, shareFunction.string10ToColor(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; for (int k = 0; k < ReferenceXrayList.Count; k++) { if (int.Parse(ReferenceXrayList[k].StdID) == STDnum) { STDEditor.STDXray tDXray = new STDEditor.STDXray(); tDXray.StdID = (STDId + 1).ToString(); tDXray.XrayData = ReferenceXrayList[k].XrayData; tDXray.InfoState = (int)STDEditor.STDXrayInfoState.Add; m_SubMidWindow.m_STDEditor.m_STDXrayList.Add(tDXray); } } } } }