From 34fbde42fc539b473e5cad2a584670fc77285f76 Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Wed, 9 Jul 2014 13:50:27 +0200 Subject: [PATCH] Improved way of translating wxEvent commands to TOOL_ACTIONs. --- common/tool/tool_dispatcher.cpp | 23 +---- pcbnew/edit.cpp | 164 ++++++++++++++------------------ pcbnew/tools/common_actions.cpp | 37 ++++--- pcbnew/tools/common_actions.h | 5 +- 4 files changed, 99 insertions(+), 130 deletions(-) diff --git a/common/tool/tool_dispatcher.cpp b/common/tool/tool_dispatcher.cpp index 89c09a16ec..cd416f7c50 100644 --- a/common/tool/tool_dispatcher.cpp +++ b/common/tool/tool_dispatcher.cpp @@ -288,27 +288,10 @@ void TOOL_DISPATCHER::DispatchWxEvent( wxEvent& aEvent ) void TOOL_DISPATCHER::DispatchWxCommand( wxCommandEvent& aEvent ) { - boost::optional evt; - - switch( aEvent.GetId() ) - { - case ID_ZOOM_IN: // toolbar button "Zoom In" - evt = COMMON_ACTIONS::zoomInCenter.MakeEvent(); - break; - - case ID_ZOOM_OUT: // toolbar button "Zoom In" - evt = COMMON_ACTIONS::zoomOutCenter.MakeEvent(); - break; - - case ID_ZOOM_PAGE: // toolbar button "Fit on Screen" - evt = COMMON_ACTIONS::zoomFitScreen.MakeEvent(); - break; - - default: - aEvent.Skip(); - break; - } + boost::optional evt = COMMON_ACTIONS::TranslateLegacyId( aEvent.GetId() ); if( evt ) m_toolMgr->ProcessEvent( *evt ); + else + aEvent.Skip(); } diff --git a/pcbnew/edit.cpp b/pcbnew/edit.cpp index eb6bb85843..efb92a7064 100644 --- a/pcbnew/edit.cpp +++ b/pcbnew/edit.cpp @@ -1385,125 +1385,99 @@ void PCB_EDIT_FRAME::OnSelectTool( wxCommandEvent& aEvent ) if( GetToolId() == id ) return; - if( IsGalCanvasActive() ) + INSTALL_UNBUFFERED_DC( dc, m_canvas ); + + // Stop the current command and deselect the current tool. + m_canvas->EndMouseCapture( ID_NO_TOOL_SELECTED, m_canvas->GetDefaultCursor() ); + + switch( id ) { - std::string actionName = COMMON_ACTIONS::TranslateLegacyId( id ); + case ID_NO_TOOL_SELECTED: + SetToolID( id, m_canvas->GetDefaultCursor(), wxEmptyString ); + break; - if( !actionName.empty() || id == ID_NO_TOOL_SELECTED ) + case ID_TRACK_BUTT: + if( g_Drc_On ) + SetToolID( id, wxCURSOR_PENCIL, _( "Add tracks" ) ); + else + SetToolID( id, wxCURSOR_QUESTION_ARROW, _( "Add tracks" ) ); + + if( (GetBoard()->m_Status_Pcb & LISTE_RATSNEST_ITEM_OK) == 0 ) { - const int MAX_TRIALS = 10; - int trials = 0; - - // Cancel the current tool - // TODO while sending a lot of cancel events works for sure, it is not the most - // elegant way to cancel a tool, this should be probably done another way - while( m_toolManager->GetCurrentTool()->GetName() != "pcbnew.InteractiveSelection" && - trials++ < MAX_TRIALS ) - { - TOOL_EVENT cancel( TC_ANY, TA_CANCEL_TOOL ); - m_toolManager->ProcessEvent( cancel ); - } - - if( !actionName.empty() ) - m_toolManager->RunAction( actionName ); + Compile_Ratsnest( &dc, true ); } - } - else - { - INSTALL_UNBUFFERED_DC( dc, m_canvas ); - // Stop the current command and deselect the current tool. - m_canvas->EndMouseCapture( ID_NO_TOOL_SELECTED, m_canvas->GetDefaultCursor() ); + break; - switch( id ) - { - case ID_NO_TOOL_SELECTED: - SetToolID( id, m_canvas->GetDefaultCursor(), wxEmptyString ); - break; + case ID_PCB_MODULE_BUTT: + SetToolID( id, wxCURSOR_PENCIL, _( "Add module" ) ); + break; - case ID_TRACK_BUTT: - if( g_Drc_On ) - SetToolID( id, wxCURSOR_PENCIL, _( "Add tracks" ) ); - else - SetToolID( id, wxCURSOR_QUESTION_ARROW, _( "Add tracks" ) ); + case ID_PCB_ZONES_BUTT: + SetToolID( id, wxCURSOR_PENCIL, _( "Add zones" ) ); - if( (GetBoard()->m_Status_Pcb & LISTE_RATSNEST_ITEM_OK) == 0 ) - { - Compile_Ratsnest( &dc, true ); - } + if( DisplayOpt.DisplayZonesMode != 0 ) + DisplayInfoMessage( this, _( "Warning: zone display is OFF!!!" ) ); - break; + if( !GetBoard()->IsHighLightNetON() && (GetBoard()->GetHighLightNetCode() > 0 ) ) + HighLight( &dc ); - case ID_PCB_MODULE_BUTT: - SetToolID( id, wxCURSOR_PENCIL, _( "Add module" ) ); - break; + break; - case ID_PCB_ZONES_BUTT: - SetToolID( id, wxCURSOR_PENCIL, _( "Add zones" ) ); + case ID_PCB_KEEPOUT_AREA_BUTT: + SetToolID( id, wxCURSOR_PENCIL, _( "Add keepout" ) ); + break; - if( DisplayOpt.DisplayZonesMode != 0 ) - DisplayInfoMessage( this, _( "Warning: zone display is OFF!!!" ) ); + case ID_PCB_MIRE_BUTT: + SetToolID( id, wxCURSOR_PENCIL, _( "Add layer alignment target" ) ); + break; - if( !GetBoard()->IsHighLightNetON() && (GetBoard()->GetHighLightNetCode() > 0 ) ) - HighLight( &dc ); + case ID_PCB_PLACE_OFFSET_COORD_BUTT: + SetToolID( id, wxCURSOR_PENCIL, _( "Adjust zero" ) ); + break; - break; + case ID_PCB_PLACE_GRID_COORD_BUTT: + SetToolID( id, wxCURSOR_PENCIL, _( "Adjust grid origin" ) ); + break; - case ID_PCB_KEEPOUT_AREA_BUTT: - SetToolID( id, wxCURSOR_PENCIL, _( "Add keepout" ) ); - break; + case ID_PCB_ADD_LINE_BUTT: + SetToolID( id, wxCURSOR_PENCIL, _( "Add graphic line" ) ); + break; - case ID_PCB_MIRE_BUTT: - SetToolID( id, wxCURSOR_PENCIL, _( "Add layer alignment target" ) ); - break; + case ID_PCB_ARC_BUTT: + SetToolID( id, wxCURSOR_PENCIL, _( "Add graphic arc" ) ); + break; - case ID_PCB_PLACE_OFFSET_COORD_BUTT: - SetToolID( id, wxCURSOR_PENCIL, _( "Adjust zero" ) ); - break; + case ID_PCB_CIRCLE_BUTT: + SetToolID( id, wxCURSOR_PENCIL, _( "Add graphic circle" ) ); + break; - case ID_PCB_PLACE_GRID_COORD_BUTT: - SetToolID( id, wxCURSOR_PENCIL, _( "Adjust grid origin" ) ); - break; + case ID_PCB_ADD_TEXT_BUTT: + SetToolID( id, wxCURSOR_PENCIL, _( "Add text" ) ); + break; - case ID_PCB_ADD_LINE_BUTT: - SetToolID( id, wxCURSOR_PENCIL, _( "Add graphic line" ) ); - break; + case ID_COMPONENT_BUTT: + SetToolID( id, wxCURSOR_HAND, _( "Add module" ) ); + break; - case ID_PCB_ARC_BUTT: - SetToolID( id, wxCURSOR_PENCIL, _( "Add graphic arc" ) ); - break; + case ID_PCB_DIMENSION_BUTT: + SetToolID( id, wxCURSOR_PENCIL, _( "Add dimension" ) ); + break; - case ID_PCB_CIRCLE_BUTT: - SetToolID( id, wxCURSOR_PENCIL, _( "Add graphic circle" ) ); - break; + case ID_PCB_DELETE_ITEM_BUTT: + SetToolID( id, wxCURSOR_BULLSEYE, _( "Delete item" ) ); + break; - case ID_PCB_ADD_TEXT_BUTT: - SetToolID( id, wxCURSOR_PENCIL, _( "Add text" ) ); - break; + case ID_PCB_HIGHLIGHT_BUTT: + SetToolID( id, wxCURSOR_HAND, _( "Highlight net" ) ); + break; - case ID_COMPONENT_BUTT: - SetToolID( id, wxCURSOR_HAND, _( "Add module" ) ); - break; + case ID_PCB_SHOW_1_RATSNEST_BUTT: + SetToolID( id, wxCURSOR_HAND, _( "Select rats nest" ) ); - case ID_PCB_DIMENSION_BUTT: - SetToolID( id, wxCURSOR_PENCIL, _( "Add dimension" ) ); - break; + if( ( GetBoard()->m_Status_Pcb & LISTE_RATSNEST_ITEM_OK ) == 0 ) + Compile_Ratsnest( &dc, true ); - case ID_PCB_DELETE_ITEM_BUTT: - SetToolID( id, wxCURSOR_BULLSEYE, _( "Delete item" ) ); - break; - - case ID_PCB_HIGHLIGHT_BUTT: - SetToolID( id, wxCURSOR_HAND, _( "Highlight net" ) ); - break; - - case ID_PCB_SHOW_1_RATSNEST_BUTT: - SetToolID( id, wxCURSOR_HAND, _( "Select rats nest" ) ); - - if( ( GetBoard()->m_Status_Pcb & LISTE_RATSNEST_ITEM_OK ) == 0 ) - Compile_Ratsnest( &dc, true ); - - break; - } + break; } } diff --git a/pcbnew/tools/common_actions.cpp b/pcbnew/tools/common_actions.cpp index cd41f0e96c..856fcf81f6 100644 --- a/pcbnew/tools/common_actions.cpp +++ b/pcbnew/tools/common_actions.cpp @@ -269,46 +269,55 @@ TOOL_ACTION COMMON_ACTIONS::showHelp( "pcbnew.Control.showHelp", "", "" ); -std::string COMMON_ACTIONS::TranslateLegacyId( int aId ) +boost::optional COMMON_ACTIONS::TranslateLegacyId( int aId ) { switch( aId ) { case ID_PCB_MODULE_BUTT: - return COMMON_ACTIONS::placeModule.GetName(); + return COMMON_ACTIONS::placeModule.MakeEvent(); case ID_TRACK_BUTT: - return COMMON_ACTIONS::routerActivate.GetName(); + return COMMON_ACTIONS::routerActivate.MakeEvent(); case ID_PCB_ZONES_BUTT: - return COMMON_ACTIONS::drawZone.GetName(); + return COMMON_ACTIONS::drawZone.MakeEvent(); case ID_PCB_KEEPOUT_AREA_BUTT: - return COMMON_ACTIONS::drawKeepout.GetName(); + return COMMON_ACTIONS::drawKeepout.MakeEvent(); case ID_PCB_ADD_LINE_BUTT: - return COMMON_ACTIONS::drawLine.GetName(); + return COMMON_ACTIONS::drawLine.MakeEvent(); case ID_PCB_CIRCLE_BUTT: - return COMMON_ACTIONS::drawCircle.GetName(); + return COMMON_ACTIONS::drawCircle.MakeEvent(); case ID_PCB_ARC_BUTT: - return COMMON_ACTIONS::drawArc.GetName(); + return COMMON_ACTIONS::drawArc.MakeEvent(); case ID_PCB_ADD_TEXT_BUTT: - return COMMON_ACTIONS::placeTextPcb.GetName(); + return COMMON_ACTIONS::placeTextPcb.MakeEvent(); case ID_MODEDIT_TEXT_TOOL: - return COMMON_ACTIONS::placeTextModule.GetName(); + return COMMON_ACTIONS::placeTextModule.MakeEvent(); case ID_PCB_DIMENSION_BUTT: - return COMMON_ACTIONS::drawDimension.GetName(); + return COMMON_ACTIONS::drawDimension.MakeEvent(); case ID_PCB_MIRE_BUTT: - return COMMON_ACTIONS::placeTarget.GetName(); + return COMMON_ACTIONS::placeTarget.MakeEvent(); case ID_PCB_PLACE_GRID_COORD_BUTT: - return COMMON_ACTIONS::gridSetOrigin.GetName(); + return COMMON_ACTIONS::gridSetOrigin.MakeEvent(); + + case ID_ZOOM_IN: // toolbar button "Zoom In" + return COMMON_ACTIONS::zoomInCenter.MakeEvent(); + + case ID_ZOOM_OUT: // toolbar button "Zoom In" + return COMMON_ACTIONS::zoomOutCenter.MakeEvent(); + + case ID_ZOOM_PAGE: // toolbar button "Fit on Screen" + return COMMON_ACTIONS::zoomFitScreen.MakeEvent(); } - return ""; + return boost::optional(); } diff --git a/pcbnew/tools/common_actions.h b/pcbnew/tools/common_actions.h index 800a085f2a..ec98e65abd 100644 --- a/pcbnew/tools/common_actions.h +++ b/pcbnew/tools/common_actions.h @@ -23,6 +23,9 @@ */ #include +#include + +class TOOL_EVENT; /** * Class COMMON_ACTIONS @@ -153,5 +156,5 @@ public: * @return std::string is name of the corresponding TOOL_ACTION. It may be empty, if there is * no corresponding TOOL_ACTION. */ - static std::string TranslateLegacyId( int aId ); + static boost::optional TranslateLegacyId( int aId ); };