kicad/eeschema/schematic_undo_redo.cpp

404 lines
13 KiB
C++
Raw Normal View History

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2004 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
* Copyright (C) 2004-2021 KiCad Developers, see change_log.txt for contributors.
*
* 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 <ee_actions.h>
2018-01-30 10:49:51 +00:00
#include <sch_edit_frame.h>
#include <tool/tool_manager.h>
#include <schematic.h>
#include <sch_bus_entry.h>
#include <sch_junction.h>
#include <sch_line.h>
#include <sch_bitmap.h>
#include <tools/ee_selection_tool.h>
2021-02-22 23:47:17 +00:00
#include <drawing_sheet/ds_proxy_undo_item.h>
#include <tool/actions.h>
/* Functions to undo and redo edit commands.
2008-02-26 19:19:54 +00:00
*
* 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)
2008-02-26 19:19:54 +00:00
*
2007-09-01 12:00:30 +00:00
* there are 3 cases:
* - delete item(s) command
* - change item(s) command
* - add item(s) command
* and 2 cases for block:
* - move list of items
* - mirror (Y) list of items
2008-02-26 19:19:54 +00:00
*
2007-09-01 12:00:30 +00:00
* Undo command
* - delete item(s) command:
* => deleted items are moved in undo list
2008-02-26 19:19:54 +00:00
*
2007-09-01 12:00:30 +00:00
* - 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.
2008-02-26 19:19:54 +00:00
*
2007-09-01 12:00:30 +00:00
* - add item(s) command
* =>A list of item(s) is made. The .m_Item member of each wrapper points
* the new item.
2008-02-26 19:19:54 +00:00
*
2007-09-01 12:00:30 +00:00
* Redo command
* - delete item(s) old command:
* => deleted items are moved into m_tree
2008-02-26 19:19:54 +00:00
*
2007-09-01 12:00:30 +00:00
* - change item(s) command
* => the copy of item(s) is moved in Undo list
2008-02-26 19:19:54 +00:00
*
2007-09-01 12:00:30 +00:00
* - add item(s) command
* => The list of item(s) is used to create a deleted list in undo
* list(same as a delete command)
2008-02-26 19:19:54 +00:00
*
* Some block operations that change items can be undone without memorized
* 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.
*
2007-09-01 12:00:30 +00:00
* A problem is the hierarchical sheet handling.
* the data associated (sub-hierarchy, undo/redo list) is deleted only
2007-09-01 12:00:30 +00:00
* when the sheet is really deleted (i.e. when deleted from undo or redo list)
* This is handled by its destructor.
2007-09-01 12:00:30 +00:00
*/
/* Used if undo / redo command:
* swap data between Item and its copy, pointed by its picked item link member
* swapped data is data modified by editing, so not all values are swapped
2007-09-01 12:00:30 +00:00
*/
void SCH_EDIT_FRAME::StartNewUndo()
{
PICKED_ITEMS_LIST* blank = new PICKED_ITEMS_LIST();
PushCommandToUndoList( blank );
}
void SCH_EDIT_FRAME::SaveCopyInUndoList( SCH_SCREEN* aScreen,
SCH_ITEM* aItem,
UNDO_REDO aCommandType,
bool aAppend )
{
PICKED_ITEMS_LIST* commandToUndo = nullptr;
wxCHECK( aItem, /* void */ );
// Connectivity may change
aItem->SetConnectivityDirty();
PICKED_ITEMS_LIST* lastUndo = PopCommandFromUndoList();
// If the last stack was empty, use that one instead of creating a new stack
if( lastUndo )
{
if( aAppend || !lastUndo->GetCount() )
commandToUndo = lastUndo;
else
PushCommandToUndoList( lastUndo );
}
if( !commandToUndo )
{
commandToUndo = new PICKED_ITEMS_LIST();
}
2007-09-01 12:00:30 +00:00
ITEM_PICKER itemWrapper( aScreen, aItem, aCommandType );
itemWrapper.SetFlags( aItem->GetFlags() );
2007-09-01 12:00:30 +00:00
switch( aCommandType )
2007-09-01 12:00:30 +00:00
{
2020-08-26 18:04:32 +00:00
case UNDO_REDO::CHANGED: /* Create a copy of item */
itemWrapper.SetLink( aItem->Duplicate( true ) );
2012-02-05 13:02:46 +00:00
commandToUndo->PushItem( itemWrapper );
break;
2007-09-01 12:00:30 +00:00
2020-08-26 18:04:32 +00:00
case UNDO_REDO::NEWITEM:
case UNDO_REDO::DELETED:
commandToUndo->PushItem( itemWrapper );
break;
default:
wxFAIL_MSG( wxString::Format( wxT( "SaveCopyInUndoList() error (unknown code %X)" ),
aCommandType ) );
break;
}
if( commandToUndo->GetCount() )
{
/* Save the copy in undo list */
PushCommandToUndoList( commandToUndo );
/* Clear redo list, because after new save there is no redo to do */
2020-07-13 13:50:35 +00:00
ClearUndoORRedoList( REDO_LIST );
}
else
{
delete commandToUndo;
}
}
void SCH_EDIT_FRAME::SaveCopyInUndoList( const PICKED_ITEMS_LIST& aItemsList,
UNDO_REDO aTypeCommand,
bool aAppend )
{
PICKED_ITEMS_LIST* commandToUndo = nullptr;
if( !aItemsList.GetCount() )
return;
PICKED_ITEMS_LIST* lastUndo = PopCommandFromUndoList();
// If the last stack was empty, use that one instead of creating a new stack
if( lastUndo )
{
if( aAppend || !lastUndo->GetCount() )
commandToUndo = lastUndo;
else
PushCommandToUndoList( lastUndo );
}
if( !commandToUndo )
commandToUndo = new PICKED_ITEMS_LIST();
2009-08-06 15:42:09 +00:00
// Copy picker list:
if( !commandToUndo->GetCount() )
commandToUndo->CopyList( aItemsList );
else
{
// Unless we are appending, in which case, get the picker items
for( unsigned ii = 0; ii < aItemsList.GetCount(); ii++ )
commandToUndo->PushItem( aItemsList.GetItemWrapper( ii) );
}
2009-08-06 15:42:09 +00:00
// Verify list, and creates data if needed
for( unsigned ii = 0; ii < commandToUndo->GetCount(); ii++ )
{
SCH_ITEM* sch_item = dynamic_cast<SCH_ITEM*>( commandToUndo->GetPickedItem( ii ) );
// Common items implemented in EDA_DRAW_FRAME will not be SCH_ITEMs.
if( !sch_item )
continue;
2009-08-06 15:42:09 +00:00
// Connectivity may change
sch_item->SetConnectivityDirty();
2020-08-26 18:04:32 +00:00
UNDO_REDO command = commandToUndo->GetPickedItemStatus( ii );
2020-08-26 18:04:32 +00:00
if( command == UNDO_REDO::UNSPECIFIED )
{
command = aTypeCommand;
commandToUndo->SetPickedItemStatus( command, ii );
2009-07-23 15:37:00 +00:00
}
2009-08-06 15:42:09 +00:00
switch( command )
{
2020-08-26 18:04:32 +00:00
case UNDO_REDO::CHANGED:
2009-08-06 15:42:09 +00:00
/* If needed, create a copy of item, and put in undo list
* in the picker, as link
* If this link is not null, the copy is already done
*/
if( commandToUndo->GetPickedItemLink( ii ) == nullptr )
commandToUndo->SetPickedItemLink( sch_item->Duplicate( true ), ii );
wxASSERT( commandToUndo->GetPickedItemLink( ii ) );
break;
2020-08-26 18:04:32 +00:00
case UNDO_REDO::NEWITEM:
case UNDO_REDO::DELETED:
case UNDO_REDO::EXCHANGE_T:
case UNDO_REDO::PAGESETTINGS:
2007-09-01 12:00:30 +00:00
break;
default:
wxFAIL_MSG( wxString::Format( wxT( "Unknown undo/redo command %d" ), command ) );
break;
2007-09-01 12:00:30 +00:00
}
}
if( commandToUndo->GetCount() )
{
/* Save the copy in undo list */
PushCommandToUndoList( commandToUndo );
2008-02-26 19:19:54 +00:00
/* Clear redo list, because after new save there is no redo to do */
2020-07-13 13:50:35 +00:00
ClearUndoORRedoList( REDO_LIST );
}
2009-08-06 15:42:09 +00:00
else // Should not occur
{
delete commandToUndo;
}
2007-05-06 16:03:28 +00:00
}
void SCH_EDIT_FRAME::PutDataInPreviousState( PICKED_ITEMS_LIST* aList )
{
// Undo in the reverse order of list creation: (this can allow stacked changes like the
// same item can be changed and deleted in the same complex command).
// After hitting 0, subtracting 1 will roll the value over to its max representation
for( unsigned ii = aList->GetCount() - 1; ii < std::numeric_limits<unsigned>::max(); ii-- )
2007-09-01 12:00:30 +00:00
{
UNDO_REDO status = aList->GetPickedItemStatus( ii );
EDA_ITEM* eda_item = aList->GetPickedItem( ii );
SCH_SCREEN* screen = dynamic_cast<SCH_SCREEN*>( aList->GetScreenForItem( ii ) );
wxCHECK( screen, /* void */ );
eda_item->SetFlags( aList->GetPickerFlags( ii ) );
eda_item->ClearEditFlags();
eda_item->ClearTempFlags();
if( status == UNDO_REDO::NOP )
{
continue;
}
2020-08-26 18:04:32 +00:00
if( status == UNDO_REDO::NEWITEM )
2007-09-01 12:00:30 +00:00
{
// If we are removing the current sheet, get out first
if( SCH_SHEET* sheet = dyn_cast<SCH_SHEET*>( eda_item ) )
{
if( sheet->GetScreen() == GetScreen() )
GetToolManager()->RunAction( EE_ACTIONS::leaveSheet );
}
RemoveFromScreen( eda_item, screen );
aList->SetPickedItemStatus( UNDO_REDO::DELETED, ii );
}
2020-08-26 18:04:32 +00:00
else if( status == UNDO_REDO::DELETED )
{
// deleted items are re-inserted on undo
AddToScreen( eda_item, screen );
aList->SetPickedItemStatus( UNDO_REDO::NEWITEM, ii );
}
2020-08-26 18:04:32 +00:00
else if( status == UNDO_REDO::PAGESETTINGS )
{
SetCurrentSheet( *m_schematic->GetSheets().FindSheetForScreen( screen ) );
DisplayCurrentSheet();
// swap current settings with stored settings
2021-02-22 23:47:17 +00:00
DS_PROXY_UNDO_ITEM alt_item( this );
DS_PROXY_UNDO_ITEM* item = static_cast<DS_PROXY_UNDO_ITEM*>( eda_item );
item->Restore( this );
*item = alt_item;
}
else if( SCH_ITEM* item = dynamic_cast<SCH_ITEM*>( eda_item ) )
{
// everything else is modified in place
SCH_ITEM* alt_item = static_cast<SCH_ITEM*>( aList->GetPickedItemLink( ii ) );
// The root sheet is a pseudo object that owns the root screen object but is not on
// the root screen so do not attempt to remove it from the screen it owns.
if( item != &Schematic().Root() )
RemoveFromScreen( item, screen );
switch( status )
{
2020-08-26 18:04:32 +00:00
case UNDO_REDO::CHANGED:
item->SwapData( alt_item );
break;
2020-08-26 18:04:32 +00:00
case UNDO_REDO::EXCHANGE_T:
aList->SetPickedItem( alt_item, ii );
aList->SetPickedItemLink( item, ii );
item = alt_item;
break;
default:
wxFAIL_MSG( wxString::Format( wxT( "Unknown undo/redo command %d" ),
aList->GetPickedItemStatus( ii ) ) );
break;
}
if( item->Type() == SCH_SYMBOL_T )
{
SCH_SYMBOL* sym = static_cast<SCH_SYMBOL*>( item );
sym->UpdatePins();
}
if( item != &Schematic().Root() )
AddToScreen( item, screen );
2007-09-01 12:00:30 +00:00
}
}
GetCanvas()->GetView()->ClearHiddenFlags();
}
2007-09-01 12:00:30 +00:00
void SCH_EDIT_FRAME::RollbackSchematicFromUndo()
{
PICKED_ITEMS_LIST* undo = PopCommandFromUndoList();
// Skip empty frames
while( undo && ( !undo->GetCount()
|| ( undo->GetCount() == 1 && undo->GetPickedItemStatus( 0 ) == UNDO_REDO::NOP ) ) )
{
delete undo;
undo = PopCommandFromUndoList();
}
if( undo )
{
PutDataInPreviousState( undo );
undo->ClearListAndDeleteItems();
delete undo;
SetSheetNumberAndCount();
UpdateHierarchyNavigator();
TestDanglingEnds();
m_toolManager->GetTool<EE_SELECTION_TOOL>()->RebuildSelection();
}
GetCanvas()->Refresh();
}
2020-07-13 13:50:35 +00:00
void SCH_EDIT_FRAME::ClearUndoORRedoList( UNDO_REDO_LIST whichList, int aItemCount )
{
if( aItemCount == 0 )
return;
2020-07-13 13:50:35 +00:00
UNDO_REDO_CONTAINER& list = whichList == UNDO_LIST ? m_undoList : m_redoList;
for( PICKED_ITEMS_LIST* command : list.m_CommandsList )
{
command->ClearListAndDeleteItems();
delete command;
}
2020-07-13 13:50:35 +00:00
list.m_CommandsList.clear();
}