KiCad PCB EDA Suite
Loading...
Searching...
No Matches
commit.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 2016-2017 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Tomasz Wlostowski <[email protected]>
8 * @author Maciej Suminski <[email protected]>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
24#include <core/kicad_algo.h>
25#include <commit.h>
26#include <eda_item.h>
27#include <eda_group.h>
28#include <macros.h>
29
33
34
36{
37 for( COMMIT_LINE& ent : m_entries )
38 delete ent.m_copy;
39}
40
41
42COMMIT& COMMIT::Stage( EDA_ITEM* aItem, CHANGE_TYPE aChangeType, BASE_SCREEN* aScreen, RECURSE_MODE aRecurse )
43{
44 int flags = aChangeType & CHT_FLAGS;
45 int changeType = aChangeType & CHT_TYPE;
46 EDA_ITEM* undoItem = undoLevelItem( aItem );
47
48 if( undoItem != aItem )
49 {
50 changeType = CHT_MODIFY;
51
52 // CHT_DONE means the original add/remove was already applied to the child, but that
53 // semantic doesn't carry over when we remap to a modify of the parent
54 flags &= ~CHT_DONE;
55 }
56
57 wxASSERT( changeType != CHT_MODIFY || ( flags & CHT_DONE ) == 0 );
58
59 switch( changeType )
60 {
61 case CHT_ADD:
62 if( m_addedItems.find( { aItem, aScreen } ) != m_addedItems.end() )
63 break;
64
65 makeEntry( aItem, CHT_ADD | flags, nullptr, aScreen );
66 break;
67
68 case CHT_REMOVE:
69 if( m_deletedItems.find( { aItem, aScreen } ) != m_deletedItems.end() )
70 break;
71
72 makeEntry( aItem, CHT_REMOVE | flags, makeImage( aItem ), aScreen );
73
74 if( EDA_GROUP* parentGroup = aItem->GetParentGroup() )
75 {
76 if( parentGroup->AsEdaItem()->GetFlags() & STRUCT_DELETED )
77 Modify( parentGroup->AsEdaItem(), aScreen, RECURSE_MODE::NO_RECURSE );
78 }
79
80 break;
81
82 case CHT_MODIFY:
83 if( m_addedItems.find( { aItem, aScreen } ) != m_addedItems.end() )
84 break;
85
86 if( m_changedItems.find( { undoItem, aScreen } ) != m_changedItems.end() )
87 break;
88
89 makeEntry( undoItem, CHT_MODIFY | flags, makeImage( undoItem ), aScreen );
90 break;
91
92 default:
93 UNIMPLEMENTED_FOR( undoItem->GetClass() );
94 }
95
96 return *this;
97}
98
99
100COMMIT& COMMIT::Stage( std::vector<EDA_ITEM*> &container, CHANGE_TYPE aChangeType, BASE_SCREEN *aScreen )
101{
102 for( EDA_ITEM* item : container )
103 Stage( item, aChangeType, aScreen);
104
105 return *this;
106}
107
108
109COMMIT& COMMIT::Stage( const PICKED_ITEMS_LIST &aItems, UNDO_REDO aModFlag, BASE_SCREEN *aScreen )
110{
111 for( unsigned int i = 0; i < aItems.GetCount(); i++ )
112 {
113 UNDO_REDO change_type = aItems.GetPickedItemStatus( i );
114 EDA_ITEM* item = aItems.GetPickedItem( i );
115
116 if( change_type == UNDO_REDO::UNSPECIFIED )
117 change_type = aModFlag;
118
119 if( EDA_ITEM* copy = aItems.GetPickedItemLink( i ) )
120 {
121 assert( change_type == UNDO_REDO::CHANGED );
122
123 // There was already a copy created, so use it
124 Modified( item, copy, aScreen );
125 }
126 else
127 {
128 Stage( item, convert( change_type ), aScreen );
129 }
130 }
131
132 return *this;
133}
134
135
136void COMMIT::Unstage( EDA_ITEM* aItem, BASE_SCREEN* aScreen )
137{
138 std::erase_if( m_entries,
139 [&]( COMMIT_LINE& line )
140 {
141 if( line.m_item == aItem && line.m_screen == aScreen )
142 {
143 // Only new items which have never been committed can be unstaged
144 wxASSERT( line.m_item->IsNew() );
145
146 delete line.m_item;
147 delete line.m_copy;
148 return true;
149 }
150
151 return false;
152 } );
153}
154
155
156void COMMIT::Unmodify( EDA_ITEM* aItem, BASE_SCREEN* aScreen )
157{
158 auto changedIt = m_changedItems.find( { aItem, aScreen } );
159
160 if( changedIt == m_changedItems.end() )
161 return;
162
163 std::erase_if( m_entries,
164 [&]( COMMIT_LINE& line )
165 {
166 if( line.m_item == aItem && line.m_screen == aScreen
167 && ( line.m_type & CHT_TYPE ) == CHT_MODIFY )
168 {
169 delete line.m_copy;
170 return true;
171 }
172
173 return false;
174 } );
175
176 m_changedItems.erase( changedIt );
177}
178
179
181{
182 if( undoLevelItem( aItem ) != aItem )
183 wxFAIL_MSG( "We've no way to get a copy of the undo level item at this point" );
184 else
185 makeEntry( aItem, CHT_MODIFY, aCopy, aScreen );
186
187 return *this;
188}
189
190
192{
193 COMMIT_LINE* entry = findEntry( undoLevelItem( aItem ), aScreen );
194
195 return entry ? entry->m_type : 0;
196}
197
198
199void COMMIT::makeEntry( EDA_ITEM* aItem, CHANGE_TYPE aType, EDA_ITEM* aCopy, BASE_SCREEN *aScreen )
200{
201 COMMIT_LINE ent;
202
203 ent.m_item = aItem;
204 ent.m_type = aType;
205 ent.m_copy = aCopy;
206 ent.m_screen = aScreen;
207
208 // N.B. Do not throw an assertion for multiple changed items. An item can be changed
209 // multiple times in a single commit such as when importing graphics and grouping them.
210
211 switch( aType & CHT_TYPE )
212 {
213 case CHT_ADD: m_addedItems.insert( { aItem, aScreen } ); break;
214 case CHT_REMOVE: m_deletedItems.insert( { aItem, aScreen } ); break;
215 case CHT_MODIFY: m_changedItems.insert( { aItem, aScreen } ); break;
216 default: wxFAIL; break;
217 }
218
219 m_entries.push_back( ent );
220}
221
222
224{
225 for( COMMIT_LINE& entry : m_entries )
226 {
227 if( entry.m_item == aItem && entry.m_screen == aScreen )
228 return &entry;
229 }
230
231 return nullptr;
232}
233
234
236{
237 switch( aType )
238 {
239 case UNDO_REDO::NEWITEM: return CHT_ADD;
240 case UNDO_REDO::DELETED: return CHT_REMOVE;
241 case UNDO_REDO::CHANGED: return CHT_MODIFY;
242 default: wxFAIL; return CHT_MODIFY;
243 }
244}
245
246
248{
249 switch( aType )
250 {
251 case CHT_ADD: return UNDO_REDO::NEWITEM;
252 case CHT_REMOVE: return UNDO_REDO::DELETED;
253 case CHT_MODIFY: return UNDO_REDO::CHANGED;
254 default: wxFAIL; return UNDO_REDO::CHANGED;
255 }
256}
257
Handles how to draw a screen (a board, a schematic ...)
Definition base_screen.h:37
std::set< std::pair< EDA_ITEM *, BASE_SCREEN * > > m_deletedItems
Definition commit.h:182
virtual EDA_ITEM * undoLevelItem(EDA_ITEM *aItem) const =0
void Unstage(EDA_ITEM *aItem, BASE_SCREEN *aScreen)
Definition commit.cpp:136
COMMIT & Modified(EDA_ITEM *aItem, EDA_ITEM *aCopy, BASE_SCREEN *aScreen=nullptr)
Create an undo entry for an item that has been already modified.
Definition commit.cpp:180
void Unmodify(EDA_ITEM *aItem, BASE_SCREEN *aScreen)
Definition commit.cpp:156
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr, RECURSE_MODE aRecurse=RECURSE_MODE::NO_RECURSE)
Modify a given item in the model.
Definition commit.h:102
COMMIT()
Definition commit.cpp:30
virtual ~COMMIT()
Definition commit.cpp:35
COMMIT_LINE * findEntry(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Search for an entry describing change for a particular item.
Definition commit.cpp:223
std::set< std::pair< EDA_ITEM *, BASE_SCREEN * > > m_addedItems
Definition commit.h:180
virtual void makeEntry(EDA_ITEM *aItem, CHANGE_TYPE aType, EDA_ITEM *aCopy=nullptr, BASE_SCREEN *aScreen=nullptr)
Definition commit.cpp:199
std::vector< COMMIT_LINE > m_entries
Definition commit.h:183
std::set< std::pair< EDA_ITEM *, BASE_SCREEN * > > m_changedItems
Definition commit.h:181
virtual EDA_ITEM * makeImage(EDA_ITEM *aItem) const =0
CHANGE_TYPE convert(UNDO_REDO aType) const
Definition commit.cpp:235
int GetStatus(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Returns status of an item.
Definition commit.cpp:191
virtual COMMIT & Stage(EDA_ITEM *aItem, CHANGE_TYPE aChangeType, BASE_SCREEN *aScreen=nullptr, RECURSE_MODE aRecurse=RECURSE_MODE::NO_RECURSE)
Add a change of the item aItem of type aChangeType to the change list.
Definition commit.cpp:42
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
virtual EDA_GROUP * GetParentGroup() const
Definition eda_item.h:114
bool IsNew() const
Definition eda_item.h:129
virtual wxString GetClass() const =0
Return the class name.
A holder to handle information on schematic or board items.
UNDO_REDO GetPickedItemStatus(unsigned int aIdx) const
EDA_ITEM * GetPickedItemLink(unsigned int aIdx) const
unsigned GetCount() const
EDA_ITEM * GetPickedItem(unsigned int aIdx) const
CHANGE_TYPE
Types of changes.
Definition commit.h:37
@ CHT_MODIFY
Definition commit.h:40
@ CHT_REMOVE
Definition commit.h:39
@ CHT_DONE
Flag to indicate the change is already applied.
Definition commit.h:43
@ CHT_TYPE
Definition commit.h:41
@ CHT_ADD
Definition commit.h:38
@ CHT_FLAGS
Definition commit.h:44
RECURSE_MODE
Definition eda_item.h:48
@ NO_RECURSE
Definition eda_item.h:50
#define STRUCT_DELETED
flag indication structures to be erased
This file contains miscellaneous commonly used macros and functions.
#define UNIMPLEMENTED_FOR(type)
Definition macros.h:92
EDA_ITEM * m_copy
Optional copy of the item.
Definition commit.h:148
CHANGE_TYPE m_type
Modification type.
Definition commit.h:149
EDA_ITEM * m_item
Main item that is added/deleted/modified.
Definition commit.h:147
BASE_SCREEN * m_screen
Definition commit.h:150
UNDO_REDO
Undo Redo considerations: Basically we have 3 cases New item Deleted item Modified item there is also...