diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 88db67da3b..c9caece966 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -228,6 +228,7 @@ set( COMMON_SRCS math/math_util.cpp + tool/tool_action.cpp tool/tool_base.cpp tool/tool_manager.cpp tool/tool_dispatcher.cpp diff --git a/common/tool/tool_action.cpp b/common/tool/tool_action.cpp new file mode 100644 index 0000000000..4d1fd9d658 --- /dev/null +++ b/common/tool/tool_action.cpp @@ -0,0 +1,47 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2013 CERN + * @author Maciej Suminski + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, you may find one here: + * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html + * or you may search the http://www.gnu.org website for the version 2 license, + * or you may write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#include + +std::string TOOL_ACTION::GetToolName() const +{ + int dotCount = std::count( m_name.begin(), m_name.end(), '.' ); + + switch( dotCount ) + { + case 0: + assert( false ); // Invalid action name format + return ""; + + case 1: + return m_name; + + case 2: + return m_name.substr( 0, m_name.rfind( '.' ) ); + + default: + assert( false ); // TODO not implemented + return ""; + } +} diff --git a/common/tool/tool_event.cpp b/common/tool/tool_event.cpp index 50739ba419..68518a2d50 100644 --- a/common/tool/tool_event.cpp +++ b/common/tool/tool_event.cpp @@ -92,6 +92,7 @@ const std::string TOOL_EVENT::Format() const { TA_CONTEXT_MENU_CHOICE, "context-menu-choice" }, { TA_UNDO_REDO, "undo-redo" }, { TA_ACTION, "action" }, + { TA_ACTIVATE, "activate" }, { 0, "" } }; diff --git a/common/tool/tool_manager.cpp b/common/tool/tool_manager.cpp index 1147304d5b..6bbc0cd51e 100644 --- a/common/tool/tool_manager.cpp +++ b/common/tool/tool_manager.cpp @@ -294,7 +294,7 @@ bool TOOL_MANAGER::invokeTool( TOOL_BASE* aTool ) { wxASSERT( aTool != NULL ); - TOOL_EVENT evt( TC_COMMAND, TA_ACTION, aTool->GetName() ); + TOOL_EVENT evt( TC_COMMAND, TA_ACTIVATE, aTool->GetName() ); ProcessEvent( evt ); return true; @@ -497,8 +497,11 @@ bool TOOL_MANAGER::dispatchStandardEvents( TOOL_EVENT& aEvent ) if( m_actionMgr->RunHotKey( aEvent.Modifier() | aEvent.KeyCode() ) ) return false; // hotkey event was handled so it does not go any further } - else if( aEvent.Category() == TC_COMMAND ) // it may be a tool activation event + 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 } @@ -509,14 +512,12 @@ bool TOOL_MANAGER::dispatchStandardEvents( TOOL_EVENT& aEvent ) bool TOOL_MANAGER::dispatchActivation( TOOL_EVENT& aEvent ) { - // Look for the tool that has the same name as parameter in the processed command TOOL_EVENT - BOOST_FOREACH( TOOL_STATE* st, m_toolState | boost::adaptors::map_values ) + std::map::iterator tool = m_toolNameIndex.find( *aEvent.m_commandStr ); + + if( tool != m_toolNameIndex.end() ) { - if( st->theTool->GetName() == aEvent.m_commandStr ) - { - runTool( st->theTool ); - return true; - } + runTool( tool->second->theTool ); + return true; } return false; diff --git a/include/tool/tool_action.h b/include/tool/tool_action.h index 152cf52928..c9d3510bc1 100644 --- a/include/tool/tool_action.h +++ b/include/tool/tool_action.h @@ -132,7 +132,6 @@ public: * Checks if the action has a hot key assigned. * * @return True if there is a hot key assigned, false otherwise. - * */ bool HasHotKey() const { @@ -141,14 +140,17 @@ public: /** * Function MakeEvent() - * Returns the event associated with the action (ie. the event that will be sent after + * Returns the event associated with the action (i.e. the event that will be sent after * activating the action). * * @return The event associated with the action. */ TOOL_EVENT MakeEvent() const { - return TOOL_EVENT( TC_COMMAND, TA_ACTION, m_name, m_scope ); + if( IsActivation() ) + return TOOL_EVENT( TC_COMMAND, TA_ACTIVATE, m_name, m_scope ); + else + return TOOL_EVENT( TC_COMMAND, TA_ACTION, m_name, m_scope ); } const std::string& GetMenuItem() const @@ -181,9 +183,15 @@ public: * stripped of the last part (e.g. for "pcbnew.InteractiveDrawing.drawCircle" it is * "pcbnew.InteractiveDrawing"). */ - std::string GetToolName() const + std::string GetToolName() const; + + /** + * Returns true if the action is intended to activate a tool. + */ + bool IsActivation() const { - return m_name.substr( 0, m_name.rfind( '.' ) ); + // Tool activation events are of format appName.toolName + return std::count( m_name.begin(), m_name.end(), '.' ) == 1; } private: diff --git a/include/tool/tool_event.h b/include/tool/tool_event.h index 49f8fe55f1..854e4d3b38 100644 --- a/include/tool/tool_event.h +++ b/include/tool/tool_event.h @@ -91,9 +91,12 @@ enum TOOL_ACTIONS // This event is sent *before* undo/redo command is performed. TA_UNDO_REDO = 0x10000, - // Tool action (allows to control tools) + // Tool action (allows to control tools). TA_ACTION = 0x20000, + // Tool activation event. + TA_ACTIVATE = 0x40000, + TA_ANY = 0xffffffff };