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 <macros.h>
32
34{
35}
36
37
39{
40 for( COMMIT_LINE& ent : m_changes )
41 {
42 if( ent.m_copy )
43 delete ent.m_copy;
44 }
45}
46
47
48COMMIT& COMMIT::Stage( EDA_ITEM* aItem, CHANGE_TYPE aChangeType, BASE_SCREEN* aScreen )
49{
50 // CHT_MODIFY and CHT_DONE are not compatible
51 wxASSERT( ( aChangeType & ( CHT_MODIFY | CHT_DONE ) ) != ( CHT_MODIFY | CHT_DONE ) );
52
53 int flag = aChangeType & CHT_FLAGS;
54
55 switch( aChangeType & CHT_TYPE )
56 {
57 case CHT_ADD:
58 makeEntry( aItem, CHT_ADD | flag, nullptr, aScreen );
59 return *this;
60
61 case CHT_REMOVE:
62 wxASSERT( m_deletedItems.find( aItem ) == m_deletedItems.end() );
63 m_deletedItems.insert( aItem );
64 makeEntry( aItem, CHT_REMOVE | flag, nullptr, aScreen );
65 return *this;
66
67 case CHT_MODIFY:
68 {
69 EDA_ITEM* parent = parentObject( aItem );
70 EDA_ITEM* clone = makeImage( parent );
71
72 wxASSERT( clone );
73
74 if( clone )
75 return createModified( parent, clone, flag, aScreen );
76
77 break;
78 }
79
80 case CHT_GROUP:
81 case CHT_UNGROUP:
82 makeEntry( aItem, aChangeType, nullptr, aScreen );
83 return *this;
84
85 default:
86 wxFAIL;
87 }
88
89 return *this;
90}
91
92
93COMMIT& COMMIT::Stage( std::vector<EDA_ITEM*> &container, CHANGE_TYPE aChangeType,
94 BASE_SCREEN *aScreen )
95{
96 for( EDA_ITEM* item : container )
97 Stage( item, aChangeType, aScreen);
98
99 return *this;
100}
101
102
103COMMIT& COMMIT::Stage( const PICKED_ITEMS_LIST &aItems, UNDO_REDO aModFlag, BASE_SCREEN *aScreen )
104{
105 for( unsigned int i = 0; i < aItems.GetCount(); i++ )
106 {
107 UNDO_REDO change_type = aItems.GetPickedItemStatus( i );
108 EDA_ITEM* item = aItems.GetPickedItem( i );
109
110 if( change_type == UNDO_REDO::UNSPECIFIED )
111 change_type = aModFlag;
112
113 if( EDA_ITEM* copy = aItems.GetPickedItemLink( i ) )
114 {
115 assert( change_type == UNDO_REDO::CHANGED );
116
117 // There was already a copy created, so use it
118 Modified( item, copy, aScreen );
119 }
120 else
121 {
122 Stage( item, convert( change_type ), aScreen );
123 }
124 }
125
126 return *this;
127}
128
129
131{
132 COMMIT_LINE* entry = findEntry( parentObject( aItem ), aScreen );
133
134 return entry ? entry->m_type : 0;
135}
136
137
138COMMIT& COMMIT::createModified( EDA_ITEM* aItem, EDA_ITEM* aCopy, int aExtraFlags,
139 BASE_SCREEN* aScreen )
140{
141 EDA_ITEM* parent = parentObject( aItem );
142
143 if( m_changedItems.find( parent ) != m_changedItems.end() )
144 {
145 delete aCopy;
146 return *this; // item has been already modified once
147 }
148
149 makeEntry( parent, CHT_MODIFY | aExtraFlags, aCopy, aScreen );
150
151 return *this;
152}
153
154
155void COMMIT::makeEntry( EDA_ITEM* aItem, CHANGE_TYPE aType, EDA_ITEM* aCopy, BASE_SCREEN *aScreen )
156{
157 // Expect an item copy if it is going to be modified
158 wxASSERT( !!aCopy == ( ( aType & CHT_TYPE ) == CHT_MODIFY ) );
159
160 COMMIT_LINE ent;
161
162 ent.m_item = aItem;
163 ent.m_type = aType;
164 ent.m_copy = aCopy;
165 ent.m_screen = aScreen;
166
167 // N.B. Do not throw an assertion for multiple changed items. An item can be changed
168 // multiple times in a single commit such as when importing graphics and grouping them.
169
170 m_changedItems.insert( aItem );
171 m_changes.push_back( ent );
172}
173
174
176{
177 for( COMMIT_LINE& change : m_changes )
178 {
179 if( change.m_item == aItem && change.m_screen == aScreen )
180 return &change;
181 }
182
183 return nullptr;
184}
185
186
188{
189 switch( aType )
190 {
191 case UNDO_REDO::NEWITEM: return CHT_ADD;
192 case UNDO_REDO::DELETED: return CHT_REMOVE;
193 case UNDO_REDO::REGROUP: return CHT_GROUP;
194 case UNDO_REDO::UNGROUP: return CHT_UNGROUP;
195 case UNDO_REDO::CHANGED: return CHT_MODIFY;
196 default: wxASSERT( false ); return CHT_MODIFY;
197 }
198}
199
200
202{
203 switch( aType )
204 {
205 case CHT_ADD: return UNDO_REDO::NEWITEM;
206 case CHT_REMOVE: return UNDO_REDO::DELETED;
207 case CHT_GROUP: return UNDO_REDO::REGROUP;
208 case CHT_UNGROUP: return UNDO_REDO::UNGROUP;
209 case CHT_MODIFY: return UNDO_REDO::CHANGED;
210 default: wxASSERT( false ); return UNDO_REDO::CHANGED;
211 }
212}
213
Handles how to draw a screen (a board, a schematic ...)
Definition: base_screen.h:41
Represent a set of changes (additions, deletions or modifications) of a data model (e....
Definition: commit.h:74
virtual COMMIT & Stage(EDA_ITEM *aItem, CHANGE_TYPE aChangeType, BASE_SCREEN *aScreen=nullptr)
Add a change of the item aItem of type aChangeType to the change list.
Definition: commit.cpp:48
COMMIT & createModified(EDA_ITEM *aItem, EDA_ITEM *aCopy, int aExtraFlags=0, BASE_SCREEN *aScreen=nullptr)
Definition: commit.cpp:138
std::set< EDA_ITEM * > m_deletedItems
Definition: commit.h:200
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.h:118
COMMIT()
Definition: commit.cpp:33
virtual ~COMMIT()
Definition: commit.cpp:38
COMMIT_LINE * findEntry(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Search for an entry describing change for a particular item.
Definition: commit.cpp:175
virtual void makeEntry(EDA_ITEM *aItem, CHANGE_TYPE aType, EDA_ITEM *aCopy=nullptr, BASE_SCREEN *aScreen=nullptr)
Definition: commit.cpp:155
std::set< EDA_ITEM * > m_changedItems
Definition: commit.h:199
virtual EDA_ITEM * parentObject(EDA_ITEM *aItem) const =0
virtual EDA_ITEM * makeImage(EDA_ITEM *aItem) const =0
CHANGE_TYPE convert(UNDO_REDO aType) const
Definition: commit.cpp:187
int GetStatus(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Returns status of an item.
Definition: commit.cpp:130
std::vector< COMMIT_LINE > m_changes
Definition: commit.h:201
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:89
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_GROUP
Definition: commit.h:45
@ CHT_REMOVE
Definition: commit.h:43
@ CHT_DONE
Flag to indicate the change is already applied.
Definition: commit.h:49
@ CHT_TYPE
Definition: commit.h:47
@ CHT_ADD
Definition: commit.h:42
@ CHT_UNGROUP
Definition: commit.h:46
@ CHT_FLAGS
Definition: commit.h:50
This file contains miscellaneous commonly used macros and functions.
EDA_ITEM * m_copy
Optional copy of the item.
Definition: commit.h:164
CHANGE_TYPE m_type
Modification type.
Definition: commit.h:165
EDA_ITEM * m_item
Main item that is added/deleted/modified.
Definition: commit.h:163
BASE_SCREEN * m_screen
Definition: commit.h:167
UNDO_REDO
Undo Redo considerations: Basically we have 3 cases New item Deleted item Modified item there is also...