⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 diagramentity.cpp

📁 So you wanted to add a forms editor to your application? A dialog editor? Something that allows draw
💻 CPP
📖 第 1 页 / 共 4 页
字号:
/* ==========================================================================
	Class :			CDiagramEntity

	Author :		Johan Rosengren, Abstrakt Mekanik AB

	Date :			2004-03-29

	Purpose :		"CDiagramEntity" is the base class for all objects that can 
					be drawn and managed by "CDiagramEditor".

	Description :	"CDiagramEntity" is derived from "CObject", to allow 
					instances to be stored in "CObArrays".

	Usage :			Classes should be derived from "CDiagramEntity". "Clone" 
					must be overridden, returning a copy of the this 
					pointer.

					Normally, "Draw" should also be overridden. 

					The class supports basic saving to a text file. If this 
					is desired, "SetType" must be called from the derived 
					class ctor with a string uniquely identifying the class. 
					"FromString" and "GetString" must be overridden if other 
					properties than the default are to be saved. Loading can 
					be accomplished by creating a static "factory"-function 
					returning an instance of the class if "FromString" returns 
					"TRUE" for a given line from a data file. See "CreateFromString" 
					in this class for a model implementation.

					Minimum- and maximum sizes for an instance of the 
					derived object can be set in the class ctor by calling
					"SetConstraints". A 0-constraint means that the object 
					can't be turned "inside out", while -1 means no 
					constraints.

					Popup menus for the derived classes can be created by 
					overriding "ShowPopup". Command ids for the menu items 
					must be in the range "CMD_START" to "CMD_END" inclusively, 
					and if a menu alternative is selected, it will be 
					returned to the class instance through "DoMessage" - 
					which must of course also be overriddden.

					Each derived class can also have a property dialog. The 
					dialog class must be derived from "CDiagramPropertyDlg". 
					The derived "CDiagramEntity" class must have a class 
					member instance of the desired "CDiagramPropertyDlg"-
					derived class, and call "SetPropertyDialog" in the "ctor". 
					Transport of data to and from the object is made in the 
					"CDiagramPropertyDlg"-derived class (see 
					CDiagramPropertyDlg.cpp)

					The number, position and types of the selection rects 
					can be modified by overriding "GetHitCode" and 
					"DrawSelectionMarkers", see CDiagramLine.cpp for an 
					example.

					"BodyInRect" can be overridden to allow non-rect hit 
					testing, see CDiagramLine.cpp for an example.

					"GetCursor" can be overridden to display other cursors 
					than the default ones.

   ========================================================================*/
#include "stdafx.h"
#include "DiagramEntity.h"
#include "DiagramEntityContainer.h"
#include "Tokenizer.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDiagramEntity

CDiagramEntity::CDiagramEntity()
/* ============================================================
	Function :		CDiagramEntity::CDiagramEntity
	Description :	Constructor
	Access :		Public

	Return :		void
	Parameters :	none

	Usage :			

   ============================================================*/
{
	SetParent( NULL );
	SetPropertyDialog( NULL, 0 );
	Clear();
	SetType( _T( "basic" ) );

	SetGroup( 0 );
}

CDiagramEntity::~CDiagramEntity()
/* ============================================================
	Function :		CDiagramEntity::~CDiagramEntity
	Description :	Destructor
	Access :		Public

	Return :		void
	Parameters :	none

	Usage :			

   ============================================================*/
{
}

void CDiagramEntity::Clear()
/* ============================================================
	Function :		CDiagramEntity::Clear
	Description :	Zero all properties of this object.
	Access :		Protected

	Return :		void
	Parameters :	none

	Usage :			Call to initialize the object.

   ============================================================*/
{

	SetRect( 0.0, 0.0, 0.0, 0.0 );
	SetMarkerSize( CSize( 8, 8 ) );
	SetConstraints( CSize( 1, 1 ), CSize( -1, -1 ) );
	Select( FALSE );
	SetParent( NULL );
	SetName( _T( "" ) );

}

CDiagramEntity* CDiagramEntity::Clone()
/* ============================================================
	Function :		CDiagramEntity::Clone
	Description :	Clone this object to a new object.
	Access :		Public

	Return :		CDiagramEntity*	-	The new object.
	Parameters :	none

	Usage :			Call to create a clone of the object. The 
					caller will have to delete the object.

   ============================================================*/
{
	CDiagramEntity* obj = new CDiagramEntity;
	obj->Copy( this );
	return obj;
}

void CDiagramEntity::Copy( CDiagramEntity* obj )
/* ============================================================
	Function :		CDiagramEntity::Copy
	Description :	Copy the information in "obj" to this object.
	Access :		Public

	Return :		void
	Parameters :	CDiagramEntity* obj	-	The object to copy 
											from.
					
	Usage :			Copies basic information. from "obj" to this.
					"GetType" can be used to check for the correct 
					object type in overridden versions.
   ============================================================*/
{

	Clear();

	SetMarkerSize( obj->GetMarkerSize() );
	SetConstraints( obj->GetMinimumSize(), obj->GetMaximumSize() );

	Select( obj->IsSelected() );
	SetParent( obj->GetParent() );
	SetType( obj->GetType() );
	SetTitle( obj->GetTitle() );
	SetName( obj->GetName() );
	SetGroup( obj->GetGroup() );

	SetRect( obj->GetLeft(), obj->GetTop(), obj->GetRight(), obj->GetBottom() );

}

BOOL CDiagramEntity::FromString( const CString& str )
/* ============================================================
	Function :		CDiagramEntity::FromString
	Description :	Sets the values for an object from "str". 
	Access :		Public

	Return :		BOOL				-	"TRUE" if "str" 
											represents an 
											object of this 
											type.
	Parameters :	const CString& str	-	Possible text 
											format 
											representation.
					
	Usage :			Can be called to fill an existing object 
					with information from a string created with 
					"GetString".

   ============================================================*/
{

	BOOL result = FALSE;
	CString data( str );
	CString header = GetHeaderFromString( data );
	if( header == GetType() )
		if( GetDefaultFromString( data ) )
			result = TRUE;

	return result;

}

CString CDiagramEntity::GetHeaderFromString( CString& str )
/* ============================================================
	Function :		CDiagramEntity::GetHeaderFromString
	Description :	Gets the header from "str".
	Access :		Protected

	Return :		CString			-	The type of "str".
	Parameters :	CString& str	-	"CString" to get type from.
					
	Usage :			Call as a part of loading the object. "str" 
					will have the type removed after the call.

   ============================================================*/
{

	CTokenizer main( str, _T( ":" ) );
	CString header;
	CString data;
	if( main.GetSize() == 2 )
	{

		main.GetAt( 0, header );
		main.GetAt( 1, data );
		header.TrimLeft();
		header.TrimRight();
		data.TrimLeft();
		data.TrimRight();

		str = data;
	}

	return header;

}

BOOL CDiagramEntity::GetDefaultFromString( CString& str )
/* ============================================================
	Function :		CDiagramEntity::GetDefaultFromString
	Description :	Gets the default properties from "str"
	Access :		Protected

	Return :		BOOL			-	"TRUE" if the default 
										properties could be loaded ok.
	Parameters :	CString& str	-	"CString" to get the 
										default properties from.
					
	Usage :			Call as a part of loading the object from 
					disk. The default object properties will 
					be stripped from "str" and the object 
					properties set from the data.

   ============================================================*/
{
	BOOL result = FALSE;
	CString data( str );
	if( data[ data.GetLength() -1 ] == _TCHAR( ';' ) )
		data = data.Left( data.GetLength() - 1 ); // Strip the ';'

	CTokenizer tok( data ); 
	int size = tok.GetSize();
	if( size >= 7 )
	{
		double left;
		double top;
		double right;
		double bottom;
		CString title;
		CString name;
		int group;
		int count = 0;

		tok.GetAt( count++, left );
		tok.GetAt( count++, top );
		tok.GetAt( count++, right );
		tok.GetAt( count++, bottom );
		tok.GetAt( count++, title );
		tok.GetAt( count++, name );
		tok.GetAt( count++, group );

		SetRect( left, top, right, bottom );

		title.Replace( _T( "\\colon" ), _T( ":" ) );
		title.Replace( _T( "\\semicolon" ), _T( ";" ) );
		title.Replace( _T( "\\comma" ), _T( "," ) );
		title.Replace( _T( "\\newline" ), _T( "\r\n" ) );

		name.Replace( _T( "\\colon" ), _T( ":" ) );
		name.Replace( _T( "\\semicolon" ), _T( ";" ) );
		name.Replace( _T( "\\comma" ), _T( "," ) );
		name.Replace( _T( "\\newline" ), _T( "\r\n" ) );

		SetTitle( title );
		SetName( name );
		SetGroup( group );

		// Rebuild rest of string
		str = _T( "" );
		for( int t = count ; t < size ; t++ )
		{
			tok.GetAt( t, data );

			str += data;
			if( t < size - 1 )
				str += _T( "," );
		}

		result = TRUE;
	}

	return result;

}

BOOL CDiagramEntity::LoadFromString( CString& data )
/* ============================================================
	Function :		CDiagramEntity::LoadFromString
	Description :	Loads the object from "data".
	Access :		Public

	Return :		BOOL			-	"TRUE" if "str" is a 
										well-formed object prefix.
	Parameters :	CString& data	-	String to load from
					
	Usage :			Call to load the first part of an object 
					from string.

   ============================================================*/
{

	BOOL result = FALSE;
	CString header = GetHeaderFromString( data );
	if( header == GetType() )
		if( GetDefaultFromString( data ) )
			result = TRUE;

	return result;

}

CDiagramEntity* CDiagramEntity::CreateFromString( const CString& str )
/* ============================================================
	Function :		CDiagramEntity::CreateFromString
	Description :	Static factory function that creates and 
					returns an instance of this class if "str" 
					is a valid representation.
	Access :		Public

	Return :		CDiagramEntity*		-	The object, or "NULL" 
											if "str" is not a 
											representation of 
											this type.
	Parameters :	const CString& str	-	The string to create 
											from.
					
	Usage :			Can be used as a factory for text file loads. 
					Each object type should have its own 
					version - the default one is a model 
					implementation.

   ============================================================*/
{

	CDiagramEntity* obj = new CDiagramEntity;
	if(!obj->FromString( str ) )
	{
		delete obj;
		obj = NULL;
	}

	return obj;

}

CString CDiagramEntity::GetString() const
/* ============================================================
	Function :		CDiagramEntity::GetString
	Description :	Creates a string representing the object.
	Access :		Public

	Return :		CString	-	The resulting string
	Parameters :	none

	Usage :			Used to save this object to a text file.

   ============================================================*/
{

	CString str = GetDefaultGetString();

	str += _T( ";" );

	return str;

}

CString CDiagramEntity::GetDefaultGetString() const
/* ============================================================
	Function :		CDiagramEntity::GetDefaultString
	Description :	Gets the default properties of the object 
					as a string.
	Access :		Protected

	Return :		CString	-	Resulting string
	Parameters :	none

	Usage :			Call as a part of the saving of objects 
					to disk.

   ============================================================*/
{
	CString str;

	CString title = GetTitle();
	title.Replace( _T( ":" ), _T( "\\colon" ) );
	title.Replace( _T( ";" ), _T( "\\semicolon" ) );
	title.Replace( _T( "," ), _T( "\\comma" ) );
	title.Replace( _T( "\r\n" ), _T( "\\newline" ) );

	CString name = GetName();
	name.Replace( _T( ":" ), _T( "\\colon" ) );
	name.Replace( _T( ";" ), _T( "\\semicolon" ) );
	name.Replace( _T( "," ), _T( "\\comma" ) );
	name.Replace( _T( "\r\n" ), _T( "\\newline" ) );

	str.Format( _T( "%s:%f,%f,%f,%f,%s,%s,%i" ), GetType(), GetLeft(), GetTop(), GetRight(), GetBottom(), title, name, GetGroup() );

	return str;
}

CRect CDiagramEntity::GetRect() const
/* ============================================================
	Function :		CDiagramEntity::GetRect
	Description :	Returns the object rectangle.
	Access :		Public

	Return :		CRect	-	The object rectangle.
	Parameters :	none

	Usage :			Call to get the object position and size. 
					Will round of fractions.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -