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, see <https://www.gnu.org/licenses/>.
20 */
21
22#include <eda_item.h>
23#include <eda_group.h>
24#include <undo_redo_container.h>
25
26
28{
30 SetItem( nullptr );
31 m_pickerFlags = 0;
32 m_link = nullptr;
33 m_screen = nullptr;
34}
35
36
37ITEM_PICKER::ITEM_PICKER( BASE_SCREEN* aScreen, EDA_ITEM* aItem, UNDO_REDO aUndoRedoStatus )
38{
39 m_undoRedoStatus = aUndoRedoStatus;
40 SetItem( aItem );
41 m_pickerFlags = 0;
42 m_link = nullptr;
43 m_screen = aScreen;
44}
45
46
50
51
55
56
58{
59 m_pickedItem = nullptr;
61
62 if( aItem )
63 {
64 m_pickedItem = aItem;
65 m_pickedItemType = aItem->Type();
66
67 if( EDA_GROUP* group = dynamic_cast<EDA_GROUP*>( aItem ) )
68 m_groupMembers = group->GetGroupMemberIds();
69
70 m_groupId = aItem->GetParentGroupId();
71 }
72}
73
74
76{
77 m_link = aItem;
78
79 if( aItem )
80 {
81 if( EDA_GROUP* group = dynamic_cast<EDA_GROUP*>( aItem ) )
82 m_groupMembers = group->GetGroupMemberIds();
83
84 m_groupId = aItem->GetParentGroupId();
85 }
86}
87
88
90{
91 m_ItemsList.push_back( aItem );
92}
93
94
96{
97 ITEM_PICKER item;
98
99 if( m_ItemsList.size() != 0 )
100 {
101 item = m_ItemsList.back();
102 m_ItemsList.pop_back();
103 }
104
105 return item;
106}
107
108
110{
111 for( const ITEM_PICKER& picker : m_ItemsList )
112 {
113 if( picker.GetItem() == aItem )
114 return true;
115 }
116
117 return false;
118}
119
120
121int PICKED_ITEMS_LIST::FindItem( const EDA_ITEM* aItem ) const
122{
123 for( size_t i = 0; i < m_ItemsList.size(); i++ )
124 {
125 if( m_ItemsList[i].GetItem() == aItem )
126 return i;
127 }
128
129 return -1;
130}
131
132
134{
135 m_ItemsList.clear();
136}
137
138
139void PICKED_ITEMS_LIST::ClearListAndDeleteItems( std::function<void(EDA_ITEM*)> aItemDeleter )
140{
141 while( GetCount() > 0 )
142 {
144
145 if( wrapper.GetItem() == nullptr ) // No more items in list.
146 break;
147
148 // The Link is an undo construct; it is always owned by the undo/redo container
149 if( wrapper.GetLink() )
150 aItemDeleter( wrapper.GetLink() );
151
152 if( wrapper.GetFlags() & UR_TRANSIENT )
153 {
154 aItemDeleter( wrapper.GetItem() );
155 }
156 else if( wrapper.GetStatus() == UNDO_REDO::DELETED )
157 {
158 // This should really be replaced with UR_TRANSIENT, but currently many clients
159 // (eeschema in particular) abuse this to achieve non-undo-related deletions.
160 aItemDeleter( wrapper.GetItem() );
161 }
162 }
163}
164
165
166const ITEM_PICKER& PICKED_ITEMS_LIST::GetItemWrapper( unsigned int aIdx ) const
167{
168 return m_ItemsList.at( aIdx );
169}
170
171
173{
174 return m_ItemsList.at( aIdx );
175}
176
177
179{
180 if( aIdx < m_ItemsList.size() )
181 return m_ItemsList[aIdx].GetItem();
182
183 return nullptr;
184}
185
186
188{
189 if( aIdx < m_ItemsList.size() )
190 return m_ItemsList[aIdx].GetScreen();
191
192 return nullptr;
193}
194
195
197{
198 if( aIdx < m_ItemsList.size() )
199 return m_ItemsList[aIdx].GetLink();
200
201 return nullptr;
202}
203
204
206{
207 if( aIdx < m_ItemsList.size() )
208 return m_ItemsList[aIdx].GetStatus();
209
211}
212
213
215{
216 if( aIdx < m_ItemsList.size() )
217 return m_ItemsList[aIdx].GetFlags();
218
219 return 0;
220}
221
222
223bool PICKED_ITEMS_LIST::SetPickedItem( EDA_ITEM* aItem, unsigned aIdx )
224{
225 if( aIdx < m_ItemsList.size() )
226 {
227 m_ItemsList[aIdx].SetItem( aItem );
228 return true;
229 }
230
231 return false;
232}
233
234
236{
237 if( aIdx < m_ItemsList.size() )
238 {
239 m_ItemsList[aIdx].SetLink( aLink );
240 return true;
241 }
242
243 return false;
244}
245
246
248{
249 if( aIdx < m_ItemsList.size() )
250 {
251 m_ItemsList[aIdx].SetStatus( aStatus );
252 return true;
253 }
254
255 return false;
256}
257
258
260{
261 if( aIdx < m_ItemsList.size() )
262 {
263 m_ItemsList[aIdx].SetFlags( aFlags );
264 return true;
265 }
266
267 return false;
268}
269
270
272{
273 if( aIdx >= m_ItemsList.size() )
274 return false;
275
276 m_ItemsList.erase( m_ItemsList.begin() + aIdx );
277 return true;
278}
279
280
282{
283 m_ItemsList = aSource.m_ItemsList; // Vector's copy
284}
285
286
288{
289 std::vector <ITEM_PICKER> tmp;
290
291 while( !m_ItemsList.empty() )
292 {
293 tmp.push_back( m_ItemsList.back() );
294 m_ItemsList.pop_back();
295 }
296
297 m_ItemsList.swap( tmp );
298}
299
300
304
305
310
311
313{
314 for( unsigned ii = 0; ii < m_CommandsList.size(); ii++ )
315 delete m_CommandsList[ii];
316
317 m_CommandsList.clear();
318}
319
320
322{
323 m_CommandsList.push_back( aItem );
324}
325
326
328{
329 if( m_CommandsList.size() != 0 )
330 {
331 PICKED_ITEMS_LIST* item = m_CommandsList.back();
332 m_CommandsList.pop_back();
333 return item;
334 }
335
336 return nullptr;
337}
Handles how to draw a screen (a board, a schematic ...)
Definition base_screen.h:37
A set of EDA_ITEMs (i.e., without duplicates).
Definition eda_group.h:42
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:96
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:108
KIID GetParentGroupId() const
Definition eda_item.cpp:97
std::vector< KIID > m_groupMembers
void SetItem(EDA_ITEM *aItem)
void SetLink(EDA_ITEM *aItem)
BASE_SCREEN * m_screen
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:74
UNDO_REDO
Undo Redo considerations: Basically we have 3 cases New item Deleted item Modified item there is also...