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