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
|
2020-12-27 00:41:04 +00:00
|
|
|
* Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
2013-09-20 16:21:01 +00:00
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ACTION_MANAGER_H_
|
|
|
|
#define ACTION_MANAGER_H_
|
|
|
|
|
2014-04-09 15:05:05 +00:00
|
|
|
#include <list>
|
2013-09-20 16:21:01 +00:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
2020-05-02 13:36:29 +00:00
|
|
|
#include <set>
|
2013-09-20 16:21:01 +00:00
|
|
|
|
2020-07-27 22:42:23 +00:00
|
|
|
#include <tool/selection_conditions.h>
|
|
|
|
|
2013-09-20 16:21:01 +00:00
|
|
|
class TOOL_BASE;
|
|
|
|
class TOOL_MANAGER;
|
|
|
|
class TOOL_ACTION;
|
|
|
|
|
2020-07-27 22:42:23 +00:00
|
|
|
/**
|
|
|
|
* Functors that can be used to figure out how the action controls should be displayed in the UI
|
|
|
|
* and if an action should be enabled given the current selection.
|
|
|
|
*
|
2020-12-27 00:41:04 +00:00
|
|
|
* @note @c checkCondition is also used for determining the state of a toggled toolbar item
|
2020-07-27 22:42:23 +00:00
|
|
|
* (the item is toggled when the condition is true).
|
|
|
|
*/
|
|
|
|
struct ACTION_CONDITIONS
|
|
|
|
{
|
|
|
|
ACTION_CONDITIONS()
|
|
|
|
{
|
|
|
|
checkCondition = SELECTION_CONDITIONS::ShowNever; // Never check by default
|
|
|
|
enableCondition = SELECTION_CONDITIONS::ShowAlways; // Always enable by default
|
|
|
|
showCondition = SELECTION_CONDITIONS::ShowAlways; // Always show by default
|
|
|
|
}
|
|
|
|
|
2020-08-05 22:00:26 +00:00
|
|
|
ACTION_CONDITIONS& Check( const SELECTION_CONDITION& aCondition )
|
2020-07-27 22:42:23 +00:00
|
|
|
{
|
|
|
|
checkCondition = aCondition;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-08-05 22:00:26 +00:00
|
|
|
ACTION_CONDITIONS& Enable( const SELECTION_CONDITION& aCondition )
|
2020-07-27 22:42:23 +00:00
|
|
|
{
|
|
|
|
enableCondition = aCondition;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-08-05 22:00:26 +00:00
|
|
|
ACTION_CONDITIONS& Show( const SELECTION_CONDITION& aCondition )
|
2020-07-27 22:42:23 +00:00
|
|
|
{
|
|
|
|
showCondition = aCondition;
|
|
|
|
return *this;
|
|
|
|
}
|
2020-12-27 00:41:04 +00:00
|
|
|
|
|
|
|
SELECTION_CONDITION checkCondition; ///< Returns true if the UI control should be checked
|
|
|
|
SELECTION_CONDITION enableCondition; ///< Returns true if the UI control should be enabled
|
|
|
|
SELECTION_CONDITION showCondition; ///< Returns true if the UI control should be shown
|
2020-07-27 22:42:23 +00:00
|
|
|
};
|
|
|
|
|
2013-09-27 16:51:21 +00:00
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Manage #TOOL_ACTION objects.
|
2013-09-27 16:51:21 +00:00
|
|
|
*
|
2020-12-27 00:41:04 +00:00
|
|
|
* Registering them and allows one to run them using associated hot keys, names or ids.
|
2013-09-27 16:51:21 +00:00
|
|
|
*/
|
2013-09-20 16:21:01 +00:00
|
|
|
class ACTION_MANAGER
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* @param aToolManager is a tool manager instance that is used to pass events to tools.
|
|
|
|
*/
|
|
|
|
ACTION_MANAGER( TOOL_MANAGER* aToolManager );
|
|
|
|
|
2013-11-28 14:24:19 +00:00
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Unregister every registered action.
|
2013-11-28 14:24:19 +00:00
|
|
|
*/
|
|
|
|
~ACTION_MANAGER();
|
|
|
|
|
2013-09-20 16:21:01 +00:00
|
|
|
/**
|
2021-06-09 19:32:58 +00:00
|
|
|
* Add a tool action to the manager and sets it up. After that it is possible to invoke
|
2013-09-27 14:23:43 +00:00
|
|
|
* the action using hotkeys or sending a command event with its name.
|
2020-07-27 22:42:23 +00:00
|
|
|
*
|
2013-09-20 16:21:01 +00:00
|
|
|
* @param aAction: action to be added. Ownership is not transferred.
|
|
|
|
*/
|
|
|
|
void RegisterAction( TOOL_ACTION* aAction );
|
|
|
|
|
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Generate an unique ID from for an action with given name.
|
2013-09-20 16:21:01 +00:00
|
|
|
*/
|
|
|
|
static int MakeActionId( const std::string& aActionName );
|
|
|
|
|
2019-06-09 21:57:23 +00:00
|
|
|
/**
|
|
|
|
* Get a list of currently-registered actions mapped by their name.
|
|
|
|
*/
|
2020-10-27 11:03:35 +00:00
|
|
|
const std::map<std::string, TOOL_ACTION*>& GetActions() const;
|
2019-06-09 21:57:23 +00:00
|
|
|
|
2022-09-27 21:03:58 +00:00
|
|
|
/**
|
|
|
|
* Test if a UI ID corresponds to an action ID in our system.
|
|
|
|
*/
|
|
|
|
bool IsActionUIId( int aId ) const;
|
|
|
|
|
2013-09-20 16:21:01 +00:00
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Find an action with a given name (if there is one available).
|
2020-07-27 22:42:23 +00:00
|
|
|
*
|
2014-07-09 14:25:50 +00:00
|
|
|
* @param aActionName is the searched action.
|
|
|
|
* @return Pointer to a TOOL_ACTION object or NULL if there is no such action.
|
2013-09-20 16:21:01 +00:00
|
|
|
*/
|
2014-07-09 14:25:50 +00:00
|
|
|
TOOL_ACTION* FindAction( const std::string& aActionName ) const;
|
2014-02-28 14:46:05 +00:00
|
|
|
|
2013-09-20 16:21:01 +00:00
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Run an action associated with a hotkey (if there is one available).
|
2020-07-27 22:42:23 +00:00
|
|
|
*
|
2013-12-05 13:48:44 +00:00
|
|
|
* @param aHotKey is the hotkey to be handled.
|
2013-09-20 16:21:01 +00:00
|
|
|
* @return True if there was an action associated with the hotkey, false otherwise.
|
|
|
|
*/
|
2020-06-23 10:21:16 +00:00
|
|
|
bool RunHotKey( int aHotKey ) const;
|
2013-09-20 16:21:01 +00:00
|
|
|
|
2015-07-28 08:29:00 +00:00
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Return the hot key associated with a given action or 0 if there is none.
|
2020-07-27 22:42:23 +00:00
|
|
|
*
|
2015-07-28 08:29:00 +00:00
|
|
|
* @param aAction is the queried action.
|
|
|
|
*/
|
|
|
|
int GetHotKey( const TOOL_ACTION& aAction ) const;
|
|
|
|
|
2015-05-05 18:39:41 +00:00
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Optionally read the hotkey config files and then rebuilds the internal hotkey maps.
|
2015-05-05 18:39:42 +00:00
|
|
|
*/
|
2019-06-12 10:22:23 +00:00
|
|
|
void UpdateHotKeys( bool aFullUpdate );
|
2015-05-05 18:39:42 +00:00
|
|
|
|
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Return list of TOOL_ACTIONs.
|
|
|
|
*
|
|
|
|
* #TOOL_ACTIONs add themselves to the list upon their creation.
|
|
|
|
*
|
2015-05-05 18:39:41 +00:00
|
|
|
* @return List of TOOL_ACTIONs.
|
|
|
|
*/
|
|
|
|
static std::list<TOOL_ACTION*>& GetActionList()
|
|
|
|
{
|
|
|
|
static std::list<TOOL_ACTION*> actionList;
|
|
|
|
|
|
|
|
return actionList;
|
|
|
|
}
|
|
|
|
|
2020-07-27 22:42:23 +00:00
|
|
|
/**
|
|
|
|
* Set the conditions the UI elements for activating a specific tool action should use
|
|
|
|
* for determining the current UI state (e.g. checked, enabled, shown)
|
|
|
|
*
|
2020-12-27 00:41:04 +00:00
|
|
|
* @param aAction is the tool action using these conditions.
|
|
|
|
* @param aConditions are the conditions to use for the action.
|
2020-07-27 22:42:23 +00:00
|
|
|
*/
|
|
|
|
void SetConditions( const TOOL_ACTION& aAction, const ACTION_CONDITIONS& aConditions );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the conditions to use for a specific tool action.
|
|
|
|
*
|
2020-12-27 00:41:04 +00:00
|
|
|
* @param aAction is the tool action.
|
|
|
|
* @return the action conditions, returns nullptr if no conditions are registered.
|
2020-07-27 22:42:23 +00:00
|
|
|
*/
|
|
|
|
const ACTION_CONDITIONS* GetCondition( const TOOL_ACTION& aAction ) const;
|
|
|
|
|
2013-09-20 16:21:01 +00:00
|
|
|
private:
|
2020-12-27 00:41:04 +00:00
|
|
|
// Resolve a hotkey by applying legacy and current settings over the action's
|
2019-06-09 21:57:23 +00:00
|
|
|
// default hotkey.
|
2022-06-02 23:35:19 +00:00
|
|
|
int processHotKey( TOOL_ACTION* aAction, const std::map<std::string, int>& aLegacyMap,
|
|
|
|
const std::map<std::string, int>& aHotKeyMap );
|
2015-05-05 18:39:42 +00:00
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< Tool manager needed to run actions
|
2013-09-20 16:21:01 +00:00
|
|
|
TOOL_MANAGER* m_toolMgr;
|
2013-09-27 14:23:43 +00:00
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< Map for indexing actions by their names
|
2013-09-20 16:21:01 +00:00
|
|
|
std::map<std::string, TOOL_ACTION*> m_actionNameIndex;
|
2013-09-27 14:23:43 +00:00
|
|
|
|
2022-09-27 21:03:58 +00:00
|
|
|
///< Map for recording actions that have custom UI IDs
|
|
|
|
std::map<int, TOOL_ACTION*> m_customUIIdIndex;
|
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< Map for indexing actions by their hotkeys
|
2014-04-09 15:05:05 +00:00
|
|
|
typedef std::map<int, std::list<TOOL_ACTION*> > HOTKEY_LIST;
|
|
|
|
HOTKEY_LIST m_actionHotKeys;
|
2015-07-28 08:29:00 +00:00
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< Quick action<->hot key lookup
|
2015-07-28 08:29:00 +00:00
|
|
|
std::map<int, int> m_hotkeys;
|
2020-07-27 22:42:23 +00:00
|
|
|
|
|
|
|
/// Map the command ID that wx uses for the action to the UI conditions for the
|
|
|
|
/// menu/toolbar items
|
|
|
|
std::map<int, ACTION_CONDITIONS> m_uiConditions;
|
2013-09-20 16:21:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* ACTION_MANAGER_H_ */
|