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-2020 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  SCH_ITEM* aItem,
100  UNDO_REDO aCommandType,
101  bool aAppend,
102  const wxPoint& aTransformPoint )
103 {
104  PICKED_ITEMS_LIST* commandToUndo = nullptr;
105 
106  wxCHECK( aItem, /* void */ );
107 
108  // Connectivity may change
109  aItem->SetConnectivityDirty();
110 
111  if( aAppend )
112  commandToUndo = PopCommandFromUndoList();
113 
114  if( !commandToUndo )
115  {
116  commandToUndo = new PICKED_ITEMS_LIST();
117  commandToUndo->m_TransformPoint = aTransformPoint;
118  }
119 
120  ITEM_PICKER itemWrapper( aScreen, aItem, aCommandType );
121  itemWrapper.SetFlags( aItem->GetFlags() );
122 
123  switch( aCommandType )
124  {
125  case UNDO_REDO::CHANGED: /* Create a copy of item */
126  itemWrapper.SetLink( aItem->Duplicate( true ) );
127  commandToUndo->PushItem( itemWrapper );
128  break;
129 
130  case UNDO_REDO::NEWITEM:
131  case UNDO_REDO::DELETED:
132  case UNDO_REDO::ROTATED:
133  case UNDO_REDO::MOVED:
134  commandToUndo->PushItem( itemWrapper );
135  break;
136 
137  default:
138  wxFAIL_MSG( wxString::Format( wxT( "SaveCopyInUndoList() error (unknown code %X)" ),
139  aCommandType ) );
140  break;
141  }
142 
143  if( commandToUndo->GetCount() )
144  {
145  /* Save the copy in undo list */
146  PushCommandToUndoList( commandToUndo );
147 
148  /* Clear redo list, because after new save there is no redo to do */
150  }
151  else
152  {
153  delete commandToUndo;
154  }
155 }
156 
157 
159  UNDO_REDO aTypeCommand,
160  bool aAppend,
161  const wxPoint& aTransformPoint )
162 {
163  PICKED_ITEMS_LIST* commandToUndo = nullptr;
164 
165  if( !aItemsList.GetCount() )
166  return;
167 
168  // Can't append a WIRE IMAGE, so fail to a new undo point
169  if( aAppend )
170  commandToUndo = PopCommandFromUndoList();
171 
172  if( !commandToUndo )
173  {
174  commandToUndo = new PICKED_ITEMS_LIST();
175  commandToUndo->m_TransformPoint = aTransformPoint;
176  commandToUndo->m_Status = aTypeCommand;
177  }
178 
179  // Copy picker list:
180  if( !commandToUndo->GetCount() )
181  commandToUndo->CopyList( aItemsList );
182  else
183  {
184  // Unless we are appending, in which case, get the picker items
185  for( unsigned ii = 0; ii < aItemsList.GetCount(); ii++ )
186  commandToUndo->PushItem( aItemsList.GetItemWrapper( ii) );
187  }
188 
189  // Verify list, and creates data if needed
190  for( unsigned ii = 0; ii < commandToUndo->GetCount(); ii++ )
191  {
192  SCH_ITEM* sch_item = dynamic_cast<SCH_ITEM*>( commandToUndo->GetPickedItem( ii ) );
193 
194  // Common items implemented in EDA_DRAW_FRAME will not be SCH_ITEMs.
195  if( !sch_item )
196  continue;
197 
198  // Connectivity may change
199  sch_item->SetConnectivityDirty();
200 
201  UNDO_REDO command = commandToUndo->GetPickedItemStatus( ii );
202 
203  if( command == UNDO_REDO::UNSPECIFIED )
204  {
205  command = aTypeCommand;
206  commandToUndo->SetPickedItemStatus( command, ii );
207  }
208 
209  switch( command )
210  {
211  case UNDO_REDO::CHANGED:
212 
213  /* If needed, create a copy of item, and put in undo list
214  * in the picker, as link
215  * If this link is not null, the copy is already done
216  */
217  if( commandToUndo->GetPickedItemLink( ii ) == nullptr )
218  commandToUndo->SetPickedItemLink( sch_item->Duplicate( true ), ii );
219 
220  wxASSERT( commandToUndo->GetPickedItemLink( ii ) );
221  break;
222 
223  case UNDO_REDO::MOVED:
226  case UNDO_REDO::ROTATED:
227  case UNDO_REDO::NEWITEM:
228  case UNDO_REDO::DELETED:
231  break;
232 
233  default:
234  wxFAIL_MSG( wxString::Format( wxT( "Unknown undo/redo command %d" ), command ) );
235  break;
236  }
237  }
238 
239  if( commandToUndo->GetCount() )
240  {
241  /* Save the copy in undo list */
242  PushCommandToUndoList( commandToUndo );
243 
244  /* Clear redo list, because after new save there is no redo to do */
246  }
247  else // Should not occur
248  {
249  delete commandToUndo;
250  }
251 }
252 
253 
255 {
256  // Undo in the reverse order of list creation: (this can allow stacked changes like the
257  // same item can be changed and deleted in the same complex command).
258  for( int ii = aList->GetCount() - 1; ii >= 0; ii-- )
259  {
260  UNDO_REDO status = aList->GetPickedItemStatus((unsigned) ii );
261  EDA_ITEM* eda_item = aList->GetPickedItem( (unsigned) ii );
262 
263  eda_item->SetFlags( aList->GetPickerFlags( (unsigned) ii ) );
264  eda_item->ClearEditFlags();
265  eda_item->ClearTempFlags();
266 
267  if( status == UNDO_REDO::NEWITEM )
268  {
269  // new items are deleted on undo
270  RemoveFromScreen( eda_item, (SCH_SCREEN*) aList->GetScreenForItem( (unsigned) ii ) );
271  aList->SetPickedItemStatus( UNDO_REDO::DELETED, (unsigned) ii );
272  }
273  else if( status == UNDO_REDO::DELETED )
274  {
275  // deleted items are re-inserted on undo
276  AddToScreen( eda_item, (SCH_SCREEN*) aList->GetScreenForItem( (unsigned) ii ) );
277  aList->SetPickedItemStatus( UNDO_REDO::NEWITEM, (unsigned) ii );
278  }
279  else if( status == UNDO_REDO::PAGESETTINGS )
280  {
281  // swap current settings with stored settings
282  WS_PROXY_UNDO_ITEM alt_item( this );
283  WS_PROXY_UNDO_ITEM* item = (WS_PROXY_UNDO_ITEM*) eda_item;
284  item->Restore( this );
285  *item = alt_item;
287  }
288  else if( dynamic_cast<SCH_ITEM*>( eda_item ) )
289  {
290  // everything else is modified in place
291  SCH_ITEM* item = (SCH_ITEM*) eda_item;
292  SCH_ITEM* alt_item = (SCH_ITEM*) aList->GetPickedItemLink( (unsigned) ii );
293 
294  // The root sheet is a pseudo object that owns the root screen object but is not on
295  // the root screen so do not attempt to remove it from the screen it owns.
296  if( item != &Schematic().Root() )
297  RemoveFromScreen( item, (SCH_SCREEN*) aList->GetScreenForItem( (unsigned) ii ) );
298 
299  switch( status )
300  {
301  case UNDO_REDO::CHANGED:
302  item->SwapData( alt_item );
303 
304  if( item->Type() == SCH_COMPONENT_T )
305  static_cast<SCH_COMPONENT*>( item )->UpdatePins();
306 
307  break;
308 
309  case UNDO_REDO::MOVED:
310  item->Move( aRedoCommand ? aList->m_TransformPoint : -aList->m_TransformPoint );
311  break;
312 
314  item->MirrorY( aList->m_TransformPoint.x );
315  break;
316 
318  item->MirrorX( aList->m_TransformPoint.y );
319  break;
320 
321  case UNDO_REDO::ROTATED:
322  if( aRedoCommand )
323  item->Rotate( aList->m_TransformPoint );
324  else
325  {
326  // Rotate 270 deg to undo 90-deg rotate
327  item->Rotate( aList->m_TransformPoint );
328  item->Rotate( aList->m_TransformPoint );
329  item->Rotate( aList->m_TransformPoint );
330  }
331  break;
332 
334  aList->SetPickedItem( alt_item, (unsigned) ii );
335  aList->SetPickedItemLink( item, (unsigned) ii );
336  item = alt_item;
337  break;
338 
339  default:
340  wxFAIL_MSG( wxString::Format( wxT( "Unknown undo/redo command %d" ),
341  aList->GetPickedItemStatus( (unsigned) ii ) ) );
342  break;
343  }
344 
345  if( item != &Schematic().Root() )
346  AddToScreen( item, (SCH_SCREEN*) aList->GetScreenForItem( (unsigned) ii ) );
347  }
348  }
349 
351  selTool->RebuildSelection();
352 
353  // Bitmaps are cached in Opengl: clear the cache, because
354  // the cache data can be invalid
357 }
358 
359 
361 {
363 
364  if( undo )
365  {
366  PutDataInPreviousState( undo, false );
367  undo->ClearListAndDeleteItems();
368  delete undo;
369 
371 
373  }
374 
375  SyncView();
376  GetCanvas()->Refresh();
377 }
378 
379 
380 void SCH_EDIT_FRAME::ClearUndoORRedoList( UNDO_REDO_LIST whichList, int aItemCount )
381 {
382  if( aItemCount == 0 )
383  return;
384 
385  UNDO_REDO_CONTAINER& list = whichList == UNDO_LIST ? m_undoList : m_redoList;
386 
387  for( PICKED_ITEMS_LIST* command : list.m_CommandsList )
388  {
389  command->ClearListAndDeleteItems();
390  delete command;
391  }
392 
393  list.m_CommandsList.clear();
394 }
395 
396 
void Restore(EDA_DRAW_FRAME *aFrame, KIGFX::VIEW *aView=nullptr)
KIGFX::SCH_VIEW * GetView() const override
Function GetView() Returns a pointer to the VIEW instance used in the panel.
EDA_ITEM * GetPickedItemLink(unsigned int aIdx) const
Function GetPickedItemLink.
void ClearHiddenFlags()
Clear the hide flag of all items in the view.
Definition: sch_view.cpp:180
void ClearTempFlags()
Definition: eda_item.h:234
UNDO_REDO_CONTAINER m_undoList
void RecacheAllItems()
Function RecacheAllItems() Rebuilds GAL display lists.
Definition: view.cpp:1377
static TOOL_ACTION zoomFitScreen
Definition: actions.h:94
BASE_SCREEN * GetScreenForItem(unsigned int aIdx) const
Function GetScreenForItem.
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:141
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)
Function PushCommandToUndoList add a command to undo in undo list delete the very old commands when t...
STATUS_FLAGS GetPickerFlags(unsigned aIdx) const
Function GetPickerFlags returns the value of the picker flag.
void PushItem(const ITEM_PICKER &aItem)
Function PushItem pushes aItem to the top of the list.
bool TestDanglingEnds()
Test all of the connectable objects in the schematic for unused connection points.
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
Function GetCount.
virtual void Rotate(wxPoint aPosition)=0
Rotate the item around aPosition 90 degrees in the clockwise direction.
UNDO_REDO_CONTAINER is a holder to handle alist of undo (or redo) command.
void CopyList(const PICKED_ITEMS_LIST &aSource)
Function CopyList copies all data from aSource to the list.
void SetFlags(STATUS_FLAGS aMask)
Definition: eda_item.h:221
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:213
std::vector< PICKED_ITEMS_LIST * > m_CommandsList
bool SetPickedItem(EDA_ITEM *aItem, unsigned aIdx)
Function SetPickedItem.
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.
SCHEMATIC & Schematic() const
EDA_ITEM * GetPickedItem(unsigned int aIdx) const
Function GetPickedItem.
void SaveCopyInUndoList(SCH_SCREEN *aScreen, SCH_ITEM *aItemToCopy, UNDO_REDO aTypeCommand, bool aAppend, const wxPoint &aTransformPoint=wxPoint(0, 0))
Create a copy of the current schematic item, and put it in the undo list.
virtual PICKED_ITEMS_LIST * PopCommandFromUndoList()
PopCommandFromUndoList return the last command to undo and remove it from list nothing is deleted.
void SetFlags(STATUS_FLAGS aFlags)
virtual void MirrorX(int aXaxis_position)=0
Mirror item relative to the X axis about aXaxis_position.
PICKED_ITEMS_LIST is a holder to handle information on schematic or board items.
virtual void MirrorY(int aYaxis_position)=0
Mirror item relative to the Y axis about aYaxis_position.
UNDO_REDO_CONTAINER m_redoList
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=NULL) override
Update the board display after modifying it by a python script (note: it is automatically called by a...
void PutDataInPreviousState(PICKED_ITEMS_LIST *aList, bool aRedoCommand)
Restore an undo or redo command to put data pointed by aList in the previous state.
UNDO_REDO_LIST
Function ClearUndoORRedoList (virtual).
ITEM_PICKER GetItemWrapper(unsigned int aIdx) const
Function GetItemWrapper.
bool SetPickedItemStatus(UNDO_REDO aStatus, unsigned aIdx)
Function SetPickedItemStatus sets the type of undo/redo operation for a given picked item.
TOOL_MANAGER * m_toolManager
Definition: tools_holder.h:48
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 Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:201
void SetSheetNumberAndCount()
Set the m_ScreenNumber and m_NumberOfScreens members for screens.
void RollbackSchematicFromUndo()
Performs an undo of the last edit WITHOUT logging a corresponding redo.
void RebuildSelection()
Rebuilds the selection from the EDA_ITEMs' selection flags.
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: eda_item.h:148
bool SetPickedItemLink(EDA_ITEM *aLink, unsigned aIdx)
Function SetPickedItemLink set the link associated to a given picked item.
UNDO_REDO GetPickedItemStatus(unsigned int aIdx) const
Function GetPickedItemStatus.
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:76
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:240
STATUS_FLAGS GetFlags() const
Definition: eda_item.h:223
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:194
void ClearListAndDeleteItems()
Function ClearListAndDeleteItems deletes the list of pickers, AND the data pointed by m_PickedItem or...
void SetConnectivityDirty(bool aDirty=true)
Definition: sch_item.h:440
KICAD_T Type() const
Function Type()
Definition: eda_item.h:182
virtual void Move(const wxPoint &aMoveVector)=0
Move the item by aMoveVector to a new position.