QSelObjManager.cpp
资源名称:QGIS.rar [点击查看]
上传用户:oybseng
上传日期:2015-04-27
资源大小:7831k
文件大小:8k
源码类别:
GDI/图象编程
开发平台:
Visual C++
- // QSelObjManager.cpp: implementation of the CQSelObjManager class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "..stdafx.h"
- #include "..includeResource.h"
- #include "..includeQSelObjManager.h"
- #include "..includeQBaseObj.h"
- #include "..includeQCoordSys.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CQSelObjManager::CQSelObjManager()
- {
- m_nSelLineNum = 0;
- m_nSelPointNum = 0;
- m_nSelLabelNum = 0;
- m_pCurObj = NULL;
- m_nFlashColorIndex = 0;
- m_crFlashColor[0] = RGB(255,0,0);
- m_crFlashColor[1] = RGB(0,0,255);
- }
- CQSelObjManager::~CQSelObjManager()
- {
- m_pCurObj = 0;
- }
- void CQSelObjManager::AddSelObj(CQBaseObj * pObj)
- {
- if(!pObj || pObj->GetObjSeleted()) return;
- //效率的优化
- int nObjcount = GetSelObjCount();
- int i=0;
- if(nObjcount>0)
- {
- POSITION pos = GetHeadPosition();
- while (pos)
- {
- CQBaseObj * ppObj = GetNext(pos);
- if(ppObj->GetObjID() == pObj->GetObjID())
- break;
- else
- ++i;
- }
- }
- if(i>=nObjcount)
- {
- pObj->SetObjSelected(TRUE);
- //这里要添加代码防止同一个对象被选中两次
- CObList::AddTail(pObj); // 从尾部添加对象
- //还没有添加其他对象 暂时只有点和线两个对象
- switch(pObj->GetObjType())
- {
- case QGIS_OBJ_POINT:
- m_nSelPointNum++;
- break;
- case QGIS_OBJ_LINE:
- m_nSelLineNum++;
- break;
- case QGIS_OBJ_LABEL:
- m_nSelLabelNum++;
- break;
- default:
- break;
- }
- m_pCurObj = pObj;
- }
- }
- void CQSelObjManager::RemoveSelObj(CQBaseObj * pObj)
- {
- if(!pObj)return;
- int nCount = GetSelObjCount(); //获取选中对象的数量
- for(int i=0;i<nCount;i++)
- {
- POSITION pos = FindIndex(i);
- CQBaseObj * p = (CQBaseObj *)GetAt(pos);
- if(p->GetObjMapID() == pObj->GetObjMapID() && p->GetObjID() == pObj->GetObjID())
- {
- CObList::RemoveAt(pos);
- pObj->SetObjSelected(FALSE);
- switch(pObj->GetObjType())
- {
- case QGIS_OBJ_POINT:
- m_nSelPointNum--;
- break;
- case QGIS_OBJ_LINE:
- m_nSelLineNum--;
- break;
- default:
- break;
- }
- break;
- }
- if(m_pCurObj && pObj->GetObjID() == m_pCurObj->GetObjID())
- {
- if(GetSelObjCount()>0)
- {
- m_pCurObj = GetHead();
- }
- else
- {
- m_pCurObj = NULL;
- }
- }
- }
- }
- inline int CQSelObjManager::GetSelObjCount()
- {
- return CObList::GetCount();
- }
- void CQSelObjManager::RemoveAllSelObj()
- {
- if(GetCount() == 0)return;
- POSITION pos = CObList::GetHeadPosition();
- while(pos)
- {
- //将所有选中图元的选中状态设置为假
- ((CQBaseObj *)CObList::GetNext(pos))->SetObjSelected(FALSE);
- }
- CObList::RemoveAll();
- m_pCurObj = NULL;
- m_nSelPointNum = 0;
- m_nSelLineNum = 0;
- }
- void CQSelObjManager::DisplaySelObjs(CQCoordSys * pSys,CDC * pDC)
- {
- POSITION pos = CObList::GetHeadPosition();
- while(pos)
- {
- CQBaseObj * pObj = (CQBaseObj *)CObList::GetNext(pos);
- if(pObj)
- {
- COLORREF cr = m_crFlashColor[m_nFlashColorIndex];
- pObj->Display(pSys,pDC,R2_COPYPEN,2,&cr);
- }
- }
- }
- void CQSelObjManager::ResetSelObj(CQBaseObj * pObj)
- {
- RemoveAllSelObj();
- m_pCurObj = 0;
- if(!pObj) return;
- AddSelObj(pObj);
- m_pCurObj = pObj;
- }
- void CQSelObjManager::SetCurSelObj(CQBaseObj * pObj)
- {
- if(!pObj)
- {
- m_pCurObj = NULL;
- return;
- }
- BOOL bOk = FALSE;
- POSITION pos = CObList::GetHeadPosition();
- while (pos)
- {
- CQBaseObj * p = (CQBaseObj *)CObList::GetNext(pos);
- if(p)
- {
- if(p->GetObjID() == pObj->GetObjID())
- {
- bOk = TRUE;
- m_pCurObj = pObj;
- return;
- }
- }
- }
- if(!bOk)
- {
- AddSelObj(pObj);
- m_pCurObj = pObj;
- }
- }
- CQBaseObj * CQSelObjManager::GetCurSelObj()
- {
- if(m_pCurObj)
- return m_pCurObj;
- return NULL;
- }
- int CQSelObjManager::FindSelObjIndex(CQBaseObj * pObj)
- {
- if(!pObj)return-1;
- int i = 0;
- POSITION pos = CObList::GetHeadPosition();
- while(pos)
- {
- CQBaseObj * p = (CQBaseObj *)CObList::GetNext(pos);
- if(p->GetObjID() == pObj->GetObjID())
- {
- return i;
- }
- i++;
- }
- return -1;
- }
- void CQSelObjManager::BeginFlash(CWnd *pWnd,UINT_PTR uEventID,UINT uTime)
- {
- if(pWnd) //假如当前窗口存在
- {
- pWnd->SetTimer(uEventID,uTime,NULL);
- }
- }
- void CQSelObjManager::FlashObj(CQCoordSys *pCoorSys,CWnd *pWnd)
- {
- CDC * pDC = pWnd->GetDC();
- if(pWnd)
- {
- DisplaySelObjs(pCoorSys,pDC);
- m_nFlashColorIndex = !m_nFlashColorIndex; // 换颜色
- }
- pWnd->ReleaseDC(pDC);
- }
- void CQSelObjManager::EndFlash(CWnd * pWnd,UINT_PTR uEventID)
- {
- if(pWnd)
- {
- pWnd->KillTimer(uEventID);
- ResetSelObj(NULL);
- }
- }
- void CQSelObjManager::StopFlash(CWnd * pWnd,UINT_PTR uEventID)
- {
- if(pWnd)
- {
- pWnd->KillTimer(uEventID);
- }
- }
- void CQSelObjManager::GetFlashColor(COLORREF cr[2])
- {
- memcpy(cr,m_crFlashColor,sizeof(COLORREF)*2); //内存拷贝
- }
- COLORREF CQSelObjManager::GetFlashColor(int nFlashColorIndex)
- {
- return m_crFlashColor[nFlashColorIndex];
- }
- COLORREF CQSelObjManager::GetFlashColor()
- {
- return m_crFlashColor[m_nFlashColorIndex];
- }
- void CQSelObjManager::SetFlashColor(COLORREF crArr[2])
- {
- m_crFlashColor[0] = crArr[0];
- m_crFlashColor[1] = crArr[1];
- }
- inline POSITION CQSelObjManager::GetHeadPosition()
- {
- return CObList::GetHeadPosition();
- }
- inline CQBaseObj * CQSelObjManager::GetNext(POSITION & pos)
- {
- return (CQBaseObj *)CObList::GetNext(pos);
- }
- inline POSITION CQSelObjManager::FindIndex(int lIndex)
- {
- return CObList::FindIndex(lIndex);
- }
- inline CQBaseObj * CQSelObjManager::GetAt(POSITION & pos)
- {
- return (CQBaseObj *)CObList::GetAt(pos);
- }
- inline CQBaseObj * CQSelObjManager::GetHead()
- {
- return (CQBaseObj *)CObList::GetHead();
- }
- inline CQBaseObj * CQSelObjManager::GetTail()
- {
- return (CQBaseObj *)CObList::GetTail();
- }
- inline void CQSelObjManager::RemoveAt(POSITION & pos)
- {
- CQBaseObj * pObj = GetAt(pos);
- if(!pObj) return;
- switch(pObj->GetObjType())
- {
- case QGIS_OBJ_POINT:
- m_nSelPointNum--;
- break;
- case QGIS_OBJ_LINE:
- m_nSelLineNum--;
- break;
- default:
- break;
- }
- if(m_pCurObj && m_pCurObj->GetObjID() == pObj->GetObjID())
- {
- if(GetSelObjCount()>0)
- {
- m_pCurObj = GetHead();
- }
- else
- {
- m_pCurObj = 0;
- }
- }
- CObList::RemoveAt(pos);
- }
- inline int CQSelObjManager::GetCount() const
- {
- return CObList::GetCount();
- }
- inline void CQSelObjManager::Copy(CObList * pList)
- {
- if(!pList)return;
- CObList::RemoveAll();
- CObList::AddTail(pList);
- if(GetSelObjCount()>0)
- {
- m_pCurObj = GetHead();
- }
- else
- {
- m_pCurObj = 0;
- }
- POSITION pos = GetHeadPosition();
- while (pos)
- {
- CQBaseObj * pObj = GetNext(pos);
- if(!pObj)return;
- pObj->SetObjSelected(TRUE);
- switch(pObj->GetObjType())
- {
- case QGIS_OBJ_POINT:
- m_nSelPointNum++;
- break;
- case QGIS_OBJ_LINE:
- m_nSelLineNum++;
- break;
- default:
- break;
- }
- }
- }
- void CQSelObjManager::GetBoundingRect(CBoundaryRect & bRect)
- {
- if(GetCount()<=0)return;
- CBoundaryRect rectTemp,rcResult;
- GetHead()->GetBoundingRect(&rectTemp);
- rcResult.Union(&rectTemp);
- POSITION pos = GetHeadPosition();
- while (pos)
- {
- CQBaseObj * pObj = GetNext(pos);
- if(!pObj)return;
- pObj->GetBoundingRect(&rectTemp);
- rcResult.Union(&rectTemp);
- }
- bRect = rcResult;
- }
- void CQSelObjManager::GetScreenRect(CQCoordSys *pCoorSys,CRect &rect)
- {
- CBoundaryRect bRect;
- GetBoundingRect(bRect);
- pCoorSys->WPtoLP(bRect,&rect);
- rect.left -= 2;
- rect.top -= 2;
- rect.right += 2;
- rect.bottom += 2;
- }
- void CQSelObjManager::BeginFlash(HWND pWnd,UINT_PTR uEventID,UINT uTime)
- {
- if(pWnd)
- ::SetTimer(pWnd,uEventID,uTime,NULL);
- }
- void CQSelObjManager::FlashObj(CQCoordSys *pCoorSys,HWND pWnd)
- {
- if(pWnd)
- {
- CDC * pDC = new CDC();
- HDC hDC = ::GetDC(pWnd);
- pDC->Attach(hDC);
- DisplaySelObjs(pCoorSys,pDC);
- hDC = pDC->Detach();
- ::ReleaseDC(pWnd,hDC);
- delete pDC;
- m_nFlashColorIndex=!m_nFlashColorIndex;
- }
- }
- //结束图元的闪烁显示
- void CQSelObjManager::EndFlash(HWND pWnd,UINT_PTR uEventID)
- {
- if(pWnd)
- {
- ::KillTimer(pWnd,uEventID);
- ResetSelObj(NULL);
- }
- }