diff --git a/common/tool/action_manager.cpp b/common/tool/action_manager.cpp new file mode 100644 index 0000000000..2cd67b8464 --- /dev/null +++ b/common/tool/action_manager.cpp @@ -0,0 +1,106 @@ +/* + * 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 +#include +#include +#include + +ACTION_MANAGER::ACTION_MANAGER( TOOL_MANAGER* aToolManager ) : + m_toolMgr( aToolManager ) +{ +} + + +ACTION_MANAGER::~ACTION_MANAGER() +{ +} + + +void ACTION_MANAGER::RegisterAction( TOOL_ACTION* aAction ) +{ + int aId = MakeActionId( aAction->m_name ); + aAction->setId( aId ); + + m_actionNameIndex[aAction->m_name] = aAction; + m_actionIdIndex[aAction->m_id] = aAction; + + if( aAction->HasHotKey() ) + m_actionHotKeys[aAction->m_currentHotKey] = aAction; + + aAction->setActionMgr( this ); +} + + +void ACTION_MANAGER::UnregisterAction( TOOL_ACTION* aAction ) +{ + // Indicate that we no longer care about the object + aAction->setActionMgr( NULL ); + + m_actionNameIndex.erase( aAction->m_name ); + m_actionIdIndex.erase( aAction->m_id ); + + if( aAction->HasHotKey() ) + m_actionHotKeys.erase( aAction->m_currentHotKey ); +} + + +int ACTION_MANAGER::MakeActionId( const std::string& aActionName ) +{ + static int currentActionId = 0; + return currentActionId++; +} + + +bool ACTION_MANAGER::RunAction( const std::string& aActionName ) const +{ + std::map::const_iterator it = m_actionNameIndex.find( aActionName ); + + if( it == m_actionNameIndex.end() ) + return false; + + runAction( it->second ); + + return true; +} + + +bool ACTION_MANAGER::RunHotKey( int aHotKey ) const +{ + std::map::const_iterator it = m_actionHotKeys.find( aHotKey ); + + if( it == m_actionHotKeys.end() ) + return false; + + runAction( it->second ); + + return true; +} + + +void ACTION_MANAGER::runAction( const TOOL_ACTION* aAction ) const +{ + TOOL_EVENT event = aAction->GetEvent(); + m_toolMgr->ProcessEvent( event ); +} diff --git a/include/tool/action_manager.h b/include/tool/action_manager.h new file mode 100644 index 0000000000..c21d8f33ae --- /dev/null +++ b/include/tool/action_manager.h @@ -0,0 +1,100 @@ +/* + * 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 + */ + +#ifndef ACTION_MANAGER_H_ +#define ACTION_MANAGER_H_ + +#include +#include + +class TOOL_BASE; +class TOOL_MANAGER; +class TOOL_ACTION; + +class ACTION_MANAGER +{ +public: + /** + * Constructor. + * @param aToolManager is a tool manager instance that is used to pass events to tools. + */ + ACTION_MANAGER( TOOL_MANAGER* aToolManager ); + ~ACTION_MANAGER(); + + /** + * Function RegisterAction() + * Adds a tool action to the manager set and sets it up. After that is is possible to invoke + * the action using hotkeys or its name. + * @param aAction: action to be added. Ownership is not transferred. + */ + void RegisterAction( TOOL_ACTION* aAction ); + + /** + * Function UnregisterAction() + * Removes a tool action from the manager set and makes it unavailable for further usage. + * @param aAction: action to be removed. + */ + void UnregisterAction( TOOL_ACTION* aAction ); + + /** + * Generates an unique ID from for a tool with given name. + */ + static int MakeActionId( const std::string& aActionName ); + + /** + * Function RunAction() + * Runs an action with a given name (if there is one available). + * @param aActionName is the name of action to be run. + * @return True if there was an action associated with the name, false otherwise. + */ + bool RunAction( const std::string& aActionName ) const; + + // TODO to be considered + //bool RunAction( int aActionId ) const; + //bool RunAction( TOOL_ACTION* aAction ) const; + + /** + * Function RunHotKey() + * Runs an action associated with a hotkey (if there is one available). + * @param aHotKey is the hotkey to be served. + * @return True if there was an action associated with the hotkey, false otherwise. + */ + bool RunHotKey( int aHotKey ) const; + +private: + TOOL_MANAGER* m_toolMgr; + std::map m_actionIdIndex; + std::map m_actionNameIndex; + std::map m_actionHotKeys; + + /** + * Function runAction() + * Prepares an appropriate event and sends it to the destination specified in a TOOL_ACTION + * object. + * @param aAction is the action to be run. + */ + void runAction( const TOOL_ACTION* aAction ) const; +}; + +#endif /* ACTION_MANAGER_H_ */