KiCad PCB EDA Suite
schematic_undo_redo.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2004 Jean-Pierre Charras, [email protected]
5  * Copyright (C) 2004-2021 KiCad Developers, see change_log.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #include <ee_actions.h>
26 #include <sch_edit_frame.h>
27 #include <tool/tool_manager.h>
28 #include <schematic.h>
29 #include <sch_bus_entry.h>
30 #include <sch_junction.h>
31 #include <sch_line.h>
32 #include <sch_bitmap.h>
35 #include <tool/actions.h>
36 
37 
38 /* Functions to undo and redo edit commands.
39  *
40  * m_UndoList and m_RedoList handle a std::vector of PICKED_ITEMS_LIST
41  * Each PICKED_ITEMS_LIST handle a std::vector of pickers (class ITEM_PICKER),
42  * that store the list of schematic items that are concerned by the command to
43  * undo or redo and is created for each command to undo (handle also a command
44  * to redo). each picker has a pointer pointing to an item to undo or redo (in
45  * fact: deleted, added or modified), and has a pointer to a copy of this item,
46  * when this item has been modified (the old values of parameters are
47  * therefore saved)
48  *
49  * there are 3 cases:
50  * - delete item(s) command
51  * - change item(s) command
52  * - add item(s) command
53  * and 2 cases for block:
54  * - move list of items
55  * - mirror (Y) list of items
56  *
57  * Undo command
58  * - delete item(s) command:
59  * => deleted items are moved in undo list
60  *
61  * - change item(s) command
62  * => A copy of item(s) is made (a DrawPickedStruct list of wrappers)
63  * the .m_Link member of each wrapper points the modified item.
64  * the .m_Item member of each wrapper points the old copy of this item.
65  *
66  * - add item(s) command
67  * =>A list of item(s) is made. The .m_Item member of each wrapper points
68  * the new item.
69  *
70  * Redo command
71  * - delete item(s) old command:
72  * => deleted items are moved into m_tree
73  *
74  * - change item(s) command
75  * => the copy of item(s) is moved in Undo list
76  *
77  * - add item(s) command
78  * => The list of item(s) is used to create a deleted list in undo
79  * list(same as a delete command)
80  *
81  * Some block operations that change items can be undone without memorized
82  * items, just the coordinates of the transform: move list of items (undo/
83  * redo is made by moving with the opposite move vector) mirror (Y) and flip
84  * list of items (undo/redo is made by mirror or flip items) so they are
85  * handled specifically.
86  *
87  * A problem is the hierarchical sheet handling.
88  * the data associated (sub-hierarchy, undo/redo list) is deleted only
89  * when the sheet is really deleted (i.e. when deleted from undo or redo list)
90  * This is handled by its destructor.
91  */
92 
93 
94 /* Used if undo / redo command:
95  * swap data between Item and its copy, pointed by its picked item link member
96  * swapped data is data modified by editing, so not all values are swapped
97  */
98 
100 {
101  PICKED_ITEMS_LIST* blank = new PICKED_ITEMS_LIST();
102  PushCommandToUndoList( blank );
103 }
104 
105 
107  SCH_ITEM* aItem,
108  UNDO_REDO aCommandType,
109  bool aAppend )
110 {
111  PICKED_ITEMS_LIST* commandToUndo = nullptr;
112 
113  wxCHECK( aItem, /* void */ );
114 
115  // Connectivity may change
116  aItem->SetConnectivityDirty();
117 
119 
120  // If the last stack was empty, use that one instead of creating a new stack
121  if( lastUndo )
122  {
123  if( aAppend || !lastUndo->GetCount() )
124  commandToUndo = lastUndo;
125  else
126  PushCommandToUndoList( lastUndo );
127  }
128 
129  if( !commandToUndo )
130  {
131  commandToUndo = new PICKED_ITEMS_LIST();
132  }
133 
134  ITEM_PICKER itemWrapper( aScreen, aItem, aCommandType );
135  itemWrapper.SetFlags( aItem->GetFlags() );
136 
137  switch( aCommandType )
138  {
139  case UNDO_REDO::CHANGED: /* Create a copy of item */
140  itemWrapper.SetLink( aItem->Duplicate( true ) );
141  commandToUndo->PushItem( itemWrapper );
142  break;
143 
144  case UNDO_REDO::NEWITEM:
145  case UNDO_REDO::DELETED:
146  commandToUndo->PushItem( itemWrapper );
147  break;
148 
149  default:
150  wxFAIL_MSG( wxString::Format( wxT( "SaveCopyInUndoList() error (unknown code %X)" ),
151  aCommandType ) );
152  break;
153  }
154 
155  if( commandToUndo->GetCount() )
156  {
157  /* Save the copy in undo list */
158  PushCommandToUndoList( commandToUndo );
159 
160  /* Clear redo list, because after new save there is no redo to do */
162  }
163  else
164  {
165  delete commandToUndo;
166  }
167 }
168 
169 
171  UNDO_REDO aTypeCommand,
172  bool aAppend )
173 {
174  PICKED_ITEMS_LIST* commandToUndo = nullptr;
175 
176  if( !aItemsList.GetCount() )
177  return;
178 
180 
181  // If the last stack was empty, use that one instead of creating a new stack
182  if( lastUndo )
183  {
184  if( aAppend || !lastUndo->GetCount() )
185  commandToUndo = lastUndo;
186  else
187  PushCommandToUndoList( lastUndo );
188  }
189 
190  if( !commandToUndo )
191  commandToUndo = new PICKED_ITEMS_LIST();
192 
193  // Copy picker list:
194  if( !commandToUndo->GetCount() )
195  commandToUndo->CopyList( aItemsList );
196  else
197  {
198  // Unless we are appending, in which case, get the picker items
199  for( unsigned ii = 0; ii < aItemsList.GetCount(); ii++ )
200  commandToUndo->PushItem( aItemsList.GetItemWrapper( ii) );
201  }
202 
203  // Verify list, and creates data if needed
204  for( unsigned ii = 0; ii < commandToUndo->GetCount(); ii++ )
205  {
206  SCH_ITEM* sch_item = dynamic_cast<SCH_ITEM*>( commandToUndo->GetPickedItem( ii ) );
207 
208  // Common items implemented in EDA_DRAW_FRAME will not be SCH_ITEMs.
209  if( !sch_item )
210  continue;
211 
212  // Connectivity may change
213  sch_item->SetConnectivityDirty();
214 
215  UNDO_REDO command = commandToUndo->GetPickedItemStatus( ii );
216 
217  if( command == UNDO_REDO::UNSPECIFIED )
218  {
219  command = aTypeCommand;
220  commandToUndo->SetPickedItemStatus( command, ii );
221  }
222 
223  switch( command )
224  {
225  case UNDO_REDO::CHANGED:
226 
227  /* If needed, create a copy of item, and put in undo list
228  * in the picker, as link
229  * If this link is not null, the copy is already done
230  */
231  if( commandToUndo->GetPickedItemLink( ii ) == nullptr )
232  commandToUndo->SetPickedItemLink( sch_item->Duplicate( true ), ii );
233 
234  wxASSERT( commandToUndo->GetPickedItemLink( ii ) );
235  break;
236 
237  case UNDO_REDO::NEWITEM:
238  case UNDO_REDO::DELETED:
241  break;
242 
243  default:
244  wxFAIL_MSG( wxString::Format( wxT( "Unknown undo/redo command %d" ), command ) );
245  break;
246  }
247  }
248 
249  if( commandToUndo->GetCount() )
250  {
251  /* Save the copy in undo list */
252  PushCommandToUndoList( commandToUndo );
253 
254  /* Clear redo list, because after new save there is no redo to do */
256  }
257  else // Should not occur
258  {
259  delete commandToUndo;
260  }
261 }
262 
263 
265 {
266  // Undo in the reverse order of list creation: (this can allow stacked changes like the
267  // same item can be changed and deleted in the same complex command).
268  // After hitting 0, subtracting 1 will roll the value over to its max representation
269  for( unsigned ii = aList->GetCount() - 1; ii < std::numeric_limits<unsigned>::max(); ii-- )
270  {
271  UNDO_REDO status = aList->GetPickedItemStatus( ii );
272  EDA_ITEM* eda_item = aList->GetPickedItem( ii );
273  SCH_SCREEN* screen = dynamic_cast<SCH_SCREEN*>( aList->GetScreenForItem( ii ) );
274 
275  wxCHECK( screen, /* void */ );
276 
277  eda_item->SetFlags( aList->GetPickerFlags( ii ) );
278  eda_item->ClearEditFlags();
279  eda_item->ClearTempFlags();
280 
281  if( status == UNDO_REDO::NOP )
282  {
283  continue;
284  }
285  if( status == UNDO_REDO::NEWITEM )
286  {
287  // If we are removing the current sheet, get out first
288  if( SCH_SHEET* sheet = dyn_cast<SCH_SHEET*>( eda_item ) )
289  {
290  if( sheet->GetScreen() == GetScreen() )
292  }
293 
294  RemoveFromScreen( eda_item, screen );
296  }
297  else if( status == UNDO_REDO::DELETED )
298  {
299  // deleted items are re-inserted on undo
300  AddToScreen( eda_item, screen );
302  }
303  else if( status == UNDO_REDO::PAGESETTINGS )
304  {
307 
308  // swap current settings with stored settings
309  DS_PROXY_UNDO_ITEM alt_item( this );
310  DS_PROXY_UNDO_ITEM* item = static_cast<DS_PROXY_UNDO_ITEM*>( eda_item );
311  item->Restore( this );
312  *item = alt_item;
313  }
314  else if( SCH_ITEM* item = dynamic_cast<SCH_ITEM*>( eda_item ) )
315  {
316  // everything else is modified in place
317  SCH_ITEM* alt_item = static_cast<SCH_ITEM*>( aList->GetPickedItemLink( ii ) );
318 
319  // The root sheet is a pseudo object that owns the root screen object but is not on
320  // the root screen so do not attempt to remove it from the screen it owns.
321  if( item != &Schematic().Root() )
322  RemoveFromScreen( item, screen );
323 
324  switch( status )
325  {
326  case UNDO_REDO::CHANGED:
327  item->SwapData( alt_item );
328  break;
329 
331  aList->SetPickedItem( alt_item, ii );
332  aList->SetPickedItemLink( item, ii );
333  item = alt_item;
334  break;
335 
336  default:
337  wxFAIL_MSG( wxString::Format( wxT( "Unknown undo/redo command %d" ),
338  aList->GetPickedItemStatus( ii ) ) );
339  break;
340  }
341 
342  if( item->Type() == SCH_SYMBOL_T )
343  {
344  SCH_SYMBOL* sym = static_cast<SCH_SYMBOL*>( item );
345  sym->UpdatePins();
346  }
347 
348  if( item != &Schematic().Root() )
349  AddToScreen( item, screen );
350  }
351  }
352 
354 }
355 
356 
358 {
360 
361  // Skip empty frames
362  while( undo && ( !undo->GetCount()
363  || ( undo->GetCount() == 1 && undo->GetPickedItemStatus( 0 ) == UNDO_REDO::NOP ) ) )
364  {
365  delete undo;
367  }
368 
369  if( undo )
370  {
372  undo->ClearListAndDeleteItems();
373  delete undo;
374 
377 
379 
380  m_toolManager->GetTool<EE_SELECTION_TOOL>()->RebuildSelection();
381  }
382 
383  GetCanvas()->Refresh();
384 }
385 
386 
387 void SCH_EDIT_FRAME::ClearUndoORRedoList( UNDO_REDO_LIST whichList, int aItemCount )
388 {
389  if( aItemCount == 0 )
390  return;
391 
392  UNDO_REDO_CONTAINER& list = whichList == UNDO_LIST ? m_undoList : m_redoList;
393 
394  for( PICKED_ITEMS_LIST* command : list.m_CommandsList )
395  {
396  command->ClearListAndDeleteItems();
397  delete command;
398  }
399 
400  list.m_CommandsList.clear();
401 }
402 
403 
void SetCurrentSheet(const SCH_SHEET_PATH &aSheet)
KIGFX::SCH_VIEW * GetView() const override
Return a pointer to the #VIEW instance used in the panel.
EDA_ITEM * GetPickedItemLink(unsigned int aIdx) const
SCHEMATIC * m_schematic
The currently loaded schematic.
void ClearHiddenFlags()
Clear the hide flag of all items in the view.
Definition: sch_view.cpp:171
void ClearTempFlags()
Definition: eda_item.h:165
void SetFlags(EDA_ITEM_FLAGS aFlags)
SCH_SHEET_PATH * FindSheetForScreen(const SCH_SCREEN *aScreen)
Return a pointer to the first SCH_SHEET_PATH object (not necessarily the only one) using a particular...
void StartNewUndo()
Create a new, blank stack for future Undo commands to be pushed to.
UNDO_REDO_CONTAINER m_undoList
void Restore(EDA_DRAW_FRAME *aFrame, KIGFX::VIEW *aView=nullptr)
void SetFlags(EDA_ITEM_FLAGS aMask)
Definition: eda_item.h:152
BASE_SCREEN * GetScreenForItem(unsigned int aIdx) const
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Run the specified action.
Definition: tool_manager.h:143
void RemoveFromScreen(EDA_ITEM *aItem, SCH_SCREEN *aScreen)
Remove an item from the screen (and view) aScreen is the screen the item is located on,...
virtual void PushCommandToUndoList(PICKED_ITEMS_LIST *aItem)
Add a command to undo in the undo list.
void PushItem(const ITEM_PICKER &aItem)
Push aItem to the top of the list.
void DisplayCurrentSheet()
Draw the current sheet on the display.
Definition: hierarch.cpp:313
void SetLink(EDA_ITEM *aItem)
UNDO_REDO
Undo Redo considerations: Basically we have 3 cases New item Deleted item Modified item there is also...
unsigned GetCount() const
static TOOL_ACTION leaveSheet
Definition: ee_actions.h:186
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
A holder to handle a list of undo (or redo) commands.
void CopyList(const PICKED_ITEMS_LIST &aSource)
Copy all data from aSource to the list.
SCH_ITEM * Duplicate(bool doClone=false) const
Routine to create a new copy of given item.
Definition: sch_item.cpp:85
void TestDanglingEnds()
Test all of the connectable objects in the schematic for unused connection points.
std::vector< PICKED_ITEMS_LIST * > m_CommandsList
bool SetPickedItem(EDA_ITEM *aItem, unsigned aIdx)
SCH_DRAW_PANEL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
void UpdatePins()
Updates the cache of SCH_PIN objects for each pin.
Definition: sch_symbol.cpp:307
EDA_ITEM_FLAGS GetPickerFlags(unsigned aIdx) const
Return the value of the picker flag.
SCHEMATIC & Schematic() const
void UpdateHierarchyNavigator(bool aForceUpdate=false)
Run the Hierarchy Navigator dialog.
EDA_ITEM * GetPickedItem(unsigned int aIdx) const
virtual PICKED_ITEMS_LIST * PopCommandFromUndoList()
Return the last command to undo and remove it from list, nothing is deleted.
A holder to handle information on schematic or board items.
UNDO_REDO_CONTAINER m_redoList
EDA_ITEM_FLAGS GetFlags() const
Definition: eda_item.h:154
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:54
UNDO_REDO_LIST
Specifies whether we are interacting with the undo or redo stacks.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
ITEM_PICKER GetItemWrapper(unsigned int aIdx) const
SCH_SHEET_LIST GetSheets() const override
Builds and returns an updated schematic hierarchy TODO: can this be cached?
Definition: schematic.h:87
SCH_SHEET & Root() const
Definition: schematic.h:92
bool SetPickedItemStatus(UNDO_REDO aStatus, unsigned aIdx)
Set the type of undo/redo operation for a given picked item.
TOOL_MANAGER * m_toolManager
Definition: tools_holder.h:158
Schematic symbol object.
Definition: sch_symbol.h:78
void AddToScreen(EDA_ITEM *aItem, SCH_SCREEN *aScreen)
Add an item to the screen (and view) aScreen is the screen the item is located on,...
void SetSheetNumberAndCount()
Set the m_ScreenNumber and m_NumberOfScreens members for screens.
void SaveCopyInUndoList(SCH_SCREEN *aScreen, SCH_ITEM *aItemToCopy, UNDO_REDO aTypeCommand, bool aAppend)
Create a copy of the current schematic item, and put it in the undo list.
void RollbackSchematicFromUndo()
Perform an undo of the last edit WITHOUT logging a corresponding redo.
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=nullptr) override
Update the board display after modifying it by a python script (note: it is automatically called by a...
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:99
bool SetPickedItemLink(EDA_ITEM *aLink, unsigned aIdx)
Set the link associated to a given picked item.
UNDO_REDO GetPickedItemStatus(unsigned int aIdx) const
void PutDataInPreviousState(PICKED_ITEMS_LIST *aList)
Restore an undo or redo command to put data pointed by aList in the previous state.
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:54
void ClearUndoORRedoList(UNDO_REDO_LIST whichList, int aItemCount=-1) override
Free the undo or redo list from aList element.
void ClearEditFlags()
Definition: eda_item.h:171
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:182
void ClearListAndDeleteItems()
Delete the list of pickers AND the data pointed by #m_PickedItem or #m_PickedItemLink according to th...
void SetConnectivityDirty(bool aDirty=true)
Definition: sch_item.h:415