Use the mean of all positions as center when rotating label-only selection

Fixes https://gitlab.com/kicad/code/kicad/issues/9690
This commit is contained in:
Mikolaj Wielgus 2021-12-03 16:17:02 +01:00
parent c186d1f319
commit 5f8f1542f8
1 changed files with 18 additions and 6 deletions

View File

@ -71,29 +71,41 @@ bool SELECTION::Contains( EDA_ITEM* aItem ) const
VECTOR2I SELECTION::GetCenter() const VECTOR2I SELECTION::GetCenter() const
{ {
KICAD_T labelTypes[] = { SCH_LABEL_T, SCH_GLOBAL_LABEL_T, SCH_HIER_LABEL_T, EOT }; 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 // If the selection contains only labels calculate the center as the mean of all positions
// calculating the centerpoint. // instead of using the center of the total bounding box. Otherwise rotating the selection will
// also translate it.
for( EDA_ITEM* item : m_items ) for( EDA_ITEM* item : m_items )
{ {
if( !item->IsType( labelTypes ) ) if( !item->IsType( labelTypes ) )
{ {
includeLabels = false; hasOnlyLabels = false;
break; break;
} }
} }
EDA_RECT bbox; 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<VECTOR2I>( center );
}
for( EDA_ITEM* item : m_items ) for( EDA_ITEM* item : m_items )
{ {
if( !item->IsType( labelTypes ) || includeLabels ) if( !item->IsType( labelTypes ) )
bbox.Merge( item->GetBoundingBox() ); bbox.Merge( item->GetBoundingBox() );
} }
return static_cast<VECTOR2I>( bbox.Centre() ); return static_cast<VECTOR2I>( bbox.GetCenter() );
} }