Fix a bunch of group issues with importing graphics.

Groups do not own their children; they only reference them.  That
being said, they do own *selection* and *moving* of their children.
This might address some of 6493, but in any case will be a
pre-requisite for fixing it.

Fixes https://gitlab.com/kicad/code/kicad/issues/6493
This commit is contained in:
Jeff Young 2020-11-27 18:02:31 +00:00
parent 4f651901aa
commit 0293f880ec
1 changed files with 32 additions and 51 deletions

View File

@ -1017,65 +1017,59 @@ int DRAWING_TOOL::PlaceImportedGraphics( const TOOL_EVENT& aEvent )
m_toolMgr->RunAction( ACTIONS::cancelInteractive, true ); m_toolMgr->RunAction( ACTIONS::cancelInteractive, true );
// Add a VIEW_GROUP that serves as a preview for the new item std::vector<BOARD_ITEM*> newItems; // all new items, including group
PCBNEW_SELECTION preview; std::vector<BOARD_ITEM*> selectedItems; // the group, or newItems if no group
BOARD_COMMIT commit( m_frame ); PCBNEW_SELECTION preview;
PCB_GROUP* grp = nullptr; BOARD_COMMIT commit( m_frame );
PCB_GROUP* group = nullptr;
if( dlg.ShouldGroupItems() ) if( dlg.ShouldGroupItems() )
{ {
if( m_isFootprintEditor ) if( m_isFootprintEditor )
grp = new PCB_GROUP( m_frame->GetBoard()->GetFirstFootprint() ); group = new PCB_GROUP( m_frame->GetBoard()->GetFirstFootprint() );
else else
grp = new PCB_GROUP( m_frame->GetBoard() ); group = new PCB_GROUP( m_frame->GetBoard() );
newItems.push_back( group );
selectedItems.push_back( group );
preview.Add( group );
} }
// Build the undo list & add items to the current view for( std::unique_ptr<EDA_ITEM>& ptr : list )
for( auto& ptr : list)
{ {
EDA_ITEM* item = ptr.get(); BOARD_ITEM* item = static_cast<BOARD_ITEM*>( ptr.get() );
if( m_isFootprintEditor ) if( m_isFootprintEditor )
wxASSERT( item->Type() == PCB_FP_SHAPE_T || item->Type() == PCB_FP_TEXT_T ); wxASSERT( item->Type() == PCB_FP_SHAPE_T || item->Type() == PCB_FP_TEXT_T );
else else
wxASSERT( item->Type() == PCB_SHAPE_T || item->Type() == PCB_TEXT_T ); wxASSERT( item->Type() == PCB_SHAPE_T || item->Type() == PCB_TEXT_T );
if( grp ) newItems.push_back( item );
grp->AddItem( static_cast<BOARD_ITEM*>( item ) );
else if( dlg.IsPlacementInteractive() ) if( group )
preview.Add( item ); group->AddItem( item );
else else
commit.Add( item ); selectedItems.push_back( item );
preview.Add( item );
ptr.release(); ptr.release();
} }
if( !dlg.IsPlacementInteractive() ) if( !dlg.IsPlacementInteractive() )
{ {
if( grp ) for( BOARD_ITEM* item : newItems )
{ commit.Add( item );
grp->AddChildrenToCommit( commit );
commit.Add( grp );
}
commit.Push( _( "Place a DXF_SVG drawing" ) ); commit.Push( _( "Place a DXF_SVG drawing" ) );
return 0; return 0;
} }
if( grp )
preview.Add( grp );
std::vector<BOARD_ITEM*> newItems;
for( EDA_ITEM* item : preview )
newItems.push_back( static_cast<BOARD_ITEM*>( item ) );
BOARD_ITEM* firstItem = static_cast<BOARD_ITEM*>( preview.Front() );
m_view->Add( &preview ); m_view->Add( &preview );
// Clear the current selection then select the drawings so that edit tools work on them // Clear the current selection then select the drawings so that edit tools work on them
m_toolMgr->RunAction( PCB_ACTIONS::selectionClear, true ); m_toolMgr->RunAction( PCB_ACTIONS::selectionClear, true );
m_toolMgr->RunAction( PCB_ACTIONS::selectItems, true, &newItems ); m_toolMgr->RunAction( PCB_ACTIONS::selectItems, true, &selectedItems );
m_controls->ShowCursor( true ); m_controls->ShowCursor( true );
m_controls->ForceCursorPosition( false ); m_controls->ForceCursorPosition( false );
@ -1084,10 +1078,10 @@ int DRAWING_TOOL::PlaceImportedGraphics( const TOOL_EVENT& aEvent )
// Now move the new items to the current cursor position: // Now move the new items to the current cursor position:
VECTOR2I cursorPos = m_controls->GetCursorPosition(); VECTOR2I cursorPos = m_controls->GetCursorPosition();
VECTOR2I delta = cursorPos - firstItem->GetPosition(); VECTOR2I delta = cursorPos - static_cast<BOARD_ITEM*>( preview.Front() )->GetPosition();
for( EDA_ITEM* item : preview ) for( BOARD_ITEM* item : selectedItems )
static_cast<BOARD_ITEM*>( item )->Move( (wxPoint) delta ); item->Move( (wxPoint) delta );
m_view->Update( &preview ); m_view->Update( &preview );
@ -1114,25 +1108,17 @@ int DRAWING_TOOL::PlaceImportedGraphics( const TOOL_EVENT& aEvent )
{ {
m_toolMgr->RunAction( PCB_ACTIONS::selectionClear, true ); m_toolMgr->RunAction( PCB_ACTIONS::selectionClear, true );
// If a group is being used, we must delete the items themselves, for( BOARD_ITEM* item : newItems )
// since they are only in the group and not in the preview delete item;
if( grp )
{
grp->RunOnChildren( [&]( BOARD_ITEM* bItem )
{
delete bItem ;
} );
}
preview.FreeItems();
break; break;
} }
else if( evt->IsMotion() ) else if( evt->IsMotion() )
{ {
delta = cursorPos - firstItem->GetPosition(); delta = cursorPos - static_cast<BOARD_ITEM*>( preview.Front() )->GetPosition();
for( auto item : preview ) for( BOARD_ITEM* item : selectedItems )
static_cast<BOARD_ITEM*>( item )->Move( (wxPoint) delta ); item->Move( (wxPoint) delta );
m_view->Update( &preview ); m_view->Update( &preview );
} }
@ -1143,13 +1129,8 @@ int DRAWING_TOOL::PlaceImportedGraphics( const TOOL_EVENT& aEvent )
else if( evt->IsClick( BUT_LEFT ) ) else if( evt->IsClick( BUT_LEFT ) )
{ {
// Place the imported drawings // Place the imported drawings
for( EDA_ITEM* item : preview ) for( BOARD_ITEM* item : newItems )
{
if( item->Type() == PCB_GROUP_T )
static_cast<PCB_GROUP*>( item )->AddChildrenToCommit( commit );
commit.Add( item ); commit.Add( item );
}
commit.Push( _( "Place a DXF_SVG drawing" ) ); commit.Push( _( "Place a DXF_SVG drawing" ) );
break; // This is a one-shot command, not a tool break; // This is a one-shot command, not a tool