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.

439 lines
14 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
18 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-2017 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 <sch_draw_panel.h>
  30. #include <sch_edit_frame.h>
  31. #include <general.h>
  32. #include <list_operations.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. #include <sch_view.h>
  42. /* Functions to undo and redo edit commands.
  43. * commands to undo are stored in CurrentScreen->m_UndoList
  44. * commands to redo are stored in CurrentScreen->m_RedoList
  45. *
  46. * m_UndoList and m_RedoList handle a std::vector of PICKED_ITEMS_LIST
  47. * Each PICKED_ITEMS_LIST handle a std::vector of pickers (class ITEM_PICKER),
  48. * that store the list of schematic items that are concerned by the command to
  49. * undo or redo and is created for each command to undo (handle also a command
  50. * to redo). each picker has a pointer pointing to an item to undo or redo (in
  51. * fact: deleted, added or modified), and has a pointer to a copy of this item,
  52. * when this item has been modified (the old values of parameters are
  53. * therefore saved)
  54. *
  55. * there are 3 cases:
  56. * - delete item(s) command
  57. * - change item(s) command
  58. * - add item(s) command
  59. * and 2 cases for block:
  60. * - move list of items
  61. * - mirror (Y) list of items
  62. *
  63. * Undo command
  64. * - delete item(s) command:
  65. * => deleted items are moved in undo list
  66. *
  67. * - change item(s) command
  68. * => A copy of item(s) is made (a DrawPickedStruct list of wrappers)
  69. * the .m_Link member of each wrapper points the modified item.
  70. * the .m_Item member of each wrapper points the old copy of this item.
  71. *
  72. * - add item(s) command
  73. * =>A list of item(s) is made. The .m_Item member of each wrapper points
  74. * the new item.
  75. *
  76. * Redo command
  77. * - delete item(s) old command:
  78. * => deleted items are moved in GetDrawItems() list, and in
  79. *
  80. * - change item(s) command
  81. * => the copy of item(s) is moved in Undo list
  82. *
  83. * - add item(s) command
  84. * => The list of item(s) is used to create a deleted list in undo
  85. * list(same as a delete command)
  86. *
  87. * Some block operations that change items can be undone without memorized
  88. * items, just the coordinates of the transform: move list of items (undo/
  89. * redo is made by moving with the opposite move vector) mirror (Y) and flip
  90. * list of items (undo/redo is made by mirror or flip items) so they are
  91. * handled specifically.
  92. *
  93. * A problem is the hierarchical sheet handling.
  94. * the data associated (sub-hierarchy, undo/redo list) is deleted only
  95. * when the sheet is really deleted (i.e. when deleted from undo or redo list)
  96. * This is handled by its destructor.
  97. */
  98. /* Used if undo / redo command:
  99. * swap data between Item and its copy, pointed by its picked item link member
  100. * swapped data is data modified by editing, so not all values are swapped
  101. */
  102. void SCH_EDIT_FRAME::SaveCopyInUndoList( SCH_ITEM* aItem,
  103. UNDO_REDO_T aCommandType,
  104. bool aAppend,
  105. const wxPoint& aTransformPoint )
  106. {
  107. PICKED_ITEMS_LIST* commandToUndo = NULL;
  108. /* Does not save a null item or a UR_WIRE_IMAGE command type. UR_WIRE_IMAGE commands
  109. * are handled by the overloaded version of SaveCopyInUndoList that takes a reference
  110. * to a PICKED_ITEMS_LIST.
  111. */
  112. if( aItem == NULL || aCommandType == UR_WIRE_IMAGE )
  113. return;
  114. if( aAppend )
  115. commandToUndo = GetScreen()->PopCommandFromUndoList();
  116. if( !commandToUndo )
  117. {
  118. commandToUndo = new PICKED_ITEMS_LIST();
  119. commandToUndo->m_TransformPoint = aTransformPoint;
  120. }
  121. ITEM_PICKER itemWrapper( aItem, aCommandType );
  122. itemWrapper.SetFlags( aItem->GetFlags() );
  123. switch( aCommandType )
  124. {
  125. case UR_CHANGED: /* Create a copy of item */
  126. itemWrapper.SetLink( DuplicateStruct( aItem, true ) );
  127. commandToUndo->PushItem( itemWrapper );
  128. break;
  129. case UR_NEW:
  130. case UR_DELETED:
  131. case UR_ROTATED:
  132. case UR_MOVED:
  133. commandToUndo->PushItem( itemWrapper );
  134. break;
  135. default:
  136. wxFAIL_MSG( wxString::Format( wxT( "SaveCopyInUndoList() error (unknown code %X)" ),
  137. aCommandType ) );
  138. break;
  139. }
  140. if( commandToUndo->GetCount() )
  141. {
  142. /* Save the copy in undo list */
  143. GetScreen()->PushCommandToUndoList( commandToUndo );
  144. /* Clear redo list, because after new save there is no redo to do */
  145. GetScreen()->ClearUndoORRedoList( GetScreen()->m_RedoList );
  146. }
  147. else
  148. {
  149. delete commandToUndo;
  150. }
  151. }
  152. void SCH_EDIT_FRAME::SaveCopyInUndoList( const PICKED_ITEMS_LIST& aItemsList,
  153. UNDO_REDO_T aTypeCommand,
  154. bool aAppend,
  155. const wxPoint& aTransformPoint )
  156. {
  157. PICKED_ITEMS_LIST* commandToUndo = NULL;
  158. if( !aItemsList.GetCount() )
  159. return;
  160. // Can't append a WIRE IMAGE, so fail to a new undo point
  161. if( aAppend && ( aTypeCommand != UR_WIRE_IMAGE ) )
  162. {
  163. commandToUndo = GetScreen()->PopCommandFromUndoList();
  164. if( commandToUndo && commandToUndo->m_Status == UR_WIRE_IMAGE )
  165. {
  166. GetScreen()->PushCommandToUndoList( commandToUndo );
  167. commandToUndo = NULL;
  168. }
  169. }
  170. if( !commandToUndo )
  171. {
  172. commandToUndo = new PICKED_ITEMS_LIST();
  173. commandToUndo->m_TransformPoint = aTransformPoint;
  174. commandToUndo->m_Status = aTypeCommand;
  175. }
  176. // Copy picker list:
  177. if( !commandToUndo->GetCount() )
  178. commandToUndo->CopyList( aItemsList );
  179. else
  180. {
  181. // Unless we are appending, in which case, get the picker items
  182. for( unsigned ii = 0; ii < aItemsList.GetCount(); ii++ )
  183. commandToUndo->PushItem( aItemsList.GetItemWrapper( ii) );
  184. }
  185. // Verify list, and creates data if needed
  186. for( unsigned ii = 0; ii < commandToUndo->GetCount(); ii++ )
  187. {
  188. SCH_ITEM* item = (SCH_ITEM*) commandToUndo->GetPickedItem( ii );
  189. wxASSERT( item );
  190. UNDO_REDO_T command = commandToUndo->GetPickedItemStatus( ii );
  191. if( command == UR_UNSPECIFIED )
  192. {
  193. command = aTypeCommand;
  194. commandToUndo->SetPickedItemStatus( command, ii );
  195. }
  196. switch( command )
  197. {
  198. case UR_CHANGED: /* Create a copy of item */
  199. /* If needed, create a copy of item, and put in undo list
  200. * in the picker, as link
  201. * If this link is not null, the copy is already done
  202. */
  203. if( commandToUndo->GetPickedItemLink( ii ) == NULL )
  204. commandToUndo->SetPickedItemLink( DuplicateStruct( item, true ), ii );
  205. wxASSERT( commandToUndo->GetPickedItemLink( ii ) );
  206. break;
  207. case UR_MOVED:
  208. case UR_MIRRORED_Y:
  209. case UR_MIRRORED_X:
  210. case UR_ROTATED:
  211. case UR_NEW:
  212. case UR_DELETED:
  213. case UR_EXCHANGE_T:
  214. case UR_WIRE_IMAGE:
  215. break;
  216. default:
  217. wxFAIL_MSG( wxString::Format( wxT( "Unknown undo/redo command %d" ), command ) );
  218. break;
  219. }
  220. }
  221. if( commandToUndo->GetCount() || aTypeCommand == UR_WIRE_IMAGE )
  222. {
  223. /* Save the copy in undo list */
  224. GetScreen()->PushCommandToUndoList( commandToUndo );
  225. /* Clear redo list, because after new save there is no redo to do */
  226. GetScreen()->ClearUndoORRedoList( GetScreen()->m_RedoList );
  227. }
  228. else // Should not occur
  229. {
  230. delete commandToUndo;
  231. }
  232. }
  233. void SCH_EDIT_FRAME::PutDataInPreviousState( PICKED_ITEMS_LIST* aList, bool aRedoCommand )
  234. {
  235. SCH_ITEM* item;
  236. SCH_ITEM* next_item;
  237. SCH_ITEM* alt_item;
  238. // Exchange the current wires, buses, and junctions with the copy save by the last edit.
  239. if( aList->m_Status == UR_WIRE_IMAGE )
  240. {
  241. PICKED_ITEMS_LIST oldItems;
  242. oldItems.m_Status = UR_WIRE_IMAGE;
  243. // Remove all of the wires, buses, and junctions from the current screen.
  244. for( item = GetScreen()->GetDrawItems(); item; item = next_item )
  245. {
  246. next_item = item->Next();
  247. if( item->Type() == SCH_JUNCTION_T || item->Type() == SCH_LINE_T )
  248. {
  249. GetScreen()->Remove( item );
  250. GetCanvas()->GetView()->Remove( item );
  251. oldItems.PushItem( ITEM_PICKER( item, UR_WIRE_IMAGE ) );
  252. }
  253. }
  254. // Copy the saved wires, buses, and junctions to the current screen.
  255. for( unsigned int i = 0; i < aList->GetCount(); i++ )
  256. {
  257. item = static_cast<SCH_ITEM*>( aList->GetPickedItem( i ) );
  258. AddToScreen( item );
  259. }
  260. // Copy the previous wires, buses, and junctions to the picked item list for the
  261. // redo operation.
  262. *aList = oldItems;
  263. return;
  264. }
  265. // Undo in the reverse order of list creation: (this can allow stacked changes like the
  266. // same item can be changes and deleted in the same complex command.
  267. for( int ii = aList->GetCount() - 1; ii >= 0; ii-- )
  268. {
  269. item = (SCH_ITEM*) aList->GetPickedItem( (unsigned) ii );
  270. wxASSERT( item );
  271. item->ClearFlags();
  272. SCH_ITEM* image = (SCH_ITEM*) aList->GetPickedItemLink( (unsigned) ii );
  273. switch( aList->GetPickedItemStatus( (unsigned) ii ) )
  274. {
  275. case UR_CHANGED: /* Exchange old and new data for each item */
  276. item->SwapData( image );
  277. break;
  278. case UR_NEW: /* new items are deleted */
  279. aList->SetPickedItemStatus( UR_DELETED, (unsigned) ii );
  280. RemoveFromScreen( item );
  281. //schprintf("UndoRemFroMscreen %p %s\n", item, (const char *)item->GetClass().c_str() );
  282. break;
  283. case UR_DELETED: /* deleted items are put in the draw item list, as new items */
  284. aList->SetPickedItemStatus( UR_NEW, (unsigned) ii );
  285. AddToScreen( item );
  286. break;
  287. case UR_MOVED:
  288. item->ClearFlags();
  289. item->SetFlags( aList->GetPickerFlags( (unsigned) ii ) );
  290. item->Move( aRedoCommand ? aList->m_TransformPoint : -aList->m_TransformPoint );
  291. item->ClearFlags();
  292. break;
  293. case UR_MIRRORED_Y:
  294. item->MirrorY( aList->m_TransformPoint.x );
  295. break;
  296. case UR_MIRRORED_X:
  297. item->MirrorX( aList->m_TransformPoint.y );
  298. break;
  299. case UR_ROTATED:
  300. // To undo a rotate 90 deg transform we must rotate 270 deg to undo
  301. // and 90 deg to redo:
  302. item->Rotate( aList->m_TransformPoint );
  303. if( aRedoCommand )
  304. break; // A only one rotate transform is OK
  305. // Make 3 rotate 90 deg transforms is this is actually an undo command
  306. item->Rotate( aList->m_TransformPoint );
  307. item->Rotate( aList->m_TransformPoint );
  308. break;
  309. case UR_EXCHANGE_T:
  310. alt_item = (SCH_ITEM*) aList->GetPickedItemLink( (unsigned) ii );
  311. alt_item->SetNext( NULL );
  312. alt_item->SetBack( NULL );
  313. RemoveFromScreen( item );
  314. AddToScreen( alt_item );
  315. aList->SetPickedItem( alt_item, (unsigned) ii );
  316. aList->SetPickedItemLink( item, (unsigned) ii );
  317. break;
  318. default:
  319. wxFAIL_MSG( wxString::Format( wxT( "Unknown undo/redo command %d" ),
  320. aList->GetPickedItemStatus( (unsigned) ii ) ) );
  321. break;
  322. }
  323. }
  324. // Bitmaps are cached in Opengl: clear the cache, because
  325. // the cache data can be invalid
  326. GetCanvas()->GetGAL()->ClearCache();
  327. GetCanvas()->GetView()->UpdateAllItems( KIGFX::ALL );
  328. }
  329. void SCH_EDIT_FRAME::GetSchematicFromUndoList( wxCommandEvent& event )
  330. {
  331. if( GetScreen()->GetUndoCommandCount() <= 0 || isBusy() )
  332. return;
  333. /* Get the old list */
  334. PICKED_ITEMS_LIST* List = GetScreen()->PopCommandFromUndoList();
  335. /* Undo the command */
  336. PutDataInPreviousState( List, false );
  337. /* Put the old list in RedoList */
  338. List->ReversePickersListOrder();
  339. GetScreen()->PushCommandToRedoList( List );
  340. SetSheetNumberAndCount();
  341. TestDanglingEnds();
  342. SyncView();
  343. GetCanvas()->Refresh();
  344. OnModify();
  345. }
  346. void SCH_EDIT_FRAME::GetSchematicFromRedoList( wxCommandEvent& event )
  347. {
  348. if( GetScreen()->GetRedoCommandCount() == 0 || isBusy() )
  349. return;
  350. /* Get the old list */
  351. PICKED_ITEMS_LIST* List = GetScreen()->PopCommandFromRedoList();
  352. /* Redo the command: */
  353. PutDataInPreviousState( List, true );
  354. /* Put the old list in UndoList */
  355. List->ReversePickersListOrder();
  356. GetScreen()->PushCommandToUndoList( List );
  357. SetSheetNumberAndCount();
  358. TestDanglingEnds();
  359. SyncView();
  360. GetCanvas()->Refresh();
  361. OnModify();
  362. }