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.

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