From 06361eb2c1a2cb99a6f45017ed6043216d5511ae Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Wed, 9 Jul 2014 13:50:27 +0200 Subject: [PATCH] Added AF_ACTIVATE flag for TOOL_ACTIONs. Reworked the way of processing events in TOOL_MANAGER class. Added GetCommandStr() for TOOL_EVENT class. --- common/tool/tool_manager.cpp | 72 ++++++++++++++++----------------- include/tool/tool_action.h | 13 +++--- include/tool/tool_event.h | 19 ++++++++- include/tool/tool_manager.h | 10 +++++ pcbnew/router/router_tool.cpp | 14 +++---- pcbnew/tools/common_actions.cpp | 38 ++++++++--------- pcbnew/tools/drawing_tool.cpp | 37 +++++++++++++---- 7 files changed, 126 insertions(+), 77 deletions(-) diff --git a/common/tool/tool_manager.cpp b/common/tool/tool_manager.cpp index 6bbc0cd51e..0f4e3f22e2 100644 --- a/common/tool/tool_manager.cpp +++ b/common/tool/tool_manager.cpp @@ -495,15 +495,7 @@ bool TOOL_MANAGER::dispatchStandardEvents( TOOL_EVENT& aEvent ) { // Check if there is a hotkey associated if( m_actionMgr->RunHotKey( aEvent.Modifier() | aEvent.KeyCode() ) ) - return false; // hotkey event was handled so it does not go any further - } - else if( aEvent.Action() == TA_ACTIVATE ) - { - // Check if the tool name conforms to the the used tool name format - assert( std::count( aEvent.m_commandStr->begin(), aEvent.m_commandStr->end(), '.' ) == 1 ); - - dispatchActivation( aEvent ); - // do not return false, as the event has to go on to the destined tool + return false; // hotkey event was handled so it does not go any further } return true; @@ -512,41 +504,23 @@ bool TOOL_MANAGER::dispatchStandardEvents( TOOL_EVENT& aEvent ) bool TOOL_MANAGER::dispatchActivation( TOOL_EVENT& aEvent ) { - std::map::iterator tool = m_toolNameIndex.find( *aEvent.m_commandStr ); - - if( tool != m_toolNameIndex.end() ) + if( aEvent.IsActivate() ) { - runTool( tool->second->theTool ); - return true; + std::map::iterator tool = m_toolNameIndex.find( *aEvent.m_commandStr ); + + if( tool != m_toolNameIndex.end() ) + { + runTool( tool->second->theTool ); + return true; + } } return false; } -void TOOL_MANAGER::finishTool( TOOL_STATE* aState ) +void TOOL_MANAGER::dispatchContextMenu( TOOL_EVENT& aEvent ) { - if( !aState->Pop() ) // if there are no other contexts saved on the stack - { - // find the tool and deactivate it - std::deque::iterator tool = std::find( m_activeTools.begin(), m_activeTools.end(), - aState->theTool->GetId() ); - - if( tool != m_activeTools.end() ) - m_activeTools.erase( tool ); - } -} - - -bool TOOL_MANAGER::ProcessEvent( TOOL_EVENT& aEvent ) -{ - // Early dispatch of events destined for the TOOL_MANAGER - if( !dispatchStandardEvents( aEvent ) ) - return false; - - dispatchInternal( aEvent ); - - // popup menu handling BOOST_FOREACH( TOOL_ID toolId, m_activeTools ) { TOOL_STATE* st = m_toolIdIndex[toolId]; @@ -577,6 +551,32 @@ bool TOOL_MANAGER::ProcessEvent( TOOL_EVENT& aEvent ) break; } } +} + + +void TOOL_MANAGER::finishTool( TOOL_STATE* aState ) +{ + if( !aState->Pop() ) // if there are no other contexts saved on the stack + { + // find the tool and deactivate it + std::deque::iterator tool = std::find( m_activeTools.begin(), m_activeTools.end(), + aState->theTool->GetId() ); + + if( tool != m_activeTools.end() ) + m_activeTools.erase( tool ); + } +} + + +bool TOOL_MANAGER::ProcessEvent( TOOL_EVENT& aEvent ) +{ + // Early dispatch of events destined for the TOOL_MANAGER + if( !dispatchStandardEvents( aEvent ) ) + return false; + + dispatchInternal( aEvent ); + dispatchActivation( aEvent ); + dispatchContextMenu( aEvent ); if( m_view->IsDirty() ) { diff --git a/include/tool/tool_action.h b/include/tool/tool_action.h index c9d3510bc1..e215efba83 100644 --- a/include/tool/tool_action.h +++ b/include/tool/tool_action.h @@ -29,7 +29,6 @@ #include #include -#include #include /** @@ -47,10 +46,10 @@ class TOOL_ACTION public: TOOL_ACTION( const std::string& aName, TOOL_ACTION_SCOPE aScope = AS_CONTEXT, int aDefaultHotKey = 0, const std::string& aMenuItem = std::string( "" ), - const std::string& aMenuDesc = std::string( "" ) ) : + const std::string& aMenuDesc = std::string( "" ), TOOL_ACTION_FLAGS aFlags = AF_NONE ) : m_name( aName ), m_scope( aScope ), m_defaultHotKey( aDefaultHotKey ), m_currentHotKey( aDefaultHotKey ), m_menuItem( aMenuItem ), - m_menuDescription( aMenuDesc ), m_id( -1 ) + m_menuDescription( aMenuDesc ), m_id( -1 ), m_flags( aFlags ) { TOOL_MANAGER::GetActionList().push_back( this ); } @@ -190,8 +189,7 @@ public: */ bool IsActivation() const { - // Tool activation events are of format appName.toolName - return std::count( m_name.begin(), m_name.end(), '.' ) == 1; + return m_flags & AF_ACTIVATE; } private: @@ -200,7 +198,7 @@ private: /// Name of the action (convention is: app.[tool.]action.name) std::string m_name; - /// Scope of the action (ie. the event that is issued after activation). + /// Scope of the action (i.e. the event that is issued after activation). TOOL_ACTION_SCOPE m_scope; /// Default hot key that activates the action. @@ -221,6 +219,9 @@ private: /// Unique ID for fast matching. Assigned by ACTION_MANAGER. int m_id; + /// Action flags + TOOL_ACTION_FLAGS m_flags; + /// Origin of the action // const TOOL_BASE* m_origin; diff --git a/include/tool/tool_event.h b/include/tool/tool_event.h index 854e4d3b38..fd6eaa2c72 100644 --- a/include/tool/tool_event.h +++ b/include/tool/tool_event.h @@ -126,6 +126,13 @@ enum TOOL_ACTION_SCOPE AS_GLOBAL ///> Global action (toolbar/main menu event, global shortcut) }; +/// Flags for tool actions +enum TOOL_ACTION_FLAGS +{ + AF_NONE = 0, + AF_ACTIVATE = 1 ///> Action activates a tool +}; + /// Defines when a context menu is opened. enum CONTEXT_MENU_TRIGGER { @@ -268,6 +275,11 @@ public: return m_actions == TA_CANCEL_TOOL; } + bool IsActivate() const + { + return m_actions == TA_ACTIVATE; + } + ///> Returns information about key modifiers state (Ctrl, Alt, etc.) int Modifier( int aMask = MD_MODIFIER_MASK ) const { @@ -334,11 +346,16 @@ public: */ bool IsAction( const TOOL_ACTION* aAction ) const; - boost::optional GetCommandId() + boost::optional GetCommandId() const { return m_commandId; } + boost::optional GetCommandStr() const + { + return m_commandStr; + } + private: friend class TOOL_MANAGER; diff --git a/include/tool/tool_manager.h b/include/tool/tool_manager.h index 54ec87bf57..fdc17480d6 100644 --- a/include/tool/tool_manager.h +++ b/include/tool/tool_manager.h @@ -274,6 +274,10 @@ private: struct TOOL_STATE; typedef std::pair TRANSITION; + /** + * Function dispatchInternal + * Passes an event at first to the active tools, then to all others. + */ void dispatchInternal( TOOL_EVENT& aEvent ); /** @@ -292,6 +296,12 @@ private: */ bool dispatchActivation( TOOL_EVENT& aEvent ); + /** + * Function dispatchContextMenu() + * Handles context menu related events. + */ + void dispatchContextMenu( TOOL_EVENT& aEvent ); + /** * Function invokeTool() * Invokes a tool by sending a proper event (in contrary to runTool, which makes the tool run diff --git a/pcbnew/router/router_tool.cpp b/pcbnew/router/router_tool.cpp index 7416c14c17..f961722180 100644 --- a/pcbnew/router/router_tool.cpp +++ b/pcbnew/router/router_tool.cpp @@ -480,8 +480,7 @@ void ROUTER_TOOL::updateEndItem( TOOL_EVENT& aEvent ) VECTOR2I p = getView()->ToWorld( ctls->GetMousePosition() ); VECTOR2I cp = ctls->GetCursorPosition(); int layer; - - bool snapEnabled = !aEvent.Modifier(MD_SHIFT); + bool snapEnabled = !aEvent.Modifier( MD_SHIFT ); m_router->EnableSnapping ( snapEnabled ); @@ -554,7 +553,7 @@ void ROUTER_TOOL::performRouting() while( OPT_TOOL_EVENT evt = Wait() ) { - if( evt->IsCancel() ) + if( evt->IsCancel() || evt->IsActivate() ) break; else if( evt->Action() == TA_UNDO_REDO ) { @@ -663,7 +662,7 @@ int ROUTER_TOOL::Main( TOOL_EVENT& aEvent ) m_needsSync = false; } - if( evt->IsCancel() ) + if( evt->IsCancel() || evt->IsActivate() ) break; // Finish else if( evt->Action() == TA_UNDO_REDO ) m_needsSync = true; @@ -677,10 +676,11 @@ int ROUTER_TOOL::Main( TOOL_EVENT& aEvent ) performDragging(); else performRouting(); - } else if ( evt->IsAction( &ACT_Drag ) ) + } + else if ( evt->IsAction( &ACT_Drag ) ) performDragging(); - handleCommonEvents(*evt); + handleCommonEvents( *evt ); } // Restore the default settings @@ -715,7 +715,7 @@ void ROUTER_TOOL::performDragging() while( OPT_TOOL_EVENT evt = Wait() ) { - if( evt->IsCancel() ) + if( evt->IsCancel() || evt->IsActivate() ) break; else if( evt->Action() == TA_UNDO_REDO ) { diff --git a/pcbnew/tools/common_actions.cpp b/pcbnew/tools/common_actions.cpp index 856fcf81f6..5f0fdf5d3c 100644 --- a/pcbnew/tools/common_actions.cpp +++ b/pcbnew/tools/common_actions.cpp @@ -29,7 +29,7 @@ // Selection tool actions TOOL_ACTION COMMON_ACTIONS::selectionActivate( "pcbnew.InteractiveSelection", - AS_GLOBAL, 0, "", "" ); // No description, it is not supposed to be shown anywhere + AS_GLOBAL, 0, "", "", AF_ACTIVATE ); // No description, it is not supposed to be shown anywhere TOOL_ACTION COMMON_ACTIONS::selectionSingle( "pcbnew.InteractiveSelection.Single", AS_GLOBAL, 0, "", "" ); // No description, it is not supposed to be shown anywhere @@ -41,7 +41,7 @@ TOOL_ACTION COMMON_ACTIONS::selectionClear( "pcbnew.InteractiveSelection.Clear", // Edit tool actions TOOL_ACTION COMMON_ACTIONS::editActivate( "pcbnew.InteractiveEdit", AS_GLOBAL, 'M', - "Move", "Moves the selected item(s)" ); + "Move", "Moves the selected item(s)", AF_ACTIVATE ); TOOL_ACTION COMMON_ACTIONS::rotate( "pcbnew.InteractiveEdit.rotate", AS_GLOBAL, 'R', @@ -63,50 +63,43 @@ TOOL_ACTION COMMON_ACTIONS::properties( "pcbnew.InteractiveEdit.properties", // Drawing tool actions TOOL_ACTION COMMON_ACTIONS::drawLine( "pcbnew.InteractiveDrawing.line", AS_GLOBAL, 0, - "Draw a line", "Draw a line" ); + "Draw a line", "Draw a line", AF_ACTIVATE ); TOOL_ACTION COMMON_ACTIONS::drawCircle( "pcbnew.InteractiveDrawing.circle", AS_GLOBAL, 0, - "Draw a circle", "Draw a circle" ); + "Draw a circle", "Draw a circle", AF_ACTIVATE ); TOOL_ACTION COMMON_ACTIONS::drawArc( "pcbnew.InteractiveDrawing.arc", AS_GLOBAL, 0, - "Draw an arc", "Draw an arc" ); + "Draw an arc", "Draw an arc", AF_ACTIVATE ); TOOL_ACTION COMMON_ACTIONS::placeTextModule( "pcbnew.InteractiveDrawing.textPcb", AS_GLOBAL, 0, - "Add a text", "Add a text" ); + "Add a text", "Add a text", AF_ACTIVATE ); TOOL_ACTION COMMON_ACTIONS::placeTextPcb( "pcbnew.InteractiveDrawing.textModule", AS_GLOBAL, 0, - "Add a text", "Add a text" ); + "Add a text", "Add a text", AF_ACTIVATE ); TOOL_ACTION COMMON_ACTIONS::drawDimension( "pcbnew.InteractiveDrawing.dimension", AS_GLOBAL, 0, - "Add a dimension", "Add a dimension" ); + "Add a dimension", "Add a dimension", AF_ACTIVATE ); TOOL_ACTION COMMON_ACTIONS::drawZone( "pcbnew.InteractiveDrawing.zone", AS_GLOBAL, 0, - "Add a filled zone", "Add a filled zone" ); + "Add a filled zone", "Add a filled zone", AF_ACTIVATE ); TOOL_ACTION COMMON_ACTIONS::drawKeepout( "pcbnew.InteractiveDrawing.keepout", AS_GLOBAL, 0, - "Add a keepout area", "Add a keepout area" ); + "Add a keepout area", "Add a keepout area", AF_ACTIVATE ); TOOL_ACTION COMMON_ACTIONS::placeTarget( "pcbnew.InteractiveDrawing.placeTarget", AS_GLOBAL, 0, - "Add layer alignment target", "Add layer alignment target" ); + "Add layer alignment target", "Add layer alignment target", AF_ACTIVATE ); TOOL_ACTION COMMON_ACTIONS::placeModule( "pcbnew.InteractiveDrawing.placeModule", AS_GLOBAL, 'O', - "Add modules", "Add modules" ); - -TOOL_ACTION COMMON_ACTIONS::routerActivate( "pcbnew.InteractiveRouter", - AS_GLOBAL, 'X', - "Run push & shove router", "Run push & shove router" ); - -TOOL_ACTION COMMON_ACTIONS::pointEditorUpdate( "pcbnew.PointEditor.update", - AS_GLOBAL, 0, "", "" ); // No description, it is not supposed to be shown anywhere + "Add modules", "Add modules", AF_ACTIVATE ); // View Controls @@ -268,6 +261,13 @@ TOOL_ACTION COMMON_ACTIONS::showHelp( "pcbnew.Control.showHelp", AS_GLOBAL, '?', "", "" ); +TOOL_ACTION COMMON_ACTIONS::routerActivate( "pcbnew.InteractiveRouter", + AS_GLOBAL, 'X', + "Run push & shove router", "Run push & shove router", AF_ACTIVATE ); + +TOOL_ACTION COMMON_ACTIONS::pointEditorUpdate( "pcbnew.PointEditor.update", + AS_GLOBAL, 0, "", "" ); // No description, it is not supposed to be shown anywhere + boost::optional COMMON_ACTIONS::TranslateLegacyId( int aId ) { diff --git a/pcbnew/tools/drawing_tool.cpp b/pcbnew/tools/drawing_tool.cpp index 9d3cf63670..37fc7b0698 100644 --- a/pcbnew/tools/drawing_tool.cpp +++ b/pcbnew/tools/drawing_tool.cpp @@ -123,7 +123,7 @@ int DRAWING_TOOL::DrawArc( TOOL_EVENT& aEvent ) { cursorPos = m_controls->GetCursorPosition(); - if( evt->IsCancel() ) + if( evt->IsCancel() || evt->IsActivate() ) { if( step != SET_ORIGIN ) // start from the beginning { @@ -135,6 +135,9 @@ int DRAWING_TOOL::DrawArc( TOOL_EVENT& aEvent ) } else break; + + if( evt->IsActivate() ) // now finish unconditionally + break; } else if( evt->IsKeyPressed() && step != SET_ORIGIN ) @@ -301,7 +304,7 @@ int DRAWING_TOOL::PlaceTextModule( TOOL_EVENT& aEvent ) { VECTOR2I cursorPos = m_controls->GetCursorPosition(); - if( evt->IsCancel() ) + if( evt->IsCancel() || evt->IsActivate() ) { if( text ) { @@ -315,6 +318,9 @@ int DRAWING_TOOL::PlaceTextModule( TOOL_EVENT& aEvent ) } else break; + + if( evt->IsActivate() ) // now finish unconditionally + break; } else if( text && evt->Category() == TC_COMMAND ) @@ -406,7 +412,7 @@ int DRAWING_TOOL::PlaceTextPcb( TOOL_EVENT& aEvent ) { VECTOR2I cursorPos = m_controls->GetCursorPosition(); - if( evt->IsCancel() ) + if( evt->IsCancel() || evt->IsActivate() ) { if( text ) { @@ -420,6 +426,9 @@ int DRAWING_TOOL::PlaceTextPcb( TOOL_EVENT& aEvent ) } else break; + + if( evt->IsActivate() ) // now finish unconditionally + break; } else if( text && evt->Category() == TC_COMMAND ) @@ -520,7 +529,7 @@ int DRAWING_TOOL::DrawDimension( TOOL_EVENT& aEvent ) { VECTOR2I cursorPos = m_controls->GetCursorPosition(); - if( evt->IsCancel() ) + if( evt->IsCancel() || evt->IsActivate() ) { if( step != SET_ORIGIN ) // start from the beginning { @@ -532,6 +541,9 @@ int DRAWING_TOOL::DrawDimension( TOOL_EVENT& aEvent ) } else break; + + if( evt->IsActivate() ) // now finish unconditionally + break; } else if( evt->IsKeyPressed() && step != SET_ORIGIN ) @@ -707,7 +719,7 @@ int DRAWING_TOOL::PlaceTarget( TOOL_EVENT& aEvent ) { cursorPos = m_controls->GetCursorPosition(); - if( evt->IsCancel() ) + if( evt->IsCancel() || evt->IsActivate() ) break; else if( evt->IsKeyPressed() ) @@ -783,7 +795,7 @@ int DRAWING_TOOL::PlaceModule( TOOL_EVENT& aEvent ) { VECTOR2I cursorPos = m_controls->GetCursorPosition(); - if( evt->IsCancel() ) + if( evt->IsCancel() || evt->IsActivate() ) { if( module ) { @@ -796,6 +808,9 @@ int DRAWING_TOOL::PlaceModule( TOOL_EVENT& aEvent ) } else break; + + if( evt->IsActivate() ) // now finish unconditionally + break; } else if( module && evt->Category() == TC_COMMAND ) @@ -915,7 +930,7 @@ int DRAWING_TOOL::drawSegment( int aShape, bool aContinous ) updatePreview = true; } - if( evt->IsCancel() ) + if( evt->IsCancel() || evt->IsActivate() ) { if( !graphic ) break; @@ -927,6 +942,9 @@ int DRAWING_TOOL::drawSegment( int aShape, bool aContinous ) graphic = NULL; m_controls->SetAutoPan( false ); + + if( evt->IsActivate() ) // now finish unconditionally + break; } else if( graphic && evt->IsKeyPressed() ) @@ -1093,7 +1111,7 @@ int DRAWING_TOOL::drawZone( bool aKeepout ) updatePreview = true; } - if( evt->IsCancel() ) + if( evt->IsCancel() || evt->IsActivate() ) { if( numPoints > 0 ) // cancel the current zone { @@ -1114,6 +1132,9 @@ int DRAWING_TOOL::drawZone( bool aKeepout ) } else // there is no zone currently drawn - just stop the tool break; + + if( evt->IsActivate() ) // now finish unconditionally + break; } else if( evt->IsClick( BUT_LEFT ) )