#include "..\stdafx.h" #include "coretools.h" #include "WSysTreeGrid.h" #include "THVacCfgOperation.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif #define SYSTEM_KEY -2 // 所属系统 CWSysTreeGrid::CWSysTreeGrid():CBaseTreeGrid(true) { //创建列表表头 AppendCol(_T("管线系统"), 120); AppendCol(_T("颜色"), 40); GetCol(1).SetRightButton(true); AppendCol(_T("线宽"), 40); AppendCol(_T("线型"), 150); GetCol(3).SetComboSel(true); AppendCol(_T("图层名称"), 120); AppendCol(_T("标注代号"), 120); AppendCol(_T("图层代号"), 1); SetTreeLine(); //禁止一些危险操作 SetNoAppendButton(true); SetNoDefaultMenu(true); SetRowPosFix(true); SetColFixLength(TRUE); } CWSysTreeGrid::~CWSysTreeGrid() { } // 创建新的行 BOOL CWSysTreeGrid::InsertNewTreeRow(CUIIValueTable &vTable, const Ttable& arFieldName, LPCTSTR pszParent) { // 创建根节点(除第一个单元格可编辑外,其它的单元格都不可编辑) WDGridTreeRow* pParentRow = new WDGridTreeRow(*this); (*pParentRow)[0].SetText(pszParent); m_lstpParents.AddTail(pParentRow); // 遍历临时数据库表中的记录,作为表格的子行插入到表格 if (!InsertChildTreeRow(pParentRow, vTable, arFieldName)) { pParentRow->ClearChild(); DELETE_PTR(pParentRow); return FALSE; } AppendRow(pParentRow); return TRUE; } BOOL CWSysTreeGrid::InsertNewTreeRow(CUIIValueTable& vTable, const CStringArray& arFieldName, LPCTSTR pszParName) { // 父节点表行 WDGridTreeRow* pParentRow = new WDGridTreeRow(*this); (*pParentRow)[0].SetText(pszParName); m_lstpParents.AddTail(pParentRow); // 遍历临时数据库表中的记录,作为表格的孩子行插入到表格 if (!InsertChildTreeRow(pParentRow, vTable, arFieldName)) { // 删除新建的行 pParentRow->ClearChild(); DELETE_PTR(pParentRow); return FALSE; } AppendRow(pParentRow); return TRUE; } BOOL CWSysTreeGrid::InsertChildTreeRow(WDGridTreeRow* pParentRow, CUIIValueTable &vTable, const Ttable& arFieldName) { CString strFieldName; CStringArray arRecord; WDGridTreeRow * pChildRow = NULL; int iLen = vTable.GetTableLogicSize(); INT_PTR nFiledLen(0); for (int i = 0; i < iLen; i++) { // 创建表格行 pChildRow = new WDGridTreeRow(*this); if (NULL == pChildRow) continue; // 获取到表记录中的数据 vTable.GetValueRecord(i, arRecord); // 遍历数据库表头,找到表格对应的列序号,把数据写入 nFiledLen = arFieldName.GetSize(); for (int k = 0; k < nFiledLen; k++) { strFieldName = arFieldName.GetAt(k).m_sFieldName; int iIndexCol = -3; GetGridIndex(strFieldName, iIndexCol); if (SYSTEM_KEY != iIndexCol && -3 != iIndexCol) { // 排除所属系统 arRecord.GetAt(k); (*pChildRow)[iIndexCol].SetText(arRecord.GetAt(k)); SetCellGrade((*pChildRow)[iIndexCol]); } } pParentRow->AppendChild(pChildRow); } SetCellGrade((*pParentRow)[0]); return TRUE; } BOOL CWSysTreeGrid::InsertChildTreeRow(WDGridTreeRow* pParentRow, CUIIValueTable& vTable, const CStringArray& arFieldName) { // 遍历临时数据库表中的记录,作为表格的孩子行插入到表格 CString strFieldName; CStringArray arRecord; WDGridTreeRow * pChildRow = NULL; for (int i = 0; i < vTable.GetTableLogicSize(); i++) { // 创建表格行 pChildRow = new WDGridTreeRow(*this); if (NULL == pChildRow) continue; // 获取到表记录中的数据 vTable.GetValueRecord(i, arRecord); // 遍历数据库表头,找到表格对应的列序号,把数据写入 for (int k = 0; k < arFieldName.GetSize(); k++) { strFieldName = arFieldName.GetAt(k); int iIndexCol = -3; GetGridIndex(strFieldName, iIndexCol); if (SYSTEM_KEY != iIndexCol || -3 != iIndexCol) { // 排除所属系统 (*pChildRow)[iIndexCol].SetText(arRecord.GetAt(k)); SetCellGrade((*pChildRow)[iIndexCol]); } } pParentRow->AppendChild(pChildRow); } SetCellGrade((*pParentRow)[0]); return TRUE; } void CWSysTreeGrid::OnInsertRow(WDGridRow &row) { // 映射数据库中的子段 Ttable arFieldName; if (!THVacCfgOperation::MapDBKeyToGrid(m_arField, arFieldName)) return; // 表格中加入对应的项 WDGridTreeRow* pParentRow = (WDGridTreeRow*)&row; if (NULL == pParentRow) return; // 判断系统名称是否存在 CString strNewSys = _T("新系统"); strNewSys = GetNewSysName(strNewSys); (*pParentRow)[0].SetText(strNewSys); m_lstpParents.AddTail(pParentRow); // 遍历临时数据库表中的记录,作为表格的孩子行插入到表格 CString strFieldName; CStringArray arRecord; WDGridTreeRow * pChildRow = NULL; int iLen = m_tabModel.GetTableLogicSize(); INT_PTR nFiledLen(0); for (int i = 0; i < iLen; i++) { // 创建表格行 pChildRow = new WDGridTreeRow(*this); if (NULL == pChildRow) continue; // 获取到表记录中的数据 m_tabModel.GetValueRecord(i, arRecord); // 遍历数据库表头,找到表格对应的列序号,把数据写入 nFiledLen = arFieldName.GetSize(); for (int k = 0; k < nFiledLen; k++) { strFieldName = arFieldName.GetAt(k).m_sFieldName; int iIndexCol = -3; GetGridIndex(strFieldName, iIndexCol); if (SYSTEM_KEY != iIndexCol && -3 != iIndexCol) { // 排除所属系统 (*pChildRow)[iIndexCol].SetText(arRecord.GetAt(k)); SetCellGrade((*pChildRow)[iIndexCol]); } } pParentRow->AppendChild(pChildRow); } SetCellGrade((*pParentRow)[0]); } bool CWSysTreeGrid::OnBeforeDeleteRow() { return WDGrid::OnBeforeDeleteRow(); } void CWSysTreeGrid::OnEndDeleteRow() { WDGrid::OnEndDeleteRow(); } bool CWSysTreeGrid::OnMoveRow( long i ) { return false; } void CWSysTreeGrid::OnEndMoveRow() { WDGrid::OnEndMoveRow(); } // 刷新单元格 bool CWSysTreeGrid::OnRefresh(WDGridCell& wdCell) { if (1 == wdCell.GetColIndex() && !wdCell.GetRow().IsParentRow()) { CString strText = (LPCTSTR)wdCell.GetText(); if (0 != strText.CompareNoCase(_T(""))) { CRect rect; wdCell.GetTextRect(rect); rect.DeflateRect(2, 2); CDC& dc = GetDC(); int iSave = dc.SaveDC(); // 填充客户区 自绘区域,再写入文字 int r,g,b; CString sText = (LPCTSTR)wdCell.GetText(); CoreTools::ColorToRGB(sText, &r, &g, &b); dc.FillSolidRect(&rect, RGB(r, g, b)); dc.DrawText(sText, rect, DT_VCENTER|DT_SINGLELINE); dc.RestoreDC(iSave); pDC->ExcludeClipRect(rect); } return true; } WDGrid::OnRefresh(wdCell); return true; } // 单元格combox加载数据 void CWSysTreeGrid::OnCellComboString(WDGridCell &wdCell, CComboBox &combo) { if (3 == wdCell.GetColIndex()) { // 线型 // 把获取的数据加载到Combo CString strText; CStringArray arLine; for (int i = 0; i < m_tabLineType.GetTableLogicSize(); i++) { m_tabLineType.GetValueRecord(i, arLine); strText.Format(_T("%s:%s"), arLine.GetAt(0), arLine.GetAt(1)); combo.AddString(strText); } } } WCellCtrl* CWSysTreeGrid::OnGetCellEdit(WDGridCell& cell) { return CBaseTreeGrid::OnGetCellEdit(cell); } bool CWSysTreeGrid::OnModified(WDGridCell& cell, CString& text) { WDGridCol &Col = cell.GetCol(); WDGridRow &row = cell.GetRow(); if (Col.GetIndex() == 0) { if (!(row.IsTreeRow() && row.IsParentRow())) return false; } CString csOrigSys = (LPCTSTR)cell.GetText(); cell.SetText(text); cell.GetRow().Refresh(); // 更新图层名称 if (cell.GetColIndex() != 0) return true; WDGridTreeRow* pTreeRow = (WDGridTreeRow*)&row; WDGridTreeRow* pChild = NULL; CString csDel = _T("系统"), csOrigLayer, csNewSys(text); int nFind = csNewSys.Find(csDel, 0); if (nFind > -1) csNewSys = csNewSys.Left(nFind); nFind = csOrigSys.Find(csDel, 0); if (nFind > -1) csOrigSys = csOrigSys.Left(nFind); for (pTreeRow->MoveToFirst(); !pTreeRow->IsOut(); pTreeRow->MoveToNext()) { pChild = pTreeRow->CurChild(); if (pChild == NULL) continue; csOrigLayer = (*pChild)[4].GetText(); if (csOrigLayer.Find(csOrigSys, 0) > -1) { csOrigLayer.Replace(csOrigSys, csNewSys); (*pChild)[4].SetText(csOrigLayer); } } Refresh(); return true; } void CWSysTreeGrid::OnCmbSelchange(WDGridCell& cell, CComboBox* pCmb) { long lRow = cell.GetRowIndex(); long lCol = cell.GetColIndex(); if (NULL == pCmb) return; CString strOld = (LPCTSTR)cell.GetText(); CString strText; int iIndex = pCmb->GetCurSel(); pCmb->GetLBText(iIndex, strText); // 查询指定字符串的序号,并指定显示到编辑框 iIndex = pCmb->FindString(0, strText); pCmb->SetCurSel(iIndex); pCmb->UpdateData(); // 刷新控件单元格儿显示 2009-2-24 ywb GetCell(lRow, lCol).SetText(strText); GetCell(lRow, lCol).Refresh(); } // 根据数据库中的关键字,找到对应的表格列序号;SYSTEM_KEY 所属系统,LAYCODE_KEY 图层代号 BOOL CWSysTreeGrid::GetGridIndex(LPCTSTR pszDBKey, int &iIndexCol) { if (NULL == pszDBKey) return FALSE; // 所属系统 CString strText = _T("所属系统"); if (0 == strText.CompareNoCase(pszDBKey)) { iIndexCol = SYSTEM_KEY; return TRUE; } CString strColName; for (int i = 0; i < GetColNum(); i++) { strColName = GetCol(i).GetName(); if (0 == strColName.CompareNoCase(pszDBKey)) { iIndexCol = i; return TRUE; } } return FALSE; } // 设置单元个编辑的权限 BOOL CWSysTreeGrid::SetCellGrade(WDGridCell &wdCell) { // 设置根行单元格的权限(除第1个单元格可编辑,其它都不可编辑) if (wdCell.GetRow().IsParentRow()) { for (int i = 1; i < wdCell.GetGrid().GetColNum(); i++) wdCell.GetRow()[i].SetReadOnly(true); } return TRUE; } CString CWSysTreeGrid::GetNewSysName(LPCTSTR pszSys) { CString strNewSys = pszSys; CString strSys = pszSys; BOOL bContinue = TRUE; while (bContinue) { bContinue = FALSE; CString strExitSys; for (long i = 0; i < GetRowNum(); i++) { if (GetRow(i).IsParentRow()) strExitSys = GetRow(i)[0].GetText(); int iCount = 0; if (0 == strExitSys.CompareNoCase(strNewSys)) { int iLIndex = strExitSys.Find(_T("(")); int iRIndex = strExitSys.Find(_T(")")); if (-1 != iLIndex && -1 != iRIndex) { strSys = strSys.Left(iLIndex + 1); CString strCount = strExitSys.Mid(iLIndex + 1, iRIndex - iLIndex - 1); iCount = _ttoi(strCount); } iCount += 1; strNewSys.Format(_T("%s(%d)"), strSys, iCount); bContinue = TRUE; } } } return strNewSys; } // 防治误删加增加操作 BOOL CWSysTreeGrid::PreTranslateMessage( MSG* pMsg ) { if(pMsg->message == WM_KEYDOWN) { int nVirtKey = (int)pMsg->wParam; // virtual-key code if (nVirtKey == VK_ESCAPE || nVirtKey == VK_TAB) return CBaseTreeGrid::PreTranslateMessage(pMsg); if (GetCurColIndex() < 0) return TRUE; } return CBaseTreeGrid::PreTranslateMessage(pMsg); } bool CWSysTreeGrid::OnCellButtonDown( WDGridCell& cell, CString& res ) { if (cell.GetColIndex() == 1) { cell.Select(); int iRetColorIdx(0); int iCurLayerColor(0); if (Adesk::kTrue == acedSetColorDialog(iRetColorIdx, TRUE, iCurLayerColor)) { CString csCellBKColorIdx; csCellBKColorIdx.Format(_T("%d"), iRetColorIdx); res = csCellBKColorIdx; // 直接改cell文字 AutoCloseCell(100); return true; } } return false; }