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 31 of file commit.cpp.

32 {
33 }

◆ ~COMMIT()

COMMIT::~COMMIT ( )
virtual

Add a new item to the model.

Definition at line 36 of file commit.cpp.

37 {
38  for( COMMIT_LINE& ent : m_changes )
39  {
40  if( ent.m_copy )
41  delete ent.m_copy;
42  }
43 }
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().

◆ 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 199 of file commit.cpp.

200 {
201  switch( aType )
202  {
203  case UNDO_REDO::NEWITEM:
204  return CHT_ADD;
205 
206  case UNDO_REDO::DELETED:
207  return CHT_REMOVE;
208 
209  default:
210  assert( false );
211  // fall through
212 
213  case UNDO_REDO::CHANGED:
214  return CHT_MODIFY;
215  }
216 }
Definition: commit.h:40

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

Referenced by Stage().

◆ createModified()

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

Definition at line 147 of file commit.cpp.

148 {
149  EDA_ITEM* parent = parentObject( aItem );
150  auto entryIt = m_changedItems.find( parent );
151 
152  if( entryIt != m_changedItems.end() )
153  {
154  delete aCopy;
155  return *this; // item has been already modified once
156  }
157 
158  makeEntry( parent, CHT_MODIFY | aExtraFlags, aCopy );
159 
160  return *this;
161 }
virtual void makeEntry(EDA_ITEM *aItem, CHANGE_TYPE aType, EDA_ITEM *aCopy=nullptr)
Definition: commit.cpp:164
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:150
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 187 of file commit.cpp.

188 {
189  for( COMMIT_LINE& change : m_changes )
190  {
191  if( change.m_item == aItem )
192  return &change;
193  }
194 
195  return nullptr;
196 }
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 129 of file commit.cpp.

130 {
131  COMMIT_LINE* entry = findEntry( parentObject( aItem ) );
132 
133  return entry ? entry->m_type : 0;
134 }
COMMIT_LINE * findEntry(EDA_ITEM *aItem)
Search for an entry describing change for a particular item.
Definition: commit.cpp:187
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 164 of file commit.cpp.

165 {
166  // Expect an item copy if it is going to be modified
167  wxASSERT( !!aCopy == ( ( aType & CHT_TYPE ) == CHT_MODIFY ) );
168 
169  if( m_changedItems.find( aItem ) != m_changedItems.end() )
170  {
171  eraseIf( m_changes, [aItem] ( const COMMIT_LINE& aEnt ) {
172  return aEnt.m_item == aItem;
173  } );
174  }
175 
176  COMMIT_LINE ent;
177 
178  ent.m_item = aItem;
179  ent.m_type = aType;
180  ent.m_copy = aCopy;
181 
182  m_changedItems.insert( aItem );
183  m_changes.push_back( ent );
184 }
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:138
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:147

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_TARGET_PROPERTIES::TransferDataFromWindow(), DIALOG_GROUP_PROPERTIES::TransferDataFromWindow(), DIALOG_GRAPHIC_ITEM_PROPERTIES::TransferDataFromWindow(), DIALOG_TEXT_PROPERTIES::TransferDataFromWindow(), DIALOG_FOOTPRINT_PROPERTIES_FP_EDITOR::TransferDataFromWindow(), DIALOG_FOOTPRINT_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 GRAPHICS_CLEANER::cleanupSegments(), TRACKS_CLEANER::deleteDanglingTracks(), TRACKS_CLEANER::mergeCollinearSegments(), TRACKS_CLEANER::removeItems(), 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 46 of file commit.cpp.

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

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

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 101 of file commit.cpp.

102 {
103  for( unsigned int i = 0; i < aItems.GetCount(); i++ )
104  {
105  UNDO_REDO change_type = aItems.GetPickedItemStatus( i );
106  EDA_ITEM* item = aItems.GetPickedItem( i );
107  EDA_ITEM* copy = NULL;
108 
109  if( change_type == UNDO_REDO::UNSPECIFIED )
110  change_type = aModFlag;
111 
112  if( ( copy = aItems.GetPickedItemLink( i ) ) )
113  {
114  assert( change_type == UNDO_REDO::CHANGED );
115 
116  // There was already a copy created, so use it
117  Modified( item, copy );
118  }
119  else
120  {
121  Stage( item, convert( change_type ) );
122  }
123  }
124 
125  return *this;
126 }
EDA_ITEM * GetPickedItemLink(unsigned int aIdx) const
virtual COMMIT & Stage(EDA_ITEM *aItem, CHANGE_TYPE aChangeType)
Definition: commit.cpp:46
UNDO_REDO
Undo Redo considerations: Basically we have 3 cases New item Deleted item Modified item there is also...
unsigned GetCount() const
#define NULL
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:150
UNDO_REDO GetPickedItemStatus(unsigned int aIdx) const
CHANGE_TYPE convert(UNDO_REDO aType) const
Definition: commit.cpp:199

References CHANGED, convert(), copy, PICKED_ITEMS_LIST::GetCount(), PICKED_ITEMS_LIST::GetPickedItem(), PICKED_ITEMS_LIST::GetPickedItemLink(), PICKED_ITEMS_LIST::GetPickedItemStatus(), Modified(), NULL, 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: