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.

392 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
16 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-2021 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. #include <sch_edit_frame.h>
  25. #include <tool/tool_manager.h>
  26. #include <schematic.h>
  27. #include <sch_bus_entry.h>
  28. #include <sch_junction.h>
  29. #include <sch_line.h>
  30. #include <sch_bitmap.h>
  31. #include <tools/ee_selection_tool.h>
  32. #include <drawing_sheet/ds_proxy_undo_item.h>
  33. #include <tool/actions.h>
  34. /* Functions to undo and redo edit commands.
  35. *
  36. * m_UndoList and m_RedoList handle a std::vector of PICKED_ITEMS_LIST
  37. * Each PICKED_ITEMS_LIST handle a std::vector of pickers (class ITEM_PICKER),
  38. * that store the list of schematic items that are concerned by the command to
  39. * undo or redo and is created for each command to undo (handle also a command
  40. * to redo). each picker has a pointer pointing to an item to undo or redo (in
  41. * fact: deleted, added or modified), and has a pointer to a copy of this item,
  42. * when this item has been modified (the old values of parameters are
  43. * therefore saved)
  44. *
  45. * there are 3 cases:
  46. * - delete item(s) command
  47. * - change item(s) command
  48. * - add item(s) command
  49. * and 2 cases for block:
  50. * - move list of items
  51. * - mirror (Y) list of items
  52. *
  53. * Undo command
  54. * - delete item(s) command:
  55. * => deleted items are moved in undo list
  56. *
  57. * - change item(s) command
  58. * => A copy of item(s) is made (a DrawPickedStruct list of wrappers)
  59. * the .m_Link member of each wrapper points the modified item.
  60. * the .m_Item member of each wrapper points the old copy of this item.
  61. *
  62. * - add item(s) command
  63. * =>A list of item(s) is made. The .m_Item member of each wrapper points
  64. * the new item.
  65. *
  66. * Redo command
  67. * - delete item(s) old command:
  68. * => deleted items are moved into m_tree
  69. *
  70. * - change item(s) command
  71. * => the copy of item(s) is moved in Undo list
  72. *
  73. * - add item(s) command
  74. * => The list of item(s) is used to create a deleted list in undo
  75. * list(same as a delete command)
  76. *
  77. * Some block operations that change items can be undone without memorized
  78. * items, just the coordinates of the transform: move list of items (undo/
  79. * redo is made by moving with the opposite move vector) mirror (Y) and flip
  80. * list of items (undo/redo is made by mirror or flip items) so they are
  81. * handled specifically.
  82. *
  83. * A problem is the hierarchical sheet handling.
  84. * the data associated (sub-hierarchy, undo/redo list) is deleted only
  85. * when the sheet is really deleted (i.e. when deleted from undo or redo list)
  86. * This is handled by its destructor.
  87. */
  88. /* Used if undo / redo command:
  89. * swap data between Item and its copy, pointed by its picked item link member
  90. * swapped data is data modified by editing, so not all values are swapped
  91. */
  92. void SCH_EDIT_FRAME::StartNewUndo()
  93. {
  94. PICKED_ITEMS_LIST* blank = new PICKED_ITEMS_LIST();
  95. PushCommandToUndoList( blank );
  96. }
  97. void SCH_EDIT_FRAME::SaveCopyInUndoList( SCH_SCREEN* aScreen,
  98. SCH_ITEM* aItem,
  99. UNDO_REDO aCommandType,
  100. bool aAppend )
  101. {
  102. PICKED_ITEMS_LIST* commandToUndo = nullptr;
  103. wxCHECK( aItem, /* void */ );
  104. // Connectivity may change
  105. aItem->SetConnectivityDirty();
  106. PICKED_ITEMS_LIST* lastUndo = PopCommandFromUndoList();
  107. // If the last stack was empty, use that one instead of creating a new stack
  108. if( lastUndo )
  109. {
  110. if( aAppend || !lastUndo->GetCount() )
  111. commandToUndo = lastUndo;
  112. else
  113. PushCommandToUndoList( lastUndo );
  114. }
  115. if( !commandToUndo )
  116. {
  117. commandToUndo = new PICKED_ITEMS_LIST();
  118. }
  119. ITEM_PICKER itemWrapper( aScreen, aItem, aCommandType );
  120. itemWrapper.SetFlags( aItem->GetFlags() );
  121. switch( aCommandType )
  122. {
  123. case UNDO_REDO::CHANGED: /* Create a copy of item */
  124. itemWrapper.SetLink( aItem->Duplicate( true ) );
  125. commandToUndo->PushItem( itemWrapper );
  126. break;
  127. case UNDO_REDO::NEWITEM:
  128. case UNDO_REDO::DELETED:
  129. commandToUndo->PushItem( itemWrapper );
  130. break;
  131. default:
  132. wxFAIL_MSG( wxString::Format( wxT( "SaveCopyInUndoList() error (unknown code %X)" ),
  133. aCommandType ) );
  134. break;
  135. }
  136. if( commandToUndo->GetCount() )
  137. {
  138. /* Save the copy in undo list */
  139. PushCommandToUndoList( commandToUndo );
  140. /* Clear redo list, because after new save there is no redo to do */
  141. ClearUndoORRedoList( REDO_LIST );
  142. }
  143. else
  144. {
  145. delete commandToUndo;
  146. }
  147. }
  148. void SCH_EDIT_FRAME::SaveCopyInUndoList( const PICKED_ITEMS_LIST& aItemsList,
  149. UNDO_REDO aTypeCommand,
  150. bool aAppend )
  151. {
  152. PICKED_ITEMS_LIST* commandToUndo = nullptr;
  153. if( !aItemsList.GetCount() )
  154. return;
  155. PICKED_ITEMS_LIST* lastUndo = PopCommandFromUndoList();
  156. // If the last stack was empty, use that one instead of creating a new stack
  157. if( lastUndo )
  158. {
  159. if( aAppend || !lastUndo->GetCount() )
  160. commandToUndo = lastUndo;
  161. else
  162. PushCommandToUndoList( lastUndo );
  163. }
  164. if( !commandToUndo )
  165. commandToUndo = new PICKED_ITEMS_LIST();
  166. // Copy picker list:
  167. if( !commandToUndo->GetCount() )
  168. commandToUndo->CopyList( aItemsList );
  169. else
  170. {
  171. // Unless we are appending, in which case, get the picker items
  172. for( unsigned ii = 0; ii < aItemsList.GetCount(); ii++ )
  173. commandToUndo->PushItem( aItemsList.GetItemWrapper( ii) );
  174. }
  175. // Verify list, and creates data if needed
  176. for( unsigned ii = 0; ii < commandToUndo->GetCount(); ii++ )
  177. {
  178. SCH_ITEM* sch_item = dynamic_cast<SCH_ITEM*>( commandToUndo->GetPickedItem( ii ) );
  179. // Common items implemented in EDA_DRAW_FRAME will not be SCH_ITEMs.
  180. if( !sch_item )
  181. continue;
  182. // Connectivity may change
  183. sch_item->SetConnectivityDirty();
  184. UNDO_REDO command = commandToUndo->GetPickedItemStatus( ii );
  185. if( command == UNDO_REDO::UNSPECIFIED )
  186. {
  187. command = aTypeCommand;
  188. commandToUndo->SetPickedItemStatus( command, ii );
  189. }
  190. switch( command )
  191. {
  192. case UNDO_REDO::CHANGED:
  193. /* If needed, create a copy of item, and put in undo list
  194. * in the picker, as link
  195. * If this link is not null, the copy is already done
  196. */
  197. if( commandToUndo->GetPickedItemLink( ii ) == nullptr )
  198. commandToUndo->SetPickedItemLink( sch_item->Duplicate( true ), ii );
  199. wxASSERT( commandToUndo->GetPickedItemLink( ii ) );
  200. break;
  201. case UNDO_REDO::NEWITEM:
  202. case UNDO_REDO::DELETED:
  203. case UNDO_REDO::EXCHANGE_T:
  204. case UNDO_REDO::PAGESETTINGS:
  205. break;
  206. default:
  207. wxFAIL_MSG( wxString::Format( wxT( "Unknown undo/redo command %d" ), command ) );
  208. break;
  209. }
  210. }
  211. if( commandToUndo->GetCount() )
  212. {
  213. /* Save the copy in undo list */
  214. PushCommandToUndoList( commandToUndo );
  215. /* Clear redo list, because after new save there is no redo to do */
  216. ClearUndoORRedoList( REDO_LIST );
  217. }
  218. else // Should not occur
  219. {
  220. delete commandToUndo;
  221. }
  222. }
  223. void SCH_EDIT_FRAME::PutDataInPreviousState( PICKED_ITEMS_LIST* aList )
  224. {
  225. // Undo in the reverse order of list creation: (this can allow stacked changes like the
  226. // same item can be changed and deleted in the same complex command).
  227. for( int ii = aList->GetCount() - 1; ii >= 0; ii-- )
  228. {
  229. UNDO_REDO status = aList->GetPickedItemStatus((unsigned) ii );
  230. EDA_ITEM* eda_item = aList->GetPickedItem( (unsigned) ii );
  231. SCH_SCREEN* screen =
  232. dynamic_cast< SCH_SCREEN* >( aList->GetScreenForItem( (unsigned) ii ) );
  233. wxCHECK( screen, /* void */ );
  234. eda_item->SetFlags( aList->GetPickerFlags( (unsigned) ii ) );
  235. eda_item->ClearEditFlags();
  236. eda_item->ClearTempFlags();
  237. if( status == UNDO_REDO::NOP )
  238. {
  239. continue;
  240. }
  241. if( status == UNDO_REDO::NEWITEM )
  242. {
  243. // new items are deleted on undo
  244. RemoveFromScreen( eda_item, screen );
  245. aList->SetPickedItemStatus( UNDO_REDO::DELETED, (unsigned) ii );
  246. }
  247. else if( status == UNDO_REDO::DELETED )
  248. {
  249. // deleted items are re-inserted on undo
  250. AddToScreen( eda_item, screen );
  251. aList->SetPickedItemStatus( UNDO_REDO::NEWITEM, (unsigned) ii );
  252. }
  253. else if( status == UNDO_REDO::PAGESETTINGS )
  254. {
  255. // swap current settings with stored settings
  256. DS_PROXY_UNDO_ITEM alt_item( this );
  257. DS_PROXY_UNDO_ITEM* item = static_cast<DS_PROXY_UNDO_ITEM*>( eda_item );
  258. item->Restore( this );
  259. *item = alt_item;
  260. }
  261. else if( dynamic_cast<SCH_ITEM*>( eda_item ) )
  262. {
  263. // everything else is modified in place
  264. SCH_ITEM* item = (SCH_ITEM*) eda_item;
  265. SCH_ITEM* alt_item = (SCH_ITEM*) aList->GetPickedItemLink( (unsigned) ii );
  266. // The root sheet is a pseudo object that owns the root screen object but is not on
  267. // the root screen so do not attempt to remove it from the screen it owns.
  268. if( item != &Schematic().Root() )
  269. RemoveFromScreen( item, screen );
  270. switch( status )
  271. {
  272. case UNDO_REDO::CHANGED:
  273. item->SwapData( alt_item );
  274. break;
  275. case UNDO_REDO::EXCHANGE_T:
  276. aList->SetPickedItem( alt_item, (unsigned) ii );
  277. aList->SetPickedItemLink( item, (unsigned) ii );
  278. item = alt_item;
  279. break;
  280. default:
  281. wxFAIL_MSG( wxString::Format( wxT( "Unknown undo/redo command %d" ),
  282. aList->GetPickedItemStatus( (unsigned) ii ) ) );
  283. break;
  284. }
  285. if( item != &Schematic().Root() )
  286. AddToScreen( item, screen );
  287. }
  288. }
  289. // Bitmaps are cached in Opengl: clear the cache, because
  290. // the cache data can be invalid
  291. GetCanvas()->GetView()->RecacheAllItems();
  292. GetCanvas()->GetView()->ClearHiddenFlags();
  293. }
  294. void SCH_EDIT_FRAME::RollbackSchematicFromUndo()
  295. {
  296. PICKED_ITEMS_LIST* undo = PopCommandFromUndoList();
  297. // Skip empty frames
  298. while( undo && ( !undo->GetCount()
  299. || ( undo->GetCount() == 1 && undo->GetPickedItemStatus( 0 ) == UNDO_REDO::NOP ) ) )
  300. {
  301. delete undo;
  302. undo = PopCommandFromUndoList();
  303. }
  304. if( undo )
  305. {
  306. PutDataInPreviousState( undo );
  307. undo->ClearListAndDeleteItems();
  308. delete undo;
  309. SetSheetNumberAndCount();
  310. UpdateHierarchyNavigator();
  311. TestDanglingEnds();
  312. m_toolManager->GetTool<EE_SELECTION_TOOL>()->RebuildSelection();
  313. }
  314. SyncView();
  315. GetCanvas()->Refresh();
  316. }
  317. void SCH_EDIT_FRAME::ClearUndoORRedoList( UNDO_REDO_LIST whichList, int aItemCount )
  318. {
  319. if( aItemCount == 0 )
  320. return;
  321. UNDO_REDO_CONTAINER& list = whichList == UNDO_LIST ? m_undoList : m_redoList;
  322. for( PICKED_ITEMS_LIST* command : list.m_CommandsList )
  323. {
  324. command->ClearListAndDeleteItems();
  325. delete command;
  326. }
  327. list.m_CommandsList.clear();
  328. }