From b63bbdea5f302aab12bc9173c50559356d47e21e Mon Sep 17 00:00:00 2001 From: Ian McInerney Date: Mon, 26 Sep 2022 23:44:34 +0100 Subject: [PATCH] Add function-chain contructor for TOOL_ACTION A function-chain constructor method allows for the long list of parameters to the constructor to be shrunk to only the ones needed by each action, and allow self-documenting code for what each part of a TOOL_ACTION constructor does. Fixes: https://gitlab.com/kicad/code/kicad/-/issues/7617 --- common/tool/action_manager.cpp | 4 ++ common/tool/tool_action.cpp | 21 ++++++ include/tool/tool_action.h | 115 +++++++++++++++++++++++++++++++++ 3 files changed, 140 insertions(+) diff --git a/common/tool/action_manager.cpp b/common/tool/action_manager.cpp index 7f08cc50ab..6d164d21d8 100644 --- a/common/tool/action_manager.cpp +++ b/common/tool/action_manager.cpp @@ -44,6 +44,10 @@ ACTION_MANAGER::ACTION_MANAGER( TOOL_MANAGER* aToolManager ) : if( action->m_id == -1 ) action->m_id = MakeActionId( action->m_name ); + wxLogTrace( kicadTraceToolStack, + "ACTION_MANAGER::ACTION_MANAGER: Registering action %s with ID %d and UI ID %d", + action->m_name, action->m_id, action->GetUIId() ); + RegisterAction( action ); } } diff --git a/common/tool/tool_action.cpp b/common/tool/tool_action.cpp index a71053141e..8e87e33a7f 100644 --- a/common/tool/tool_action.cpp +++ b/common/tool/tool_action.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include TOOL_ACTION::TOOL_ACTION( const std::string& aName, TOOL_ACTION_SCOPE aScope, @@ -63,6 +64,26 @@ TOOL_ACTION::TOOL_ACTION() : } +TOOL_ACTION::TOOL_ACTION( const TOOL_ACTION_ARGS& aArgs ) : + m_name( aArgs.m_name.value_or( "" ) ), + m_scope( aArgs.m_scope.value_or( AS_CONTEXT ) ), + m_defaultHotKey( aArgs.m_defaultHotKey.value_or( 0 ) ), + m_hotKey( aArgs.m_defaultHotKey.value_or( 0 ) ), + m_legacyName( aArgs.m_legacyName.value_or( "" ) ), + m_label( aArgs.m_menuText.value_or( wxEmptyString ) ), + m_tooltip( aArgs.m_tooltip.value_or( wxEmptyString ) ), + m_icon( aArgs.m_icon.value_or( BITMAPS::INVALID_BITMAP) ), + m_id( -1 ), + m_flags( aArgs.m_flags.value_or( AF_NONE ) ), + m_param( aArgs.m_param.value_or( nullptr ) ) +{ + // Action name is the only mandatory part + assert( !m_name.empty() ); + + ACTION_MANAGER::GetActionList().push_back( this ); +} + + TOOL_ACTION::~TOOL_ACTION() { ACTION_MANAGER::GetActionList().remove( this ); diff --git a/include/tool/tool_action.h b/include/tool/tool_action.h index ddc7b9b87f..ce1a3c347a 100644 --- a/include/tool/tool_action.h +++ b/include/tool/tool_action.h @@ -30,6 +30,7 @@ #include #include +#include #include @@ -53,6 +54,119 @@ enum TOOL_ACTION_FLAGS AF_NOTIFY = 2 ///< Action is a notification (it is by default passed to all tools) }; +/** + * Build up the properties of a TOOL_ACTION in an incremental manner that is static-construction + * safe. + */ +class TOOL_ACTION_ARGS +{ +public: + TOOL_ACTION_ARGS() = default; + + /** + * The name of the action, the convention is "app.tool.actionName". + * + * This is a required property. + */ + TOOL_ACTION_ARGS& Name( std::string aName ) + { + m_name = aName; + return *this; + } + + /** + * The scope of the action. + */ + TOOL_ACTION_ARGS& Scope( TOOL_ACTION_SCOPE aScope ) + { + m_scope = aScope; + return *this; + } + + /** + * The default hotkey to assign to the action. + */ + TOOL_ACTION_ARGS& DefaultHotkey( int aDefaultHotkey ) + { + m_defaultHotKey = aDefaultHotkey; + return *this; + } + + /** + * The legacy hotkey name from the old system. + * + * This property is only needed for existing actions and shouldn't be used in new actions. + */ + TOOL_ACTION_ARGS& LegacyHotkeyName( std::string aLegacyName ) + { + m_legacyName = aLegacyName; + return *this; + } + + /** + *The string to use when displaying the action in a menu. + */ + TOOL_ACTION_ARGS& MenuText( wxString aMenuText ) + { + m_menuText = aMenuText; + return *this; + } + + /** + * The string to use as a tooltip for the action in menus and toolbars. + */ + TOOL_ACTION_ARGS& Tooltip( wxString aTooltip ) + { + m_tooltip = aTooltip; + return *this; + } + + /** + * The bitmap to use as the icon for the action in toolbars and menus. + */ + TOOL_ACTION_ARGS& Icon( BITMAPS aIcon ) + { + m_icon = aIcon; + return *this; + } + + /** + * Flags describing the type of the action. + */ + TOOL_ACTION_ARGS& Flags( TOOL_ACTION_FLAGS aFlags ) + { + m_flags = aFlags; + return *this; + } + + /** + * Custom parameter to pass information to the tool. + */ + TOOL_ACTION_ARGS& Parameter( void* aParam ) + { + m_param = aParam; + return *this; + } + +protected: + // Let the TOOL_ACTION constructor have direct access to the members here + friend class TOOL_ACTION; + + std::optional m_name; + std::optional m_scope; + std::optional m_flags; + + std::optional m_defaultHotKey; + std::optional m_legacyName; + + std::optional m_menuText; + std::optional m_tooltip; + + std::optional m_icon; + + std::optional m_param; +}; + /** * Represent a single user action. * @@ -67,6 +181,7 @@ enum TOOL_ACTION_FLAGS class TOOL_ACTION { public: + TOOL_ACTION( const TOOL_ACTION_ARGS& aArgs ); TOOL_ACTION( const std::string& aName, TOOL_ACTION_SCOPE aScope = AS_CONTEXT, int aDefaultHotKey = 0, const std::string& aLegacyHotKeyName = "", const wxString& aMenuText = wxEmptyString,