File: undo_redo.cpp

package info (click to toggle)
kicad 9.0.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 769,124 kB
  • sloc: cpp: 960,330; ansic: 121,001; xml: 66,428; python: 18,382; sh: 1,010; awk: 301; asm: 292; makefile: 227; javascript: 167; perl: 10
file content (668 lines) | stat: -rw-r--r-- 22,246 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2012 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
 * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
 * Copyright (C) 2016 CERN
 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
 * @author Maciej Suminski <maciej.suminski@cern.ch>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

#include <functional>
using namespace std::placeholders;
#include <macros.h>
#include <pcb_edit_frame.h>
#include <pcb_track.h>
#include <pcb_group.h>
#include <pcb_generator.h>
#include <pcb_target.h>
#include <footprint.h>
#include <lset.h>
#include <pad.h>
#include <origin_viewitem.h>
#include <connectivity/connectivity_data.h>
#include <tool/tool_manager.h>
#include <tool/actions.h>
#include <tools/pcb_selection_tool.h>
#include <tools/pcb_control.h>
#include <tools/board_editor_control.h>
#include <board_commit.h>
#include <drawing_sheet/ds_proxy_undo_item.h>
#include <wx/msgdlg.h>

/* Functions to undo and redo edit commands.
 *  commands to undo are stored in CurrentScreen->m_UndoList
 *  commands to redo are stored in CurrentScreen->m_RedoList
 *
 *  m_UndoList and m_RedoList handle a std::vector of PICKED_ITEMS_LIST
 *  Each PICKED_ITEMS_LIST handle a std::vector of pickers (class ITEM_PICKER),
 *  that store the list of schematic items that are concerned by the command to undo or redo
 *  and is created for each command to undo (handle also a command to redo).
 *  each picker has a pointer pointing to an item to undo or redo (in fact: deleted, added or
 *  modified),
 * and has a pointer to a copy of this item, when this item has been modified
 * (the old values of parameters are therefore saved)
 *
 *  there are 3 cases:
 *  - delete item(s) command
 *  - change item(s) command
 *  - add item(s) command
 *
 *  Undo command
 *  - delete item(s) command:
 *       =>  deleted items are moved in undo list
 *
 *  - change item(s) command
 *      => A copy of item(s) is made (a DrawPickedStruct list of wrappers)
 *      the .m_Link member of each wrapper points the modified item.
 *      the .m_Item member of each wrapper points the old copy of this item.
 *
 *  - add item(s) command
 *      =>A list of item(s) is made. The .m_Item member of each wrapper points the new item.
 *
 *  Redo command
 *  - delete item(s) old command:
 *      => deleted items are moved in EEDrawList list, and in
 *
 *  - change item(s) command
 *      => the copy of item(s) is moved in Undo list
 *
 *  - add item(s) command
 *      => The list of item(s) is used to create a deleted list in undo list(same as a delete
 *         command)
 *
 *   Some block operations that change items can be undone without memorize items, just the
 *   coordinates of the transform:
 *      move list of items (undo/redo is made by moving with the opposite move vector)
 *      mirror (Y) and flip list of items (undo/redo is made by mirror or flip items)
 *      so they are handled specifically.
 *
 */


void PCB_BASE_EDIT_FRAME::saveCopyInUndoList( PICKED_ITEMS_LIST* commandToUndo,
                                              const PICKED_ITEMS_LIST& aItemsList,
                                              UNDO_REDO aCommandType )
{
    int preExisting = commandToUndo->GetCount();

    for( unsigned ii = 0; ii < aItemsList.GetCount(); ii++ )
        commandToUndo->PushItem( aItemsList.GetItemWrapper(ii) );

    for( unsigned ii = preExisting; ii < commandToUndo->GetCount(); ii++ )
    {
        EDA_ITEM* item    = commandToUndo->GetPickedItem( ii );
        UNDO_REDO command = commandToUndo->GetPickedItemStatus( ii );

        if( command == UNDO_REDO::UNSPECIFIED )
        {
            command = aCommandType;
            commandToUndo->SetPickedItemStatus( command, ii );
        }

        wxASSERT( item );

        switch( command )
        {
        case UNDO_REDO::CHANGED:
        case UNDO_REDO::DRILLORIGIN:
        case UNDO_REDO::GRIDORIGIN:
            // If we don't yet have a copy in the link, set one up
            if( !commandToUndo->GetPickedItemLink( ii ) )
                commandToUndo->SetPickedItemLink( BOARD_COMMIT::MakeImage( item ), ii );

            break;

        case UNDO_REDO::NEWITEM:
        case UNDO_REDO::DELETED:
        case UNDO_REDO::PAGESETTINGS:
        case UNDO_REDO::REGROUP:
        case UNDO_REDO::UNGROUP:
            break;

        default:
            wxFAIL_MSG( wxString::Format( wxT( "Unrecognized undo command: %X" ), command ) );
            break;
        }
    }

    if( commandToUndo->GetCount() )
    {
        /* Save the copy in undo list */
        PushCommandToUndoList( commandToUndo );

        /* Clear redo list, because after a new command one cannot redo a command */
        ClearUndoORRedoList( REDO_LIST );
    }
    else
    {
        // Should not occur
        wxASSERT( false );
        delete commandToUndo;
    }
}


void PCB_BASE_EDIT_FRAME::SaveCopyInUndoList( EDA_ITEM* aItem, UNDO_REDO aCommandType )
{
    PICKED_ITEMS_LIST* commandToUndo = new PICKED_ITEMS_LIST();
    PICKED_ITEMS_LIST  itemsList;

    itemsList.PushItem( ITEM_PICKER( nullptr, aItem, aCommandType ) );
    saveCopyInUndoList( commandToUndo, itemsList, aCommandType );
}


void PCB_BASE_EDIT_FRAME::SaveCopyInUndoList( const PICKED_ITEMS_LIST& aItemsList,
                                              UNDO_REDO aCommandType )
{
    PICKED_ITEMS_LIST* commandToUndo = new PICKED_ITEMS_LIST();
    commandToUndo->SetDescription( aItemsList.GetDescription() );

    saveCopyInUndoList( commandToUndo, aItemsList, aCommandType );
}


void PCB_BASE_EDIT_FRAME::AppendCopyToUndoList( const PICKED_ITEMS_LIST& aItemsList,
                                                UNDO_REDO aCommandType )
{
    PICKED_ITEMS_LIST* commandToUndo = PopCommandFromUndoList();

    if( !commandToUndo )
    {
        commandToUndo = new PICKED_ITEMS_LIST();
        commandToUndo->SetDescription( aItemsList.GetDescription() );
    }

    saveCopyInUndoList( commandToUndo, aItemsList, aCommandType );
}


void PCB_BASE_EDIT_FRAME::RestoreCopyFromUndoList( wxCommandEvent& aEvent )
{
    if( UndoRedoBlocked() )
        return;

    if( GetUndoCommandCount() <= 0 )
        return;

    // Inform tools that undo command was issued
    m_toolManager->ProcessEvent( { TC_MESSAGE, TA_UNDO_REDO_PRE, AS_GLOBAL } );

    // Get the old list
    PICKED_ITEMS_LIST* list = PopCommandFromUndoList();

    // Undo the command
    PutDataInPreviousState( list );

    // Put the old list in RedoList
    list->ReversePickersListOrder();
    PushCommandToRedoList( list );

    OnModify();

    m_toolManager->ProcessEvent( { TC_MESSAGE, TA_UNDO_REDO_POST, AS_GLOBAL } );
    m_toolManager->PostEvent( EVENTS::SelectedItemsModified );

    GetCanvas()->Refresh();
}


void PCB_BASE_EDIT_FRAME::RestoreCopyFromRedoList( wxCommandEvent& aEvent )
{
    if( UndoRedoBlocked() )
        return;

    if( GetRedoCommandCount() == 0 )
        return;

    // Inform tools that redo command was issued
    m_toolManager->ProcessEvent( EVENTS::UndoRedoPreEvent );

    // Get the old list
    PICKED_ITEMS_LIST* list = PopCommandFromRedoList();

    // Redo the command
    PutDataInPreviousState( list );

    // Put the old list in UndoList
    list->ReversePickersListOrder();
    PushCommandToUndoList( list );

    OnModify();

    m_toolManager->ProcessEvent( EVENTS::UndoRedoPostEvent );
    m_toolManager->PostEvent( EVENTS::SelectedItemsModified );

    GetCanvas()->Refresh();
}


void PCB_BASE_EDIT_FRAME::PutDataInPreviousState( PICKED_ITEMS_LIST* aList )
{
    bool not_found = false;
    bool reBuild_ratsnest = false;
    bool deep_reBuild_ratsnest = false;  // true later if pointers must be rebuilt
    bool solder_mask_dirty = false;

    auto view = GetCanvas()->GetView();
    auto connectivity = GetBoard()->GetConnectivity();

    GetBoard()->IncrementTimeStamp();   // clear caches

    // Enum to track the modification type of items. Used to enable bulk BOARD_LISTENER
    // callbacks at the end of the undo / redo operation
    enum ITEM_CHANGE_TYPE
    {
        ADDED,
        DELETED,
        CHANGED
    };

    std::unordered_map<EDA_ITEM*, ITEM_CHANGE_TYPE> item_changes;

    auto update_item_change_state =
            [&]( EDA_ITEM* item, ITEM_CHANGE_TYPE change_type )
            {
                auto item_itr = item_changes.find( item );

                if( item_itr == item_changes.end() )
                {
                    // First time we've seen this item - tag the current change type
                    item_changes.insert( { item, change_type } );
                    return;
                }

                // Update the item state based on the current and next change type
                switch( item_itr->second )
                {
                case ITEM_CHANGE_TYPE::ADDED:
                {
                    if( change_type == ITEM_CHANGE_TYPE::DELETED )
                    {
                        // The item was previously added, now deleted - as far as bulk callbacks
                        // are concerned, the item has never existed
                        item_changes.erase( item_itr );
                    }
                    else if( change_type == ITEM_CHANGE_TYPE::ADDED )
                    {
                        // Error condition - added an already added item
                        wxASSERT_MSG( false, wxT( "UndoRedo: should not add already added item" ) );
                    }

                    // For all other cases, the item remains as ADDED as seen by the bulk callbacks
                    break;
                }
                case ITEM_CHANGE_TYPE::DELETED:
                {
                    // This is an error condition - item has already been deleted so should not
                    // be operated on further
                    wxASSERT_MSG( false, wxT( "UndoRedo: should not alter already deleted item" ) );
                    break;
                }
                case ITEM_CHANGE_TYPE::CHANGED:
                {
                    if( change_type == ITEM_CHANGE_TYPE::DELETED )
                    {
                        item_itr->second = ITEM_CHANGE_TYPE::DELETED;
                    }
                    else if( change_type == ITEM_CHANGE_TYPE::ADDED )
                    {
                        // This is an error condition - item has already been changed so should not
                        // be added
                        wxASSERT_MSG( false,
                                      wxT( "UndoRedo: should not add already changed item" ) );
                    }

                    // Otherwise, item remains CHANGED
                    break;
                }
                }
            };

    // Undo in the reverse order of list creation: (this can allow stacked changes
    // like the same item can be changes and deleted in the same complex command

    // Restore changes in reverse order
    for( int ii = (int) aList->GetCount() - 1; ii >= 0 ; ii-- )
    {
        EDA_ITEM* eda_item = aList->GetPickedItem( (unsigned) ii );

        /* Test for existence of item on board.
         * It could be deleted, and no more on board:
         *   - if a call to SaveCopyInUndoList was forgotten in Pcbnew
         *   - in zones outlines, when a change in one zone merges this zone with an other
         * This test avoids a Pcbnew crash
         * Obviously, this test is not made for deleted items
         */
        UNDO_REDO status = aList->GetPickedItemStatus( ii );

        if( status != UNDO_REDO::DELETED
                && status != UNDO_REDO::REGROUP
                && status != UNDO_REDO::UNGROUP
                && 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
        {
            if( GetBoard()->GetItem( eda_item->m_Uuid ) == DELETED_BOARD_ITEM::GetInstance() )
            {
                // Remove this non existent item
                aList->RemovePicker( ii );
                not_found = true;

                if( aList->GetCount() == 0 )
                    break;

                continue;
            }
        }

        // see if we must rebuild ratsnets and pointers lists
        switch( eda_item->Type() )
        {
        case PCB_FOOTPRINT_T:
            deep_reBuild_ratsnest = true;   // Pointers on pads can be invalid
            KI_FALLTHROUGH;

        case PCB_ZONE_T:
        case PCB_TRACE_T:
        case PCB_ARC_T:
        case PCB_VIA_T:
        case PCB_PAD_T:
            reBuild_ratsnest = true;
            break;

        case PCB_NETINFO_T:
            reBuild_ratsnest = true;
            deep_reBuild_ratsnest = true;
            break;

        default:
            break;
        }

        switch( eda_item->Type() )
        {
        case PCB_FOOTPRINT_T:
            solder_mask_dirty = true;
            break;

        case PCB_VIA_T:
            solder_mask_dirty = true;
            break;

        case PCB_ZONE_T:
        case PCB_TRACE_T:
        case PCB_ARC_T:
        case PCB_PAD_T:
        case PCB_SHAPE_T:
        {
            LSET layers = static_cast<BOARD_ITEM*>( eda_item )->GetLayerSet();

            if( layers.test( F_Mask ) || layers.test( B_Mask ) )
                solder_mask_dirty = true;

            break;
        }

        default:
            break;
        }

        switch( aList->GetPickedItemStatus( ii ) )
        {
        case UNDO_REDO::CHANGED:    /* Exchange old and new data for each item */
        {
            BOARD_ITEM*           item = (BOARD_ITEM*) eda_item;
            BOARD_ITEM_CONTAINER* parent = GetBoard();
            PCB_GROUP*            parentGroup = item->GetParentGroup();

            if( item->GetParentFootprint() )
            {
                // We need the current item and it's parent, which may be different from what
                // was stored if we're multiple frames up the undo stack.
                item = GetBoard()->GetItem( item->m_Uuid );
                parent = item->GetParentFootprint();
            }

            BOARD_ITEM* image = (BOARD_ITEM*) aList->GetPickedItemLink( ii );

            view->Remove( item );

            if( parentGroup )
                parentGroup->RemoveItem( item );

            parent->Remove( item );

            item->SwapItemData( image );

            item->ClearFlags( UR_TRANSIENT );
            image->SetFlags( UR_TRANSIENT );

            if( PCB_GROUP* group = dynamic_cast<PCB_GROUP*>( item ) )
            {
                group->RunOnChildren( [&]( BOARD_ITEM* child )
                                      {
                                          child->SetParentGroup( group );
                                      } );
            }

            view->Add( item );
            view->Hide( item, false );
            parent->Add( item );

            if( parentGroup )
                parentGroup->AddItem( item );

            update_item_change_state( item, ITEM_CHANGE_TYPE::CHANGED );
            break;
        }

        case UNDO_REDO::NEWITEM:        /* new items are deleted */
            aList->SetPickedItemStatus( UNDO_REDO::DELETED, ii );
            GetModel()->Remove( (BOARD_ITEM*) eda_item, REMOVE_MODE::BULK );
            update_item_change_state( eda_item, ITEM_CHANGE_TYPE::DELETED );

            if( eda_item->Type() != PCB_NETINFO_T )
                view->Remove( eda_item );

            eda_item->SetFlags( UR_TRANSIENT );

            break;

        case UNDO_REDO::DELETED:    /* deleted items are put in List, as new items */
            aList->SetPickedItemStatus( UNDO_REDO::NEWITEM, ii );

            eda_item->ClearFlags( UR_TRANSIENT );

            GetModel()->Add( (BOARD_ITEM*) eda_item, ADD_MODE::BULK_APPEND );
            update_item_change_state( eda_item, ITEM_CHANGE_TYPE::ADDED );

            if( eda_item->Type() != PCB_NETINFO_T )
                view->Add( eda_item );

            break;

        case UNDO_REDO::REGROUP:    /* grouped items are ungrouped */
            aList->SetPickedItemStatus( UNDO_REDO::UNGROUP, ii );

            if( eda_item->IsBOARD_ITEM() )
            {
                BOARD_ITEM* boardItem = static_cast<BOARD_ITEM*>( eda_item );

                if( PCB_GROUP* group = boardItem->GetParentGroup() )
                {
                    aList->SetPickedItemGroupId( group->m_Uuid, ii );

                    group->RemoveItem( boardItem );
                }
            }

            break;

        case UNDO_REDO::UNGROUP:    /* ungrouped items are re-added to their previuos groups */
            aList->SetPickedItemStatus( UNDO_REDO::REGROUP, ii );

            if( eda_item->IsBOARD_ITEM() )
            {
                BOARD_ITEM* boardItem = static_cast<BOARD_ITEM*>( eda_item );

                PCB_GROUP* group = dynamic_cast<PCB_GROUP*>(
                        GetBoard()->GetItem( aList->GetPickedItemGroupId( ii ) ) );

                if( group )
                    group->AddItem( boardItem );
            }

            break;

        case UNDO_REDO::DRILLORIGIN:
        case UNDO_REDO::GRIDORIGIN:
        {
            // Warning: DRILLORIGIN and GRIDORIGIN undo/redo command create EDA_ITEMs
            // that cannot be casted to BOARD_ITEMs
            EDA_ITEM* image = aList->GetPickedItemLink( ii );
            VECTOR2D origin = image->GetPosition();
            image->SetPosition( eda_item->GetPosition() );

            if( aList->GetPickedItemStatus( ii ) == UNDO_REDO::DRILLORIGIN )
                BOARD_EDITOR_CONTROL::DoSetDrillOrigin( view, this, eda_item, origin );
            else
                PCB_CONTROL::DoSetGridOrigin( view, this, eda_item, origin );

            break;
        }

        case UNDO_REDO::PAGESETTINGS:
        {
            // swap current settings with stored settings
            DS_PROXY_UNDO_ITEM  alt_item( this );
            DS_PROXY_UNDO_ITEM* item = static_cast<DS_PROXY_UNDO_ITEM*>( eda_item );
            item->Restore( this );
            *item = std::move( alt_item );
            break;
        }

        default:
            wxFAIL_MSG( wxString::Format( wxT( "PutDataInPreviousState() error (unknown code %X)" ),
                                          aList->GetPickedItemStatus( ii ) ) );
            break;
        }
    }

    if( not_found )
        wxMessageBox( _( "Incomplete undo/redo operation: some items not found" ) );

    if( IsType( FRAME_PCB_EDITOR ) )
    {
        if( reBuild_ratsnest || deep_reBuild_ratsnest )
        {
            // Connectivity may have changed; rebuild internal caches to remove stale items
            GetBoard()->BuildConnectivity();
            Compile_Ratsnest( false );
        }

        if( solder_mask_dirty )
            HideSolderMask();
    }

    PCB_SELECTION_TOOL* selTool = m_toolManager->GetTool<PCB_SELECTION_TOOL>();
    selTool->RebuildSelection();

    GetBoard()->SanitizeNetcodes();

    // Invoke bulk BOARD_LISTENER callbacks
    std::vector<BOARD_ITEM*> added_items, deleted_items, changed_items;

    for( auto& [item, changeType] : item_changes )
    {
        switch( changeType )
        {
        case ITEM_CHANGE_TYPE::ADDED:
            added_items.push_back( static_cast<BOARD_ITEM*>( item ) );
            break;

        case ITEM_CHANGE_TYPE::DELETED:
            deleted_items.push_back( static_cast<BOARD_ITEM*>( item ) );
            break;

        case ITEM_CHANGE_TYPE::CHANGED:
            changed_items.push_back( static_cast<BOARD_ITEM*>( item ) );
            break;
        }
    }

    if( added_items.size() > 0 || deleted_items.size() > 0 || changed_items.size() > 0 )
        GetBoard()->OnItemsCompositeUpdate( added_items, deleted_items, changed_items );
}


void PCB_BASE_EDIT_FRAME::ClearUndoORRedoList( UNDO_REDO_LIST whichList, int aItemCount )
{
    if( aItemCount == 0 )
        return;

    UNDO_REDO_CONTAINER& list = ( whichList == UNDO_LIST ) ? m_undoList : m_redoList;

    if( aItemCount < 0 )
    {
        list.ClearCommandList();
    }
    else
    {
        for( int ii = 0; ii < aItemCount; ii++ )
        {
            if( list.m_CommandsList.size() == 0 )
                break;

            PICKED_ITEMS_LIST* curr_cmd = list.m_CommandsList[0];
            list.m_CommandsList.erase( list.m_CommandsList.begin() );
            ClearListAndDeleteItems( curr_cmd );
            delete curr_cmd;    // Delete command
        }
    }
}


void PCB_BASE_EDIT_FRAME::ClearListAndDeleteItems( PICKED_ITEMS_LIST* aList )
{
    aList->ClearListAndDeleteItems(
            []( EDA_ITEM* item )
            {
                wxASSERT_MSG( item->HasFlag( UR_TRANSIENT ),
                              "Item on undo/redo list not owned by undo/redo!" );

                if( item->IsBOARD_ITEM() )
                    static_cast<BOARD_ITEM*>( item )->SetParentGroup( nullptr );

                delete item;
            } );
}


void PCB_BASE_EDIT_FRAME::RollbackFromUndo()
{
    PICKED_ITEMS_LIST* undo = PopCommandFromUndoList();
    PutDataInPreviousState( undo );
    ClearListAndDeleteItems( undo );
    delete undo;

    GetCanvas()->Refresh();
}