Skip to content

Commit

Permalink
Parametrized TOOL_ACTIONs.
Browse files Browse the repository at this point in the history
  • Loading branch information
msuminsk committed Apr 30, 2015
1 parent 89699a1 commit 5c984aa
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 56 deletions.
10 changes: 8 additions & 2 deletions common/tool/tool_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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 );
Expand Down
25 changes: 11 additions & 14 deletions include/tool/tool_action.h
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
* @author Maciej Suminski <[email protected]>
*
Expand Down Expand Up @@ -41,18 +41,18 @@ 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
{
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 );
}
Expand Down Expand Up @@ -150,11 +150,11 @@ class TOOL_ACTION
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
Expand Down Expand Up @@ -219,7 +219,7 @@ class TOOL_ACTION
/// 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.
Expand All @@ -243,11 +243,8 @@ class TOOL_ACTION
/// 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
23 changes: 13 additions & 10 deletions include/tool/tool_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,24 +159,24 @@ class TOOL_EVENT
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 )
{
Expand All @@ -198,14 +198,15 @@ class TOOL_EVENT
}

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;
Expand Down Expand Up @@ -360,9 +361,10 @@ class TOOL_EVENT
* Returns a non-standard parameter assigned to the event. Its meaning depends on the
* target tool.
*/
void* Parameter() const
template<typename T>
inline T Parameter() const
{
return m_param;
return reinterpret_cast<T>( m_param );
}

/**
Expand All @@ -371,9 +373,10 @@ class TOOL_EVENT
* target tool.
* @param aParam is the new parameter.
*/
void SetParameter(void* aParam)
template<typename T>
void SetParameter(T aParam)
{
m_param = aParam;
m_param = (void*) aParam;
}

boost::optional<int> GetCommandId() const
Expand Down
19 changes: 10 additions & 9 deletions pcbnew/tools/common_actions.cpp
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
*
* This program is free software; you can redistribute it and/or
Expand All @@ -25,6 +25,7 @@
#include "common_actions.h"
#include <tool/action_manager.h>
#include <pcbnew_id.h>
#include <layers_id_colors_and_visibility.h>
#include <wx/defs.h>

// These members are static in class COMMON_ACTIONS: Build them here:
Expand Down Expand Up @@ -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, '+',
Expand Down
21 changes: 2 additions & 19 deletions pcbnew/tools/pcbnew_control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<long>() );
setTransitions();

return 0;
Expand Down Expand Up @@ -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 );
Expand Down
4 changes: 2 additions & 2 deletions pcbnew/tools/selection_tool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<BOARD_ITEM*>( aEvent.Parameter() );
BOARD_ITEM* item = aEvent.Parameter<BOARD_ITEM*>();

if( item )
{
Expand All @@ -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<BOARD_ITEM*>( aEvent.Parameter() );
BOARD_ITEM* item = aEvent.Parameter<BOARD_ITEM*>();

if( item )
{
Expand Down

0 comments on commit 5c984aa

Please sign in to comment.