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.

312 lines
11 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 (C) 2009-2021 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 <vector>
  30. class EDA_ITEM;
  31. class PICKED_ITEMS_LIST;
  32. class BASE_SCREEN;
  33. /**
  34. * Undo Redo considerations:
  35. * Basically we have 3 cases
  36. * New item
  37. * Deleted item
  38. * Modified item
  39. * there is also a specific case in Eeschema, when wires are modified
  40. * If an item is modified, a copy of the "old" item parameters value is held.
  41. * When an item is deleted or added (new item) the pointer points the item, and there is
  42. * no other copy.
  43. */
  44. /**
  45. * Type of undo/redo operations
  46. *
  47. * Each type must be redo/undone by a specific operation.
  48. */
  49. enum class UNDO_REDO {
  50. UNSPECIFIED = 0, // illegal
  51. NOP, // Undo/redo will ignore this entry. Only forces the start of a new stack
  52. CHANGED, // params of items have a value changed: undo is made by exchange
  53. // values with a copy of these values
  54. NEWITEM, // new item, undo by changing in deleted
  55. DELETED, // deleted item, undo by changing in deleted
  56. LIBEDIT, // Specific to the component editor (symbol_editor creates a full copy
  57. // of the current component when changed)
  58. LIB_RENAME, // As LIBEDIT, but old copy should be removed from library
  59. EXCHANGE_T, // Use for changing the schematic text type where swapping
  60. // data structure is insufficient to restore the change.
  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. REGROUP, // new group of items created (do not use GROUP to avoid collision
  65. // with an header on msys2)
  66. UNGROUP // existing group destroyed (items not destroyed)
  67. };
  68. class ITEM_PICKER
  69. {
  70. public:
  71. // ITEM_PICKER( EDA_ITEM* aItem = NULL, UNDO_REDO aStatus = UNSPECIFIED );
  72. ITEM_PICKER();
  73. ITEM_PICKER( BASE_SCREEN* aScreen, EDA_ITEM* aItem,
  74. UNDO_REDO aStatus = UNDO_REDO::UNSPECIFIED );
  75. EDA_ITEM* GetItem() const { return m_pickedItem; }
  76. void SetItem( EDA_ITEM* aItem );
  77. KICAD_T GetItemType() const { return m_pickedItemType; }
  78. void SetStatus( UNDO_REDO aStatus ) { m_undoRedoStatus = aStatus; }
  79. UNDO_REDO GetStatus() const { return m_undoRedoStatus; }
  80. void SetFlags( EDA_ITEM_FLAGS aFlags ) { m_pickerFlags = aFlags; }
  81. EDA_ITEM_FLAGS GetFlags() const { return m_pickerFlags; }
  82. void SetLink( EDA_ITEM* aItem ) { m_link = aItem; }
  83. EDA_ITEM* GetLink() const { return m_link; }
  84. BASE_SCREEN* GetScreen() const { return m_screen; }
  85. private:
  86. EDA_ITEM_FLAGS m_pickerFlags; /* a copy of m_flags member. useful in mode/drag
  87. * undo/redo commands */
  88. UNDO_REDO m_undoRedoStatus; /* type of operation to undo/redo for this item */
  89. EDA_ITEM* m_pickedItem; /* Pointer on the schematic or board item that is concerned
  90. * (picked), or in undo redo commands, the copy of an
  91. * edited item. */
  92. KICAD_T m_pickedItemType; /* type of schematic or board item that is concerned */
  93. EDA_ITEM* m_link; /* Pointer on another item. Used in undo redo command
  94. * used when a duplicate exists i.e. when an item is
  95. * modified, and the copy of initial item exists (the
  96. * duplicate) m_Item points the duplicate (i.e the old
  97. * copy of an active item) and m_Link points the active
  98. * item in schematic */
  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. private:
  110. std::vector <ITEM_PICKER> m_ItemsList;
  111. public:
  112. PICKED_ITEMS_LIST();
  113. ~PICKED_ITEMS_LIST();
  114. /**
  115. * Push \a aItem to the top of the list.
  116. *
  117. * @param aItem Picker to push on to the list.
  118. */
  119. void PushItem( const ITEM_PICKER& aItem );
  120. /**
  121. * @return The picker removed from the top of the list.
  122. */
  123. ITEM_PICKER PopItem();
  124. /**
  125. * @return True if \a aItem is found in the pick list.
  126. */
  127. bool ContainsItem( const EDA_ITEM* aItem ) const;
  128. /**
  129. * @return Index of the searched item. If the item is not stored in the list, negative value
  130. * is returned.
  131. */
  132. int FindItem( const EDA_ITEM* aItem ) const;
  133. /**
  134. * Delete only the list of pickers NOT the picked data itself.
  135. */
  136. void ClearItemsList();
  137. /**
  138. * Delete the list of pickers AND the data pointed by #m_PickedItem or #m_PickedItemLink
  139. * according to the type of undo/redo command recorded.
  140. */
  141. void ClearListAndDeleteItems();
  142. /**
  143. * @return The count of pickers stored in this list.
  144. */
  145. unsigned GetCount() const
  146. {
  147. return m_ItemsList.size();
  148. }
  149. /**
  150. * Reverse the order of pickers stored in this list.
  151. *
  152. * This is useful when pop a list from Undo to Redo (and vice-versa) because
  153. * sometimes undo (or redo) a command needs to keep the order of successive
  154. * changes. Obviously, undo and redo are in reverse order
  155. */
  156. void ReversePickersListOrder();
  157. /**
  158. * @return The picker of a picked item.
  159. * @param aIdx Index of the picker in the picked list if this picker does not exist,
  160. * a picker is returned, with its members set to 0 or NULL.
  161. */
  162. ITEM_PICKER GetItemWrapper( unsigned int aIdx ) const;
  163. /**
  164. * @return A pointer to the picked item.
  165. * @param aIdx Index of the picked item in the picked list.
  166. */
  167. EDA_ITEM* GetPickedItem( unsigned int aIdx ) const;
  168. /**
  169. * @return A pointer to the picked item's screen.
  170. * @param aIdx Index of the picked item in the picked list.
  171. */
  172. BASE_SCREEN* GetScreenForItem( unsigned int aIdx ) const;
  173. /**
  174. * @return link of the picked item, or null if does not exist.
  175. * @param aIdx Index of the picked item in the picked list.
  176. */
  177. EDA_ITEM* GetPickedItemLink( unsigned int aIdx ) const;
  178. /**
  179. * @return The type of undo/redo operation associated to the picked item,
  180. * or UNSPECIFIED if does not exist.
  181. * @param aIdx Index of the picked item in the picked list.
  182. */
  183. UNDO_REDO GetPickedItemStatus( unsigned int aIdx ) const;
  184. /**
  185. * Return the value of the picker flag.
  186. *
  187. * @param aIdx Index of the picker in the picked list.
  188. * @return The value stored in the picker, if the picker exists, or 0 if does not exist.
  189. */
  190. EDA_ITEM_FLAGS GetPickerFlags( unsigned aIdx ) const;
  191. /**
  192. * @param aItem A pointer to the item to pick.
  193. * @param aIdx Index of the picker in the picked list.
  194. * @return True if the picker exists or false if does not exist.
  195. */
  196. bool SetPickedItem( EDA_ITEM* aItem, unsigned aIdx );
  197. /**
  198. * @param aItem A pointer to the item to pick.
  199. * @param aStatus The type of undo/redo operation associated to the item to pick.
  200. * @param aIdx Index of the picker in the picked list.
  201. * @return True if the picker exists or false if does not exist.
  202. */
  203. bool SetPickedItem( EDA_ITEM* aItem, UNDO_REDO aStatus, unsigned aIdx );
  204. /**
  205. * Set the link associated to a given picked item.
  206. *
  207. * @param aLink is the link to the item associated to the picked item.
  208. * @param aIdx is index of the picker in the picked list.
  209. * @return true if the picker exists, or false if does not exist.
  210. */
  211. bool SetPickedItemLink( EDA_ITEM* aLink, unsigned aIdx );
  212. /**
  213. * Set the type of undo/redo operation for a given picked item.
  214. *
  215. * @param aStatus The type of undo/redo operation associated to the picked item
  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 SetPickedItemStatus( UNDO_REDO aStatus, unsigned aIdx );
  220. /**
  221. * Set the flags of the picker (usually to the picked item m_flags value).
  222. *
  223. * @param aFlags The flag value to save in picker.
  224. * @param aIdx Index of the picker in the picked list.
  225. * @return True if the picker exists or false if does not exist.
  226. */
  227. bool SetPickerFlags( EDA_ITEM_FLAGS aFlags, unsigned aIdx );
  228. /**
  229. * Remove one entry (one picker) from the list of picked items.
  230. *
  231. * @param aIdx Index of the picker in the picked list.
  232. * @return True if ok or false if did not exist.
  233. */
  234. bool RemovePicker( unsigned aIdx );
  235. /**
  236. * Copy all data from aSource to the list.
  237. *
  238. * Items picked are not copied. just pointer in them are copied.
  239. *
  240. * @param aSource The list of items to copy to the list.
  241. */
  242. void CopyList( const PICKED_ITEMS_LIST& aSource );
  243. };
  244. /**
  245. * A holder to handle a list of undo (or redo) commands.
  246. */
  247. class UNDO_REDO_CONTAINER
  248. {
  249. public:
  250. UNDO_REDO_CONTAINER();
  251. ~UNDO_REDO_CONTAINER();
  252. void PushCommand( PICKED_ITEMS_LIST* aCommand );
  253. PICKED_ITEMS_LIST* PopCommand();
  254. void ClearCommandList();
  255. std::vector <PICKED_ITEMS_LIST*> m_CommandsList; // the list of possible undo/redo commands
  256. };
  257. #endif // _CLASS_UNDOREDO_CONTAINER_H