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.

346 lines
12 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. REGROUP, // new group of items created (NB: can't use GROUP because of collision
  65. // with a header on msys2)
  66. UNGROUP, // existing group destroyed (items not destroyed)
  67. REPEAT_ITEM // storage entry for the editor's global repeatItems list
  68. };
  69. class ITEM_PICKER
  70. {
  71. public:
  72. // ITEM_PICKER( EDA_ITEM* aItem = NULL, UNDO_REDO aStatus = UNSPECIFIED );
  73. ITEM_PICKER();
  74. ITEM_PICKER( BASE_SCREEN* aScreen, EDA_ITEM* aItem,
  75. UNDO_REDO aStatus = UNDO_REDO::UNSPECIFIED );
  76. EDA_ITEM* GetItem() const { return m_pickedItem; }
  77. void SetItem( EDA_ITEM* aItem );
  78. KICAD_T GetItemType() const { return m_pickedItemType; }
  79. void SetStatus( UNDO_REDO aStatus ) { m_undoRedoStatus = aStatus; }
  80. UNDO_REDO GetStatus() const { return m_undoRedoStatus; }
  81. void SetFlags( EDA_ITEM_FLAGS aFlags ) { m_pickerFlags = aFlags; }
  82. EDA_ITEM_FLAGS GetFlags() const { return m_pickerFlags; }
  83. void SetLink( EDA_ITEM* aItem ) { m_link = aItem; }
  84. EDA_ITEM* GetLink() const { return m_link; }
  85. KIID GetGroupId() const { return m_groupId; }
  86. void SetGroupId( KIID aId ) { m_groupId = aId; }
  87. BASE_SCREEN* GetScreen() const { return m_screen; }
  88. private:
  89. EDA_ITEM_FLAGS m_pickerFlags; /* A copy of m_flags member. Currently used only to flag
  90. * transient items. */
  91. UNDO_REDO m_undoRedoStatus; /* type of operation to undo/redo for this item */
  92. EDA_ITEM* m_pickedItem; /* Pointer on the schematic or board item that is concerned
  93. * (picked), or in undo redo commands, the copy of an
  94. * edited item. */
  95. KICAD_T m_pickedItemType; /* type of schematic or board item that is concerned */
  96. EDA_ITEM* m_link; /* Pointer on another item. Used in undo redo command
  97. * used when a duplicate exists i.e. when an item is
  98. * modified, and the copy of initial item exists (the
  99. * duplicate) m_Item points the duplicate (i.e the old
  100. * copy of an active item) and m_Link points the active
  101. * item in schematic */
  102. KIID m_groupId; /* Id of the group of items in case this is a
  103. * group/ungroup command */
  104. BASE_SCREEN* m_screen; /* For new and deleted items the screen the item should
  105. * be added to/removed from. */
  106. };
  107. /**
  108. * A holder to handle information on schematic or board items.
  109. *
  110. * The information held is a pointer on each item, and the command made.
  111. */
  112. class PICKED_ITEMS_LIST
  113. {
  114. public:
  115. PICKED_ITEMS_LIST();
  116. ~PICKED_ITEMS_LIST();
  117. /**
  118. * Push \a aItem to the top of the list.
  119. *
  120. * @param aItem Picker to push on to the list.
  121. */
  122. void PushItem( const ITEM_PICKER& aItem );
  123. /**
  124. * @return The picker removed from the top of the list.
  125. */
  126. ITEM_PICKER PopItem();
  127. /**
  128. * @return True if \a aItem is found in the pick list.
  129. */
  130. bool ContainsItem( const EDA_ITEM* aItem ) const;
  131. /**
  132. * Check the undo/redo list for any #EDA_ITEM of type \a aItemType.
  133. *
  134. * @param aItemType is an #EDA_ITEM type from the list of #KICAD_T types.
  135. * @return true if an item of \a aItemType is found in the pick list.
  136. */
  137. bool ContainsItemType( KICAD_T aItemType ) const;
  138. /**
  139. * @return Index of the searched item. If the item is not stored in the list, negative value
  140. * is returned.
  141. */
  142. int FindItem( const EDA_ITEM* aItem ) const;
  143. /**
  144. * Delete only the list of pickers NOT the picked data itself.
  145. */
  146. void ClearItemsList();
  147. /**
  148. * Delete the list of pickers AND the data pointed by #m_PickedItem or #m_PickedItemLink
  149. * according to the type of undo/redo command recorded.
  150. */
  151. void ClearListAndDeleteItems( std::function<void(EDA_ITEM*)> aItemDeleter );
  152. /**
  153. * @return The count of pickers stored in this list.
  154. */
  155. unsigned GetCount() const
  156. {
  157. return m_ItemsList.size();
  158. }
  159. /**
  160. * Reverse the order of pickers stored in this list.
  161. *
  162. * This is useful when pop a list from Undo to Redo (and vice-versa) because
  163. * sometimes undo (or redo) a command needs to keep the order of successive
  164. * changes. Obviously, undo and redo are in reverse order
  165. */
  166. void ReversePickersListOrder();
  167. /**
  168. * @return The picker of a picked item.
  169. * @param aIdx Index of the picker in the picked list if this picker does not exist,
  170. * a picker is returned, with its members set to 0 or NULL.
  171. */
  172. ITEM_PICKER GetItemWrapper( unsigned int aIdx ) const;
  173. /**
  174. * @return A pointer to the picked item.
  175. * @param aIdx Index of the picked item in the picked list.
  176. */
  177. EDA_ITEM* GetPickedItem( unsigned int aIdx ) const;
  178. /**
  179. * @return A pointer to the picked item's screen.
  180. * @param aIdx Index of the picked item in the picked list.
  181. */
  182. BASE_SCREEN* GetScreenForItem( unsigned int aIdx ) const;
  183. /**
  184. * @return link of the picked item, or null if does not exist.
  185. * @param aIdx Index of the picked item in the picked list.
  186. */
  187. EDA_ITEM* GetPickedItemLink( unsigned int aIdx ) const;
  188. /**
  189. * @return The type of undo/redo operation associated to the picked item,
  190. * or UNSPECIFIED if does not exist.
  191. * @param aIdx Index of the picked item in the picked list.
  192. */
  193. UNDO_REDO GetPickedItemStatus( unsigned int aIdx ) const;
  194. /**
  195. * @return The group id of the picked item, or null KIID if does not exist.
  196. * @param aIdx Index of the picked item in the picked list.
  197. */
  198. KIID GetPickedItemGroupId( unsigned int aIdx ) const;
  199. /**
  200. * Return the value of the picker flag.
  201. *
  202. * @param aIdx Index of the picker in the picked list.
  203. * @return The value stored in the picker, if the picker exists, or 0 if does not exist.
  204. */
  205. EDA_ITEM_FLAGS GetPickerFlags( unsigned aIdx ) const;
  206. /**
  207. * @param aItem A pointer to the item to pick.
  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 SetPickedItem( EDA_ITEM* aItem, unsigned aIdx );
  212. /**
  213. * @param aItem A pointer to the item to pick.
  214. * @param aStatus The type of undo/redo operation associated to the item to pick.
  215. * @param aIdx Index of the picker in the picked list.
  216. * @return True if the picker exists or false if does not exist.
  217. */
  218. bool SetPickedItem( EDA_ITEM* aItem, UNDO_REDO aStatus, unsigned aIdx );
  219. /**
  220. * Set the link associated to a given picked item.
  221. *
  222. * @param aLink is the link to the item associated to the picked item.
  223. * @param aIdx is index of the picker in the picked list.
  224. * @return true if the picker exists, or false if does not exist.
  225. */
  226. bool SetPickedItemLink( EDA_ITEM* aLink, unsigned aIdx );
  227. /**
  228. * Set the group id associated to a given picked item.
  229. *
  230. * @param aId is the group id to associate to the picked item.
  231. * @param aIdx is index of the picker in the picked list.
  232. * @return true if the picker exists, or false if does not exist.
  233. */
  234. bool SetPickedItemGroupId( KIID aId, unsigned aIdx );
  235. /**
  236. * Set the type of undo/redo operation for a given picked item.
  237. *
  238. * @param aStatus The type of undo/redo operation associated to the picked item
  239. * @param aIdx Index of the picker in the picked list
  240. * @return True if the picker exists or false if does not exist
  241. */
  242. bool SetPickedItemStatus( UNDO_REDO aStatus, unsigned aIdx );
  243. /**
  244. * Set the flags of the picker (usually to the picked item m_flags value).
  245. *
  246. * @param aFlags The flag value to save in picker.
  247. * @param aIdx Index of the picker in the picked list.
  248. * @return True if the picker exists or false if does not exist.
  249. */
  250. bool SetPickerFlags( EDA_ITEM_FLAGS aFlags, unsigned aIdx );
  251. /**
  252. * Remove one entry (one picker) from the list of picked items.
  253. *
  254. * @param aIdx Index of the picker in the picked list.
  255. * @return True if ok or false if did not exist.
  256. */
  257. bool RemovePicker( unsigned aIdx );
  258. /**
  259. * Copy all data from aSource to the list.
  260. *
  261. * Items picked are not copied. just pointer in them are copied.
  262. *
  263. * @param aSource The list of items to copy to the list.
  264. */
  265. void CopyList( const PICKED_ITEMS_LIST& aSource );
  266. wxString GetDescription() const { return m_description; }
  267. void SetDescription( const wxString& aDescription ) { m_description = aDescription; }
  268. private:
  269. wxString m_description;
  270. std::vector<ITEM_PICKER> m_ItemsList;
  271. };
  272. /**
  273. * A holder to handle a list of undo (or redo) commands.
  274. */
  275. class UNDO_REDO_CONTAINER
  276. {
  277. public:
  278. UNDO_REDO_CONTAINER();
  279. ~UNDO_REDO_CONTAINER();
  280. void PushCommand( PICKED_ITEMS_LIST* aCommand );
  281. PICKED_ITEMS_LIST* PopCommand();
  282. void ClearCommandList();
  283. std::vector <PICKED_ITEMS_LIST*> m_CommandsList; // the list of possible undo/redo commands
  284. };
  285. #endif // _CLASS_UNDOREDO_CONTAINER_H