From d5275b16aa83c974505def74a0cec756f219e0c8 Mon Sep 17 00:00:00 2001 From: Ian McInerney Date: Fri, 6 Sep 2019 21:52:52 +0200 Subject: [PATCH] eeschema: Find the closest line end to the mouse when grabbing Fixes: lp:1842175 * https://bugs.launchpad.net/kicad/+bug/1842175 --- eeschema/onrightclick.cpp | 10 +++++++++- eeschema/sch_line.cpp | 16 +++++++++++++++- eeschema/sch_line.h | 9 +++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/eeschema/onrightclick.cpp b/eeschema/onrightclick.cpp index aff1cd46e8..b09c9f0527 100644 --- a/eeschema/onrightclick.cpp +++ b/eeschema/onrightclick.cpp @@ -150,7 +150,15 @@ bool SCH_EDIT_FRAME::OnRightClick( const wxPoint& aPosition, wxMenu* PopMenu ) return false; if( item ) - SetCrossHairPosition( item->GetPosition(), false ); + { + SCH_LINE* line = dynamic_cast( item ); + + // The schematic lines have two possible points to use + if( line ) + SetCrossHairPosition( line->GetClosestPoint( aPosition ), false ); + else + SetCrossHairPosition( item->GetPosition(), false ); + } } // If a command is in progress: add "cancel" and "end tool" menu diff --git a/eeschema/sch_line.cpp b/eeschema/sch_line.cpp index da795077dc..814b72e69d 100644 --- a/eeschema/sch_line.cpp +++ b/eeschema/sch_line.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -299,7 +300,7 @@ int SCH_LINE::GetPenSize() const { if( m_size > 0 ) return m_size; - + return GetDefaultWidth(); } @@ -487,6 +488,19 @@ EDA_ITEM* SCH_LINE::MergeOverlap( SCH_LINE* aLine ) } +wxPoint SCH_LINE::GetClosestPoint( const wxPoint& aPoint ) +{ + // Compute the euclidean distance between the reference and the ends + double startDis = GetLineLength( aPoint, m_start ); + double endDis = GetLineLength( aPoint, m_end ); + + if( startDis < endDis ) + return m_start; + else + return m_end; +} + + void SCH_LINE::GetEndPoints( std::vector & aItemList ) { if( GetLayer() == LAYER_NOTES ) diff --git a/eeschema/sch_line.h b/eeschema/sch_line.h index d4d8870e7f..d8914df70d 100644 --- a/eeschema/sch_line.h +++ b/eeschema/sch_line.h @@ -81,6 +81,15 @@ public: void SetEndPoint( const wxPoint& aPosition ) { m_end = aPosition; } + /** + * Find the end point of the line (either start or end) that is closest + * to the provided point. + * + * @param aPoint the point to compare against + * @return The coordinates of the closest end point + */ + wxPoint GetClosestPoint( const wxPoint& aPoint ); + int GetDefaultStyle() const; void SetLineStyle( const int aStyle );