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 <stambaughw@gmail.com>
6 * Copyright (C) 2021 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
ITEM_PICKER()30 ITEM_PICKER::ITEM_PICKER()
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
ITEM_PICKER(BASE_SCREEN * aScreen,EDA_ITEM * aItem,UNDO_REDO aUndoRedoStatus)40 ITEM_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
SetItem(EDA_ITEM * aItem)50 void ITEM_PICKER::SetItem( EDA_ITEM* aItem )
51 {
52 m_pickedItem = aItem;
53 m_pickedItemType = aItem ? aItem->Type() : TYPE_NOT_INIT;
54 }
55
56
PICKED_ITEMS_LIST()57 PICKED_ITEMS_LIST::PICKED_ITEMS_LIST()
58 {
59 }
60
61
~PICKED_ITEMS_LIST()62 PICKED_ITEMS_LIST::~PICKED_ITEMS_LIST()
63 {
64 }
65
66
PushItem(const ITEM_PICKER & aItem)67 void PICKED_ITEMS_LIST::PushItem( const ITEM_PICKER& aItem )
68 {
69 m_ItemsList.push_back( aItem );
70 }
71
72
PopItem()73 ITEM_PICKER PICKED_ITEMS_LIST::PopItem()
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
ContainsItem(const EDA_ITEM * aItem) const87 bool 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
FindItem(const EDA_ITEM * aItem) const99 int PICKED_ITEMS_LIST::FindItem( const EDA_ITEM* aItem ) const
100 {
101 for( size_t i = 0; i < m_ItemsList.size(); i++ )
102 {
103 if( m_ItemsList[i].GetItem() == aItem )
104 return i;
105 }
106
107 return -1;
108 }
109
110
ClearItemsList()111 void PICKED_ITEMS_LIST::ClearItemsList()
112 {
113 m_ItemsList.clear();
114 }
115
116
ClearListAndDeleteItems()117 void PICKED_ITEMS_LIST::ClearListAndDeleteItems()
118 {
119 // Delete items is they are not flagged NEWITEM, or if this is a block operation
120 while( GetCount() > 0 )
121 {
122 ITEM_PICKER wrapper = PopItem();
123
124 if( wrapper.GetItem() == nullptr ) // No more items in list.
125 break;
126
127 // The Link is an undo construct; it is always owned by the undo/redo container
128 if( wrapper.GetLink() )
129 delete wrapper.GetLink();
130
131 if( wrapper.GetFlags() & UR_TRANSIENT )
132 {
133 delete wrapper.GetItem();
134 }
135 else if( wrapper.GetStatus() == UNDO_REDO::DELETED )
136 {
137 // This should really be replaced with UR_TRANSIENT, but currently many clients
138 // (eeschema in particular) abuse this to achieve non-undo-related deletions.
139 delete wrapper.GetItem();
140 }
141 }
142 }
143
144
GetItemWrapper(unsigned int aIdx) const145 ITEM_PICKER PICKED_ITEMS_LIST::GetItemWrapper( unsigned int aIdx ) const
146 {
147 ITEM_PICKER picker;
148
149 if( aIdx < m_ItemsList.size() )
150 picker = m_ItemsList[aIdx];
151
152 return picker;
153 }
154
155
GetPickedItem(unsigned int aIdx) const156 EDA_ITEM* PICKED_ITEMS_LIST::GetPickedItem( unsigned int aIdx ) const
157 {
158 if( aIdx < m_ItemsList.size() )
159 return m_ItemsList[aIdx].GetItem();
160
161 return nullptr;
162 }
163
164
GetScreenForItem(unsigned int aIdx) const165 BASE_SCREEN* PICKED_ITEMS_LIST::GetScreenForItem( unsigned int aIdx ) const
166 {
167 if( aIdx < m_ItemsList.size() )
168 return m_ItemsList[aIdx].GetScreen();
169
170 return nullptr;
171 }
172
173
GetPickedItemLink(unsigned int aIdx) const174 EDA_ITEM* PICKED_ITEMS_LIST::GetPickedItemLink( unsigned int aIdx ) const
175 {
176 if( aIdx < m_ItemsList.size() )
177 return m_ItemsList[aIdx].GetLink();
178
179 return nullptr;
180 }
181
182
GetPickedItemStatus(unsigned int aIdx) const183 UNDO_REDO PICKED_ITEMS_LIST::GetPickedItemStatus( unsigned int aIdx ) const
184 {
185 if( aIdx < m_ItemsList.size() )
186 return m_ItemsList[aIdx].GetStatus();
187
188 return UNDO_REDO::UNSPECIFIED;
189 }
190
191
GetPickerFlags(unsigned aIdx) const192 EDA_ITEM_FLAGS PICKED_ITEMS_LIST::GetPickerFlags( unsigned aIdx ) const
193 {
194 if( aIdx < m_ItemsList.size() )
195 return m_ItemsList[aIdx].GetFlags();
196
197 return 0;
198 }
199
200
SetPickedItem(EDA_ITEM * aItem,unsigned aIdx)201 bool PICKED_ITEMS_LIST::SetPickedItem( EDA_ITEM* aItem, unsigned aIdx )
202 {
203 if( aIdx < m_ItemsList.size() )
204 {
205 m_ItemsList[aIdx].SetItem( aItem );
206 return true;
207 }
208
209 return false;
210 }
211
212
SetPickedItemLink(EDA_ITEM * aLink,unsigned aIdx)213 bool PICKED_ITEMS_LIST::SetPickedItemLink( EDA_ITEM* aLink, unsigned aIdx )
214 {
215 if( aIdx < m_ItemsList.size() )
216 {
217 m_ItemsList[aIdx].SetLink( aLink );
218 return true;
219 }
220
221 return false;
222 }
223
224
SetPickedItem(EDA_ITEM * aItem,UNDO_REDO aStatus,unsigned aIdx)225 bool PICKED_ITEMS_LIST::SetPickedItem( EDA_ITEM* aItem, UNDO_REDO aStatus, unsigned aIdx )
226 {
227 if( aIdx < m_ItemsList.size() )
228 {
229 m_ItemsList[aIdx].SetItem( aItem );
230 m_ItemsList[aIdx].SetStatus( aStatus );
231 return true;
232 }
233
234 return false;
235 }
236
237
SetPickedItemStatus(UNDO_REDO aStatus,unsigned aIdx)238 bool PICKED_ITEMS_LIST::SetPickedItemStatus( UNDO_REDO aStatus, unsigned aIdx )
239 {
240 if( aIdx < m_ItemsList.size() )
241 {
242 m_ItemsList[aIdx].SetStatus( aStatus );
243 return true;
244 }
245
246 return false;
247 }
248
249
SetPickerFlags(EDA_ITEM_FLAGS aFlags,unsigned aIdx)250 bool PICKED_ITEMS_LIST::SetPickerFlags( EDA_ITEM_FLAGS aFlags, unsigned aIdx )
251 {
252 if( aIdx < m_ItemsList.size() )
253 {
254 m_ItemsList[aIdx].SetFlags( aFlags );
255 return true;
256 }
257
258 return false;
259 }
260
261
RemovePicker(unsigned aIdx)262 bool PICKED_ITEMS_LIST::RemovePicker( unsigned aIdx )
263 {
264 if( aIdx >= m_ItemsList.size() )
265 return false;
266
267 m_ItemsList.erase( m_ItemsList.begin() + aIdx );
268 return true;
269 }
270
271
CopyList(const PICKED_ITEMS_LIST & aSource)272 void PICKED_ITEMS_LIST::CopyList( const PICKED_ITEMS_LIST& aSource )
273 {
274 m_ItemsList = aSource.m_ItemsList; // Vector's copy
275 }
276
277
ReversePickersListOrder()278 void PICKED_ITEMS_LIST::ReversePickersListOrder()
279 {
280 std::vector <ITEM_PICKER> tmp;
281
282 while( !m_ItemsList.empty() )
283 {
284 tmp.push_back( m_ItemsList.back() );
285 m_ItemsList.pop_back();
286 }
287
288 m_ItemsList.swap( tmp );
289 }
290
291
UNDO_REDO_CONTAINER()292 UNDO_REDO_CONTAINER::UNDO_REDO_CONTAINER()
293 {
294 }
295
296
~UNDO_REDO_CONTAINER()297 UNDO_REDO_CONTAINER::~UNDO_REDO_CONTAINER()
298 {
299 ClearCommandList();
300 }
301
302
ClearCommandList()303 void UNDO_REDO_CONTAINER::ClearCommandList()
304 {
305 for( unsigned ii = 0; ii < m_CommandsList.size(); ii++ )
306 delete m_CommandsList[ii];
307
308 m_CommandsList.clear();
309 }
310
311
PushCommand(PICKED_ITEMS_LIST * aItem)312 void UNDO_REDO_CONTAINER::PushCommand( PICKED_ITEMS_LIST* aItem )
313 {
314 m_CommandsList.push_back( aItem );
315 }
316
317
PopCommand()318 PICKED_ITEMS_LIST* UNDO_REDO_CONTAINER::PopCommand()
319 {
320 if( m_CommandsList.size() != 0 )
321 {
322 PICKED_ITEMS_LIST* item = m_CommandsList.back();
323 m_CommandsList.pop_back();
324 return item;
325 }
326
327 return nullptr;
328 }
329