From 5c984aa0690be40363ac1995d715834eb313b694 Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Thu, 30 Apr 2015 10:46:01 +0200 Subject: [PATCH] Parametrized TOOL_ACTIONs. --- common/tool/tool_manager.cpp | 10 ++++++++-- include/tool/tool_action.h | 25 +++++++++++-------------- include/tool/tool_event.h | 23 +++++++++++++---------- pcbnew/tools/common_actions.cpp | 19 ++++++++++--------- pcbnew/tools/pcbnew_control.cpp | 21 ++------------------- pcbnew/tools/selection_tool.cpp | 4 ++-- 6 files changed, 46 insertions(+), 56 deletions(-) diff --git a/common/tool/tool_manager.cpp b/common/tool/tool_manager.cpp index ff6ada4725..db7a6013e8 100644 --- a/common/tool/tool_manager.cpp +++ b/common/tool/tool_manager.cpp @@ -303,7 +303,10 @@ bool TOOL_MANAGER::RunAction( const std::string& aActionName, bool aNow, void* a if( action ) { TOOL_EVENT event = action->MakeEvent(); - event.SetParameter( aParam ); + + // Allow to override the action parameter + if( aParam ) + event.SetParameter( aParam ); if( aNow ) ProcessEvent( event ); @@ -320,7 +323,10 @@ bool TOOL_MANAGER::RunAction( const std::string& aActionName, bool aNow, void* a void TOOL_MANAGER::RunAction( const TOOL_ACTION& aAction, bool aNow, void* aParam ) { TOOL_EVENT event = aAction.MakeEvent(); - event.SetParameter( aParam ); + + // Allow to override the action parameter + if( aParam ) + event.SetParameter( aParam ); if( aNow ) ProcessEvent( event ); diff --git a/include/tool/tool_action.h b/include/tool/tool_action.h index 032a4aefc7..ff9de67d5a 100644 --- a/include/tool/tool_action.h +++ b/include/tool/tool_action.h @@ -1,7 +1,7 @@ /* * This program source code file is part of KiCad, a free EDA CAD application. * - * Copyright (C) 2013 CERN + * Copyright (C) 2013-2015 CERN * @author Tomasz Wlostowski * @author Maciej Suminski * @@ -41,7 +41,7 @@ struct BITMAP_OPAQUE; * - running the DRC from the menu * and so on, and so forth.... * Action class groups all necessary properties of an action, including explanation, - * icons, hotkeys,.menu items, etc. + * icons, hotkeys, menu items, etc. */ class TOOL_ACTION { @@ -49,10 +49,10 @@ public: TOOL_ACTION( const std::string& aName, TOOL_ACTION_SCOPE aScope = AS_CONTEXT, int aDefaultHotKey = 0, const wxString aMenuItem = wxEmptyString, const wxString& aMenuDesc = wxEmptyString, const BITMAP_OPAQUE* aIcon = NULL, - TOOL_ACTION_FLAGS aFlags = AF_NONE ) : + TOOL_ACTION_FLAGS aFlags = AF_NONE, void* aParam = NULL ) : m_name( aName ), m_scope( aScope ), m_defaultHotKey( aDefaultHotKey ), - m_currentHotKey( aDefaultHotKey ), m_menuItem( aMenuItem ), - m_menuDescription( aMenuDesc ), m_icon( aIcon ), m_id( -1 ), m_flags( aFlags ) + m_currentHotKey( aDefaultHotKey ), m_menuItem( aMenuItem ), m_menuDescription( aMenuDesc ), + m_icon( aIcon ), m_id( -1 ), m_flags( aFlags ), m_param( aParam ) { TOOL_MANAGER::GetActionList().push_back( this ); } @@ -150,11 +150,11 @@ public: TOOL_EVENT MakeEvent() const { if( IsActivation() ) - return TOOL_EVENT( TC_COMMAND, TA_ACTIVATE, m_name, m_scope ); + return TOOL_EVENT( TC_COMMAND, TA_ACTIVATE, m_name, m_scope, m_param ); else if( IsNotification() ) - return TOOL_EVENT( TC_MESSAGE, TA_NONE, m_name, m_scope ); + return TOOL_EVENT( TC_MESSAGE, TA_NONE, m_name, m_scope, m_param ); else - return TOOL_EVENT( TC_COMMAND, TA_ACTION, m_name, m_scope ); + return TOOL_EVENT( TC_COMMAND, TA_ACTION, m_name, m_scope, m_param ); } const wxString& GetMenuItem() const @@ -219,7 +219,7 @@ private: /// Name of the action (convention is: app.[tool.]action.name) std::string m_name; - /// Scope of the action (i.e. the event that is issued after activation). + /// Scope of the action TOOL_ACTION_SCOPE m_scope; /// Default hot key that activates the action. @@ -243,11 +243,8 @@ private: /// Action flags TOOL_ACTION_FLAGS m_flags; - /// Origin of the action - // const TOOL_BASE* m_origin; - - /// Originating UI object - // wxWindow* m_uiOrigin; + /// Generic parameter + void* m_param; }; #endif diff --git a/include/tool/tool_event.h b/include/tool/tool_event.h index 435451b676..d7ead512ba 100644 --- a/include/tool/tool_event.h +++ b/include/tool/tool_event.h @@ -159,24 +159,24 @@ public: const std::string Format() const; TOOL_EVENT( TOOL_EVENT_CATEGORY aCategory = TC_NONE, TOOL_ACTIONS aAction = TA_NONE, - TOOL_ACTION_SCOPE aScope = AS_GLOBAL ) : + TOOL_ACTION_SCOPE aScope = AS_GLOBAL, void* aParameter = NULL ) : m_category( aCategory ), m_actions( aAction ), m_scope( aScope ), m_mouseButtons( 0 ), m_keyCode( 0 ), m_modifiers( 0 ), - m_param( NULL ) {} + m_param( aParameter ) {} TOOL_EVENT( TOOL_EVENT_CATEGORY aCategory, TOOL_ACTIONS aAction, int aExtraParam, - TOOL_ACTION_SCOPE aScope = AS_GLOBAL ) : + TOOL_ACTION_SCOPE aScope = AS_GLOBAL, void* aParameter = NULL ) : m_category( aCategory ), m_actions( aAction ), m_scope( aScope ), m_mouseButtons( 0 ), m_keyCode( 0 ), m_modifiers( 0 ), - m_param( NULL ) + m_param( aParameter ) { if( aCategory == TC_MOUSE ) { @@ -198,14 +198,15 @@ public: } TOOL_EVENT( TOOL_EVENT_CATEGORY aCategory, TOOL_ACTIONS aAction, - const std::string& aExtraParam, TOOL_ACTION_SCOPE aScope = AS_GLOBAL ) : + const std::string& aExtraParam, TOOL_ACTION_SCOPE aScope = AS_GLOBAL, + void* aParameter = NULL ) : m_category( aCategory ), m_actions( aAction ), m_scope( aScope ), m_mouseButtons( 0 ), m_keyCode( 0 ), m_modifiers( 0 ), - m_param( NULL ) + m_param( aParameter ) { if( aCategory == TC_COMMAND || aCategory == TC_MESSAGE ) m_commandStr = aExtraParam; @@ -360,9 +361,10 @@ public: * Returns a non-standard parameter assigned to the event. Its meaning depends on the * target tool. */ - void* Parameter() const + template + inline T Parameter() const { - return m_param; + return reinterpret_cast( m_param ); } /** @@ -371,9 +373,10 @@ public: * target tool. * @param aParam is the new parameter. */ - void SetParameter(void* aParam) + template + void SetParameter(T aParam) { - m_param = aParam; + m_param = (void*) aParam; } boost::optional GetCommandId() const diff --git a/pcbnew/tools/common_actions.cpp b/pcbnew/tools/common_actions.cpp index 599111c0e2..b6d2af06d1 100644 --- a/pcbnew/tools/common_actions.cpp +++ b/pcbnew/tools/common_actions.cpp @@ -1,7 +1,7 @@ /* * This program source code file is part of KiCad, a free EDA CAD application. * - * Copyright (C) 2013 CERN + * Copyright (C) 2013-2015 CERN * @author Maciej Suminski * * This program is free software; you can redistribute it and/or @@ -25,6 +25,7 @@ #include "common_actions.h" #include #include +#include #include // These members are static in class COMMON_ACTIONS: Build them here: @@ -246,35 +247,35 @@ TOOL_ACTION COMMON_ACTIONS::highContrastDec( "pcbnew.Control.highContrastDec", // Layer control TOOL_ACTION COMMON_ACTIONS::layerTop( "pcbnew.Control.layerTop", AS_GLOBAL, WXK_PAGEUP, - "", "" ); + "", "", NULL, AF_NONE, (void*) F_Cu ); TOOL_ACTION COMMON_ACTIONS::layerInner1( "pcbnew.Control.layerInner1", AS_GLOBAL, WXK_F5, - "", "" ); + "", "", NULL, AF_NONE, (void*) In1_Cu ); TOOL_ACTION COMMON_ACTIONS::layerInner2( "pcbnew.Control.layerInner2", AS_GLOBAL, WXK_F6, - "", "" ); + "", "", NULL, AF_NONE, (void*) In2_Cu ); TOOL_ACTION COMMON_ACTIONS::layerInner3( "pcbnew.Control.layerInner3", AS_GLOBAL, WXK_F7, - "", "" ); + "", "", NULL, AF_NONE, (void*) In3_Cu ); TOOL_ACTION COMMON_ACTIONS::layerInner4( "pcbnew.Control.layerInner4", AS_GLOBAL, WXK_F8, - "", "" ); + "", "", NULL, AF_NONE, (void*) In4_Cu ); TOOL_ACTION COMMON_ACTIONS::layerInner5( "pcbnew.Control.layerInner5", AS_GLOBAL, WXK_F9, - "", "" ); + "", "", NULL, AF_NONE, (void*) In5_Cu ); TOOL_ACTION COMMON_ACTIONS::layerInner6( "pcbnew.Control.layerInner6", AS_GLOBAL, WXK_F10, - "", "" ); + "", "", NULL, AF_NONE, (void*) In6_Cu ); TOOL_ACTION COMMON_ACTIONS::layerBottom( "pcbnew.Control.layerBottom", AS_GLOBAL, WXK_PAGEDOWN, - "", "" ); + "", "", NULL, AF_NONE, (void*) B_Cu ); TOOL_ACTION COMMON_ACTIONS::layerNext( "pcbnew.Control.layerNext", AS_GLOBAL, '+', diff --git a/pcbnew/tools/pcbnew_control.cpp b/pcbnew/tools/pcbnew_control.cpp index c32c728f7f..bc4b469ef3 100644 --- a/pcbnew/tools/pcbnew_control.cpp +++ b/pcbnew/tools/pcbnew_control.cpp @@ -285,23 +285,7 @@ int PCBNEW_CONTROL::HighContrastDec( const TOOL_EVENT& aEvent ) // Layer control int PCBNEW_CONTROL::LayerSwitch( const TOOL_EVENT& aEvent ) { - if( aEvent.IsAction( &COMMON_ACTIONS::layerTop ) ) - m_frame->SwitchLayer( NULL, F_Cu ); - else if( aEvent.IsAction( &COMMON_ACTIONS::layerInner1 ) ) - m_frame->SwitchLayer( NULL, In1_Cu ); - else if( aEvent.IsAction( &COMMON_ACTIONS::layerInner2 ) ) - m_frame->SwitchLayer( NULL, In2_Cu ); - else if( aEvent.IsAction( &COMMON_ACTIONS::layerInner3 ) ) - m_frame->SwitchLayer( NULL, In3_Cu ); - else if( aEvent.IsAction( &COMMON_ACTIONS::layerInner4 ) ) - m_frame->SwitchLayer( NULL, In4_Cu ); - else if( aEvent.IsAction( &COMMON_ACTIONS::layerInner5 ) ) - m_frame->SwitchLayer( NULL, In5_Cu ); - else if( aEvent.IsAction( &COMMON_ACTIONS::layerInner6 ) ) - m_frame->SwitchLayer( NULL, In6_Cu ); - else if( aEvent.IsAction( &COMMON_ACTIONS::layerBottom ) ) - m_frame->SwitchLayer( NULL, B_Cu ); - + m_frame->SwitchLayer( NULL, (LAYER_ID) aEvent.Parameter() ); setTransitions(); return 0; @@ -450,8 +434,7 @@ int PCBNEW_CONTROL::GridPrev( const TOOL_EVENT& aEvent ) int PCBNEW_CONTROL::GridSetOrigin( const TOOL_EVENT& aEvent ) { Activate(); - m_frame->SetToolID( ID_PCB_PLACE_GRID_COORD_BUTT, wxCURSOR_PENCIL, - _( "Adjust grid origin" ) ); + m_frame->SetToolID( ID_PCB_PLACE_GRID_COORD_BUTT, wxCURSOR_PENCIL, _( "Adjust grid origin" ) ); KIGFX::VIEW_CONTROLS* controls = getViewControls(); controls->ShowCursor( true ); diff --git a/pcbnew/tools/selection_tool.cpp b/pcbnew/tools/selection_tool.cpp index 6ea637506e..b6fc1ac055 100644 --- a/pcbnew/tools/selection_tool.cpp +++ b/pcbnew/tools/selection_tool.cpp @@ -509,7 +509,7 @@ int SELECTION_TOOL::ClearSelection( const TOOL_EVENT& aEvent ) int SELECTION_TOOL::SelectItem( const TOOL_EVENT& aEvent ) { // Check if there is an item to be selected - BOARD_ITEM* item = static_cast( aEvent.Parameter() ); + BOARD_ITEM* item = aEvent.Parameter(); if( item ) { @@ -527,7 +527,7 @@ int SELECTION_TOOL::SelectItem( const TOOL_EVENT& aEvent ) int SELECTION_TOOL::UnselectItem( const TOOL_EVENT& aEvent ) { // Check if there is an item to be selected - BOARD_ITEM* item = static_cast( aEvent.Parameter() ); + BOARD_ITEM* item = aEvent.Parameter(); if( item ) {