2013-09-20 16:21:01 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 CERN
|
|
|
|
* @author Maciej Suminski <maciej.suminski@cern.ch>
|
|
|
|
*
|
|
|
|
* 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 <tool/action_manager.h>
|
|
|
|
#include <tool/tool_manager.h>
|
|
|
|
#include <tool/tool_event.h>
|
|
|
|
#include <tool/tool_action.h>
|
2014-04-09 15:05:05 +00:00
|
|
|
#include <boost/foreach.hpp>
|
2013-09-26 16:38:58 +00:00
|
|
|
#include <cassert>
|
2013-09-20 16:21:01 +00:00
|
|
|
|
|
|
|
ACTION_MANAGER::ACTION_MANAGER( TOOL_MANAGER* aToolManager ) :
|
|
|
|
m_toolMgr( aToolManager )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-28 14:24:19 +00:00
|
|
|
ACTION_MANAGER::~ACTION_MANAGER()
|
|
|
|
{
|
|
|
|
while( !m_actionIdIndex.empty() )
|
|
|
|
UnregisterAction( m_actionIdIndex.begin()->second );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-20 16:21:01 +00:00
|
|
|
void ACTION_MANAGER::RegisterAction( TOOL_ACTION* aAction )
|
|
|
|
{
|
2014-04-09 15:05:05 +00:00
|
|
|
// Check if the TOOL_ACTION was not registered before
|
|
|
|
assert( aAction->GetId() == -1 );
|
|
|
|
// TOOL_ACTIONs are supposed to be named [appName.]toolName.actionName (with dots between)
|
|
|
|
// action name without specifying at least toolName is not valid
|
|
|
|
assert( aAction->GetName().find( '.', 0 ) != std::string::npos );
|
|
|
|
|
2014-05-14 14:29:53 +00:00
|
|
|
// TOOL_ACTIONs must have unique names & ids
|
2013-12-03 14:17:43 +00:00
|
|
|
assert( m_actionNameIndex.find( aAction->m_name ) == m_actionNameIndex.end() );
|
|
|
|
assert( m_actionIdIndex.find( aAction->m_id ) == m_actionIdIndex.end() );
|
2013-09-26 16:38:58 +00:00
|
|
|
|
|
|
|
aAction->setId( MakeActionId( aAction->m_name ) );
|
2013-09-20 16:21:01 +00:00
|
|
|
|
|
|
|
m_actionNameIndex[aAction->m_name] = aAction;
|
|
|
|
m_actionIdIndex[aAction->m_id] = aAction;
|
|
|
|
|
|
|
|
if( aAction->HasHotKey() )
|
2014-04-09 15:05:05 +00:00
|
|
|
m_actionHotKeys[aAction->m_currentHotKey].push_back( aAction );
|
2014-05-14 14:29:53 +00:00
|
|
|
|
|
|
|
aAction->setActionMgr( this );
|
2013-09-20 16:21:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ACTION_MANAGER::UnregisterAction( TOOL_ACTION* aAction )
|
|
|
|
{
|
2013-12-06 12:57:56 +00:00
|
|
|
m_actionNameIndex.erase( aAction->m_name );
|
|
|
|
m_actionIdIndex.erase( aAction->m_id );
|
|
|
|
|
2013-09-27 14:23:43 +00:00
|
|
|
// Indicate that the ACTION_MANAGER no longer care about the object
|
2014-05-14 14:29:53 +00:00
|
|
|
aAction->setActionMgr( NULL );
|
2013-09-27 14:23:43 +00:00
|
|
|
aAction->setId( -1 );
|
2013-09-20 16:21:01 +00:00
|
|
|
|
|
|
|
if( aAction->HasHotKey() )
|
2014-04-09 15:05:05 +00:00
|
|
|
{
|
|
|
|
std::list<TOOL_ACTION*>& actions = m_actionHotKeys[aAction->m_currentHotKey];
|
|
|
|
std::list<TOOL_ACTION*>::iterator action = std::find( actions.begin(), actions.end(), aAction );
|
|
|
|
|
|
|
|
if( action != actions.end() )
|
|
|
|
actions.erase( action );
|
|
|
|
else
|
|
|
|
assert( false );
|
|
|
|
}
|
2013-09-20 16:21:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int ACTION_MANAGER::MakeActionId( const std::string& aActionName )
|
|
|
|
{
|
2013-09-27 18:52:34 +00:00
|
|
|
static int currentActionId = 1;
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-09-20 16:21:01 +00:00
|
|
|
return currentActionId++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ACTION_MANAGER::RunAction( const std::string& aActionName ) const
|
|
|
|
{
|
|
|
|
std::map<std::string, TOOL_ACTION*>::const_iterator it = m_actionNameIndex.find( aActionName );
|
|
|
|
|
|
|
|
if( it == m_actionNameIndex.end() )
|
2013-10-14 14:13:35 +00:00
|
|
|
return false; // no action with given name found
|
2013-09-20 16:21:01 +00:00
|
|
|
|
2014-02-28 14:46:05 +00:00
|
|
|
RunAction( it->second );
|
2013-09-20 16:21:01 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-28 14:46:05 +00:00
|
|
|
void ACTION_MANAGER::RunAction( const TOOL_ACTION* aAction ) const
|
|
|
|
{
|
|
|
|
TOOL_EVENT event = aAction->MakeEvent();
|
|
|
|
|
|
|
|
m_toolMgr->ProcessEvent( event );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-20 16:21:01 +00:00
|
|
|
bool ACTION_MANAGER::RunHotKey( int aHotKey ) const
|
|
|
|
{
|
2014-04-09 15:33:22 +00:00
|
|
|
int key = std::toupper( aHotKey & ~MD_MODIFIER_MASK );
|
|
|
|
int mod = aHotKey & MD_MODIFIER_MASK;
|
2013-09-20 16:21:01 +00:00
|
|
|
|
2014-04-09 15:33:22 +00:00
|
|
|
HOTKEY_LIST::const_iterator it = m_actionHotKeys.find( key | mod );
|
|
|
|
|
|
|
|
// If no luck, try without modifier, to handle keys that require a modifier
|
|
|
|
// e.g. to get ? you need to press Shift+/ without US keyboard layout
|
|
|
|
// Hardcoding ? as Shift+/ is a bad idea, as on another layout you may need to press a
|
|
|
|
// different combination
|
2013-09-20 16:21:01 +00:00
|
|
|
if( it == m_actionHotKeys.end() )
|
2014-04-09 15:33:22 +00:00
|
|
|
{
|
|
|
|
it = m_actionHotKeys.find( key );
|
|
|
|
|
|
|
|
if( it == m_actionHotKeys.end() )
|
|
|
|
return false; // no appropriate action found for the hotkey
|
|
|
|
}
|
2013-09-20 16:21:01 +00:00
|
|
|
|
2014-04-09 15:05:05 +00:00
|
|
|
const std::list<TOOL_ACTION*>& actions = it->second;
|
2013-09-20 16:21:01 +00:00
|
|
|
|
2014-04-09 15:05:05 +00:00
|
|
|
// Choose the action that has the highest priority on the active tools stack
|
|
|
|
// If there is none, run the global action associated with the hot key
|
|
|
|
int highestPriority = -1, priority = -1;
|
|
|
|
const TOOL_ACTION* context = NULL; // pointer to context action of the highest priority tool
|
|
|
|
const TOOL_ACTION* global = NULL; // pointer to global action, if there is no context action
|
|
|
|
|
|
|
|
BOOST_FOREACH( const TOOL_ACTION* action, actions )
|
|
|
|
{
|
|
|
|
if( action->GetScope() == AS_GLOBAL )
|
|
|
|
{
|
|
|
|
// Store the global action for the hot key in case there was no possible
|
|
|
|
// context actions to run
|
|
|
|
assert( global == NULL ); // there should be only one global action per hot key
|
|
|
|
global = action;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
TOOL_BASE* tool = m_toolMgr->FindTool( action->GetToolName() );
|
|
|
|
|
|
|
|
if( tool )
|
|
|
|
{
|
|
|
|
priority = m_toolMgr->GetPriority( tool->GetId() );
|
|
|
|
|
|
|
|
if( priority >= 0 && priority > highestPriority )
|
|
|
|
{
|
|
|
|
highestPriority = priority;
|
|
|
|
context = action;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-20 16:21:01 +00:00
|
|
|
|
2014-04-09 15:05:05 +00:00
|
|
|
if( !global && !context ) // currently there is no valid action to run
|
|
|
|
return false;
|
2013-09-20 16:21:01 +00:00
|
|
|
|
2014-04-09 15:05:05 +00:00
|
|
|
if( context )
|
|
|
|
RunAction( context );
|
|
|
|
else if( global )
|
|
|
|
RunAction( global );
|
|
|
|
|
|
|
|
return true;
|
2013-12-05 13:48:44 +00:00
|
|
|
}
|