From d8acd1c7187a6591802a10022c81a5a595bd924f Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Mon, 9 Dec 2013 10:42:38 +0100 Subject: [PATCH] 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. --- common/tool/tool_manager.cpp | 40 +++++++++++++++------------------ common/view/view.cpp | 12 ++++++++++ common/view/view_item.cpp | 6 ----- include/tool/tool_base.h | 27 ++++++++++++++++++++++ include/tool/tool_interactive.h | 18 --------------- include/tool/tool_manager.h | 6 ++--- pcbnew/basepcbframe.cpp | 11 +++++++-- pcbnew/router/router_tool.cpp | 2 +- pcbnew/router/router_tool.h | 2 +- pcbnew/tools/selection_tool.cpp | 11 ++++++--- pcbnew/tools/selection_tool.h | 2 +- 11 files changed, 80 insertions(+), 57 deletions(-) diff --git a/common/tool/tool_manager.cpp b/common/tool/tool_manager.cpp index 695203f447..ffc5a2d44b 100644 --- a/common/tool/tool_manager.cpp +++ b/common/tool/tool_manager.cpp @@ -140,22 +140,20 @@ void TOOL_MANAGER::RegisterTool( TOOL_BASE* aTool ) aTool->m_toolMgr = this; - if( aTool->GetType() == INTERACTIVE ) + if( !aTool->Init() ) { - if( !static_cast( 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 - m_toolState.erase( aTool ); - m_toolNameIndex.erase( aTool->GetName() ); - m_toolIdIndex.erase( aTool->GetId() ); + // Unregister the tool + m_toolState.erase( aTool ); + m_toolNameIndex.erase( aTool->GetName() ); + m_toolIdIndex.erase( aTool->GetId() ); - delete st; - delete aTool; - } + delete st; + delete aTool; } } @@ -251,7 +249,7 @@ bool TOOL_MANAGER::runTool( TOOL_BASE* aTool ) state->idle = false; - static_cast( aTool )->Reset(); + aTool->Reset( TOOL_INTERACTIVE::RUN ); // Add the tool on the front of the processing queue (it gets events first) 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, const TOOL_EVENT_LIST& aConditions ) { @@ -513,15 +518,6 @@ void TOOL_MANAGER::SetEnvironment( EDA_ITEM* aModel, KIGFX::VIEW* aView, m_view = aView; m_viewControls = aViewControls; 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 )->Reset(); - } } diff --git a/common/view/view.cpp b/common/view/view.cpp index a68fbc7f71..a607c6112b 100644 --- a/common/view/view.cpp +++ b/common/view/view.cpp @@ -122,6 +122,12 @@ void VIEW::Remove( VIEW_ITEM* aItem ) { VIEW_LAYER& l = m_layers[layers[i]]; 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]]; l.items->Remove( aItem ); 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 diff --git a/common/view/view_item.cpp b/common/view/view_item.cpp index e2ddd6a01d..305ab68579 100644 --- a/common/view/view_item.cpp +++ b/common/view/view_item.cpp @@ -34,17 +34,13 @@ void VIEW_ITEM::ViewSetVisible( bool aIsVisible ) bool update = false; if( m_visible != aIsVisible ) - { update = true; - } m_visible = aIsVisible; // update only if the visibility has really changed if( update ) - { ViewUpdate( APPEARANCE ); - } } @@ -60,9 +56,7 @@ void VIEW_ITEM::ViewUpdate( int aUpdateFlags ) void VIEW_ITEM::ViewRelease() { if( m_view && m_view->IsDynamic() ) - { m_view->Remove( this ); - } } diff --git a/include/tool/tool_base.h b/include/tool/tool_base.h index 7425ea8801..681e3099d9 100644 --- a/include/tool/tool_base.h +++ b/include/tool/tool_base.h @@ -70,6 +70,33 @@ public: 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() * Returns the type of the tool. diff --git a/include/tool/tool_interactive.h b/include/tool/tool_interactive.h index 855f84af5c..7e816bd3a4 100644 --- a/include/tool/tool_interactive.h +++ b/include/tool/tool_interactive.h @@ -48,24 +48,6 @@ public: TOOL_INTERACTIVE( const std::string& aName ); 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() * diff --git a/include/tool/tool_manager.h b/include/tool/tool_manager.h index 57026c2712..6e9c18acb0 100644 --- a/include/tool/tool_manager.h +++ b/include/tool/tool_manager.h @@ -127,10 +127,10 @@ public: TOOL_BASE* FindTool( const std::string& aName ) const; /** - * Resets the state of a given tool by clearing its wait and - * transition lists and calling tool's internal Reset() method. + * Function ResetTools() + * 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 diff --git a/pcbnew/basepcbframe.cpp b/pcbnew/basepcbframe.cpp index aeda80468a..22983f9063 100644 --- a/pcbnew/basepcbframe.cpp +++ b/pcbnew/basepcbframe.cpp @@ -187,7 +187,10 @@ void PCB_BASE_FRAME::SetBoard( BOARD* aBoard ) // update the tool manager with the new board and its view. if( m_toolManager ) + { 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 ); - m_toolManager->SetEnvironment( m_Pcb, m_galCanvas->GetView(), - m_galCanvas->GetViewControls(), this ); + if( aEnable ) + { + m_toolManager->SetEnvironment( m_Pcb, m_galCanvas->GetView(), + m_galCanvas->GetViewControls(), this ); + m_toolManager->ResetTools( TOOL_BASE::GAL_SWITCH ); + } } diff --git a/pcbnew/router/router_tool.cpp b/pcbnew/router/router_tool.cpp index d85b1470ae..19b92a42cf 100644 --- a/pcbnew/router/router_tool.cpp +++ b/pcbnew/router/router_tool.cpp @@ -72,7 +72,7 @@ ROUTER_TOOL::~ROUTER_TOOL() } -void ROUTER_TOOL::Reset() +void ROUTER_TOOL::Reset( RESET_REASON aReason ) { if( m_router ) delete m_router; diff --git a/pcbnew/router/router_tool.h b/pcbnew/router/router_tool.h index da2a36601d..c43283a051 100644 --- a/pcbnew/router/router_tool.h +++ b/pcbnew/router/router_tool.h @@ -41,7 +41,7 @@ public: ROUTER_TOOL(); ~ROUTER_TOOL(); - void Reset(); + void Reset( RESET_REASON aReason ); int Main( TOOL_EVENT& aEvent ); private: diff --git a/pcbnew/tools/selection_tool.cpp b/pcbnew/tools/selection_tool.cpp index f24dbaffd9..bf78405612 100644 --- a/pcbnew/tools/selection_tool.cpp +++ b/pcbnew/tools/selection_tool.cpp @@ -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 getView()->Remove( m_selection.group ); @@ -157,7 +163,6 @@ void SELECTION_TOOL::ClearSelection() item->ViewSetVisible( true ); item->ClearSelected(); } - m_selection.Clear(); getEditFrame()->SetCurItem( NULL ); diff --git a/pcbnew/tools/selection_tool.h b/pcbnew/tools/selection_tool.h index fa6ed0f545..33fc4b2494 100644 --- a/pcbnew/tools/selection_tool.h +++ b/pcbnew/tools/selection_tool.h @@ -85,7 +85,7 @@ public: }; /// @copydoc TOOL_INTERACTIVE::Reset() - void Reset(); + void Reset( RESET_REASON aReason ); /** * Function Main()