KiCad PCB EDA Suite
symbol_editor_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) 2007 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2014-2020 KiCad Developers, see CHANGELOG.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 <symbol_edit_frame.h>
26 #include <symbol_library_manager.h>
27 #include <widgets/lib_tree.h>
28 #include <symbol_tree_pane.h>
29 #include <tool/tool_manager.h>
30 #include <tools/ee_actions.h>
32 
33 void SYMBOL_EDIT_FRAME::SaveCopyInUndoList( EDA_ITEM* aItem, UNDO_REDO aUndoType, bool aAppend )
34 {
35  wxASSERT_MSG( !aAppend, "Append not needed/supported for symbol editor" );
36 
37  if( !aItem )
38  return;
39 
40  LIB_PART* copyItem;
41  PICKED_ITEMS_LIST* lastcmd = new PICKED_ITEMS_LIST();
42 
43  copyItem = new LIB_PART( * (LIB_PART*) aItem );
44 
45  // Clear current flags (which can be temporary set by a current edit command).
46  copyItem->ClearTempFlags();
47  copyItem->ClearEditFlags();
48  copyItem->SetFlags( UR_TRANSIENT );
49 
50  ITEM_PICKER wrapper( GetScreen(), copyItem, aUndoType );
51  lastcmd->PushItem( wrapper );
52  PushCommandToUndoList( lastcmd );
53 
54  // Clear redo list, because after new save there is no redo to do.
56 }
57 
58 
60 {
61  if( GetRedoCommandCount() <= 0 )
62  return;
63 
65 
66  // Load the last redo entry
68  ITEM_PICKER redoWrapper = redoCommand->PopItem();
69  delete redoCommand;
70  LIB_PART* part = (LIB_PART*) redoWrapper.GetItem();
71  wxCHECK( part, /* void */ );
72  part->ClearFlags( UR_TRANSIENT );
73  UNDO_REDO undoRedoType = redoWrapper.GetStatus();
74 
75  // Store the current part in the undo buffer
76  PICKED_ITEMS_LIST* undoCommand = new PICKED_ITEMS_LIST();
77  LIB_PART* oldPart = m_my_part;
78  oldPart->SetFlags( UR_TRANSIENT );
79  ITEM_PICKER undoWrapper( GetScreen(), oldPart, undoRedoType );
80  undoCommand->PushItem( undoWrapper );
81  PushCommandToUndoList( undoCommand );
82 
83  // Do not delete the previous part by calling SetCurPart( part )
84  // which calls delete <previous part>.
85  // <previous part> is now put in undo list and is owned by this list
86  // Just set the current part to the part which come from the redo list
87  m_my_part = part;
88 
89  if( undoRedoType == UNDO_REDO::LIB_RENAME )
90  {
91  wxString lib = GetCurLib();
92  m_libMgr->UpdatePartAfterRename( part, oldPart->GetName(), lib );
93 
94  // Reselect the renamed part
95  m_treePane->GetLibTree()->SelectLibId( LIB_ID( lib, part->GetName() ) );
96  }
97 
99  SetShowDeMorgan( part->HasConversion() );
100  updateTitle();
101 
102  RebuildView();
103  OnModify();
104 }
105 
106 
108 {
109  if( GetUndoCommandCount() <= 0 )
110  return;
111 
113 
114  // Load the last undo entry
115  PICKED_ITEMS_LIST* undoCommand = PopCommandFromUndoList();
116  ITEM_PICKER undoWrapper = undoCommand->PopItem();
117  delete undoCommand;
118  LIB_PART* part = (LIB_PART*) undoWrapper.GetItem();
119  wxCHECK( part, /* void */ );
120  part->ClearFlags( UR_TRANSIENT );
121  UNDO_REDO undoRedoType = undoWrapper.GetStatus();
122 
123  // Store the current part in the redo buffer
124  PICKED_ITEMS_LIST* redoCommand = new PICKED_ITEMS_LIST();
125  LIB_PART* oldPart = m_my_part;
126  oldPart->SetFlags( UR_TRANSIENT );
127  ITEM_PICKER redoWrapper( GetScreen(), oldPart, undoRedoType );
128  redoCommand->PushItem( redoWrapper );
129  PushCommandToRedoList( redoCommand );
130 
131  // Do not delete the previous part by calling SetCurPart( part ),
132  // which calls delete <previous part>.
133  // <previous part> is now put in redo list and is owned by this list.
134  // Just set the current part to the part which come from the undo list
135  m_my_part = part;
136 
137  if( undoRedoType == UNDO_REDO::LIB_RENAME )
138  {
139  wxString lib = GetCurLib();
140  m_libMgr->UpdatePartAfterRename( part, oldPart->GetName(), lib );
141 
142  // Reselect the renamed part
143  m_treePane->GetLibTree()->SelectLibId( LIB_ID( lib, part->GetName() ) );
144  }
145 
147  SetShowDeMorgan( part->HasConversion() );
148  updateTitle();
149 
150  RebuildView();
151  OnModify();
152 }
153 
154 
156 {
158 
159  // Load the last undo entry
160  PICKED_ITEMS_LIST* undoCommand = PopCommandFromUndoList();
161 
162  // Check if we were already at the top of the stack
163  if( !undoCommand )
164  return;
165 
166  ITEM_PICKER undoWrapper = undoCommand->PopItem();
167  delete undoCommand;
168  LIB_PART* part = (LIB_PART*) undoWrapper.GetItem();
169  part->ClearFlags( UR_TRANSIENT );
170  SetCurPart( part, false );
171 
173  selTool->RebuildSelection();
174 
176  SetShowDeMorgan( part->HasConversion() );
177 
178  RebuildView();
179 }
void OnModify() override
Must be called after a schematic change in order to set the "modify" flag of the current symbol.
bool UpdatePartAfterRename(LIB_PART *aPart, const wxString &oldAlias, const wxString &aLibrary)
Update the part buffer with a new version of the part when the name has changed.
#define UR_TRANSIENT
indicates the item is owned by the undo/redo stack
Definition: eda_item.h:134
wxString GetName() const override
Definition: lib_symbol.h:129
bool HasConversion() const
Test if part has more than one body conversion type (DeMorgan).
void SetShowDeMorgan(bool show)
virtual void PushCommandToRedoList(PICKED_ITEMS_LIST *aItem)
Add a command to redo in the redo list.
EDA_ITEM * GetItem() const
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
SYMBOL_TREE_PANE * m_treePane
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Run the specified action.
Definition: tool_manager.h:141
virtual void PushCommandToUndoList(PICKED_ITEMS_LIST *aItem)
Add a command to undo in the undo list.
virtual int GetRedoCommandCount() const
void PushItem(const ITEM_PICKER &aItem)
Push aItem to the top of the list.
virtual PICKED_ITEMS_LIST * PopCommandFromRedoList()
Return the last command to undo and remove it from list, nothing is deleted.
void ClearUndoORRedoList(UNDO_REDO_LIST whichList, int aItemCount=-1) override
Free the undo or redo list from aList element.
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
wxString GetCurLib() const
The nickname of the current library being edited and empty string if none.
UNDO_REDO
Undo Redo considerations: Basically we have 3 cases New item Deleted item Modified item there is also...
void ClearTempFlags()
Clears the status flag all draw objects in this part.
void updateTitle()
Update the main window title bar with the current library name and read only status of the library.
void SetFlags(STATUS_FLAGS aMask)
Definition: eda_item.h:202
virtual int GetUndoCommandCount() const
void SaveCopyInUndoList(EDA_ITEM *aItem, UNDO_REDO aUndoType=UNDO_REDO::LIBEDIT, bool aAppend=false)
Create a copy of the current symbol, and save it in the undo list.
Define a library symbol object.
Definition: lib_symbol.h:93
virtual PICKED_ITEMS_LIST * PopCommandFromUndoList()
Return the last command to undo and remove it from list, nothing is deleted.
void SetCurPart(LIB_PART *aPart, bool aUpdateZoom)
Take ownership of aPart and notes that it is the one currently being edited.
static TOOL_ACTION clearSelection
Clears the current selection.
Definition: ee_actions.h:54
A holder to handle information on schematic or board items.
TOOL_MANAGER * m_toolManager
Definition: tools_holder.h:157
void ClearEditFlags()
void RebuildSelection()
Rebuild the selection from the EDA_ITEMs' selection flags.
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:149
void ClearFlags(STATUS_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: eda_item.h:203
SYMBOL_LIBRARY_MANAGER * m_libMgr
LIB_TREE * GetLibTree() const
UNDO_REDO GetStatus() const
void SelectLibId(const LIB_ID &aLibId)
Select an item in the tree widget.
Definition: lib_tree.cpp:177