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>
29 
30 #include <base_struct.h>
32 
33 
34 ITEM_PICKER::ITEM_PICKER( EDA_ITEM* aItem, UNDO_REDO_T aUndoRedoStatus )
35 {
36  m_undoRedoStatus = aUndoRedoStatus;
37  SetItem( aItem );
38  m_pickerFlags = 0;
39  m_link = NULL;
40 }
41 
42 
44 {
46 }
47 
49 {
50 }
51 
52 
54 {
55  m_ItemsList.push_back( aItem );
56 }
57 
58 
60 {
61  ITEM_PICKER item;
62 
63  if( m_ItemsList.size() != 0 )
64  {
65  item = m_ItemsList.back();
66  m_ItemsList.pop_back();
67  }
68 
69  return item;
70 }
71 
72 
73 bool PICKED_ITEMS_LIST::ContainsItem( const EDA_ITEM* aItem ) const
74 {
75  for( size_t i = 0; i < m_ItemsList.size(); i++ )
76  {
77  if( m_ItemsList[ i ].GetItem() == aItem )
78  return true;
79  }
80 
81  return false;
82 }
83 
84 
85 int PICKED_ITEMS_LIST::FindItem( const EDA_ITEM* aItem ) const
86 {
87  for( size_t i = 0; i < m_ItemsList.size(); i++ )
88  {
89  if( m_ItemsList[i].GetItem() == aItem )
90  return i;
91  }
92 
93  return -1;
94 }
95 
96 
98 {
99  m_ItemsList.clear();
100 }
101 
102 
104 {
105  bool show_error_message = true;
106 
107  // Delete items is they are not flagged UR_NEW, or if this is a block operation
108  while( GetCount() > 0 )
109  {
110  ITEM_PICKER wrapper = PopItem();
111  if( wrapper.GetItem() == NULL ) // No more item in list.
112  break;
113  switch( wrapper.GetStatus() )
114  {
115  case UR_UNSPECIFIED:
116  if( show_error_message )
117  wxMessageBox( wxT( "ClearListAndDeleteItems() error: UR_UNSPECIFIED command type" ) );
118 
119  show_error_message = false;
120  break;
121 
122  case UR_WIRE_IMAGE:
123  {
124  // Specific to eeschema: a linked list of wires is stored. The wrapper picks only
125  // the first item (head of list), and is owner of all picked items.
126  EDA_ITEM* item = wrapper.GetItem();
127 
128  while( item )
129  {
130  // Delete old copy of wires
131  EDA_ITEM* nextitem = item->Next();
132  delete item;
133  item = nextitem;
134  }
135  }
136  break;
137 
138  case UR_MOVED:
139  case UR_FLIPPED:
140  case UR_MIRRORED_X:
141  case UR_MIRRORED_Y:
142  case UR_ROTATED:
144  case UR_NEW: // Do nothing, items are in use, the picker is not owner of items
145  break;
146 
147  case UR_CHANGED:
148  case UR_EXCHANGE_T:
149  delete wrapper.GetLink(); // the picker is owner of this item
150  break;
151 
152  case UR_DELETED: // the picker is owner of this item
153  case UR_LIBEDIT: /* Libedit save always a copy of the current item
154  * So, the picker is always owner of the picked item
155  */
156  delete wrapper.GetItem();
157  break;
158 
159  default:
160  wxFAIL_MSG( wxString::Format( wxT( "Cannot clear unknown undo/redo command %d" ),
161  wrapper.GetStatus() ) );
162  break;
163  }
164  }
165 }
166 
167 
169 {
170  ITEM_PICKER picker;
171 
172  if( aIdx < m_ItemsList.size() )
173  picker = m_ItemsList[aIdx];
174 
175  return picker;
176 }
177 
178 
179 EDA_ITEM* PICKED_ITEMS_LIST::GetPickedItem( unsigned int aIdx ) const
180 {
181  if( aIdx < m_ItemsList.size() )
182  return m_ItemsList[aIdx].GetItem();
183  else
184  return NULL;
185 }
186 
187 
189 {
190  if( aIdx < m_ItemsList.size() )
191  return m_ItemsList[aIdx].GetLink();
192  else
193  return NULL;
194 }
195 
196 
198 {
199  if( aIdx < m_ItemsList.size() )
200  return m_ItemsList[aIdx].GetStatus();
201  else
202  return UR_UNSPECIFIED;
203 }
204 
205 
207 {
208  if( aIdx < m_ItemsList.size() )
209  return m_ItemsList[aIdx].GetFlags();
210  else
211  return 0;
212 }
213 
214 
215 bool PICKED_ITEMS_LIST::SetPickedItem( EDA_ITEM* aItem, unsigned aIdx )
216 {
217  if( aIdx < m_ItemsList.size() )
218  {
219  m_ItemsList[aIdx].SetItem( aItem );
220  return true;
221  }
222  else
223  return false;
224 }
225 
226 
227 bool PICKED_ITEMS_LIST::SetPickedItemLink( EDA_ITEM* aLink, unsigned aIdx )
228 {
229  if( aIdx < m_ItemsList.size() )
230  {
231  m_ItemsList[aIdx].SetLink( aLink );
232  return true;
233  }
234  else
235  return false;
236 }
237 
238 
239 bool PICKED_ITEMS_LIST::SetPickedItem( EDA_ITEM* aItem, UNDO_REDO_T aStatus, unsigned aIdx )
240 {
241  if( aIdx < m_ItemsList.size() )
242  {
243  m_ItemsList[aIdx].SetItem( aItem );
244  m_ItemsList[aIdx].SetStatus( aStatus );
245  return true;
246  }
247  else
248  return false;
249 }
250 
251 
253 {
254  if( aIdx < m_ItemsList.size() )
255  {
256  m_ItemsList[aIdx].SetStatus( aStatus );
257  return true;
258  }
259  else
260  return false;
261 }
262 
263 
264 bool PICKED_ITEMS_LIST::SetPickerFlags( STATUS_FLAGS aFlags, unsigned aIdx )
265 {
266  if( aIdx < m_ItemsList.size() )
267  {
268  m_ItemsList[aIdx].SetFlags( aFlags );
269  return true;
270  }
271  else
272  return false;
273 }
274 
275 
276 bool PICKED_ITEMS_LIST::RemovePicker( unsigned aIdx )
277 {
278  if( aIdx >= m_ItemsList.size() )
279  return false;
280  m_ItemsList.erase( m_ItemsList.begin() + aIdx );
281  return true;
282 }
283 
284 
286 {
287  m_ItemsList = aSource.m_ItemsList; // Vector's copy
288 }
289 
290 
292 {
293  std::vector <ITEM_PICKER> tmp;
294  while( !m_ItemsList.empty() )
295  {
296  tmp.push_back( m_ItemsList.back() );
297  m_ItemsList.pop_back();
298  }
299 
300  m_ItemsList.swap( tmp );
301 }
302 
303 
304 /**********************************************/
305 /********** UNDO_REDO_CONTAINER ***************/
306 /**********************************************/
307 
309 {
310 }
311 
312 
314 {
316 }
317 
318 
320 {
321  for( unsigned ii = 0; ii < m_CommandsList.size(); ii++ )
322  delete m_CommandsList[ii];
323 
324  m_CommandsList.clear();
325 }
326 
327 
329 {
330  m_CommandsList.push_back( aItem );
331 }
332 
333 
335 {
336  if( m_CommandsList.size() != 0 )
337  {
338  PICKED_ITEMS_LIST* item = m_CommandsList.back();
339  m_CommandsList.pop_back();
340  return item;
341  }
342 
343  return NULL;
344 }
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:206
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:144
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:151
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...