KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <symbol_edit_frame.h>
23#include <sch_screen.h>
25#include <widgets/lib_tree.h>
26#include <tool/tool_manager.h>
29
30
31void SYMBOL_EDIT_FRAME::PushSymbolToUndoList( const wxString& aDescription, LIB_SYMBOL* aSymbolCopy,
32 UNDO_REDO aUndoType )
33{
34 if( !aSymbolCopy )
35 return;
36
37 PICKED_ITEMS_LIST* lastcmd = new PICKED_ITEMS_LIST();
38
39 // Clear current flags (which can be temporary set by a current edit command).
40 aSymbolCopy->ClearTempFlags();
41 aSymbolCopy->ClearEditFlags();
42 aSymbolCopy->SetFlags( UR_TRANSIENT );
43
44 ITEM_PICKER wrapper( GetScreen(), aSymbolCopy, aUndoType );
45 lastcmd->PushItem( wrapper );
46 lastcmd->SetDescription( aDescription );
47 PushCommandToUndoList( lastcmd );
48
49 // Clear redo list, because after new save there is no redo to do.
51}
52
53
54void SYMBOL_EDIT_FRAME::SaveCopyInUndoList( const wxString& aDescription, LIB_SYMBOL* aSymbol,
55 UNDO_REDO aUndoType )
56{
57 if( aSymbol )
58 PushSymbolToUndoList( aDescription, new LIB_SYMBOL( *aSymbol ), aUndoType );
59}
60
61
63{
64 if( GetRedoCommandCount() <= 0 )
65 return;
66
67 // Load the last redo entry
69 ITEM_PICKER redoWrapper = redoCommand->PopItem();
70 wxString description = redoCommand->GetDescription();
71
72 delete redoCommand;
73
74 LIB_SYMBOL* symbol = (LIB_SYMBOL*) redoWrapper.GetItem();
75 UNDO_REDO undoRedoType = redoWrapper.GetStatus();
76 wxCHECK( symbol, /* void */ );
77 symbol->ClearFlags( UR_TRANSIENT );
78
79 // Store the current symbol in the undo buffer
80 PICKED_ITEMS_LIST* undoCommand = new PICKED_ITEMS_LIST();
81 LIB_SYMBOL* oldSymbol = m_symbol;
82
83 oldSymbol->SetFlags( UR_TRANSIENT );
84 ITEM_PICKER undoWrapper( GetScreen(), oldSymbol, undoRedoType );
85 undoCommand->SetDescription( description );
86 undoCommand->PushItem( undoWrapper );
87 PushCommandToUndoList( undoCommand );
88
89 // Do not delete the previous symbol by calling SetCurSymbol( symbol )
90 // which calls delete <previous symbol>.
91 // <previous symbol> is now put in undo list and is owned by this list
92 // Just set the current symbol to the symbol which come from the redo list
93 m_symbol = symbol;
94
95 if( m_activeTab )
96 m_activeTab->RefreshFrameOwnedObjects( m_symbol, GetScreen() );
97
98 if( undoRedoType == UNDO_REDO::LIB_RENAME )
99 {
100 m_libMgr->UpdateSymbolAfterRename( symbol, oldSymbol->GetName(), oldSymbol->GetLibNickname() );
101
102 // Reselect the renamed symbol
103 m_treePane->GetLibTree()->SelectLibId( LIB_ID( symbol->GetLibNickname(), symbol->GetName() ) );
104 }
105
107
108 RenameSymbolTab( oldSymbol->GetLibId(), symbol->GetLibId() );
109
110 RebuildView();
111 OnModify();
112}
113
114
116{
117 if( GetUndoCommandCount() <= 0 )
118 return;
119
120 // Load the last undo entry
122 wxString description = undoCommand->GetDescription();
123 ITEM_PICKER undoWrapper = undoCommand->PopItem();
124
125 delete undoCommand;
126
127 LIB_SYMBOL* symbol = (LIB_SYMBOL*) undoWrapper.GetItem();
128 UNDO_REDO undoRedoType = undoWrapper.GetStatus();
129 wxCHECK( symbol, /* void */ );
130 symbol->ClearFlags( UR_TRANSIENT );
131
132 // Store the current symbol in the redo buffer
133 PICKED_ITEMS_LIST* redoCommand = new PICKED_ITEMS_LIST();
134 LIB_SYMBOL* oldSymbol = m_symbol;
135
136 oldSymbol->SetFlags( UR_TRANSIENT );
137 ITEM_PICKER redoWrapper( GetScreen(), oldSymbol, undoRedoType );
138 redoCommand->PushItem( redoWrapper );
139 redoCommand->SetDescription( description );
140 PushCommandToRedoList( redoCommand );
141
142 // Do not delete the previous symbol by calling SetCurSymbol( symbol ),
143 // which calls delete <previous symbol>.
144 // <previous symbol> is now put in redo list and is owned by this list.
145 // Just set the current symbol to the symbol which come from the undo list
146 m_symbol = symbol;
147
148 if( m_activeTab )
149 m_activeTab->RefreshFrameOwnedObjects( m_symbol, GetScreen() );
150
151 if( undoRedoType == UNDO_REDO::LIB_RENAME )
152 {
153 m_libMgr->UpdateSymbolAfterRename( symbol, oldSymbol->GetName(), oldSymbol->GetLibNickname() );
154
155 // Reselect the renamed symbol
156 m_treePane->GetLibTree()->SelectLibId( LIB_ID( symbol->GetLibNickname(), symbol->GetName() ) );
157 }
158
160
161 RenameSymbolTab( oldSymbol->GetLibId(), symbol->GetLibId() );
162
163 RebuildView();
164 OnModify();
165}
166
167
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:152
void ClearFlags(EDA_ITEM_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition eda_item.h:154
EDA_ITEM * GetItem() const
UNDO_REDO GetStatus() const
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
Define a library symbol object.
Definition lib_symbol.h:114
const LIB_ID & GetLibId() const override
Definition lib_symbol.h:183
void ClearTempFlags() override
Clears the status flag all draw objects in this symbol.
wxString GetName() const override
Definition lib_symbol.h:176
wxString GetLibNickname() const override
Sets the Description field text value.
Definition lib_symbol.h:189
void ClearEditFlags() override
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)
wxString GetDescription() const
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
void RenameSymbolTab(const LIB_ID &aOldId, const LIB_ID &aNewId)
Update the open tab for aOldId, if any, to the renamed symbol aNewId so its label and key track the r...
void RebuildSymbolUnitAndBodyStyleLists()
LIB_SYMBOL_LIBRARY_MANAGER * m_libMgr
void PushSymbolToUndoList(const wxString &aDescription, LIB_SYMBOL *aSymbolCopy, UNDO_REDO aUndoType=UNDO_REDO::LIBEDIT)
SYMBOL_EDITOR_TAB_CONTEXT * m_activeTab
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.
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.
#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...