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.

382 lines
12 KiB

18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
17 years ago
17 years ago
18 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
18 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2004 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
  5. * Copyright (C) 2004-2011 KiCad Developers, see change_log.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. /**
  25. * @file schematic_undo_redo.cpp
  26. * @brief Eeschema undo and redo functions for schematic editor.
  27. */
  28. #include <fctsys.h>
  29. #include <class_drawpanel.h>
  30. #include <schframe.h>
  31. #include <general.h>
  32. #include <protos.h>
  33. #include <sch_bus_entry.h>
  34. #include <sch_marker.h>
  35. #include <sch_junction.h>
  36. #include <sch_line.h>
  37. #include <sch_no_connect.h>
  38. #include <sch_component.h>
  39. #include <sch_sheet.h>
  40. #include <sch_bitmap.h>
  41. /* Functions to undo and redo edit commands.
  42. * commands to undo are stored in CurrentScreen->m_UndoList
  43. * commands to redo are stored in CurrentScreen->m_RedoList
  44. *
  45. * m_UndoList and m_RedoList handle a std::vector of PICKED_ITEMS_LIST
  46. * Each PICKED_ITEMS_LIST handle a std::vector of pickers (class ITEM_PICKER),
  47. * that store the list of schematic items that are concerned by the command to
  48. * undo or redo and is created for each command to undo (handle also a command
  49. * to redo). each picker has a pointer pointing to an item to undo or redo (in
  50. * fact: deleted, added or modified), and has a pointer to a copy of this item,
  51. * when this item has been modified (the old values of parameters are
  52. * therefore saved)
  53. *
  54. * there are 3 cases:
  55. * - delete item(s) command
  56. * - change item(s) command
  57. * - add item(s) command
  58. * and 2 cases for block:
  59. * - move list of items
  60. * - mirror (Y) list of items
  61. *
  62. * Undo command
  63. * - delete item(s) command:
  64. * => deleted items are moved in undo list
  65. *
  66. * - change item(s) command
  67. * => A copy of item(s) is made (a DrawPickedStruct list of wrappers)
  68. * the .m_Link member of each wrapper points the modified item.
  69. * the .m_Item member of each wrapper points the old copy of this item.
  70. *
  71. * - add item(s) command
  72. * =>A list of item(s) is made. The .m_Item member of each wrapper points
  73. * the new item.
  74. *
  75. * Redo command
  76. * - delete item(s) old command:
  77. * => deleted items are moved in GetDrawItems() list, and in
  78. *
  79. * - change item(s) command
  80. * => the copy of item(s) is moved in Undo list
  81. *
  82. * - add item(s) command
  83. * => The list of item(s) is used to create a deleted list in undo
  84. * list(same as a delete command)
  85. *
  86. * Some block operations that change items can be undone without memorized
  87. * items, just the coordinates of the transform: move list of items (undo/
  88. * redo is made by moving with the opposite move vector) mirror (Y) and flip
  89. * list of items (undo/redo is made by mirror or flip items) so they are
  90. * handled specifically.
  91. *
  92. * A problem is the hierarchical sheet handling.
  93. * the data associated (sub-hierarchy, undo/redo list) is deleted only
  94. * when the sheet is really deleted (i.e. when deleted from undo or redo list)
  95. * This is handled by its destructor.
  96. */
  97. /* Used if undo / redo command:
  98. * swap data between Item and its copy, pointed by its picked item link member
  99. * swapped data is data modified by edition, so not all values are swapped
  100. */
  101. void SCH_EDIT_FRAME::SaveCopyInUndoList( SCH_ITEM* aItem,
  102. UNDO_REDO_T aCommandType,
  103. const wxPoint& aTransformPoint )
  104. {
  105. /* Does not save a null item or a UR_WIRE_IMAGE command type. UR_WIRE_IMAGE commands
  106. * are handled by the overloaded version of SaveCopyInUndoList that takes a reference
  107. * to a PICKED_ITEMS_LIST.
  108. */
  109. if( aItem == NULL || aCommandType == UR_WIRE_IMAGE )
  110. return;
  111. PICKED_ITEMS_LIST* commandToUndo = new PICKED_ITEMS_LIST();
  112. commandToUndo->m_TransformPoint = aTransformPoint;
  113. ITEM_PICKER itemWrapper( aItem, aCommandType );
  114. itemWrapper.SetFlags( aItem->GetFlags() );
  115. switch( aCommandType )
  116. {
  117. case UR_CHANGED: /* Create a copy of item */
  118. itemWrapper.SetLink( DuplicateStruct( aItem, true ) );
  119. commandToUndo->PushItem( itemWrapper );
  120. break;
  121. case UR_NEW:
  122. case UR_DELETED:
  123. case UR_ROTATED:
  124. case UR_MOVED:
  125. commandToUndo->PushItem( itemWrapper );
  126. break;
  127. default:
  128. wxFAIL_MSG( wxString::Format( wxT( "SaveCopyInUndoList() error (unknown code %X)" ),
  129. aCommandType ) );
  130. break;
  131. }
  132. if( commandToUndo->GetCount() )
  133. {
  134. /* Save the copy in undo list */
  135. GetScreen()->PushCommandToUndoList( commandToUndo );
  136. /* Clear redo list, because after new save there is no redo to do */
  137. GetScreen()->ClearUndoORRedoList( GetScreen()->m_RedoList );
  138. }
  139. else
  140. {
  141. delete commandToUndo;
  142. }
  143. }
  144. void SCH_EDIT_FRAME::SaveCopyInUndoList( const PICKED_ITEMS_LIST& aItemsList,
  145. UNDO_REDO_T aTypeCommand,
  146. const wxPoint& aTransformPoint )
  147. {
  148. PICKED_ITEMS_LIST* commandToUndo = new PICKED_ITEMS_LIST();
  149. commandToUndo->m_TransformPoint = aTransformPoint;
  150. commandToUndo->m_Status = aTypeCommand;
  151. // Copy picker list:
  152. commandToUndo->CopyList( aItemsList );
  153. // Verify list, and creates data if needed
  154. for( unsigned ii = 0; ii < commandToUndo->GetCount(); ii++ )
  155. {
  156. SCH_ITEM* item = (SCH_ITEM*) commandToUndo->GetPickedItem( ii );
  157. wxASSERT( item );
  158. UNDO_REDO_T command = commandToUndo->GetPickedItemStatus( ii );
  159. if( command == UR_UNSPECIFIED )
  160. {
  161. command = aTypeCommand;
  162. commandToUndo->SetPickedItemStatus( command, ii );
  163. }
  164. switch( command )
  165. {
  166. case UR_CHANGED: /* Create a copy of item */
  167. /* If needed, create a copy of item, and put in undo list
  168. * in the picker, as link
  169. * If this link is not null, the copy is already done
  170. */
  171. if( commandToUndo->GetPickedItemLink( ii ) == NULL )
  172. commandToUndo->SetPickedItemLink( DuplicateStruct( item, true ), ii );
  173. wxASSERT( commandToUndo->GetPickedItemLink( ii ) );
  174. break;
  175. case UR_MOVED:
  176. case UR_MIRRORED_Y:
  177. case UR_MIRRORED_X:
  178. case UR_ROTATED:
  179. case UR_NEW:
  180. case UR_DELETED:
  181. case UR_EXCHANGE_T:
  182. case UR_WIRE_IMAGE:
  183. break;
  184. default:
  185. wxFAIL_MSG( wxString::Format( wxT( "Unknown undo/redo command %d" ), command ) );
  186. break;
  187. }
  188. }
  189. if( commandToUndo->GetCount() || aTypeCommand == UR_WIRE_IMAGE )
  190. {
  191. /* Save the copy in undo list */
  192. GetScreen()->PushCommandToUndoList( commandToUndo );
  193. /* Clear redo list, because after new save there is no redo to do */
  194. GetScreen()->ClearUndoORRedoList( GetScreen()->m_RedoList );
  195. }
  196. else // Should not occur
  197. {
  198. delete commandToUndo;
  199. }
  200. }
  201. void SCH_EDIT_FRAME::PutDataInPreviousState( PICKED_ITEMS_LIST* aList, bool aRedoCommand )
  202. {
  203. SCH_ITEM* item;
  204. SCH_ITEM* alt_item;
  205. // Exchange the current wires, buses, and junctions with the copy save by the last edit.
  206. if( aList->m_Status == UR_WIRE_IMAGE )
  207. {
  208. DLIST< SCH_ITEM > oldWires;
  209. // Prevent items from being deleted when the DLIST goes out of scope.
  210. oldWires.SetOwnership( false );
  211. // Remove all of the wires, buses, and junctions from the current screen.
  212. GetScreen()->ExtractWires( oldWires, false );
  213. // Copy the saved wires, buses, and junctions to the current screen.
  214. for( unsigned int i = 0; i < aList->GetCount(); i++ )
  215. GetScreen()->Append( (SCH_ITEM*) aList->GetPickedItem( i ) );
  216. aList->ClearItemsList();
  217. // Copy the previous wires, buses, and junctions to the picked item list for the
  218. // redo operation.
  219. while( oldWires.GetCount() != 0 )
  220. {
  221. ITEM_PICKER picker = ITEM_PICKER( oldWires.PopFront(), UR_WIRE_IMAGE );
  222. aList->PushItem( picker );
  223. }
  224. return;
  225. }
  226. // Undo in the reverse order of list creation: (this can allow stacked changes like the
  227. // same item can be changes and deleted in the same complex command.
  228. for( int ii = aList->GetCount() - 1; ii >= 0; ii-- )
  229. {
  230. item = (SCH_ITEM*) aList->GetPickedItem( ii );
  231. wxASSERT( item );
  232. item->ClearFlags();
  233. SCH_ITEM* image = (SCH_ITEM*) aList->GetPickedItemLink( ii );
  234. switch( aList->GetPickedItemStatus( ii ) )
  235. {
  236. case UR_CHANGED: /* Exchange old and new data for each item */
  237. item->SwapData( image );
  238. break;
  239. case UR_NEW: /* new items are deleted */
  240. aList->SetPickedItemStatus( UR_DELETED, ii );
  241. GetScreen()->Remove( item );
  242. break;
  243. case UR_DELETED: /* deleted items are put in the draw item list, as new items */
  244. aList->SetPickedItemStatus( UR_NEW, ii );
  245. GetScreen()->Append( item );
  246. break;
  247. case UR_MOVED:
  248. item->ClearFlags();
  249. item->SetFlags( aList->GetPickerFlags( ii ) );
  250. item->Move( aRedoCommand ? aList->m_TransformPoint : -aList->m_TransformPoint );
  251. item->ClearFlags();
  252. break;
  253. case UR_MIRRORED_Y:
  254. item->MirrorY( aList->m_TransformPoint.x );
  255. break;
  256. case UR_MIRRORED_X:
  257. item->MirrorX( aList->m_TransformPoint.y );
  258. break;
  259. case UR_ROTATED:
  260. // To undo a rotate 90 deg transform we must rotate 270 deg to undo
  261. // and 90 deg to redo:
  262. item->Rotate( aList->m_TransformPoint );
  263. if( aRedoCommand )
  264. break; // A only one rotate transform is OK
  265. // Make 3 rotate 90 deg transforms is this is actually an undo command
  266. item->Rotate( aList->m_TransformPoint );
  267. item->Rotate( aList->m_TransformPoint );
  268. break;
  269. case UR_EXCHANGE_T:
  270. alt_item = (SCH_ITEM*) aList->GetPickedItemLink( ii );
  271. alt_item->SetNext( NULL );
  272. alt_item->SetBack( NULL );
  273. GetScreen()->Remove( item );
  274. GetScreen()->Append( alt_item );
  275. aList->SetPickedItem( alt_item, ii );
  276. aList->SetPickedItemLink( item, ii );
  277. break;
  278. default:
  279. wxFAIL_MSG( wxString::Format( wxT( "Unknown undo/redo command %d" ),
  280. aList->GetPickedItemStatus( ii ) ) );
  281. break;
  282. }
  283. }
  284. }
  285. void SCH_EDIT_FRAME::GetSchematicFromUndoList( wxCommandEvent& event )
  286. {
  287. if( GetScreen()->GetUndoCommandCount() <= 0 )
  288. return;
  289. /* Get the old list */
  290. PICKED_ITEMS_LIST* List = GetScreen()->PopCommandFromUndoList();
  291. /* Undo the command */
  292. PutDataInPreviousState( List, false );
  293. /* Put the old list in RedoList */
  294. List->ReversePickersListOrder();
  295. GetScreen()->PushCommandToRedoList( List );
  296. OnModify();
  297. SetSheetNumberAndCount();
  298. GetScreen()->TestDanglingEnds();
  299. m_canvas->Refresh();
  300. }
  301. void SCH_EDIT_FRAME::GetSchematicFromRedoList( wxCommandEvent& event )
  302. {
  303. if( GetScreen()->GetRedoCommandCount() == 0 )
  304. return;
  305. /* Get the old list */
  306. PICKED_ITEMS_LIST* List = GetScreen()->PopCommandFromRedoList();
  307. /* Redo the command: */
  308. PutDataInPreviousState( List, true );
  309. /* Put the old list in UndoList */
  310. List->ReversePickersListOrder();
  311. GetScreen()->PushCommandToUndoList( List );
  312. OnModify();
  313. SetSheetNumberAndCount();
  314. GetScreen()->TestDanglingEnds();
  315. m_canvas->Refresh();
  316. }