KiCad PCB EDA Suite
Loading...
Searching...
No Matches
commit.h
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#ifndef __COMMIT_H
29#define __COMMIT_H
30
31#include <set>
32#include <vector>
33#include <wx/string.h>
34#include <undo_redo_container.h>
35#include <kiid.h>
36#include <eda_item.h>
37
38class EDA_GROUP;
39class BASE_SCREEN;
40
47
48 CHT_DONE = 32,
50};
51
52template<typename T>
54{
55 return CHANGE_TYPE( (int) aTypeA | (int) aTypeB );
56}
57
58template<typename T>
60{
61 return CHANGE_TYPE( (int) aTypeA & (int) aTypeB );
62}
63
64
72class COMMIT
73{
74public:
75 COMMIT();
76 virtual ~COMMIT();
77
79 COMMIT& Add( EDA_ITEM* aItem, BASE_SCREEN *aScreen = nullptr )
80 {
81 return Stage( aItem, CHT_ADD, aScreen );
82 }
83
85 COMMIT& Added( EDA_ITEM* aItem, BASE_SCREEN *aScreen = nullptr )
86 {
87 return Stage( aItem, CHT_ADD | CHT_DONE, aScreen );
88 }
89
91 COMMIT& Remove( EDA_ITEM* aItem, BASE_SCREEN *aScreen = nullptr )
92 {
93 return Stage( aItem, CHT_REMOVE, aScreen );
94 }
95
97 COMMIT& Removed( EDA_ITEM* aItem, BASE_SCREEN *aScreen = nullptr )
98 {
99 return Stage( aItem, CHT_REMOVE | CHT_DONE, aScreen );
100 }
101
107 COMMIT& Modify( EDA_ITEM* aItem, BASE_SCREEN *aScreen = nullptr,
108 RECURSE_MODE aRecurse = RECURSE_MODE::NO_RECURSE )
109 {
110 return Stage( aItem, CHT_MODIFY, aScreen, aRecurse );
111 }
112
118 COMMIT& Modified( EDA_ITEM* aItem, EDA_ITEM* aCopy, BASE_SCREEN *aScreen = nullptr )
119 {
120 return createModified( aItem, aCopy, 0, aScreen );
121 }
122
123 template<class Range>
124
125 COMMIT& StageItems( const Range& aRange, CHANGE_TYPE aChangeType )
126 {
127 for( const auto& item : aRange )
128 Stage( item, aChangeType );
129
130 return *this;
131 }
132
134 virtual COMMIT& Stage( EDA_ITEM* aItem, CHANGE_TYPE aChangeType, BASE_SCREEN *aScreen = nullptr,
135 RECURSE_MODE aRecurse = RECURSE_MODE::NO_RECURSE );
136
137 virtual COMMIT& Stage( std::vector<EDA_ITEM*>& container, CHANGE_TYPE aChangeType,
138 BASE_SCREEN *aScreen = nullptr );
139
140 virtual COMMIT& Stage( const PICKED_ITEMS_LIST& aItems,
141 UNDO_REDO aModFlag = UNDO_REDO::UNSPECIFIED,
142 BASE_SCREEN *aScreen = nullptr );
143
144 void Unstage( EDA_ITEM* aItem, BASE_SCREEN* aScreen );
145
147 virtual void Push( const wxString& aMessage = wxT( "A commit" ), int aFlags = 0 ) = 0;
148
150 virtual void Revert() = 0;
151
152 bool Empty() const
153 {
154 return m_changes.empty();
155 }
156
158 int GetStatus( EDA_ITEM* aItem, BASE_SCREEN *aScreen = nullptr );
159
160 EDA_ITEM* GetFirst() const { return m_changes.empty() ? nullptr : m_changes[0].m_item; }
161
162protected:
164 {
170 };
171
173 void clear()
174 {
175 m_changedItems.clear();
176 m_deletedItems.clear();
177 m_changes.clear();
178 }
179
180 COMMIT& createModified( EDA_ITEM* aItem, EDA_ITEM* aCopy, int aExtraFlags = 0,
181 BASE_SCREEN *aScreen = nullptr );
182
183 virtual void makeEntry( EDA_ITEM* aItem, CHANGE_TYPE aType, EDA_ITEM* aCopy = nullptr,
184 BASE_SCREEN *aScreen = nullptr );
185
191 COMMIT_LINE* findEntry( EDA_ITEM* aItem, BASE_SCREEN *aScreen = nullptr );
192
193 virtual EDA_ITEM* parentObject( EDA_ITEM* aItem ) const = 0;
194
195 virtual EDA_ITEM* makeImage( EDA_ITEM* aItem ) const = 0;
196
197 CHANGE_TYPE convert( UNDO_REDO aType ) const;
198 UNDO_REDO convert( CHANGE_TYPE aType ) const;
199
200protected:
201 std::set<EDA_ITEM*> m_changedItems;
202 std::set<EDA_ITEM*> m_deletedItems;
203 std::vector<COMMIT_LINE> m_changes;
204};
205
206#endif
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:73
COMMIT & Remove(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Remove a new item from the model.
Definition: commit.h:91
virtual void Revert()=0
Revert the commit by restoring the modified items state.
COMMIT & Added(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Notify observers that aItem has been added.
Definition: commit.h:85
COMMIT & StageItems(const Range &aRange, CHANGE_TYPE aChangeType)
Definition: commit.h:125
COMMIT & createModified(EDA_ITEM *aItem, EDA_ITEM *aCopy, int aExtraFlags=0, BASE_SCREEN *aScreen=nullptr)
Definition: commit.cpp:150
void Unstage(EDA_ITEM *aItem, BASE_SCREEN *aScreen)
Definition: commit.cpp:122
std::set< EDA_ITEM * > m_deletedItems
Definition: commit.h:202
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
virtual void Push(const wxString &aMessage=wxT("A commit"), int aFlags=0)=0
Execute the changes.
bool Empty() const
Definition: commit.h:152
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:107
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:184
virtual void makeEntry(EDA_ITEM *aItem, CHANGE_TYPE aType, EDA_ITEM *aCopy=nullptr, BASE_SCREEN *aScreen=nullptr)
Definition: commit.cpp:167
COMMIT & Add(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Add a new item to the model.
Definition: commit.h:79
std::set< EDA_ITEM * > m_changedItems
Definition: commit.h:201
EDA_ITEM * GetFirst() const
Definition: commit.h:160
COMMIT & Removed(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Definition: commit.h:97
virtual EDA_ITEM * parentObject(EDA_ITEM *aItem) const =0
virtual EDA_ITEM * makeImage(EDA_ITEM *aItem) const =0
int GetStatus(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Returns status of an item.
Definition: commit.cpp:142
void clear()
Should be called in Push() & Revert() methods.
Definition: commit.h:173
std::vector< COMMIT_LINE > m_changes
Definition: commit.h:203
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
Definition: kiid.h:49
A holder to handle information on schematic or board items.
CHANGE_TYPE operator|(CHANGE_TYPE aTypeA, T aTypeB)
Definition: commit.h:53
CHANGE_TYPE
Types of changes.
Definition: commit.h:42
@ CHT_MODIFY
Definition: commit.h:45
@ CHT_REMOVE
Definition: commit.h:44
@ CHT_DONE
Flag to indicate the change is already applied.
Definition: commit.h:48
@ CHT_TYPE
Definition: commit.h:46
@ CHT_ADD
Definition: commit.h:43
@ CHT_FLAGS
Definition: commit.h:49
CHANGE_TYPE operator&(CHANGE_TYPE aTypeA, T aTypeB)
Definition: commit.h:59
RECURSE_MODE
Definition: eda_item.h:50
KIID & NilUuid()
Definition: kiid.cpp:69
EDA_ITEM * m_copy
Optional copy of the item.
Definition: commit.h:166
CHANGE_TYPE m_type
Modification type.
Definition: commit.h:167
KIID m_parent
Parent item (primarily for undo of deleted items)
Definition: commit.h:168
EDA_ITEM * m_item
Main item that is added/deleted/modified.
Definition: commit.h:165
BASE_SCREEN * m_screen
Definition: commit.h:169
UNDO_REDO
Undo Redo considerations: Basically we have 3 cases New item Deleted item Modified item there is also...