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.

382 lines
12 KiB

10 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2016 CERN
  5. * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
  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 <class_board.h>
  25. #include <class_module.h>
  26. #include <pcb_edit_frame.h>
  27. #include <tool/tool_manager.h>
  28. #include <ratsnest_data.h>
  29. #include <view/view.h>
  30. #include <board_commit.h>
  31. #include <tools/pcb_tool.h>
  32. #include <connectivity_data.h>
  33. #include <functional>
  34. using namespace std::placeholders;
  35. #include "pcb_draw_panel_gal.h"
  36. BOARD_COMMIT::BOARD_COMMIT( PCB_TOOL* aTool )
  37. {
  38. m_toolMgr = aTool->GetManager();
  39. m_editModules = aTool->EditingModules();
  40. }
  41. BOARD_COMMIT::BOARD_COMMIT( EDA_DRAW_FRAME* aFrame )
  42. {
  43. m_toolMgr = aFrame->GetToolManager();
  44. m_editModules = aFrame->IsType( FRAME_PCB_MODULE_EDITOR );
  45. }
  46. BOARD_COMMIT::~BOARD_COMMIT()
  47. {
  48. }
  49. void BOARD_COMMIT::Push( const wxString& aMessage, bool aCreateUndoEntry )
  50. {
  51. // Objects potentially interested in changes:
  52. PICKED_ITEMS_LIST undoList;
  53. KIGFX::VIEW* view = m_toolMgr->GetView();
  54. BOARD* board = (BOARD*) m_toolMgr->GetModel();
  55. PCB_BASE_FRAME* frame = (PCB_BASE_FRAME*) m_toolMgr->GetEditFrame();
  56. auto connectivity = board->GetConnectivity();
  57. std::set<EDA_ITEM*> savedModules;
  58. if( Empty() )
  59. return;
  60. for( COMMIT_LINE& ent : m_changes )
  61. {
  62. int changeType = ent.m_type & CHT_TYPE;
  63. int changeFlags = ent.m_type & CHT_FLAGS;
  64. BOARD_ITEM* boardItem = static_cast<BOARD_ITEM*>( ent.m_item );
  65. // Module items need to be saved in the undo buffer before modification
  66. if( m_editModules )
  67. {
  68. // Be sure that we are storing a module
  69. if( ent.m_item->Type() != PCB_MODULE_T )
  70. ent.m_item = ent.m_item->GetParent();
  71. // We have not saved the module yet, so let's create an entry
  72. if( savedModules.count( ent.m_item ) == 0 )
  73. {
  74. if( !ent.m_copy )
  75. {
  76. wxASSERT( changeType != CHT_MODIFY ); // too late to make a copy..
  77. ent.m_copy = ent.m_item->Clone();
  78. }
  79. wxASSERT( ent.m_item->Type() == PCB_MODULE_T );
  80. wxASSERT( ent.m_copy->Type() == PCB_MODULE_T );
  81. if( aCreateUndoEntry )
  82. {
  83. ITEM_PICKER itemWrapper( ent.m_item, UR_CHANGED );
  84. itemWrapper.SetLink( ent.m_copy );
  85. undoList.PushItem( itemWrapper );
  86. frame->SaveCopyInUndoList( undoList, UR_CHANGED );
  87. }
  88. savedModules.insert( ent.m_item );
  89. static_cast<MODULE*>( ent.m_item )->SetLastEditTime();
  90. }
  91. }
  92. switch( changeType )
  93. {
  94. case CHT_ADD:
  95. {
  96. if( !m_editModules )
  97. {
  98. if( aCreateUndoEntry )
  99. {
  100. undoList.PushItem( ITEM_PICKER( boardItem, UR_NEW ) );
  101. }
  102. if( !( changeFlags & CHT_DONE ) )
  103. board->Add( boardItem ); // handles connectivity
  104. }
  105. else
  106. {
  107. // modules inside modules are not supported yet
  108. wxASSERT( boardItem->Type() != PCB_MODULE_T );
  109. boardItem->SetParent( board->m_Modules.GetFirst() );
  110. if( !( changeFlags & CHT_DONE ) )
  111. board->m_Modules->Add( boardItem );
  112. }
  113. view->Add( boardItem );
  114. break;
  115. }
  116. case CHT_REMOVE:
  117. {
  118. if( !m_editModules && aCreateUndoEntry )
  119. {
  120. undoList.PushItem( ITEM_PICKER( boardItem, UR_DELETED ) );
  121. }
  122. switch( boardItem->Type() )
  123. {
  124. // Module items
  125. case PCB_PAD_T:
  126. case PCB_MODULE_EDGE_T:
  127. case PCB_MODULE_TEXT_T:
  128. {
  129. // Do not allow footprint text removal when not editing a module
  130. if( !m_editModules )
  131. break;
  132. bool remove = true;
  133. if( boardItem->Type() == PCB_MODULE_TEXT_T )
  134. {
  135. TEXTE_MODULE* text = static_cast<TEXTE_MODULE*>( boardItem );
  136. switch( text->GetType() )
  137. {
  138. case TEXTE_MODULE::TEXT_is_REFERENCE:
  139. //DisplayError( frame, _( "Cannot delete component reference." ) );
  140. remove = false;
  141. break;
  142. case TEXTE_MODULE::TEXT_is_VALUE:
  143. //DisplayError( frame, _( "Cannot delete component value." ) );
  144. remove = false;
  145. break;
  146. case TEXTE_MODULE::TEXT_is_DIVERS: // suppress warnings
  147. break;
  148. default:
  149. wxASSERT( false );
  150. break;
  151. }
  152. }
  153. if( remove )
  154. {
  155. view->Remove( boardItem );
  156. if( !( changeFlags & CHT_DONE ) )
  157. {
  158. MODULE* module = static_cast<MODULE*>( boardItem->GetParent() );
  159. wxASSERT( module && module->Type() == PCB_MODULE_T );
  160. module->Delete( boardItem );
  161. }
  162. board->m_Status_Pcb = 0; // it is done in the legacy view (ratsnest perhaps?)
  163. }
  164. break;
  165. }
  166. // Board items
  167. case PCB_LINE_T: // a segment not on copper layers
  168. case PCB_TEXT_T: // a text on a layer
  169. case PCB_TRACE_T: // a track segment (segment on a copper layer)
  170. case PCB_VIA_T: // a via (like track segment on a copper layer)
  171. case PCB_DIMENSION_T: // a dimension (graphic item)
  172. case PCB_TARGET_T: // a target (graphic item)
  173. case PCB_MARKER_T: // a marker used to show something
  174. case PCB_ZONE_T: // SEG_ZONE items are now deprecated
  175. case PCB_ZONE_AREA_T:
  176. view->Remove( boardItem );
  177. if( !( changeFlags & CHT_DONE ) )
  178. board->Remove( boardItem );
  179. break;
  180. case PCB_MODULE_T:
  181. {
  182. // There are no modules inside a module yet
  183. wxASSERT( !m_editModules );
  184. MODULE* module = static_cast<MODULE*>( boardItem );
  185. module->ClearFlags();
  186. view->Remove( module );
  187. if( !( changeFlags & CHT_DONE ) )
  188. board->Remove( module ); // handles connectivity
  189. // Clear flags to indicate, that the ratsnest, list of nets & pads are not valid anymore
  190. board->m_Status_Pcb = 0;
  191. }
  192. break;
  193. default: // other types do not need to (or should not) be handled
  194. wxASSERT( false );
  195. break;
  196. }
  197. break;
  198. }
  199. case CHT_MODIFY:
  200. {
  201. if( !m_editModules && aCreateUndoEntry )
  202. {
  203. ITEM_PICKER itemWrapper( boardItem, UR_CHANGED );
  204. wxASSERT( ent.m_copy );
  205. itemWrapper.SetLink( ent.m_copy );
  206. undoList.PushItem( itemWrapper );
  207. }
  208. if( ent.m_copy )
  209. connectivity->MarkItemNetAsDirty( static_cast<BOARD_ITEM*>( ent.m_copy ) );
  210. connectivity->Update( boardItem );
  211. view->Update( boardItem );
  212. // if no undo entry is needed, the copy would create a memory leak
  213. if( !aCreateUndoEntry )
  214. delete ent.m_copy;
  215. break;
  216. }
  217. default:
  218. wxASSERT( false );
  219. break;
  220. }
  221. }
  222. if( !m_editModules && aCreateUndoEntry )
  223. frame->SaveCopyInUndoList( undoList, UR_UNSPECIFIED );
  224. if( TOOL_MANAGER* toolMgr = frame->GetToolManager() )
  225. toolMgr->PostEvent( { TC_MESSAGE, TA_MODEL_CHANGE, AS_GLOBAL } );
  226. if ( !m_editModules )
  227. {
  228. auto panel = static_cast<PCB_DRAW_PANEL_GAL*>( frame->GetGalCanvas() );
  229. connectivity->RecalculateRatsnest();
  230. panel->RedrawRatsnest();
  231. }
  232. frame->OnModify();
  233. frame->UpdateMsgPanel();
  234. clear();
  235. }
  236. EDA_ITEM* BOARD_COMMIT::parentObject( EDA_ITEM* aItem ) const
  237. {
  238. switch( aItem->Type() )
  239. {
  240. case PCB_PAD_T:
  241. case PCB_MODULE_EDGE_T:
  242. case PCB_MODULE_TEXT_T:
  243. return aItem->GetParent();
  244. default:
  245. return aItem;
  246. }
  247. return aItem;
  248. }
  249. void BOARD_COMMIT::Revert()
  250. {
  251. PICKED_ITEMS_LIST undoList;
  252. KIGFX::VIEW* view = m_toolMgr->GetView();
  253. BOARD* board = (BOARD*) m_toolMgr->GetModel();
  254. auto connectivity = board->GetConnectivity();
  255. for( auto it = m_changes.rbegin(); it != m_changes.rend(); ++it )
  256. {
  257. COMMIT_LINE& ent = *it;
  258. BOARD_ITEM* item = static_cast<BOARD_ITEM*>( ent.m_item );
  259. BOARD_ITEM* copy = static_cast<BOARD_ITEM*>( ent.m_copy );
  260. int changeType = ent.m_type & CHT_TYPE;
  261. int changeFlags = ent.m_type & CHT_FLAGS;
  262. switch( changeType )
  263. {
  264. case CHT_ADD:
  265. if( !( changeFlags & CHT_DONE ) )
  266. break;
  267. view->Remove( item );
  268. connectivity->Remove( item );
  269. board->Remove( item );
  270. break;
  271. case CHT_REMOVE:
  272. if( !( changeFlags & CHT_DONE ) )
  273. break;
  274. if( item->Type() == PCB_MODULE_T )
  275. {
  276. MODULE* newModule = static_cast<MODULE*>( item );
  277. newModule->RunOnChildren( std::bind( &EDA_ITEM::ClearFlags, _1, SELECTED ) );
  278. }
  279. view->Add( item );
  280. connectivity->Add( item );
  281. board->Add( item );
  282. break;
  283. case CHT_MODIFY:
  284. {
  285. view->Remove( item );
  286. connectivity->Remove( item );
  287. item->SwapData( copy );
  288. item->ClearFlags( SELECTED );
  289. // Update all pads/drawings/texts, as they become invalid
  290. // for the VIEW after SwapData() called for modules
  291. if( item->Type() == PCB_MODULE_T )
  292. {
  293. MODULE* newModule = static_cast<MODULE*>( item );
  294. newModule->RunOnChildren( std::bind( &EDA_ITEM::ClearFlags, _1, SELECTED ) );
  295. }
  296. view->Add( item );
  297. connectivity->Add( item );
  298. delete copy;
  299. break;
  300. }
  301. default:
  302. wxASSERT( false );
  303. break;
  304. }
  305. }
  306. if ( !m_editModules )
  307. connectivity->RecalculateRatsnest();
  308. clear();
  309. }