umleditor.cpp

来自「uml编辑器很牛」· C++ 代码 · 共 1,677 行 · 第 1/4 页

CPP
1,677
字号

	BOOL result = FALSE;
	if( m_currentCursor )
	{
		::SetCursor( m_currentCursor );
		result = TRUE;
	}
	else
		result = CDiagramEditor::OnSetCursor( pWnd, nHitTest, message );

	return result;

}

void CUMLEditor::OnRButtonDown( UINT nFlags, CPoint point ) 
/* ============================================================
	Function :		CUMLEditor::OnRButtonDown
	Description :	Handler for the "WM_RBUTTONDOWN"-message.
	Access :		Protected

	Return :		void
	Parameters :	UINT nFlags		-	Not used
					CPoint point	-	Not used.
					
	Usage :			Called from MFC. We remove dangling lines.

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

	// The default handler will select leave the current 
	// link - if any - dangling, and so we start by deleting it.
	if( GetSelectCount() == 1 )
	{
		CUMLLineSegment* seg = dynamic_cast< CUMLLineSegment* >( GetSelectedObject() );
		if( seg && !IsConnected( seg ) )
			GetDiagramEntityContainer()->Remove( seg );
	}

	CDiagramEditor::OnRButtonDown( nFlags, point );

}

void CUMLEditor::OnRButtonUp( UINT nFlags, CPoint point ) 
/* ============================================================
	Function :		CUMLEditor::OnRButtonUp
	Description :	Handler for the "WM_RBUTTONUP"-message.
	Access :		Protected

	Return :		void
	Parameters :	UINT nFlags		-	Not used
					CPoint point	-	Not used
					
	Usage :			Called from MFC. We end the line drawing 
					mode.

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

	if( !m_drawingLine )
		CDiagramEditor::OnRButtonUp( nFlags, point );

	m_drawingLine = FALSE;

}

/////////////////////////////////////////////////////////////////////////////
// CUMLEditor private helpers

void CUMLEditor::ModifyLinkedPositions()
/* ============================================================
	Function :		CUMLEditor::ModifyLinkedPositions
	Description :	Will modify the position of all unselected 
					items linked to a selected one, with the 
					difference between start and current as 
					offset. 
	Access :		Protected
					
	Return :		void
	Parameters :	none

	Usage :			Should be called after all operations that 
					might move an object. Movement direction is 
					dependent on the link type.

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

	CUMLEntity* obj = NULL;
	int count = 0;
	while( ( obj = GetObjectAt( count++ ) ) )
		if( obj->GetPackage() == GetPackage() && obj->IsSelected() )
			GetUMLEntityContainer()->AdjustLinkedObjects( obj );

}

CUMLEntity* CUMLEditor::GetNamedObject( const CString& name ) const
/* ============================================================
	Function :		CUMLEditor::GetNamedObject
	Description :	Returns the object with the name attribute 
					name.
	Access :		Private
					
	Return :		CUMLEntity*			-	The object, or 
											"NULL" if not found.
	Parameters :	const CString& name	-	The name of the 
											object to find.
					
	Usage :			Call to get the object with the name name, 
					if it exists.

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

	CUMLEntity* result = NULL;
	CUMLEntity* obj;
	int count = 0;
	while( ( obj = GetObjectAt( count++ ) ) )
		if( obj && obj->GetName() == name )
			result = obj;

	return result;

}

CUMLEntity* CUMLEditor::GetAllNamedObject( const CString& name ) const
/* ============================================================
	Function :		CUMLEditor::GetNamedObject
	Description :	Returns the object with the name attribute 
					name.
	Access :		Private
					
	Return :		CUMLEntity*			-	The object, or 
											"NULL" if not found.
	Parameters :	const CString& name	-	The name of the 
											object to find.
					
	Usage :			Call to get the object with the name name, 
					if it exists.

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

	CUMLEditor* const local = const_cast<CUMLEditor* const>(this);

	CString package = GetPackage();
	local->SetPackage( _T( "all" ) );

	CUMLEntity* result = NULL;
	CUMLEntity* obj;
	int count = 0;
	while( ( obj = GetObjectAt( count++ ) ) )
	{
		if( obj && obj->GetName() == name )
		{
			result = obj;
			count = -2;
		}
	}

	local->SetPackage( package );
	return result;

}

/////////////////////////////////////////////////////////////////////////////
// CUMLEditor implementation

BOOL CUMLEditor::IsConnected( CUMLLineSegment* link ) const
/* ============================================================
	Function :		CUMLEditor::IsConnected
	Description :	Checks if the line that "link" is a segment 
					of is connected at both ends.
	Access :		Private

	Return :		BOOL					-	"TRUE" if connected at both ends.
	Parameters :	CUMLLineSegment* link	-	Segment to check
					
	Usage :			Call to see if a line is connected.

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

	CString name = link->GetName();
	BOOL result = FALSE;
	if( link->GetLink( LINK_START ).GetLength() && link->GetLink( LINK_END ).GetLength() )
		result = TRUE;

	return result;

}

CString CUMLEditor::GetPackage() const
/* ============================================================
	Function :		CUMLEditor::GetPackage
	Description :	Gets the current package name.
	Access :		Public

	Return :		CString	-	Current package.
	Parameters :	none

	Usage :			Call to get the current package.

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

	return GetUMLEntityContainer()->GetPackage();

}

void CUMLEditor::SetPackage( const CString& package )
/* ============================================================
	Function :		CUMLEditor::SetPackage
	Description :	Sets the current package name.
	Access :		Public

	Return :		void
	Parameters :	const CString& package	-	New package
					
	Usage :			Call to set the current package.

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

	GetUMLEntityContainer()->SetPackage( package );

}

CUMLEntity* CUMLEditor::GetObjectAt( int index ) const
/* ============================================================
	Function :		CUMLEditor::GetObjectAt
	Description :	Gets the object at "index".
	Access :		Private

	Return :		CUMLEntity*	-	Object at index or "NULL" if 
									out of bounds.
	Parameters :	int index	-	Index to get object from.
					
	Usage :			Convenience function to get an already 
					casted object.

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

	CUMLEntityContainer* objs = GetUMLEntityContainer();
	CUMLEntity* obj = static_cast< CUMLEntity* >( objs->GetAt( index ) );

	return obj;

}

BOOL CUMLEditor::PartialLinesSelected() const
/* ============================================================
	Function :		CUMLEditor::PartialLinesSelected
	Description :	Check if only part of a line is selected.
	Access :		Private

	Return :		BOOL	-	"TRUE" if only a part of a line 
								is selected.
	Parameters :	none

	Usage :			Call to see if only a part of a line is 
					selected.

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

	CUMLEntity* obj;
	int count = 0;
	if( GetSelectCount() != GetObjectCount() && GetSelectCount() > 1 )
	{
		while( ( obj = GetObjectAt( count++ ) ) )
		{
			CUMLLineSegment* line = dynamic_cast< CUMLLineSegment* >( obj );
			if( line && line->IsSelected() )
				if( !GetUMLEntityContainer()->LineSelected( line ) )
					return TRUE;
		}
	}

	return FALSE;

}

void CUMLEditor::SetCursor( CPoint point ) 
/* ============================================================
	Function :		CUMLEditor::SetCursor
	Description :	Selects the cursor for the position "point".
	Access :		Private

	Return :		void
	Parameters :	CPoint point	-	Position to check.
					
	Usage :			Call to see what cursor should be displayed 
					when the mouse cursor is at position 
					"point".

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

	m_currentCursor = NULL;

	CUMLLineSegment* seg = dynamic_cast< CUMLLineSegment* >( GetSelectedObject() );
	CUMLLineSegment* draw = dynamic_cast< CUMLLineSegment* >( GetDrawingObject() );

	if( seg && GetInteractMode() == MODE_RESIZING )
	{
		ScreenToVirtual( point );

		CUMLEntity* obj;
		int count = 0;
		BOOL overOther = FALSE;
		while( ( obj = GetObjectAt( count++ ) ) )
		{
			if( obj->GetPackage() == GetPackage() )
			{
				CPoint hit = seg->GetRect().BottomRight();
				int linkCode = obj->GetLinkCode( hit );
				if( obj != seg )
				{
					if( linkCode != LINK_NONE )
					{
						if( ( seg->IsHorizontal() && ( linkCode == LINK_LEFT || linkCode == LINK_RIGHT ) ) ||
							( !seg->IsHorizontal() && ( linkCode == LINK_TOP || linkCode == LINK_BOTTOM ) ) )
						{
							overOther = TRUE;
							count = -2;
						}
					}
				}
			}
		}

		if( overOther )
			m_currentCursor = m_curAttach;
		else
			m_currentCursor = m_curDrawLine;

	}
	else if( draw && GetInteractMode() == MODE_DRAWING )
	{
		BOOL result = FALSE;
		ScreenToVirtual( point );

		CUMLEntity* obj;
		int count = 0;
		while( ( obj = GetObjectAt( count++ ) ) )
		{
			if( obj->GetPackage() == GetPackage() )
			{
				int linkCode = obj->GetLinkCode( point );
				if( linkCode & LINK_ALL )
				{
					m_currentCursor = m_curAttach;
					result = TRUE;
					count = -2;
				}
			}
		}

		if( !result )
			m_currentCursor = LoadCursor( NULL, IDC_ARROW );

	}
	else if( GetInteractMode() == MODE_NONE ||  GetInteractMode() == MODE_RUBBERBANDING )
	{
		CString package = GetPackage();
		int size = round( 20 * GetZoom() );
		if( package.GetLength() && point.x < size && point.y < size )
			m_currentCursor = m_curUp;
	}

	if( m_currentCursor )
		::SetCursor( m_currentCursor );

}

void CUMLEditor::SetDisplayOptions( int displayOption )
/* ============================================================
	Function :		CUMLEditor::SetDisplayOptions
	Description :	Sets the current diaply option.
	Access :		Public

	Return :		void
	Parameters :	int displayOption	-	New display options
					
	Usage :			Call to set the display option. 
					It can be a combination of the following:
						"DISPLAY_ALL" Show everything
						"DISPLAY_ONLY_PUBLIC" Show only public attributes and operations.
						"DISPLAY_NO_MARKERS" Don't show access markers
						"DISPLAY_NO_ATTRIBUTES" Don't show attributes
						"DISPLAY_NO_OPERATIONS" Don't show operations.
						"DISPLAY_NO_OPERATION_ATTRIBUTE_NAMES" Don't show names for operation parameters

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

	CUMLEntityContainer* objs = GetUMLEntityContainer();
	if( objs )
		objs->SetDisplayOptions( displayOption );

}

int CUMLEditor::GetDisplayOptions() const
/* ============================================================
	Function :		CUMLEditor::GetDisplayOptions
	Description :	Gets the current display options.
	Access :		Public

	Return :		int		-	Current display option(s)
	Parameters :	none

	Usage :			Call to get the current display options.
					It can be a combination of the following:
						"DISPLAY_ALL" Show everything
						"DISPLAY_ONLY_PUBLIC" Show only public attributes and operations.
						"DISPLAY_NO_MARKERS" Don't show access markers
						"DISPLAY_NO_ATTRIBUTES" Don't show attributes
						"DISPLAY_NO_OPERATIONS" Don't show operations.
						"DISPLAY_NO_OPERATION_ATTRIBUTE_NAMES" Don't show names for operation parameters

⌨️ 快捷键说明

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