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@gmail.com>
6  * Copyright (C) 2021 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 
31 {
33  SetItem( nullptr );
34  m_pickerFlags = 0;
35  m_link = nullptr;
36  m_screen = nullptr;
37 }
38 
39 
40 ITEM_PICKER::ITEM_PICKER( BASE_SCREEN* aScreen, EDA_ITEM* aItem, UNDO_REDO aUndoRedoStatus )
41 {
42  m_undoRedoStatus = aUndoRedoStatus;
43  SetItem( aItem );
44  m_pickerFlags = 0;
45  m_link = nullptr;
46  m_screen = aScreen;
47 }
48 
49 
51 {
52  m_pickedItem = aItem;
53  m_pickedItemType = aItem ? aItem->Type() : TYPE_NOT_INIT;
54 }
55 
56 
58 {
59 }
60 
61 
63 {
64 }
65 
66 
68 {
69  m_ItemsList.push_back( aItem );
70 }
71 
72 
74 {
75  ITEM_PICKER item;
76 
77  if( m_ItemsList.size() != 0 )
78  {
79  item = m_ItemsList.back();
80  m_ItemsList.pop_back();
81  }
82 
83  return item;
84 }
85 
86 
87 bool PICKED_ITEMS_LIST::ContainsItem( const EDA_ITEM* aItem ) const
88 {
89  for( const ITEM_PICKER& picker : m_ItemsList )
90  {
91  if( picker.GetItem() == aItem )
92  return true;
93  }
94 
95  return false;
96 }
97 
98 
99 int PICKED_ITEMS_LIST::FindItem( const EDA_ITEM* aItem ) const
100 {
101  for( size_t i = 0; i < m_ItemsList.size(); i++ )
102  {
103  if( m_ItemsList[i].GetItem() == aItem )
104  return i;
105  }
106 
107  return -1;
108 }
109 
110 
112 {
113  m_ItemsList.clear();
114 }
115 
116 
118 {
119  // Delete items is they are not flagged NEWITEM, or if this is a block operation
120  while( GetCount() > 0 )
121  {
122  ITEM_PICKER wrapper = PopItem();
123 
124  if( wrapper.GetItem() == nullptr ) // No more items in list.
125  break;
126 
127  // The Link is an undo construct; it is always owned by the undo/redo container
128  if( wrapper.GetLink() )
129  delete wrapper.GetLink();
130 
131  if( wrapper.GetFlags() & UR_TRANSIENT )
132  {
133  delete wrapper.GetItem();
134  }
135  else if( wrapper.GetStatus() == UNDO_REDO::DELETED )
136  {
137  // This should really be replaced with UR_TRANSIENT, but currently many clients
138  // (eeschema in particular) abuse this to achieve non-undo-related deletions.
139  delete wrapper.GetItem();
140  }
141  }
142 }
143 
144 
146 {
147  ITEM_PICKER picker;
148 
149  if( aIdx < m_ItemsList.size() )
150  picker = m_ItemsList[aIdx];
151 
152  return picker;
153 }
154 
155 
156 EDA_ITEM* PICKED_ITEMS_LIST::GetPickedItem( unsigned int aIdx ) const
157 {
158  if( aIdx < m_ItemsList.size() )
159  return m_ItemsList[aIdx].GetItem();
160 
161  return nullptr;
162 }
163 
164 
166 {
167  if( aIdx < m_ItemsList.size() )
168  return m_ItemsList[aIdx].GetScreen();
169 
170  return nullptr;
171 }
172 
173 
175 {
176  if( aIdx < m_ItemsList.size() )
177  return m_ItemsList[aIdx].GetLink();
178 
179  return nullptr;
180 }
181 
182 
184 {
185  if( aIdx < m_ItemsList.size() )
186  return m_ItemsList[aIdx].GetStatus();
187 
188  return UNDO_REDO::UNSPECIFIED;
189 }
190 
191 
193 {
194  if( aIdx < m_ItemsList.size() )
195  return m_ItemsList[aIdx].GetFlags();
196 
197  return 0;
198 }
199 
200 
201 bool PICKED_ITEMS_LIST::SetPickedItem( EDA_ITEM* aItem, unsigned aIdx )
202 {
203  if( aIdx < m_ItemsList.size() )
204  {
205  m_ItemsList[aIdx].SetItem( aItem );
206  return true;
207  }
208 
209  return false;
210 }
211 
212 
213 bool PICKED_ITEMS_LIST::SetPickedItemLink( EDA_ITEM* aLink, unsigned aIdx )
214 {
215  if( aIdx < m_ItemsList.size() )
216  {
217  m_ItemsList[aIdx].SetLink( aLink );
218  return true;
219  }
220 
221  return false;
222 }
223 
224 
225 bool PICKED_ITEMS_LIST::SetPickedItem( EDA_ITEM* aItem, UNDO_REDO aStatus, unsigned aIdx )
226 {
227  if( aIdx < m_ItemsList.size() )
228  {
229  m_ItemsList[aIdx].SetItem( aItem );
230  m_ItemsList[aIdx].SetStatus( aStatus );
231  return true;
232  }
233 
234  return false;
235 }
236 
237 
238 bool PICKED_ITEMS_LIST::SetPickedItemStatus( UNDO_REDO aStatus, unsigned aIdx )
239 {
240  if( aIdx < m_ItemsList.size() )
241  {
242  m_ItemsList[aIdx].SetStatus( aStatus );
243  return true;
244  }
245 
246  return false;
247 }
248 
249 
251 {
252  if( aIdx < m_ItemsList.size() )
253  {
254  m_ItemsList[aIdx].SetFlags( aFlags );
255  return true;
256  }
257 
258  return false;
259 }
260 
261 
262 bool PICKED_ITEMS_LIST::RemovePicker( unsigned aIdx )
263 {
264  if( aIdx >= m_ItemsList.size() )
265  return false;
266 
267  m_ItemsList.erase( m_ItemsList.begin() + aIdx );
268  return true;
269 }
270 
271 
273 {
274  m_ItemsList = aSource.m_ItemsList; // Vector's copy
275 }
276 
277 
279 {
280  std::vector <ITEM_PICKER> tmp;
281 
282  while( !m_ItemsList.empty() )
283  {
284  tmp.push_back( m_ItemsList.back() );
285  m_ItemsList.pop_back();
286  }
287 
288  m_ItemsList.swap( tmp );
289 }
290 
291 
293 {
294 }
295 
296 
298 {
300 }
301 
302 
304 {
305  for( unsigned ii = 0; ii < m_CommandsList.size(); ii++ )
306  delete m_CommandsList[ii];
307 
308  m_CommandsList.clear();
309 }
310 
311 
313 {
314  m_CommandsList.push_back( aItem );
315 }
316 
317 
319 {
320  if( m_CommandsList.size() != 0 )
321  {
322  PICKED_ITEMS_LIST* item = m_CommandsList.back();
323  m_CommandsList.pop_back();
324  return item;
325  }
326 
327  return nullptr;
328 }
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.
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