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.

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