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 ITEM_PICKER::ITEM_PICKER( EDA_ITEM* aItem, UNDO_REDO_T aUndoRedoStatus )
32 {
33  m_undoRedoStatus = aUndoRedoStatus;
34  SetItem( aItem );
35  m_pickerFlags = 0;
36  m_link = NULL;
37 }
38 
39 
41 {
43 }
44 
46 {
47 }
48 
49 
51 {
52  m_ItemsList.push_back( aItem );
53 }
54 
55 
57 {
58  ITEM_PICKER item;
59 
60  if( m_ItemsList.size() != 0 )
61  {
62  item = m_ItemsList.back();
63  m_ItemsList.pop_back();
64  }
65 
66  return item;
67 }
68 
69 
70 bool PICKED_ITEMS_LIST::ContainsItem( const EDA_ITEM* aItem ) const
71 {
72  for( size_t i = 0; i < m_ItemsList.size(); i++ )
73  {
74  if( m_ItemsList[ i ].GetItem() == aItem )
75  return true;
76  }
77 
78  return false;
79 }
80 
81 
82 int PICKED_ITEMS_LIST::FindItem( const EDA_ITEM* aItem ) const
83 {
84  for( size_t i = 0; i < m_ItemsList.size(); i++ )
85  {
86  if( m_ItemsList[i].GetItem() == aItem )
87  return i;
88  }
89 
90  return -1;
91 }
92 
93 
95 {
96  m_ItemsList.clear();
97 }
98 
99 
101 {
102  // Delete items is they are not flagged UR_NEW, or if this is a block operation
103  while( GetCount() > 0 )
104  {
105  ITEM_PICKER wrapper = PopItem();
106  if( wrapper.GetItem() == NULL ) // No more item in list.
107  break;
108 
109  // The Link is an undo construct; it is always owned by the undo/redo container
110  if( wrapper.GetLink() )
111  delete wrapper.GetLink();
112 
113  if( wrapper.GetFlags() & UR_TRANSIENT )
114  {
115  delete wrapper.GetItem();
116  }
117  else if( wrapper.GetStatus() == UR_DELETED )
118  {
119  // This should really be replaced with UR_TRANSIENT, but currently many clients
120  // (eeschema in particular) abuse this to achieve non-undo-related deletions.
121  delete wrapper.GetItem();
122  }
123  }
124 }
125 
126 
128 {
129  ITEM_PICKER picker;
130 
131  if( aIdx < m_ItemsList.size() )
132  picker = m_ItemsList[aIdx];
133 
134  return picker;
135 }
136 
137 
138 EDA_ITEM* PICKED_ITEMS_LIST::GetPickedItem( unsigned int aIdx ) const
139 {
140  if( aIdx < m_ItemsList.size() )
141  return m_ItemsList[aIdx].GetItem();
142 
143  return NULL;
144 }
145 
146 
148 {
149  if( aIdx < m_ItemsList.size() )
150  return m_ItemsList[aIdx].GetLink();
151 
152  return NULL;
153 }
154 
155 
157 {
158  if( aIdx < m_ItemsList.size() )
159  return m_ItemsList[aIdx].GetStatus();
160 
161  return UR_UNSPECIFIED;
162 }
163 
164 
166 {
167  if( aIdx < m_ItemsList.size() )
168  return m_ItemsList[aIdx].GetFlags();
169 
170  return 0;
171 }
172 
173 
174 bool PICKED_ITEMS_LIST::SetPickedItem( EDA_ITEM* aItem, unsigned aIdx )
175 {
176  if( aIdx < m_ItemsList.size() )
177  {
178  m_ItemsList[aIdx].SetItem( aItem );
179  return true;
180  }
181 
182  return false;
183 }
184 
185 
186 bool PICKED_ITEMS_LIST::SetPickedItemLink( EDA_ITEM* aLink, unsigned aIdx )
187 {
188  if( aIdx < m_ItemsList.size() )
189  {
190  m_ItemsList[aIdx].SetLink( aLink );
191  return true;
192  }
193 
194  return false;
195 }
196 
197 
198 bool PICKED_ITEMS_LIST::SetPickedItem( EDA_ITEM* aItem, UNDO_REDO_T aStatus, unsigned aIdx )
199 {
200  if( aIdx < m_ItemsList.size() )
201  {
202  m_ItemsList[aIdx].SetItem( aItem );
203  m_ItemsList[aIdx].SetStatus( aStatus );
204  return true;
205  }
206 
207  return false;
208 }
209 
210 
212 {
213  if( aIdx < m_ItemsList.size() )
214  {
215  m_ItemsList[aIdx].SetStatus( aStatus );
216  return true;
217  }
218 
219  return false;
220 }
221 
222 
223 bool PICKED_ITEMS_LIST::SetPickerFlags( STATUS_FLAGS aFlags, unsigned aIdx )
224 {
225  if( aIdx < m_ItemsList.size() )
226  {
227  m_ItemsList[aIdx].SetFlags( aFlags );
228  return true;
229  }
230 
231  return false;
232 }
233 
234 
235 bool PICKED_ITEMS_LIST::RemovePicker( unsigned aIdx )
236 {
237  if( aIdx >= m_ItemsList.size() )
238  return false;
239 
240  m_ItemsList.erase( m_ItemsList.begin() + aIdx );
241  return true;
242 }
243 
244 
246 {
247  m_ItemsList = aSource.m_ItemsList; // Vector's copy
248 }
249 
250 
252 {
253  std::vector <ITEM_PICKER> tmp;
254  while( !m_ItemsList.empty() )
255  {
256  tmp.push_back( m_ItemsList.back() );
257  m_ItemsList.pop_back();
258  }
259 
260  m_ItemsList.swap( tmp );
261 }
262 
263 
264 /**********************************************/
265 /********** UNDO_REDO_CONTAINER ***************/
266 /**********************************************/
267 
269 {
270 }
271 
272 
274 {
276 }
277 
278 
280 {
281  for( unsigned ii = 0; ii < m_CommandsList.size(); ii++ )
282  delete m_CommandsList[ii];
283 
284  m_CommandsList.clear();
285 }
286 
287 
289 {
290  m_CommandsList.push_back( aItem );
291 }
292 
293 
295 {
296  if( m_CommandsList.size() != 0 )
297  {
298  PICKED_ITEMS_LIST* item = m_CommandsList.back();
299  m_CommandsList.pop_back();
300  return item;
301  }
302 
303  return NULL;
304 }
EDA_ITEM * GetPickedItemLink(unsigned int aIdx) const
Function GetPickedItemLink.
std::vector< ITEM_PICKER > m_ItemsList
int FindItem(const EDA_ITEM *aItem) const
Function FindItem.
ITEM_PICKER(EDA_ITEM *aItem=NULL, UNDO_REDO_T aUndoRedoStatus=UR_UNSPECIFIED)
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
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.
std::vector< PICKED_ITEMS_LIST * > m_CommandsList
bool SetPickedItem(EDA_ITEM *aItem, unsigned aIdx)
Function SetPickedItem.
EDA_ITEM * GetPickedItem(unsigned int aIdx) const
Function GetPickedItem.
STATUS_FLAGS GetFlags() const
unsigned STATUS_FLAGS
Definition: base_struct.h:156
bool RemovePicker(unsigned aIdx)
Function RemovePicker removes one entry (one picker) from the list of picked items.
Class 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.
size_t i
Definition: json11.cpp:649
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:163
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:147
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...