You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
5.2 KiB

9 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright 2016-2017 CERN
  5. * Copyright (C) 2020-2021 KiCad Developers, see change_log.txt for contributors.
  6. *
  7. * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
  8. * @author Maciej Suminski <maciej.suminski@cern.ch>
  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. #ifndef __COMMIT_H
  28. #define __COMMIT_H
  29. #include <set>
  30. #include <vector>
  31. #include <wx/string.h>
  32. #include <undo_redo_container.h>
  33. class EDA_ITEM;
  34. ///< Types of changes
  35. enum CHANGE_TYPE {
  36. CHT_ADD = 1,
  37. CHT_REMOVE = 2,
  38. CHT_MODIFY = 4,
  39. CHT_TYPE = CHT_ADD | CHT_REMOVE | CHT_MODIFY,
  40. ///< Flag to indicate the change is already applied,
  41. ///< just notify observers (not compatible with CHT_MODIFY)
  42. CHT_DONE = 8,
  43. CHT_FLAGS = CHT_DONE
  44. };
  45. template<typename T>
  46. CHANGE_TYPE operator|( CHANGE_TYPE aTypeA, T aTypeB )
  47. {
  48. return CHANGE_TYPE( (int) aTypeA | (int) aTypeB );
  49. }
  50. template<typename T>
  51. CHANGE_TYPE operator&( CHANGE_TYPE aTypeA, T aTypeB )
  52. {
  53. return CHANGE_TYPE( (int) aTypeA & (int) aTypeB );
  54. }
  55. /**
  56. * Represent a set of changes (additions, deletions or modifications) of a data model
  57. * (e.g. the BOARD) class.
  58. *
  59. * The class can be used to propagate changes to subscribed objects (e.g. views, ratsnest),
  60. * and automatically create undo/redo points.
  61. */
  62. class COMMIT
  63. {
  64. public:
  65. COMMIT();
  66. virtual ~COMMIT();
  67. ///< Add a new item to the model
  68. COMMIT& Add( EDA_ITEM* aItem )
  69. {
  70. return Stage( aItem, CHT_ADD );
  71. }
  72. ///< Notify observers that aItem has been added
  73. COMMIT& Added( EDA_ITEM* aItem )
  74. {
  75. return Stage( aItem, CHT_ADD | CHT_DONE );
  76. }
  77. ///< Remove a new item from the model
  78. COMMIT& Remove( EDA_ITEM* aItem )
  79. {
  80. return Stage( aItem, CHT_REMOVE );
  81. }
  82. ///< Notify observers that aItem has been removed
  83. COMMIT& Removed( EDA_ITEM* aItem )
  84. {
  85. return Stage( aItem, CHT_REMOVE | CHT_DONE );
  86. }
  87. ///< Modify a given item in the model.
  88. ///< Must be called before modification is performed.
  89. COMMIT& Modify( EDA_ITEM* aItem )
  90. {
  91. return Stage( aItem, CHT_MODIFY );
  92. }
  93. ///< Create an undo entry for an item that has been already modified. Requires a copy done
  94. ///< before the modification.
  95. COMMIT& Modified( EDA_ITEM* aItem, EDA_ITEM* aCopy )
  96. {
  97. return createModified( aItem, aCopy );
  98. }
  99. template<class Range>
  100. COMMIT& StageItems( const Range& aRange, CHANGE_TYPE aChangeType )
  101. {
  102. for( const auto& item : aRange )
  103. Stage( item, aChangeType );
  104. return *this;
  105. }
  106. ///< Add a change of the item aItem of type aChangeType to the change list.
  107. virtual COMMIT& Stage( EDA_ITEM* aItem, CHANGE_TYPE aChangeType );
  108. virtual COMMIT& Stage( std::vector<EDA_ITEM*>& container, CHANGE_TYPE aChangeType );
  109. virtual COMMIT& Stage( const PICKED_ITEMS_LIST& aItems,
  110. UNDO_REDO aModFlag = UNDO_REDO::UNSPECIFIED );
  111. ///< Execute the changes.
  112. virtual void Push( const wxString& aMessage = wxT( "A commit" ),
  113. bool aCreateUndoEntry = true, bool aSetDirtyBit = true ) = 0;
  114. ///< Revert the commit by restoring the modified items state.
  115. virtual void Revert() = 0;
  116. bool Empty() const
  117. {
  118. return m_changes.empty();
  119. }
  120. ///< Returns status of an item.
  121. int GetStatus( EDA_ITEM* aItem );
  122. protected:
  123. struct COMMIT_LINE
  124. {
  125. EDA_ITEM* m_item; ///< Main item that is added/deleted/modified
  126. EDA_ITEM* m_copy; ///< Optional copy of the item
  127. CHANGE_TYPE m_type; ///< Modification type
  128. };
  129. // Should be called in Push() & Revert() methods
  130. void clear()
  131. {
  132. m_changedItems.clear();
  133. m_changes.clear();
  134. }
  135. COMMIT& createModified( EDA_ITEM* aItem, EDA_ITEM* aCopy, int aExtraFlags = 0 );
  136. virtual void makeEntry( EDA_ITEM* aItem, CHANGE_TYPE aType, EDA_ITEM* aCopy = nullptr );
  137. /**
  138. * Search for an entry describing change for a particular item.
  139. *
  140. * @return null if there is no related entry.
  141. */
  142. COMMIT_LINE* findEntry( EDA_ITEM* aItem );
  143. virtual EDA_ITEM* parentObject( EDA_ITEM* aItem ) const = 0;
  144. CHANGE_TYPE convert( UNDO_REDO aType ) const;
  145. std::set<EDA_ITEM*> m_changedItems;
  146. std::vector<COMMIT_LINE> m_changes;
  147. };
  148. #endif