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.

329 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@verizon.net>
  6. * Copyright (C) 2009 KiCad Developers, see change_log.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 <vector>
  28. #include <base_struct.h>
  29. class PICKED_ITEMS_LIST;
  30. /**
  31. * Undo Redo considerations:
  32. * Basically we have 3 cases
  33. * New item
  34. * Deleted item
  35. * Modified item
  36. * there is also a specific case in Eeschema, when wires are modified
  37. * If an item is modified, a copy of the "old" item parameters value is held.
  38. * When an item is deleted or added (new item) the pointer points the item, and there is
  39. * no other copy.
  40. * However, because there are some commands that concern a lot of items
  41. * and modify them, but modifications are easy to undo/redo,
  42. * so a copy of old items is not necessary. They are block command
  43. * Move block
  44. * Rotate block
  45. * Mirror or Flip block
  46. * and they are undo/redo by the same command
  47. */
  48. /* Type of undo/redo operations
  49. * each type must be redo/undone by a specific operation
  50. */
  51. enum UNDO_REDO_T {
  52. UR_UNSPECIFIED = 0, // illegal
  53. UR_CHANGED, // params of items have a value changed: undo is made by exchange
  54. // values with a copy of these values
  55. UR_NEW, // new item, undo by changing in deleted
  56. UR_DELETED, // deleted item, undo by changing in deleted
  57. UR_MOVED, // moved item, undo by move it
  58. UR_MIRRORED_X, // mirrored item, undo by mirror X
  59. UR_MIRRORED_Y, // mirrored item, undo by mirror Y
  60. UR_ROTATED, // Rotated item (counterclockwise), undo by rotating it
  61. UR_ROTATED_CLOCKWISE, // Rotated item (clockwise), undo by rotating it
  62. UR_FLIPPED, // flipped (board items only), undo by flipping it
  63. UR_WIRE_IMAGE, // Specific to Eeschema for handling wires changes.
  64. UR_LIBEDIT, // Specific to the component editor (libedit creates a full copy
  65. // of the current component when changed)
  66. UR_LIB_RENAME, // As UR_LIBEDIT, but old copy should be removed from library
  67. UR_EXCHANGE_T, ///< Use for changing the schematic text type where swapping
  68. ///< data structure is insufficient to restore the change.
  69. UR_DRILLORIGIN, // origin changed (like UR_CHANGED, contains the origin and a copy)
  70. UR_GRIDORIGIN // origin changed (like UR_CHANGED, contains the origin and a copy)
  71. };
  72. class ITEM_PICKER
  73. {
  74. private:
  75. STATUS_FLAGS m_pickerFlags; /* a copy of m_Flags member. useful in mode/drag
  76. * undo/redo commands */
  77. UNDO_REDO_T m_undoRedoStatus; /* type of operation to undo/redo for this item */
  78. EDA_ITEM* m_pickedItem; /* Pointer on the schematic or board item that is concerned
  79. * (picked), or in undo redo commands, the copy of an
  80. * edited item. */
  81. KICAD_T m_pickedItemType; /* type of schematic or board item that is concerned */
  82. EDA_ITEM* m_link; /* Pointer on another item. Used in undo redo command
  83. * used when a duplicate exists i.e. when an item is
  84. * modified, and the copy of initial item exists (the
  85. * duplicate) m_Item points the duplicate (i.e the old
  86. * copy of an active item) and m_Link points the active
  87. * item in schematic */
  88. public:
  89. ITEM_PICKER( EDA_ITEM* aItem = NULL, UNDO_REDO_T aUndoRedoStatus = UR_UNSPECIFIED );
  90. EDA_ITEM* GetItem() const { return m_pickedItem; }
  91. void SetItem( EDA_ITEM* aItem )
  92. {
  93. m_pickedItem = aItem;
  94. m_pickedItemType = aItem ? aItem->Type() : TYPE_NOT_INIT;
  95. }
  96. KICAD_T GetItemType() const { return m_pickedItemType; }
  97. void SetStatus( UNDO_REDO_T aStatus ) { m_undoRedoStatus = aStatus; }
  98. UNDO_REDO_T GetStatus() const { return m_undoRedoStatus; }
  99. void SetFlags( STATUS_FLAGS aFlags ) { m_pickerFlags = aFlags; }
  100. STATUS_FLAGS GetFlags() const { return m_pickerFlags; }
  101. void SetLink( EDA_ITEM* aItem ) { m_link = aItem; }
  102. EDA_ITEM* GetLink() const { return m_link; }
  103. };
  104. /**
  105. * Class PICKED_ITEMS_LIST
  106. * is a holder to handle information on schematic or board items.
  107. * The information held is a pointer on each item, and the command made.
  108. */
  109. class PICKED_ITEMS_LIST
  110. {
  111. public:
  112. UNDO_REDO_T m_Status; /* info about operation to undo/redo for this item. can be
  113. * UR_UNSPECIFIED */
  114. wxPoint m_TransformPoint; /* used to undo redo command by the same command: usually
  115. * need to know the rotate point or the move vector */
  116. private:
  117. std::vector <ITEM_PICKER> m_ItemsList;
  118. public:
  119. PICKED_ITEMS_LIST();
  120. ~PICKED_ITEMS_LIST();
  121. /**
  122. * Function PushItem
  123. * pushes \a aItem to the top of the list
  124. * @param aItem Picker to push on to the list.
  125. */
  126. void PushItem( const ITEM_PICKER& aItem );
  127. /**
  128. * Function PopItem
  129. * @return The picker removed from the top of the list.
  130. */
  131. ITEM_PICKER PopItem();
  132. /**
  133. * Function IsItemInList
  134. * @return True if \a aItem is found in the pick list.
  135. */
  136. bool ContainsItem( const EDA_ITEM* aItem ) const;
  137. /**
  138. * Function FindItem
  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. * Function ClearItemsList
  145. * deletes only the list of pickers, NOT the picked data itself.
  146. */
  147. void ClearItemsList();
  148. /**
  149. * Function ClearListAndDeleteItems
  150. * deletes the list of pickers, AND the data pointed by m_PickedItem or
  151. * m_PickedItemLink, according to the type of undo/redo command recorded
  152. */
  153. void ClearListAndDeleteItems();
  154. /**
  155. * Function GetCount
  156. * @return The count of pickers stored in this list.
  157. */
  158. unsigned GetCount() const
  159. {
  160. return m_ItemsList.size();
  161. }
  162. /**
  163. * Function ReversePickersListOrder
  164. * reverses the order of pickers stored in this list.
  165. * <p>
  166. * This is useful when pop a list from Undo to Redo (and vice-versa) because
  167. * sometimes undo (or redo) a command needs to keep the order of successive
  168. * changes. Obviously, undo and redo are in reverse order
  169. */
  170. void ReversePickersListOrder();
  171. /**
  172. * Function GetItemWrapper
  173. * @return The picker of a picked item.
  174. * @param aIdx Index of the picker in the picked list
  175. * if this picker does not exist, a picker is returned,
  176. * with its members set to 0 or NULL
  177. */
  178. ITEM_PICKER GetItemWrapper( unsigned int aIdx ) const;
  179. /**
  180. * Function GetPickedItem
  181. * @return A pointer to the picked item
  182. * @param aIdx Index of the picked item in the picked list
  183. */
  184. EDA_ITEM* GetPickedItem( unsigned int aIdx ) const;
  185. /**
  186. * Function GetPickedItemLink
  187. * @return link of the picked item, or null if does not exist
  188. * @param aIdx Index of the picked item in the picked list
  189. */
  190. EDA_ITEM* GetPickedItemLink( unsigned int aIdx ) const;
  191. /**
  192. * Function GetPickedItemStatus
  193. * @return The type of undo/redo operation associated to the picked item,
  194. * or UR_UNSPECIFIED if does not exist
  195. * @param aIdx Index of the picked item in the picked list
  196. */
  197. UNDO_REDO_T GetPickedItemStatus( unsigned int aIdx ) const;
  198. /**
  199. * Function GetPickerFlags
  200. * returns the value of the picker flag.
  201. * @param aIdx Index of the picker in the picked list
  202. * @return The value stored in the picker, if the picker exists, or 0 if does not exist
  203. */
  204. STATUS_FLAGS GetPickerFlags( unsigned aIdx ) const;
  205. /**
  206. * Function SetPickedItem
  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. * Function SetPickedItem
  214. * @param aItem A pointer to the item to pick
  215. * @param aStatus The type of undo/redo operation associated to the item to pick
  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 SetPickedItem( EDA_ITEM* aItem, UNDO_REDO_T aStatus, unsigned aIdx );
  220. /**
  221. * Function SetPickedItemLink
  222. * set the link associated to a given picked item.
  223. * @param aLink = the link to the item associated to the picked item
  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 SetPickedItemLink( EDA_ITEM* aLink, unsigned aIdx );
  228. /**
  229. * Function SetPickedItemStatus
  230. * sets the type of undo/redo operation for a given picked item.
  231. * @param aStatus The type of undo/redo operation associated to the picked item
  232. * @param aIdx Index of the picker in the picked list
  233. * @return True if the picker exists or false if does not exist
  234. */
  235. bool SetPickedItemStatus( UNDO_REDO_T aStatus, unsigned aIdx );
  236. /**
  237. * Function SetPickerFlags
  238. * set the flags of the picker (usually to the picked item m_Flags value)
  239. * @param aFlags The flag value to save in picker
  240. * @param aIdx Index of the picker in the picked list
  241. * @return True if the picker exists or false if does not exist
  242. */
  243. bool SetPickerFlags( STATUS_FLAGS aFlags, unsigned aIdx );
  244. /**
  245. * Function RemovePicker
  246. * removes one entry (one picker) from the list of picked items.
  247. * @param aIdx Index of the picker in the picked list
  248. * @return True if ok or false if did not exist
  249. */
  250. bool RemovePicker( unsigned aIdx );
  251. /**
  252. * Function CopyList
  253. * copies all data from aSource to the list.
  254. * Items picked are not copied. just pointer in them are copied
  255. * @param aSource The list of items to copy to the list.
  256. */
  257. void CopyList( const PICKED_ITEMS_LIST& aSource );
  258. };
  259. /**
  260. * Class UNDO_REDO_CONTAINER
  261. * is a holder to handle alist of undo (or redo) command.
  262. * this class handles a list of ITEM_PICKER (each manage one schematic or board item).
  263. */
  264. class UNDO_REDO_CONTAINER
  265. {
  266. public:
  267. std::vector <PICKED_ITEMS_LIST*> m_CommandsList; // the list of possible undo/redo commands
  268. public:
  269. UNDO_REDO_CONTAINER();
  270. ~UNDO_REDO_CONTAINER();
  271. void PushCommand( PICKED_ITEMS_LIST* aCommand );
  272. PICKED_ITEMS_LIST* PopCommand();
  273. void ClearCommandList();
  274. };
  275. #endif // _CLASS_UNDOREDO_CONTAINER_H