KiCad PCB EDA Suite
class_undoredo_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) 2009 jean-pierre.charras@gipsa-lab.inpg.fr
5  * Copyright (C) 2011 Wayne Stambaugh <stambaughw@verizon.net>
6  * Copyright (C) 2009 KiCad Developers, see change_log.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 <common.h>
28 #include <base_struct.h>
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 = NULL;
38 }
39 
40 
42 {
44 }
45 
47 {
48 }
49 
50 
52 {
53  m_ItemsList.push_back( aItem );
54 }
55 
56 
58 {
59  ITEM_PICKER item;
60 
61  if( m_ItemsList.size() != 0 )
62  {
63  item = m_ItemsList.back();
64  m_ItemsList.pop_back();
65  }
66 
67  return item;
68 }
69 
70 
71 bool PICKED_ITEMS_LIST::ContainsItem( const EDA_ITEM* aItem ) const
72 {
73  for( size_t i = 0; i < m_ItemsList.size(); i++ )
74  {
75  if( m_ItemsList[ i ].GetItem() == aItem )
76  return true;
77  }
78 
79  return false;
80 }
81 
82 
83 int PICKED_ITEMS_LIST::FindItem( const EDA_ITEM* aItem ) const
84 {
85  for( size_t i = 0; i < m_ItemsList.size(); i++ )
86  {
87  if( m_ItemsList[i].GetItem() == aItem )
88  return i;
89  }
90 
91  return -1;
92 }
93 
94 
96 {
97  m_ItemsList.clear();
98 }
99 
100 
102 {
103  bool show_error_message = true;
104 
105  // Delete items is they are not flagged UR_NEW, or if this is a block operation
106  while( GetCount() > 0 )
107  {
108  ITEM_PICKER wrapper = PopItem();
109  if( wrapper.GetItem() == NULL ) // No more item in list.
110  break;
111  switch( wrapper.GetStatus() )
112  {
113  case UR_UNSPECIFIED:
114  if( show_error_message )
115  wxMessageBox( wxT( "ClearListAndDeleteItems() error: UR_UNSPECIFIED command type" ) );
116 
117  show_error_message = false;
118  break;
119 
120  case UR_WIRE_IMAGE:
121  {
122  // Specific to eeschema: a linked list of wires is stored. The wrapper picks only
123  // the first item (head of list), and is owner of all picked items.
124  EDA_ITEM* item = wrapper.GetItem();
125 
126  while( item )
127  {
128  // Delete old copy of wires
129  EDA_ITEM* nextitem = item->Next();
130  delete item;
131  item = nextitem;
132  }
133  }
134  break;
135 
136  case UR_MOVED:
137  case UR_FLIPPED:
138  case UR_MIRRORED_X:
139  case UR_MIRRORED_Y:
140  case UR_ROTATED:
142  case UR_NEW: // Do nothing, items are in use, the picker is not owner of items
143  break;
144 
145  case UR_CHANGED:
146  case UR_EXCHANGE_T:
147  delete wrapper.GetLink(); // the picker is owner of this item
148  break;
149 
150  case UR_DELETED: // the picker is owner of this item
151  case UR_LIBEDIT: /* Libedit save always a copy of the current item
152  * So, the picker is always owner of the picked item
153  */
154  delete wrapper.GetItem();
155  break;
156 
157  default:
158  wxFAIL_MSG( wxString::Format( wxT( "Cannot clear unknown undo/redo command %d" ),
159  wrapper.GetStatus() ) );
160  break;
161  }
162  }
163 }
164 
165 
167 {
168  ITEM_PICKER picker;
169 
170  if( aIdx < m_ItemsList.size() )
171  picker = m_ItemsList[aIdx];
172 
173  return picker;
174 }
175 
176 
177 EDA_ITEM* PICKED_ITEMS_LIST::GetPickedItem( unsigned int aIdx ) const
178 {
179  if( aIdx < m_ItemsList.size() )
180  return m_ItemsList[aIdx].GetItem();
181 
182  return NULL;
183 }
184 
185 
187 {
188  if( aIdx < m_ItemsList.size() )
189  return m_ItemsList[aIdx].GetLink();
190 
191  return NULL;
192 }
193 
194 
196 {
197  if( aIdx < m_ItemsList.size() )
198  return m_ItemsList[aIdx].GetStatus();
199 
200  return UR_UNSPECIFIED;
201 }
202 
203 
205 {
206  if( aIdx < m_ItemsList.size() )
207  return m_ItemsList[aIdx].GetFlags();
208 
209  return 0;
210 }
211 
212 
213 bool PICKED_ITEMS_LIST::SetPickedItem( EDA_ITEM* aItem, unsigned aIdx )
214 {
215  if( aIdx < m_ItemsList.size() )
216  {
217  m_ItemsList[aIdx].SetItem( aItem );
218  return true;
219  }
220 
221  return false;
222 }
223 
224 
225 bool PICKED_ITEMS_LIST::SetPickedItemLink( EDA_ITEM* aLink, unsigned aIdx )
226 {
227  if( aIdx < m_ItemsList.size() )
228  {
229  m_ItemsList[aIdx].SetLink( aLink );
230  return true;
231  }
232 
233  return false;
234 }
235 
236 
237 bool PICKED_ITEMS_LIST::SetPickedItem( EDA_ITEM* aItem, UNDO_REDO_T aStatus, unsigned aIdx )
238 {
239  if( aIdx < m_ItemsList.size() )
240  {
241  m_ItemsList[aIdx].SetItem( aItem );
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].SetStatus( aStatus );
255  return true;
256  }
257 
258  return false;
259 }
260 
261 
262 bool PICKED_ITEMS_LIST::SetPickerFlags( STATUS_FLAGS aFlags, unsigned aIdx )
263 {
264  if( aIdx < m_ItemsList.size() )
265  {
266  m_ItemsList[aIdx].SetFlags( aFlags );
267  return true;
268  }
269 
270  return false;
271 }
272 
273 
274 bool PICKED_ITEMS_LIST::RemovePicker( unsigned aIdx )
275 {
276  if( aIdx >= m_ItemsList.size() )
277  return false;
278 
279  m_ItemsList.erase( m_ItemsList.begin() + aIdx );
280  return true;
281 }
282 
283 
285 {
286  m_ItemsList = aSource.m_ItemsList; // Vector's copy
287 }
288 
289 
291 {
292  std::vector <ITEM_PICKER> tmp;
293  while( !m_ItemsList.empty() )
294  {
295  tmp.push_back( m_ItemsList.back() );
296  m_ItemsList.pop_back();
297  }
298 
299  m_ItemsList.swap( tmp );
300 }
301 
302 
303 /**********************************************/
304 /********** UNDO_REDO_CONTAINER ***************/
305 /**********************************************/
306 
308 {
309 }
310 
311 
313 {
315 }
316 
317 
319 {
320  for( unsigned ii = 0; ii < m_CommandsList.size(); ii++ )
321  delete m_CommandsList[ii];
322 
323  m_CommandsList.clear();
324 }
325 
326 
328 {
329  m_CommandsList.push_back( aItem );
330 }
331 
332 
334 {
335  if( m_CommandsList.size() != 0 )
336  {
337  PICKED_ITEMS_LIST* item = m_CommandsList.back();
338  m_CommandsList.pop_back();
339  return item;
340  }
341 
342  return NULL;
343 }
std::vector< ITEM_PICKER > m_ItemsList
Use for changing the schematic text type where swapping data structure is insufficient to restor the ...
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.
void PushItem(const ITEM_PICKER &aItem)
Function PushItem pushes aItem to the top of the list.
EDA_ITEM * Next() const
Definition: base_struct.h:220
UNDO_REDO_T GetPickedItemStatus(unsigned int aIdx) const
Function GetPickedItemStatus.
PICKED_ITEMS_LIST * PopCommand()
EDA_ITEM * GetPickedItemLink(unsigned int aIdx) const
Function GetPickedItemLink.
UNDO_REDO_T GetStatus() const
void PushCommand(PICKED_ITEMS_LIST *aCommand)
UNDO_REDO_T
Undo Redo considerations: Basically we have 3 cases New item Deleted item Modified item there is also...
bool ContainsItem(const EDA_ITEM *aItem) const
Function IsItemInList.
void CopyList(const PICKED_ITEMS_LIST &aSource)
Function CopyList copies all data from aSource to the list.
EDA_ITEM * GetItem() const
std::vector< PICKED_ITEMS_LIST * > m_CommandsList
bool SetPickedItem(EDA_ITEM *aItem, unsigned aIdx)
Function SetPickedItem.
unsigned STATUS_FLAGS
Definition: base_struct.h:158
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
unsigned GetCount() const
Function GetCount.
STATUS_FLAGS GetPickerFlags(unsigned aIdx) const
Function GetPickerFlags returns the value of the picker flag.
bool SetPickedItemStatus(UNDO_REDO_T aStatus, unsigned aIdx)
Function SetPickedItemStatus sets the type of undo/redo operation for a given picked item...
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
EDA_ITEM * GetPickedItem(unsigned int aIdx) const
Function GetPickedItem.
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.
The common library.
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.
UNDO_REDO_T m_undoRedoStatus
Basic classes for most KiCad items.
int FindItem(const EDA_ITEM *aItem) const
Function FindItem.
ITEM_PICKER GetItemWrapper(unsigned int aIdx) const
Function GetItemWrapper.
EDA_ITEM * GetLink() const
void ClearListAndDeleteItems()
Function ClearListAndDeleteItems deletes the list of pickers, AND the data pointed by m_PickedItem or...