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.

605 lines
20 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2012 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
  5. * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  6. * Copyright (C) 2016 CERN
  7. * Copyright (C) 2012-2022 KiCad Developers, see AUTHORS.txt for contributors.
  8. * @author Maciej Suminski <maciej.suminski@cern.ch>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, you may find one here:
  22. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  23. * or you may search the http://www.gnu.org website for the version 2 license,
  24. * or you may write to the Free Software Foundation, Inc.,
  25. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  26. */
  27. #include <functional>
  28. using namespace std::placeholders;
  29. #include <macros.h>
  30. #include <pcb_edit_frame.h>
  31. #include <board.h>
  32. #include <pcb_track.h>
  33. #include <pcb_group.h>
  34. #include <pcb_target.h>
  35. #include <footprint.h>
  36. #include <pad.h>
  37. #include <origin_viewitem.h>
  38. #include <connectivity/connectivity_data.h>
  39. #include <tool/tool_manager.h>
  40. #include <tool/actions.h>
  41. #include <tools/pcb_selection_tool.h>
  42. #include <tools/pcb_control.h>
  43. #include <tools/board_editor_control.h>
  44. #include <drawing_sheet/ds_proxy_undo_item.h>
  45. #include <wx/msgdlg.h>
  46. /* Functions to undo and redo edit commands.
  47. * commands to undo are stored in CurrentScreen->m_UndoList
  48. * commands to redo are stored in CurrentScreen->m_RedoList
  49. *
  50. * m_UndoList and m_RedoList handle a std::vector of PICKED_ITEMS_LIST
  51. * Each PICKED_ITEMS_LIST handle a std::vector of pickers (class ITEM_PICKER),
  52. * that store the list of schematic items that are concerned by the command to undo or redo
  53. * and is created for each command to undo (handle also a command to redo).
  54. * each picker has a pointer pointing to an item to undo or redo (in fact: deleted, added or
  55. * modified),
  56. * and has a pointer to a copy of this item, when this item has been modified
  57. * (the old values of parameters are therefore saved)
  58. *
  59. * there are 3 cases:
  60. * - delete item(s) command
  61. * - change item(s) command
  62. * - add item(s) command
  63. *
  64. * Undo command
  65. * - delete item(s) command:
  66. * => deleted items are moved in undo list
  67. *
  68. * - change item(s) command
  69. * => A copy of item(s) is made (a DrawPickedStruct list of wrappers)
  70. * the .m_Link member of each wrapper points the modified item.
  71. * the .m_Item member of each wrapper points the old copy of this item.
  72. *
  73. * - add item(s) command
  74. * =>A list of item(s) is made. The .m_Item member of each wrapper points the new item.
  75. *
  76. * Redo command
  77. * - delete item(s) old command:
  78. * => deleted items are moved in EEDrawList 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 list(same as a delete
  85. * command)
  86. *
  87. * Some block operations that change items can be undone without memorize items, just the
  88. * coordinates of the transform:
  89. * move list of items (undo/redo is made by moving with the opposite move vector)
  90. * mirror (Y) and flip list of items (undo/redo is made by mirror or flip items)
  91. * so they are handled specifically.
  92. *
  93. */
  94. /**
  95. * Test if aItem exists somewhere in undo/redo lists of items. Used by PutDataInPreviousState
  96. * to be sure an item was not deleted since an undo or redo.
  97. *
  98. * This could be possible:
  99. * - if a call to SaveCopyInUndoList was forgotten in Pcbnew
  100. * - in zones outlines, when a change in one zone merges this zone with an other
  101. * Before using this function to test existence of items, it must be called with aItem = NULL to
  102. * prepare the list.
  103. *
  104. * @param aPcb is the board to test.
  105. * @param aItem is the item to find or NULL to build the list of existing items.
  106. */
  107. static bool TestForExistingItem( BOARD* aPcb, BOARD_ITEM* aItem )
  108. {
  109. for( PCB_TRACK* item : aPcb->Tracks() )
  110. {
  111. if( aItem == static_cast<BOARD_ITEM*>( item ) )
  112. return true;
  113. }
  114. for( FOOTPRINT* item : aPcb->Footprints() )
  115. {
  116. if( aItem == static_cast<BOARD_ITEM*>( item ) )
  117. return true;
  118. }
  119. for( BOARD_ITEM* item : aPcb->Drawings() )
  120. {
  121. if( aItem == static_cast<BOARD_ITEM*>( item ) )
  122. return true;
  123. }
  124. for( ZONE* item : aPcb->Zones() )
  125. {
  126. if( aItem == static_cast<BOARD_ITEM*>( item ) )
  127. return true;
  128. }
  129. NETINFO_LIST& netInfo = aPcb->GetNetInfo();
  130. for( NETINFO_LIST::iterator i = netInfo.begin(); i != netInfo.end(); ++i )
  131. {
  132. if( aItem == static_cast<BOARD_ITEM*>( *i ) )
  133. return true;
  134. }
  135. for( PCB_GROUP* item : aPcb->Groups() )
  136. {
  137. if( aItem == static_cast<BOARD_ITEM*>( item ) )
  138. return true;
  139. }
  140. return false;
  141. }
  142. void PCB_BASE_EDIT_FRAME::saveCopyInUndoList( PICKED_ITEMS_LIST* commandToUndo,
  143. const PICKED_ITEMS_LIST& aItemsList,
  144. UNDO_REDO aCommandType )
  145. {
  146. int preExisting = commandToUndo->GetCount();
  147. // First, filter unnecessary stuff from the list (i.e. for multiple pads / labels modified),
  148. // take the first occurrence of the footprint (we save copies of footprints when one of its
  149. // subitems is changed).
  150. for( unsigned ii = 0; ii < aItemsList.GetCount(); ii++ )
  151. {
  152. ITEM_PICKER curr_picker = aItemsList.GetItemWrapper(ii);
  153. BOARD_ITEM* item = dynamic_cast<BOARD_ITEM*>( aItemsList.GetPickedItem( ii ) );
  154. // For items belonging to footprints, we need to save state of the parent footprint
  155. if( item && item->GetParent() && item->GetParent()->Type() == PCB_FOOTPRINT_T )
  156. {
  157. item = item->GetParent();
  158. // Check if the parent footprint has already been saved in another entry
  159. bool found = false;
  160. for( unsigned j = 0; j < commandToUndo->GetCount(); j++ )
  161. {
  162. if( commandToUndo->GetPickedItem( j ) == item
  163. && commandToUndo->GetPickedItemStatus( j ) == UNDO_REDO::CHANGED )
  164. {
  165. found = true;
  166. break;
  167. }
  168. }
  169. if( !found )
  170. {
  171. // Create a clean copy of the parent footprint
  172. FOOTPRINT* orig = static_cast<FOOTPRINT*>( item );
  173. FOOTPRINT* clone = new FOOTPRINT( *orig );
  174. clone->SetParent( GetBoard() );
  175. clone->SetParentGroup( nullptr );
  176. // Clear current flags (which can be temporary set by a current edit command)
  177. for( BOARD_ITEM* child : clone->GraphicalItems() )
  178. child->ClearEditFlags();
  179. for( PAD* pad : clone->Pads() )
  180. pad->ClearEditFlags();
  181. clone->Reference().ClearEditFlags();
  182. clone->Value().ClearEditFlags();
  183. ITEM_PICKER picker( nullptr, item, UNDO_REDO::CHANGED );
  184. picker.SetLink( clone );
  185. commandToUndo->PushItem( picker );
  186. }
  187. else
  188. {
  189. continue;
  190. }
  191. }
  192. else
  193. {
  194. // Normal case: all other BOARD_ITEMs, are simply copied to the new list
  195. commandToUndo->PushItem( curr_picker );
  196. }
  197. }
  198. for( unsigned ii = preExisting; ii < commandToUndo->GetCount(); ii++ )
  199. {
  200. EDA_ITEM* item = commandToUndo->GetPickedItem( ii );
  201. UNDO_REDO command = commandToUndo->GetPickedItemStatus( ii );
  202. if( command == UNDO_REDO::UNSPECIFIED )
  203. {
  204. command = aCommandType;
  205. commandToUndo->SetPickedItemStatus( command, ii );
  206. }
  207. wxASSERT( item );
  208. switch( command )
  209. {
  210. case UNDO_REDO::CHANGED:
  211. case UNDO_REDO::DRILLORIGIN:
  212. case UNDO_REDO::GRIDORIGIN:
  213. // If we don't yet have a copy in the link, set one up
  214. if( !commandToUndo->GetPickedItemLink( ii ) )
  215. {
  216. BOARD_ITEM* clone = static_cast<BOARD_ITEM*>( item->Clone() );
  217. clone->SetParentGroup( nullptr );
  218. commandToUndo->SetPickedItemLink( clone, ii );
  219. }
  220. break;
  221. case UNDO_REDO::NEWITEM:
  222. case UNDO_REDO::DELETED:
  223. case UNDO_REDO::PAGESETTINGS:
  224. case UNDO_REDO::REGROUP:
  225. case UNDO_REDO::UNGROUP:
  226. break;
  227. default:
  228. wxFAIL_MSG( wxString::Format( wxT( "SaveCopyInUndoList() error (unknown code %X)" ),
  229. command ) );
  230. break;
  231. }
  232. }
  233. if( commandToUndo->GetCount() )
  234. {
  235. /* Save the copy in undo list */
  236. PushCommandToUndoList( commandToUndo );
  237. /* Clear redo list, because after a new command one cannot redo a command */
  238. ClearUndoORRedoList( REDO_LIST );
  239. }
  240. else
  241. {
  242. // Should not occur
  243. wxASSERT( false );
  244. delete commandToUndo;
  245. }
  246. }
  247. void PCB_BASE_EDIT_FRAME::SaveCopyInUndoList( EDA_ITEM* aItem, UNDO_REDO aCommandType )
  248. {
  249. PICKED_ITEMS_LIST* commandToUndo = new PICKED_ITEMS_LIST();
  250. PICKED_ITEMS_LIST itemsList;
  251. itemsList.PushItem( ITEM_PICKER( nullptr, aItem, aCommandType ) );
  252. saveCopyInUndoList( commandToUndo, itemsList, aCommandType );
  253. }
  254. void PCB_BASE_EDIT_FRAME::SaveCopyInUndoList( const PICKED_ITEMS_LIST& aItemsList,
  255. UNDO_REDO aCommandType )
  256. {
  257. PICKED_ITEMS_LIST* commandToUndo = new PICKED_ITEMS_LIST();
  258. saveCopyInUndoList( commandToUndo, aItemsList, aCommandType );
  259. }
  260. void PCB_BASE_EDIT_FRAME::AppendCopyToUndoList( const PICKED_ITEMS_LIST& aItemsList,
  261. UNDO_REDO aCommandType )
  262. {
  263. PICKED_ITEMS_LIST* commandToUndo = PopCommandFromUndoList();
  264. if( !commandToUndo )
  265. commandToUndo = new PICKED_ITEMS_LIST();
  266. saveCopyInUndoList( commandToUndo, aItemsList, aCommandType );
  267. }
  268. void PCB_BASE_EDIT_FRAME::RestoreCopyFromUndoList( wxCommandEvent& aEvent )
  269. {
  270. if( UndoRedoBlocked() )
  271. return;
  272. if( GetUndoCommandCount() <= 0 )
  273. return;
  274. // Inform tools that undo command was issued
  275. m_toolManager->ProcessEvent( { TC_MESSAGE, TA_UNDO_REDO_PRE, AS_GLOBAL } );
  276. // Get the old list
  277. PICKED_ITEMS_LIST* list = PopCommandFromUndoList();
  278. // Undo the command
  279. PutDataInPreviousState( list );
  280. // Put the old list in RedoList
  281. list->ReversePickersListOrder();
  282. PushCommandToRedoList( list );
  283. OnModify();
  284. m_toolManager->ProcessEvent( { TC_MESSAGE, TA_UNDO_REDO_POST, AS_GLOBAL } );
  285. m_toolManager->PostEvent( EVENTS::SelectedItemsModified );
  286. GetCanvas()->Refresh();
  287. }
  288. void PCB_BASE_EDIT_FRAME::RestoreCopyFromRedoList( wxCommandEvent& aEvent )
  289. {
  290. if( UndoRedoBlocked() )
  291. return;
  292. if( GetRedoCommandCount() == 0 )
  293. return;
  294. // Inform tools that redo command was issued
  295. m_toolManager->ProcessEvent( { TC_MESSAGE, TA_UNDO_REDO_PRE, AS_GLOBAL } );
  296. // Get the old list
  297. PICKED_ITEMS_LIST* list = PopCommandFromRedoList();
  298. // Redo the command
  299. PutDataInPreviousState( list );
  300. // Put the old list in UndoList
  301. list->ReversePickersListOrder();
  302. PushCommandToUndoList( list );
  303. OnModify();
  304. m_toolManager->ProcessEvent( { TC_MESSAGE, TA_UNDO_REDO_POST, AS_GLOBAL } );
  305. m_toolManager->PostEvent( EVENTS::SelectedItemsModified );
  306. GetCanvas()->Refresh();
  307. }
  308. void PCB_BASE_EDIT_FRAME::PutDataInPreviousState( PICKED_ITEMS_LIST* aList )
  309. {
  310. bool not_found = false;
  311. bool reBuild_ratsnest = false;
  312. bool deep_reBuild_ratsnest = false; // true later if pointers must be rebuilt
  313. auto view = GetCanvas()->GetView();
  314. auto connectivity = GetBoard()->GetConnectivity();
  315. PCB_GROUP* group = nullptr;
  316. GetBoard()->IncrementTimeStamp(); // clear caches
  317. // Undo in the reverse order of list creation: (this can allow stacked changes
  318. // like the same item can be changes and deleted in the same complex command
  319. // Restore changes in reverse order
  320. for( int ii = aList->GetCount() - 1; ii >= 0 ; ii-- )
  321. {
  322. EDA_ITEM* eda_item = aList->GetPickedItem( (unsigned) ii );
  323. /* Test for existence of item on board.
  324. * It could be deleted, and no more on board:
  325. * - if a call to SaveCopyInUndoList was forgotten in Pcbnew
  326. * - in zones outlines, when a change in one zone merges this zone with an other
  327. * This test avoids a Pcbnew crash
  328. * Obviously, this test is not made for deleted items
  329. */
  330. UNDO_REDO status = aList->GetPickedItemStatus( ii );
  331. if( status != UNDO_REDO::DELETED
  332. && status != UNDO_REDO::DRILLORIGIN // origin markers never on board
  333. && status != UNDO_REDO::GRIDORIGIN // origin markers never on board
  334. && status != UNDO_REDO::PAGESETTINGS ) // nor are page settings proxy items
  335. {
  336. if( !TestForExistingItem( GetBoard(), (BOARD_ITEM*) eda_item ) )
  337. {
  338. // Checking if it ever happens
  339. wxASSERT_MSG( false, wxT( "Item in the undo buffer does not exist" ) );
  340. // Remove this non existent item
  341. aList->RemovePicker( ii );
  342. not_found = true;
  343. if( aList->GetCount() == 0 )
  344. break;
  345. continue;
  346. }
  347. }
  348. // see if we must rebuild ratsnets and pointers lists
  349. switch( eda_item->Type() )
  350. {
  351. case PCB_FOOTPRINT_T:
  352. deep_reBuild_ratsnest = true; // Pointers on pads can be invalid
  353. KI_FALLTHROUGH;
  354. case PCB_ZONE_T:
  355. case PCB_TRACE_T:
  356. case PCB_ARC_T:
  357. case PCB_VIA_T:
  358. case PCB_PAD_T:
  359. reBuild_ratsnest = true;
  360. break;
  361. case PCB_NETINFO_T:
  362. reBuild_ratsnest = true;
  363. deep_reBuild_ratsnest = true;
  364. break;
  365. default:
  366. break;
  367. }
  368. switch( aList->GetPickedItemStatus( ii ) )
  369. {
  370. case UNDO_REDO::CHANGED: /* Exchange old and new data for each item */
  371. {
  372. BOARD_ITEM* item = (BOARD_ITEM*) eda_item;
  373. BOARD_ITEM* image = (BOARD_ITEM*) aList->GetPickedItemLink( ii );
  374. // Remove all pads/drawings/texts, as they become invalid
  375. // for the VIEW after SwapItemData() called for footprints
  376. view->Remove( item );
  377. connectivity->Remove( item );
  378. item->SwapItemData( image );
  379. if( item->Type() == PCB_GROUP_T )
  380. {
  381. group = static_cast<PCB_GROUP*>( item );
  382. group->RunOnChildren( [&]( BOARD_ITEM* child )
  383. {
  384. child->SetParentGroup( group );
  385. } );
  386. }
  387. view->Add( item );
  388. view->Hide( item, false );
  389. connectivity->Add( item );
  390. item->GetBoard()->OnItemChanged( item );
  391. break;
  392. }
  393. case UNDO_REDO::NEWITEM: /* new items are deleted */
  394. aList->SetPickedItemStatus( UNDO_REDO::DELETED, ii );
  395. GetModel()->Remove( (BOARD_ITEM*) eda_item );
  396. if( eda_item->Type() != PCB_NETINFO_T )
  397. view->Remove( eda_item );
  398. break;
  399. case UNDO_REDO::DELETED: /* deleted items are put in List, as new items */
  400. aList->SetPickedItemStatus( UNDO_REDO::NEWITEM, ii );
  401. GetModel()->Add( (BOARD_ITEM*) eda_item );
  402. if( eda_item->Type() != PCB_NETINFO_T )
  403. view->Add( eda_item );
  404. if( eda_item->Type() == PCB_GROUP_T )
  405. group = static_cast<PCB_GROUP*>( eda_item );
  406. break;
  407. case UNDO_REDO::REGROUP:
  408. aList->SetPickedItemStatus( UNDO_REDO::UNGROUP, ii );
  409. static_cast<BOARD_ITEM*>( eda_item )->SetParentGroup( nullptr );
  410. break;
  411. case UNDO_REDO::UNGROUP:
  412. aList->SetPickedItemStatus( UNDO_REDO::REGROUP, ii );
  413. if( group )
  414. group->AddItem( static_cast<BOARD_ITEM*>( eda_item ) );
  415. break;
  416. case UNDO_REDO::DRILLORIGIN:
  417. case UNDO_REDO::GRIDORIGIN:
  418. {
  419. BOARD_ITEM* item = (BOARD_ITEM*) eda_item;
  420. BOARD_ITEM* image = (BOARD_ITEM*) aList->GetPickedItemLink( ii );
  421. VECTOR2D origin = image->GetPosition();
  422. image->SetPosition( eda_item->GetPosition() );
  423. if( aList->GetPickedItemStatus( ii ) == UNDO_REDO::DRILLORIGIN )
  424. BOARD_EDITOR_CONTROL::DoSetDrillOrigin( view, this, item, origin );
  425. else
  426. PCB_CONTROL::DoSetGridOrigin( view, this, item, origin );
  427. break;
  428. }
  429. case UNDO_REDO::PAGESETTINGS:
  430. {
  431. // swap current settings with stored settings
  432. DS_PROXY_UNDO_ITEM alt_item( this );
  433. DS_PROXY_UNDO_ITEM* item = static_cast<DS_PROXY_UNDO_ITEM*>( eda_item );
  434. item->Restore( this );
  435. *item = alt_item;
  436. break;
  437. }
  438. default:
  439. wxFAIL_MSG( wxString::Format( wxT( "PutDataInPreviousState() error (unknown code %X)" ),
  440. aList->GetPickedItemStatus( ii ) ) );
  441. break;
  442. }
  443. }
  444. if( not_found )
  445. wxMessageBox( _( "Incomplete undo/redo operation: some items not found" ) );
  446. // Rebuild pointers and connectivity that can be changed.
  447. // connectivity can be rebuilt only in the board editor frame
  448. if( IsType( FRAME_PCB_EDITOR ) && ( reBuild_ratsnest || deep_reBuild_ratsnest ) )
  449. {
  450. Compile_Ratsnest( false );
  451. }
  452. PCB_SELECTION_TOOL* selTool = m_toolManager->GetTool<PCB_SELECTION_TOOL>();
  453. selTool->RebuildSelection();
  454. GetBoard()->SanitizeNetcodes();
  455. }
  456. void PCB_BASE_EDIT_FRAME::ClearUndoORRedoList( UNDO_REDO_LIST whichList, int aItemCount )
  457. {
  458. if( aItemCount == 0 )
  459. return;
  460. UNDO_REDO_CONTAINER& list = whichList == UNDO_LIST ? m_undoList : m_redoList;
  461. unsigned icnt = list.m_CommandsList.size();
  462. if( aItemCount > 0 )
  463. icnt = aItemCount;
  464. for( unsigned ii = 0; ii < icnt; 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. ClearListAndDeleteItems( curr_cmd );
  471. delete curr_cmd; // Delete command
  472. }
  473. }
  474. void PCB_BASE_EDIT_FRAME::ClearListAndDeleteItems( PICKED_ITEMS_LIST* aList )
  475. {
  476. aList->ClearListAndDeleteItems( []( EDA_ITEM* item )
  477. {
  478. static_cast<BOARD_ITEM*>( item )->SetParentGroup( nullptr );
  479. delete item;
  480. } );
  481. }
  482. void PCB_BASE_EDIT_FRAME::RollbackFromUndo()
  483. {
  484. PICKED_ITEMS_LIST* undo = PopCommandFromUndoList();
  485. PutDataInPreviousState( undo );
  486. ClearListAndDeleteItems( undo );
  487. delete undo;
  488. GetCanvas()->Refresh();
  489. }