Moved Init() & Reset() from TOOL_INTERACTIVE to TOOL_BASE.

Added REASON enum for Reset() function, so tools will know why a reset occured.
Fixed SELECTION_TOOL (it was bailing out, when a new board was loaded and some items were still selected).
Added removal of VIEW_ITEM groups after changing layers and removing items.
This commit is contained in:
Maciej Suminski 2013-12-09 10:42:38 +01:00
parent aebb8b3ff9
commit d8acd1c718
11 changed files with 80 additions and 57 deletions

View File

@ -140,22 +140,20 @@ void TOOL_MANAGER::RegisterTool( TOOL_BASE* aTool )
aTool->m_toolMgr = this; aTool->m_toolMgr = this;
if( aTool->GetType() == INTERACTIVE ) if( !aTool->Init() )
{ {
if( !static_cast<TOOL_INTERACTIVE*>( aTool )->Init() ) std::string msg = StrPrintf( "Initialization of the %s tool failed",
{ aTool->GetName().c_str() );
std::string msg = StrPrintf( "Initialization of the %s tool failed", aTool->GetName().c_str() );
DisplayError( NULL, wxString::FromUTF8( msg.c_str() ) ); DisplayError( NULL, wxString::FromUTF8( msg.c_str() ) );
// Unregister the tool // Unregister the tool
m_toolState.erase( aTool ); m_toolState.erase( aTool );
m_toolNameIndex.erase( aTool->GetName() ); m_toolNameIndex.erase( aTool->GetName() );
m_toolIdIndex.erase( aTool->GetId() ); m_toolIdIndex.erase( aTool->GetId() );
delete st; delete st;
delete aTool; delete aTool;
}
} }
} }
@ -251,7 +249,7 @@ bool TOOL_MANAGER::runTool( TOOL_BASE* aTool )
state->idle = false; state->idle = false;
static_cast<TOOL_INTERACTIVE*>( aTool )->Reset(); aTool->Reset( TOOL_INTERACTIVE::RUN );
// Add the tool on the front of the processing queue (it gets events first) // Add the tool on the front of the processing queue (it gets events first)
m_activeTools.push_front( aTool->GetId() ); m_activeTools.push_front( aTool->GetId() );
@ -282,6 +280,13 @@ TOOL_BASE* TOOL_MANAGER::FindTool( const std::string& aName ) const
} }
void TOOL_MANAGER::ResetTools( TOOL_BASE::RESET_REASON aReason )
{
BOOST_FOREACH( TOOL_BASE* tool, m_toolState | boost::adaptors::map_keys )
tool->Reset( aReason );
}
void TOOL_MANAGER::ScheduleNextState( TOOL_BASE* aTool, TOOL_STATE_FUNC& aHandler, void TOOL_MANAGER::ScheduleNextState( TOOL_BASE* aTool, TOOL_STATE_FUNC& aHandler,
const TOOL_EVENT_LIST& aConditions ) const TOOL_EVENT_LIST& aConditions )
{ {
@ -513,15 +518,6 @@ void TOOL_MANAGER::SetEnvironment( EDA_ITEM* aModel, KIGFX::VIEW* aView,
m_view = aView; m_view = aView;
m_viewControls = aViewControls; m_viewControls = aViewControls;
m_editFrame = aFrame; m_editFrame = aFrame;
// Reset state of the registered tools
BOOST_FOREACH( TOOL_ID toolId, m_activeTools )
{
TOOL_BASE* tool = m_toolIdIndex[toolId]->theTool;
if( tool->GetType() == INTERACTIVE )
static_cast<TOOL_INTERACTIVE*>( tool )->Reset();
}
} }

View File

@ -122,6 +122,12 @@ void VIEW::Remove( VIEW_ITEM* aItem )
{ {
VIEW_LAYER& l = m_layers[layers[i]]; VIEW_LAYER& l = m_layers[layers[i]];
l.items->Remove( aItem ); l.items->Remove( aItem );
MarkTargetDirty( l.target );
// Clear the GAL cache
int prevGroup = aItem->getGroup( layers[i] );
if( prevGroup >= 0 )
m_gal->DeleteGroup( prevGroup );
} }
} }
@ -930,6 +936,12 @@ void VIEW::updateLayers( VIEW_ITEM* aItem )
VIEW_LAYER& l = m_layers[layers[i]]; VIEW_LAYER& l = m_layers[layers[i]];
l.items->Remove( aItem ); l.items->Remove( aItem );
MarkTargetDirty( l.target ); MarkTargetDirty( l.target );
// Redraw the item from scratch
int prevGroup = aItem->getGroup( layers[i] );
if( prevGroup >= 0 )
m_gal->DeleteGroup( prevGroup );
} }
// Add the item to new layer set // Add the item to new layer set

View File

@ -34,17 +34,13 @@ void VIEW_ITEM::ViewSetVisible( bool aIsVisible )
bool update = false; bool update = false;
if( m_visible != aIsVisible ) if( m_visible != aIsVisible )
{
update = true; update = true;
}
m_visible = aIsVisible; m_visible = aIsVisible;
// update only if the visibility has really changed // update only if the visibility has really changed
if( update ) if( update )
{
ViewUpdate( APPEARANCE ); ViewUpdate( APPEARANCE );
}
} }
@ -60,9 +56,7 @@ void VIEW_ITEM::ViewUpdate( int aUpdateFlags )
void VIEW_ITEM::ViewRelease() void VIEW_ITEM::ViewRelease()
{ {
if( m_view && m_view->IsDynamic() ) if( m_view && m_view->IsDynamic() )
{
m_view->Remove( this ); m_view->Remove( this );
}
} }

View File

@ -70,6 +70,33 @@ public:
virtual ~TOOL_BASE() {}; virtual ~TOOL_BASE() {};
///> Determines the reason of reset for a tool
enum RESET_REASON
{
RUN, ///< Tool is invoked after being inactive
MODEL_RELOAD, ///< Model changes
GAL_SWITCH ///< Rendering engine changes
};
/**
* Function Init()
* Init() is called once upon a registration of the tool.
*
* @return True if the initialization went fine, false - otherwise.
*/
virtual bool Init()
{
return true;
}
/**
* Function Reset()
* Brings the tool to a known, initial state. If the tool claimed anything from
* the model or the view, it must release it when its reset.
* @param aReason contains information about the reason of tool reset.
*/
virtual void Reset( RESET_REASON aReason ) = 0;
/** /**
* Function GetType() * Function GetType()
* Returns the type of the tool. * Returns the type of the tool.

View File

@ -48,24 +48,6 @@ public:
TOOL_INTERACTIVE( const std::string& aName ); TOOL_INTERACTIVE( const std::string& aName );
virtual ~TOOL_INTERACTIVE(); virtual ~TOOL_INTERACTIVE();
/**
* Function Reset()
* Brings the tool to a known, initial state. If the tool claimed anything from
* the model or the view, it must release it when its reset.
*/
virtual void Reset() = 0;
/**
* Function Init()
* Init() is called once upon a registration of the tool.
*
* @return True if the initialization went fine, false - otherwise.
*/
virtual bool Init()
{
return true;
}
/** /**
* Function SetContextMenu() * Function SetContextMenu()
* *

View File

@ -127,10 +127,10 @@ public:
TOOL_BASE* FindTool( const std::string& aName ) const; TOOL_BASE* FindTool( const std::string& aName ) const;
/** /**
* Resets the state of a given tool by clearing its wait and * Function ResetTools()
* transition lists and calling tool's internal Reset() method. * Resets all tools (i.e. calls their Reset() method).
*/ */
void ResetTool( TOOL_BASE* aTool ); void ResetTools( TOOL_BASE::RESET_REASON aReason );
/** /**
* Takes an event from the TOOL_DISPATCHER and propagates it to * Takes an event from the TOOL_DISPATCHER and propagates it to

View File

@ -187,7 +187,10 @@ void PCB_BASE_FRAME::SetBoard( BOARD* aBoard )
// update the tool manager with the new board and its view. // update the tool manager with the new board and its view.
if( m_toolManager ) if( m_toolManager )
{
m_toolManager->SetEnvironment( m_Pcb, view, m_galCanvas->GetViewControls(), this ); m_toolManager->SetEnvironment( m_Pcb, view, m_galCanvas->GetViewControls(), this );
m_toolManager->ResetTools( TOOL_BASE::MODEL_RELOAD );
}
} }
} }
@ -610,8 +613,12 @@ void PCB_BASE_FRAME::UseGalCanvas( bool aEnable )
ViewReloadBoard( m_Pcb ); ViewReloadBoard( m_Pcb );
m_toolManager->SetEnvironment( m_Pcb, m_galCanvas->GetView(), if( aEnable )
m_galCanvas->GetViewControls(), this ); {
m_toolManager->SetEnvironment( m_Pcb, m_galCanvas->GetView(),
m_galCanvas->GetViewControls(), this );
m_toolManager->ResetTools( TOOL_BASE::GAL_SWITCH );
}
} }

View File

@ -72,7 +72,7 @@ ROUTER_TOOL::~ROUTER_TOOL()
} }
void ROUTER_TOOL::Reset() void ROUTER_TOOL::Reset( RESET_REASON aReason )
{ {
if( m_router ) if( m_router )
delete m_router; delete m_router;

View File

@ -41,7 +41,7 @@ public:
ROUTER_TOOL(); ROUTER_TOOL();
~ROUTER_TOOL(); ~ROUTER_TOOL();
void Reset(); void Reset( RESET_REASON aReason );
int Main( TOOL_EVENT& aEvent ); int Main( TOOL_EVENT& aEvent );
private: private:

View File

@ -64,9 +64,15 @@ SELECTION_TOOL::~SELECTION_TOOL()
} }
void SELECTION_TOOL::Reset() void SELECTION_TOOL::Reset( RESET_REASON aReason )
{ {
ClearSelection(); if( aReason == TOOL_BASE::MODEL_RELOAD )
// Remove pointers to the selected items from containers
// without changing their properties (as they are already deleted)
m_selection.Clear();
else
// Restore previous properties of selected items and remove them from containers
ClearSelection();
// Reinsert the VIEW_GROUP, in case it was removed from the VIEW // Reinsert the VIEW_GROUP, in case it was removed from the VIEW
getView()->Remove( m_selection.group ); getView()->Remove( m_selection.group );
@ -157,7 +163,6 @@ void SELECTION_TOOL::ClearSelection()
item->ViewSetVisible( true ); item->ViewSetVisible( true );
item->ClearSelected(); item->ClearSelected();
} }
m_selection.Clear(); m_selection.Clear();
getEditFrame<PCB_EDIT_FRAME>()->SetCurItem( NULL ); getEditFrame<PCB_EDIT_FRAME>()->SetCurItem( NULL );

View File

@ -85,7 +85,7 @@ public:
}; };
/// @copydoc TOOL_INTERACTIVE::Reset() /// @copydoc TOOL_INTERACTIVE::Reset()
void Reset(); void Reset( RESET_REASON aReason );
/** /**
* Function Main() * Function Main()