KiCad PCB EDA Suite
COMMIT Class Referenceabstract

Represent a set of changes (additions, deletions or modifications) of a data model (e.g. More...

#include <commit.h>

Inheritance diagram for COMMIT:
BOARD_COMMIT

Classes

struct  COMMIT_LINE
 

Public Member Functions

 COMMIT ()
 
virtual ~COMMIT ()
 Add a new item to the model. More...
 
COMMITAdd (EDA_ITEM *aItem)
 Notify observers that aItem has been added. More...
 
COMMITAdded (EDA_ITEM *aItem)
 Remove a new item from the model. More...
 
COMMITRemove (EDA_ITEM *aItem)
 Notify observers that aItem has been removed. More...
 
COMMITRemoved (EDA_ITEM *aItem)
 Modify a given item in the model. More...
 
COMMITModify (EDA_ITEM *aItem)
 Create an undo entry for an item that has been already modified. More...
 
COMMITModified (EDA_ITEM *aItem, EDA_ITEM *aCopy)
 
template<class Range >
COMMITStageItems (const Range &aRange, CHANGE_TYPE aChangeType)
 Add a change of the item aItem of type aChangeType to the change list. More...
 
virtual COMMITStage (EDA_ITEM *aItem, CHANGE_TYPE aChangeType)
 
virtual COMMITStage (std::vector< EDA_ITEM * > &container, CHANGE_TYPE aChangeType)
 
virtual COMMITStage (const PICKED_ITEMS_LIST &aItems, UNDO_REDO aModFlag=UNDO_REDO::UNSPECIFIED)
 Execute the changes. More...
 
virtual void Push (const wxString &aMessage=wxT("A commit"), bool aCreateUndoEntry=true, bool aSetDirtyBit=true)=0
 Revert the commit by restoring the modified items state. More...
 
virtual void Revert ()=0
 
bool Empty () const
 Returns status of an item. More...
 
int GetStatus (EDA_ITEM *aItem)
 

Protected Member Functions

void clear ()
 
COMMITcreateModified (EDA_ITEM *aItem, EDA_ITEM *aCopy, int aExtraFlags=0)
 
virtual void makeEntry (EDA_ITEM *aItem, CHANGE_TYPE aType, EDA_ITEM *aCopy=nullptr)
 
COMMIT_LINEfindEntry (EDA_ITEM *aItem)
 Search for an entry describing change for a particular item. More...
 
virtual EDA_ITEMparentObject (EDA_ITEM *aItem) const =0
 
CHANGE_TYPE convert (UNDO_REDO aType) const
 

Protected Attributes

std::set< EDA_ITEM * > m_changedItems
 
std::vector< COMMIT_LINEm_changes
 

Detailed Description

Represent a set of changes (additions, deletions or modifications) of a data model (e.g.

the BOARD) class.

The class can be used to propagate changes to subscribed objects (e.g. views, ratsnest), and automatically create undo/redo points.

Definition at line 71 of file commit.h.

Constructor & Destructor Documentation

◆ COMMIT()

COMMIT::COMMIT ( )

Definition at line 34 of file commit.cpp.

35 {
36 }

◆ ~COMMIT()

COMMIT::~COMMIT ( )
virtual

Add a new item to the model.

Definition at line 39 of file commit.cpp.

40 {
41  for( COMMIT_LINE& ent : m_changes )
42  {
43  if( ent.m_copy )
44  delete ent.m_copy;
45  }
46 }
std::vector< COMMIT_LINE > m_changes
Definition: commit.h:179

References m_changes.

Member Function Documentation

◆ Add()

◆ Added()

COMMIT& COMMIT::Added ( EDA_ITEM aItem)
inline

Remove a new item from the model.

Definition at line 84 of file commit.h.

References CHT_ADD, CHT_DONE, and Stage().

Referenced by PCB_EDIT_FRAME::RunActionPlugin().

◆ clear()

void COMMIT::clear ( )
inlineprotected

Definition at line 157 of file commit.h.

158  {
159  m_changedItems.clear();
160  m_changes.clear();
161  }
std::set< EDA_ITEM * > m_changedItems
Definition: commit.h:178
std::vector< COMMIT_LINE > m_changes
Definition: commit.h:179

References m_changedItems, and m_changes.

Referenced by BOARD_COMMIT::Push(), and BOARD_COMMIT::Revert().

◆ convert()

CHANGE_TYPE COMMIT::convert ( UNDO_REDO  aType) const
protected

Definition at line 203 of file commit.cpp.

204 {
205  switch( aType )
206  {
207  case UNDO_REDO::NEWITEM:
208  return CHT_ADD;
209 
210  case UNDO_REDO::DELETED:
211  return CHT_REMOVE;
212 
213  default:
214  assert( false );
216 
217  case UNDO_REDO::CHANGED:
218  return CHT_MODIFY;
219  }
220 }
Definition: commit.h:40
#define KI_FALLTHROUGH
The KI_FALLTHROUGH macro is to be used when switch statement cases should purposely fallthrough from ...
Definition: macros.h:83

References CHANGED, CHT_ADD, CHT_MODIFY, CHT_REMOVE, DELETED, KI_FALLTHROUGH, and NEWITEM.

Referenced by Stage().

◆ createModified()

COMMIT & COMMIT::createModified ( EDA_ITEM aItem,
EDA_ITEM aCopy,
int  aExtraFlags = 0 
)
protected

Definition at line 150 of file commit.cpp.

151 {
152  EDA_ITEM* parent = parentObject( aItem );
153  auto entryIt = m_changedItems.find( parent );
154 
155  if( entryIt != m_changedItems.end() )
156  {
157  delete aCopy;
158  return *this; // item has been already modified once
159  }
160 
161  makeEntry( parent, CHT_MODIFY | aExtraFlags, aCopy );
162 
163  return *this;
164 }
virtual void makeEntry(EDA_ITEM *aItem, CHANGE_TYPE aType, EDA_ITEM *aCopy=nullptr)
Definition: commit.cpp:167
std::set< EDA_ITEM * > m_changedItems
Definition: commit.h:178
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:100
virtual EDA_ITEM * parentObject(EDA_ITEM *aItem) const =0

References CHT_MODIFY, m_changedItems, makeEntry(), and parentObject().

Referenced by Modified(), and Stage().

◆ Empty()

bool COMMIT::Empty ( ) const
inline

Returns status of an item.

Definition at line 140 of file commit.h.

References m_changes.

Referenced by DIALOG_CLEANUP_GRAPHICS::doCleanup(), DIALOG_CLEANUP_TRACKS_AND_VIAS::doCleanup(), and BOARD_COMMIT::Push().

◆ findEntry()

COMMIT::COMMIT_LINE * COMMIT::findEntry ( EDA_ITEM aItem)
protected

Search for an entry describing change for a particular item.

Returns
null if there is no related entry.

Definition at line 191 of file commit.cpp.

192 {
193  for( COMMIT_LINE& change : m_changes )
194  {
195  if( change.m_item == aItem )
196  return &change;
197  }
198 
199  return nullptr;
200 }
std::vector< COMMIT_LINE > m_changes
Definition: commit.h:179

References m_changes.

Referenced by GetStatus().

◆ GetStatus()

int COMMIT::GetStatus ( EDA_ITEM aItem)

Definition at line 132 of file commit.cpp.

133 {
134  COMMIT_LINE* entry = findEntry( parentObject( aItem ) );
135 
136  return entry ? entry->m_type : 0;
137 }
COMMIT_LINE * findEntry(EDA_ITEM *aItem)
Search for an entry describing change for a particular item.
Definition: commit.cpp:191
virtual EDA_ITEM * parentObject(EDA_ITEM *aItem) const =0

References findEntry(), COMMIT::COMMIT_LINE::m_type, and parentObject().

Referenced by BOARD_NETLIST_UPDATER::updateComponentPadConnections(), and BOARD_NETLIST_UPDATER::updateFootprintParameters().

◆ makeEntry()

void COMMIT::makeEntry ( EDA_ITEM aItem,
CHANGE_TYPE  aType,
EDA_ITEM aCopy = nullptr 
)
protectedvirtual

Definition at line 167 of file commit.cpp.

168 {
169  // Expect an item copy if it is going to be modified
170  wxASSERT( !!aCopy == ( ( aType & CHT_TYPE ) == CHT_MODIFY ) );
171 
172  if( m_changedItems.find( aItem ) != m_changedItems.end() )
173  {
174  eraseIf( m_changes, [aItem] ( const COMMIT_LINE& aEnt )
175  {
176  return aEnt.m_item == aItem;
177  } );
178  }
179 
180  COMMIT_LINE ent;
181 
182  ent.m_item = aItem;
183  ent.m_type = aType;
184  ent.m_copy = aCopy;
185 
186  m_changedItems.insert( aItem );
187  m_changes.push_back( ent );
188 }
Flag to indicate the change is already applied, just notify observers (not compatible with CHT_MODIFY...
Definition: commit.h:43
void eraseIf(Container &c, F &&f)
Definition: commit.cpp:141
std::set< EDA_ITEM * > m_changedItems
Definition: commit.h:178
std::vector< COMMIT_LINE > m_changes
Definition: commit.h:179

References CHT_MODIFY, CHT_TYPE, eraseIf(), m_changedItems, m_changes, COMMIT::COMMIT_LINE::m_copy, COMMIT::COMMIT_LINE::m_item, and COMMIT::COMMIT_LINE::m_type.

Referenced by createModified(), and Stage().

◆ Modified()

COMMIT& COMMIT::Modified ( EDA_ITEM aItem,
EDA_ITEM aCopy 
)
inline

Definition at line 110 of file commit.h.

111  {
112  return createModified( aItem, aCopy );
113  }
COMMIT & createModified(EDA_ITEM *aItem, EDA_ITEM *aCopy, int aExtraFlags=0)
Definition: commit.cpp:150

References createModified().

Referenced by Stage(), BOARD_NETLIST_UPDATER::updateComponentPadConnections(), and BOARD_NETLIST_UPDATER::updateFootprintParameters().

◆ Modify()

COMMIT& COMMIT::Modify ( EDA_ITEM aItem)
inline

Create an undo entry for an item that has been already modified.

Requires a copy done before the modification.

Definition at line 103 of file commit.h.

References CHT_MODIFY, and Stage().

Referenced by PCB_POINT_EDITOR::addCorner(), AR_AUTOPLACER::AutoplaceFootprints(), doPushPadProperties(), DRAWING_TOOL::DrawVia(), PAD_TOOL::EnumeratePads(), PAD_TOOL::explodePad(), ZONE_FILLER::Fill(), ARRAY_CREATOR::Invoke(), TRACKS_CLEANER::mergeCollinearSegments(), mergeZones(), BOARD_EDITOR_CONTROL::modifyLockSelected(), FOOTPRINT_EDIT_FRAME::OnEditItemRequest(), PAD_TOOL::pastePadProperties(), DIALOG_GLOBAL_EDIT_TEXT_AND_GRAPHICS::processItem(), CN_CONNECTIVITY_ALGO::propagateConnections(), DIALOG_BOARD_REANNOTATE::ReannotateBoard(), PAD_TOOL::recombinePad(), PCB_POINT_EDITOR::removeCorner(), GROUP_TOOL::RemoveFromGroup(), DRAWING_TOOL::SetAnchor(), BOARD_EDITOR_CONTROL::TrackWidthDec(), BOARD_EDITOR_CONTROL::TrackWidthInc(), DIALOG_TRACK_VIA_PROPERTIES::TransferDataFromWindow(), DIALOG_UNUSED_PAD_LAYERS::TransferDataFromWindow(), DIALOG_DIMENSION_PROPERTIES::TransferDataFromWindow(), DIALOG_GROUP_PROPERTIES::TransferDataFromWindow(), DIALOG_TARGET_PROPERTIES::TransferDataFromWindow(), DIALOG_FOOTPRINT_PROPERTIES_FP_EDITOR::TransferDataFromWindow(), DIALOG_TEXT_PROPERTIES::TransferDataFromWindow(), DIALOG_FOOTPRINT_PROPERTIES::TransferDataFromWindow(), DIALOG_GRAPHIC_ITEM_PROPERTIES::TransferDataFromWindow(), DIALOG_PAD_PROPERTIES::TransferDataFromWindow(), BOARD_NETLIST_UPDATER::updateCopperZoneNets(), BOARD_EDITOR_CONTROL::ViaSizeDec(), BOARD_EDITOR_CONTROL::ViaSizeInc(), ZONE_FILLER_TOOL::ZoneUnfill(), and ZONE_FILLER_TOOL::ZoneUnfillAll().

◆ parentObject()

virtual EDA_ITEM* COMMIT::parentObject ( EDA_ITEM aItem) const
protectedpure virtual

Implemented in BOARD_COMMIT.

Referenced by createModified(), GetStatus(), and Stage().

◆ Push()

virtual void COMMIT::Push ( const wxString &  aMessage = wxT("A commit"),
bool  aCreateUndoEntry = true,
bool  aSetDirtyBit = true 
)
pure virtual

Revert the commit by restoring the modified items state.

Implemented in BOARD_COMMIT.

Referenced by DIALOG_TRACK_VIA_PROPERTIES::TransferDataFromWindow(), and DIALOG_UNUSED_PAD_LAYERS::TransferDataFromWindow().

◆ Remove()

◆ Removed()

COMMIT& COMMIT::Removed ( EDA_ITEM aItem)
inline

Modify a given item in the model.

Must be called before modification is performed.

Definition at line 96 of file commit.h.

References CHT_DONE, CHT_REMOVE, and Stage().

Referenced by TRACKS_CLEANER::deleteDanglingTracks(), TRACKS_CLEANER::mergeCollinearSegments(), TRACKS_CLEANER::removeItems(), PCB_EDIT_FRAME::RunActionPlugin(), and BOARD_NETLIST_UPDATER::UpdateNetlist().

◆ Revert()

virtual void COMMIT::Revert ( )
pure virtual

Implemented in BOARD_COMMIT.

◆ Stage() [1/3]

COMMIT & COMMIT::Stage ( EDA_ITEM aItem,
CHANGE_TYPE  aChangeType 
)
virtual

Reimplemented in BOARD_COMMIT.

Definition at line 49 of file commit.cpp.

50 {
51  // CHT_MODIFY and CHT_DONE are not compatible
52  assert( ( aChangeType & ( CHT_MODIFY | CHT_DONE ) ) != ( CHT_MODIFY | CHT_DONE ) );
53 
54  int flag = aChangeType & CHT_FLAGS;
55 
56  switch( aChangeType & CHT_TYPE )
57  {
58  case CHT_ADD:
59  assert( m_changedItems.find( aItem ) == m_changedItems.end() );
60  makeEntry( aItem, CHT_ADD | flag );
61  return *this;
62 
63  case CHT_REMOVE:
64  makeEntry( aItem, CHT_REMOVE | flag );
65  return *this;
66 
67  case CHT_MODIFY:
68  {
69  EDA_ITEM* parent = parentObject( aItem );
70  EDA_ITEM* clone = nullptr;
71 
72  assert( parent );
73 
74  if( parent )
75  clone = parent->Clone();
76 
77  assert( clone );
78 
79  if( clone )
80  return createModified( parent, clone, flag );
81 
82  break;
83  }
84 
85  default:
86  assert( false );
87  }
88 
89  return *this;
90 }
Flag to indicate the change is already applied, just notify observers (not compatible with CHT_MODIFY...
Definition: commit.h:43
Definition: commit.h:40
virtual void makeEntry(EDA_ITEM *aItem, CHANGE_TYPE aType, EDA_ITEM *aCopy=nullptr)
Definition: commit.cpp:167
virtual EDA_ITEM * Clone() const
Create a duplicate of this item with linked list members set to NULL.
Definition: eda_item.cpp:83
std::set< EDA_ITEM * > m_changedItems
Definition: commit.h:178
COMMIT & createModified(EDA_ITEM *aItem, EDA_ITEM *aCopy, int aExtraFlags=0)
Definition: commit.cpp:150
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:100
virtual EDA_ITEM * parentObject(EDA_ITEM *aItem) const =0

References CHT_ADD, CHT_DONE, CHT_FLAGS, CHT_MODIFY, CHT_REMOVE, CHT_TYPE, EDA_ITEM::Clone(), createModified(), flag, m_changedItems, makeEntry(), and parentObject().

Referenced by Add(), Added(), Modify(), Remove(), Removed(), BOARD_COMMIT::Stage(), Stage(), and StageItems().

◆ Stage() [2/3]

COMMIT & COMMIT::Stage ( std::vector< EDA_ITEM * > &  container,
CHANGE_TYPE  aChangeType 
)
virtual

Reimplemented in BOARD_COMMIT.

Definition at line 93 of file commit.cpp.

94 {
95  for( EDA_ITEM* item : container )
96  {
97  Stage( item, aChangeType );
98  }
99 
100  return *this;
101 }
virtual COMMIT & Stage(EDA_ITEM *aItem, CHANGE_TYPE aChangeType)
Definition: commit.cpp:49
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:100

References Stage().

◆ Stage() [3/3]

COMMIT & COMMIT::Stage ( const PICKED_ITEMS_LIST aItems,
UNDO_REDO  aModFlag = UNDO_REDO::UNSPECIFIED 
)
virtual

Execute the changes.

Reimplemented in BOARD_COMMIT.

Definition at line 104 of file commit.cpp.

105 {
106  for( unsigned int i = 0; i < aItems.GetCount(); i++ )
107  {
108  UNDO_REDO change_type = aItems.GetPickedItemStatus( i );
109  EDA_ITEM* item = aItems.GetPickedItem( i );
110  EDA_ITEM* copy = nullptr;
111 
112  if( change_type == UNDO_REDO::UNSPECIFIED )
113  change_type = aModFlag;
114 
115  if( ( copy = aItems.GetPickedItemLink( i ) ) )
116  {
117  assert( change_type == UNDO_REDO::CHANGED );
118 
119  // There was already a copy created, so use it
120  Modified( item, copy );
121  }
122  else
123  {
124  Stage( item, convert( change_type ) );
125  }
126  }
127 
128  return *this;
129 }
EDA_ITEM * GetPickedItemLink(unsigned int aIdx) const
virtual COMMIT & Stage(EDA_ITEM *aItem, CHANGE_TYPE aChangeType)
Definition: commit.cpp:49
UNDO_REDO
Undo Redo considerations: Basically we have 3 cases New item Deleted item Modified item there is also...
unsigned GetCount() const
EDA_ITEM * GetPickedItem(unsigned int aIdx) const
COMMIT & Modified(EDA_ITEM *aItem, EDA_ITEM *aCopy)
Definition: commit.h:110
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:100
UNDO_REDO GetPickedItemStatus(unsigned int aIdx) const
CHANGE_TYPE convert(UNDO_REDO aType) const
Definition: commit.cpp:203

References CHANGED, convert(), copy, PICKED_ITEMS_LIST::GetCount(), PICKED_ITEMS_LIST::GetPickedItem(), PICKED_ITEMS_LIST::GetPickedItemLink(), PICKED_ITEMS_LIST::GetPickedItemStatus(), Modified(), Stage(), and UNSPECIFIED.

◆ StageItems()

template<class Range >
COMMIT& COMMIT::StageItems ( const Range &  aRange,
CHANGE_TYPE  aChangeType 
)
inline

Add a change of the item aItem of type aChangeType to the change list.

Definition at line 117 of file commit.h.

References Stage().

Referenced by PCB_POINT_EDITOR::OnSelectionChange().

Member Data Documentation

◆ m_changedItems

std::set<EDA_ITEM*> COMMIT::m_changedItems
protected

Definition at line 178 of file commit.h.

Referenced by clear(), createModified(), makeEntry(), and Stage().

◆ m_changes

std::vector<COMMIT_LINE> COMMIT::m_changes
protected

The documentation for this class was generated from the following files: