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, 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 <tool/tool_manager.h>
31
32
33void SYMBOL_EDIT_FRAME::PushSymbolToUndoList( const wxString& aDescription, LIB_SYMBOL* aSymbolCopy,
34 UNDO_REDO aUndoType )
35{
36 if( !aSymbolCopy )
37 return;
38
39 PICKED_ITEMS_LIST* lastcmd = new PICKED_ITEMS_LIST();
40
41 // Clear current flags (which can be temporary set by a current edit command).
42 aSymbolCopy->ClearTempFlags();
43 aSymbolCopy->ClearEditFlags();
44 aSymbolCopy->SetFlags( UR_TRANSIENT );
45
46 ITEM_PICKER wrapper( GetScreen(), aSymbolCopy, aUndoType );
47 lastcmd->PushItem( wrapper );
48 lastcmd->SetDescription( aDescription );
49 PushCommandToUndoList( lastcmd );
50
51 // Clear redo list, because after new save there is no redo to do.
53}
54
55
56void SYMBOL_EDIT_FRAME::SaveCopyInUndoList( const wxString& aDescription, LIB_SYMBOL* aSymbol,
57 UNDO_REDO aUndoType )
58{
59 if( aSymbol )
60 PushSymbolToUndoList( aDescription, new LIB_SYMBOL( *aSymbol ), aUndoType );
61}
62
63
65{
66 if( GetRedoCommandCount() <= 0 )
67 return;
68
69 // Load the last redo entry
71 ITEM_PICKER redoWrapper = redoCommand->PopItem();
72 wxString description = redoCommand->GetDescription();
73
74 delete redoCommand;
75
76 LIB_SYMBOL* symbol = (LIB_SYMBOL*) redoWrapper.GetItem();
77 UNDO_REDO undoRedoType = redoWrapper.GetStatus();
78 wxCHECK( symbol, /* void */ );
79 symbol->ClearFlags( UR_TRANSIENT );
80
81 // Store the current symbol in the undo buffer
82 PICKED_ITEMS_LIST* undoCommand = new PICKED_ITEMS_LIST();
83 LIB_SYMBOL* oldSymbol = m_symbol;
84
85 oldSymbol->SetFlags( UR_TRANSIENT );
86 ITEM_PICKER undoWrapper( GetScreen(), oldSymbol, undoRedoType );
87 undoCommand->SetDescription( description );
88 undoCommand->PushItem( undoWrapper );
89 PushCommandToUndoList( undoCommand );
90
91 // Do not delete the previous symbol by calling SetCurSymbol( symbol )
92 // which calls delete <previous symbol>.
93 // <previous symbol> is now put in undo list and is owned by this list
94 // Just set the current symbol to the symbol which come from the redo list
95 m_symbol = symbol;
96
97 if( undoRedoType == UNDO_REDO::LIB_RENAME )
98 {
99 m_libMgr->UpdateSymbolAfterRename( symbol, oldSymbol->GetName(), oldSymbol->GetLibNickname() );
100
101 // Reselect the renamed symbol
102 m_treePane->GetLibTree()->SelectLibId( LIB_ID( symbol->GetLibNickname(), symbol->GetName() ) );
103 }
104
106 UpdateTitle();
107
108 RebuildView();
109 OnModify();
110}
111
112
114{
115 if( GetUndoCommandCount() <= 0 )
116 return;
117
118 // Load the last undo entry
120 wxString description = undoCommand->GetDescription();
121 ITEM_PICKER undoWrapper = undoCommand->PopItem();
122
123 delete undoCommand;
124
125 LIB_SYMBOL* symbol = (LIB_SYMBOL*) undoWrapper.GetItem();
126 UNDO_REDO undoRedoType = undoWrapper.GetStatus();
127 wxCHECK( symbol, /* void */ );
128 symbol->ClearFlags( UR_TRANSIENT );
129
130 // Store the current symbol in the redo buffer
131 PICKED_ITEMS_LIST* redoCommand = new PICKED_ITEMS_LIST();
132 LIB_SYMBOL* oldSymbol = m_symbol;
133
134 oldSymbol->SetFlags( UR_TRANSIENT );
135 ITEM_PICKER redoWrapper( GetScreen(), oldSymbol, undoRedoType );
136 redoCommand->PushItem( redoWrapper );
137 redoCommand->SetDescription( description );
138 PushCommandToRedoList( redoCommand );
139
140 // Do not delete the previous symbol by calling SetCurSymbol( symbol ),
141 // which calls delete <previous symbol>.
142 // <previous symbol> is now put in redo list and is owned by this list.
143 // Just set the current symbol to the symbol which come from the undo list
144 m_symbol = symbol;
145
146 if( undoRedoType == UNDO_REDO::LIB_RENAME )
147 {
148 m_libMgr->UpdateSymbolAfterRename( symbol, oldSymbol->GetName(), oldSymbol->GetLibNickname() );
149
150 // Reselect the renamed symbol
151 m_treePane->GetLibTree()->SelectLibId( LIB_ID( symbol->GetLibNickname(), symbol->GetName() ) );
152 }
153
155 UpdateTitle();
156
157 RebuildView();
158 OnModify();
159}
160
161
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:156
void ClearFlags(EDA_ITEM_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition eda_item.h:158
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:49
Define a library symbol object.
Definition lib_symbol.h:83
void ClearTempFlags() override
Clears the status flag all draw objects in this symbol.
wxString GetName() const override
Definition lib_symbol.h:145
wxString GetLibNickname() const override
Sets the Description field text value.
Definition lib_symbol.h:158
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 RebuildSymbolUnitAndBodyStyleLists()
LIB_SYMBOL_LIBRARY_MANAGER * m_libMgr
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.
#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...