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.

427 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
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 <ee_actions.h>
  25. #include <sch_edit_frame.h>
  26. #include <tool/tool_manager.h>
  27. #include <schematic.h>
  28. #include <sch_bus_entry.h>
  29. #include <sch_junction.h>
  30. #include <sch_line.h>
  31. #include <sch_bitmap.h>
  32. #include <tools/ee_selection_tool.h>
  33. #include <drawing_sheet/ds_proxy_undo_item.h>
  34. #include <tool/actions.h>
  35. /* Functions to undo and redo edit commands.
  36. *
  37. * m_UndoList and m_RedoList handle a std::vector of PICKED_ITEMS_LIST
  38. * Each PICKED_ITEMS_LIST handle a std::vector of pickers (class ITEM_PICKER),
  39. * that store the list of schematic items that are concerned by the command to
  40. * undo or redo and is created for each command to undo (handle also a command
  41. * to redo). each picker has a pointer pointing to an item to undo or redo (in
  42. * fact: deleted, added or modified), and has a pointer to a copy of this item,
  43. * when this item has been modified (the old values of parameters are
  44. * therefore saved)
  45. *
  46. * there are 3 cases:
  47. * - delete item(s) command
  48. * - change item(s) command
  49. * - add item(s) command
  50. * and 2 cases for block:
  51. * - move list of items
  52. * - mirror (Y) list of items
  53. *
  54. * Undo command
  55. * - delete item(s) command:
  56. * => deleted items are moved in undo list
  57. *
  58. * - change item(s) command
  59. * => A copy of item(s) is made (a DrawPickedStruct list of wrappers)
  60. * the .m_Link member of each wrapper points the modified item.
  61. * the .m_Item member of each wrapper points the old copy of this item.
  62. *
  63. * - add item(s) command
  64. * =>A list of item(s) is made. The .m_Item member of each wrapper points
  65. * the new item.
  66. *
  67. * Redo command
  68. * - delete item(s) old command:
  69. * => deleted items are moved into m_tree
  70. *
  71. * - change item(s) command
  72. * => the copy of item(s) is moved in Undo list
  73. *
  74. * - add item(s) command
  75. * => The list of item(s) is used to create a deleted list in undo
  76. * list(same as a delete command)
  77. *
  78. * Some block operations that change items can be undone without memorized
  79. * items, just the coordinates of the transform: move list of items (undo/
  80. * redo is made by moving with the opposite move vector) mirror (Y) and flip
  81. * list of items (undo/redo is made by mirror or flip items) so they are
  82. * handled specifically.
  83. *
  84. * A problem is the hierarchical sheet handling.
  85. * the data associated (sub-hierarchy, undo/redo list) is deleted only
  86. * when the sheet is really deleted (i.e. when deleted from undo or redo list)
  87. * This is handled by its destructor.
  88. */
  89. /* Used if undo / redo command:
  90. * swap data between Item and its copy, pointed by its picked item link member
  91. * swapped data is data modified by editing, so not all values are swapped
  92. */
  93. void SCH_EDIT_FRAME::StartNewUndo()
  94. {
  95. PICKED_ITEMS_LIST* blank = new PICKED_ITEMS_LIST();
  96. PushCommandToUndoList( blank );
  97. }
  98. void SCH_EDIT_FRAME::SaveCopyInUndoList( SCH_SCREEN* aScreen, SCH_ITEM* aItem,
  99. UNDO_REDO aCommandType, bool aAppend,
  100. bool aDirtyConnectivity )
  101. {
  102. PICKED_ITEMS_LIST* commandToUndo = nullptr;
  103. wxCHECK( aItem, /* void */ );
  104. if( aDirtyConnectivity )
  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, bool aAppend,
  150. bool aDirtyConnectivity )
  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. if( aDirtyConnectivity )
  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. // After hitting 0, subtracting 1 will roll the value over to its max representation
  228. for( unsigned ii = aList->GetCount() - 1; ii < std::numeric_limits<unsigned>::max(); ii-- )
  229. {
  230. UNDO_REDO status = aList->GetPickedItemStatus( ii );
  231. EDA_ITEM* eda_item = aList->GetPickedItem( ii );
  232. SCH_SCREEN* screen = dynamic_cast<SCH_SCREEN*>( aList->GetScreenForItem( ii ) );
  233. wxCHECK( screen, /* void */ );
  234. eda_item->SetFlags( aList->GetPickerFlags( 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. // If we are removing the current sheet, get out first
  244. if( SCH_SHEET* sheet = dyn_cast<SCH_SHEET*>( eda_item ) )
  245. {
  246. if( sheet->GetScreen() == GetScreen() )
  247. GetToolManager()->RunAction( EE_ACTIONS::leaveSheet );
  248. }
  249. RemoveFromScreen( eda_item, screen );
  250. aList->SetPickedItemStatus( UNDO_REDO::DELETED, ii );
  251. }
  252. else if( status == UNDO_REDO::DELETED )
  253. {
  254. // deleted items are re-inserted on undo
  255. AddToScreen( eda_item, screen );
  256. aList->SetPickedItemStatus( UNDO_REDO::NEWITEM, ii );
  257. }
  258. else if( status == UNDO_REDO::PAGESETTINGS )
  259. {
  260. SetCurrentSheet( *m_schematic->GetSheets().FindSheetForScreen( screen ) );
  261. DisplayCurrentSheet();
  262. // swap current settings with stored settings
  263. DS_PROXY_UNDO_ITEM alt_item( this );
  264. DS_PROXY_UNDO_ITEM* item = static_cast<DS_PROXY_UNDO_ITEM*>( eda_item );
  265. item->Restore( this );
  266. *item = alt_item;
  267. }
  268. else if( SCH_ITEM* item = dynamic_cast<SCH_ITEM*>( eda_item ) )
  269. {
  270. // everything else is modified in place
  271. SCH_ITEM* alt_item = static_cast<SCH_ITEM*>( aList->GetPickedItemLink( ii ) );
  272. // The root sheet is a pseudo object that owns the root screen object but is not on
  273. // the root screen so do not attempt to remove it from the screen it owns.
  274. if( item != &Schematic().Root() )
  275. RemoveFromScreen( item, screen );
  276. switch( status )
  277. {
  278. case UNDO_REDO::CHANGED:
  279. item->SwapData( alt_item );
  280. // Special cases for items which have instance data
  281. if( item->GetParent() && item->GetParent()->Type() == SCH_SYMBOL_T
  282. && item->Type() == SCH_FIELD_T )
  283. {
  284. SCH_FIELD* field = static_cast<SCH_FIELD*>( item );
  285. SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item->GetParent() );
  286. if( field->GetId() == REFERENCE_FIELD )
  287. {
  288. symbol->SetRef( m_schematic->GetSheets().FindSheetForScreen( screen ),
  289. field->GetText() );
  290. }
  291. else if( field->GetId() == VALUE_FIELD )
  292. {
  293. symbol->SetValue( m_schematic->GetSheets().FindSheetForScreen( screen ),
  294. field->GetText() );
  295. }
  296. else if( field->GetId() == FOOTPRINT_FIELD )
  297. {
  298. symbol->SetFootprint( m_schematic->GetSheets().FindSheetForScreen( screen ),
  299. field->GetText() );
  300. }
  301. }
  302. break;
  303. case UNDO_REDO::EXCHANGE_T:
  304. aList->SetPickedItem( alt_item, ii );
  305. aList->SetPickedItemLink( item, ii );
  306. item = alt_item;
  307. break;
  308. default:
  309. wxFAIL_MSG( wxString::Format( wxT( "Unknown undo/redo command %d" ),
  310. aList->GetPickedItemStatus( ii ) ) );
  311. break;
  312. }
  313. if( item->Type() == SCH_SYMBOL_T )
  314. {
  315. SCH_SYMBOL* sym = static_cast<SCH_SYMBOL*>( item );
  316. sym->UpdatePins();
  317. }
  318. if( item != &Schematic().Root() )
  319. AddToScreen( item, screen );
  320. }
  321. }
  322. GetCanvas()->GetView()->ClearHiddenFlags();
  323. }
  324. void SCH_EDIT_FRAME::RollbackSchematicFromUndo()
  325. {
  326. PICKED_ITEMS_LIST* undo = PopCommandFromUndoList();
  327. // Skip empty frames
  328. while( undo && ( !undo->GetCount()
  329. || ( undo->GetCount() == 1 && undo->GetPickedItemStatus( 0 ) == UNDO_REDO::NOP ) ) )
  330. {
  331. delete undo;
  332. undo = PopCommandFromUndoList();
  333. }
  334. if( undo )
  335. {
  336. PutDataInPreviousState( undo );
  337. undo->ClearListAndDeleteItems();
  338. delete undo;
  339. SetSheetNumberAndCount();
  340. UpdateHierarchyNavigator();
  341. TestDanglingEnds();
  342. m_toolManager->GetTool<EE_SELECTION_TOOL>()->RebuildSelection();
  343. }
  344. GetCanvas()->Refresh();
  345. }
  346. void SCH_EDIT_FRAME::ClearUndoORRedoList( UNDO_REDO_LIST whichList, int aItemCount )
  347. {
  348. if( aItemCount == 0 )
  349. return;
  350. UNDO_REDO_CONTAINER& list = whichList == UNDO_LIST ? m_undoList : m_redoList;
  351. for( PICKED_ITEMS_LIST* command : list.m_CommandsList )
  352. {
  353. command->ClearListAndDeleteItems();
  354. delete command;
  355. }
  356. list.m_CommandsList.clear();
  357. }