diff --git a/pcbnew/tools/edit_tool.cpp b/pcbnew/tools/edit_tool.cpp index fa082f6a31..250da3a075 100644 --- a/pcbnew/tools/edit_tool.cpp +++ b/pcbnew/tools/edit_tool.cpp @@ -334,10 +334,8 @@ int EDIT_TOOL::Main( const TOOL_EVENT& aEvent ) KIGFX::VIEW_CONTROLS* controls = getViewControls(); PCB_BASE_EDIT_FRAME* editFrame = getEditFrame(); VECTOR2I originalCursorPos = controls->GetCursorPosition(); - auto& existing_selection = m_selectionTool->GetSelection(); - if( !existing_selection.GetBoundingBox().Contains( originalCursorPos.x, originalCursorPos.y ) ) - m_toolMgr->RunAction( PCB_ACTIONS::selectionClear, true ); + m_selectionTool->ClearIfOutside( getViewControls()->GetCursorPosition() ); // Be sure that there is at least one item that we can modify. If nothing was selected before, // try looking for the stuff under mouse cursor (i.e. Kicad old-style hover selection) @@ -636,10 +634,8 @@ int EDIT_TOOL::Properties( const TOOL_EVENT& aEvent ) { PCB_BASE_EDIT_FRAME* editFrame = getEditFrame(); VECTOR2I originalCursorPos = getViewControls()->GetCursorPosition(); - auto& existing_selection = m_selectionTool->GetSelection(); - if( !existing_selection.GetBoundingBox().Contains( originalCursorPos.x, originalCursorPos.y ) ) - m_toolMgr->RunAction( PCB_ACTIONS::selectionClear, true ); + m_selectionTool->ClearIfOutside( getViewControls()->GetCursorPosition() ); const auto& selection = m_selectionTool->RequestSelection( []( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector ) @@ -681,11 +677,8 @@ int EDIT_TOOL::Properties( const TOOL_EVENT& aEvent ) int EDIT_TOOL::Rotate( const TOOL_EVENT& aEvent ) { PCB_BASE_EDIT_FRAME* editFrame = getEditFrame(); - VECTOR2I originalCursorPos = getViewControls()->GetCursorPosition(); - auto& existing_selection = m_selectionTool->GetSelection(); - if( !existing_selection.GetBoundingBox().Contains( originalCursorPos.x, originalCursorPos.y ) ) - m_toolMgr->RunAction( PCB_ACTIONS::selectionClear, true ); + m_selectionTool->ClearIfOutside( getViewControls()->GetCursorPosition() ); auto& selection = m_selectionTool->RequestSelection( []( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector ) @@ -768,11 +761,7 @@ static void mirrorPadX( D_PAD& aPad, const wxPoint& aMirrorPoint ) int EDIT_TOOL::Mirror( const TOOL_EVENT& aEvent ) { - VECTOR2I originalCursorPos = getViewControls()->GetCursorPosition(); - auto& existing_selection = m_selectionTool->GetSelection(); - - if( !existing_selection.GetBoundingBox().Contains( originalCursorPos.x, originalCursorPos.y ) ) - m_toolMgr->RunAction( PCB_ACTIONS::selectionClear, true ); + m_selectionTool->ClearIfOutside( getViewControls()->GetCursorPosition() ); auto& selection = m_selectionTool->RequestSelection( []( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector ) @@ -854,11 +843,7 @@ int EDIT_TOOL::Mirror( const TOOL_EVENT& aEvent ) int EDIT_TOOL::Flip( const TOOL_EVENT& aEvent ) { - VECTOR2I originalCursorPos = getViewControls()->GetCursorPosition(); - auto& existing_selection = m_selectionTool->GetSelection(); - - if( !existing_selection.GetBoundingBox().Contains( originalCursorPos.x, originalCursorPos.y ) ) - m_toolMgr->RunAction( PCB_ACTIONS::selectionClear, true ); + m_selectionTool->ClearIfOutside( getViewControls()->GetCursorPosition() ); auto& selection = m_selectionTool->RequestSelection( []( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector ) @@ -907,11 +892,7 @@ int EDIT_TOOL::Remove( const TOOL_EVENT& aEvent ) return 1; std::vector lockedItems; - VECTOR2I originalCursorPos = getViewControls()->GetCursorPosition(); - auto& existing_selection = m_selectionTool->GetSelection(); - - if( !existing_selection.GetBoundingBox().Contains( originalCursorPos.x, originalCursorPos.y ) ) - m_toolMgr->RunAction( PCB_ACTIONS::selectionClear, true ); + m_selectionTool->ClearIfOutside( getViewControls()->GetCursorPosition() ); // get a copy instead of reference (as we're going to clear the selection before removing items) PCBNEW_SELECTION selectionCopy; @@ -1021,11 +1002,7 @@ int EDIT_TOOL::Remove( const TOOL_EVENT& aEvent ) int EDIT_TOOL::MoveExact( const TOOL_EVENT& aEvent ) { - VECTOR2I originalCursorPos = getViewControls()->GetCursorPosition(); - auto& existing_selection = m_selectionTool->GetSelection(); - - if( !existing_selection.GetBoundingBox().Contains( originalCursorPos.x, originalCursorPos.y ) ) - m_toolMgr->RunAction( PCB_ACTIONS::selectionClear, true ); + m_selectionTool->ClearIfOutside( getViewControls()->GetCursorPosition() ); const auto& selection = m_selectionTool->RequestSelection( []( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector ) @@ -1102,11 +1079,7 @@ int EDIT_TOOL::MoveExact( const TOOL_EVENT& aEvent ) int EDIT_TOOL::Duplicate( const TOOL_EVENT& aEvent ) { bool increment = aEvent.IsAction( &PCB_ACTIONS::duplicateIncrement ); - VECTOR2I originalCursorPos = getViewControls()->GetCursorPosition(); - auto& existing_selection = m_selectionTool->GetSelection(); - - if( !existing_selection.GetBoundingBox().Contains( originalCursorPos.x, originalCursorPos.y ) ) - m_toolMgr->RunAction( PCB_ACTIONS::selectionClear, true ); + m_selectionTool->ClearIfOutside( getViewControls()->GetCursorPosition() ); // Be sure that there is at least one item that we can modify const auto& selection = m_selectionTool->RequestSelection( @@ -1184,11 +1157,7 @@ int EDIT_TOOL::Duplicate( const TOOL_EVENT& aEvent ) int EDIT_TOOL::CreateArray( const TOOL_EVENT& aEvent ) { - VECTOR2I originalCursorPos = getViewControls()->GetCursorPosition(); - auto& existing_selection = m_selectionTool->GetSelection(); - - if( !existing_selection.GetBoundingBox().Contains( originalCursorPos.x, originalCursorPos.y ) ) - m_toolMgr->RunAction( PCB_ACTIONS::selectionClear, true ); + m_selectionTool->ClearIfOutside( getViewControls()->GetCursorPosition() ); const auto& selection = m_selectionTool->RequestSelection( []( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector ) @@ -1354,11 +1323,7 @@ bool EDIT_TOOL::updateModificationPoint( PCBNEW_SELECTION& aSelection ) int EDIT_TOOL::EditFpInFpEditor( const TOOL_EVENT& aEvent ) { - VECTOR2I originalCursorPos = getViewControls()->GetCursorPosition(); - auto& existing_selection = m_selectionTool->GetSelection(); - - if( !existing_selection.GetBoundingBox().Contains( originalCursorPos.x, originalCursorPos.y ) ) - m_toolMgr->RunAction( PCB_ACTIONS::selectionClear, true ); + m_selectionTool->ClearIfOutside( getViewControls()->GetCursorPosition() ); const auto& selection = m_selectionTool->RequestSelection( FootprintFilter ); diff --git a/pcbnew/tools/selection_tool.cpp b/pcbnew/tools/selection_tool.cpp index 5653a1da6d..f93411dbad 100644 --- a/pcbnew/tools/selection_tool.cpp +++ b/pcbnew/tools/selection_tool.cpp @@ -386,6 +386,13 @@ PCBNEW_SELECTION& SELECTION_TOOL::GetSelection() } +void SELECTION_TOOL::ClearIfOutside( const VECTOR2I& aPt ) +{ + if( !m_selection.Empty() && !m_selection.GetBoundingBox().Contains( aPt.x, aPt.y ) ) + m_toolMgr->RunAction( PCB_ACTIONS::selectionClear, true ); +} + + PCBNEW_SELECTION& SELECTION_TOOL::RequestSelection( CLIENT_SELECTION_FILTER aClientFilter, std::vector* aFiltered, bool aConfirmLockedItems ) { diff --git a/pcbnew/tools/selection_tool.h b/pcbnew/tools/selection_tool.h index 8a824429a9..486151f2f5 100644 --- a/pcbnew/tools/selection_tool.h +++ b/pcbnew/tools/selection_tool.h @@ -97,6 +97,13 @@ public: PCBNEW_SELECTION& RequestSelection( CLIENT_SELECTION_FILTER aClientFilter, std::vector* aFiltered = nullptr, bool aConfirmLockedItems = false ); + /** + * Clears the selection if the selection Bounding Box doesn't fall within the given point + * + * @param aPt Point to check -- This is inclusive of the edge. + */ + void ClearIfOutside( const VECTOR2I& aPt ); + ///> Checks if the user has agreed to modify locked items for the given selection. SELECTION_LOCK_FLAGS CheckLock();