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 <class_drawpanel.h>
27 
28 #include <lib_edit_frame.h>
29 #include <class_libentry.h>
30 #include <lib_manager.h>
31 #include <component_tree.h>
32 #include <cmp_tree_pane.h>
33 
34 
36 {
37  LIB_PART* CopyItem;
38  PICKED_ITEMS_LIST* lastcmd = new PICKED_ITEMS_LIST();
39 
40  CopyItem = new LIB_PART( * (LIB_PART*) ItemToCopy );
41 
42  // Clear current flags (which can be temporary set by a current edit command).
43  CopyItem->ClearStatus();
44  CopyItem->SetFlags( CopyItem->GetFlags() | UR_TRANSIENT );
45 
46  ITEM_PICKER wrapper( CopyItem, undoType );
47  lastcmd->PushItem( wrapper );
48  GetScreen()->PushCommandToUndoList( lastcmd );
49 
50  // Clear redo list, because after new save there is no redo to do.
51  GetScreen()->ClearUndoORRedoList( GetScreen()->m_RedoList );
52 }
53 
54 
55 void LIB_EDIT_FRAME::GetComponentFromRedoList( wxCommandEvent& event )
56 {
57  if( GetScreen()->GetRedoCommandCount() <= 0 )
58  return;
59 
60  // Load the last redo entry
62  ITEM_PICKER redoWrapper = redoCommand->PopItem();
63  delete redoCommand;
64  LIB_PART* part = (LIB_PART*) redoWrapper.GetItem();
65  wxCHECK( part, /* void */ );
66  part->SetFlags( part->GetFlags() & ~UR_TRANSIENT );
67  UNDO_REDO_T undoRedoType = redoWrapper.GetStatus();
68 
69  // Store the current part in the undo buffer
70  PICKED_ITEMS_LIST* undoCommand = new PICKED_ITEMS_LIST();
71  LIB_PART* oldPart = GetCurPart();
72  oldPart->SetFlags( oldPart->GetFlags() | UR_TRANSIENT );
73  ITEM_PICKER undoWrapper( oldPart, undoRedoType );
74  undoCommand->PushItem( undoWrapper );
75  GetScreen()->PushCommandToUndoList( undoCommand );
76 
77  // Do not delete the previous part by calling SetCurPart( part )
78  // which calls delete <previous part>.
79  // <previous part> is now put in undo list and is owned by this list
80  // Just set the current part to the part which come from the redo list
81  m_my_part = part;
82 
83  if( undoRedoType == UR_LIB_RENAME )
84  {
85  wxString lib = GetCurLib();
86  m_libMgr->UpdatePartAfterRename( part, oldPart->GetName(), lib );
87 
88  // Reselect the renamed part
89  m_treePane->GetCmpTree()->SelectLibId( LIB_ID( lib, part->GetName() ) );
90  }
91 
92  if( !m_aliasName.IsEmpty() && !part->HasAlias( m_aliasName ) )
93  m_aliasName = part->GetName();
94 
95  SetDrawItem( NULL );
98  SetShowDeMorgan( part->HasConversion() );
100  DisplayCmpDoc();
101  OnModify();
102  m_canvas->Refresh();
103 }
104 
105 
106 void LIB_EDIT_FRAME::GetComponentFromUndoList( wxCommandEvent& event )
107 {
108  if( GetScreen()->GetUndoCommandCount() <= 0 )
109  return;
110 
111  // Load the last undo entry
113  ITEM_PICKER undoWrapper = undoCommand->PopItem();
114  delete undoCommand;
115  LIB_PART* part = (LIB_PART*) undoWrapper.GetItem();
116  wxCHECK( part, /* void */ );
117  part->SetFlags( part->GetFlags() & ~UR_TRANSIENT );
118  UNDO_REDO_T undoRedoType = undoWrapper.GetStatus();
119 
120  // Store the current part in the redo buffer
121  PICKED_ITEMS_LIST* redoCommand = new PICKED_ITEMS_LIST();
122  LIB_PART* oldPart = GetCurPart();
123  oldPart->SetFlags( oldPart->GetFlags() | UR_TRANSIENT );
124  ITEM_PICKER redoWrapper( oldPart, undoRedoType );
125  redoCommand->PushItem( redoWrapper );
126  GetScreen()->PushCommandToRedoList( redoCommand );
127 
128  // Do not delete the previous part by calling SetCurPart( part ),
129  // which calls delete <previous part>.
130  // <previous part> is now put in redo list and is owned by this list.
131  // Just set the current part to the part which come from the undo list
132  m_my_part = part;
133 
134  if( undoRedoType == UR_LIB_RENAME )
135  {
136  wxString lib = GetCurLib();
137  m_libMgr->UpdatePartAfterRename( part, oldPart->GetName(), lib );
138 
139  // Reselect the renamed part
140  m_treePane->GetCmpTree()->SelectLibId( LIB_ID( lib, part->GetName() ) );
141  }
142 
143  if( !m_aliasName.IsEmpty() && !part->HasAlias( m_aliasName ) )
144  m_aliasName = part->GetName();
145 
146  SetDrawItem( NULL );
149  SetShowDeMorgan( part->HasConversion() );
150  DisplayLibInfos();
151  DisplayCmpDoc();
152  OnModify();
153  m_canvas->Refresh();
154 }
virtual void PushCommandToUndoList(PICKED_ITEMS_LIST *aItem)
Function PushCommandToUndoList add a command to undo in undo list delete the very old commands when t...
STATUS_FLAGS GetFlags() const
Definition: base_struct.h:284
bool HasConversion() const
Test if part has more than one body conversion type (DeMorgan).
virtual void Refresh(bool eraseBackground=true, const wxRect *rect=NULL) override
Update the board display after modifying it bu a python script (note: it is automatically called by a...
Definition: draw_panel.cpp:339
UNDO_REDO_T
Undo Redo considerations: Basically we have 3 cases New item Deleted item Modified item there is also...
LIB_MANAGER * m_libMgr
manager taking care of temporary modificatoins
SCH_SCREEN * GetScreen() const override
Function GetScreen returns a pointer to a BASE_SCREEN or one of its derivatives.
bool HasAlias(const wxString &aName) const
Test if alias aName is in part alias list.
void OnModify()
Must be called after a schematic change in order to set the "modify" flag of the current screen...
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
void UpdateAliasSelectList()
LIB_PART * m_my_part
a part I own, it is not in any library, but a copy could be.
UNDO_REDO_T GetStatus() const
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 DisplayLibInfos()
Updates the main window title bar with the current library name and read only status of the library...
Definition: libedit.cpp:58
EDA_ITEM * GetItem() const
LIB_PART * GetCurPart() const
Return the current part being edited or NULL if none selected.
void SetFlags(STATUS_FLAGS aMask)
Definition: base_struct.h:282
void SetDrawItem(LIB_ITEM *drawItem)
Class LIB_ITEM 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.
void GetComponentFromRedoList(wxCommandEvent &event)
wxString GetCurLib() const
The nickname of the current library being edited and empty string if none.
Class PICKED_ITEMS_LIST is a holder to handle information on schematic or board items.
void DisplayCmpDoc()
Display the documentation of the selected component.
Definition: libedit.cpp:655
void SelectLibId(const LIB_ID &aLibId)
Select a part in the tree widget.
EDA_DRAW_PANEL * m_canvas
The area to draw on.
Definition: draw_frame.h:93
void ClearStatus()
Clears the status flag all draw objects in this part.
static wxString m_aliasName
void SaveCopyInUndoList(EDA_ITEM *ItemToCopy, UNDO_REDO_T undoType=UR_LIBEDIT)
Create a copy of the current component, and save it in the undo list.
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:180
ITEM_PICKER PopItem()
Function PopItem.
const wxString & GetName() const
#define UR_TRANSIENT
indicates the item is owned by the undo/redo stack
Definition: base_struct.h:155
Definition of class LIB_EDIT_FRAME.
COMPONENT_TREE * GetCmpTree() const
Definition: cmp_tree_pane.h:46
virtual void ClearUndoORRedoList(UNDO_REDO_CONTAINER &aList, int aItemCount=-1) override
Free the undo or redo list from aList element.
Definition: sch_screen.cpp:569
void GetComponentFromUndoList(wxCommandEvent &event)
void UpdatePartSelectList()
CMP_TREE_PANE * m_treePane
component search tree widget
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. ...