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.

396 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
18 years ago
17 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. if( aItem == NULL )
  109. return;
  110. // Connectivity may change
  111. aItem->SetConnectivityDirty();
  112. if( aAppend )
  113. commandToUndo = GetScreen()->PopCommandFromUndoList();
  114. if( !commandToUndo )
  115. {
  116. commandToUndo = new PICKED_ITEMS_LIST();
  117. commandToUndo->m_TransformPoint = aTransformPoint;
  118. }
  119. ITEM_PICKER itemWrapper( aItem, aCommandType );
  120. itemWrapper.SetFlags( aItem->GetFlags() );
  121. switch( aCommandType )
  122. {
  123. case UR_CHANGED: /* Create a copy of item */
  124. itemWrapper.SetLink( DuplicateStruct( aItem, true ) );
  125. commandToUndo->PushItem( itemWrapper );
  126. break;
  127. case UR_NEW:
  128. case UR_DELETED:
  129. case UR_ROTATED:
  130. case UR_MOVED:
  131. commandToUndo->PushItem( itemWrapper );
  132. break;
  133. default:
  134. wxFAIL_MSG( wxString::Format( wxT( "SaveCopyInUndoList() error (unknown code %X)" ),
  135. aCommandType ) );
  136. break;
  137. }
  138. if( commandToUndo->GetCount() )
  139. {
  140. /* Save the copy in undo list */
  141. GetScreen()->PushCommandToUndoList( commandToUndo );
  142. /* Clear redo list, because after new save there is no redo to do */
  143. GetScreen()->ClearUndoORRedoList( GetScreen()->m_RedoList );
  144. }
  145. else
  146. {
  147. delete commandToUndo;
  148. }
  149. }
  150. void SCH_EDIT_FRAME::SaveCopyInUndoList( const PICKED_ITEMS_LIST& aItemsList,
  151. UNDO_REDO_T aTypeCommand,
  152. bool aAppend,
  153. const wxPoint& aTransformPoint )
  154. {
  155. PICKED_ITEMS_LIST* commandToUndo = NULL;
  156. if( !aItemsList.GetCount() )
  157. return;
  158. // Can't append a WIRE IMAGE, so fail to a new undo point
  159. if( aAppend )
  160. commandToUndo = GetScreen()->PopCommandFromUndoList();
  161. if( !commandToUndo )
  162. {
  163. commandToUndo = new PICKED_ITEMS_LIST();
  164. commandToUndo->m_TransformPoint = aTransformPoint;
  165. commandToUndo->m_Status = aTypeCommand;
  166. }
  167. // Copy picker list:
  168. if( !commandToUndo->GetCount() )
  169. commandToUndo->CopyList( aItemsList );
  170. else
  171. {
  172. // Unless we are appending, in which case, get the picker items
  173. for( unsigned ii = 0; ii < aItemsList.GetCount(); ii++ )
  174. commandToUndo->PushItem( aItemsList.GetItemWrapper( ii) );
  175. }
  176. // Verify list, and creates data if needed
  177. for( unsigned ii = 0; ii < commandToUndo->GetCount(); ii++ )
  178. {
  179. SCH_ITEM* item = (SCH_ITEM*) commandToUndo->GetPickedItem( ii );
  180. wxASSERT( item );
  181. // Connectivity may change
  182. item->SetConnectivityDirty();
  183. UNDO_REDO_T command = commandToUndo->GetPickedItemStatus( ii );
  184. if( command == UR_UNSPECIFIED )
  185. {
  186. command = aTypeCommand;
  187. commandToUndo->SetPickedItemStatus( command, ii );
  188. }
  189. switch( command )
  190. {
  191. case UR_CHANGED: /* Create a copy of item */
  192. /* If needed, create a copy of item, and put in undo list
  193. * in the picker, as link
  194. * If this link is not null, the copy is already done
  195. */
  196. if( commandToUndo->GetPickedItemLink( ii ) == NULL )
  197. commandToUndo->SetPickedItemLink( DuplicateStruct( item, true ), ii );
  198. wxASSERT( commandToUndo->GetPickedItemLink( ii ) );
  199. break;
  200. case UR_MOVED:
  201. case UR_MIRRORED_Y:
  202. case UR_MIRRORED_X:
  203. case UR_ROTATED:
  204. case UR_NEW:
  205. case UR_DELETED:
  206. case UR_EXCHANGE_T:
  207. break;
  208. default:
  209. wxFAIL_MSG( wxString::Format( wxT( "Unknown undo/redo command %d" ), command ) );
  210. break;
  211. }
  212. }
  213. if( commandToUndo->GetCount() )
  214. {
  215. /* Save the copy in undo list */
  216. GetScreen()->PushCommandToUndoList( commandToUndo );
  217. /* Clear redo list, because after new save there is no redo to do */
  218. GetScreen()->ClearUndoORRedoList( GetScreen()->m_RedoList );
  219. }
  220. else // Should not occur
  221. {
  222. delete commandToUndo;
  223. }
  224. }
  225. void SCH_EDIT_FRAME::PutDataInPreviousState( PICKED_ITEMS_LIST* aList, bool aRedoCommand )
  226. {
  227. SCH_ITEM* item;
  228. SCH_ITEM* alt_item;
  229. // Undo in the reverse order of list creation: (this can allow stacked changes like the
  230. // same item can be changed and deleted in the same complex command).
  231. for( int ii = aList->GetCount() - 1; ii >= 0; ii-- )
  232. {
  233. UNDO_REDO_T status = aList->GetPickedItemStatus((unsigned) ii );
  234. item = (SCH_ITEM*) aList->GetPickedItem( (unsigned) ii );
  235. alt_item = (SCH_ITEM*) aList->GetPickedItemLink( (unsigned) ii );
  236. item->ClearFlags();
  237. if( status == UR_NEW )
  238. {
  239. // new items are deleted on undo
  240. RemoveFromScreen( item );
  241. aList->SetPickedItemStatus( UR_DELETED, (unsigned) ii );
  242. }
  243. else if (status == UR_DELETED )
  244. {
  245. // deleted items are re-inserted on undo
  246. AddToScreen( item );
  247. aList->SetPickedItemStatus( UR_NEW, (unsigned) ii );
  248. }
  249. else
  250. {
  251. // everthing else is modified in place
  252. RemoveFromScreen( item );
  253. switch( status )
  254. {
  255. case UR_CHANGED:
  256. item->SwapData( alt_item );
  257. break;
  258. case UR_MOVED:
  259. item->SetFlags( aList->GetPickerFlags( (unsigned) ii ) );
  260. item->Move( aRedoCommand ? aList->m_TransformPoint : -aList->m_TransformPoint );
  261. item->ClearFlags();
  262. break;
  263. case UR_MIRRORED_Y:
  264. item->MirrorY( aList->m_TransformPoint.x );
  265. break;
  266. case UR_MIRRORED_X:
  267. item->MirrorX( aList->m_TransformPoint.y );
  268. break;
  269. case UR_ROTATED:
  270. if( aRedoCommand )
  271. item->Rotate( aList->m_TransformPoint );
  272. else
  273. {
  274. // Rotate 270 deg to undo 90-deg rotate
  275. item->Rotate( aList->m_TransformPoint );
  276. item->Rotate( aList->m_TransformPoint );
  277. item->Rotate( aList->m_TransformPoint );
  278. }
  279. break;
  280. case UR_EXCHANGE_T:
  281. alt_item->SetNext( NULL );
  282. alt_item->SetBack( NULL );
  283. aList->SetPickedItem( alt_item, (unsigned) ii );
  284. aList->SetPickedItemLink( item, (unsigned) ii );
  285. item = alt_item;
  286. break;
  287. default:
  288. wxFAIL_MSG( wxString::Format( wxT( "Unknown undo/redo command %d" ),
  289. aList->GetPickedItemStatus( (unsigned) ii ) ) );
  290. break;
  291. }
  292. AddToScreen( item );
  293. }
  294. }
  295. // Bitmaps are cached in Opengl: clear the cache, because
  296. // the cache data can be invalid
  297. GetCanvas()->GetView()->RecacheAllItems();
  298. GetCanvas()->GetView()->ClearHiddenFlags();
  299. }
  300. void SCH_EDIT_FRAME::GetSchematicFromUndoList( wxCommandEvent& event )
  301. {
  302. if( GetScreen()->GetUndoCommandCount() <= 0 || isBusy() )
  303. return;
  304. /* Get the old list */
  305. PICKED_ITEMS_LIST* List = GetScreen()->PopCommandFromUndoList();
  306. /* Undo the command */
  307. PutDataInPreviousState( List, false );
  308. /* Put the old list in RedoList */
  309. List->ReversePickersListOrder();
  310. GetScreen()->PushCommandToRedoList( List );
  311. SetSheetNumberAndCount();
  312. TestDanglingEnds();
  313. SyncView();
  314. GetCanvas()->Refresh();
  315. OnModify();
  316. }
  317. void SCH_EDIT_FRAME::GetSchematicFromRedoList( wxCommandEvent& event )
  318. {
  319. if( GetScreen()->GetRedoCommandCount() == 0 || isBusy() )
  320. return;
  321. /* Get the old list */
  322. PICKED_ITEMS_LIST* List = GetScreen()->PopCommandFromRedoList();
  323. /* Redo the command: */
  324. PutDataInPreviousState( List, true );
  325. /* Put the old list in UndoList */
  326. List->ReversePickersListOrder();
  327. GetScreen()->PushCommandToUndoList( List );
  328. SetSheetNumberAndCount();
  329. TestDanglingEnds();
  330. SyncView();
  331. GetCanvas()->Refresh();
  332. OnModify();
  333. }