KiCad PCB EDA Suite
libedit_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-2017 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 <fctsys.h>
26 #include <sch_draw_panel.h>
27 
28 #include <lib_edit_frame.h>
29 #include <class_libentry.h>
30 #include <lib_manager.h>
31 #include <widgets/lib_tree.h>
32 #include <symbol_tree_pane.h>
33 #include <tool/tool_manager.h>
34 #include <tools/ee_actions.h>
36 
37 void LIB_EDIT_FRAME::SaveCopyInUndoList( EDA_ITEM* ItemToCopy, UNDO_REDO_T undoType, bool aAppend )
38 {
39  wxASSERT_MSG( !aAppend, "Append not needed/supported for LibEdit" );
40 
41  LIB_PART* CopyItem;
42  PICKED_ITEMS_LIST* lastcmd = new PICKED_ITEMS_LIST();
43 
44  CopyItem = new LIB_PART( * (LIB_PART*) ItemToCopy );
45 
46  // Clear current flags (which can be temporary set by a current edit command).
47  CopyItem->ClearTempFlags();
48  CopyItem->ClearEditFlags();
49  CopyItem->SetFlags( UR_TRANSIENT );
50 
51  ITEM_PICKER wrapper( CopyItem, undoType );
52  lastcmd->PushItem( wrapper );
53  GetScreen()->PushCommandToUndoList( lastcmd );
54 
55  // Clear redo list, because after new save there is no redo to do.
56  GetScreen()->ClearUndoORRedoList( GetScreen()->m_RedoList );
57 }
58 
59 
61 {
62  if( GetScreen()->GetRedoCommandCount() <= 0 )
63  return;
64 
66 
67  // Load the last redo entry
69  ITEM_PICKER redoWrapper = redoCommand->PopItem();
70  delete redoCommand;
71  LIB_PART* part = (LIB_PART*) redoWrapper.GetItem();
72  wxCHECK( part, /* void */ );
73  part->ClearFlags( UR_TRANSIENT );
74  UNDO_REDO_T undoRedoType = redoWrapper.GetStatus();
75 
76  // Store the current part in the undo buffer
77  PICKED_ITEMS_LIST* undoCommand = new PICKED_ITEMS_LIST();
78  LIB_PART* oldPart = GetCurPart();
79  oldPart->SetFlags( UR_TRANSIENT );
80  ITEM_PICKER undoWrapper( oldPart, undoRedoType );
81  undoCommand->PushItem( undoWrapper );
82  GetScreen()->PushCommandToUndoList( undoCommand );
83 
84  // Do not delete the previous part by calling SetCurPart( part )
85  // which calls delete <previous part>.
86  // <previous part> is now put in undo list and is owned by this list
87  // Just set the current part to the part which come from the redo list
88  m_my_part = part;
89 
90  if( undoRedoType == UR_LIB_RENAME )
91  {
92  wxString lib = GetCurLib();
93  m_libMgr->UpdatePartAfterRename( part, oldPart->GetName(), lib );
94 
95  // Reselect the renamed part
96  m_treePane->GetLibTree()->SelectLibId( LIB_ID( lib, part->GetName() ) );
97  }
98 
100  SetShowDeMorgan( part->HasConversion() );
101  updateTitle();
102 
103  RebuildView();
104  OnModify();
105 }
106 
107 
109 {
110  if( GetScreen()->GetUndoCommandCount() <= 0 )
111  return;
112 
114 
115  // Load the last undo entry
117  ITEM_PICKER undoWrapper = undoCommand->PopItem();
118  delete undoCommand;
119  LIB_PART* part = (LIB_PART*) undoWrapper.GetItem();
120  wxCHECK( part, /* void */ );
121  part->ClearFlags( UR_TRANSIENT );
122  UNDO_REDO_T undoRedoType = undoWrapper.GetStatus();
123 
124  // Store the current part in the redo buffer
125  PICKED_ITEMS_LIST* redoCommand = new PICKED_ITEMS_LIST();
126  LIB_PART* oldPart = GetCurPart();
127  oldPart->SetFlags( UR_TRANSIENT );
128  ITEM_PICKER redoWrapper( oldPart, undoRedoType );
129  redoCommand->PushItem( redoWrapper );
130  GetScreen()->PushCommandToRedoList( redoCommand );
131 
132  // Do not delete the previous part by calling SetCurPart( part ),
133  // which calls delete <previous part>.
134  // <previous part> is now put in redo list and is owned by this list.
135  // Just set the current part to the part which come from the undo list
136  m_my_part = part;
137 
138  if( undoRedoType == UR_LIB_RENAME )
139  {
140  wxString lib = GetCurLib();
141  m_libMgr->UpdatePartAfterRename( part, oldPart->GetName(), lib );
142 
143  // Reselect the renamed part
144  m_treePane->GetLibTree()->SelectLibId( LIB_ID( lib, part->GetName() ) );
145  }
146 
148  SetShowDeMorgan( part->HasConversion() );
149  updateTitle();
150 
151  RebuildView();
152  OnModify();
153 }
154 
155 
157 {
159 
160  // Load the last undo entry
162  ITEM_PICKER undoWrapper = undoCommand->PopItem();
163  delete undoCommand;
164  LIB_PART* part = (LIB_PART*) undoWrapper.GetItem();
165  part->ClearFlags( UR_TRANSIENT );
166  SetCurPart( part );
167 
169  selTool->RebuildSelection();
170 
172  SetShowDeMorgan( part->HasConversion() );
173 
174  RebuildView();
175 }
virtual void PushCommandToUndoList(PICKED_ITEMS_LIST *aItem)
Function PushCommandToUndoList add a command to undo in undo list delete the very old commands when t...
LIB_PART * GetCurPart() const
Return the current part being edited or NULL if none selected.
bool HasConversion() const
Test if part has more than one body conversion type (DeMorgan).
UNDO_REDO_T GetStatus() const
UNDO_REDO_T
Undo Redo considerations: Basically we have 3 cases New item Deleted item Modified item there is also...
EDA_ITEM * GetItem() const
LIB_MANAGER * m_libMgr
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:109
void PushItem(const ITEM_PICKER &aItem)
Function PushItem pushes aItem to the top of the list.
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
LIB_PART * m_my_part
void ClearTempFlags()
Clears the status flag all draw objects in this part.
wxString GetCurLib() const
The nickname of the current library being edited and empty string if none.
void SetShowDeMorgan(bool show)
virtual PICKED_ITEMS_LIST * PopCommandFromUndoList()
PopCommandFromUndoList return the last command to undo and remove it from list nothing is deleted.
void SetFlags(STATUS_FLAGS aMask)
Definition: base_struct.h:252
Class LIB_PIN definition.
virtual PICKED_ITEMS_LIST * PopCommandFromRedoList()
PopCommandFromRedoList return the last command to undo and remove it from list nothing is deleted.
Define a library symbol object.
static TOOL_ACTION clearSelection
Clears the current selection.
Definition: ee_actions.h:57
void GetComponentFromRedoList()
Class PICKED_ITEMS_LIST is a holder to handle information on schematic or board items.
void RebuildSymbolUnitsList()
void updateTitle()
Updates the main window title bar with the current library name and read only status of the library.
Definition: libedit.cpp:48
void SetCurPart(LIB_PART *aPart)
Take ownership of aPart and notes that it is the one currently being edited.
const wxString & GetName() const
void ClearEditFlags()
void RebuildSelection()
Rebuilds the selection from the EDA_ITEMs' selection flags.
virtual void PushCommandToRedoList(PICKED_ITEMS_LIST *aItem)
Function PushCommandToRedoList add a command to redo in redo list delete the very old commands when t...
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:150
ITEM_PICKER PopItem()
Function PopItem.
void OnModify() override
Must be called after a schematic change in order to set the "modify" flag of the current symbol.
void ClearFlags(STATUS_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: base_struct.h:253
#define UR_TRANSIENT
indicates the item is owned by the undo/redo stack
Definition: base_struct.h:134
TOOL_MANAGER * m_toolManager
virtual void ClearUndoORRedoList(UNDO_REDO_CONTAINER &aList, int aItemCount=-1) override
Free the undo or redo list from aList element.
Definition: sch_screen.cpp:598
LIB_TREE * GetLibTree() const
SYMBOL_TREE_PANE * m_treePane
void SaveCopyInUndoList(EDA_ITEM *ItemToCopy, UNDO_REDO_T undoType=UR_LIBEDIT, bool aAppend=false)
Create a copy of the current component, and save it in the undo list.
void SelectLibId(const LIB_ID &aLibId)
Select an item in the tree widget.
Definition: lib_tree.cpp:159
bool UpdatePartAfterRename(LIB_PART *aPart, const wxString &oldAlias, const wxString &aLibrary)
Updates the part buffer with a new version of the part when the name has changed.