diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index e44fc6c3f6..d4d7113ca5 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -431,6 +431,7 @@ set( COMMON_SRCS tool/edit_constraints.cpp tool/edit_points.cpp tool/editor_conditions.cpp + tool/grid_helper.cpp tool/grid_menu.cpp tool/picker_tool.cpp tool/selection_conditions.cpp @@ -560,7 +561,7 @@ set( PCB_COMMON_SRCS ${CMAKE_SOURCE_DIR}/pcbnew/sel_layer.cpp ${CMAKE_SOURCE_DIR}/pcbnew/zone_settings.cpp - ${CMAKE_SOURCE_DIR}/pcbnew/tools/grid_helper.cpp + ${CMAKE_SOURCE_DIR}/pcbnew/tools/pcb_grid_helper.cpp ${CMAKE_SOURCE_DIR}/pcbnew/tools/pcb_actions.cpp ${CMAKE_SOURCE_DIR}/pcbnew/tools/pcb_editor_conditions.cpp ${CMAKE_SOURCE_DIR}/pcbnew/tools/pcb_viewer_tools.cpp diff --git a/common/tool/grid_helper.cpp b/common/tool/grid_helper.cpp new file mode 100644 index 0000000000..5d91ab3419 --- /dev/null +++ b/common/tool/grid_helper.cpp @@ -0,0 +1,113 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2018-2021 KiCad Developers, see AUTHORS.txt for contributors. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, you may find one here: + * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html + * or you may search the http://www.gnu.org website for the version 2 license, + * or you may write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + + +#include +using namespace std::placeholders; + +#include // for KiROUND +#include +#include +#include +#include + + +GRID_HELPER::GRID_HELPER( TOOL_MANAGER* aToolMgr ) : + m_toolMgr( aToolMgr ) +{ + m_enableSnap = true; + m_enableSnapLine = true; + m_enableGrid = true; + m_snapItem = nullptr; +} + + +GRID_HELPER::~GRID_HELPER() +{ +} + + +VECTOR2I GRID_HELPER::GetGrid() const +{ + VECTOR2D size = m_toolMgr->GetView()->GetGAL()->GetGridSize(); + + return VECTOR2I( KiROUND( size.x ), KiROUND( size.y ) ); +} + + +VECTOR2I GRID_HELPER::GetOrigin() const +{ + VECTOR2D origin = m_toolMgr->GetView()->GetGAL()->GetGridOrigin(); + + return VECTOR2I( origin ); +} + + +void GRID_HELPER::SetAuxAxes( bool aEnable, const VECTOR2I& aOrigin ) +{ + if( aEnable ) + { + m_auxAxis = aOrigin; + m_viewAxis.SetPosition( wxPoint( aOrigin ) ); + m_toolMgr->GetView()->SetVisible( &m_viewAxis, true ); + } + else + { + m_auxAxis = OPT(); + m_toolMgr->GetView()->SetVisible( &m_viewAxis, false ); + } +} + + +VECTOR2I GRID_HELPER::AlignGrid( const VECTOR2I& aPoint ) const +{ + const VECTOR2D gridOffset( GetOrigin() ); + const VECTOR2D grid( GetGrid() ); + + VECTOR2I nearest( KiROUND( ( aPoint.x - gridOffset.x ) / grid.x ) * grid.x + gridOffset.x, + KiROUND( ( aPoint.y - gridOffset.y ) / grid.y ) * grid.y + gridOffset.y ); + + return nearest; +} + + +VECTOR2I GRID_HELPER::Align( const VECTOR2I& aPoint ) const +{ + if( !m_toolMgr->GetView()->GetGAL()->GetGridSnapping() ) + return aPoint; + + VECTOR2I nearest = AlignGrid( aPoint ); + + if( !m_auxAxis ) + return nearest; + + if( std::abs( m_auxAxis->x - aPoint.x ) < std::abs( nearest.x - aPoint.x ) ) + nearest.x = m_auxAxis->x; + + if( std::abs( m_auxAxis->y - aPoint.y ) < std::abs( nearest.y - aPoint.y ) ) + nearest.y = m_auxAxis->y; + + return nearest; +} + + diff --git a/eeschema/tools/ee_grid_helper.cpp b/eeschema/tools/ee_grid_helper.cpp index 4546302512..d276ba427a 100644 --- a/eeschema/tools/ee_grid_helper.cpp +++ b/eeschema/tools/ee_grid_helper.cpp @@ -23,38 +23,17 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ -/** - * EE_GRID_HELPER - * - * A helper class for doing grid and object snapping. - * - * It shares its roots with PCBNew's GRID_HELPER, but uses the layers architecture to split - * connectable items from graphic items. - */ - #include -using namespace std::placeholders; - -#include -#include -#include // for KiROUND -#include #include #include #include #include -#include - #include "ee_grid_helper.h" EE_GRID_HELPER::EE_GRID_HELPER( TOOL_MANAGER* aToolMgr ) : - m_toolMgr( aToolMgr ) + GRID_HELPER( aToolMgr ) { - m_enableSnap = true; - m_enableSnapLine = true; - m_enableGrid = true; - m_snapItem = nullptr; KIGFX::VIEW* view = m_toolMgr->GetView(); m_viewAxis.SetSize( 20000 ); @@ -78,112 +57,6 @@ EE_GRID_HELPER::EE_GRID_HELPER( TOOL_MANAGER* aToolMgr ) : } -EE_GRID_HELPER::~EE_GRID_HELPER() -{ -} - - -VECTOR2I EE_GRID_HELPER::GetGrid() const -{ - VECTOR2D size = m_toolMgr->GetView()->GetGAL()->GetGridSize(); - - return VECTOR2I( KiROUND( size.x ), KiROUND( size.y ) ); -} - - -VECTOR2I EE_GRID_HELPER::GetOrigin() const -{ - VECTOR2D origin = m_toolMgr->GetView()->GetGAL()->GetGridOrigin(); - - return VECTOR2I( origin ); -} - - -void EE_GRID_HELPER::SetAuxAxes( bool aEnable, const VECTOR2I& aOrigin ) -{ - if( aEnable ) - { - m_auxAxis = aOrigin; - m_viewAxis.SetPosition( wxPoint( aOrigin ) ); - m_toolMgr->GetView()->SetVisible( &m_viewAxis, true ); - } - else - { - m_auxAxis = OPT(); - m_toolMgr->GetView()->SetVisible( &m_viewAxis, false ); - } -} - - -VECTOR2I EE_GRID_HELPER::AlignGrid( const VECTOR2I& aPoint ) const -{ - const VECTOR2D gridOffset( GetOrigin() ); - const VECTOR2D grid( GetGrid() ); - - VECTOR2I nearest( KiROUND( ( aPoint.x - gridOffset.x ) / grid.x ) * grid.x + gridOffset.x, - KiROUND( ( aPoint.y - gridOffset.y ) / grid.y ) * grid.y + gridOffset.y ); - - return nearest; -} - - -VECTOR2I EE_GRID_HELPER::Align( const VECTOR2I& aPoint ) const -{ - if( !m_toolMgr->GetView()->GetGAL()->GetGridSnapping() ) - return aPoint; - - VECTOR2I nearest = AlignGrid( aPoint ); - - if( !m_auxAxis ) - return nearest; - - if( std::abs( m_auxAxis->x - aPoint.x ) < std::abs( nearest.x - aPoint.x ) ) - nearest.x = m_auxAxis->x; - - if( std::abs( m_auxAxis->y - aPoint.y ) < std::abs( nearest.y - aPoint.y ) ) - nearest.y = m_auxAxis->y; - - return nearest; -} - - -VECTOR2I EE_GRID_HELPER::AlignToWire( const VECTOR2I& aPoint, const SEG& aSeg ) -{ - OPT_VECTOR2I pts[6]; - - if( !m_enableSnap ) - return aPoint; - - const VECTOR2D gridOffset( GetOrigin() ); - const VECTOR2D gridSize( GetGrid() ); - - VECTOR2I nearest( KiROUND( ( aPoint.x - gridOffset.x ) / gridSize.x ) * gridSize.x + gridOffset.x, - KiROUND( ( aPoint.y - gridOffset.y ) / gridSize.y ) * gridSize.y + gridOffset.y ); - - pts[0] = aSeg.A; - pts[1] = aSeg.B; - pts[2] = aSeg.IntersectLines( SEG( nearest + VECTOR2I( -1, 0 ), nearest + VECTOR2I( 1, 0 ) ) ); - pts[3] = aSeg.IntersectLines( SEG( nearest + VECTOR2I( 0, -1 ), nearest + VECTOR2I( 0, 1 ) ) ); - - int min_d = std::numeric_limits::max(); - - for( int i = 0; i < 4; i++ ) - { - if( pts[i] && aSeg.Contains( *pts[i] ) ) - { - int d = (*pts[i] - aPoint).EuclideanNorm(); - - if( d < min_d ) - { - min_d = d; - nearest = *pts[i]; - } - } - } - - return nearest; -} - VECTOR2I EE_GRID_HELPER::BestDragOrigin( const VECTOR2I &aMousePos, int aLayer, const EE_SELECTION& aItems ) { @@ -230,32 +103,6 @@ VECTOR2I EE_GRID_HELPER::BestDragOrigin( const VECTOR2I &aMousePos, int aLayer, } -std::set EE_GRID_HELPER::queryVisible( const BOX2I& aArea, - const EE_SELECTION& aSkip ) const -{ - std::set items; - std::vector selectedItems; - - KIGFX::VIEW* view = m_toolMgr->GetView(); - - view->Query( aArea, selectedItems ); - - for( const KIGFX::VIEW::LAYER_ITEM_PAIR& it : selectedItems ) - { - SCH_ITEM* item = static_cast( it.first ); - - // The item must be visible and on an active layer - if( view->IsVisible( item ) && item->ViewGetLOD( it.second, view ) < view->GetScale() ) - items.insert ( item ); - } - - for( EDA_ITEM* skipItem : aSkip ) - items.erase( static_cast( skipItem ) ); - - return items; -} - - VECTOR2I EE_GRID_HELPER::BestSnapAnchor( const VECTOR2I& aOrigin, int aLayer, SCH_ITEM* aSkip ) { EE_SELECTION skipItems; @@ -380,7 +227,33 @@ SCH_ITEM* EE_GRID_HELPER::GetSnapped() const if( !m_snapItem ) return nullptr; - return m_snapItem->item; + return static_cast( m_snapItem->item ); +} + + +std::set EE_GRID_HELPER::queryVisible( const BOX2I& aArea, + const EE_SELECTION& aSkipList ) const +{ + std::set items; + std::vector selectedItems; + + KIGFX::VIEW* view = m_toolMgr->GetView(); + + view->Query( aArea, selectedItems ); + + for( const KIGFX::VIEW::LAYER_ITEM_PAIR& it : selectedItems ) + { + SCH_ITEM* item = static_cast( it.first ); + + // The item must be visible and on an active layer + if( view->IsVisible( item ) && item->ViewGetLOD( it.second, view ) < view->GetScale() ) + items.insert ( item ); + } + + for( EDA_ITEM* skipItem : aSkipList ) + items.erase( static_cast( skipItem ) ); + + return items; } @@ -422,12 +295,14 @@ EE_GRID_HELPER::ANCHOR* EE_GRID_HELPER::nearestAnchor( const VECTOR2I& aPos, int for( ANCHOR& a : m_anchors ) { + SCH_ITEM* item = static_cast( a.item ); + if( ( aFlags & a.flags ) != aFlags ) continue; - if( aMatchLayer == LAYER_CONNECTABLE && !a.item->IsConnectable() ) + if( aMatchLayer == LAYER_CONNECTABLE && !item->IsConnectable() ) continue; - else if( aMatchLayer == LAYER_GRAPHICS && a.item->IsConnectable() ) + else if( aMatchLayer == LAYER_GRAPHICS && item->IsConnectable() ) continue; double dist = a.Distance( aPos ); diff --git a/eeschema/tools/ee_grid_helper.h b/eeschema/tools/ee_grid_helper.h index f5e43af485..d680b968a2 100644 --- a/eeschema/tools/ee_grid_helper.h +++ b/eeschema/tools/ee_grid_helper.h @@ -2,7 +2,7 @@ * This program source code file is part of KiCad, a free EDA CAD application. * * Copyright (C) 2014 CERN - * Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors. + * Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors. * @author Tomasz Wlostowski * * This program is free software; you can redistribute it and/or @@ -23,26 +23,15 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ -/** - * EE_GRID_HELPER - * - * A helper class for doing grid and object snapping. - * - * It shares its roots with PCBNew's GRID_HELPER, but uses the layers architecture to split - * connectable items from graphic items. - */ +#ifndef EE_GRID_HELPER_H +#define EE_GRID_HELPER_H -#ifndef __GRID_HELPER_H -#define __GRID_HELPER_H - -#include #include #include +#include #include -class LSET; class SCH_ITEM; -class SEG; enum EE_GRID_HELPER_LAYERS : int @@ -53,15 +42,11 @@ enum EE_GRID_HELPER_LAYERS : int }; -class EE_GRID_HELPER +class EE_GRID_HELPER : public GRID_HELPER { public: EE_GRID_HELPER( TOOL_MANAGER* aToolMgr ); - ~EE_GRID_HELPER(); - - VECTOR2I GetGrid() const; - VECTOR2I GetOrigin() const; /** * Function GetSnapped @@ -72,70 +57,13 @@ public: */ SCH_ITEM* GetSnapped() const; - void SetAuxAxes( bool aEnable, const VECTOR2I& aOrigin = VECTOR2I( 0, 0 ) ); - - VECTOR2I Align( const VECTOR2I& aPoint ) const; - - VECTOR2I AlignGrid( const VECTOR2I& aPoint ) const; - - VECTOR2I AlignToWire( const VECTOR2I& aPoint, const SEG& aSeg ); - VECTOR2I BestDragOrigin( const VECTOR2I& aMousePos, int aLayer, const EE_SELECTION& aItems ); VECTOR2I BestSnapAnchor( const VECTOR2I& aOrigin, int aLayer, SCH_ITEM* aDraggedItem ); VECTOR2I BestSnapAnchor( const VECTOR2I& aOrigin, int aLayer, const EE_SELECTION& aSkip = {} ); - void SetSkipPoint( const VECTOR2I& aPoint ) - { - m_skipPoint = aPoint; - } - - /** - * We clear the skip point by setting it to an unreachable position, thereby preventing matching - */ - void ClearSkipPoint() - { - m_skipPoint = VECTOR2I( std::numeric_limits::min(), std::numeric_limits::min() ); - } - - void SetSnap( bool aSnap ) { m_enableSnap = aSnap; } - void SetUseGrid( bool aSnapToGrid ) { m_enableGrid = aSnapToGrid; } - void SetSnapLine( bool aSnap ) { m_enableSnapLine = aSnap; } - private: - enum ANCHOR_FLAGS { - CORNER = 1, - OUTLINE = 2, - SNAPPABLE = 4, - ORIGIN = 8, - VERTICAL = 16, - HORIZONTAL = 32 - }; - - struct ANCHOR - { - ANCHOR( VECTOR2I aPos, int aFlags = CORNER | SNAPPABLE, SCH_ITEM* aItem = NULL ) : - pos( aPos ), - flags( aFlags ), - item( aItem ) - { }; - - VECTOR2I pos; - int flags; - SCH_ITEM* item; - - double Distance( const VECTOR2I& aP ) const - { - return ( aP - pos ).EuclideanNorm(); - } - }; - - std::set queryVisible( const BOX2I& aArea, const EE_SELECTION& aSkip ) const; - - void addAnchor( const VECTOR2I& aPos, int aFlags, SCH_ITEM* aItem ) - { - m_anchors.emplace_back( ANCHOR( aPos, aFlags, aItem ) ); - } + std::set queryVisible( const BOX2I& aArea, const EE_SELECTION& aSkipList ) const; ANCHOR* nearestAnchor( const VECTOR2I& aPos, int aFlags, int aMatchLayer ); @@ -148,27 +76,6 @@ private: * @param aFrom Is this for an anchor that is designating a source point (aFrom=true) or not */ void computeAnchors( SCH_ITEM* aItem, const VECTOR2I& aRefPos, bool aFrom = false ); - - void clearAnchors() - { - m_anchors.clear(); - } - - std::vector m_anchors; - - TOOL_MANAGER* m_toolMgr; - OPT m_auxAxis; - - bool m_enableSnap; // Allow snapping to other items on the layers - bool m_enableGrid; // If true, allow snapping to grid - bool m_enableSnapLine; // Allow drawing lines from snap points - ANCHOR* m_snapItem; // Pointer to the currently snapped item in m_anchors - // (NULL if not snapped) - VECTOR2I m_skipPoint; // When drawing a line, we avoid snapping to the source - // point - KIGFX::ORIGIN_VIEWITEM m_viewSnapPoint; - KIGFX::ORIGIN_VIEWITEM m_viewSnapLine; - KIGFX::ORIGIN_VIEWITEM m_viewAxis; }; #endif diff --git a/include/tool/grid_helper.h b/include/tool/grid_helper.h new file mode 100644 index 0000000000..32caeb8fd3 --- /dev/null +++ b/include/tool/grid_helper.h @@ -0,0 +1,123 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, you may find one here: + * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html + * or you may search the http://www.gnu.org website for the version 2 license, + * or you may write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#ifndef GRID_HELPER_H +#define GRID_HELPER_H + +#include +#include +#include + +class TOOL_MANAGER; +class EDA_ITEM; + + +class GRID_HELPER +{ +public: + GRID_HELPER( TOOL_MANAGER* aToolMgr ); + ~GRID_HELPER(); + + VECTOR2I GetGrid() const; + VECTOR2I GetOrigin() const; + + void SetAuxAxes( bool aEnable, const VECTOR2I& aOrigin = VECTOR2I( 0, 0 ) ); + + virtual VECTOR2I Align( const VECTOR2I& aPoint ) const; + + VECTOR2I AlignGrid( const VECTOR2I& aPoint ) const; + + void SetSkipPoint( const VECTOR2I& aPoint ) + { + m_skipPoint = aPoint; + } + + /** + * We clear the skip point by setting it to an unreachable position, thereby preventing matching + */ + void ClearSkipPoint() + { + m_skipPoint = VECTOR2I( std::numeric_limits::min(), std::numeric_limits::min() ); + } + + void SetSnap( bool aSnap ) { m_enableSnap = aSnap; } + void SetUseGrid( bool aSnapToGrid ) { m_enableGrid = aSnapToGrid; } + void SetSnapLine( bool aSnap ) { m_enableSnapLine = aSnap; } + +protected: + enum ANCHOR_FLAGS { + CORNER = 1, + OUTLINE = 2, + SNAPPABLE = 4, + ORIGIN = 8, + VERTICAL = 16, + HORIZONTAL = 32 + }; + + struct ANCHOR + { + ANCHOR( VECTOR2I aPos, int aFlags = CORNER | SNAPPABLE, EDA_ITEM* aItem = NULL ) : + pos( aPos ), + flags( aFlags ), + item( aItem ) + { }; + + VECTOR2I pos; + int flags; + EDA_ITEM* item; + + double Distance( const VECTOR2I& aP ) const + { + return ( aP - pos ).EuclideanNorm(); + } + }; + + void addAnchor( const VECTOR2I& aPos, int aFlags, EDA_ITEM* aItem ) + { + m_anchors.emplace_back( ANCHOR( aPos, aFlags, aItem ) ); + } + + void clearAnchors() + { + m_anchors.clear(); + } + +protected: + std::vector m_anchors; + + TOOL_MANAGER* m_toolMgr; + OPT m_auxAxis; + + bool m_enableSnap; // Allow snapping to other items on the layers + bool m_enableGrid; // If true, allow snapping to grid + bool m_enableSnapLine; // Allow drawing lines from snap points + ANCHOR* m_snapItem; // Pointer to the currently snapped item in m_anchors + // (NULL if not snapped) + VECTOR2I m_skipPoint; // When drawing a line, we avoid snapping to the source + // point + KIGFX::ORIGIN_VIEWITEM m_viewSnapPoint; + KIGFX::ORIGIN_VIEWITEM m_viewSnapLine; + KIGFX::ORIGIN_VIEWITEM m_viewAxis; +}; + +#endif diff --git a/pcbnew/CMakeLists.txt b/pcbnew/CMakeLists.txt index 01b1113356..3d6fd0c42a 100644 --- a/pcbnew/CMakeLists.txt +++ b/pcbnew/CMakeLists.txt @@ -343,7 +343,7 @@ set( PCBNEW_CLASS_SRCS tools/pcb_selection_tool.cpp tools/pcb_tool_base.cpp tools/placement_tool.cpp - tools/point_editor.cpp + tools/pcb_point_editor.cpp tools/position_relative_tool.cpp tools/tool_event_utils.cpp tools/zone_create_helper.cpp diff --git a/pcbnew/footprint_edit_frame.cpp b/pcbnew/footprint_edit_frame.cpp index 9e599af108..7450a9f131 100644 --- a/pcbnew/footprint_edit_frame.cpp +++ b/pcbnew/footprint_edit_frame.cpp @@ -29,7 +29,7 @@ #include "tools/pcb_control.h" #include "tools/pcb_picker_tool.h" #include "tools/placement_tool.h" -#include "tools/point_editor.h" +#include "tools/pcb_point_editor.h" #include "tools/pcb_selection_tool.h" #include <3d_viewer/eda_3d_viewer.h> #include @@ -928,7 +928,7 @@ void FOOTPRINT_EDIT_FRAME::setupTools() m_toolManager->RegisterTool( new EDIT_TOOL ); m_toolManager->RegisterTool( new PAD_TOOL ); m_toolManager->RegisterTool( new DRAWING_TOOL ); - m_toolManager->RegisterTool( new POINT_EDITOR ); + m_toolManager->RegisterTool( new PCB_POINT_EDITOR ); m_toolManager->RegisterTool( new PCB_CONTROL ); // copy/paste m_toolManager->RegisterTool( new FOOTPRINT_EDITOR_CONTROL ); m_toolManager->RegisterTool( new ALIGN_DISTRIBUTE_TOOL ); @@ -942,7 +942,7 @@ void FOOTPRINT_EDIT_FRAME::setupTools() m_toolManager->GetTool()->SetIsFootprintEditor( true ); m_toolManager->GetTool()->SetIsFootprintEditor( true ); m_toolManager->GetTool()->SetIsFootprintEditor( true ); - m_toolManager->GetTool()->SetIsFootprintEditor( true ); + m_toolManager->GetTool()->SetIsFootprintEditor( true ); m_toolManager->GetTool()->SetIsFootprintEditor( true ); m_toolManager->GetTool()->SetIsFootprintEditor( true ); m_toolManager->GetTool()->SetIsFootprintEditor( true ); diff --git a/pcbnew/pcb_edit_frame.cpp b/pcbnew/pcb_edit_frame.cpp index 445d80537d..33854bc162 100644 --- a/pcbnew/pcb_edit_frame.cpp +++ b/pcbnew/pcb_edit_frame.cpp @@ -59,7 +59,7 @@ #include #include #include -#include +#include #include #include #include @@ -473,7 +473,7 @@ void PCB_EDIT_FRAME::setupTools() m_toolManager->RegisterTool( new GLOBAL_EDIT_TOOL ); m_toolManager->RegisterTool( new PAD_TOOL ); m_toolManager->RegisterTool( new DRAWING_TOOL ); - m_toolManager->RegisterTool( new POINT_EDITOR ); + m_toolManager->RegisterTool( new PCB_POINT_EDITOR ); m_toolManager->RegisterTool( new PCB_CONTROL ); m_toolManager->RegisterTool( new BOARD_EDITOR_CONTROL ); m_toolManager->RegisterTool( new BOARD_INSPECTION_TOOL ); diff --git a/pcbnew/router/pns_tool_base.cpp b/pcbnew/router/pns_tool_base.cpp index 41d656f29a..485ddf6510 100644 --- a/pcbnew/router/pns_tool_base.cpp +++ b/pcbnew/router/pns_tool_base.cpp @@ -31,7 +31,7 @@ using namespace std::placeholders; #include #include -#include +#include #include #include @@ -103,7 +103,7 @@ void TOOL_BASE::Reset( RESET_REASON aReason ) m_router->LoadSettings( settings->m_PnsSettings.get() ); - m_gridHelper = new GRID_HELPER( m_toolMgr, frame()->GetMagneticItemsSettings() ); + m_gridHelper = new PCB_GRID_HELPER( m_toolMgr, frame()->GetMagneticItemsSettings() ); } diff --git a/pcbnew/router/pns_tool_base.h b/pcbnew/router/pns_tool_base.h index 963e12cc9a..6ae1fd12fb 100644 --- a/pcbnew/router/pns_tool_base.h +++ b/pcbnew/router/pns_tool_base.h @@ -34,7 +34,7 @@ #include "pns_router.h" -class GRID_HELPER; +class PCB_GRID_HELPER; class PNS_KICAD_IFACE; class PNS_TUNE_STATUS_POPUP; @@ -73,7 +73,7 @@ protected: ITEM* m_endItem; VECTOR2I m_endSnapPoint; - GRID_HELPER* m_gridHelper; + PCB_GRID_HELPER* m_gridHelper; PNS_KICAD_IFACE* m_iface; ROUTER* m_router; diff --git a/pcbnew/router/router_tool.cpp b/pcbnew/router/router_tool.cpp index b73fa8c779..4a897c19e6 100644 --- a/pcbnew/router/router_tool.cpp +++ b/pcbnew/router/router_tool.cpp @@ -40,7 +40,7 @@ using namespace std::placeholders; #include #include #include -#include +#include #include "router_tool.h" #include "pns_segment.h" diff --git a/pcbnew/tools/drawing_tool.cpp b/pcbnew/tools/drawing_tool.cpp index be7959ebd3..61c5658fa3 100644 --- a/pcbnew/tools/drawing_tool.cpp +++ b/pcbnew/tools/drawing_tool.cpp @@ -30,7 +30,7 @@ #include #include #include -#include +#include #include #include #include @@ -643,7 +643,7 @@ int DRAWING_TOOL::DrawDimension( const TOOL_EVENT& aEvent ) TOOL_EVENT originalEvent = aEvent; DIMENSION_BASE* dimension = nullptr; BOARD_COMMIT commit( m_frame ); - GRID_HELPER grid( m_toolMgr, m_frame->GetMagneticItemsSettings() ); + PCB_GRID_HELPER grid( m_toolMgr, m_frame->GetMagneticItemsSettings() ); const BOARD_DESIGN_SETTINGS& boardSettings = m_board->GetDesignSettings(); @@ -1159,7 +1159,7 @@ int DRAWING_TOOL::SetAnchor( const TOOL_EVENT& aEvent ) return 0; SCOPED_DRAW_MODE scopedDrawMode( m_mode, MODE::ANCHOR ); - GRID_HELPER grid( m_toolMgr, m_frame->GetMagneticItemsSettings() ); + PCB_GRID_HELPER grid( m_toolMgr, m_frame->GetMagneticItemsSettings() ); std::string tool = aEvent.GetCommandStr().get(); m_frame->PushTool( tool ); @@ -1248,7 +1248,7 @@ bool DRAWING_TOOL::drawSegment( const std::string& aTool, PCB_SHAPE** aGraphic, assert( shape == S_SEGMENT || shape == S_CIRCLE || shape == S_RECT ); EDA_UNITS userUnits = m_frame->GetUserUnits(); - GRID_HELPER grid( m_toolMgr, m_frame->GetMagneticItemsSettings() ); + PCB_GRID_HELPER grid( m_toolMgr, m_frame->GetMagneticItemsSettings() ); PCB_SHAPE*& graphic = *aGraphic; m_lineWidth = getSegmentWidth( m_frame->GetActiveLayer() ); @@ -1566,7 +1566,7 @@ bool DRAWING_TOOL::drawArc( const std::string& aTool, PCB_SHAPE** aGraphic, bool PCB_SELECTION preview; m_view->Add( &preview ); m_view->Add( &arcAsst ); - GRID_HELPER grid( m_toolMgr, m_frame->GetMagneticItemsSettings() ); + PCB_GRID_HELPER grid( m_toolMgr, m_frame->GetMagneticItemsSettings() ); m_controls->ShowCursor( true ); @@ -1860,7 +1860,7 @@ int DRAWING_TOOL::DrawZone( const TOOL_EVENT& aEvent ) m_controls->ShowCursor( true ); bool started = false; - GRID_HELPER grid( m_toolMgr, m_frame->GetMagneticItemsSettings() ); + PCB_GRID_HELPER grid( m_toolMgr, m_frame->GetMagneticItemsSettings() ); STATUS_TEXT_POPUP status( m_frame ); status.SetTextColor( wxColour( 255, 0, 0 ) ); status.SetText( _( "Self-intersecting polygons are not allowed" ) ); @@ -2046,7 +2046,7 @@ int DRAWING_TOOL::DrawVia( const TOOL_EVENT& aEvent ) { struct VIA_PLACER : public INTERACTIVE_PLACER_BASE { - GRID_HELPER m_gridHelper; + PCB_GRID_HELPER m_gridHelper; VIA_PLACER( PCB_BASE_EDIT_FRAME* aFrame ) : m_gridHelper( aFrame->GetToolManager(), aFrame->GetMagneticItemsSettings() ) diff --git a/pcbnew/tools/edit_tool.cpp b/pcbnew/tools/edit_tool.cpp index 315be8f34d..386b2c8725 100644 --- a/pcbnew/tools/edit_tool.cpp +++ b/pcbnew/tools/edit_tool.cpp @@ -42,7 +42,7 @@ #include #include #include -#include +#include #include #include #include @@ -398,7 +398,7 @@ int EDIT_TOOL::doMoveSelection( TOOL_EVENT aEvent, bool aPickReference ) bool restore_state = false; VECTOR2I totalMovement; - GRID_HELPER grid( m_toolMgr, editFrame->GetMagneticItemsSettings() ); + PCB_GRID_HELPER grid( m_toolMgr, editFrame->GetMagneticItemsSettings() ); TOOL_EVENT* evt = const_cast( &aEvent ); VECTOR2I prevPos; @@ -1949,7 +1949,7 @@ int EDIT_TOOL::copyToClipboard( const TOOL_EVENT& aEvent ) { std::string tool = "pcbnew.InteractiveEdit.selectReferencePoint"; CLIPBOARD_IO io; - GRID_HELPER grid( m_toolMgr, getEditFrame()->GetMagneticItemsSettings() ); + PCB_GRID_HELPER grid( m_toolMgr, getEditFrame()->GetMagneticItemsSettings() ); frame()->PushTool( tool ); Activate(); diff --git a/pcbnew/tools/grid_helper.h b/pcbnew/tools/grid_helper.h deleted file mode 100644 index 69199ad026..0000000000 --- a/pcbnew/tools/grid_helper.h +++ /dev/null @@ -1,167 +0,0 @@ -/* - * This program source code file is part of KiCad, a free EDA CAD application. - * - * Copyright (C) 2014 CERN - * Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors. - * @author Tomasz Wlostowski - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, you may find one here: - * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html - * or you may search the http://www.gnu.org website for the version 2 license, - * or you may write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA - */ - -#ifndef __GRID_HELPER_H -#define __GRID_HELPER_H - -#include -#include -#include -#include -#include - -class TOOL_MANAGER; - -class GRID_HELPER { -public: - - GRID_HELPER( TOOL_MANAGER* aToolMgr, MAGNETIC_SETTINGS* aMagneticSettings ); - ~GRID_HELPER(); - - VECTOR2I GetGrid() const; - VECTOR2I GetOrigin() const; - - /** - * Function GetSnapped - * If the GRID_HELPER has highlighted a snap point (target shown), this function - * will return a pointer to the item to which it snapped. - * - * @return NULL if not snapped. Pointer to snapped item otherwise - */ - BOARD_ITEM* GetSnapped() const; - - void SetAuxAxes( bool aEnable, const VECTOR2I& aOrigin = VECTOR2I( 0, 0 ) ); - - VECTOR2I Align( const VECTOR2I& aPoint ) const; - - VECTOR2I AlignToSegment ( const VECTOR2I& aPoint, const SEG& aSeg ); - - VECTOR2I BestDragOrigin( const VECTOR2I& aMousePos, std::vector& aItem ); - - VECTOR2I AlignToArc ( const VECTOR2I& aPoint, const SHAPE_ARC& aSeg ); - - VECTOR2I BestSnapAnchor( const VECTOR2I& aOrigin, BOARD_ITEM* aDraggedItem ); - VECTOR2I BestSnapAnchor( const VECTOR2I& aOrigin, const LSET& aLayers, - const std::vector& aSkip = {} ); - - void SetSkipPoint( const VECTOR2I& aPoint ) - { - m_skipPoint = aPoint; - } - - /** - * We clear the skip point by setting it to an unreachable position, thereby preventing matching - */ - void ClearSkipPoint() - { - m_skipPoint = VECTOR2I( std::numeric_limits::min(), std::numeric_limits::min() ); - } - - void SetSnap( bool aSnap ) - { - m_enableSnap = aSnap; - } - - void SetSnapLine( bool aSnap ) - { - m_enableSnapLine = aSnap; - } - - void SetUseGrid( bool aGrid = true ) - { - m_enableGrid = aGrid; - } - -private: - enum ANCHOR_FLAGS { - CORNER = 0x1, - OUTLINE = 0x2, - SNAPPABLE = 0x4, - ORIGIN = 0x8 - }; - - struct ANCHOR - { - ANCHOR( VECTOR2I aPos, int aFlags = CORNER | SNAPPABLE, BOARD_ITEM* aItem = NULL ) : - pos( aPos ), - flags( aFlags ), - item( aItem ) - { }; - - VECTOR2I pos; - int flags; - BOARD_ITEM* item; - - double Distance( const VECTOR2I& aP ) const - { - return ( aP - pos ).EuclideanNorm(); - } - }; - - std::vector m_anchors; - - std::set queryVisible( const BOX2I& aArea, - const std::vector& aSkip ) const; - - void addAnchor( const VECTOR2I& aPos, int aFlags, BOARD_ITEM* aItem ) - { - m_anchors.emplace_back( ANCHOR( aPos, aFlags, aItem ) ); - } - - ANCHOR* nearestAnchor( const VECTOR2I& aPos, int aFlags, LSET aMatchLayers ); - - /** - * computeAnchors inserts the local anchor points in to the grid helper for the specified - * board item, given the reference point and the direction of use for the point. - * - * @param aItem The board item for which to compute the anchors - * @param aRefPos The point for which to compute the anchors (if used by the component) - * @param aFrom Is this for an anchor that is designating a source point (aFrom=true) or not - */ - void computeAnchors( BOARD_ITEM* aItem, const VECTOR2I& aRefPos, bool aFrom = false ); - - void clearAnchors() - { - m_anchors.clear(); - } - - TOOL_MANAGER* m_toolMgr; - OPT m_auxAxis; - - bool m_enableSnap; // If true, allow snapping to other items on the layers - bool m_enableGrid; // If true, allow snapping to grid - bool m_enableSnapLine; // If true, allow drawing lines from snap points - ANCHOR* m_snapItem; // Pointer to the currently snapped item in m_anchors - // (NULL if not snapped) - VECTOR2I m_skipPoint; // When drawing a line, we avoid snapping to the source point - - MAGNETIC_SETTINGS* m_magneticSettings; - - KIGFX::ORIGIN_VIEWITEM m_viewSnapPoint; - KIGFX::ORIGIN_VIEWITEM m_viewSnapLine; - KIGFX::ORIGIN_VIEWITEM m_viewAxis; -}; - -#endif diff --git a/pcbnew/tools/pcb_actions.cpp b/pcbnew/tools/pcb_actions.cpp index e16bd38429..80c814e818 100644 --- a/pcbnew/tools/pcb_actions.cpp +++ b/pcbnew/tools/pcb_actions.cpp @@ -1104,7 +1104,7 @@ TOOL_ACTION PCB_ACTIONS::distributeVertically( "pcbnew.AlignAndDistribute.distri _( "Distributes selected items along the vertical axis" ), distribute_vertical_xpm ); -// POINT_EDITOR +// PCB_POINT_EDITOR // TOOL_ACTION PCB_ACTIONS::pointEditorAddCorner( "pcbnew.PointEditor.addCorner", AS_GLOBAL, diff --git a/pcbnew/tools/grid_helper.cpp b/pcbnew/tools/pcb_grid_helper.cpp similarity index 89% rename from pcbnew/tools/grid_helper.cpp rename to pcbnew/tools/pcb_grid_helper.cpp index 1e3959cfd6..c1defea850 100644 --- a/pcbnew/tools/grid_helper.cpp +++ b/pcbnew/tools/pcb_grid_helper.cpp @@ -2,7 +2,7 @@ * This program source code file is part of KiCad, a free EDA CAD application. * * Copyright (C) 2014 CERN - * Copyright (C) 2018-2020 KiCad Developers, see AUTHORS.txt for contributors. + * Copyright (C) 2018-2021 KiCad Developers, see AUTHORS.txt for contributors. * @author Tomasz Wlostowski * * This program is free software; you can redistribute it and/or @@ -24,8 +24,6 @@ */ #include -using namespace std::placeholders; - #include #include #include @@ -39,26 +37,18 @@ using namespace std::placeholders; #include #include #include // for KiROUND -#include #include #include #include #include #include -#include - -#include "grid_helper.h" +#include "pcb_grid_helper.h" -GRID_HELPER::GRID_HELPER( TOOL_MANAGER* aToolMgr, MAGNETIC_SETTINGS* aMagneticSettings ) : - m_toolMgr( aToolMgr ), +PCB_GRID_HELPER::PCB_GRID_HELPER( TOOL_MANAGER* aToolMgr, MAGNETIC_SETTINGS* aMagneticSettings ) : + GRID_HELPER( aToolMgr ), m_magneticSettings( aMagneticSettings ) { - m_enableSnap = true; - m_enableGrid = true; - m_enableSnapLine = true; - m_snapItem = nullptr; - KIGFX::VIEW* view = m_toolMgr->GetView(); KIGFX::RENDER_SETTINGS* settings = view->GetPainter()->GetSettings(); KIGFX::COLOR4D auxItemsColor = settings->GetLayerColor( LAYER_AUX_ITEMS ); @@ -85,68 +75,7 @@ GRID_HELPER::GRID_HELPER( TOOL_MANAGER* aToolMgr, MAGNETIC_SETTINGS* aMagneticSe } -GRID_HELPER::~GRID_HELPER() -{ -} - - -VECTOR2I GRID_HELPER::GetGrid() const -{ - VECTOR2D size = m_toolMgr->GetView()->GetGAL()->GetGridSize(); - - return VECTOR2I( KiROUND( size.x ), KiROUND( size.y ) ); -} - - -VECTOR2I GRID_HELPER::GetOrigin() const -{ - VECTOR2D origin = m_toolMgr->GetView()->GetGAL()->GetGridOrigin(); - - return VECTOR2I( origin ); -} - - -void GRID_HELPER::SetAuxAxes( bool aEnable, const VECTOR2I& aOrigin ) -{ - if( aEnable ) - { - m_auxAxis = aOrigin; - m_viewAxis.SetPosition( wxPoint( aOrigin ) ); - m_toolMgr->GetView()->SetVisible( &m_viewAxis, true ); - } - else - { - m_auxAxis = OPT(); - m_toolMgr->GetView()->SetVisible( &m_viewAxis, false ); - } -} - - -VECTOR2I GRID_HELPER::Align( const VECTOR2I& aPoint ) const -{ - if( !m_enableGrid ) - return aPoint; - - const VECTOR2D gridOffset( GetOrigin() ); - const VECTOR2D grid( GetGrid() ); - - VECTOR2I nearest( KiROUND( ( aPoint.x - gridOffset.x ) / grid.x ) * grid.x + gridOffset.x, - KiROUND( ( aPoint.y - gridOffset.y ) / grid.y ) * grid.y + gridOffset.y ); - - if( !m_auxAxis ) - return nearest; - - if( std::abs( m_auxAxis->x - aPoint.x ) < std::abs( nearest.x - aPoint.x ) ) - nearest.x = m_auxAxis->x; - - if( std::abs( m_auxAxis->y - aPoint.y ) < std::abs( nearest.y - aPoint.y ) ) - nearest.y = m_auxAxis->y; - - return nearest; -} - - -VECTOR2I GRID_HELPER::AlignToSegment( const VECTOR2I& aPoint, const SEG& aSeg ) +VECTOR2I PCB_GRID_HELPER::AlignToSegment( const VECTOR2I& aPoint, const SEG& aSeg ) { OPT_VECTOR2I pts[6]; @@ -184,7 +113,7 @@ VECTOR2I GRID_HELPER::AlignToSegment( const VECTOR2I& aPoint, const SEG& aSeg ) } -VECTOR2I GRID_HELPER::AlignToArc( const VECTOR2I& aPoint, const SHAPE_ARC& aArc ) +VECTOR2I PCB_GRID_HELPER::AlignToArc( const VECTOR2I& aPoint, const SHAPE_ARC& aArc ) { if( !m_enableSnap ) return aPoint; @@ -214,7 +143,8 @@ VECTOR2I GRID_HELPER::AlignToArc( const VECTOR2I& aPoint, const SHAPE_ARC& aArc } -VECTOR2I GRID_HELPER::BestDragOrigin( const VECTOR2I &aMousePos, std::vector& aItems ) +VECTOR2I PCB_GRID_HELPER::BestDragOrigin( const VECTOR2I &aMousePos, + std::vector& aItems ) { clearAnchors(); @@ -259,48 +189,7 @@ VECTOR2I GRID_HELPER::BestDragOrigin( const VECTOR2I &aMousePos, std::vector GRID_HELPER::queryVisible( const BOX2I& aArea, - const std::vector& aSkip ) const -{ - std::set items; - std::vector selectedItems; - - KIGFX::VIEW* view = m_toolMgr->GetView(); - RENDER_SETTINGS* settings = view->GetPainter()->GetSettings(); - const std::set& activeLayers = settings->GetHighContrastLayers(); - bool isHighContrast = settings->GetHighContrast(); - - view->Query( aArea, selectedItems ); - - for( const KIGFX::VIEW::LAYER_ITEM_PAIR& it : selectedItems ) - { - BOARD_ITEM* item = static_cast( it.first ); - - // If we are in the footprint editor, don't use the footprint itself - if( static_cast( m_toolMgr->GetCurrentTool() )->IsFootprintEditor() - && item->Type() == PCB_FOOTPRINT_T ) - { - continue; - } - - // The item must be visible and on an active layer - if( view->IsVisible( item ) - && ( !isHighContrast || activeLayers.count( it.second ) ) - && item->ViewGetLOD( it.second, view ) < view->GetScale() ) - { - items.insert ( item ); - } - } - - - for( BOARD_ITEM* skipItem : aSkip ) - items.erase( skipItem ); - - return items; -} - - -VECTOR2I GRID_HELPER::BestSnapAnchor( const VECTOR2I& aOrigin, BOARD_ITEM* aDraggedItem ) +VECTOR2I PCB_GRID_HELPER::BestSnapAnchor( const VECTOR2I& aOrigin, BOARD_ITEM* aDraggedItem ) { LSET layers; std::vector item; @@ -317,8 +206,8 @@ VECTOR2I GRID_HELPER::BestSnapAnchor( const VECTOR2I& aOrigin, BOARD_ITEM* aDrag } -VECTOR2I GRID_HELPER::BestSnapAnchor( const VECTOR2I& aOrigin, const LSET& aLayers, - const std::vector& aSkip ) +VECTOR2I PCB_GRID_HELPER::BestSnapAnchor( const VECTOR2I& aOrigin, const LSET& aLayers, + const std::vector& aSkip ) { // Tuning constant: snap radius in screen space const int snapSize = 25; @@ -403,16 +292,56 @@ VECTOR2I GRID_HELPER::BestSnapAnchor( const VECTOR2I& aOrigin, const LSET& aLaye } -BOARD_ITEM* GRID_HELPER::GetSnapped() const +BOARD_ITEM* PCB_GRID_HELPER::GetSnapped() const { if( !m_snapItem ) return nullptr; - return m_snapItem->item; + return static_cast( m_snapItem->item ); } -void GRID_HELPER::computeAnchors( BOARD_ITEM* aItem, const VECTOR2I& aRefPos, bool aFrom ) +std::set PCB_GRID_HELPER::queryVisible( const BOX2I& aArea, + const std::vector& aSkip ) const +{ + std::set items; + std::vector selectedItems; + + KIGFX::VIEW* view = m_toolMgr->GetView(); + RENDER_SETTINGS* settings = view->GetPainter()->GetSettings(); + const std::set& activeLayers = settings->GetHighContrastLayers(); + bool isHighContrast = settings->GetHighContrast(); + + view->Query( aArea, selectedItems ); + + for( const KIGFX::VIEW::LAYER_ITEM_PAIR& it : selectedItems ) + { + BOARD_ITEM* item = static_cast( it.first ); + + // If we are in the footprint editor, don't use the footprint itself + if( static_cast( m_toolMgr->GetCurrentTool() )->IsFootprintEditor() + && item->Type() == PCB_FOOTPRINT_T ) + { + continue; + } + + // The item must be visible and on an active layer + if( view->IsVisible( item ) + && ( !isHighContrast || activeLayers.count( it.second ) ) + && item->ViewGetLOD( it.second, view ) < view->GetScale() ) + { + items.insert ( item ); + } + } + + for( BOARD_ITEM* skipItem : aSkip ) + items.erase( skipItem ); + + return items; +} + + +void PCB_GRID_HELPER::computeAnchors( BOARD_ITEM* aItem, const VECTOR2I& aRefPos, bool aFrom ) { VECTOR2I origin; KIGFX::VIEW* view = m_toolMgr->GetView(); @@ -749,15 +678,17 @@ void GRID_HELPER::computeAnchors( BOARD_ITEM* aItem, const VECTOR2I& aRefPos, bo } -GRID_HELPER::ANCHOR* GRID_HELPER::nearestAnchor( const VECTOR2I& aPos, int aFlags, - LSET aMatchLayers ) +PCB_GRID_HELPER::ANCHOR* PCB_GRID_HELPER::nearestAnchor( const VECTOR2I& aPos, int aFlags, + LSET aMatchLayers ) { double minDist = std::numeric_limits::max(); ANCHOR* best = NULL; for( ANCHOR& a : m_anchors ) { - if( ( aMatchLayers & a.item->GetLayerSet() ) == 0 ) + BOARD_ITEM* item = static_cast( a.item ); + + if( ( aMatchLayers & item->GetLayerSet() ) == 0 ) continue; if( ( aFlags & a.flags ) != aFlags ) diff --git a/pcbnew/tools/pcb_grid_helper.h b/pcbnew/tools/pcb_grid_helper.h new file mode 100644 index 0000000000..f06f6b0f3f --- /dev/null +++ b/pcbnew/tools/pcb_grid_helper.h @@ -0,0 +1,87 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2014 CERN + * Copyright (C) 2020-2021 KiCad Developers, see AUTHORS.txt for contributors. + * @author Tomasz Wlostowski + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, you may find one here: + * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html + * or you may search the http://www.gnu.org website for the version 2 license, + * or you may write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#ifndef PCB_GRID_HELPER_H +#define PCB_GRID_HELPER_H + +#include +#include + +class TOOL_MANAGER; + +class PCB_GRID_HELPER : public GRID_HELPER +{ +public: + + PCB_GRID_HELPER( TOOL_MANAGER* aToolMgr, MAGNETIC_SETTINGS* aMagneticSettings ); + + /** + * Function GetSnapped + * If the PCB_GRID_HELPER has highlighted a snap point (target shown), this function + * will return a pointer to the item to which it snapped. + * + * @return NULL if not snapped. Pointer to snapped item otherwise + */ + BOARD_ITEM* GetSnapped() const; + + VECTOR2I Align( const VECTOR2I& aPoint ) const override + { + if( !m_enableGrid ) + return aPoint; + + return GRID_HELPER::Align( aPoint ); + } + + VECTOR2I AlignToSegment ( const VECTOR2I& aPoint, const SEG& aSeg ); + + VECTOR2I BestDragOrigin( const VECTOR2I& aMousePos, std::vector& aItem ); + + VECTOR2I AlignToArc ( const VECTOR2I& aPoint, const SHAPE_ARC& aSeg ); + + VECTOR2I BestSnapAnchor( const VECTOR2I& aOrigin, BOARD_ITEM* aDraggedItem ); + VECTOR2I BestSnapAnchor( const VECTOR2I& aOrigin, const LSET& aLayers, + const std::vector& aSkip = {} ); + +private: + std::set queryVisible( const BOX2I& aArea, + const std::vector& aSkip ) const; + + ANCHOR* nearestAnchor( const VECTOR2I& aPos, int aFlags, LSET aMatchLayers ); + + /** + * computeAnchors inserts the local anchor points in to the grid helper for the specified + * board item, given the reference point and the direction of use for the point. + * + * @param aItem The board item for which to compute the anchors + * @param aRefPos The point for which to compute the anchors (if used by the component) + * @param aFrom Is this for an anchor that is designating a source point (aFrom=true) or not + */ + void computeAnchors( BOARD_ITEM* aItem, const VECTOR2I& aRefPos, bool aFrom = false ); + +private: + MAGNETIC_SETTINGS* m_magneticSettings; +}; + +#endif diff --git a/pcbnew/tools/pcb_picker_tool.cpp b/pcbnew/tools/pcb_picker_tool.cpp index 86aa07489a..ffea0aaeba 100644 --- a/pcbnew/tools/pcb_picker_tool.cpp +++ b/pcbnew/tools/pcb_picker_tool.cpp @@ -25,7 +25,7 @@ #include "pcb_picker_tool.h" #include "pcb_actions.h" -#include "grid_helper.h" +#include "pcb_grid_helper.h" #include #include #include "pcb_selection_tool.h" @@ -42,7 +42,7 @@ int PCB_PICKER_TOOL::Main( const TOOL_EVENT& aEvent ) { KIGFX::VIEW_CONTROLS* controls = getViewControls(); PCB_BASE_FRAME* frame = getEditFrame(); - GRID_HELPER grid( m_toolMgr, frame->GetMagneticItemsSettings() ); + PCB_GRID_HELPER grid( m_toolMgr, frame->GetMagneticItemsSettings() ); int finalize_state = WAIT_CANCEL; std::string tool = *aEvent.Parameter(); diff --git a/pcbnew/tools/point_editor.cpp b/pcbnew/tools/pcb_point_editor.cpp similarity index 95% rename from pcbnew/tools/point_editor.cpp rename to pcbnew/tools/pcb_point_editor.cpp index b5ddf85a40..dbd2dade1f 100644 --- a/pcbnew/tools/point_editor.cpp +++ b/pcbnew/tools/pcb_point_editor.cpp @@ -1,7 +1,7 @@ /* * This program source code file is part of KiCad, a free EDA CAD application. * - * Copyright (C) 2013-2019 CERN + * Copyright (C) 2013-2021 CERN * @author Maciej Suminski * * This program is free software; you can redistribute it and/or @@ -25,16 +25,14 @@ #include #include using namespace std::placeholders; -#include #include #include -#include #include #include #include "pcb_actions.h" #include "pcb_selection_tool.h" -#include "point_editor.h" -#include "grid_helper.h" +#include "pcb_point_editor.h" +#include "pcb_grid_helper.h" #include #include #include @@ -88,7 +86,7 @@ enum DIMENSION_POINTS DIM_CROSSBAREND, }; -POINT_EDITOR::POINT_EDITOR() : +PCB_POINT_EDITOR::PCB_POINT_EDITOR() : PCB_TOOL_BASE( "pcbnew.PointEditor" ), m_selectionTool( nullptr ), m_editedPoint( nullptr ), @@ -101,7 +99,7 @@ POINT_EDITOR::POINT_EDITOR() : } -void POINT_EDITOR::Reset( RESET_REASON aReason ) +void PCB_POINT_EDITOR::Reset( RESET_REASON aReason ) { m_refill = false; m_editPoints.reset(); @@ -114,7 +112,7 @@ void POINT_EDITOR::Reset( RESET_REASON aReason ) } -bool POINT_EDITOR::Init() +bool PCB_POINT_EDITOR::Init() { // Find the selection tool, so they can cooperate m_selectionTool = m_toolMgr->GetTool(); @@ -122,16 +120,16 @@ bool POINT_EDITOR::Init() wxASSERT_MSG( m_selectionTool, "pcbnew.InteractiveSelection tool is not available" ); auto& menu = m_selectionTool->GetToolMenu().GetMenu(); - menu.AddItem( PCB_ACTIONS::pointEditorAddCorner, POINT_EDITOR::addCornerCondition ); + menu.AddItem( PCB_ACTIONS::pointEditorAddCorner, PCB_POINT_EDITOR::addCornerCondition ); menu.AddItem( PCB_ACTIONS::pointEditorRemoveCorner, - std::bind( &POINT_EDITOR::removeCornerCondition, this, _1 ) ); + std::bind( &PCB_POINT_EDITOR::removeCornerCondition, this, _1 ) ); return true; } -void POINT_EDITOR::buildForPolyOutline( std::shared_ptr points, - const SHAPE_POLY_SET* aOutline ) +void PCB_POINT_EDITOR::buildForPolyOutline( std::shared_ptr points, + const SHAPE_POLY_SET* aOutline ) { int cornersCount = aOutline->TotalVertices(); @@ -164,7 +162,7 @@ void POINT_EDITOR::buildForPolyOutline( std::shared_ptr points, } -std::shared_ptr POINT_EDITOR::makePoints( EDA_ITEM* aItem ) +std::shared_ptr PCB_POINT_EDITOR::makePoints( EDA_ITEM* aItem ) { std::shared_ptr points = std::make_shared( aItem ); @@ -345,7 +343,7 @@ std::shared_ptr POINT_EDITOR::makePoints( EDA_ITEM* aItem ) } -void POINT_EDITOR::updateEditedPoint( const TOOL_EVENT& aEvent ) +void PCB_POINT_EDITOR::updateEditedPoint( const TOOL_EVENT& aEvent ) { EDIT_POINT* point; EDIT_POINT* hovered = nullptr; @@ -386,7 +384,7 @@ void POINT_EDITOR::updateEditedPoint( const TOOL_EVENT& aEvent ) } -int POINT_EDITOR::OnSelectionChange( const TOOL_EVENT& aEvent ) +int PCB_POINT_EDITOR::OnSelectionChange( const TOOL_EVENT& aEvent ) { if( !m_selectionTool || aEvent.Matches( EVENTS::InhibitSelectionEditing ) ) return 0; @@ -404,7 +402,7 @@ int POINT_EDITOR::OnSelectionChange( const TOOL_EVENT& aEvent ) controls->ShowCursor( true ); - GRID_HELPER grid( m_toolMgr, editFrame->GetMagneticItemsSettings() ); + PCB_GRID_HELPER grid( m_toolMgr, editFrame->GetMagneticItemsSettings() ); BOARD_ITEM* item = static_cast( selection.Front() ); if( !item ) @@ -691,8 +689,8 @@ static void pinEditedCorner( int aEditedPointIndex, int aMinWidth, int aMinHeigh } -void POINT_EDITOR::editArcEndpointKeepShape( PCB_SHAPE* aArc, VECTOR2I aStart, VECTOR2I aMid, - VECTOR2I aEnd, const VECTOR2I aCursor ) const +void PCB_POINT_EDITOR::editArcEndpointKeepShape( PCB_SHAPE* aArc, VECTOR2I aStart, VECTOR2I aMid, + VECTOR2I aEnd, const VECTOR2I aCursor ) const { VECTOR2I diff; @@ -707,9 +705,9 @@ void POINT_EDITOR::editArcEndpointKeepShape( PCB_SHAPE* aArc, VECTOR2I aStart, V } -void POINT_EDITOR::editArcEndpointKeepCenter( PCB_SHAPE* aArc, VECTOR2I aCenter, VECTOR2I aStart, - VECTOR2I aMid, VECTOR2I aEnd, - const VECTOR2I aCursor ) const +void PCB_POINT_EDITOR::editArcEndpointKeepCenter( PCB_SHAPE* aArc, VECTOR2I aCenter, + VECTOR2I aStart, VECTOR2I aMid, VECTOR2I aEnd, + const VECTOR2I aCursor ) const { bool clockwise; bool movingStart; @@ -784,8 +782,9 @@ void POINT_EDITOR::editArcEndpointKeepCenter( PCB_SHAPE* aArc, VECTOR2I aCenter, } -void POINT_EDITOR::editArcMidKeepCenter( PCB_SHAPE* aArc, VECTOR2I aCenter, VECTOR2I aStart, - VECTOR2I aMid, VECTOR2I aEnd, const VECTOR2I aCursor ) const +void PCB_POINT_EDITOR::editArcMidKeepCenter( PCB_SHAPE* aArc, VECTOR2I aCenter, VECTOR2I aStart, + VECTOR2I aMid, VECTOR2I aEnd, + const VECTOR2I aCursor ) const { // Now, update the edit point position // Express the point in a cercle-centered coordinate system. @@ -842,8 +841,8 @@ void POINT_EDITOR::editArcMidKeepCenter( PCB_SHAPE* aArc, VECTOR2I aCenter, VECT } -void POINT_EDITOR::editArcMidKeepEndpoints( PCB_SHAPE* aArc, VECTOR2I aStart, VECTOR2I aEnd, - const VECTOR2I aCursor ) const +void PCB_POINT_EDITOR::editArcMidKeepEndpoints( PCB_SHAPE* aArc, VECTOR2I aStart, VECTOR2I aEnd, + const VECTOR2I aCursor ) const { // Let 'm' be the middle point of the chord between the start and end points VECTOR2I m = ( aStart + aEnd ) / 2; @@ -859,7 +858,7 @@ void POINT_EDITOR::editArcMidKeepEndpoints( PCB_SHAPE* aArc, VECTOR2I aStart, VE } -void POINT_EDITOR::updateItem() const +void PCB_POINT_EDITOR::updateItem() const { EDA_ITEM* item = m_editPoints->GetParent(); @@ -1314,7 +1313,7 @@ void POINT_EDITOR::updateItem() const } -void POINT_EDITOR::finishItem() +void PCB_POINT_EDITOR::finishItem() { auto item = m_editPoints->GetParent(); @@ -1331,7 +1330,7 @@ void POINT_EDITOR::finishItem() } -bool POINT_EDITOR::validatePolygon( SHAPE_POLY_SET& aPoly ) const +bool PCB_POINT_EDITOR::validatePolygon( SHAPE_POLY_SET& aPoly ) const { bool valid = !aPoly.IsSelfIntersecting(); @@ -1353,7 +1352,7 @@ bool POINT_EDITOR::validatePolygon( SHAPE_POLY_SET& aPoly ) const } -void POINT_EDITOR::updatePoints() +void PCB_POINT_EDITOR::updatePoints() { if( !m_editPoints ) return; @@ -1575,7 +1574,7 @@ void POINT_EDITOR::updatePoints() } -void POINT_EDITOR::setEditedPoint( EDIT_POINT* aPoint ) +void PCB_POINT_EDITOR::setEditedPoint( EDIT_POINT* aPoint ) { KIGFX::VIEW_CONTROLS* controls = getViewControls(); @@ -1597,7 +1596,7 @@ void POINT_EDITOR::setEditedPoint( EDIT_POINT* aPoint ) } -void POINT_EDITOR::setAltConstraint( bool aEnabled ) +void PCB_POINT_EDITOR::setAltConstraint( bool aEnabled ) { if( aEnabled ) { @@ -1636,7 +1635,7 @@ void POINT_EDITOR::setAltConstraint( bool aEnabled ) } -EDIT_POINT POINT_EDITOR::get45DegConstrainer() const +EDIT_POINT PCB_POINT_EDITOR::get45DegConstrainer() const { EDA_ITEM* item = m_editPoints->GetParent(); @@ -1691,7 +1690,7 @@ EDIT_POINT POINT_EDITOR::get45DegConstrainer() const } -bool POINT_EDITOR::canAddCorner( const EDA_ITEM& aItem ) +bool PCB_POINT_EDITOR::canAddCorner( const EDA_ITEM& aItem ) { const auto type = aItem.Type(); @@ -1709,7 +1708,7 @@ bool POINT_EDITOR::canAddCorner( const EDA_ITEM& aItem ) } -bool POINT_EDITOR::addCornerCondition( const SELECTION& aSelection ) +bool PCB_POINT_EDITOR::addCornerCondition( const SELECTION& aSelection ) { if( aSelection.Size() != 1 ) return false; @@ -1736,7 +1735,7 @@ findVertex( SHAPE_POLY_SET& aPolySet, const EDIT_POINT& aPoint ) } -bool POINT_EDITOR::removeCornerCondition( const SELECTION& ) +bool PCB_POINT_EDITOR::removeCornerCondition( const SELECTION& ) { if( !m_editPoints || !m_editedPoint ) return false; @@ -1780,7 +1779,7 @@ bool POINT_EDITOR::removeCornerCondition( const SELECTION& ) } -int POINT_EDITOR::addCorner( const TOOL_EVENT& aEvent ) +int PCB_POINT_EDITOR::addCorner( const TOOL_EVENT& aEvent ) { if( !m_editPoints ) return 0; @@ -1911,7 +1910,7 @@ int POINT_EDITOR::addCorner( const TOOL_EVENT& aEvent ) } -int POINT_EDITOR::removeCorner( const TOOL_EVENT& aEvent ) +int PCB_POINT_EDITOR::removeCorner( const TOOL_EVENT& aEvent ) { if( !m_editPoints || !m_editedPoint ) return 0; @@ -1987,27 +1986,27 @@ int POINT_EDITOR::removeCorner( const TOOL_EVENT& aEvent ) } -int POINT_EDITOR::modifiedSelection( const TOOL_EVENT& aEvent ) +int PCB_POINT_EDITOR::modifiedSelection( const TOOL_EVENT& aEvent ) { updatePoints(); return 0; } -int POINT_EDITOR::changeEditMethod( const TOOL_EVENT& aEvent ) +int PCB_POINT_EDITOR::changeEditMethod( const TOOL_EVENT& aEvent ) { m_altEditMethod = !m_altEditMethod; return 0; } -void POINT_EDITOR::setTransitions() +void PCB_POINT_EDITOR::setTransitions() { - Go( &POINT_EDITOR::OnSelectionChange, ACTIONS::activatePointEditor.MakeEvent() ); - Go( &POINT_EDITOR::addCorner, PCB_ACTIONS::pointEditorAddCorner.MakeEvent() ); - Go( &POINT_EDITOR::removeCorner, PCB_ACTIONS::pointEditorRemoveCorner.MakeEvent() ); - Go( &POINT_EDITOR::modifiedSelection, EVENTS::SelectedItemsModified ); - Go( &POINT_EDITOR::OnSelectionChange, EVENTS::SelectedEvent ); - Go( &POINT_EDITOR::OnSelectionChange, EVENTS::UnselectedEvent ); - Go( &POINT_EDITOR::changeEditMethod, ACTIONS::changeEditMethod.MakeEvent() ); - Go( &POINT_EDITOR::OnSelectionChange, EVENTS::InhibitSelectionEditing ); - Go( &POINT_EDITOR::OnSelectionChange, EVENTS::UninhibitSelectionEditing ); + Go( &PCB_POINT_EDITOR::OnSelectionChange, ACTIONS::activatePointEditor.MakeEvent() ); + Go( &PCB_POINT_EDITOR::addCorner, PCB_ACTIONS::pointEditorAddCorner.MakeEvent() ); + Go( &PCB_POINT_EDITOR::removeCorner, PCB_ACTIONS::pointEditorRemoveCorner.MakeEvent() ); + Go( &PCB_POINT_EDITOR::modifiedSelection, EVENTS::SelectedItemsModified ); + Go( &PCB_POINT_EDITOR::OnSelectionChange, EVENTS::SelectedEvent ); + Go( &PCB_POINT_EDITOR::OnSelectionChange, EVENTS::UnselectedEvent ); + Go( &PCB_POINT_EDITOR::changeEditMethod, ACTIONS::changeEditMethod.MakeEvent() ); + Go( &PCB_POINT_EDITOR::OnSelectionChange, EVENTS::InhibitSelectionEditing ); + Go( &PCB_POINT_EDITOR::OnSelectionChange, EVENTS::UninhibitSelectionEditing ); } diff --git a/pcbnew/tools/point_editor.h b/pcbnew/tools/pcb_point_editor.h similarity index 97% rename from pcbnew/tools/point_editor.h rename to pcbnew/tools/pcb_point_editor.h index 8bb47b5626..6f380e7b52 100644 --- a/pcbnew/tools/point_editor.h +++ b/pcbnew/tools/pcb_point_editor.h @@ -22,8 +22,8 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ -#ifndef POINT_EDITOR_H -#define POINT_EDITOR_H +#ifndef PCB_POINT_EDITOR_H +#define PCB_POINT_EDITOR_H #include #include "tool/edit_points.h" @@ -36,14 +36,14 @@ class PCB_SELECTION_TOOL; class SHAPE_POLY_SET; /** - * POINT_EDITOR + * PCB_POINT_EDITOR * * Tool that displays edit points allowing to modify items by dragging the points. */ -class POINT_EDITOR : public PCB_TOOL_BASE +class PCB_POINT_EDITOR : public PCB_TOOL_BASE { public: - POINT_EDITOR(); + PCB_POINT_EDITOR(); /// @copydoc TOOL_INTERACTIVE::Reset() void Reset( RESET_REASON aReason ) override; diff --git a/pcbnew/tools/pcb_viewer_tools.cpp b/pcbnew/tools/pcb_viewer_tools.cpp index 6f213e7a3d..a8c99baa90 100644 --- a/pcbnew/tools/pcb_viewer_tools.cpp +++ b/pcbnew/tools/pcb_viewer_tools.cpp @@ -27,7 +27,7 @@ #include #include #include -#include +#include #include #include #include @@ -215,7 +215,7 @@ int PCB_VIEWER_TOOLS::MeasureTool( const TOOL_EVENT& aEvent ) view.Add( &ruler ); view.SetVisible( &ruler, false ); - GRID_HELPER grid( m_toolMgr, frame()->GetMagneticItemsSettings() ); + PCB_GRID_HELPER grid( m_toolMgr, frame()->GetMagneticItemsSettings() ); bool originSet = false;