KiCad PCB EDA Suite
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 (C) 2016 CERN
5  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
6  * @author Maciej Suminski <maciej.suminski@cern.ch>
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 #ifndef __COMMIT_H
27 #define __COMMIT_H
28 
29 #include <set>
30 #include <vector>
31 
33 
34 class EDA_ITEM;
35 
38  CHT_ADD = 1,
42 
45  CHT_DONE = 8,
47 };
48 
49 template<typename T>
51 {
52  return CHANGE_TYPE( (int) aTypeA | (int) aTypeB );
53 }
54 
55 template<typename T>
57 {
58  return CHANGE_TYPE( (int) aTypeA & (int) aTypeB );
59 }
60 
61 
71 class COMMIT
72 {
73 public:
74  COMMIT();
75  virtual ~COMMIT();
76 
78  COMMIT& Add( EDA_ITEM* aItem )
79  {
80  return Stage( aItem, CHT_ADD );
81  }
82 
84  COMMIT& Added( EDA_ITEM* aItem )
85  {
86  return Stage( aItem, CHT_ADD | CHT_DONE );
87  }
88 
90  COMMIT& Remove( EDA_ITEM* aItem )
91  {
92  return Stage( aItem, CHT_REMOVE );
93  }
94 
96  COMMIT& Removed( EDA_ITEM* aItem )
97  {
98  return Stage( aItem, CHT_REMOVE | CHT_DONE );
99  }
100 
103  COMMIT& Modify( EDA_ITEM* aItem )
104  {
105  return Stage( aItem, CHT_MODIFY );
106  }
107 
110  COMMIT& Modified( EDA_ITEM* aItem, EDA_ITEM* aCopy );
111 
112  template<class Range>
113  COMMIT& StageItems( const Range& aRange, CHANGE_TYPE aChangeType )
114  {
115  for( const auto& item : aRange )
116  Stage( item, aChangeType );
117 
118  return *this;
119  }
120 
121 
123  COMMIT& Stage( EDA_ITEM* aItem, CHANGE_TYPE aChangeType );
124 
125  COMMIT& Stage( std::vector<EDA_ITEM*>& container, CHANGE_TYPE aChangeType );
126 
127  COMMIT& Stage( const PICKED_ITEMS_LIST& aItems, UNDO_REDO_T aModFlag = UR_UNSPECIFIED );
128 
130  virtual void Push( const wxString& aMessage = wxT( "A commit" ), bool aCreateUndoEntry = true ) = 0;
131 
133  virtual void Revert() = 0;
134 
135  bool Empty() const
136  {
137  return m_changes.empty();
138  }
139 
140 protected:
141  struct COMMIT_LINE
142  {
145 
148 
151  };
152 
153  // Should be called in Push() & Revert() methods
154  void clear()
155  {
156  m_changedItems.clear();
157  m_changes.clear();
158  }
159 
160  virtual void makeEntry( EDA_ITEM* aItem, CHANGE_TYPE aType, EDA_ITEM* aCopy = NULL );
161 
162  virtual EDA_ITEM* parentObject( EDA_ITEM* aItem ) const = 0;
163 
164  CHANGE_TYPE convert( UNDO_REDO_T aType ) const;
165 
166  std::set<EDA_ITEM*> m_changedItems;
167  std::vector<COMMIT_LINE> m_changes;
168 };
169 
170 #endif
virtual void Revert()=0
Revertes the commit by restoring the modifed items state.
COMMIT & Modify(EDA_ITEM *aItem)
Modifies a given item in the model.
Definition: commit.h:103
COMMIT & Stage(EDA_ITEM *aItem, CHANGE_TYPE aChangeType)
Adds a change of the item aItem of type aChangeType to the change list.
Definition: commit.cpp:44
COMMIT & Added(EDA_ITEM *aItem)
Notifies observers that aItem has been added
Definition: commit.h:84
void clear()
Definition: commit.h:154
COMMIT & Modified(EDA_ITEM *aItem, EDA_ITEM *aCopy)
Creates an undo entry for an item that has been already modified.
Definition: commit.cpp:83
T
enum T contains all this lexer's tokens.
COMMIT & Add(EDA_ITEM *aItem)
Adds a new item to the model
Definition: commit.h:78
CHANGE_TYPE m_type
Modification type
Definition: commit.h:150
Definition: commit.h:38
Flag to indicate the change is already applied, just notify observers (not compatible with CHT_MODIFY...
Definition: commit.h:45
Class COMMIT.
Definition: commit.h:71
EDA_ITEM * m_item
Main item that is added/deleted/modified
Definition: commit.h:144
CHANGE_TYPE convert(UNDO_REDO_T aType) const
Definition: commit.cpp:154
virtual void Push(const wxString &aMessage=wxT("A commit"), bool aCreateUndoEntry=true)=0
Executes the changes.
bool Empty() const
Definition: commit.h:135
CHANGE_TYPE operator|(CHANGE_TYPE aTypeA, T aTypeB)
Definition: commit.h:50
COMMIT & Removed(EDA_ITEM *aItem)
Notifies observers that aItem has been removed
Definition: commit.h:96
UNDO_REDO_T
Undo Redo considerations: Basically we have 3 cases New item Deleted item Modified item there is also...
CHANGE_TYPE operator&(CHANGE_TYPE aTypeA, T aTypeB)
Definition: commit.h:56
COMMIT & StageItems(const Range &aRange, CHANGE_TYPE aChangeType)
Definition: commit.h:113
virtual ~COMMIT()
Definition: commit.cpp:34
std::set< EDA_ITEM * > m_changedItems
Definition: commit.h:166
COMMIT & Remove(EDA_ITEM *aItem)
Removes a new item from the model
Definition: commit.h:90
Class PICKED_ITEMS_LIST is a holder to handle information on schematic or board items.
CHANGE_TYPE
Types of changes
Definition: commit.h:37
std::vector< COMMIT_LINE > m_changes
Definition: commit.h:167
COMMIT()
Definition: commit.cpp:29
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:151
EDA_ITEM * m_copy
Optional copy of the item
Definition: commit.h:147
virtual EDA_ITEM * parentObject(EDA_ITEM *aItem) const =0
virtual void makeEntry(EDA_ITEM *aItem, CHANGE_TYPE aType, EDA_ITEM *aCopy=NULL)
Definition: commit.cpp:138