QSelObjManager.cpp
上传用户:oybseng
上传日期:2015-04-27
资源大小:7831k
文件大小:8k
源码类别:

GDI/图象编程

开发平台:

Visual C++

  1. // QSelObjManager.cpp: implementation of the CQSelObjManager class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "..stdafx.h"
  5. #include "..includeResource.h"
  6. #include "..includeQSelObjManager.h"
  7. #include "..includeQBaseObj.h"
  8. #include "..includeQCoordSys.h"
  9. #ifdef _DEBUG
  10. #undef THIS_FILE
  11. static char THIS_FILE[]=__FILE__;
  12. #define new DEBUG_NEW
  13. #endif
  14. //////////////////////////////////////////////////////////////////////
  15. // Construction/Destruction
  16. //////////////////////////////////////////////////////////////////////
  17. CQSelObjManager::CQSelObjManager()
  18. {
  19. m_nSelLineNum = 0;
  20. m_nSelPointNum  = 0;
  21. m_nSelLabelNum = 0;
  22. m_pCurObj = NULL;
  23. m_nFlashColorIndex = 0;
  24. m_crFlashColor[0] = RGB(255,0,0);
  25. m_crFlashColor[1] = RGB(0,0,255);
  26. }
  27. CQSelObjManager::~CQSelObjManager()
  28. {
  29. m_pCurObj = 0;
  30. }
  31. void CQSelObjManager::AddSelObj(CQBaseObj * pObj)
  32. {
  33. if(!pObj || pObj->GetObjSeleted()) return;
  34. //效率的优化
  35. int nObjcount = GetSelObjCount();
  36. int i=0;
  37. if(nObjcount>0)
  38. {
  39. POSITION pos = GetHeadPosition();
  40. while (pos)
  41. {
  42. CQBaseObj * ppObj = GetNext(pos);
  43. if(ppObj->GetObjID() == pObj->GetObjID())
  44. break;
  45. else
  46. ++i;
  47. }
  48. }
  49. if(i>=nObjcount)
  50. {
  51. pObj->SetObjSelected(TRUE);
  52. //这里要添加代码防止同一个对象被选中两次
  53. CObList::AddTail(pObj); // 从尾部添加对象
  54. //还没有添加其他对象 暂时只有点和线两个对象
  55. switch(pObj->GetObjType())
  56. {
  57. case QGIS_OBJ_POINT:
  58. m_nSelPointNum++;
  59. break;
  60. case QGIS_OBJ_LINE:
  61. m_nSelLineNum++;
  62. break;
  63. case QGIS_OBJ_LABEL:
  64. m_nSelLabelNum++;
  65. break;
  66. default:
  67. break;
  68. }
  69. m_pCurObj = pObj;
  70. }
  71. }
  72. void CQSelObjManager::RemoveSelObj(CQBaseObj * pObj)
  73. {
  74. if(!pObj)return;
  75. int nCount = GetSelObjCount(); //获取选中对象的数量
  76. for(int i=0;i<nCount;i++)
  77. {
  78. POSITION pos = FindIndex(i);
  79. CQBaseObj * p = (CQBaseObj *)GetAt(pos);
  80. if(p->GetObjMapID() == pObj->GetObjMapID() && p->GetObjID() == pObj->GetObjID())
  81. {
  82. CObList::RemoveAt(pos);
  83. pObj->SetObjSelected(FALSE);
  84. switch(pObj->GetObjType())
  85. {
  86. case QGIS_OBJ_POINT:
  87. m_nSelPointNum--;
  88. break;
  89. case QGIS_OBJ_LINE:
  90. m_nSelLineNum--;
  91.     break;
  92. default:
  93.     break;
  94. }
  95. break;
  96. }
  97. if(m_pCurObj && pObj->GetObjID() == m_pCurObj->GetObjID())
  98. {
  99. if(GetSelObjCount()>0)
  100. {
  101. m_pCurObj = GetHead();
  102. }
  103. else
  104. {
  105. m_pCurObj = NULL;
  106. }
  107. }
  108. }
  109. }
  110. inline int CQSelObjManager::GetSelObjCount()
  111. {
  112. return CObList::GetCount();
  113. }
  114. void CQSelObjManager::RemoveAllSelObj()
  115. {
  116. if(GetCount() == 0)return;
  117. POSITION pos = CObList::GetHeadPosition();
  118. while(pos)
  119. {
  120. //将所有选中图元的选中状态设置为假
  121. ((CQBaseObj *)CObList::GetNext(pos))->SetObjSelected(FALSE);
  122. }
  123. CObList::RemoveAll();
  124. m_pCurObj = NULL;
  125. m_nSelPointNum = 0;
  126. m_nSelLineNum = 0;
  127. }
  128. void CQSelObjManager::DisplaySelObjs(CQCoordSys * pSys,CDC * pDC)
  129. {
  130. POSITION pos = CObList::GetHeadPosition();
  131. while(pos)
  132. {
  133. CQBaseObj * pObj = (CQBaseObj *)CObList::GetNext(pos);
  134. if(pObj)
  135. {
  136. COLORREF cr = m_crFlashColor[m_nFlashColorIndex];
  137. pObj->Display(pSys,pDC,R2_COPYPEN,2,&cr);
  138. }
  139. }
  140. }
  141. void CQSelObjManager::ResetSelObj(CQBaseObj * pObj)
  142. {
  143. RemoveAllSelObj();
  144. m_pCurObj = 0;
  145. if(!pObj) return;
  146. AddSelObj(pObj);
  147. m_pCurObj = pObj;
  148. }
  149. void CQSelObjManager::SetCurSelObj(CQBaseObj * pObj)
  150. {
  151. if(!pObj)
  152. {
  153. m_pCurObj = NULL;
  154. return;
  155. }
  156. BOOL bOk = FALSE;
  157. POSITION pos = CObList::GetHeadPosition();
  158. while (pos)
  159. {
  160. CQBaseObj * p = (CQBaseObj *)CObList::GetNext(pos);
  161. if(p)
  162. {
  163. if(p->GetObjID() == pObj->GetObjID())
  164. {
  165. bOk = TRUE;
  166. m_pCurObj = pObj;
  167. return;
  168. }
  169. }
  170. }
  171. if(!bOk)
  172. {
  173. AddSelObj(pObj);
  174. m_pCurObj = pObj;
  175. }
  176. }
  177. CQBaseObj * CQSelObjManager::GetCurSelObj()
  178. {
  179. if(m_pCurObj)
  180. return m_pCurObj;
  181. return NULL;
  182. }
  183. int CQSelObjManager::FindSelObjIndex(CQBaseObj * pObj)
  184. {
  185. if(!pObj)return-1;
  186. int i = 0;
  187. POSITION pos = CObList::GetHeadPosition();
  188. while(pos)
  189. {
  190. CQBaseObj * p = (CQBaseObj *)CObList::GetNext(pos);
  191. if(p->GetObjID() == pObj->GetObjID())
  192. {
  193. return i;
  194. }
  195. i++;
  196. }
  197. return -1;
  198. }
  199. void CQSelObjManager::BeginFlash(CWnd *pWnd,UINT_PTR uEventID,UINT uTime)
  200. {
  201. if(pWnd) //假如当前窗口存在
  202. {
  203. pWnd->SetTimer(uEventID,uTime,NULL);
  204. }
  205. }
  206. void CQSelObjManager::FlashObj(CQCoordSys *pCoorSys,CWnd *pWnd)
  207. {
  208. CDC * pDC = pWnd->GetDC();
  209. if(pWnd)
  210. {
  211. DisplaySelObjs(pCoorSys,pDC);
  212. m_nFlashColorIndex = !m_nFlashColorIndex; // 换颜色
  213. }
  214. pWnd->ReleaseDC(pDC);
  215. }
  216. void CQSelObjManager::EndFlash(CWnd * pWnd,UINT_PTR uEventID)
  217. {
  218. if(pWnd)
  219. {
  220. pWnd->KillTimer(uEventID);
  221. ResetSelObj(NULL);
  222. }
  223. }
  224. void CQSelObjManager::StopFlash(CWnd * pWnd,UINT_PTR uEventID)
  225. {
  226. if(pWnd)
  227. {
  228. pWnd->KillTimer(uEventID);
  229. }
  230. }
  231. void CQSelObjManager::GetFlashColor(COLORREF cr[2])
  232. {
  233. memcpy(cr,m_crFlashColor,sizeof(COLORREF)*2);  //内存拷贝
  234. }
  235. COLORREF CQSelObjManager::GetFlashColor(int nFlashColorIndex)
  236. {
  237. return m_crFlashColor[nFlashColorIndex];
  238. }
  239. COLORREF CQSelObjManager::GetFlashColor()
  240. {
  241. return m_crFlashColor[m_nFlashColorIndex]; 
  242. }
  243. void CQSelObjManager::SetFlashColor(COLORREF crArr[2])
  244. {
  245. m_crFlashColor[0] = crArr[0];
  246. m_crFlashColor[1] = crArr[1];
  247. }
  248. inline POSITION CQSelObjManager::GetHeadPosition()
  249. {
  250. return CObList::GetHeadPosition();
  251. }
  252. inline CQBaseObj * CQSelObjManager::GetNext(POSITION & pos)
  253. {
  254. return (CQBaseObj *)CObList::GetNext(pos);
  255. }
  256. inline POSITION CQSelObjManager::FindIndex(int lIndex)
  257. {
  258. return CObList::FindIndex(lIndex);
  259. }
  260. inline CQBaseObj * CQSelObjManager::GetAt(POSITION & pos)
  261. {
  262. return (CQBaseObj *)CObList::GetAt(pos);
  263. }
  264. inline CQBaseObj * CQSelObjManager::GetHead()
  265. {
  266. return (CQBaseObj *)CObList::GetHead();
  267. }
  268. inline CQBaseObj * CQSelObjManager::GetTail()
  269. {
  270. return (CQBaseObj *)CObList::GetTail();
  271. }
  272. inline void CQSelObjManager::RemoveAt(POSITION & pos)
  273. {
  274. CQBaseObj * pObj = GetAt(pos);
  275. if(!pObj) return;
  276. switch(pObj->GetObjType())
  277. {
  278. case QGIS_OBJ_POINT:
  279. m_nSelPointNum--;
  280. break;
  281. case QGIS_OBJ_LINE:
  282. m_nSelLineNum--;
  283.     break;
  284. default:
  285.     break;
  286. }
  287. if(m_pCurObj && m_pCurObj->GetObjID() == pObj->GetObjID())
  288. {
  289. if(GetSelObjCount()>0)
  290. {
  291. m_pCurObj = GetHead();
  292. }
  293. else
  294. {
  295. m_pCurObj = 0;
  296. }
  297. }
  298. CObList::RemoveAt(pos);
  299. }
  300. inline int CQSelObjManager::GetCount() const
  301. {
  302. return CObList::GetCount();
  303. }
  304. inline void CQSelObjManager::Copy(CObList * pList)
  305. {
  306. if(!pList)return;
  307. CObList::RemoveAll();
  308. CObList::AddTail(pList);
  309. if(GetSelObjCount()>0)
  310. {
  311. m_pCurObj = GetHead();
  312. }
  313. else
  314. {
  315. m_pCurObj = 0;
  316. }
  317. POSITION pos = GetHeadPosition();
  318. while (pos)
  319. {
  320. CQBaseObj * pObj = GetNext(pos);
  321. if(!pObj)return;
  322. pObj->SetObjSelected(TRUE);
  323. switch(pObj->GetObjType())
  324. {
  325. case QGIS_OBJ_POINT:
  326. m_nSelPointNum++;
  327. break;
  328. case QGIS_OBJ_LINE:
  329. m_nSelLineNum++;
  330.     break;
  331. default:
  332.     break;
  333. }
  334. }
  335. }
  336. void CQSelObjManager::GetBoundingRect(CBoundaryRect & bRect)
  337. {
  338. if(GetCount()<=0)return;
  339. CBoundaryRect rectTemp,rcResult;
  340. GetHead()->GetBoundingRect(&rectTemp);
  341. rcResult.Union(&rectTemp);
  342. POSITION pos = GetHeadPosition();
  343. while (pos)
  344. {
  345. CQBaseObj * pObj = GetNext(pos);
  346. if(!pObj)return;
  347. pObj->GetBoundingRect(&rectTemp);
  348. rcResult.Union(&rectTemp);
  349. }
  350. bRect = rcResult;
  351. }
  352. void CQSelObjManager::GetScreenRect(CQCoordSys *pCoorSys,CRect &rect)
  353. {
  354. CBoundaryRect bRect;
  355. GetBoundingRect(bRect);
  356. pCoorSys->WPtoLP(bRect,&rect);
  357. rect.left -= 2;
  358. rect.top -= 2;
  359. rect.right += 2;
  360. rect.bottom += 2;
  361. }
  362. void CQSelObjManager::BeginFlash(HWND pWnd,UINT_PTR uEventID,UINT uTime)
  363. {
  364. if(pWnd)
  365. ::SetTimer(pWnd,uEventID,uTime,NULL);
  366. }
  367. void CQSelObjManager::FlashObj(CQCoordSys *pCoorSys,HWND pWnd)
  368. {
  369. if(pWnd)
  370. {
  371. CDC * pDC = new CDC();
  372. HDC hDC = ::GetDC(pWnd);
  373. pDC->Attach(hDC);
  374. DisplaySelObjs(pCoorSys,pDC);
  375. hDC = pDC->Detach();
  376. ::ReleaseDC(pWnd,hDC);
  377. delete pDC;
  378. m_nFlashColorIndex=!m_nFlashColorIndex;
  379. }
  380. }
  381. //结束图元的闪烁显示
  382. void CQSelObjManager::EndFlash(HWND pWnd,UINT_PTR uEventID)
  383. {
  384. if(pWnd)
  385. {
  386. ::KillTimer(pWnd,uEventID);
  387. ResetSelObj(NULL);
  388. }
  389. }