Скачиваний:
17
Добавлен:
01.05.2014
Размер:
7.08 Кб
Скачать
// RegionSearchView.cpp : implementation of the CRegionSearchView class
//

#include "stdafx.h"
#include "RegionSearch.h"

#include "RegionSearchDoc.h"
#include "RegionSearchView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CRegionSearchView

IMPLEMENT_DYNCREATE(CRegionSearchView, CView)

BEGIN_MESSAGE_MAP(CRegionSearchView, CView)
	//{{AFX_MSG_MAP(CRegionSearchView)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRegionSearchView construction/destruction

CRegionSearchView::CRegionSearchView()
{
	// TODO: add construction code here

}

CRegionSearchView::~CRegionSearchView()
{
}

BOOL CRegionSearchView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CRegionSearchView drawing

void CRegionSearchView::OnDraw(CDC* pDC)
{
	CRegionSearchDoc* pDoc = GetDocument();
	
	GetClientRect(&ClientRect);
	
	if(pDoc->Tree)
	{
		Counter=0;
		DrawTree(pDC, pDoc->Tree->GetRoot(), 1, 0, ClientRect.Width()-1);
	}
	DrawLegend(pDC);
}

void CRegionSearchView::DrawTree(CDC* pDC, BinaryTree<RegionTreeNode>* t, int l, int lx, int rx)
{
	Counter++;
	CRegionSearchDoc* pDoc = GetDocument();

	int x=(lx+rx)/2;
	if(t->GetLeftSubTree())
	{
		pDC->MoveTo(x, 70*l);
		pDC->LineTo((lx+x)/2, 70*(l+1));
	}
	if(t->GetRightSubTree())
	{
		pDC->MoveTo(x, 70*l);
		pDC->LineTo((rx+x)/2, 70*(l+1));
	}
	const std::list<SearchStep>& steps = pDoc->GetSearchSteps();
	if(!pDoc->GetShowNodes())
	{
		pDC->Ellipse(x-25, 70*l-25, x+25, 70*l+25);
		
		COLORREF prevColor = pDC->GetTextColor();
		if(std::find(pDoc->GetSearchSteps().begin(), pDoc->GetSearchSteps().end(),
			SearchStep(t, SearchStep::NodeFound)) != pDoc->GetSearchSteps().end())
				pDC->SetTextColor(RGB(255,0,0));
		
		if(std::find(pDoc->GetSearchSteps().begin(), pDoc->GetSearchSteps().end(),
			SearchStep(t, SearchStep::SearchNode)) != pDoc->GetSearchSteps().end())
				pDC->SetTextColor(RGB(0,255,0));
		std::ostringstream os;
		os<<"["<<t->GetNodeValue().GetB()<<", "<<t->GetNodeValue().GetE()<<")";
		pDC->TextOut(x-22, 70*l-10, os.str().c_str());
		pDC->SetTextColor(prevColor);
	}

	if(pDoc->GetShowPoints())
	{
		if(!t->GetNodeValue().GetFlag())
		{
			std::ostringstream os1;
			const std::vector<Point>& ps = t->GetNodeValue().GetPoints();
			for(int i=0; i<ps.size(); i++)
				os1<<ps[i].number<<"  ";
			CSize sz = pDC->GetTextExtent(CString(os1.str().c_str()));

			pDC->TextOut(x-sz.cx/2, 70*l+30, os1.str().c_str());
		}
		else
		{
			std::ostringstream os1, os2, os3, os4 ,os5;
			const std::vector<Point>& ps = t->GetNodeValue().GetPoints();
			std::vector<Point>::const_iterator i = ps.begin();

			while(i!=t->GetNodeValue().GetFirstPoint())
				os1<<i++->number<<"  ";
			//первая точка
			if(i!=t->GetNodeValue().GetPoints().end())
				os2<<i++->number<<"  ";
			//последующие точки, не включая последнюю
			while(std::distance(i,t->GetNodeValue().GetLastPoint())>1)
				os3<<i++->number<<"  ";
			//последняя точка
			if(i!=ps.end()) os4<<i++->number<<"  ";
			while(i!=ps.end())
				os5<<i++->number<<"  ";

			CSize sz1 = pDC->GetTextExtent(os1.str().c_str(), os1.str().length());
			CSize sz2 = pDC->GetTextExtent(os2.str().c_str(), os2.str().length());
			CSize sz3 = pDC->GetTextExtent(os3.str().c_str(), os3.str().length());
			CSize sz4 = pDC->GetTextExtent(os4.str().c_str(), os4.str().length());
			CSize sz5 = pDC->GetTextExtent(os5.str().c_str(), os5.str().length());

			int l1 = x-(sz1.cx+sz2.cx+sz3.cx+sz4.cx+sz5.cx)/2;
			int l2 = l1 + sz1.cx;
			int l3 = l2 + sz2.cx;
			int l4 = l3 + sz3.cx;
			int l5 = l4 + sz4.cx;


			COLORREF prevColor = pDC->GetTextColor();
			pDC->TextOut(l1, 70*l+30, os1.str().c_str());
			if(std::find(pDoc->GetSearchSteps().begin(), pDoc->GetSearchSteps().end(),
				SearchStep(t, SearchStep::BinarySearch1)) != pDoc->GetSearchSteps().end())
					pDC->SetTextColor(RGB(255,0,0));
			pDC->TextOut(l2, 70*l+30, os2.str().c_str());
			pDC->SetTextColor(prevColor);

			if(std::find(pDoc->GetSearchSteps().begin(), pDoc->GetSearchSteps().end(),
				SearchStep(t, SearchStep::SelectPoints)) != pDoc->GetSearchSteps().end())
					pDC->SetTextColor(RGB(255,0,0));
			pDC->TextOut(l3, 70*l+30, os3.str().c_str());
			pDC->SetTextColor(prevColor);

			if(std::find(pDoc->GetSearchSteps().begin(), pDoc->GetSearchSteps().end(),
				SearchStep(t, SearchStep::BinarySearch2)) != pDoc->GetSearchSteps().end())
					pDC->SetTextColor(RGB(255,0,0));
			pDC->TextOut(l4, 70*l+30, os4.str().c_str());
			pDC->SetTextColor(prevColor);

			pDC->TextOut(l5, 70*l+30, os5.str().c_str());
		}
	}
	if(l+1<=pDoc->GetLevelCount())
	{
		if(t->GetLeftSubTree() && Counter<pDoc->GetBuildStepCounter())
			DrawTree(pDC, t->GetLeftSubTree(), l+1, lx, x);
		if(t->GetRightSubTree() && Counter<pDoc->GetBuildStepCounter()) DrawTree(pDC, t->GetRightSubTree(), l+1, x, rx);
	}
}

void CRegionSearchView::DrawLegend(CDC* pDC)
{
	pDC->TextOut(60, 5, "Узлы отнесения и точки, попадающие в область");
	pDC->TextOut(60, 25, "Узлы, посещеннные при поиске");
	
	pDC->FillSolidRect(10, 5, 40, 10, RGB(255,0,0));
	pDC->FillSolidRect(10, 25, 40, 10, RGB(0,255,0));
}

/////////////////////////////////////////////////////////////////////////////
// CRegionSearchView printing

BOOL CRegionSearchView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CRegionSearchView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CRegionSearchView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CRegionSearchView diagnostics

#ifdef _DEBUG
void CRegionSearchView::AssertValid() const
{
	CView::AssertValid();
}

void CRegionSearchView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CRegionSearchDoc* CRegionSearchView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CRegionSearchDoc)));
	return (CRegionSearchDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CRegionSearchView message handlers
Соседние файлы в папке Source