From 4c5afc46afedc15366ffa5d62a021eb4fea38145 Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Fri, 4 Apr 2014 11:50:15 +0200 Subject: [PATCH] Added possibility of switching tools by the right toolbar buttons, without deactivating the current tool first (previously tools did not switch if there was one active). --- include/tool/tool_manager.h | 20 ++++ pcbnew/basepcbframe.cpp | 4 +- pcbnew/edit.cpp | 211 ++++++++++++++++++---------------- pcbnew/router/router_tool.cpp | 3 +- pcbnew/tools/drawing_tool.cpp | 14 +++ pcbnew/tools/pcb_tools.cpp | 2 +- 6 files changed, 152 insertions(+), 102 deletions(-) diff --git a/include/tool/tool_manager.h b/include/tool/tool_manager.h index 1103a7f890..7689a9a3c8 100644 --- a/include/tool/tool_manager.h +++ b/include/tool/tool_manager.h @@ -186,6 +186,26 @@ public: return m_editFrame; } + /** + * Returns id of the tool that is on the top of the active tools stack + * (was invoked the most recently). + * @return Id of the currently used tool. + */ + int GetCurrentToolId() const + { + return m_activeTools.front(); + } + + /** + * Returns the tool that is on the top of the active tools stack + * (was invoked the most recently). + * @return Pointer to the currently used tool. + */ + TOOL_BASE* GetCurrentTool() const + { + return FindTool( GetCurrentToolId() ); + } + /** * Defines a state transition - the events that cause a given handler method in the tool * to be called. Called by TOOL_INTERACTIVE::Go(). May be called from a coroutine context. diff --git a/pcbnew/basepcbframe.cpp b/pcbnew/basepcbframe.cpp index 0e93394930..76c31e62a6 100644 --- a/pcbnew/basepcbframe.cpp +++ b/pcbnew/basepcbframe.cpp @@ -633,7 +633,7 @@ void PCB_BASE_FRAME::SetToolID( int aId, int aCursor, const wxString& aToolMsg ) // handle color changes for transitions in and out of ID_TRACK_BUTT if( ( GetToolId() == ID_TRACK_BUTT && aId != ID_TRACK_BUTT ) - || ( GetToolId() != ID_TRACK_BUTT && aId== ID_TRACK_BUTT ) ) + || ( GetToolId() != ID_TRACK_BUTT && aId == ID_TRACK_BUTT ) ) { if( DisplayOpt.ContrastModeDisplay ) redraw = true; @@ -641,7 +641,7 @@ void PCB_BASE_FRAME::SetToolID( int aId, int aCursor, const wxString& aToolMsg ) // must do this after the tool has been set, otherwise pad::Draw() does // not show proper color when DisplayOpt.ContrastModeDisplay is true. - if( redraw && m_canvas) + if( redraw && m_canvas ) m_canvas->Refresh(); } diff --git a/pcbnew/edit.cpp b/pcbnew/edit.cpp index eff1aa15b5..0fbe391318 100644 --- a/pcbnew/edit.cpp +++ b/pcbnew/edit.cpp @@ -1370,109 +1370,124 @@ void PCB_EDIT_FRAME::OnSelectTool( wxCommandEvent& aEvent ) if( GetToolId() == id ) return; - 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 ) - { - case ID_NO_TOOL_SELECTED: - SetToolID( id, m_canvas->GetDefaultCursor(), wxEmptyString ); - break; - - 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 ) - { - Compile_Ratsnest( &dc, true ); - } - - break; - - case ID_PCB_MODULE_BUTT: - SetToolID( id, wxCURSOR_PENCIL, _( "Add module" ) ); - break; - - case ID_PCB_ZONES_BUTT: - SetToolID( id, wxCURSOR_PENCIL, _( "Add zones" ) ); - - if( DisplayOpt.DisplayZonesMode != 0 ) - DisplayInfoMessage( this, _( "Warning: zone display is OFF!!!" ) ); - - if( !GetBoard()->IsHighLightNetON() && (GetBoard()->GetHighLightNetCode() > 0 ) ) - HighLight( &dc ); - - break; - - case ID_PCB_KEEPOUT_AREA_BUTT: - SetToolID( id, wxCURSOR_PENCIL, _( "Add keepout" ) ); - break; - - case ID_PCB_MIRE_BUTT: - SetToolID( id, wxCURSOR_PENCIL, _( "Add layer alignment target" ) ); - break; - - case ID_PCB_PLACE_OFFSET_COORD_BUTT: - SetToolID( id, wxCURSOR_PENCIL, _( "Adjust zero" ) ); - break; - - case ID_PCB_PLACE_GRID_COORD_BUTT: - SetToolID( id, wxCURSOR_PENCIL, _( "Adjust grid origin" ) ); - break; - - case ID_PCB_ADD_LINE_BUTT: - SetToolID( id, wxCURSOR_PENCIL, _( "Add graphic line" ) ); - break; - - case ID_PCB_ARC_BUTT: - SetToolID( id, wxCURSOR_PENCIL, _( "Add graphic arc" ) ); - break; - - case ID_PCB_CIRCLE_BUTT: - SetToolID( id, wxCURSOR_PENCIL, _( "Add graphic circle" ) ); - break; - - case ID_PCB_ADD_TEXT_BUTT: - SetToolID( id, wxCURSOR_PENCIL, _( "Add text" ) ); - break; - - case ID_COMPONENT_BUTT: - SetToolID( id, wxCURSOR_HAND, _( "Add module" ) ); - break; - - case ID_PCB_DIMENSION_BUTT: - SetToolID( id, wxCURSOR_PENCIL, _( "Add dimension" ) ); - break; - - 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; - } - if( IsGalCanvasActive() ) { std::string actionName = COMMON_ACTIONS::TranslateLegacyId( id ); if( !actionName.empty() ) - m_toolManager.RunAction( actionName ); + { + const int MAX_TRIALS = 10; + int trials = 0; - return; + // 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 ); + } + + m_toolManager.RunAction( actionName ); + } + } + 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() ); + + switch( id ) + { + case ID_NO_TOOL_SELECTED: + SetToolID( id, m_canvas->GetDefaultCursor(), wxEmptyString ); + break; + + 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 ) + { + Compile_Ratsnest( &dc, true ); + } + + break; + + case ID_PCB_MODULE_BUTT: + SetToolID( id, wxCURSOR_PENCIL, _( "Add module" ) ); + break; + + case ID_PCB_ZONES_BUTT: + SetToolID( id, wxCURSOR_PENCIL, _( "Add zones" ) ); + + if( DisplayOpt.DisplayZonesMode != 0 ) + DisplayInfoMessage( this, _( "Warning: zone display is OFF!!!" ) ); + + if( !GetBoard()->IsHighLightNetON() && (GetBoard()->GetHighLightNetCode() > 0 ) ) + HighLight( &dc ); + + break; + + case ID_PCB_KEEPOUT_AREA_BUTT: + SetToolID( id, wxCURSOR_PENCIL, _( "Add keepout" ) ); + break; + + case ID_PCB_MIRE_BUTT: + SetToolID( id, wxCURSOR_PENCIL, _( "Add layer alignment target" ) ); + break; + + case ID_PCB_PLACE_OFFSET_COORD_BUTT: + SetToolID( id, wxCURSOR_PENCIL, _( "Adjust zero" ) ); + break; + + case ID_PCB_PLACE_GRID_COORD_BUTT: + SetToolID( id, wxCURSOR_PENCIL, _( "Adjust grid origin" ) ); + break; + + case ID_PCB_ADD_LINE_BUTT: + SetToolID( id, wxCURSOR_PENCIL, _( "Add graphic line" ) ); + break; + + case ID_PCB_ARC_BUTT: + SetToolID( id, wxCURSOR_PENCIL, _( "Add graphic arc" ) ); + break; + + case ID_PCB_CIRCLE_BUTT: + SetToolID( id, wxCURSOR_PENCIL, _( "Add graphic circle" ) ); + break; + + case ID_PCB_ADD_TEXT_BUTT: + SetToolID( id, wxCURSOR_PENCIL, _( "Add text" ) ); + break; + + case ID_COMPONENT_BUTT: + SetToolID( id, wxCURSOR_HAND, _( "Add module" ) ); + break; + + case ID_PCB_DIMENSION_BUTT: + SetToolID( id, wxCURSOR_PENCIL, _( "Add dimension" ) ); + break; + + 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; + } } } diff --git a/pcbnew/router/router_tool.cpp b/pcbnew/router/router_tool.cpp index 88e797b92b..2608b2d21e 100644 --- a/pcbnew/router/router_tool.cpp +++ b/pcbnew/router/router_tool.cpp @@ -27,6 +27,7 @@ #include #include +#include #include #include #include @@ -432,6 +433,7 @@ int ROUTER_TOOL::Main( TOOL_EVENT& aEvent ) // SetContextMenu ( m_menu ); // setMsgPanel(true, 0, wxT("KiRouter"), wxT("Pick an item to start routing")); + getEditFrame()->SetToolID( ID_TRACK_BUTT, wxCURSOR_PENCIL, _( "Add tracks" ) ); ctls->SetSnapping( true ); ctls->ShowCursor( true ); @@ -469,4 +471,3 @@ int ROUTER_TOOL::Main( TOOL_EVENT& aEvent ) return 0; } - diff --git a/pcbnew/tools/drawing_tool.cpp b/pcbnew/tools/drawing_tool.cpp index b0155f0f1f..1d85ec6906 100644 --- a/pcbnew/tools/drawing_tool.cpp +++ b/pcbnew/tools/drawing_tool.cpp @@ -28,6 +28,7 @@ #include #include +#include #include #include @@ -69,12 +70,16 @@ void DRAWING_TOOL::Reset( RESET_REASON aReason ) int DRAWING_TOOL::DrawLine( TOOL_EVENT& aEvent ) { + m_frame->SetToolID( ID_PCB_ADD_LINE_BUTT, wxCURSOR_PENCIL, _( "Add graphic line" ) ); + return drawSegment( S_SEGMENT, true ); } int DRAWING_TOOL::DrawCircle( TOOL_EVENT& aEvent ) { + m_frame->SetToolID( ID_PCB_CIRCLE_BUTT, wxCURSOR_PENCIL, _( "Add graphic circle" ) ); + return drawSegment( S_CIRCLE, false ); } @@ -100,6 +105,7 @@ int DRAWING_TOOL::DrawArc( TOOL_EVENT& aEvent ) m_controls->SetSnapping( true ); Activate(); + m_frame->SetToolID( ID_PCB_ARC_BUTT, wxCURSOR_PENCIL, _( "Add graphic arc" ) ); enum ARC_STEPS { @@ -286,6 +292,7 @@ int DRAWING_TOOL::PlaceText( TOOL_EVENT& aEvent ) m_controls->SetAutoPan( true ); Activate(); + m_frame->SetToolID( ID_PCB_ADD_TEXT_BUTT, wxCURSOR_PENCIL, _( "Add text" ) ); // Main loop: keep receiving events while( OPT_TOOL_EVENT evt = Wait() ) @@ -390,6 +397,7 @@ int DRAWING_TOOL::DrawDimension( TOOL_EVENT& aEvent ) m_controls->SetSnapping( true ); Activate(); + m_frame->SetToolID( ID_PCB_DIMENSION_BUTT, wxCURSOR_PENCIL, _( "Add dimension" ) ); enum DIMENSION_STEPS { @@ -549,12 +557,16 @@ int DRAWING_TOOL::DrawDimension( TOOL_EVENT& aEvent ) int DRAWING_TOOL::DrawZone( TOOL_EVENT& aEvent ) { + m_frame->SetToolID( ID_PCB_ZONES_BUTT, wxCURSOR_PENCIL, _( "Add zones" ) ); + return drawZone( false ); } int DRAWING_TOOL::DrawKeepout( TOOL_EVENT& aEvent ) { + m_frame->SetToolID( ID_PCB_KEEPOUT_AREA_BUTT, wxCURSOR_PENCIL, _( "Add keepout" ) ); + return drawZone( true ); } @@ -581,6 +593,7 @@ int DRAWING_TOOL::PlaceTarget( TOOL_EVENT& aEvent ) m_controls->SetAutoPan( true ); Activate(); + m_frame->SetToolID( ID_PCB_MIRE_BUTT, wxCURSOR_PENCIL, _( "Add layer alignment target" ) ); // Main loop: keep receiving events while( OPT_TOOL_EVENT evt = Wait() ) @@ -656,6 +669,7 @@ int DRAWING_TOOL::PlaceModule( TOOL_EVENT& aEvent ) m_controls->SetAutoPan( true ); Activate(); + m_frame->SetToolID( ID_PCB_MODULE_BUTT, wxCURSOR_HAND, _( "Add module" ) ); // Main loop: keep receiving events while( OPT_TOOL_EVENT evt = Wait() ) diff --git a/pcbnew/tools/pcb_tools.cpp b/pcbnew/tools/pcb_tools.cpp index 4d618615dc..154ea93d46 100644 --- a/pcbnew/tools/pcb_tools.cpp +++ b/pcbnew/tools/pcb_tools.cpp @@ -68,7 +68,7 @@ void PCB_EDIT_FRAME::setupTools() m_toolManager.RegisterTool( new PCBNEW_CONTROL ); m_toolManager.SetEnvironment( NULL, GetGalCanvas()->GetView(), - GetGalCanvas()->GetViewControls(), this ); + GetGalCanvas()->GetViewControls(), this ); m_toolManager.ResetTools( TOOL_BASE::RUN ); // Run the selection tool, it is supposed to be always active