KiCad PCB EDA Suite
pl_editor_undo_redo.cpp
Go to the documentation of this file.
1 
6 /*
7  * This program source code file is part of KiCad, a free EDA CAD application.
8  *
9  * Copyright (C) 2013 CERN
10  * @author Jean-Pierre Charras, jp.charras at wanadoo.fr
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, you may find one here:
24  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
25  * or you may search the http://www.gnu.org website for the version 2 license,
26  * or you may write to the Free Software Foundation, Inc.,
27  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
28  */
29 
30 #include <fctsys.h>
31 #include <class_drawpanel.h>
32 #include <macros.h>
34 
35 #include <pl_editor_frame.h>
36 
37 /* Note: the Undo/redo commands use a "brute" method:
38  * the full page layout is converted to a S expression, and saved as string.
39  * When a previous version is needed, the old string is parsed,
40  * and the description replaces the current desc, just like reading a new file
41  *
42  * This is not optimal from the memory point of view, but:
43  * - the descriptions are never very long (max few thousand of bytes)
44  * - this is very easy to code
45  */
46 
47 // A helper class used in undo/redo commad:
48 class PL_ITEM_LAYOUT: public EDA_ITEM
49 {
50 public:
51  wxString m_Layout;
52 
53 public:
55 
56  // Required to keep compiler happy on debug builds.
57 #if defined(DEBUG)
58  virtual void Show( int nestLevel, std::ostream& os ) const override {}
59 #endif
60 
64  virtual wxString GetClass() const override
65  {
66  return wxT( "PL_ITEM_LAYOUT" );
67  }
68 };
69 
71 {
72  PL_ITEM_LAYOUT* copyItem = new PL_ITEM_LAYOUT;
74  pglayout.SaveInString( copyItem->m_Layout );
75 
76  PICKED_ITEMS_LIST* lastcmd = new PICKED_ITEMS_LIST();
77  ITEM_PICKER wrapper( copyItem, UR_LIBEDIT );
78  lastcmd->PushItem(wrapper);
79  GetScreen()->PushCommandToUndoList( lastcmd );
80 
81  // Clear redo list, because after new save there is no redo to do.
82  GetScreen()->ClearUndoORRedoList( GetScreen()->m_RedoList );
83 }
84 
85 
86 /* Redo the last edition:
87  * - Place the current edited layout in undo list
88  * - Get previous version of the current edited layput
89  */
90 void PL_EDITOR_FRAME::GetLayoutFromRedoList( wxCommandEvent& event )
91 {
92  if ( GetScreen()->GetRedoCommandCount() <= 0 )
93  return;
94 
95  PICKED_ITEMS_LIST* lastcmd = new PICKED_ITEMS_LIST();
96  PL_ITEM_LAYOUT* copyItem = new PL_ITEM_LAYOUT;
98  pglayout.SaveInString( copyItem->m_Layout );
99 
100  ITEM_PICKER wrapper( copyItem, UR_LIBEDIT );
101 
102  lastcmd->PushItem( wrapper );
103  GetScreen()->PushCommandToUndoList( lastcmd );
104 
105  lastcmd = GetScreen()->PopCommandFromRedoList();
106 
107  wrapper = lastcmd->PopItem();
108  copyItem = static_cast<PL_ITEM_LAYOUT*>( wrapper.GetItem() );
109  pglayout.SetPageLayout( TO_UTF8(copyItem->m_Layout) );
110  delete copyItem;
111 
112  OnModify();
114  m_canvas->Refresh();
115 }
116 
117 
118 /* Undo the last edition:
119  * - Place the current layout in Redo list
120  * - Get previous version of the current edited layout
121  */
122 void PL_EDITOR_FRAME::GetLayoutFromUndoList( wxCommandEvent& event )
123 {
124  if ( GetScreen()->GetUndoCommandCount() <= 0 )
125  return;
126 
127  PICKED_ITEMS_LIST* lastcmd = new PICKED_ITEMS_LIST();
128  PL_ITEM_LAYOUT* copyItem = new PL_ITEM_LAYOUT;
130  pglayout.SaveInString( copyItem->m_Layout );
131 
132  ITEM_PICKER wrapper( copyItem, UR_LIBEDIT );
133  lastcmd->PushItem( wrapper );
134  GetScreen()->PushCommandToRedoList( lastcmd );
135 
136  lastcmd = GetScreen()->PopCommandFromUndoList();
137 
138  wrapper = lastcmd->PopItem();
139  copyItem = static_cast<PL_ITEM_LAYOUT*>( wrapper.GetItem() );
140  pglayout.SetPageLayout( TO_UTF8(copyItem->m_Layout) );
141  delete copyItem;
142 
143  OnModify();
145  m_canvas->Refresh();
146 }
147 
148 /* Remove the last command in Undo List.
149  * Used to clean the uUndo stack after a cancel command
150  */
152 {
153  if ( GetScreen()->GetUndoCommandCount() <= 0 )
154  return;
155 
157 
158  ITEM_PICKER wrapper = lastcmd->PopItem();
159  PL_ITEM_LAYOUT* copyItem = static_cast<PL_ITEM_LAYOUT*>( wrapper.GetItem() );
160  delete copyItem;
161 }
virtual void PushCommandToUndoList(PICKED_ITEMS_LIST *aItem)
Function PushCommandToUndoList add a command to undo in undo list delete the very old commands when t...
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:325
void ClearUndoORRedoList(UNDO_REDO_CONTAINER &aList, int aItemCount=-1) override
Function ClearUndoORRedoList virtual pure in BASE_SCREEN, so it must be defined here.
void RemoveLastCommandInUndoList()
Remove the last command in Undo List.
void GetLayoutFromUndoList(wxCommandEvent &event)
Undo the last edition:
virtual wxString GetClass() const override
Get class name.
void GetLayoutFromRedoList(wxCommandEvent &event)
Redo the last edition:
void PushItem(const ITEM_PICKER &aItem)
Function PushItem pushes aItem to the top of the list.
static WORKSHEET_LAYOUT & GetTheInstance()
static function: returns the instance of WORKSHEET_LAYOUT used in the application ...
This file contains miscellaneous commonly used macros and functions.
void OnModify()
Must be called after a change in order to set the "modify" flag of the current screen.
virtual PICKED_ITEMS_LIST * PopCommandFromUndoList()
PopCommandFromUndoList return the last command to undo and remove it from list nothing is deleted...
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes...
Definition: macros.h:47
EDA_ITEM * GetItem() const
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
Function GetScreen returns 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.
WORKSHEET_LAYOUT handles the graphic items list to draw/plot the title block and other items (page re...
void SaveInString(wxString &aOutputString)
Save the description in a buffer.
EDA_DRAW_PANEL * m_canvas
The area to draw on.
Definition: draw_frame.h:92
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:165
ITEM_PICKER PopItem()
Function PopItem.
void SaveCopyInUndoList()
Save a copy of the description (in a S expr string) for Undo/redo commands.
void RebuildDesignTree()
Function RebuildDesignTree Re creates the design graphic tree list items, and ensures each item has a...
classes and function to generate graphics to plt or draw titles blocks and frame references ...
void SetPageLayout(const wxString &aFullFileName=wxEmptyString, bool Append=false)
Populates the list with a custom layout, or the default layout, if no custom layout available...