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, jaen-pierre.charras@gipsa-lab.inpg.com
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 <sch_edit_frame.h>
26 #include <tool/tool_manager.h>
27 #include <schematic.h>
28 #include <sch_bus_entry.h>
29 #include <sch_junction.h>
30 #include <sch_line.h>
31 #include <sch_bitmap.h>
34 #include <tool/actions.h>
35 
36 
37 /* Functions to undo and redo edit commands.
38  *
39  * m_UndoList and m_RedoList handle a std::vector of PICKED_ITEMS_LIST
40  * Each PICKED_ITEMS_LIST handle a std::vector of pickers (class ITEM_PICKER),
41  * that store the list of schematic items that are concerned by the command to
42  * undo or redo and is created for each command to undo (handle also a command
43  * to redo). each picker has a pointer pointing to an item to undo or redo (in
44  * fact: deleted, added or modified), and has a pointer to a copy of this item,
45  * when this item has been modified (the old values of parameters are
46  * therefore saved)
47  *
48  * there are 3 cases:
49  * - delete item(s) command
50  * - change item(s) command
51  * - add item(s) command
52  * and 2 cases for block:
53  * - move list of items
54  * - mirror (Y) list of items
55  *
56  * Undo command
57  * - delete item(s) command:
58  * => deleted items are moved in undo list
59  *
60  * - change item(s) command
61  * => A copy of item(s) is made (a DrawPickedStruct list of wrappers)
62  * the .m_Link member of each wrapper points the modified item.
63  * the .m_Item member of each wrapper points the old copy of this item.
64  *
65  * - add item(s) command
66  * =>A list of item(s) is made. The .m_Item member of each wrapper points
67  * the new item.
68  *
69  * Redo command
70  * - delete item(s) old command:
71  * => deleted items are moved into m_tree
72  *
73  * - change item(s) command
74  * => the copy of item(s) is moved in Undo list
75  *
76  * - add item(s) command
77  * => The list of item(s) is used to create a deleted list in undo
78  * list(same as a delete command)
79  *
80  * Some block operations that change items can be undone without memorized
81  * items, just the coordinates of the transform: move list of items (undo/
82  * redo is made by moving with the opposite move vector) mirror (Y) and flip
83  * list of items (undo/redo is made by mirror or flip items) so they are
84  * handled specifically.
85  *
86  * A problem is the hierarchical sheet handling.
87  * the data associated (sub-hierarchy, undo/redo list) is deleted only
88  * when the sheet is really deleted (i.e. when deleted from undo or redo list)
89  * This is handled by its destructor.
90  */
91 
92 
93 /* Used if undo / redo command:
94  * swap data between Item and its copy, pointed by its picked item link member
95  * swapped data is data modified by editing, so not all values are swapped
96  */
97 
99 {
100  PICKED_ITEMS_LIST* blank = new PICKED_ITEMS_LIST();
101  PushCommandToUndoList( blank );
102 }
103 
104 
106  SCH_ITEM* aItem,
107  UNDO_REDO aCommandType,
108  bool aAppend )
109 {
110  PICKED_ITEMS_LIST* commandToUndo = nullptr;
111 
112  wxCHECK( aItem, /* void */ );
113 
114  // Connectivity may change
115  aItem->SetConnectivityDirty();
116 
118 
119  // If the last stack was empty, use that one instead of creating a new stack
120  if( lastUndo )
121  {
122  if( aAppend || !lastUndo->GetCount() )
123  commandToUndo = lastUndo;
124  else
125  PushCommandToUndoList( lastUndo );
126  }
127 
128  if( !commandToUndo )
129  {
130  commandToUndo = new PICKED_ITEMS_LIST();
131  }
132 
133  ITEM_PICKER itemWrapper( aScreen, aItem, aCommandType );
134  itemWrapper.SetFlags( aItem->GetFlags() );
135 
136  switch( aCommandType )
137  {
138  case UNDO_REDO::CHANGED: /* Create a copy of item */
139  itemWrapper.SetLink( aItem->Duplicate( true ) );
140  commandToUndo->PushItem( itemWrapper );
141  break;
142 
143  case UNDO_REDO::NEWITEM:
144  case UNDO_REDO::DELETED:
145  commandToUndo->PushItem( itemWrapper );
146  break;
147 
148  default:
149  wxFAIL_MSG( wxString::Format( wxT( "SaveCopyInUndoList() error (unknown code %X)" ),
150  aCommandType ) );
151  break;
152  }
153 
154  if( commandToUndo->GetCount() )
155  {
156  /* Save the copy in undo list */
157  PushCommandToUndoList( commandToUndo );
158 
159  /* Clear redo list, because after new save there is no redo to do */
161  }
162  else
163  {
164  delete commandToUndo;
165  }
166 }
167 
168 
170  UNDO_REDO aTypeCommand,
171  bool aAppend )
172 {
173  PICKED_ITEMS_LIST* commandToUndo = nullptr;
174 
175  if( !aItemsList.GetCount() )
176  return;
177 
179 
180  // If the last stack was empty, use that one instead of creating a new stack
181  if( lastUndo )
182  {
183  if( aAppend || !lastUndo->GetCount() )
184  commandToUndo = lastUndo;
185  else
186  PushCommandToUndoList( lastUndo );
187  }
188 
189  if( !commandToUndo )
190  commandToUndo = new PICKED_ITEMS_LIST();
191 
192  // Copy picker list:
193  if( !commandToUndo->GetCount() )
194  commandToUndo->CopyList( aItemsList );
195  else
196  {
197  // Unless we are appending, in which case, get the picker items
198  for( unsigned ii = 0; ii < aItemsList.GetCount(); ii++ )
199  commandToUndo->PushItem( aItemsList.GetItemWrapper( ii) );
200  }
201 
202  // Verify list, and creates data if needed
203  for( unsigned ii = 0; ii < commandToUndo->GetCount(); ii++ )
204  {
205  SCH_ITEM* sch_item = dynamic_cast<SCH_ITEM*>( commandToUndo->GetPickedItem( ii ) );
206 
207  // Common items implemented in EDA_DRAW_FRAME will not be SCH_ITEMs.
208  if( !sch_item )
209  continue;
210 
211  // Connectivity may change
212  sch_item->SetConnectivityDirty();
213 
214  UNDO_REDO command = commandToUndo->GetPickedItemStatus( ii );
215 
216  if( command == UNDO_REDO::UNSPECIFIED )
217  {
218  command = aTypeCommand;
219  commandToUndo->SetPickedItemStatus( command, ii );
220  }
221 
222  switch( command )
223  {
224  case UNDO_REDO::CHANGED:
225 
226  /* If needed, create a copy of item, and put in undo list
227  * in the picker, as link
228  * If this link is not null, the copy is already done
229  */
230  if( commandToUndo->GetPickedItemLink( ii ) == nullptr )
231  commandToUndo->SetPickedItemLink( sch_item->Duplicate( true ), ii );
232 
233  wxASSERT( commandToUndo->GetPickedItemLink( ii ) );
234  break;
235 
236  case UNDO_REDO::NEWITEM:
237  case UNDO_REDO::DELETED:
240  break;
241 
242  default:
243  wxFAIL_MSG( wxString::Format( wxT( "Unknown undo/redo command %d" ), command ) );
244  break;
245  }
246  }
247 
248  if( commandToUndo->GetCount() )
249  {
250  /* Save the copy in undo list */
251  PushCommandToUndoList( commandToUndo );
252 
253  /* Clear redo list, because after new save there is no redo to do */
255  }
256  else // Should not occur
257  {
258  delete commandToUndo;
259  }
260 }
261 
262 
264 {
265  // Undo in the reverse order of list creation: (this can allow stacked changes like the
266  // same item can be changed and deleted in the same complex command).
267  for( int ii = aList->GetCount() - 1; ii >= 0; ii-- )
268  {
269  UNDO_REDO status = aList->GetPickedItemStatus((unsigned) ii );
270  EDA_ITEM* eda_item = aList->GetPickedItem( (unsigned) ii );
271  SCH_SCREEN* screen =
272  dynamic_cast< SCH_SCREEN* >( aList->GetScreenForItem( (unsigned) ii ) );
273 
274  wxCHECK( screen, /* void */ );
275 
276  eda_item->SetFlags( aList->GetPickerFlags( (unsigned) ii ) );
277  eda_item->ClearEditFlags();
278  eda_item->ClearTempFlags();
279 
280  if( status == UNDO_REDO::NOP )
281  {
282  continue;
283  }
284  if( status == UNDO_REDO::NEWITEM )
285  {
286  // new items are deleted on undo
287  RemoveFromScreen( eda_item, screen );
288  aList->SetPickedItemStatus( UNDO_REDO::DELETED, (unsigned) ii );
289  }
290  else if( status == UNDO_REDO::DELETED )
291  {
292  // deleted items are re-inserted on undo
293  AddToScreen( eda_item, screen );
294  aList->SetPickedItemStatus( UNDO_REDO::NEWITEM, (unsigned) ii );
295  }
296  else if( status == UNDO_REDO::PAGESETTINGS )
297  {
298  // swap current settings with stored settings
299  DS_PROXY_UNDO_ITEM alt_item( this );
300  DS_PROXY_UNDO_ITEM* item = static_cast<DS_PROXY_UNDO_ITEM*>( eda_item );
301  item->Restore( this );
302  *item = alt_item;
304  }
305  else if( dynamic_cast<SCH_ITEM*>( eda_item ) )
306  {
307  // everything else is modified in place
308  SCH_ITEM* item = (SCH_ITEM*) eda_item;
309  SCH_ITEM* alt_item = (SCH_ITEM*) aList->GetPickedItemLink( (unsigned) ii );
310 
311  // The root sheet is a pseudo object that owns the root screen object but is not on
312  // the root screen so do not attempt to remove it from the screen it owns.
313  if( item != &Schematic().Root() )
314  RemoveFromScreen( item, screen );
315 
316  switch( status )
317  {
318  case UNDO_REDO::CHANGED:
319  if( item->Type() == SCH_SYMBOL_T )
320  {
321  // Update the schematic library cache in case that was the change.
322  SCH_SYMBOL* symbol = dynamic_cast<SCH_SYMBOL*>( item );
323  SCH_SYMBOL* altSymbol = dynamic_cast<SCH_SYMBOL*>( alt_item );
324 
325  wxCHECK( symbol && altSymbol, /* void */ );
326 
327  screen->SwapSymbolLinks( symbol, altSymbol );
328  }
329 
330  item->SwapData( alt_item );
331 
332  if( item->Type() == SCH_SYMBOL_T )
333  static_cast<SCH_SYMBOL*>( item )->UpdatePins();
334 
335  break;
336 
338  aList->SetPickedItem( alt_item, (unsigned) ii );
339  aList->SetPickedItemLink( item, (unsigned) ii );
340  item = alt_item;
341  break;
342 
343  default:
344  wxFAIL_MSG( wxString::Format( wxT( "Unknown undo/redo command %d" ),
345  aList->GetPickedItemStatus( (unsigned) ii ) ) );
346  break;
347  }
348 
349  if( item != &Schematic().Root() )
350  AddToScreen( item, screen );
351  }
352  }
353 
354  // Bitmaps are cached in Opengl: clear the cache, because
355  // the cache data can be invalid
358 }
359 
360 
362 {
364 
365  // Skip empty frames
366  while( undo && ( !undo->GetCount()
367  || ( undo->GetCount() == 1 && undo->GetPickedItemStatus( 0 ) == UNDO_REDO::NOP ) ) )
368  {
369  delete undo;
371  }
372 
373  if( undo )
374  {
376  undo->ClearListAndDeleteItems();
377  delete undo;
378 
381 
383 
384  m_toolManager->GetTool<EE_SELECTION_TOOL>()->RebuildSelection();
385  }
386 
387  SyncView();
388  GetCanvas()->Refresh();
389 }
390 
391 
392 void SCH_EDIT_FRAME::ClearUndoORRedoList( UNDO_REDO_LIST whichList, int aItemCount )
393 {
394  if( aItemCount == 0 )
395  return;
396 
397  UNDO_REDO_CONTAINER& list = whichList == UNDO_LIST ? m_undoList : m_redoList;
398 
399  for( PICKED_ITEMS_LIST* command : list.m_CommandsList )
400  {
401  command->ClearListAndDeleteItems();
402  delete command;
403  }
404 
405  list.m_CommandsList.clear();
406 }
407 
408 
KIGFX::SCH_VIEW * GetView() const override
Return a pointer to the #VIEW instance used in the panel.
EDA_ITEM * GetPickedItemLink(unsigned int aIdx) const
void ClearHiddenFlags()
Clear the hide flag of all items in the view.
Definition: sch_view.cpp:171
void ClearTempFlags()
Definition: eda_item.h:166
void SetFlags(EDA_ITEM_FLAGS aFlags)
void StartNewUndo()
Create a new, blank stack for future Undo commands to be pushed to.
UNDO_REDO_CONTAINER m_undoList
void RecacheAllItems()
Rebuild GAL display lists.
Definition: view.cpp:1380
static TOOL_ACTION zoomFitScreen
Definition: actions.h:96
void Restore(EDA_DRAW_FRAME *aFrame, KIGFX::VIEW *aView=nullptr)
void SetFlags(EDA_ITEM_FLAGS aMask)
Definition: eda_item.h:153
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 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
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:78
virtual void SwapData(SCH_ITEM *aItem)
Swap the internal data structures aItem with the schematic item.
Definition: sch_item.cpp:197
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)
void SyncView()
Mark all items for refresh.
SCH_DRAW_PANEL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
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:155
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
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 SwapSymbolLinks(const SCH_SYMBOL *aOriginalSymbol, const SCH_SYMBOL *aNewSymbol)
Definition: sch_screen.cpp:719
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:100
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:172
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:197
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:426
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:113