KiCad PCB EDA Suite
pl_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) 2013 CERN
5  * Copyright (C) 2019 KiCad Developers, see AUTHORS.txt for contributors.
6  * @author Jean-Pierre Charras, jp.charras at wanadoo.fr
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #include <fctsys.h>
27 #include <ws_data_model.h>
28 #include <pl_editor_frame.h>
29 #include <tool/tool_manager.h>
31 #include <ws_proxy_undo_item.h>
32 #include <tool/actions.h>
33 
34 
36 {
37  PICKED_ITEMS_LIST* lastcmd = new PICKED_ITEMS_LIST();
38  WS_PROXY_UNDO_ITEM* copyItem = new WS_PROXY_UNDO_ITEM( this );
39  ITEM_PICKER wrapper( GetScreen(), copyItem, UR_LIBEDIT );
40 
41  lastcmd->PushItem( wrapper );
42  PushCommandToUndoList( lastcmd );
43 
44  // Clear redo list, because after new save there is no redo to do.
46 }
47 
48 
49 /* Redo the last edit:
50  * - Place the current edited layout in undo list
51  * - Get previous version of the current edited layput
52  */
54 {
56 
57  if ( GetRedoCommandCount() <= 0 )
58  return;
59 
60  ITEM_PICKER redoWrapper = PopCommandFromRedoList()->PopItem();
61  WS_PROXY_UNDO_ITEM* redoItem = static_cast<WS_PROXY_UNDO_ITEM*>( redoWrapper.GetItem() );
62  bool pageSettingsAndTitleBlock = redoItem->Type() == WS_PROXY_UNDO_ITEM_PLUS_T;
63 
64  PICKED_ITEMS_LIST* undoCmd = new PICKED_ITEMS_LIST();
65  WS_PROXY_UNDO_ITEM* undoItem = new WS_PROXY_UNDO_ITEM( pageSettingsAndTitleBlock ? this : nullptr );
66  ITEM_PICKER undoWrapper( GetScreen(), undoItem );
67 
68  undoCmd->PushItem( undoWrapper );
69  PushCommandToUndoList( undoCmd );
70 
71  selTool->ClearSelection();
72  redoItem->Restore( this, GetCanvas()->GetView() );
73  selTool->RebuildSelection();
74 
75  delete redoItem;
76 
77  if( pageSettingsAndTitleBlock )
78  HardRedraw(); // items based off of corners will need re-calculating
79  else
80  GetCanvas()->Refresh();
81 
82  OnModify();
83 }
84 
85 
86 /* Undo the last edit:
87  * - Place the current layout in Redo list
88  * - Get previous version of the current edited layout
89  */
91 {
93 
94  if ( GetUndoCommandCount() <= 0 )
95  return;
96 
97  ITEM_PICKER undoWrapper = PopCommandFromUndoList()->PopItem();
98  WS_PROXY_UNDO_ITEM* undoItem = static_cast<WS_PROXY_UNDO_ITEM*>( undoWrapper.GetItem() );
99  bool pageSettingsAndTitleBlock = undoItem->Type() == WS_PROXY_UNDO_ITEM_PLUS_T;
100 
101  PICKED_ITEMS_LIST* redoCmd = new PICKED_ITEMS_LIST();
102  WS_PROXY_UNDO_ITEM* redoItem = new WS_PROXY_UNDO_ITEM( pageSettingsAndTitleBlock ? this : nullptr );
103  ITEM_PICKER redoWrapper( GetScreen(), redoItem );
104 
105  redoCmd->PushItem( redoWrapper );
106  PushCommandToRedoList( redoCmd );
107 
108  selTool->ClearSelection();
109  undoItem->Restore( this, GetCanvas()->GetView() );
110  selTool->RebuildSelection();
111 
112  delete undoItem;
113 
114  if( pageSettingsAndTitleBlock )
115  HardRedraw(); // items based off of corners will need re-calculating
116  else
117  GetCanvas()->Refresh();
118 
119  OnModify();
120 }
121 
122 
123 /* Remove the last command in Undo List.
124  * Used to clean the uUndo stack after a cancel command
125  */
127 {
129 
130  if ( GetUndoCommandCount() <= 0 )
131  return;
132 
133  ITEM_PICKER undoWrapper = PopCommandFromUndoList()->PopItem();
134  WS_PROXY_UNDO_ITEM* undoItem = static_cast<WS_PROXY_UNDO_ITEM*>( undoWrapper.GetItem() );
135  bool pageSettingsAndTitleBlock = undoItem->Type() == WS_PROXY_UNDO_ITEM_PLUS_T;
136 
137  selTool->ClearSelection();
138  undoItem->Restore( this, GetCanvas()->GetView() );
139  selTool->RebuildSelection();
140 
141  delete undoItem;
142 
143  if( pageSettingsAndTitleBlock )
144  {
146  HardRedraw(); // items based off of corners will need re-calculating
147  }
148  else
149  GetCanvas()->Refresh();
150 }
void Restore(EDA_DRAW_FRAME *aFrame, KIGFX::VIEW *aView=nullptr)
void RebuildSelection()
Rebuild the selection from the flags in the view items.
void HardRedraw() override
Refresh the library tree and redraw the window.
PL_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
void ClearUndoORRedoList(UNDO_REDO_LIST whichList, int aItemCount=-1) override
Function ClearUndoORRedoList.
int GetUndoCommandCount() 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...
EDA_ITEM * GetItem() const
int GetRedoCommandCount() const
int ClearSelection(const TOOL_EVENT &aEvent)
static TOOL_ACTION zoomFitScreen
Definition: actions.h:93
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:140
void GetLayoutFromRedoList()
Redo the last edit:
virtual void PushCommandToUndoList(PICKED_ITEMS_LIST *aItem)
Function PushCommandToUndoList add a command to undo in undo list delete the very old commands when t...
void PushItem(const ITEM_PICKER &aItem)
Function PushItem pushes aItem to the top of the list.
virtual PICKED_ITEMS_LIST * PopCommandFromRedoList()
PopCommandFromRedoList return the last command to undo and remove it from list nothing is deleted.
void OnModify()
Must be called after a change in order to set the "modify" flag.
virtual PICKED_ITEMS_LIST * PopCommandFromUndoList()
PopCommandFromUndoList return the last command to undo and remove it from list nothing is deleted.
PICKED_ITEMS_LIST is a holder to handle information on schematic or board items.
virtual 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...
ITEM_PICKER PopItem()
Function PopItem.
void SaveCopyInUndoList()
Save a copy of the description (in a S expr string) for Undo/redo commands.
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:74
void RollbackFromUndo()
Apply the last command in Undo List without stacking a Redo.
virtual BASE_SCREEN * GetScreen() const
Return a pointer to a BASE_SCREEN or one of its derivatives.
void GetLayoutFromUndoList()
Undo the last edit:
KICAD_T Type() const
Function Type()
Definition: base_struct.h:193