From 5f8f1542f8595ff9920f296205e1e5cbe597cfa1 Mon Sep 17 00:00:00 2001 From: Mikolaj Wielgus Date: Fri, 3 Dec 2021 16:17:02 +0100 Subject: [PATCH] Use the mean of all positions as center when rotating label-only selection Fixes https://gitlab.com/kicad/code/kicad/issues/9690 --- common/tool/selection.cpp | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/common/tool/selection.cpp b/common/tool/selection.cpp index c9861c32d0..e25aacf76e 100644 --- a/common/tool/selection.cpp +++ b/common/tool/selection.cpp @@ -71,29 +71,41 @@ bool SELECTION::Contains( EDA_ITEM* aItem ) const VECTOR2I SELECTION::GetCenter() const { KICAD_T labelTypes[] = { SCH_LABEL_T, SCH_GLOBAL_LABEL_T, SCH_HIER_LABEL_T, EOT }; - bool includeLabels = true; + bool hasOnlyLabels = true; - // If the selection contains at least one non-label then don't include labels when - // calculating the centerpoint. + // If the selection contains only labels calculate the center as the mean of all positions + // instead of using the center of the total bounding box. Otherwise rotating the selection will + // also translate it. for( EDA_ITEM* item : m_items ) { if( !item->IsType( labelTypes ) ) { - includeLabels = false; + hasOnlyLabels = false; break; } } EDA_RECT bbox; + if( hasOnlyLabels ) + { + wxPoint center( 0, 0 ); + + for( EDA_ITEM* item : m_items ) + center += item->GetPosition(); + + center = center / m_items.size(); + return static_cast( center ); + } + for( EDA_ITEM* item : m_items ) { - if( !item->IsType( labelTypes ) || includeLabels ) + if( !item->IsType( labelTypes ) ) bbox.Merge( item->GetBoundingBox() ); } - return static_cast( bbox.Centre() ); + return static_cast( bbox.GetCenter() ); }