KiCad PCB EDA Suite
undo_redo_container.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) 2018 jp.charras at wanadoo.fr
5  * Copyright (C) 2011 Wayne Stambaugh <stambaughw@verizon.net>
6  * Copyright (C) 2018 KiCad Developers, see AUTHORS.txt for contributors.
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 <base_struct.h>
28 #include <undo_redo_container.h>
29 
30 
31 /*
32 ITEM_PICKER::ITEM_PICKER( EDA_ITEM* aItem, UNDO_REDO_T aUndoRedoStatus )
33 {
34  m_undoRedoStatus = aUndoRedoStatus;
35  SetItem( aItem );
36  m_pickerFlags = 0;
37  m_link = nullptr;
38  m_screen = nullptr;
39 }
40 */
41 
43 {
45  SetItem( nullptr );
46  m_pickerFlags = 0;
47  m_link = NULL;
48  m_screen = nullptr;
49 }
50 
51 
52 ITEM_PICKER::ITEM_PICKER( BASE_SCREEN* aScreen, EDA_ITEM* aItem, UNDO_REDO_T aUndoRedoStatus )
53 {
54  m_undoRedoStatus = aUndoRedoStatus;
55  SetItem( aItem );
56  m_pickerFlags = 0;
57  m_link = NULL;
58  m_screen = aScreen;
59 }
60 
61 
63 {
65 }
66 
68 {
69 }
70 
71 
73 {
74  m_ItemsList.push_back( aItem );
75 }
76 
77 
79 {
80  ITEM_PICKER item;
81 
82  if( m_ItemsList.size() != 0 )
83  {
84  item = m_ItemsList.back();
85  m_ItemsList.pop_back();
86  }
87 
88  return item;
89 }
90 
91 
92 bool PICKED_ITEMS_LIST::ContainsItem( const EDA_ITEM* aItem ) const
93 {
94  for( size_t i = 0; i < m_ItemsList.size(); i++ )
95  {
96  if( m_ItemsList[ i ].GetItem() == aItem )
97  return true;
98  }
99 
100  return false;
101 }
102 
103 
104 int PICKED_ITEMS_LIST::FindItem( const EDA_ITEM* aItem ) const
105 {
106  for( size_t i = 0; i < m_ItemsList.size(); i++ )
107  {
108  if( m_ItemsList[i].GetItem() == aItem )
109  return i;
110  }
111 
112  return -1;
113 }
114 
115 
117 {
118  m_ItemsList.clear();
119 }
120 
121 
123 {
124  // Delete items is they are not flagged UR_NEW, or if this is a block operation
125  while( GetCount() > 0 )
126  {
127  ITEM_PICKER wrapper = PopItem();
128  if( wrapper.GetItem() == NULL ) // No more item in list.
129  break;
130 
131  // The Link is an undo construct; it is always owned by the undo/redo container
132  if( wrapper.GetLink() )
133  delete wrapper.GetLink();
134 
135  if( wrapper.GetFlags() & UR_TRANSIENT )
136  {
137  delete wrapper.GetItem();
138  }
139  else if( wrapper.GetStatus() == UR_DELETED )
140  {
141  // This should really be replaced with UR_TRANSIENT, but currently many clients
142  // (eeschema in particular) abuse this to achieve non-undo-related deletions.
143  delete wrapper.GetItem();
144  }
145  }
146 }
147 
148 
150 {
151  ITEM_PICKER picker;
152 
153  if( aIdx < m_ItemsList.size() )
154  picker = m_ItemsList[aIdx];
155 
156  return picker;
157 }
158 
159 
160 EDA_ITEM* PICKED_ITEMS_LIST::GetPickedItem( unsigned int aIdx ) const
161 {
162  if( aIdx < m_ItemsList.size() )
163  return m_ItemsList[aIdx].GetItem();
164 
165  return NULL;
166 }
167 
168 
170 {
171  if( aIdx < m_ItemsList.size() )
172  return m_ItemsList[aIdx].GetScreen();
173 
174  return NULL;
175 }
176 
177 
179 {
180  if( aIdx < m_ItemsList.size() )
181  return m_ItemsList[aIdx].GetLink();
182 
183  return NULL;
184 }
185 
186 
188 {
189  if( aIdx < m_ItemsList.size() )
190  return m_ItemsList[aIdx].GetStatus();
191 
192  return UR_UNSPECIFIED;
193 }
194 
195 
197 {
198  if( aIdx < m_ItemsList.size() )
199  return m_ItemsList[aIdx].GetFlags();
200 
201  return 0;
202 }
203 
204 
205 bool PICKED_ITEMS_LIST::SetPickedItem( EDA_ITEM* aItem, unsigned aIdx )
206 {
207  if( aIdx < m_ItemsList.size() )
208  {
209  m_ItemsList[aIdx].SetItem( aItem );
210  return true;
211  }
212 
213  return false;
214 }
215 
216 
217 bool PICKED_ITEMS_LIST::SetPickedItemLink( EDA_ITEM* aLink, unsigned aIdx )
218 {
219  if( aIdx < m_ItemsList.size() )
220  {
221  m_ItemsList[aIdx].SetLink( aLink );
222  return true;
223  }
224 
225  return false;
226 }
227 
228 
229 bool PICKED_ITEMS_LIST::SetPickedItem( EDA_ITEM* aItem, UNDO_REDO_T aStatus, unsigned aIdx )
230 {
231  if( aIdx < m_ItemsList.size() )
232  {
233  m_ItemsList[aIdx].SetItem( aItem );
234  m_ItemsList[aIdx].SetStatus( aStatus );
235  return true;
236  }
237 
238  return false;
239 }
240 
241 
243 {
244  if( aIdx < m_ItemsList.size() )
245  {
246  m_ItemsList[aIdx].SetStatus( aStatus );
247  return true;
248  }
249 
250  return false;
251 }
252 
253 
254 bool PICKED_ITEMS_LIST::SetPickerFlags( STATUS_FLAGS aFlags, unsigned aIdx )
255 {
256  if( aIdx < m_ItemsList.size() )
257  {
258  m_ItemsList[aIdx].SetFlags( aFlags );
259  return true;
260  }
261 
262  return false;
263 }
264 
265 
266 bool PICKED_ITEMS_LIST::RemovePicker( unsigned aIdx )
267 {
268  if( aIdx >= m_ItemsList.size() )
269  return false;
270 
271  m_ItemsList.erase( m_ItemsList.begin() + aIdx );
272  return true;
273 }
274 
275 
277 {
278  m_ItemsList = aSource.m_ItemsList; // Vector's copy
279 }
280 
281 
283 {
284  std::vector <ITEM_PICKER> tmp;
285  while( !m_ItemsList.empty() )
286  {
287  tmp.push_back( m_ItemsList.back() );
288  m_ItemsList.pop_back();
289  }
290 
291  m_ItemsList.swap( tmp );
292 }
293 
294 
295 /**********************************************/
296 /********** UNDO_REDO_CONTAINER ***************/
297 /**********************************************/
298 
300 {
301 }
302 
303 
305 {
307 }
308 
309 
311 {
312  for( unsigned ii = 0; ii < m_CommandsList.size(); ii++ )
313  delete m_CommandsList[ii];
314 
315  m_CommandsList.clear();
316 }
317 
318 
320 {
321  m_CommandsList.push_back( aItem );
322 }
323 
324 
326 {
327  if( m_CommandsList.size() != 0 )
328  {
329  PICKED_ITEMS_LIST* item = m_CommandsList.back();
330  m_CommandsList.pop_back();
331  return item;
332  }
333 
334  return NULL;
335 }
EDA_ITEM * GetPickedItemLink(unsigned int aIdx) const
Function GetPickedItemLink.
std::vector< ITEM_PICKER > m_ItemsList
int FindItem(const EDA_ITEM *aItem) const
Function FindItem.
void ReversePickersListOrder()
Function ReversePickersListOrder reverses the order of pickers stored in this list.
UNDO_REDO_T GetStatus() const
UNDO_REDO_T
Undo Redo considerations: Basically we have 3 cases New item Deleted item Modified item there is also...
EDA_ITEM * GetItem() const
BASE_SCREEN * GetScreenForItem(unsigned int aIdx) const
Function GetScreenForItem.
STATUS_FLAGS GetPickerFlags(unsigned aIdx) const
Function GetPickerFlags returns the value of the picker flag.
void PushItem(const ITEM_PICKER &aItem)
Function PushItem pushes aItem to the top of the list.
PICKED_ITEMS_LIST * PopCommand()
unsigned GetCount() const
Function GetCount.
EDA_ITEM * GetLink() const
void PushCommand(PICKED_ITEMS_LIST *aCommand)
EDA_ITEM * m_link
void CopyList(const PICKED_ITEMS_LIST &aSource)
Function CopyList copies all data from aSource to the list.
#define NULL
std::vector< PICKED_ITEMS_LIST * > m_CommandsList
bool SetPickedItem(EDA_ITEM *aItem, unsigned aIdx)
Function SetPickedItem.
BASE_SCREEN handles how to draw a screen (a board, a schematic ...)
Definition: base_screen.h:43
EDA_ITEM * GetPickedItem(unsigned int aIdx) const
Function GetPickedItem.
STATUS_FLAGS GetFlags() const
unsigned STATUS_FLAGS
Definition: base_struct.h:152
bool RemovePicker(unsigned aIdx)
Function RemovePicker removes one entry (one picker) from the list of picked items.
PICKED_ITEMS_LIST is a holder to handle information on schematic or board items.
bool SetPickerFlags(STATUS_FLAGS aFlags, unsigned aIdx)
Function SetPickerFlags set the flags of the picker (usually to the picked item m_Flags value)
void SetItem(EDA_ITEM *aItem)
STATUS_FLAGS m_pickerFlags
ITEM_PICKER GetItemWrapper(unsigned int aIdx) const
Function GetItemWrapper.
bool SetPickedItemStatus(UNDO_REDO_T aStatus, unsigned aIdx)
Function SetPickedItemStatus sets the type of undo/redo operation for a given picked item.
UNDO_REDO_T GetPickedItemStatus(unsigned int aIdx) const
Function GetPickedItemStatus.
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: base_struct.h:159
ITEM_PICKER PopItem()
Function PopItem.
void ClearItemsList()
Function ClearItemsList deletes only the list of pickers, NOT the picked data itself.
bool SetPickedItemLink(EDA_ITEM *aLink, unsigned aIdx)
Function SetPickedItemLink set the link associated to a given picked item.
#define UR_TRANSIENT
indicates the item is owned by the undo/redo stack
Definition: base_struct.h:143
BASE_SCREEN * m_screen
UNDO_REDO_T m_undoRedoStatus
bool ContainsItem(const EDA_ITEM *aItem) const
Function IsItemInList.
void ClearListAndDeleteItems()
Function ClearListAndDeleteItems deletes the list of pickers, AND the data pointed by m_PickedItem or...