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.

580 lines
19 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
2 years ago
2 years ago
17 years ago
18 years ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year 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-2024 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_commit.h>
  30. #include <sch_junction.h>
  31. #include <sch_line.h>
  32. #include <sch_bitmap.h>
  33. #include <sch_sheet_pin.h>
  34. #include <sch_table.h>
  35. #include <tools/ee_selection_tool.h>
  36. #include <drawing_sheet/ds_proxy_undo_item.h>
  37. #include <tool/actions.h>
  38. #include <wx/log.h>
  39. /* Functions to undo and redo edit commands.
  40. *
  41. * m_UndoList and m_RedoList handle a std::vector of PICKED_ITEMS_LIST
  42. * Each PICKED_ITEMS_LIST handle a std::vector of pickers (class ITEM_PICKER),
  43. * that store the list of schematic items that are concerned by the command to
  44. * undo or redo and is created for each command to undo (handle also a command
  45. * to redo). each picker has a pointer pointing to an item to undo or redo (in
  46. * fact: deleted, added or modified), and has a pointer to a copy of this item,
  47. * when this item has been modified (the old values of parameters are
  48. * therefore saved)
  49. *
  50. * there are 3 cases:
  51. * - delete item(s) command
  52. * - change item(s) command
  53. * - add item(s) command
  54. * and 2 cases for block:
  55. * - move list of items
  56. * - mirror (Y) list of items
  57. *
  58. * Undo command
  59. * - delete item(s) command:
  60. * => deleted items are moved in undo list
  61. *
  62. * - change item(s) command
  63. * => A copy of item(s) is made (a DrawPickedStruct list of wrappers)
  64. * the .m_Link member of each wrapper points the modified item.
  65. * the .m_Item member of each wrapper points the old copy of this item.
  66. *
  67. * - add item(s) command
  68. * =>A list of item(s) is made. The .m_Item member of each wrapper points
  69. * the new item.
  70. *
  71. * Redo command
  72. * - delete item(s) old command:
  73. * => deleted items are moved into m_tree
  74. *
  75. * - change item(s) command
  76. * => the copy of item(s) is moved in Undo list
  77. *
  78. * - add item(s) command
  79. * => The list of item(s) is used to create a deleted list in undo
  80. * list(same as a delete command)
  81. *
  82. * Some block operations that change items can be undone without memorized
  83. * items, just the coordinates of the transform: move list of items (undo/
  84. * redo is made by moving with the opposite move vector) mirror (Y) and flip
  85. * list of items (undo/redo is made by mirror or flip items) so they are
  86. * handled specifically.
  87. *
  88. * A problem is the hierarchical sheet handling.
  89. * the data associated (sub-hierarchy, undo/redo list) is deleted only
  90. * when the sheet is really deleted (i.e. when deleted from undo or redo list)
  91. * This is handled by its destructor.
  92. */
  93. /* Used if undo / redo command:
  94. * swap data between Item and its copy, pointed by its picked item link member
  95. * swapped data is data modified by editing, so not all values are swapped
  96. */
  97. void SCH_EDIT_FRAME::SaveCopyInUndoList( SCH_SCREEN* aScreen, SCH_ITEM* aItem,
  98. UNDO_REDO aCommandType, bool aAppend,
  99. bool aDirtyConnectivity )
  100. {
  101. PICKED_ITEMS_LIST* commandToUndo = nullptr;
  102. wxCHECK( aItem, /* void */ );
  103. if( aDirtyConnectivity )
  104. {
  105. if( !aItem->IsConnectivityDirty() && aItem->Connection()
  106. && ( aItem->Connection()->Name() == m_highlightedConn
  107. || aItem->Connection()->HasDriverChanged() ) )
  108. {
  109. m_highlightedConnChanged = true;
  110. }
  111. aItem->SetConnectivityDirty();
  112. }
  113. PICKED_ITEMS_LIST* lastUndo = PopCommandFromUndoList();
  114. // If the last stack was empty, use that one instead of creating a new stack
  115. if( lastUndo )
  116. {
  117. if( aAppend || !lastUndo->GetCount() )
  118. commandToUndo = lastUndo;
  119. else
  120. PushCommandToUndoList( lastUndo );
  121. }
  122. if( !commandToUndo )
  123. {
  124. commandToUndo = new PICKED_ITEMS_LIST();
  125. }
  126. ITEM_PICKER itemWrapper( aScreen, aItem, aCommandType );
  127. itemWrapper.SetFlags( aItem->GetFlags() );
  128. switch( aCommandType )
  129. {
  130. case UNDO_REDO::CHANGED: /* Create a copy of item */
  131. itemWrapper.SetLink( aItem->Duplicate( true ) );
  132. commandToUndo->PushItem( itemWrapper );
  133. break;
  134. case UNDO_REDO::NEWITEM:
  135. case UNDO_REDO::DELETED:
  136. commandToUndo->PushItem( itemWrapper );
  137. break;
  138. default:
  139. wxFAIL_MSG( wxString::Format( wxT( "SaveCopyInUndoList() error (unknown code %X)" ),
  140. aCommandType ) );
  141. break;
  142. }
  143. if( commandToUndo->GetCount() )
  144. {
  145. /* Save the copy in undo list */
  146. PushCommandToUndoList( commandToUndo );
  147. /* Clear redo list, because after new save there is no redo to do */
  148. ClearUndoORRedoList( REDO_LIST );
  149. }
  150. else
  151. {
  152. delete commandToUndo;
  153. }
  154. }
  155. void SCH_EDIT_FRAME::SaveCopyInUndoList( const PICKED_ITEMS_LIST& aItemsList,
  156. UNDO_REDO aTypeCommand, bool aAppend,
  157. bool aDirtyConnectivity )
  158. {
  159. PICKED_ITEMS_LIST* commandToUndo = nullptr;
  160. if( !aItemsList.GetCount() )
  161. return;
  162. PICKED_ITEMS_LIST* lastUndo = PopCommandFromUndoList();
  163. // If the last stack was empty, use that one instead of creating a new stack
  164. if( lastUndo )
  165. {
  166. if( aAppend || !lastUndo->GetCount() )
  167. commandToUndo = lastUndo;
  168. else
  169. PushCommandToUndoList( lastUndo );
  170. }
  171. if( !commandToUndo )
  172. {
  173. commandToUndo = new PICKED_ITEMS_LIST();
  174. commandToUndo->SetDescription( aItemsList.GetDescription() );
  175. }
  176. // Copy picker list:
  177. if( !commandToUndo->GetCount() )
  178. {
  179. commandToUndo->CopyList( aItemsList );
  180. for( const std::unique_ptr<SCH_ITEM>& item : GetRepeatItems() )
  181. {
  182. EDA_ITEM* repeatItemClone = item->Clone();
  183. repeatItemClone->SetFlags( UR_TRANSIENT );
  184. ITEM_PICKER repeatItemPicker( nullptr, repeatItemClone, UNDO_REDO::REPEAT_ITEM );
  185. commandToUndo->PushItem( repeatItemPicker );
  186. }
  187. }
  188. else
  189. {
  190. // Unless we are appending, in which case, get the picker items
  191. for( unsigned ii = 0; ii < aItemsList.GetCount(); ii++ )
  192. commandToUndo->PushItem( aItemsList.GetItemWrapper( ii) );
  193. }
  194. // Verify list, and creates data if needed
  195. for( unsigned ii = 0; ii < commandToUndo->GetCount(); ii++ )
  196. {
  197. SCH_ITEM* sch_item = dynamic_cast<SCH_ITEM*>( commandToUndo->GetPickedItem( ii ) );
  198. // Common items implemented in EDA_DRAW_FRAME will not be SCH_ITEMs.
  199. if( !sch_item )
  200. continue;
  201. UNDO_REDO command = commandToUndo->GetPickedItemStatus( ii );
  202. if( command == UNDO_REDO::UNSPECIFIED )
  203. {
  204. command = aTypeCommand;
  205. commandToUndo->SetPickedItemStatus( command, ii );
  206. }
  207. switch( command )
  208. {
  209. case UNDO_REDO::CHANGED:
  210. /* If needed, create a copy of item, and put in undo list
  211. * in the picker, as link
  212. * If this link is not null, the copy is already done
  213. */
  214. if( commandToUndo->GetPickedItemLink( ii ) == nullptr )
  215. commandToUndo->SetPickedItemLink( sch_item->Duplicate( true ), ii );
  216. wxASSERT( commandToUndo->GetPickedItemLink( ii ) );
  217. break;
  218. case UNDO_REDO::NEWITEM:
  219. case UNDO_REDO::DELETED:
  220. case UNDO_REDO::PAGESETTINGS:
  221. case UNDO_REDO::REPEAT_ITEM:
  222. break;
  223. default:
  224. wxFAIL_MSG( wxString::Format( wxT( "Unknown undo/redo command %d" ), command ) );
  225. break;
  226. }
  227. }
  228. if( commandToUndo->GetCount() )
  229. {
  230. /* Save the copy in undo list */
  231. PushCommandToUndoList( commandToUndo );
  232. /* Clear redo list, because after new save there is no redo to do */
  233. ClearUndoORRedoList( REDO_LIST );
  234. }
  235. else // Should not occur
  236. {
  237. delete commandToUndo;
  238. }
  239. }
  240. void SCH_EDIT_FRAME::PutDataInPreviousState( PICKED_ITEMS_LIST* aList )
  241. {
  242. std::vector<SCH_ITEM*> bulkAddedItems;
  243. std::vector<SCH_ITEM*> bulkRemovedItems;
  244. std::vector<SCH_ITEM*> bulkChangedItems;
  245. std::set<SCH_TABLE*> changedTables;
  246. bool dirtyConnectivity = false;
  247. bool rebuildHierarchyNavigator = false;
  248. SCH_CLEANUP_FLAGS connectivityCleanUp = NO_CLEANUP;
  249. SCH_SHEET_LIST sheets;
  250. bool clearedRepeatItems = false;
  251. // Undo in the reverse order of list creation: (this can allow stacked changes like the
  252. // same item can be changed and deleted in the same complex command).
  253. // After hitting 0, subtracting 1 will roll the value over to its max representation
  254. for( unsigned ii = aList->GetCount() - 1; ii < std::numeric_limits<unsigned>::max(); ii-- )
  255. {
  256. UNDO_REDO status = aList->GetPickedItemStatus( ii );
  257. EDA_ITEM* eda_item = aList->GetPickedItem( ii );
  258. SCH_SCREEN* screen = dynamic_cast<SCH_SCREEN*>( aList->GetScreenForItem( ii ) );
  259. eda_item->SetFlags( aList->GetPickerFlags( ii ) );
  260. eda_item->ClearEditFlags();
  261. eda_item->ClearTempFlags();
  262. SCH_ITEM* schItem = dynamic_cast<SCH_ITEM*>( eda_item );
  263. // Set connectable object connectivity status.
  264. auto updateConnectivityFlag =
  265. [&]()
  266. {
  267. if( schItem->IsConnectable() )
  268. {
  269. schItem->SetConnectivityDirty();
  270. if( schItem->Type() == SCH_SYMBOL_T )
  271. {
  272. SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( schItem );
  273. wxCHECK( symbol, /* void */ );
  274. for( SCH_PIN* pin : symbol->GetPins() )
  275. pin->SetConnectivityDirty();
  276. }
  277. else if( schItem->Type() == SCH_SHEET_T )
  278. {
  279. SCH_SHEET* sheet = static_cast<SCH_SHEET*>( schItem );
  280. wxCHECK( sheet, /* void */ );
  281. for( SCH_SHEET_PIN* pin : sheet->GetPins() )
  282. pin->SetConnectivityDirty();
  283. }
  284. m_highlightedConnChanged = true;
  285. dirtyConnectivity = true;
  286. // Do a local clean up if there are any connectable objects in the commit
  287. if( connectivityCleanUp == NO_CLEANUP )
  288. connectivityCleanUp = LOCAL_CLEANUP;
  289. // Do a full rebauild of the connectivity if there is a sheet in the commit
  290. if( schItem->Type() == SCH_SHEET_T )
  291. connectivityCleanUp = GLOBAL_CLEANUP;
  292. }
  293. else if( schItem->Type() == SCH_RULE_AREA_T )
  294. {
  295. dirtyConnectivity = true;
  296. }
  297. };
  298. if( status == UNDO_REDO::NEWITEM )
  299. {
  300. if( schItem )
  301. updateConnectivityFlag();
  302. // If we are removing the current sheet, get out first
  303. if( eda_item->Type() == SCH_SHEET_T )
  304. {
  305. rebuildHierarchyNavigator = true;
  306. if( static_cast<SCH_SHEET*>( eda_item )->GetScreen() == GetScreen() )
  307. GetToolManager()->PostAction( EE_ACTIONS::leaveSheet );
  308. }
  309. RemoveFromScreen( eda_item, screen );
  310. aList->SetPickedItemStatus( UNDO_REDO::DELETED, ii );
  311. bulkRemovedItems.emplace_back( schItem );
  312. }
  313. else if( status == UNDO_REDO::DELETED )
  314. {
  315. if( eda_item->Type() == SCH_SHEET_T )
  316. rebuildHierarchyNavigator = true;
  317. if( schItem )
  318. updateConnectivityFlag();
  319. // deleted items are re-inserted on undo
  320. AddToScreen( eda_item, screen );
  321. aList->SetPickedItemStatus( UNDO_REDO::NEWITEM, ii );
  322. bulkAddedItems.emplace_back( schItem );
  323. }
  324. else if( status == UNDO_REDO::PAGESETTINGS )
  325. {
  326. // Lazy eval of sheet list; this is expensive even when unsorted
  327. if( sheets.empty() )
  328. sheets = m_schematic->BuildUnorderedSheetList();
  329. SCH_SHEET_PATH undoSheet = sheets.FindSheetForScreen( screen );
  330. if( GetCurrentSheet() != undoSheet )
  331. {
  332. SetCurrentSheet( undoSheet );
  333. DisplayCurrentSheet();
  334. }
  335. // swap current settings with stored settings
  336. DS_PROXY_UNDO_ITEM alt_item( this );
  337. DS_PROXY_UNDO_ITEM* item = static_cast<DS_PROXY_UNDO_ITEM*>( eda_item );
  338. item->Restore( this );
  339. *item = std::move( alt_item );
  340. }
  341. else if( status == UNDO_REDO::REPEAT_ITEM )
  342. {
  343. if( !clearedRepeatItems )
  344. {
  345. ClearRepeatItemsList();
  346. clearedRepeatItems = true;
  347. }
  348. if( schItem )
  349. AddCopyForRepeatItem( schItem );
  350. }
  351. else if( schItem )
  352. {
  353. SCH_ITEM* itemCopy = dynamic_cast<SCH_ITEM*>( aList->GetPickedItemLink( ii ) );
  354. wxCHECK2( itemCopy, continue );
  355. if( schItem->HasConnectivityChanges( itemCopy, &GetCurrentSheet() ) )
  356. updateConnectivityFlag();
  357. // The root sheet is a pseudo object that owns the root screen object but is not on
  358. // the root screen so do not attempt to remove it from the screen it owns.
  359. if( schItem != &Schematic().Root() )
  360. RemoveFromScreen( schItem, screen );
  361. switch( status )
  362. {
  363. case UNDO_REDO::CHANGED:
  364. if( schItem->Type() == SCH_SHEET_T )
  365. {
  366. const SCH_SHEET* origSheet = static_cast<const SCH_SHEET*>( schItem );
  367. const SCH_SHEET* copySheet = static_cast<const SCH_SHEET*>( itemCopy );
  368. wxCHECK2( origSheet && copySheet, continue );
  369. if( ( origSheet->GetName() != copySheet->GetName() )
  370. || ( origSheet->GetFileName() != copySheet->GetFileName() ) )
  371. {
  372. rebuildHierarchyNavigator = true;
  373. }
  374. }
  375. schItem->SwapData( itemCopy );
  376. bulkChangedItems.emplace_back( schItem );
  377. // Special cases for items which have instance data
  378. if( schItem->GetParent() && schItem->GetParent()->Type() == SCH_SYMBOL_T
  379. && schItem->Type() == SCH_FIELD_T )
  380. {
  381. SCH_FIELD* field = static_cast<SCH_FIELD*>( schItem );
  382. SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( schItem->GetParent() );
  383. if( field->GetId() == REFERENCE_FIELD )
  384. {
  385. // Lazy eval of sheet list; this is expensive even when unsorted
  386. if( sheets.empty() )
  387. sheets = m_schematic->BuildUnorderedSheetList();
  388. SCH_SHEET_PATH sheet = sheets.FindSheetForScreen( screen );
  389. symbol->SetRef( &sheet, field->GetText() );
  390. }
  391. bulkChangedItems.emplace_back( symbol );
  392. }
  393. break;
  394. default:
  395. wxFAIL_MSG( wxString::Format( wxT( "Unknown undo/redo command %d" ),
  396. aList->GetPickedItemStatus( ii ) ) );
  397. break;
  398. }
  399. if( schItem->Type() == SCH_SYMBOL_T )
  400. {
  401. SCH_SYMBOL* sym = static_cast<SCH_SYMBOL*>( schItem );
  402. sym->UpdatePins();
  403. }
  404. if( schItem != &Schematic().Root() )
  405. AddToScreen( schItem, screen );
  406. }
  407. }
  408. GetCanvas()->GetView()->ClearHiddenFlags();
  409. // Notify our listeners
  410. if( bulkAddedItems.size() > 0 )
  411. Schematic().OnItemsAdded( bulkAddedItems );
  412. if( bulkRemovedItems.size() > 0 )
  413. Schematic().OnItemsRemoved( bulkRemovedItems );
  414. if( bulkChangedItems.size() > 0 )
  415. Schematic().OnItemsChanged( bulkChangedItems );
  416. if( dirtyConnectivity )
  417. {
  418. wxLogTrace( wxS( "CONN_PROFILE" ),
  419. wxS( "Undo/redo %s clean up connectivity rebuild." ),
  420. ( connectivityCleanUp == LOCAL_CLEANUP ) ? wxS( "local" ) : wxS( "global" ) );
  421. SCH_COMMIT localCommit( m_toolManager );
  422. RecalculateConnections( &localCommit, connectivityCleanUp );
  423. // Update the hierarchy navigator when there are sheet changes.
  424. if( connectivityCleanUp == GLOBAL_CLEANUP )
  425. {
  426. SetSheetNumberAndCount();
  427. if( rebuildHierarchyNavigator )
  428. UpdateHierarchyNavigator();
  429. }
  430. }
  431. }
  432. void SCH_EDIT_FRAME::RollbackSchematicFromUndo()
  433. {
  434. PICKED_ITEMS_LIST* undo = PopCommandFromUndoList();
  435. // Skip empty frames
  436. while( undo && !undo->GetCount() )
  437. {
  438. delete undo;
  439. undo = PopCommandFromUndoList();
  440. }
  441. if( undo )
  442. {
  443. PutDataInPreviousState( undo );
  444. undo->ClearListAndDeleteItems( []( EDA_ITEM* aItem )
  445. {
  446. delete aItem;
  447. } );
  448. delete undo;
  449. m_toolManager->GetTool<EE_SELECTION_TOOL>()->RebuildSelection();
  450. }
  451. GetCanvas()->Refresh();
  452. }
  453. void SCH_EDIT_FRAME::ClearUndoORRedoList( UNDO_REDO_LIST whichList, int aItemCount )
  454. {
  455. if( aItemCount == 0 )
  456. return;
  457. UNDO_REDO_CONTAINER& list = ( whichList == UNDO_LIST ) ? m_undoList : m_redoList;
  458. if( aItemCount < 0 )
  459. {
  460. list.ClearCommandList();
  461. }
  462. else
  463. {
  464. for( int ii = 0; ii < aItemCount; ii++ )
  465. {
  466. if( list.m_CommandsList.size() == 0 )
  467. break;
  468. PICKED_ITEMS_LIST* curr_cmd = list.m_CommandsList[0];
  469. list.m_CommandsList.erase( list.m_CommandsList.begin() );
  470. curr_cmd->ClearListAndDeleteItems( []( EDA_ITEM* aItem )
  471. {
  472. delete aItem;
  473. } );
  474. delete curr_cmd; // Delete command
  475. }
  476. }
  477. }