📄 networkentitycontainer.cpp
字号:
/* ==========================================================================
CNetworkEntityContainer
Author : Johan Rosengren, Abstrakt Mekanik AB
Date : 2004-05-04
Purpose : CNetworkEntityContainer is derived from
CDiagramEntityContainer, and acts as the data repository
for CNetworkEditor.
Description : In addition to default handling, CNetworkEntityContainer
handle the link objects, as well as enhance the undo-
functionality to take link operations into consideration.
Usage : As CDiagramEntityContainer.
========================================================================*/
#include "stdafx.h"
#include "NetworkEntityContainer.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CNetworkEntityContainer::CNetworkEntityContainer()
/* ============================================================
Function : CNetworkEntityContainer::CNetworkEntityContainer
Description : constructor
Return : void
Parameters : none
Usage :
============================================================*/
{
}
CNetworkEntityContainer::~CNetworkEntityContainer()
/* ============================================================
Function : CNetworkEntityContainer::~CNetworkEntityContainer
Description : destructor
Return : void
Parameters : none
Usage :
============================================================*/
{
ClearLinks();
ClearUndo();
}
void CNetworkEntityContainer::RemoveAt( int index )
/* ============================================================
Function : CNetworkEntityContainer::RemoveAt
Description : Removes the object at index. Will also
remove all links refering to this object.
Return : void
Parameters : int index - Index for object to remove.
Usage : Overridden to remove links as well.
============================================================*/
{
CDiagramEntity* obj = GetAt( index );
if( obj )
{
CString name = obj->GetName();
CDiagramEntityContainer::RemoveAt( index );
int max = m_links.GetUpperBound();
for( int t = max; t >= 0 ; t-- )
{
CNetworkLink* link = static_cast< CNetworkLink* >( m_links.GetAt( t ) );
if( link->to == name || link->from == name )
{
delete link;
m_links.RemoveAt( t );
}
}
}
}
/////////////////////////////////////////////////////////////////////////////
// Undo operations
void CNetworkEntityContainer::Undo()
/* ============================================================
Function : CNetworkEntityContainer::Undo
Description : Undo the latest operation
Return : void
Parameters : none
Usage : Overridden to also undo link operations.
============================================================*/
{
CDiagramEntityContainer::Undo();
if( m_undoLinks.GetSize() )
{
CObArray* undo = static_cast< CObArray* >( m_undoLinks.GetAt( m_undoLinks.GetUpperBound() ) );
int count = undo->GetSize();
for( int t = 0 ; t < count ; t++ )
{
CNetworkLink* obj = static_cast< CNetworkLink* >( undo->GetAt( t ) );
AddLink( obj->Clone() );
}
int max = undo->GetSize();
for( t = max - 1 ; t >= 0 ; t-- )
delete undo->GetAt( t );
undo->RemoveAll();
delete undo;
m_undoLinks.RemoveAt( m_undoLinks.GetUpperBound() );
}
}
void CNetworkEntityContainer::Snapshot()
/* ============================================================
Function : CNetworkEntityContainer::Snapshot
Description : Creates a snapshot of the current data
state for the undo-functionality.
Return : void
Parameters : none
Usage : Overridden to save the link state as well.
============================================================*/
{
CDiagramEntityContainer::Snapshot();
if( GetUndoStackSize() > 0 && m_undoLinks.GetSize() == GetUndoStackSize() )
{
delete m_undoLinks.GetAt( 0 );
m_undoLinks.RemoveAt( 0 );
}
CObArray* undo = new CObArray;
while( !undo && m_undoLinks.GetSize() )
{
delete m_undoLinks.GetAt( 0 );
m_undoLinks.RemoveAt( 0 );
undo = new CObArray;
}
if( undo )
{
// Save all objects
int count = m_links.GetSize();
for( int t = 0 ; t < count ; t++ )
undo->Add( GetLinkAt( t )->Clone() );
// Add to undo stack
m_undoLinks.Add( undo );
}
}
void CNetworkEntityContainer::ClearUndo()
/* ============================================================
Function : CNetworkEntityContainer::ClearUndo
Description : Clears the undo-array
Return : void
Parameters : none
Usage : Overridden to also clear the link undo
states.
============================================================*/
{
CDiagramEntityContainer::ClearUndo();
int count = m_undoLinks.GetSize() - 1;
for( int t = count ; t >= 0 ; t-- )
{
CObArray* undo = static_cast< CObArray* >( m_undoLinks.GetAt(t) );
// Remove all objects in the stack entry
int max = undo->GetSize();
for( int i = 0; i < max ; i++ )
delete undo->GetAt( i );
undo->RemoveAll();
// Remove the stack entry itself.
delete undo;
}
m_undoLinks.RemoveAll();
}
/////////////////////////////////////////////////////////////////////////////
// Link operations
int CNetworkEntityContainer::GetLinks() const
/* ============================================================
Function : CNetworkEntityContainer::GetLinkCount
Description :
Return : int -
Parameters : none
Usage :
============================================================*/
{
return m_links.GetSize();
}
CObArray* CNetworkEntityContainer::GetLinkArray()
/* ============================================================
Function : CNetworkEntityContainer::GetLinkArray
Description :
Return : CObArray* -
Parameters : none
Usage :
============================================================*/
{
return &m_links;
}
CNetworkSymbol* CNetworkEntityContainer::GetSecondaryLink()
/* ============================================================
Function : CNetworkEntityContainer::GetSecondaryLink
Description : Returns the secondary object of the two
currently selected and linked.
Return : CNetworkSymbol* - Secondary object or
NULL if none.
Parameters : none
Usage : Returns NULL if not exactly two objects are
selected, or they are not linked.
============================================================*/
{
CNetworkSymbol* result = NULL;
if( GetSelectCount() == 2 )
{
int max = GetSize();
CNetworkSymbol* primary = NULL;
CNetworkSymbol* secondary = NULL;
for( int t = 0 ; t < max ; t++ )
{
CNetworkSymbol* obj = dynamic_cast< CNetworkSymbol* >( GetAt( t ) );
if( obj && obj->IsSelected() )
{
if( primary == NULL )
primary = obj;
else
secondary = obj;
}
}
if( primary && secondary )
{
CNetworkLink* link = FindLink( primary, secondary );
if( link )
{
if( primary->GetName() == link->from )
result = secondary;
else
result = primary;
}
}
}
return result;
}
BOOL CNetworkEntityContainer::CanLink()
/* ============================================================
Function : CNetworkEntityContainer::CanLink
Description : Returns if the currently selected objects
can be linked.
Return : BOOL - TRUE if currently exactly two
objects are selected and they
can be linked.
Parameters : none
Usage : Intended as a command enabler for a Link
command.
============================================================*/
{
BOOL result = TRUE;
if( GetSelectCount() == 2 )
{
CNetworkSymbol* primary = GetPrimarySelected();
CNetworkSymbol* secondary = GetSecondarySelected();
if( primary && secondary )
{
// Loop all links, trying to find one having both primary and secondary as nodes
if( FindLink( primary, secondary ) )
result = FALSE;
}
}
return result;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -