QLayerObj.cpp
资源名称:QGIS.rar [点击查看]
上传用户:oybseng
上传日期:2015-04-27
资源大小:7831k
文件大小:17k
源码类别:
GDI/图象编程
开发平台:
Visual C++
- // QLayerObj.cpp: implementation of the CQLayerObj class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "..stdafx.h"
- #include "..includeResource.h"
- #include "..includeQLayerObj.h"
- #include "..includeQPointObj.h"
- #include "..includeQLineObj.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
- //////////////////////////////////////////////////////////////////////
- CQLayerObj::CQLayerObj()
- {
- m_lLayerID = SetLayerIDByRand();
- m_lMapID = 0;
- m_bShowed = TRUE;
- m_bDeleted = FALSE;
- m_bCanEdited = TRUE;
- m_szLayerName.Format("图层%ld",m_lLayerID);
- m_szLayerDis = m_szLayerName;
- InitLayerBoundary();
- }
- CQLayerObj::CQLayerObj(CQLayerObj & Layer)
- {
- m_lLayerID = Layer.GetLayerID();
- m_lMapID = Layer.GetMapID();
- m_bShowed = Layer.GetShowFlag();
- m_bDeleted = Layer.GetDeleteFlag();
- m_bCanEdited = Layer.GetEditFlag();
- m_szLayerName = Layer.GetLayerName();
- m_szLayerDis = Layer.GetLayerDis();
- m_Boundary.Copy(*(Layer.GetBoundary()));
- int nObjCount = Layer.GetObjCount();
- CQBaseObj * pObj = NULL;
- CQBaseObj * pTemp = NULL;
- for(int j=0;j<nObjCount;j++)
- {
- pObj = Layer.GetObj(j);
- switch(pObj->GetObjType())
- {
- case 1:
- {
- pTemp = new CQPointObj;
- break;
- }
- case 2:
- {
- pTemp = new CQLineObj;
- break;
- }
- case 3:
- {
- break;
- }
- case 4:
- {
- break;
- }
- case 5:
- {
- break;
- }
- default:
- {
- pTemp = NULL;
- break;
- }
- }
- if(pTemp)
- {
- pTemp->Copy(*pObj);
- AddObject(pTemp);
- }
- }
- }
- void CQLayerObj::Copy(CQLayerObj & Layer)
- {
- m_lLayerID = Layer.GetLayerID();
- m_lMapID = Layer.GetMapID();
- m_bShowed = Layer.GetShowFlag();
- m_bDeleted = Layer.GetDeleteFlag();
- m_bCanEdited = Layer.GetEditFlag();
- m_szLayerName = Layer.GetLayerName();
- m_szLayerDis = Layer.GetLayerDis();
- m_Boundary.Copy(*(Layer.GetBoundary()));
- int nObjCount = Layer.GetObjCount();
- CQBaseObj * pObj = NULL;
- CQBaseObj * pTemp = NULL;
- for(int j=0;j<nObjCount;j++)
- {
- pObj = Layer.GetObj(j);
- switch(pObj->GetObjType())
- {
- case 1:
- {
- pTemp = new CQPointObj;
- break;
- }
- case 2:
- {
- pTemp = new CQLineObj;
- break;
- }
- case 3:
- {
- break;
- }
- case 4:
- {
- break;
- }
- case 5:
- {
- break;
- }
- default:
- {
- pTemp = NULL;
- break;
- }
- }
- if(pTemp)
- {
- pTemp->Copy(*pObj);
- AddObject(pTemp);
- }
- }
- }
- CQLayerObj::~CQLayerObj()
- {
- }
- long CQLayerObj::SetLayerIDByRand()
- {
- long lLayerID = 0;
- srand( (unsigned)time( NULL ) );
- while(1)
- {
- int g = rand()%10; //个位
- int s = rand()%10; //十位
- int b = rand()%10; //百位
- int q = rand()%10; //万位
- int m = rand()%10; //十万位
- int n = rand()%10; //百万位
- lLayerID = n*100000+m*10000+q*1000+b*100+s*10+g;
- if(lLayerID != m_lLayerID)
- break;
- }
- return lLayerID;
- }
- void CQLayerObj::SetLayerID(long lLayerID)
- {
- if(lLayerID == m_lLayerID)
- {
- m_lLayerID = SetLayerIDByRand();
- }
- m_lLayerID = lLayerID;
- }
- long CQLayerObj::GetLayerID()
- {
- return m_lLayerID;
- }
- void CQLayerObj::SetMapID(long lMapID)
- {
- m_lMapID = lMapID;
- }
- long CQLayerObj::GetMapID()
- {
- return m_lMapID;
- }
- void CQLayerObj::SetEditFlag(BOOL bCanEdit)
- {
- m_bCanEdited = bCanEdit;
- }
- BOOL CQLayerObj::GetEditFlag()
- {
- return m_bCanEdited;
- }
- void CQLayerObj::SetDeleteFlag(BOOL bDeleted)
- {
- m_bDeleted = bDeleted;
- }
- BOOL CQLayerObj::GetDeleteFlag()
- {
- return m_bDeleted;
- }
- void CQLayerObj::SetLayerDis(CString szLayerDis)
- {
- m_szLayerDis = szLayerDis;
- }
- CString CQLayerObj::GetLayerDis()
- {
- return m_szLayerDis;
- }
- void CQLayerObj::SetLayerName(CString szLayerName)
- {
- m_szLayerName = szLayerName;
- }
- CString CQLayerObj::GetLayerName()
- {
- return m_szLayerName;
- }
- void CQLayerObj::SetShowFlag(BOOL bShowed)
- {
- m_bShowed = bShowed;
- }
- BOOL CQLayerObj::GetShowFlag()
- {
- return m_bShowed;
- }
- BOOL CQLayerObj::IsEmpty()
- {
- int nObjCount = m_ObjList.GetSize();
- return nObjCount==0?TRUE:FALSE;
- }
- void CQLayerObj::InitLayerBoundary()
- {
- m_Boundary.m_fMinX = (double)LARGE_NUMBER;
- m_Boundary.m_fMinY = (double)LARGE_NUMBER;
- m_Boundary.m_fMaxX = -(double)LARGE_NUMBER;
- m_Boundary.m_fMaxY = -(double)LARGE_NUMBER;
- }
- void CQLayerObj::CalculateBoundary(CBoundaryRect * pRect)
- {
- InitLayerBoundary();
- CBoundaryRect bRect;
- int nCount = m_ObjList.GetSize();
- if(!pRect)
- {
- for(int i=0;i<nCount;i++)
- {
- if(m_ObjList[i]->GetObjDeleted() || m_ObjList[i]->GetObjHided())
- continue;
- m_ObjList[i]->GetBoundingRect(&bRect);
- m_Boundary.Union(&bRect);
- }
- }
- else
- {
- for(int i=0;i<nCount;i++)
- {
- if(m_ObjList[i]->GetObjDeleted() || m_ObjList[i]->GetObjHided())
- continue;
- m_ObjList[i]->GetBoundingRect(&bRect);
- m_Boundary.Union(&bRect);
- }
- *pRect = m_Boundary;
- }
- }
- void CQLayerObj::AddObject(CQBaseObj * pObj)
- {
- if(!pObj)return; // 假如要添加的对象不存在 则返回
- pObj->SetObjLayerID(m_lLayerID);
- pObj->SetObjMapID(m_lMapID);
- m_ObjList.Add(pObj);
- CBoundaryRect bRect;
- pObj->GetBoundingRect(&bRect); //添加对象的时候 要把对象的边界矩形也添加进去
- m_Boundary.Union(&bRect);
- }
- void CQLayerObj::RemoveObject(CQBaseObj * pObj)
- {
- if(!pObj)return;
- int nCount = m_ObjList.GetSize();
- if(nCount == 0)return;
- for(int i=0;i<nCount;i++)
- {
- if(pObj->GetObjID() == m_ObjList[i]->GetObjID())
- {
- m_ObjList.RemoveAt(i);
- return;
- }
- }
- }
- void CQLayerObj::Deleteobject(CQBaseObj * pObj)
- {
- if(!pObj)return;
- int nCount = m_ObjList.GetSize();
- if(nCount == 0)return;
- for(int i=0;i<nCount;i++)
- {
- if(pObj->GetObjID() == m_ObjList[i]->GetObjID())
- {
- m_ObjList.RemoveAt(i);
- break;
- }
- }
- delete pObj; //这就是Delete与Remove的区别
- }
- inline int CQLayerObj::GetObjCount()
- {
- return m_ObjList.GetSize();
- }
- CQBaseObj * CQLayerObj::GetObj(int nIndex)
- {
- if(nIndex<0 || nIndex>=GetObjCount()) return NULL;
- return m_ObjList[nIndex];
- }
- CQBaseObj * CQLayerObj::FindObj(long lObjID)
- {
- int nCount = GetObjCount();
- if(nCount == 0)return NULL;
- for(int i=0;i<nCount;i++)
- {
- if(m_ObjList[i]->GetObjID() == lObjID)
- {
- return m_ObjList[i];
- }
- }
- return NULL;
- }
- inline int CQLayerObj::GetObjIndex(long lObjID)
- {
- int nCount = 0;
- if((nCount = GetObjCount()) == 0)return -1;
- for(int i=0;i<nCount;i++)
- {
- if(m_ObjList[i]->GetObjID() == lObjID)
- return i;
- }
- return -1;
- }
- void CQLayerObj::ClearAll()
- {
- int nCount = 0;
- if((nCount = GetObjCount()) == 0)return;
- for(int i=0;i<nCount;i++)
- {
- delete m_ObjList[i];
- }
- m_ObjList.RemoveAll();
- CBoundaryRect r;
- m_Boundary = r; // 边界矩形改变
- }
- CString CQLayerObj::VarToStr()
- {
- CString szObj,szLayer,szResult,szBoundary;
- szResult.Format("图层ID:%ld图层名称:%s图幅ID:%ld图层描述:%s",m_lLayerID,(const char *)m_szLayerName,m_lMapID,(const char *)m_szLayerDis);
- int nCount = GetObjCount();
- if(nCount == 0)
- return CString("图层为空!");
- for(int i=0;i<nCount;i++)
- {
- szObj = m_ObjList[i]->VarToStr();
- szLayer += szObj;
- }
- szBoundary = m_Boundary.VarToStr();
- szResult = szResult + szBoundary + szLayer;
- return szResult;
- }
- void CQLayerObj::Serialize(CArchive & ar)
- {
- if(ar.IsStoring())
- {
- for(int m=0;m<m_ObjList.GetSize();m++)
- {
- CQBaseObj * pp = m_ObjList[m];
- if(pp && pp->GetObjDeleted())
- {
- Deleteobject(pp);
- m--;
- }
- }
- ar.Write(&m_lLayerID,sizeof(long));
- ar.Write(&m_lMapID,sizeof(long));
- int i = m_szLayerName.GetLength();
- ar.Write(&i,sizeof(int));
- ar.Write((const char *)m_szLayerName,i);
- i = m_szLayerDis.GetLength();
- ar.Write(&i,sizeof(int));
- ar.Write((const char *)m_szLayerDis,i);
- i = m_ObjList.GetSize();
- ar.Write(&i,sizeof(int));
- for(int j=0;j<i;j++)
- {
- CQBaseObj * pObj = (CQBaseObj *)m_ObjList[j];
- short nObjType = pObj->GetObjType();
- ar.Write(&nObjType,sizeof(short));
- pObj->Serialize(ar);
- }
- ar.Write(&m_bDeleted,sizeof(BOOL));
- ar.Write(&m_bShowed,sizeof(BOOL));
- ar.Write(&m_bCanEdited,sizeof(BOOL));
- m_Boundary.Serialize(ar);
- }
- else
- {
- ar.Read(&m_lLayerID,sizeof(long));
- ar.Read(&m_lMapID,sizeof(long));
- int i = 0;
- ar.Read(&i,sizeof(int));
- char sz[255];
- ar.Read(sz,i);
- sz[i] = ' ';
- m_szLayerName = CString(sz);
- ar.Read(&i,sizeof(int));
- ar.Read(sz,i);
- sz[i] = ' ';
- m_szLayerDis = CString(sz);
- ar.Read(&i,sizeof(int));
- for(int j=0;j<i;j++)
- {
- short nObjType = -1;
- ar.Read(&nObjType,sizeof(short));
- switch(nObjType)
- {
- case QGIS_OBJ_POINT:
- {
- CQPointObj * pPoint = new CQPointObj;
- pPoint->Serialize(ar);
- pPoint->SetObjMapID(m_lMapID);
- pPoint->SetObjLayerID(m_lLayerID);
- m_ObjList.Add(pPoint);
- break;
- }
- case QGIS_OBJ_LINE:
- {
- CQLineObj * pLine = new CQLineObj;
- pLine->Serialize(ar);
- pLine->SetObjLayerID(m_lLayerID);
- pLine->SetObjMapID(m_lMapID);
- m_ObjList.Add(pLine);
- break;
- }
- default:
- break;
- }
- }
- ar.Read(&m_bDeleted,sizeof(BOOL));
- ar.Read(&m_bShowed,sizeof(BOOL));
- ar.Read(&m_bCanEdited,sizeof(BOOL));
- m_Boundary.Serialize(ar);
- }
- }
- CBoundaryRect * CQLayerObj::GetBoundary()
- {
- return &m_Boundary;
- }
- void CQLayerObj::Save(const char * sz)
- {
- CFile file ;
- CFileException e;
- if(!file.Open(sz,CFile::modeReadWrite|CFile::modeCreate,&e))
- {
- AfxMessageBox("保存文件失败!n");
- return ;
- }
- CArchive ar( &file, CArchive::store);
- this->Serialize(ar);
- ar.Close();
- file.Close();
- }
- //显示
- void CQLayerObj::Display(CQCoordSys * pCoorSys,CDC *pDC,int nDrawMode, int nSpecialMode,COLORREF* pColor)
- {
- if(GetDeleteFlag())return;
- if(!GetShowFlag()) return;
- CRect rect;
- pCoorSys->WPtoLP(m_Boundary,&rect); //将图层的边界矩形对象转换为逻辑坐标
- int nScreenWidth = pDC->GetDeviceCaps(HORZRES); //水平距离
- int nScreenHeight = pDC->GetDeviceCaps(VERTRES);//竖直距离
- if(rect.bottom<0)return;
- if(rect.top>nScreenHeight)return;
- if(rect.left>nScreenWidth)return;
- int ncount = m_ObjList.GetSize();
- if(ncount == 0) return;
- for(int i=0;i<ncount;i++)
- {
- CQBaseObj * pObj = m_ObjList.GetAt(i);
- if(!pObj || pObj->GetObjDeleted() || pObj->GetObjHided()) continue;
- pObj->Display(pCoorSys,pDC,nDrawMode,nSpecialMode,pColor);
- }
- }
- void CQLayerObj::Move(double dx,double dy)
- {
- InitLayerBoundary(); //初始化图层边界矩形
- CBoundaryRect BRect;
- int ncount = m_ObjList.GetSize();
- if(ncount == 0)return;
- for(int i=0;i<ncount;i++)
- {
- CQBaseObj * pObj = m_ObjList.GetAt(i);
- if(!pObj || pObj->GetObjDeleted()) return; //隐藏时的状态呢?
- pObj->Move(dx,dy);
- pObj->GetBoundingRect(&BRect);
- m_Boundary.Union(&BRect); //等于重新计算边界矩形
- }
- }
- //打开图层
- BOOL CQLayerObj::Open(const char * sz)
- {
- CFile file ;
- CFileException e;
- if(!file.Open(sz,CFile::modeReadWrite,&e))
- {
- AfxMessageBox("打开文件失败!n");
- return -1;
- }
- CArchive ar( &file, CArchive::load);
- this->Serialize(ar);
- ar.Close();
- file.Close();
- //重新设置图层的名称(以打开文件明为标准)
- m_szLayerName.Empty();
- BYTE bAdd = 0;
- for(int i =strlen(sz)-1;i>=0;i--)
- {
- if(sz[i]=='.')
- {
- bAdd = 1;
- continue ;
- }
- if(sz[i]=='\')
- {
- bAdd=0;
- break;
- }
- if(bAdd) m_szLayerName.Insert(0,sz[i]);
- }
- return TRUE;
- }
- void CQLayerObj::ReadFromFile(CFile * pFile)
- {
- if(!pFile)return;
- pFile->Read(&m_lLayerID,sizeof(long));
- pFile->Read(&m_lMapID,sizeof(long));
- int ll = 0;
- char sz[255];
- pFile->Read(&ll,sizeof(int));
- pFile->Read(sz,ll);
- sz[ll]=' ';
- pFile->Read(&ll,sizeof(int));
- pFile->Read(sz,ll);
- sz[ll]=' ';
- pFile->Read(&m_bDeleted,sizeof(BOOL));
- pFile->Read(&m_bShowed,sizeof(BOOL));
- pFile->Read(&m_bCanEdited,sizeof(BOOL));
- ReadObjsFromFile(pFile);
- m_Boundary.ReadFromFile(pFile);
- }
- void CQLayerObj::WriteToFile(CFile * pFile)
- {
- if(!pFile)return;
- DeleteAllObjsHavaDeletedflag(); //删除所有带删除标志的对象
- pFile->Write(&m_lLayerID,sizeof(long)); //存图层ID
- pFile->Write(&m_lMapID,sizeof(long));
- int ll = m_szLayerName.GetLength();
- pFile->Write(&ll,sizeof(int));
- pFile->Write((const char *)m_szLayerName,sizeof(ll));
- ll = m_szLayerDis.GetLength();
- pFile->Write(&ll,sizeof(int));
- pFile->Write((const char *)m_szLayerDis,sizeof(ll));
- pFile->Write(&m_bDeleted,sizeof(BOOL));
- pFile->Write(&m_bShowed,sizeof(BOOL));
- pFile->Write(&m_bCanEdited,sizeof(BOOL));
- WriteObjsToFile(pFile);
- m_Boundary.WriteToFile(pFile);
- }
- //删除所有带删除标志的对象
- void CQLayerObj::DeleteAllObjsHavaDeletedflag()
- {
- int nDelObjCount = 0;
- CQBaseObj * pObj = 0;
- int nCount = m_ObjList.GetSize();
- for(int i=0;i<nCount;i++)
- {
- pObj = m_ObjList[i];
- if(pObj->GetObjDeleted()) //假如有删除标志
- {
- delete pObj;
- m_ObjList.RemoveAt(i);
- nDelObjCount++;
- i--;
- }
- }
- }
- void CQLayerObj::WriteObjsToFile(CFile * pFile)
- {
- if(!pFile)return;
- int nCount = m_ObjList.GetSize();
- if(nCount == 0)return;
- DeleteAllObjsHavaDeletedflag();
- pFile->Write(&nCount,sizeof(int));
- for(int i=0;i<nCount;i++)
- {
- CQBaseObj * pObj = m_ObjList[i];
- if(pObj)
- {
- WORD type = pObj->GetObjType();
- pFile->Write(&type,sizeof(WORD));
- pObj->WriteToFile(pFile);
- }
- }
- }
- void CQLayerObj::ReadObjsFromFile(CFile * pFile)
- {
- if(!pFile)return;
- int nCount = 0;
- pFile->Read(&nCount,sizeof(int));
- if(nCount == 0)return;
- int k = m_ObjList.GetSize();
- int i=0;
- for(i=0;i<k;i++)
- {
- delete m_ObjList[i];
- }
- m_ObjList.RemoveAll();
- WORD type;
- for(i=0;i<nCount;i++)
- {
- pFile->Read(&type,sizeof(WORD));
- switch(type)
- {
- case QGIS_OBJ_POINT: //点
- {
- CQPointObj * p = new CQPointObj;
- p->ReadFromFile(pFile);
- m_ObjList.Add(p);
- break;
- }
- case QGIS_OBJ_LINE:
- {
- CQLineObj * p = new CQLineObj;
- p->ReadFromFile(pFile);
- m_ObjList.Add(p);
- break;
- }
- default:
- break;
- }
- }
- }
- void CQLayerObj::Rotate(CQPoint & pt,double fRotateAngle)
- {
- int nCount = m_ObjList.GetSize();
- if(nCount == 0)return;
- InitLayerBoundary();
- CBoundaryRect BRect;
- for(int i=0;i<nCount;i++)
- {
- CQBaseObj * pObj = m_ObjList.GetAt(i);
- if(pObj->GetObjDeleted())continue;
- pObj->Rotate(pt,fRotateAngle);
- pObj->GetBoundingRect(&BRect);
- m_Boundary.Union(&BRect);
- }
- }
- CQBaseObj * CQLayerObj::Select(CQSelObjManager * pSelObjs,CQCoordSys *pCoorSys,CQPoint & pt, double fEffectedDistance)
- {
- int nCount = m_ObjList.GetSize();
- if(nCount == 0)return NULL;
- CQBaseObj * p = NULL;
- CQBaseObj * pcur = NULL;
- for(int i=0;i<nCount;i++)
- {
- p = m_ObjList[i];
- if(!p || p->GetObjDeleted() || p->GetObjHided() || p->GetObjSeleted())
- continue;
- if(p->Select(pCoorSys,pt,fEffectedDistance))
- {
- if(!pcur)
- pcur = p;
- pSelObjs->AddSelObj(p);
- }
- }
- return pcur;
- }
- int CQLayerObj::Select(CQSelObjManager * pSelObjs,CQCoordSys * pCoorSys,CBoundaryRect & rectSel)
- {
- if(!rectSel.IsCross(&m_Boundary))
- return 0;
- CQBaseObj * p = 0;
- int nCount = m_ObjList.GetSize();
- if(nCount == 0)return 0;
- int k = 0;
- for(int i=0;i<nCount;i++)
- {
- p = m_ObjList[i];
- if(!p || p->GetObjHided() || p->GetObjDeleted() || p->GetObjSeleted())
- continue;
- if(p->Select(pCoorSys,rectSel))
- {
- pSelObjs->AddSelObj(p);
- k++;
- }
- }
- return k;
- }
- CQBaseObj * CQLayerObj::SingleSelect(CQSelObjManager * pSelObjs,CQCoordSys * pCoordSys,CQPoint & pt,double fEffecttedDistance,int nSelectObjType /* = 0 */)
- {
- CQBaseObj * pObj = NULL;
- if( nSelectObjType!=QGIS_OBJ_POINT
- && nSelectObjType!=QGIS_OBJ_LINE
- && nSelectObjType!=QGIS_OBJ_LABEL
- && nSelectObjType!=QGIS_OBJ_CIRCLE
- && nSelectObjType!=QGIS_PBJ_POLYGON) //如果指定无效的对象类型
- {
- for(int i=0;i<m_ObjList.GetSize();i++)
- {
- pObj = m_ObjList[i];
- if(!pObj || pObj->GetObjDeleted() || pObj->GetObjHided() || pObj->GetObjSeleted()) continue;
- if(pObj->Select(pCoordSys,pt,fEffecttedDistance))
- {
- pSelObjs->AddSelObj(pObj);
- return pObj;
- }
- }
- }
- else
- {
- for(int i=0;i<m_ObjList.GetSize();i++)
- {
- pObj = m_ObjList[i];
- if(!pObj || pObj->GetObjDeleted() || pObj->GetObjHided() || pObj->GetObjSeleted()) continue;
- if(pObj->GetObjType()!=nSelectObjType)continue;
- if(pObj->Select(pCoordSys,pt,fEffecttedDistance))
- {
- pSelObjs->AddSelObj(pObj);
- return pObj;
- }
- }
- }
- return NULL;
- }
- void CQLayerObj::XFlex(double dcp_x,double dFlex)
- {
- int nObjs = GetObjCount();
- for(int i=0;i<nObjs;i++)
- {
- CQBaseObj * pobj = m_ObjList.GetAt(i);
- if(pobj && !pobj->GetObjDeleted() && !pobj->GetObjHided())
- {
- pobj->XFlex(dcp_x,dFlex);
- }
- }
- }
- void CQLayerObj::YFlex(double dcp_y,double dFley)
- {
- int nObjs = GetObjCount();
- for(int i=0;i<nObjs;i++)
- {
- CQBaseObj * pobj = m_ObjList.GetAt(i);
- if(pobj && !pobj->GetObjDeleted() && !pobj->GetObjHided())
- {
- pobj->XFlex(dcp_y,dFley);
- }
- }
- }