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 (C) 2021-2024 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{
32 m_undoRedoStatus = UNDO_REDO::UNSPECIFIED;
33 SetItem( nullptr );
34 m_pickerFlags = 0;
35 m_link = nullptr;
36 m_screen = nullptr;
37}
38
39
40ITEM_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
87bool 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
100{
101 for( const ITEM_PICKER& picker : m_ItemsList )
102 {
103 const EDA_ITEM* item = picker.GetItem();
104
105 wxCHECK2( item, continue );
106
107 if( item->Type() == aItemType )
108 return true;
109 }
110
111 return false;
112}
113
114
115int PICKED_ITEMS_LIST::FindItem( const EDA_ITEM* aItem ) const
116{
117 for( size_t i = 0; i < m_ItemsList.size(); i++ )
118 {
119 if( m_ItemsList[i].GetItem() == aItem )
120 return i;
121 }
122
123 return -1;
124}
125
126
128{
129 m_ItemsList.clear();
130}
131
132
133void PICKED_ITEMS_LIST::ClearListAndDeleteItems( std::function<void(EDA_ITEM*)> aItemDeleter )
134{
135 while( GetCount() > 0 )
136 {
137 ITEM_PICKER wrapper = PopItem();
138
139 if( wrapper.GetItem() == nullptr ) // No more items in list.
140 break;
141
142 // The Link is an undo construct; it is always owned by the undo/redo container
143 if( wrapper.GetLink() )
144 aItemDeleter( wrapper.GetLink() );
145
146 if( wrapper.GetFlags() & UR_TRANSIENT )
147 {
148 aItemDeleter( wrapper.GetItem() );
149 }
150 else if( wrapper.GetStatus() == UNDO_REDO::DELETED )
151 {
152 // This should really be replaced with UR_TRANSIENT, but currently many clients
153 // (eeschema in particular) abuse this to achieve non-undo-related deletions.
154 aItemDeleter( wrapper.GetItem() );
155 }
156 }
157}
158
159
161{
162 ITEM_PICKER picker;
163
164 if( aIdx < m_ItemsList.size() )
165 picker = m_ItemsList[aIdx];
166
167 return picker;
168}
169
170
172{
173 if( aIdx < m_ItemsList.size() )
174 return m_ItemsList[aIdx].GetItem();
175
176 return nullptr;
177}
178
179
181{
182 if( aIdx < m_ItemsList.size() )
183 return m_ItemsList[aIdx].GetScreen();
184
185 return nullptr;
186}
187
188
190{
191 if( aIdx < m_ItemsList.size() )
192 return m_ItemsList[aIdx].GetLink();
193
194 return nullptr;
195}
196
197
199{
200 if( aIdx < m_ItemsList.size() )
201 return m_ItemsList[aIdx].GetStatus();
202
203 return UNDO_REDO::UNSPECIFIED;
204}
205
206
208{
209 if( aIdx < m_ItemsList.size() )
210 return m_ItemsList[aIdx].GetFlags();
211
212 return 0;
213}
214
215
216bool PICKED_ITEMS_LIST::SetPickedItem( EDA_ITEM* aItem, unsigned aIdx )
217{
218 if( aIdx < m_ItemsList.size() )
219 {
220 m_ItemsList[aIdx].SetItem( aItem );
221 return true;
222 }
223
224 return false;
225}
226
227
229{
230 if( aIdx < m_ItemsList.size() )
231 {
232 m_ItemsList[aIdx].SetLink( aLink );
233 return true;
234 }
235
236 return false;
237}
238
239
240bool PICKED_ITEMS_LIST::SetPickedItem( EDA_ITEM* aItem, UNDO_REDO aStatus, unsigned aIdx )
241{
242 if( aIdx < m_ItemsList.size() )
243 {
244 m_ItemsList[aIdx].SetItem( aItem );
245 m_ItemsList[aIdx].SetStatus( aStatus );
246 return true;
247 }
248
249 return false;
250}
251
252
254{
255 if( aIdx < m_ItemsList.size() )
256 {
257 m_ItemsList[aIdx].SetStatus( aStatus );
258 return true;
259 }
260
261 return false;
262}
263
264
266{
267 if( aIdx < m_ItemsList.size() )
268 {
269 m_ItemsList[aIdx].SetFlags( aFlags );
270 return true;
271 }
272
273 return false;
274}
275
276
278{
279 if( aIdx >= m_ItemsList.size() )
280 return false;
281
282 m_ItemsList.erase( m_ItemsList.begin() + aIdx );
283 return true;
284}
285
286
288{
289 m_ItemsList = aSource.m_ItemsList; // Vector's copy
290}
291
292
294{
295 std::vector <ITEM_PICKER> tmp;
296
297 while( !m_ItemsList.empty() )
298 {
299 tmp.push_back( m_ItemsList.back() );
300 m_ItemsList.pop_back();
301 }
302
303 m_ItemsList.swap( tmp );
304}
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 nullptr;
343}
Handles how to draw a screen (a board, a schematic ...)
Definition: base_screen.h:41
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:85
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:97
void SetItem(EDA_ITEM *aItem)
BASE_SCREEN * m_screen
EDA_ITEM * GetItem() const
KICAD_T m_pickedItemType
EDA_ITEM * GetLink() const
EDA_ITEM_FLAGS GetFlags() const
EDA_ITEM_FLAGS m_pickerFlags
EDA_ITEM * m_pickedItem
UNDO_REDO m_undoRedoStatus
UNDO_REDO GetStatus() const
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
ITEM_PICKER GetItemWrapper(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.
bool ContainsItem(const EDA_ITEM *aItem) const
unsigned GetCount() const
bool ContainsItemType(KICAD_T aItemType) const
Check the undo/redo list for any EDA_ITEM of type aItemType.
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
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:78
@ 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...