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, you may find one here:
22 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
23 * or you may search the http://www.gnu.org website for the version 2 license,
24 * or you may write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
26 */
27
28#include <core/kicad_algo.h>
29#include <commit.h>
30#include <eda_item.h>
31#include <eda_group.h>
32#include <macros.h>
33
37
38
40{
41 for( COMMIT_LINE& ent : m_entries )
42 delete ent.m_copy;
43}
44
45
46COMMIT& COMMIT::Stage( EDA_ITEM* aItem, CHANGE_TYPE aChangeType, BASE_SCREEN* aScreen, RECURSE_MODE aRecurse )
47{
48 int flags = aChangeType & CHT_FLAGS;
49 int changeType = aChangeType & CHT_TYPE;
50 EDA_ITEM* undoItem = undoLevelItem( aItem );
51
52 if( undoItem != aItem )
53 changeType = CHT_MODIFY;
54
55 // CHT_MODIFY and CHT_DONE are not compatible
56 if( changeType == CHT_MODIFY )
57 wxASSERT( ( 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
157{
158 if( undoLevelItem( aItem ) != aItem )
159 wxFAIL_MSG( "We've no way to get a copy of the undo level item at this point" );
160 else
161 makeEntry( aItem, CHT_MODIFY, aCopy, aScreen );
162
163 return *this;
164}
165
166
168{
169 COMMIT_LINE* entry = findEntry( undoLevelItem( aItem ), aScreen );
170
171 return entry ? entry->m_type : 0;
172}
173
174
175void COMMIT::makeEntry( EDA_ITEM* aItem, CHANGE_TYPE aType, EDA_ITEM* aCopy, BASE_SCREEN *aScreen )
176{
177 COMMIT_LINE ent;
178
179 ent.m_item = aItem;
180 ent.m_type = aType;
181 ent.m_copy = aCopy;
182 ent.m_screen = aScreen;
183
184 // N.B. Do not throw an assertion for multiple changed items. An item can be changed
185 // multiple times in a single commit such as when importing graphics and grouping them.
186
187 switch( aType & CHT_TYPE )
188 {
189 case CHT_ADD: m_addedItems.insert( { aItem, aScreen } ); break;
190 case CHT_REMOVE: m_deletedItems.insert( { aItem, aScreen } ); break;
191 case CHT_MODIFY: m_changedItems.insert( { aItem, aScreen } ); break;
192 default: wxFAIL; break;
193 }
194
195 m_entries.push_back( ent );
196}
197
198
200{
201 for( COMMIT_LINE& entry : m_entries )
202 {
203 if( entry.m_item == aItem && entry.m_screen == aScreen )
204 return &entry;
205 }
206
207 return nullptr;
208}
209
210
212{
213 switch( aType )
214 {
215 case UNDO_REDO::NEWITEM: return CHT_ADD;
216 case UNDO_REDO::DELETED: return CHT_REMOVE;
217 case UNDO_REDO::CHANGED: return CHT_MODIFY;
218 default: wxFAIL; return CHT_MODIFY;
219 }
220}
221
222
224{
225 switch( aType )
226 {
227 case CHT_ADD: return UNDO_REDO::NEWITEM;
228 case CHT_REMOVE: return UNDO_REDO::DELETED;
229 case CHT_MODIFY: return UNDO_REDO::CHANGED;
230 default: wxFAIL; return UNDO_REDO::CHANGED;
231 }
232}
233
Handles how to draw a screen (a board, a schematic ...)
Definition base_screen.h:41
std::set< std::pair< EDA_ITEM *, BASE_SCREEN * > > m_deletedItems
Definition commit.h:184
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: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:106
COMMIT()
Definition commit.cpp:34
virtual ~COMMIT()
Definition commit.cpp:39
COMMIT_LINE * findEntry(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Search for an entry describing change for a particular item.
Definition commit.cpp:199
std::set< std::pair< EDA_ITEM *, BASE_SCREEN * > > m_addedItems
Definition commit.h:182
virtual void makeEntry(EDA_ITEM *aItem, CHANGE_TYPE aType, EDA_ITEM *aCopy=nullptr, BASE_SCREEN *aScreen=nullptr)
Definition commit.cpp:175
std::vector< COMMIT_LINE > m_entries
Definition commit.h:185
std::set< std::pair< EDA_ITEM *, BASE_SCREEN * > > m_changedItems
Definition commit.h:183
virtual EDA_ITEM * makeImage(EDA_ITEM *aItem) const =0
CHANGE_TYPE convert(UNDO_REDO aType) const
Definition commit.cpp:211
int GetStatus(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Returns status of an item.
Definition commit.cpp:167
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:46
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:98
virtual EDA_GROUP * GetParentGroup() const
Definition eda_item.h:116
bool IsNew() const
Definition eda_item.h:124
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:41
@ CHT_MODIFY
Definition commit.h:44
@ CHT_REMOVE
Definition commit.h:43
@ CHT_DONE
Flag to indicate the change is already applied.
Definition commit.h:47
@ CHT_TYPE
Definition commit.h:45
@ CHT_ADD
Definition commit.h:42
@ CHT_FLAGS
Definition commit.h:48
RECURSE_MODE
Definition eda_item.h:50
@ NO_RECURSE
Definition eda_item.h:52
#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:96
EDA_ITEM * m_copy
Optional copy of the item.
Definition commit.h:151
CHANGE_TYPE m_type
Modification type.
Definition commit.h:152
EDA_ITEM * m_item
Main item that is added/deleted/modified.
Definition commit.h:150
BASE_SCREEN * m_screen
Definition commit.h:153
UNDO_REDO
Undo Redo considerations: Basically we have 3 cases New item Deleted item Modified item there is also...