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.

305 lines
10 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2009 jean-pierre.charras@gipsa-lab.inpg.fr
  5. * Copyright (C) 2011 Wayne Stambaugh <stambaughw@gmail.com>
  6. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  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. #ifndef _CLASS_UNDOREDO_CONTAINER_H
  26. #define _CLASS_UNDOREDO_CONTAINER_H
  27. #include <core/typeinfo.h>
  28. #include <eda_item_flags.h>
  29. #include <functional>
  30. #include <kiid.h>
  31. #include <vector>
  32. #include <wx/string.h>
  33. class EDA_ITEM;
  34. class PICKED_ITEMS_LIST;
  35. class BASE_SCREEN;
  36. /**
  37. * Undo Redo considerations:
  38. * Basically we have 3 cases
  39. * New item
  40. * Deleted item
  41. * Modified item
  42. * there is also a specific case in Eeschema, when wires are modified
  43. * If an item is modified, a copy of the "old" item parameters value is held.
  44. * When an item is deleted or added (new item) the pointer points the item, and there is
  45. * no other copy.
  46. */
  47. /**
  48. * Type of undo/redo operations.
  49. *
  50. * Each type must be redo/undone by a specific operation.
  51. */
  52. enum class UNDO_REDO {
  53. UNSPECIFIED = 0, // illegal
  54. CHANGED, // params of items have a value changed: undo is made by exchange
  55. // values with a copy of these values
  56. NEWITEM, // new item, undo by changing in deleted
  57. DELETED, // deleted item, undo by changing in deleted
  58. LIBEDIT, // Specific to the component editor (symbol_editor creates a full copy
  59. // of the current component when changed)
  60. LIB_RENAME, // As LIBEDIT, but old copy should be removed from library
  61. DRILLORIGIN, // origin changed (like CHANGED, contains the origin and a copy)
  62. GRIDORIGIN, // origin changed (like CHANGED, contains the origin and a copy)
  63. PAGESETTINGS, // page settings or title block changes
  64. REPEAT_ITEM // storage entry for the editor's global repeatItems list
  65. };
  66. class ITEM_PICKER
  67. {
  68. public:
  69. ITEM_PICKER();
  70. ITEM_PICKER( BASE_SCREEN* aScreen, EDA_ITEM* aItem,
  71. UNDO_REDO aStatus = UNDO_REDO::UNSPECIFIED );
  72. EDA_ITEM* GetItem() const { return m_pickedItem; }
  73. void SetItem( EDA_ITEM* aItem );
  74. void SetStatus( UNDO_REDO aStatus ) { m_undoRedoStatus = aStatus; }
  75. UNDO_REDO GetStatus() const { return m_undoRedoStatus; }
  76. void SetFlags( EDA_ITEM_FLAGS aFlags ) { m_pickerFlags = aFlags; }
  77. EDA_ITEM_FLAGS GetFlags() const { return m_pickerFlags; }
  78. void SetLink( EDA_ITEM* aItem );
  79. EDA_ITEM* GetLink() const { return m_link; }
  80. KIID GetGroupId() const { return m_groupId; }
  81. KIID_VECT_LIST GetGroupMembers() const { return m_groupMembers; }
  82. BASE_SCREEN* GetScreen() const { return m_screen; }
  83. private:
  84. EDA_ITEM_FLAGS m_pickerFlags; /* A copy of m_flags member. Currently used only to flag
  85. * transient items. */
  86. UNDO_REDO m_undoRedoStatus; /* type of operation to undo/redo for this item */
  87. EDA_ITEM* m_pickedItem; /* Pointer on the schematic or board item that is concerned
  88. * (picked), or in undo redo commands, the copy of an
  89. * edited item. */
  90. KICAD_T m_pickedItemType; /* type of schematic or board item that is concerned */
  91. EDA_ITEM* m_link; /* Pointer on another item. Used in undo redo command
  92. * used when a duplicate exists i.e. when an item is
  93. * modified, and the copy of initial item exists (the
  94. * duplicate) m_Item points the duplicate (i.e the old
  95. * copy of an active item) and m_Link points the active
  96. * item in schematic */
  97. KIID m_groupId; /* Id of the parent group */
  98. KIID_VECT_LIST m_groupMembers; /* Ids of the members of a group */
  99. BASE_SCREEN* m_screen; /* For new and deleted items the screen the item should
  100. * be added to/removed from. */
  101. };
  102. /**
  103. * A holder to handle information on schematic or board items.
  104. *
  105. * The information held is a pointer on each item, and the command made.
  106. */
  107. class PICKED_ITEMS_LIST
  108. {
  109. public:
  110. PICKED_ITEMS_LIST();
  111. ~PICKED_ITEMS_LIST();
  112. /**
  113. * Push \a aItem to the top of the list.
  114. *
  115. * @param aItem Picker to push on to the list.
  116. */
  117. void PushItem( const ITEM_PICKER& aItem );
  118. /**
  119. * @return The picker removed from the top of the list.
  120. */
  121. ITEM_PICKER PopItem();
  122. /**
  123. * @return True if \a aItem is found in the pick list.
  124. */
  125. bool ContainsItem( const EDA_ITEM* aItem ) const;
  126. /**
  127. * @return Index of the searched item. If the item is not stored in the list, negative value
  128. * is returned.
  129. */
  130. int FindItem( const EDA_ITEM* aItem ) const;
  131. /**
  132. * Delete only the list of pickers NOT the picked data itself.
  133. */
  134. void ClearItemsList();
  135. /**
  136. * Delete the list of pickers AND the data pointed by #m_PickedItem or #m_PickedItemLink
  137. * according to the type of undo/redo command recorded.
  138. */
  139. void ClearListAndDeleteItems( std::function<void(EDA_ITEM*)> aItemDeleter );
  140. /**
  141. * @return The count of pickers stored in this list.
  142. */
  143. unsigned GetCount() const
  144. {
  145. return m_ItemsList.size();
  146. }
  147. /**
  148. * Reverse the order of pickers stored in this list.
  149. *
  150. * This is useful when pop a list from Undo to Redo (and vice-versa) because
  151. * sometimes undo (or redo) a command needs to keep the order of successive
  152. * changes. Obviously, undo and redo are in reverse order
  153. */
  154. void ReversePickersListOrder();
  155. /**
  156. * @return The picker of a picked item.
  157. * @param aIdx Index of the picker in the picked list if this picker does not exist,
  158. * a picker is returned, with its members set to 0 or NULL.
  159. */
  160. const ITEM_PICKER& GetItemWrapper( unsigned int aIdx ) const;
  161. ITEM_PICKER& GetItemWrapper( unsigned int aIdx );
  162. /**
  163. * @return A pointer to the picked item.
  164. * @param aIdx Index of the picked item in the picked list.
  165. */
  166. EDA_ITEM* GetPickedItem( unsigned int aIdx ) const;
  167. /**
  168. * @return A pointer to the picked item's screen.
  169. * @param aIdx Index of the picked item in the picked list.
  170. */
  171. BASE_SCREEN* GetScreenForItem( unsigned int aIdx ) const;
  172. /**
  173. * @return link of the picked item, or null if does not exist.
  174. * @param aIdx Index of the picked item in the picked list.
  175. */
  176. EDA_ITEM* GetPickedItemLink( unsigned int aIdx ) const;
  177. /**
  178. * @return The type of undo/redo operation associated to the picked item,
  179. * or UNSPECIFIED if does not exist.
  180. * @param aIdx Index of the picked item in the picked list.
  181. */
  182. UNDO_REDO GetPickedItemStatus( unsigned int aIdx ) const;
  183. /**
  184. * Return the value of the picker flag.
  185. *
  186. * @param aIdx Index of the picker in the picked list.
  187. * @return The value stored in the picker, if the picker exists, or 0 if does not exist.
  188. */
  189. EDA_ITEM_FLAGS GetPickerFlags( unsigned aIdx ) const;
  190. /**
  191. * @param aItem A pointer to the item to pick.
  192. * @param aIdx Index of the picker in the picked list.
  193. * @return True if the picker exists or false if does not exist.
  194. */
  195. bool SetPickedItem( EDA_ITEM* aItem, unsigned aIdx );
  196. /**
  197. * Set the link associated to a given picked item.
  198. *
  199. * @param aLink is the link to the item associated to the picked item.
  200. * @param aIdx is index of the picker in the picked list.
  201. * @return true if the picker exists, or false if does not exist.
  202. */
  203. bool SetPickedItemLink( EDA_ITEM* aLink, unsigned aIdx );
  204. /**
  205. * Set the type of undo/redo operation for a given picked item.
  206. *
  207. * @param aStatus The type of undo/redo operation associated to the picked item
  208. * @param aIdx Index of the picker in the picked list
  209. * @return True if the picker exists or false if does not exist
  210. */
  211. bool SetPickedItemStatus( UNDO_REDO aStatus, unsigned aIdx );
  212. /**
  213. * Set the flags of the picker (usually to the picked item m_flags value).
  214. *
  215. * @param aFlags The flag value to save in picker.
  216. * @param aIdx Index of the picker in the picked list.
  217. * @return True if the picker exists or false if does not exist.
  218. */
  219. bool SetPickerFlags( EDA_ITEM_FLAGS aFlags, unsigned aIdx );
  220. /**
  221. * Remove one entry (one picker) from the list of picked items.
  222. *
  223. * @param aIdx Index of the picker in the picked list.
  224. * @return True if ok or false if did not exist.
  225. */
  226. bool RemovePicker( unsigned aIdx );
  227. /**
  228. * Copy all data from aSource to the list.
  229. *
  230. * Items picked are not copied. just pointer in them are copied.
  231. *
  232. * @param aSource The list of items to copy to the list.
  233. */
  234. void CopyList( const PICKED_ITEMS_LIST& aSource );
  235. wxString GetDescription() const { return m_description; }
  236. void SetDescription( const wxString& aDescription ) { m_description = aDescription; }
  237. private:
  238. wxString m_description;
  239. std::vector<ITEM_PICKER> m_ItemsList;
  240. };
  241. /**
  242. * A holder to handle a list of undo (or redo) commands.
  243. */
  244. class UNDO_REDO_CONTAINER
  245. {
  246. public:
  247. UNDO_REDO_CONTAINER();
  248. ~UNDO_REDO_CONTAINER();
  249. void PushCommand( PICKED_ITEMS_LIST* aCommand );
  250. PICKED_ITEMS_LIST* PopCommand();
  251. void ClearCommandList();
  252. std::vector <PICKED_ITEMS_LIST*> m_CommandsList; // the list of possible undo/redo commands
  253. };
  254. #endif // _CLASS_UNDOREDO_CONTAINER_H