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 <macros.h>
28 #include <ws_data_model.h>
29 #include <ws_draw_item.h>
30 
31 #include <pl_editor_frame.h>
32 #include <tool/tool_manager.h>
34 #include <ws_proxy_undo_item.h>
35 #include <tool/actions.h>
36 
37 void PL_EDITOR_FRAME::SaveCopyInUndoList( bool aSavePageSettingsAndTitleBlock )
38 {
39  PICKED_ITEMS_LIST* lastcmd = new PICKED_ITEMS_LIST();
40  WS_PROXY_UNDO_ITEM* copyItem = new WS_PROXY_UNDO_ITEM( this );
41  ITEM_PICKER wrapper( copyItem, UR_LIBEDIT );
42 
43  lastcmd->PushItem( wrapper );
44  GetScreen()->PushCommandToUndoList( lastcmd );
45 
46  // Clear redo list, because after new save there is no redo to do.
47  GetScreen()->ClearUndoORRedoList( GetScreen()->m_RedoList );
48 }
49 
50 
51 /* Redo the last edit:
52  * - Place the current edited layout in undo list
53  * - Get previous version of the current edited layput
54  */
56 {
58 
59  if ( GetScreen()->GetRedoCommandCount() <= 0 )
60  return;
61 
63  WS_PROXY_UNDO_ITEM* redoItem = static_cast<WS_PROXY_UNDO_ITEM*>( redoWrapper.GetItem() );
64  bool pageSettingsAndTitleBlock = redoItem->Type() == WS_PROXY_UNDO_ITEM_PLUS_T;
65 
66  PICKED_ITEMS_LIST* undoCmd = new PICKED_ITEMS_LIST();
67 
68  undoCmd->PushItem( new WS_PROXY_UNDO_ITEM( pageSettingsAndTitleBlock ? this : nullptr ) );
69  GetScreen()->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 ( GetScreen()->GetUndoCommandCount() <= 0 )
95  return;
96 
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 
103  redoCmd->PushItem( new WS_PROXY_UNDO_ITEM( pageSettingsAndTitleBlock ? this : nullptr ) );
104  GetScreen()->PushCommandToRedoList( redoCmd );
105 
106  selTool->ClearSelection();
107  undoItem->Restore( this, GetCanvas()->GetView() );
108  selTool->RebuildSelection();
109 
110  delete undoItem;
111 
112  if( pageSettingsAndTitleBlock )
113  HardRedraw(); // items based off of corners will need re-calculating
114  else
115  GetCanvas()->Refresh();
116 
117  OnModify();
118 }
119 
120 
121 /* Remove the last command in Undo List.
122  * Used to clean the uUndo stack after a cancel command
123  */
125 {
127 
128  if ( GetScreen()->GetUndoCommandCount() <= 0 )
129  return;
130 
131  ITEM_PICKER undoWrapper = GetScreen()->PopCommandFromUndoList()->PopItem();
132  WS_PROXY_UNDO_ITEM* undoItem = static_cast<WS_PROXY_UNDO_ITEM*>( undoWrapper.GetItem() );
133  bool pageSettingsAndTitleBlock = undoItem->Type() == WS_PROXY_UNDO_ITEM_PLUS_T;
134 
135  selTool->ClearSelection();
136  undoItem->Restore( this, GetCanvas()->GetView() );
137  selTool->RebuildSelection();
138 
139  delete undoItem;
140 
141  if( pageSettingsAndTitleBlock )
142  {
144  HardRedraw(); // items based off of corners will need re-calculating
145  }
146  else
147  GetCanvas()->Refresh();
148 }
void Restore(EDA_DRAW_FRAME *aFrame, KIGFX::VIEW *aView=nullptr)
void SaveCopyInUndoList(bool aSavePageSettingsAndTitleBlock=false)
Save a copy of the description (in a S expr string) for Undo/redo commands.
virtual void PushCommandToUndoList(PICKED_ITEMS_LIST *aItem)
Function PushCommandToUndoList add a command to undo in undo list delete the very old commands when t...
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
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_CONTAINER &aList, int aItemCount=-1) override
Function ClearUndoORRedoList virtual pure in BASE_SCREEN, so it must be defined here.
int GetUndoCommandCount() const
Definition: base_screen.h:201
EDA_ITEM * GetItem() const
int ClearSelection(const TOOL_EVENT &aEvent)
static TOOL_ACTION zoomFitScreen
Definition: actions.h:92
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:109
void GetLayoutFromRedoList()
Redo the last edit:
void PushItem(const ITEM_PICKER &aItem)
Function PushItem pushes aItem to the top of the list.
This file contains miscellaneous commonly used macros and functions.
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.
virtual PICKED_ITEMS_LIST * PopCommandFromRedoList()
PopCommandFromRedoList return the last command to undo and remove it from list nothing is deleted.
PL_EDITOR_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
Class 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...
virtual void PushCommandToRedoList(PICKED_ITEMS_LIST *aItem)
Function PushCommandToRedoList add a command to redo in redo list delete the very old commands when t...
ITEM_PICKER PopItem()
Function PopItem.
void RollbackFromUndo()
Apply the last command in Undo List without stacking a Redo.
int GetRedoCommandCount() const
Definition: base_screen.h:206
void GetLayoutFromUndoList()
Undo the last edit:
KICAD_T Type() const
Function Type()
Definition: base_struct.h:210