KiCad PCB EDA Suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 The KiCad Developers, see AUTHORS.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>
27#include <widgets/lib_tree.h>
28#include <symbol_tree_pane.h>
29#include <tool/tool_manager.h>
32
33
34void SYMBOL_EDIT_FRAME::PushSymbolToUndoList( const wxString& aDescription, LIB_SYMBOL* aSymbolCopy,
35 UNDO_REDO aUndoType )
36{
37 if( !aSymbolCopy )
38 return;
39
40 auto* drawingTool = GetToolManager()->GetTool<SYMBOL_EDITOR_DRAWING_TOOLS>();
41
42 PICKED_ITEMS_LIST* lastcmd = new PICKED_ITEMS_LIST();
43
44 // Clear current flags (which can be temporary set by a current edit command).
45 aSymbolCopy->ClearTempFlags();
46 aSymbolCopy->ClearEditFlags();
47 aSymbolCopy->SetFlags( UR_TRANSIENT );
48
49 ITEM_PICKER wrapper( GetScreen(), aSymbolCopy, aUndoType );
50 wrapper.SetGroupId( drawingTool->GetLastPin() );
51 lastcmd->PushItem( wrapper );
52 lastcmd->SetDescription( aDescription );
53 PushCommandToUndoList( lastcmd );
54
55 // Clear redo list, because after new save there is no redo to do.
57}
58
59
60void SYMBOL_EDIT_FRAME::SaveCopyInUndoList( const wxString& aDescription, LIB_SYMBOL* aSymbol,
61 UNDO_REDO aUndoType )
62{
63 if( aSymbol )
64 PushSymbolToUndoList( aDescription, new LIB_SYMBOL( *aSymbol ), aUndoType );
65}
66
67
69{
70 if( GetRedoCommandCount() <= 0 )
71 return;
72
73 auto* drawingTool = GetToolManager()->GetTool<SYMBOL_EDITOR_DRAWING_TOOLS>();
74
75 // Load the last redo entry
77 ITEM_PICKER redoWrapper = redoCommand->PopItem();
78 wxString description = redoCommand->GetDescription();
79
80 delete redoCommand;
81
82 LIB_SYMBOL* symbol = (LIB_SYMBOL*) redoWrapper.GetItem();
83 KIID lastPin = redoWrapper.GetGroupId();
84 UNDO_REDO undoRedoType = redoWrapper.GetStatus();
85 wxCHECK( symbol, /* void */ );
86 symbol->ClearFlags( UR_TRANSIENT );
87
88 // Store the current symbol in the undo buffer
89 PICKED_ITEMS_LIST* undoCommand = new PICKED_ITEMS_LIST();
90 LIB_SYMBOL* oldSymbol = m_symbol;
91
92 oldSymbol->SetFlags( UR_TRANSIENT );
93 ITEM_PICKER undoWrapper( GetScreen(), oldSymbol, undoRedoType );
94 undoWrapper.SetGroupId( drawingTool->GetLastPin() );
95 undoCommand->SetDescription( description );
96 undoCommand->PushItem( undoWrapper );
97 PushCommandToUndoList( undoCommand );
98
99 // Do not delete the previous symbol by calling SetCurSymbol( symbol )
100 // which calls delete <previous symbol>.
101 // <previous symbol> is now put in undo list and is owned by this list
102 // Just set the current symbol to the symbol which come from the redo list
103 m_symbol = symbol;
104 drawingTool->SetLastPin( lastPin );
105
106 if( undoRedoType == UNDO_REDO::LIB_RENAME )
107 {
108 wxString lib = GetCurLib();
109 m_libMgr->UpdateSymbolAfterRename( symbol, oldSymbol->GetName(), lib );
110
111 // Reselect the renamed symbol
112 m_treePane->GetLibTree()->SelectLibId( LIB_ID( lib, symbol->GetName() ) );
113 }
114
117 UpdateTitle();
118
119 RebuildView();
120 OnModify();
121}
122
123
125{
126 if( GetUndoCommandCount() <= 0 )
127 return;
128
129 auto* drawingTool = GetToolManager()->GetTool<SYMBOL_EDITOR_DRAWING_TOOLS>();
130
131 // Load the last undo entry
133 wxString description = undoCommand->GetDescription();
134 ITEM_PICKER undoWrapper = undoCommand->PopItem();
135
136 delete undoCommand;
137
138 LIB_SYMBOL* symbol = (LIB_SYMBOL*) undoWrapper.GetItem();
139 KIID lastPin = undoWrapper.GetGroupId();
140 UNDO_REDO undoRedoType = undoWrapper.GetStatus();
141 wxCHECK( symbol, /* void */ );
142 symbol->ClearFlags( UR_TRANSIENT );
143
144 // Store the current symbol in the redo buffer
145 PICKED_ITEMS_LIST* redoCommand = new PICKED_ITEMS_LIST();
146 LIB_SYMBOL* oldSymbol = m_symbol;
147
148 oldSymbol->SetFlags( UR_TRANSIENT );
149 ITEM_PICKER redoWrapper( GetScreen(), oldSymbol, undoRedoType );
150 redoWrapper.SetGroupId( drawingTool->GetLastPin() );
151 redoCommand->PushItem( redoWrapper );
152 redoCommand->SetDescription( description );
153 PushCommandToRedoList( redoCommand );
154
155 // Do not delete the previous symbol by calling SetCurSymbol( symbol ),
156 // which calls delete <previous symbol>.
157 // <previous symbol> is now put in redo list and is owned by this list.
158 // Just set the current symbol to the symbol which come from the undo list
159 m_symbol = symbol;
160 drawingTool->SetLastPin( lastPin );
161
162 if( undoRedoType == UNDO_REDO::LIB_RENAME )
163 {
164 wxString lib = GetCurLib();
165 m_libMgr->UpdateSymbolAfterRename( symbol, oldSymbol->GetName(), lib );
166
167 // Reselect the renamed symbol
168 m_treePane->GetLibTree()->SelectLibId( LIB_ID( lib, symbol->GetName() ) );
169 }
170
173 UpdateTitle();
174
175 RebuildView();
176 OnModify();
177}
178
179
virtual void PushCommandToUndoList(PICKED_ITEMS_LIST *aItem)
Add a command to undo in the undo list.
virtual int GetRedoCommandCount() const
virtual PICKED_ITEMS_LIST * PopCommandFromRedoList()
Return the last command to undo and remove it from list, nothing is deleted.
virtual PICKED_ITEMS_LIST * PopCommandFromUndoList()
Return the last command to undo and remove it from list, nothing is deleted.
virtual int GetUndoCommandCount() const
virtual void PushCommandToRedoList(PICKED_ITEMS_LIST *aItem)
Add a command to redo in the redo list.
void SetFlags(EDA_ITEM_FLAGS aMask)
Definition: eda_item.h:135
void ClearFlags(EDA_ITEM_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: eda_item.h:137
void SetGroupId(KIID aId)
EDA_ITEM * GetItem() const
KIID GetGroupId() const
UNDO_REDO GetStatus() const
Definition: kiid.h:49
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
Define a library symbol object.
Definition: lib_symbol.h:85
void ClearTempFlags() override
Clears the status flag all draw objects in this symbol.
bool HasAlternateBodyStyle() const override
Test if symbol has more than one body conversion type (DeMorgan).
wxString GetName() const override
Definition: lib_symbol.h:149
void ClearEditFlags() override
void SelectLibId(const LIB_ID &aLibId)
Select an item in the tree widget.
Definition: lib_tree.cpp:362
A holder to handle information on schematic or board items.
void PushItem(const ITEM_PICKER &aItem)
Push aItem to the top of the list.
void SetDescription(const wxString &aDescription)
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
SYMBOL_EDITOR_DRAWING_TOOLS.
LIB_SYMBOL_LIBRARY_MANAGER * m_libMgr
wxString GetCurLib() const
The nickname of the current library being edited and empty string if none.
void PushSymbolToUndoList(const wxString &aDescription, LIB_SYMBOL *aSymbolCopy, UNDO_REDO aUndoType=UNDO_REDO::LIBEDIT)
void SaveCopyInUndoList(const wxString &aDescription, LIB_SYMBOL *aSymbol, UNDO_REDO aUndoType=UNDO_REDO::LIBEDIT)
Create a copy of the current symbol, and save it in the undo list.
void ClearUndoORRedoList(UNDO_REDO_LIST whichList, int aItemCount=-1) override
Free the undo or redo list from aList element.
void UpdateTitle()
Update the main window title bar with the current library name and read only status of the library.
SYMBOL_TREE_PANE * m_treePane
void OnModify() override
Must be called after a schematic change in order to set the "modify" flag of the current symbol.
void SetShowDeMorgan(bool show)
bool UpdateSymbolAfterRename(LIB_SYMBOL *aSymbol, const wxString &aOldSymbolName, const wxString &aLibrary)
Update the symbol buffer with a new version of the symbol when the name has changed.
LIB_TREE * GetLibTree() const
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:55
#define UR_TRANSIENT
indicates the item is owned by the undo/redo stack
UNDO_REDO
Undo Redo considerations: Basically we have 3 cases New item Deleted item Modified item there is also...