From ea3c87f243145ec5f3ebff69803d53703d03f116 Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Sat, 16 Dec 2023 14:14:47 +0000 Subject: [PATCH] Move group/ungroup to COMMIT infrastructure. --- common/commit.cpp | 41 +++++++-------- include/commit.h | 6 ++- include/undo_redo_container.h | 2 +- pcbnew/board_commit.cpp | 21 ++++++++ pcbnew/generators/pcb_tuning_pattern.cpp | 5 +- pcbnew/tools/drawing_tool.cpp | 14 ++--- pcbnew/tools/group_tool.cpp | 65 ++++++++++-------------- pcbnew/tools/pcb_actions.cpp | 4 +- pcbnew/undo_redo.cpp | 8 +-- 9 files changed, 86 insertions(+), 80 deletions(-) diff --git a/common/commit.cpp b/common/commit.cpp index 74bd697c6e..8b563cd642 100644 --- a/common/commit.cpp +++ b/common/commit.cpp @@ -80,6 +80,11 @@ COMMIT& COMMIT::Stage( EDA_ITEM* aItem, CHANGE_TYPE aChangeType, BASE_SCREEN* aS break; } + case CHT_GROUP: + case CHT_UNGROUP: + makeEntry( aItem, aChangeType, nullptr, aScreen ); + return *this; + default: wxFAIL; } @@ -182,18 +187,12 @@ CHANGE_TYPE COMMIT::convert( UNDO_REDO aType ) const { switch( aType ) { - case UNDO_REDO::NEWITEM: - return CHT_ADD; - - case UNDO_REDO::DELETED: - return CHT_REMOVE; - - default: - wxASSERT( false ); - KI_FALLTHROUGH; - - case UNDO_REDO::CHANGED: - return CHT_MODIFY; + case UNDO_REDO::NEWITEM: return CHT_ADD; + case UNDO_REDO::DELETED: return CHT_REMOVE; + case UNDO_REDO::GROUP: return CHT_GROUP; + case UNDO_REDO::UNGROUP: return CHT_UNGROUP; + case UNDO_REDO::CHANGED: return CHT_MODIFY; + default: wxASSERT( false ); return CHT_MODIFY; } } @@ -202,18 +201,12 @@ UNDO_REDO COMMIT::convert( CHANGE_TYPE aType ) const { switch( aType ) { - case CHT_ADD: - return UNDO_REDO::NEWITEM; - - case CHT_REMOVE: - return UNDO_REDO::DELETED; - - default: - wxASSERT( false ); - KI_FALLTHROUGH; - - case CHT_MODIFY: - return UNDO_REDO::CHANGED; + case CHT_ADD: return UNDO_REDO::NEWITEM; + case CHT_REMOVE: return UNDO_REDO::DELETED; + case CHT_GROUP: return UNDO_REDO::GROUP; + case CHT_UNGROUP: return UNDO_REDO::UNGROUP; + case CHT_MODIFY: return UNDO_REDO::CHANGED; + default: wxASSERT( false ); return UNDO_REDO::CHANGED; } } diff --git a/include/commit.h b/include/commit.h index aa89fcb89b..3e5d085114 100644 --- a/include/commit.h +++ b/include/commit.h @@ -42,9 +42,11 @@ enum CHANGE_TYPE { CHT_ADD = 1, CHT_REMOVE = 2, CHT_MODIFY = 4, - CHT_TYPE = CHT_ADD | CHT_REMOVE | CHT_MODIFY, + CHT_GROUP = 8, + CHT_UNGROUP = 16, + CHT_TYPE = CHT_ADD | CHT_REMOVE | CHT_MODIFY | CHT_GROUP | CHT_UNGROUP, - CHT_DONE = 8, ///< Flag to indicate the change is already applied + CHT_DONE = 32, ///< Flag to indicate the change is already applied CHT_FLAGS = CHT_DONE }; diff --git a/include/undo_redo_container.h b/include/undo_redo_container.h index 3d012de4eb..e8c071a776 100644 --- a/include/undo_redo_container.h +++ b/include/undo_redo_container.h @@ -67,7 +67,7 @@ enum class UNDO_REDO { DRILLORIGIN, // origin changed (like CHANGED, contains the origin and a copy) GRIDORIGIN, // origin changed (like CHANGED, contains the origin and a copy) PAGESETTINGS, // page settings or title block changes - REGROUP, // new group of items created (do not use GROUP to avoid collision + GROUP, // new group of items created (do not use GROUP to avoid collision // with an header on msys2) UNGROUP // existing group destroyed (items not destroyed) }; diff --git a/pcbnew/board_commit.cpp b/pcbnew/board_commit.cpp index 25fadaddf6..1238da8db1 100644 --- a/pcbnew/board_commit.cpp +++ b/pcbnew/board_commit.cpp @@ -159,6 +159,7 @@ void BOARD_COMMIT::Push( const wxString& aMessage, int aCommitFlags ) bool autofillZones = false; std::vector staleTeardropPadsAndVias; std::set staleTeardropTracks; + PCB_GROUP* addedGroup = nullptr; if( Empty() ) return; @@ -269,6 +270,9 @@ void BOARD_COMMIT::Push( const wxString& aMessage, int aCommitFlags ) } } + if( boardItem->Type() == PCB_GROUP_T ) + addedGroup = static_cast( boardItem ); + if( autofillZones && boardItem->Type() != PCB_MARKER_T ) dirtyIntersectingZones( boardItem, changeType ); @@ -377,6 +381,23 @@ void BOARD_COMMIT::Push( const wxString& aMessage, int aCommitFlags ) break; } + case CHT_UNGROUP: + boardItem->SetParentGroup( nullptr ); + + if( !( aCommitFlags & SKIP_UNDO ) ) + undoList.PushItem( ITEM_PICKER( nullptr, boardItem, UNDO_REDO::UNGROUP ) ); + + break; + + case CHT_GROUP: + if( addedGroup ) + addedGroup->AddItem( boardItem ); + + if( !( aCommitFlags & SKIP_UNDO ) ) + undoList.PushItem( ITEM_PICKER( nullptr, boardItem, UNDO_REDO::GROUP ) ); + + break; + case CHT_MODIFY: { BOARD_ITEM* boardItemCopy = dynamic_cast( ent.m_copy ); diff --git a/pcbnew/generators/pcb_tuning_pattern.cpp b/pcbnew/generators/pcb_tuning_pattern.cpp index c8a65d7dfb..5ab91f04e6 100644 --- a/pcbnew/generators/pcb_tuning_pattern.cpp +++ b/pcbnew/generators/pcb_tuning_pattern.cpp @@ -1343,10 +1343,11 @@ void PCB_TUNING_PATTERN::EditPush( GENERATOR_TOOL* aTool, BOARD* aBoard, && bounds.PointInside( track->GetEnd(), epsilon ) ) { AddItem( item ); - groupUndoList.PushItem( ITEM_PICKER( nullptr, item, UNDO_REDO::REGROUP ) ); + groupUndoList.PushItem( ITEM_PICKER( nullptr, item, UNDO_REDO::GROUP ) ); } } + aCommit->Stage( groupUndoList ); aCommit->Add( item ); } @@ -1357,8 +1358,6 @@ void PCB_TUNING_PATTERN::EditPush( GENERATOR_TOOL* aTool, BOARD* aBoard, aCommitFlags |= APPEND_UNDO; } - - aFrame->AppendCopyToUndoList( groupUndoList, UNDO_REDO::REGROUP ); } diff --git a/pcbnew/tools/drawing_tool.cpp b/pcbnew/tools/drawing_tool.cpp index ef480faa63..d3a7aac0b2 100644 --- a/pcbnew/tools/drawing_tool.cpp +++ b/pcbnew/tools/drawing_tool.cpp @@ -1550,7 +1550,7 @@ int DRAWING_TOOL::PlaceImportedGraphics( const TOOL_EVENT& aEvent ) selectedItems.push_back( item ); } - groupUndoList.PushItem( ITEM_PICKER( nullptr, item, UNDO_REDO::REGROUP ) ); + groupUndoList.PushItem( ITEM_PICKER( nullptr, item, UNDO_REDO::GROUP ) ); layer = item->GetLayer(); @@ -1562,10 +1562,10 @@ int DRAWING_TOOL::PlaceImportedGraphics( const TOOL_EVENT& aEvent ) for( BOARD_ITEM* item : newItems ) commit.Add( item ); - commit.Push( _( "Import Graphics" ) ); - if( groupUndoList.GetCount() > 0 ) - m_frame->AppendCopyToUndoList( groupUndoList, UNDO_REDO::REGROUP ); + commit.Stage( groupUndoList ); + + commit.Push( _( "Import Graphics" ) ); return 0; } @@ -1651,10 +1651,10 @@ int DRAWING_TOOL::PlaceImportedGraphics( const TOOL_EVENT& aEvent ) for( BOARD_ITEM* item : newItems ) commit.Add( item ); - commit.Push( _( "Import Graphics" ) ); - if( groupUndoList.GetCount() > 0 ) - m_frame->AppendCopyToUndoList( groupUndoList, UNDO_REDO::REGROUP ); + commit.Stage( groupUndoList ); + + commit.Push( _( "Import Graphics" ) ); break; // This is a one-shot command, not a tool } diff --git a/pcbnew/tools/group_tool.cpp b/pcbnew/tools/group_tool.cpp index 4c9b038f13..5ebe4353eb 100644 --- a/pcbnew/tools/group_tool.cpp +++ b/pcbnew/tools/group_tool.cpp @@ -249,40 +249,37 @@ int GROUP_TOOL::Group( const TOOL_EVENT& aEvent ) if( selection.Empty() ) return 0; - BOARD* board = getModel(); - PCB_GROUP* group = nullptr; - bool lockGroup = false; + BOARD* board = getModel(); + BOARD_COMMIT commit( m_toolMgr ); + PCB_GROUP* group = nullptr; if( m_isFootprintEditor ) - { group = new PCB_GROUP( board->GetFirstFootprint() ); - board->GetFirstFootprint()->Add( group ); - } else - { group = new PCB_GROUP( board ); - board->Add( group ); - } - - PICKED_ITEMS_LIST undoList; - undoList.PushItem( ITEM_PICKER( nullptr, group, UNDO_REDO::NEWITEM ) ); for( EDA_ITEM* eda_item : selection ) { if( BOARD_ITEM* item = dynamic_cast( eda_item ) ) { if( item->IsLocked() ) - lockGroup = true; - - group->AddItem( item ); - undoList.PushItem( ITEM_PICKER( nullptr, item, UNDO_REDO::REGROUP ) ); + group->SetLocked( true ); } } - m_frame->SaveCopyInUndoList( undoList, UNDO_REDO::REGROUP ); + commit.Add( group ); - if( lockGroup ) - group->SetLocked( true ); + PICKED_ITEMS_LIST groupList; + + for( EDA_ITEM* eda_item : selection ) + { + if( BOARD_ITEM* item = dynamic_cast( eda_item ) ) + groupList.PushItem( ITEM_PICKER( nullptr, item, UNDO_REDO::GROUP ) ); + } + + commit.Stage( groupList ); + + commit.Push( _( "Group Items" ) ); selTool->ClearSelection(); selTool->select( group ); @@ -296,9 +293,9 @@ int GROUP_TOOL::Group( const TOOL_EVENT& aEvent ) int GROUP_TOOL::Ungroup( const TOOL_EVENT& aEvent ) { - const PCB_SELECTION& selection = m_toolMgr->GetTool()->GetSelection(); - BOARD* board = getModel(); - std::vector members; + const PCB_SELECTION& selection = m_toolMgr->GetTool()->GetSelection(); + BOARD_COMMIT commit( m_toolMgr ); + EDA_ITEMS toSelect; if( selection.Empty() ) m_toolMgr->RunAction( PCB_ACTIONS::selectionCursor ); @@ -312,31 +309,25 @@ int GROUP_TOOL::Ungroup( const TOOL_EVENT& aEvent ) if( group ) { - PICKED_ITEMS_LIST undoList; + PICKED_ITEMS_LIST ungroupList; for( BOARD_ITEM* member : group->GetItems() ) { - undoList.PushItem( ITEM_PICKER( nullptr, member, UNDO_REDO::UNGROUP ) ); - members.push_back( member ); + ungroupList.PushItem( ITEM_PICKER( nullptr, member, UNDO_REDO::UNGROUP ) ); + toSelect.push_back( member ); } - group->RemoveAll(); + commit.Stage( ungroupList ); - if( m_isFootprintEditor ) - board->GetFirstFootprint()->Remove( group ); - else - board->Remove( group ); - - canvas()->GetView()->Remove( group ); - - undoList.PushItem( ITEM_PICKER( nullptr, group, UNDO_REDO::DELETED ) ); - m_frame->SaveCopyInUndoList( undoList, UNDO_REDO::UNGROUP ); + group->GetItems().clear(); group->SetSelected(); + commit.Remove( group ); } } - EDA_ITEMS mem( members.begin(), members.end() ); - m_toolMgr->RunAction( PCB_ACTIONS::selectItems, &mem ); + commit.Push( _( "Ungroup Items" ) ); + + m_toolMgr->RunAction( PCB_ACTIONS::selectItems, &toSelect ); m_toolMgr->PostEvent( EVENTS::SelectedItemsModified ); m_frame->OnModify(); diff --git a/pcbnew/tools/pcb_actions.cpp b/pcbnew/tools/pcb_actions.cpp index 0248e86067..f69a94857d 100644 --- a/pcbnew/tools/pcb_actions.cpp +++ b/pcbnew/tools/pcb_actions.cpp @@ -1194,14 +1194,14 @@ TOOL_ACTION PCB_ACTIONS::unlock( TOOL_ACTION_ARGS() TOOL_ACTION PCB_ACTIONS::group( TOOL_ACTION_ARGS() .Name( "pcbnew.EditorControl.group" ) .Scope( AS_GLOBAL ) - .FriendlyName( _( "Group" ) ) + .FriendlyName( _( "Group Items" ) ) .Tooltip( _( "Group the selected items so that they are treated as a single item" ) ) .Icon( BITMAPS::group ) ); TOOL_ACTION PCB_ACTIONS::ungroup( TOOL_ACTION_ARGS() .Name( "pcbnew.EditorControl.ungroup" ) .Scope( AS_GLOBAL ) - .FriendlyName( _( "Ungroup" ) ) + .FriendlyName( _( "Ungroup Items" ) ) .Tooltip( _( "Ungroup any selected groups" ) ) .Icon( BITMAPS::group_ungroup ) ); diff --git a/pcbnew/undo_redo.cpp b/pcbnew/undo_redo.cpp index adfbbdac90..722fd12ff4 100644 --- a/pcbnew/undo_redo.cpp +++ b/pcbnew/undo_redo.cpp @@ -141,7 +141,7 @@ void PCB_BASE_EDIT_FRAME::saveCopyInUndoList( PICKED_ITEMS_LIST* commandToUndo, case UNDO_REDO::NEWITEM: case UNDO_REDO::DELETED: case UNDO_REDO::PAGESETTINGS: - case UNDO_REDO::REGROUP: + case UNDO_REDO::GROUP: case UNDO_REDO::UNGROUP: break; @@ -295,8 +295,8 @@ void PCB_BASE_EDIT_FRAME::PutDataInPreviousState( PICKED_ITEMS_LIST* aList ) UNDO_REDO status = aList->GetPickedItemStatus( ii ); if( status != UNDO_REDO::DELETED + && status != UNDO_REDO::GROUP && status != UNDO_REDO::UNGROUP - && status != UNDO_REDO::REGROUP && status != UNDO_REDO::DRILLORIGIN // origin markers never on board && status != UNDO_REDO::GRIDORIGIN // origin markers never on board && status != UNDO_REDO::PAGESETTINGS ) // nor are page settings proxy items @@ -419,7 +419,7 @@ void PCB_BASE_EDIT_FRAME::PutDataInPreviousState( PICKED_ITEMS_LIST* aList ) break; - case UNDO_REDO::REGROUP: + case UNDO_REDO::GROUP: aList->SetPickedItemStatus( UNDO_REDO::UNGROUP, ii ); if( BOARD_ITEM* boardItem = dynamic_cast( eda_item ) ) @@ -428,7 +428,7 @@ void PCB_BASE_EDIT_FRAME::PutDataInPreviousState( PICKED_ITEMS_LIST* aList ) break; case UNDO_REDO::UNGROUP: - aList->SetPickedItemStatus( UNDO_REDO::REGROUP, ii ); + aList->SetPickedItemStatus( UNDO_REDO::GROUP, ii ); if( BOARD_ITEM* boardItem = dynamic_cast( eda_item ) ) {