Скачиваний:
10
Добавлен:
01.05.2014
Размер:
7.65 Кб
Скачать
// SketcherDoc.cpp : implementation of the CSketcherDoc class
//

#include "stdafx.h"
#include "Sketcher.h"

#include "Elements.h"
#include "SketcherDoc.h"
#include "GraphStructureException.h"

#include "Triangle.h"
#include "Pentagon.h"
#include "Text.h"
#include "TextInPentagon.h"

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

/////////////////////////////////////////////////////////////////////////////
// CSketcherDoc

IMPLEMENT_DYNCREATE(CSketcherDoc, CDocument)

BEGIN_MESSAGE_MAP(CSketcherDoc, CDocument)
	//{{AFX_MSG_MAP(CSketcherDoc)
	ON_COMMAND(ID_COLOR_BLACK, OnColorBlack)
	ON_COMMAND(ID_COLOR_RED, OnColorRed)
	ON_UPDATE_COMMAND_UI(ID_COLOR_BLACK, OnUpdateColorBlack)
	ON_UPDATE_COMMAND_UI(ID_COLOR_RED, OnUpdateColorRed)
	ON_COMMAND(ID_ELEMENT_PENTAGON, OnElementPentagon)
	ON_UPDATE_COMMAND_UI(ID_ELEMENT_PENTAGON, OnUpdateElementPentagon)
	ON_COMMAND(ID_ELEMENT_TRIANGLE, OnElementTriangle)
	ON_UPDATE_COMMAND_UI(ID_ELEMENT_TRIANGLE, OnUpdateElementTriangle)
	ON_COMMAND(ID_ELEMENT_TEXTINPENTAGON, OnElementTextinpentagon)
	ON_UPDATE_COMMAND_UI(ID_ELEMENT_TEXTINPENTAGON, OnUpdateElementTextinpentagon)
	ON_COMMAND(ID_ELEMENT_TEXT, OnElementText)
	ON_UPDATE_COMMAND_UI(ID_ELEMENT_TEXT, OnUpdateElementText)
	ON_COMMAND(ID_COLOR_BLUE, OnColorBlue)
	ON_UPDATE_COMMAND_UI(ID_COLOR_BLUE, OnUpdateColorBlue)
	ON_COMMAND(ID_COLOR_GREEN, OnColorGreen)
	ON_UPDATE_COMMAND_UI(ID_COLOR_GREEN, OnUpdateColorGreen)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSketcherDoc construction/destruction

CSketcherDoc::CSketcherDoc()
{
	// TODO: add one-time construction code here
   m_Element = TRIANGLE;   // Set initial element type
   m_Color = BLACK;    // Set initial drawing color
   m_DocSize = CSize(3000,3000);
   m_view_scale = 1;
}

CSketcherDoc::~CSketcherDoc()
{
	Iterator<Figure*>*i = elements.iterator();

	for(i->first();!i->endNext();i->next())
	{
		Figure* f = i->current();
		delete f;

	}
}

BOOL CSketcherDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CSketcherDoc serialization

void CSketcherDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
		ar << m_view_scale; // save scale view

		ar << elements.getCurrentSize();
		Iterator<Figure*>* i = elements.iterator();
		
		for(i->first();!i->endNext();i->next())
		{
			Figure* f = i->current();
			ar << *f;
			ar << elements.getNodeByVal(f);
		}

		int size = elements.getRealMemSize();

		ar << size;
		
		int** rel_matr = elements.getRelMatrix();

		for (int m = 0; m < size; m++) {
			for (int n = 0; n < size; n++) {
				ar << rel_matr[m][n];
			}
		}
	}
	else
	{
		// TODO: add loading code here
		int nodes, size , x, y, nodenum;
		Figure* pf;
		CString name;
		CString txt;
		COLORREF color;

		ar >> m_view_scale;

		ar >> nodes;
		
		for (int j = 0; j < nodes; j++) {

			ar >> name;
			if ( name == CString("Triangle") ) {
				ar >> x >> y >> size >> color >> nodenum;
				pf = new Triangle(CPoint(x,y), size, color);
			} else if ( name == CString("Pentagon") ) {
				ar >> x >> y >> size >> color >> nodenum;
				pf = new Pentagon(CPoint(x,y), size, color);
			} else if ( name == CString("Text") ) {
				ar >> txt >> x >> y >> color >> nodenum;
				pf = new Text(txt, CPoint(x,y), color);
			} else if ( name == CString("TextInPentagon") ) {
				ar >> x >> y >> size >> txt >> color >> nodenum;
				pf = new TextInPentagon(CPoint(x,y),size,txt, color);
			}
		
			elements.add(pf,nodenum);
		}

		ar >> size;
		
		int** rel_matr = elements.getRelMatrix();

		for (int m = 0; m < size; m++) {
			for (int n = 0; n < size; n++) {
				ar >> rel_matr[m][n];
			}
		}

	}
}

/////////////////////////////////////////////////////////////////////////////
// CSketcherDoc diagnostics

#ifdef _DEBUG
void CSketcherDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CSketcherDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CSketcherDoc commands

void CSketcherDoc::OnColorBlack() 
{
   m_Color = BLACK;        // Set the drawing color to black
}

void CSketcherDoc::OnColorRed() 
{
   m_Color = RED;          // Set the drawing color to red	
}

void CSketcherDoc::OnUpdateColorBlack(CCmdUI* pCmdUI) 
{
   // Set menu item Checked if the current color is black
   pCmdUI->SetCheck(m_Color==BLACK);
}

void CSketcherDoc::OnUpdateColorRed(CCmdUI* pCmdUI) 
{
   // Set menu item Checked if the current color is red
   pCmdUI->SetCheck(m_Color==RED);
}

void CSketcherDoc::DeleteElement(Figure* pElement)
{
   if(pElement)
   {
      // If the element pointer is valid,
      // find the pointer in the list and delete it
	   try {
	   	elements.removeByVal(pElement); //delete from the container
		delete pElement; // delete from the heap
	   } catch (GraphStructureException ex) {
		AfxMessageBox(ex.getCauseMsg(), MB_OK);
	   }
   }
}

void CSketcherDoc::SendToBack(Figure* pElement)
{
   if(pElement)
   {
      // If the element pointer is valid,
      // find the pointer in the list and remove the element
//      POSITION aPosition = m_ElementList.Find(pElement);
//      m_ElementList.RemoveAt(aPosition);

//      m_ElementList.AddHead(pElement);  // Put it back to the beginning
   }
}

void CSketcherDoc::OnElementPentagon() 
{
	// TODO: Add your command handler code here
	m_Element = PENTAGON;
}

void CSketcherDoc::OnUpdateElementPentagon(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_Element == PENTAGON);
}

void CSketcherDoc::OnElementTriangle() 
{
	// TODO: Add your command handler code here
	m_Element = TRIANGLE;
}

void CSketcherDoc::OnUpdateElementTriangle(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_Element == TRIANGLE);
}

void CSketcherDoc::OnElementTextinpentagon() 
{
	// TODO: Add your command handler code here
	if( txtDlg.DoModal() == IDOK) {
		m_Element = TEXT_IN_PENTAGON;
		ptext = txtDlg.getCharTextRepresentation();
	}
}

void CSketcherDoc::OnUpdateElementTextinpentagon(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_Element == TEXT_IN_PENTAGON);
}

void CSketcherDoc::OnElementText() 
{
	// TODO: Add your command handler code here
	if( txtDlg.DoModal() == IDOK) {
		m_Element = TEXT;
		ptext = txtDlg.getCharTextRepresentation();
	}
}

void CSketcherDoc::OnUpdateElementText(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_Element == TEXT);	
}

void CSketcherDoc::OnColorBlue() 
{
	// TODO: Add your command handler code here
	m_Color = BLUE;
}

void CSketcherDoc::OnUpdateColorBlue(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_Color==BLUE);
}

void CSketcherDoc::OnColorGreen() 
{
	// TODO: Add your command handler code here
	m_Color = GREEN;
}

void CSketcherDoc::OnUpdateColorGreen(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_Color==GREEN);
}
Соседние файлы в папке Лабораторная работа 23