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 
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->GetLibTree()->SelectLibId( LIB_ID( lib, part->GetName() ) );
90  }
91 
92  SetDrawItem( NULL );
94  SetShowDeMorgan( part->HasConversion() );
95  updateTitle();
96  DisplayCmpDoc();
97 
98  RebuildView();
99  GetCanvas()->Refresh();
100  OnModify();
101 }
102 
103 
104 void LIB_EDIT_FRAME::GetComponentFromUndoList( wxCommandEvent& event )
105 {
106  if( GetScreen()->GetUndoCommandCount() <= 0 )
107  return;
108 
109  // Load the last undo entry
111  ITEM_PICKER undoWrapper = undoCommand->PopItem();
112  delete undoCommand;
113  LIB_PART* part = (LIB_PART*) undoWrapper.GetItem();
114  wxCHECK( part, /* void */ );
115  part->SetFlags( part->GetFlags() & ~UR_TRANSIENT );
116  UNDO_REDO_T undoRedoType = undoWrapper.GetStatus();
117 
118  // Store the current part in the redo buffer
119  PICKED_ITEMS_LIST* redoCommand = new PICKED_ITEMS_LIST();
120  LIB_PART* oldPart = GetCurPart();
121  oldPart->SetFlags( oldPart->GetFlags() | UR_TRANSIENT );
122  ITEM_PICKER redoWrapper( oldPart, undoRedoType );
123  redoCommand->PushItem( redoWrapper );
124  GetScreen()->PushCommandToRedoList( redoCommand );
125 
126  // Do not delete the previous part by calling SetCurPart( part ),
127  // which calls delete <previous part>.
128  // <previous part> is now put in redo list and is owned by this list.
129  // Just set the current part to the part which come from the undo list
130  m_my_part = part;
131 
132  if( undoRedoType == UR_LIB_RENAME )
133  {
134  wxString lib = GetCurLib();
135  m_libMgr->UpdatePartAfterRename( part, oldPart->GetName(), lib );
136 
137  // Reselect the renamed part
138  m_treePane->GetLibTree()->SelectLibId( LIB_ID( lib, part->GetName() ) );
139  }
140 
141  SetDrawItem( NULL );
143  SetShowDeMorgan( part->HasConversion() );
144  updateTitle();
145  DisplayCmpDoc();
146 
147  RebuildView();
148  GetCanvas()->Refresh();
149  OnModify();
150 }
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:258
bool HasConversion() const
Test if part has more than one body conversion type (DeMorgan).
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
Return a pointer to a BASE_SCREEN or one of its derivatives.
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.
void Refresh(bool aEraseBackground=true, const wxRect *aRect=NULL) override
Update the board display after modifying it by a python script (note: it is automatically called by a...
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
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...
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:256
void SetDrawItem(LIB_ITEM *drawItem)
Class LIB_ITEM definition.
SCH_DRAW_PANEL * GetCanvas() const override
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:777
void updateTitle()
Updates the main window title bar with the current library name and read only status of the library...
Definition: libedit.cpp:58
void ClearStatus()
Clears the status flag all draw objects in this part.
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.
LIB_TREE * GetLibTree() const
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:154
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:142
Definition of class LIB_EDIT_FRAME.
virtual void ClearUndoORRedoList(UNDO_REDO_CONTAINER &aList, int aItemCount=-1) override
Free the undo or redo list from aList element.
Definition: sch_screen.cpp:546
SYMBOL_TREE_PANE * m_treePane
component search tree widget
void GetComponentFromUndoList(wxCommandEvent &event)
void UpdatePartSelectList()
void SelectLibId(const LIB_ID &aLibId)
Select an item in the tree widget.
Definition: lib_tree.cpp:162
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. ...