KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 <[email protected]>
6 * Copyright The 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 <eda_group.h>
28#include <undo_redo_container.h>
29
30
32{
33 m_undoRedoStatus = UNDO_REDO::UNSPECIFIED;
34 SetItem( nullptr );
35 m_pickerFlags = 0;
36 m_link = nullptr;
37 m_screen = nullptr;
38}
39
40
41ITEM_PICKER::ITEM_PICKER( BASE_SCREEN* aScreen, EDA_ITEM* aItem, UNDO_REDO aUndoRedoStatus )
42{
43 m_undoRedoStatus = aUndoRedoStatus;
44 SetItem( aItem );
45 m_pickerFlags = 0;
46 m_link = nullptr;
47 m_screen = aScreen;
48}
49
50
52{
53}
54
55
57{
58}
59
60
62{
63 m_pickedItem = nullptr;
65
66 if( aItem )
67 {
68 m_pickedItem = aItem;
69 m_pickedItemType = aItem->Type();
70
71 if( EDA_GROUP* group = dynamic_cast<EDA_GROUP*>( aItem ) )
72 m_groupMembers = group->GetGroupMemberIds();
73
74 m_groupId = aItem->GetParentGroupId();
75 }
76}
77
78
80{
81 m_link = aItem;
82
83 if( aItem )
84 {
85 if( EDA_GROUP* group = dynamic_cast<EDA_GROUP*>( aItem ) )
86 m_groupMembers = group->GetGroupMemberIds();
87
88 m_groupId = aItem->GetParentGroupId();
89 }
90}
91
92
94{
95 m_ItemsList.push_back( aItem );
96}
97
98
100{
101 ITEM_PICKER item;
102
103 if( m_ItemsList.size() != 0 )
104 {
105 item = m_ItemsList.back();
106 m_ItemsList.pop_back();
107 }
108
109 return item;
110}
111
112
114{
115 for( const ITEM_PICKER& picker : m_ItemsList )
116 {
117 if( picker.GetItem() == aItem )
118 return true;
119 }
120
121 return false;
122}
123
124
125int PICKED_ITEMS_LIST::FindItem( const EDA_ITEM* aItem ) const
126{
127 for( size_t i = 0; i < m_ItemsList.size(); i++ )
128 {
129 if( m_ItemsList[i].GetItem() == aItem )
130 return i;
131 }
132
133 return -1;
134}
135
136
138{
139 m_ItemsList.clear();
140}
141
142
143void PICKED_ITEMS_LIST::ClearListAndDeleteItems( std::function<void(EDA_ITEM*)> aItemDeleter )
144{
145 while( GetCount() > 0 )
146 {
148
149 if( wrapper.GetItem() == nullptr ) // No more items in list.
150 break;
151
152 // The Link is an undo construct; it is always owned by the undo/redo container
153 if( wrapper.GetLink() )
154 aItemDeleter( wrapper.GetLink() );
155
156 if( wrapper.GetFlags() & UR_TRANSIENT )
157 {
158 aItemDeleter( wrapper.GetItem() );
159 }
160 else if( wrapper.GetStatus() == UNDO_REDO::DELETED )
161 {
162 // This should really be replaced with UR_TRANSIENT, but currently many clients
163 // (eeschema in particular) abuse this to achieve non-undo-related deletions.
164 aItemDeleter( wrapper.GetItem() );
165 }
166 }
167}
168
169
170const ITEM_PICKER& PICKED_ITEMS_LIST::GetItemWrapper( unsigned int aIdx ) const
171{
172 return m_ItemsList.at( aIdx );
173}
174
175
177{
178 return m_ItemsList.at( aIdx );
179}
180
181
183{
184 if( aIdx < m_ItemsList.size() )
185 return m_ItemsList[aIdx].GetItem();
186
187 return nullptr;
188}
189
190
192{
193 if( aIdx < m_ItemsList.size() )
194 return m_ItemsList[aIdx].GetScreen();
195
196 return nullptr;
197}
198
199
201{
202 if( aIdx < m_ItemsList.size() )
203 return m_ItemsList[aIdx].GetLink();
204
205 return nullptr;
206}
207
208
210{
211 if( aIdx < m_ItemsList.size() )
212 return m_ItemsList[aIdx].GetStatus();
213
214 return UNDO_REDO::UNSPECIFIED;
215}
216
217
219{
220 if( aIdx < m_ItemsList.size() )
221 return m_ItemsList[aIdx].GetFlags();
222
223 return 0;
224}
225
226
227bool PICKED_ITEMS_LIST::SetPickedItem( EDA_ITEM* aItem, unsigned aIdx )
228{
229 if( aIdx < m_ItemsList.size() )
230 {
231 m_ItemsList[aIdx].SetItem( aItem );
232 return true;
233 }
234
235 return false;
236}
237
238
240{
241 if( aIdx < m_ItemsList.size() )
242 {
243 m_ItemsList[aIdx].SetLink( aLink );
244 return true;
245 }
246
247 return false;
248}
249
250
252{
253 if( aIdx < m_ItemsList.size() )
254 {
255 m_ItemsList[aIdx].SetStatus( aStatus );
256 return true;
257 }
258
259 return false;
260}
261
262
264{
265 if( aIdx < m_ItemsList.size() )
266 {
267 m_ItemsList[aIdx].SetFlags( aFlags );
268 return true;
269 }
270
271 return false;
272}
273
274
276{
277 if( aIdx >= m_ItemsList.size() )
278 return false;
279
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
295 while( !m_ItemsList.empty() )
296 {
297 tmp.push_back( m_ItemsList.back() );
298 m_ItemsList.pop_back();
299 }
300
301 m_ItemsList.swap( tmp );
302}
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 nullptr;
341}
Handles how to draw a screen (a board, a schematic ...)
Definition: base_screen.h:41
A set of EDA_ITEMs (i.e., without duplicates).
Definition: eda_group.h:46
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:97
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:109
KIID GetParentGroupId() const
Definition: eda_item.cpp:91
void SetItem(EDA_ITEM *aItem)
void SetLink(EDA_ITEM *aItem)
BASE_SCREEN * m_screen
KICAD_T m_pickedItemType
KIID_VECT_LIST m_groupMembers
EDA_ITEM_FLAGS m_pickerFlags
EDA_ITEM * m_pickedItem
UNDO_REDO m_undoRedoStatus
A holder to handle information on schematic or board items.
bool SetPickedItemStatus(UNDO_REDO aStatus, unsigned aIdx)
Set the type of undo/redo operation for a given picked item.
EDA_ITEM_FLAGS GetPickerFlags(unsigned aIdx) const
Return the value of the picker flag.
void PushItem(const ITEM_PICKER &aItem)
Push aItem to the top of the list.
int FindItem(const EDA_ITEM *aItem) const
UNDO_REDO GetPickedItemStatus(unsigned int aIdx) const
EDA_ITEM * GetPickedItemLink(unsigned int aIdx) const
std::vector< ITEM_PICKER > m_ItemsList
bool RemovePicker(unsigned aIdx)
Remove one entry (one picker) from the list of picked items.
const ITEM_PICKER & GetItemWrapper(unsigned int aIdx) const
bool ContainsItem(const EDA_ITEM *aItem) const
unsigned GetCount() const
bool SetPickedItem(EDA_ITEM *aItem, unsigned aIdx)
void CopyList(const PICKED_ITEMS_LIST &aSource)
Copy all data from aSource to the list.
void ReversePickersListOrder()
Reverse the order of pickers stored in this list.
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.
void ClearListAndDeleteItems(std::function< void(EDA_ITEM *)> aItemDeleter)
Delete the list of pickers AND the data pointed by #m_PickedItem or #m_PickedItemLink according to th...
BASE_SCREEN * GetScreenForItem(unsigned int aIdx) const
EDA_ITEM * GetPickedItem(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 PushCommand(PICKED_ITEMS_LIST *aCommand)
PICKED_ITEMS_LIST * PopCommand()
std::vector< PICKED_ITEMS_LIST * > m_CommandsList
std::uint32_t EDA_ITEM_FLAGS
#define UR_TRANSIENT
indicates the item is owned by the undo/redo stack
@ TYPE_NOT_INIT
Definition: typeinfo.h:81
UNDO_REDO
Undo Redo considerations: Basically we have 3 cases New item Deleted item Modified item there is also...