KiCad PCB EDA Suite
board_commit.cpp
Go to the documentation of this file.
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 
25 #include <class_board.h>
26 #include <class_module.h>
27 #include <wxPcbStruct.h>
28 #include <tool/tool_manager.h>
29 #include <ratsnest_data.h>
30 #include <view/view.h>
31 #include <board_commit.h>
32 #include <tools/pcb_tool.h>
33 #include <connectivity.h>
34 
35 #include <functional>
36 using namespace std::placeholders;
37 
39 {
40  m_toolMgr = aTool->GetManager();
41  m_editModules = aTool->EditingModules();
42 }
43 
44 
46 {
47  m_toolMgr = aFrame->GetToolManager();
48  m_editModules = aFrame->IsType( FRAME_PCB_MODULE_EDITOR );
49 }
50 
51 
53 {
54 }
55 
56 
57 void BOARD_COMMIT::Push( const wxString& aMessage, bool aCreateUndoEntry )
58 {
59  // Objects potentially interested in changes:
60  PICKED_ITEMS_LIST undoList;
61  KIGFX::VIEW* view = m_toolMgr->GetView();
62  BOARD* board = (BOARD*) m_toolMgr->GetModel();
63  PCB_BASE_FRAME* frame = (PCB_BASE_FRAME*) m_toolMgr->GetEditFrame();
64  auto connectivity = board->GetConnectivity();
65  std::set<EDA_ITEM*> savedModules;
66 
67  if( Empty() )
68  return;
69 
70  for( COMMIT_LINE& ent : m_changes )
71  {
72  int changeType = ent.m_type & CHT_TYPE;
73  int changeFlags = ent.m_type & CHT_FLAGS;
74  BOARD_ITEM* boardItem = static_cast<BOARD_ITEM*>( ent.m_item );
75 
76  // Module items need to be saved in the undo buffer before modification
77  if( m_editModules )
78  {
79  // Be sure that we are storing a module
80  if( ent.m_item->Type() != PCB_MODULE_T )
81  ent.m_item = ent.m_item->GetParent();
82 
83  // We have not saved the module yet, so let's create an entry
84  if( savedModules.count( ent.m_item ) == 0 )
85  {
86  if( !ent.m_copy )
87  {
88  assert( changeType != CHT_MODIFY ); // too late to make a copy..
89  ent.m_copy = ent.m_item->Clone();
90  }
91 
92  assert( ent.m_item->Type() == PCB_MODULE_T );
93  assert( ent.m_copy->Type() == PCB_MODULE_T );
94 
95  if( aCreateUndoEntry )
96  {
97  ITEM_PICKER itemWrapper( ent.m_item, UR_CHANGED );
98  itemWrapper.SetLink( ent.m_copy );
99  undoList.PushItem( itemWrapper );
100  frame->SaveCopyInUndoList( undoList, UR_CHANGED );
101  }
102 
103  savedModules.insert( ent.m_item );
104  static_cast<MODULE*>( ent.m_item )->SetLastEditTime();
105  }
106  }
107 
108  switch( changeType )
109  {
110  case CHT_ADD:
111  {
112  if( !m_editModules )
113  {
114  if( aCreateUndoEntry )
115  {
116  undoList.PushItem( ITEM_PICKER( boardItem, UR_NEW ) );
117  }
118 
119  if( !( changeFlags & CHT_DONE ) )
120  board->Add( boardItem );
121 
122  //ratsnest->Add( boardItem ); // TODO currently done by BOARD::Add()
123 
124  if( boardItem->Type() == PCB_MODULE_T )
125  {
126  MODULE* mod = static_cast<MODULE*>( boardItem );
127  mod->RunOnChildren( std::bind( &KIGFX::VIEW::Add, view, _1, -1 ) );
128  }
129  }
130  else
131  {
132  // modules inside modules are not supported yet
133  assert( boardItem->Type() != PCB_MODULE_T );
134 
135  if( !( changeFlags & CHT_DONE ) )
136  board->m_Modules->Add( boardItem );
137  }
138 
139  view->Add( boardItem );
140  break;
141  }
142 
143  case CHT_REMOVE:
144  {
145  if( !m_editModules && aCreateUndoEntry )
146  {
147  undoList.PushItem( ITEM_PICKER( boardItem, UR_DELETED ) );
148  }
149 
150  switch( boardItem->Type() )
151  {
152  // Module items
153  case PCB_PAD_T:
154  case PCB_MODULE_EDGE_T:
155  case PCB_MODULE_TEXT_T:
156  {
157  // Do not allow footprint text removal when not editing a module
158  if( !m_editModules )
159  break;
160 
161  bool remove = true;
162 
163  if( boardItem->Type() == PCB_MODULE_TEXT_T )
164  {
165  TEXTE_MODULE* text = static_cast<TEXTE_MODULE*>( boardItem );
166 
167  switch( text->GetType() )
168  {
170  //DisplayError( frame, _( "Cannot delete component reference." ) );
171  remove = false;
172  break;
173 
175  //DisplayError( frame, _( "Cannot delete component value." ) );
176  remove = false;
177  break;
178 
179  case TEXTE_MODULE::TEXT_is_DIVERS: // suppress warnings
180  break;
181 
182  default:
183  assert( false );
184  break;
185  }
186  }
187 
188  if( remove )
189  {
190  view->Remove( boardItem );
191 
192  if( !( changeFlags & CHT_DONE ) )
193  {
194  MODULE* module = static_cast<MODULE*>( boardItem->GetParent() );
195  assert( module && module->Type() == PCB_MODULE_T );
196  module->Delete( boardItem );
197  }
198 
199  board->m_Status_Pcb = 0; // it is done in the legacy view (ratsnest perhaps?)
200  }
201 
202  break;
203  }
204 
205  // Board items
206  case PCB_LINE_T: // a segment not on copper layers
207  case PCB_TEXT_T: // a text on a layer
208  case PCB_TRACE_T: // a track segment (segment on a copper layer)
209  case PCB_VIA_T: // a via (like track segment on a copper layer)
210  case PCB_DIMENSION_T: // a dimension (graphic item)
211  case PCB_TARGET_T: // a target (graphic item)
212  case PCB_MARKER_T: // a marker used to show something
213  case PCB_ZONE_T: // SEG_ZONE items are now deprecated
214  case PCB_ZONE_AREA_T:
215  view->Remove( boardItem );
216 
217  if( !( changeFlags & CHT_DONE ) )
218  board->Remove( boardItem );
219 
220  //ratsnest->Remove( boardItem ); // currently done by BOARD::Remove()
221  break;
222 
223  case PCB_MODULE_T:
224  {
225  // There are no modules inside a module yet
226  assert( !m_editModules );
227 
228  MODULE* module = static_cast<MODULE*>( boardItem );
229  module->ClearFlags();
230  module->RunOnChildren( std::bind( &KIGFX::VIEW::Remove, view, _1 ) );
231 
232  view->Remove( module );
233 
234  if( !( changeFlags & CHT_DONE ) )
235  board->Remove( module );
236 
237  // Clear flags to indicate, that the ratsnest, list of nets & pads are not valid anymore
238  board->m_Status_Pcb = 0;
239  }
240  break;
241 
242  default: // other types do not need to (or should not) be handled
243  assert( false );
244  break;
245  }
246  break;
247  }
248 
249  case CHT_MODIFY:
250  {
251  if( !m_editModules && aCreateUndoEntry )
252  {
253  ITEM_PICKER itemWrapper( boardItem, UR_CHANGED );
254  assert( ent.m_copy );
255  itemWrapper.SetLink( ent.m_copy );
256  undoList.PushItem( itemWrapper );
257  }
258 
259  if( boardItem->Type() == PCB_MODULE_T )
260  {
261  MODULE* module = static_cast<MODULE*>( boardItem );
262  module->RunOnChildren( [&view] ( BOARD_ITEM* aItem ) { view->Update( aItem ); } );
263  }
264 
265  view->Update ( boardItem );
266  connectivity->Update( boardItem );
267  break;
268  }
269 
270  default:
271  assert( false );
272  break;
273  }
274  }
275 
276  if( !m_editModules && aCreateUndoEntry )
277  frame->SaveCopyInUndoList( undoList, UR_UNSPECIFIED );
278 
279  if( TOOL_MANAGER* toolMgr = frame->GetToolManager() )
280  toolMgr->PostEvent( { TC_MESSAGE, TA_MODEL_CHANGE, AS_GLOBAL } );
281 
282  connectivity->RecalculateRatsnest();
283  frame->OnModify();
284  frame->UpdateMsgPanel();
285 
286  clear();
287 }
288 
289 
291 {
292  switch( aItem->Type() )
293  {
294  case PCB_PAD_T:
295  case PCB_MODULE_EDGE_T:
296  case PCB_MODULE_TEXT_T:
297  return aItem->GetParent();
298  default:
299  return aItem;
300  }
301 
302  return aItem;
303 }
304 
305 
307 {
308  PICKED_ITEMS_LIST undoList;
309  KIGFX::VIEW* view = m_toolMgr->GetView();
310  BOARD* board = (BOARD*) m_toolMgr->GetModel();
311  auto connectivity = board->GetConnectivity();
312 
313  for( auto it = m_changes.rbegin(); it != m_changes.rend(); ++it )
314  {
315  COMMIT_LINE& ent = *it;
316  BOARD_ITEM* item = static_cast<BOARD_ITEM*>( ent.m_item );
317  BOARD_ITEM* copy = static_cast<BOARD_ITEM*>( ent.m_copy );
318 
319  switch( ent.m_type )
320  {
321  case CHT_ADD:
322  if( item->Type() == PCB_MODULE_T )
323  {
324  MODULE* oldModule = static_cast<MODULE*>( item );
325  oldModule->RunOnChildren( std::bind( &KIGFX::VIEW::Remove, view, _1 ) );
326  }
327 
328  view->Remove( item );
329  connectivity->Remove( item );
330  break;
331 
332  case CHT_REMOVE:
333  if( item->Type() == PCB_MODULE_T )
334  {
335  MODULE* newModule = static_cast<MODULE*>( item );
336  newModule->RunOnChildren( std::bind( &EDA_ITEM::ClearFlags, _1, SELECTED ) );
337  newModule->RunOnChildren( std::bind( &KIGFX::VIEW::Add, view, _1, -1 ) );
338  }
339 
340  view->Add( item );
341  connectivity->Add( item );
342  break;
343 
344  case CHT_MODIFY:
345  {
346  if( item->Type() == PCB_MODULE_T )
347  {
348  MODULE* oldModule = static_cast<MODULE*>( item );
349  oldModule->RunOnChildren( std::bind( &KIGFX::VIEW::Remove, view, _1 ) );
350  }
351 
352  view->Remove( item );
353  connectivity->Remove( item );
354 
355  item->SwapData( copy );
356 
357  item->ClearFlags( SELECTED );
358 
359  // Update all pads/drawings/texts, as they become invalid
360  // for the VIEW after SwapData() called for modules
361  if( item->Type() == PCB_MODULE_T )
362  {
363  MODULE* newModule = static_cast<MODULE*>( item );
364  newModule->RunOnChildren( std::bind( &EDA_ITEM::ClearFlags, _1, SELECTED ) );
365  newModule->RunOnChildren( std::bind( &KIGFX::VIEW::Add, view, _1, -1 ) );
366  }
367 
368  view->Add( item );
369  connectivity->Add( item );
370  delete copy;
371  break;
372  }
373 
374  default:
375  assert( false );
376  break;
377  }
378  }
379 
380  connectivity->RecalculateRatsnest();
381 
382  clear();
383 }
KICAD_T Type() const
Function Type()
Definition: base_struct.h:198
BOARD_ITEM_CONTAINER * GetParent() const
BOARD_COMMIT(PCB_TOOL *aTool)
void RunOnChildren(std::function< void(BOARD_ITEM *)> aFunction)
Function RunOnChildren.
Class BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class...
TEXT_TYPE GetType() const
Class BOARD to handle a board.
virtual void Revert() override
Revertes the commit by restoring the modifed items state.
Class that computes missing connections on a PCB.
CHANGE_TYPE m_type
Modification type
Definition: commit.h:150
Definition: commit.h:38
class ZONE_CONTAINER, a zone area
Definition: typeinfo.h:114
void Remove(VIEW_ITEM *aItem)
Function Remove() Removes a VIEW_ITEM from the view.
Definition: view.cpp:341
EDA_ITEM * GetParent() const
Definition: base_struct.h:208
class TEXTE_PCB, text on a layer
Definition: typeinfo.h:104
Flag to indicate the change is already applied, just notify observers (not compatible with CHT_MODIFY...
Definition: commit.h:45
void PushItem(const ITEM_PICKER &aItem)
Function PushItem pushes aItem to the top of the list.
class D_PAD, a pad in a footprint
Definition: typeinfo.h:102
EDA_ITEM * m_item
Main item that is added/deleted/modified
Definition: commit.h:144
virtual ~BOARD_COMMIT()
void SetLink(EDA_ITEM *aItem)
class EDGE_MODULE, a footprint edge
Definition: typeinfo.h:106
virtual EDA_ITEM * parentObject(EDA_ITEM *aItem) const override
virtual void Push(const wxString &aMessage=wxT("A commit"), bool aCreateUndoEntry=true) override
Executes the changes.
class TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:107
Class TOOL_MANAGER.
Definition: tool_manager.h:49
void SwapData(BOARD_ITEM *aImage)
Swap data between aItem and aImage.
Definition: undo_redo.cpp:566
#define SELECTED
Definition: base_struct.h:120
class MODULE, a footprint
Definition: typeinfo.h:101
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_INSERT) override
>
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_INSERT) override
Adds an item to the container.
class SEGZONE, a segment used to fill a zone area (segment on a copper layer)
Definition: typeinfo.h:109
Class PICKED_ITEMS_LIST is a holder to handle information on schematic or board items.
void Update(VIEW_ITEM *aItem, int aUpdateFlags)
For dynamic VIEWs, informs the associated VIEW that the graphical representation of this item has cha...
Definition: view.cpp:1385
class DIMENSION, a dimension (graphic item)
Definition: typeinfo.h:112
class PCB_TARGET, a target (graphic item)
Definition: typeinfo.h:113
class TEXTE_MODULE, text in a footprint
Definition: typeinfo.h:105
bool EditingModules() const
Definition: pcb_tool.h:91
virtual void OnModify()
Function OnModify Virtual Must be called after a change in order to set the "modify" flag of the curr...
TOOL_MANAGER * GetToolManager() const
Function GetToolManager returns the tool manager instance, if any.
Definition: draw_frame.h:824
class MARKER_PCB, a marker used to show something
Definition: typeinfo.h:111
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:169
DLIST< MODULE > m_Modules
Definition: class_board.h:245
virtual void SaveCopyInUndoList(BOARD_ITEM *aItemToCopy, UNDO_REDO_T aTypeCommand, const wxPoint &aTransformPoint=wxPoint(0, 0))=0
Function SaveCopyInUndoList (virtual pure) Creates a new entry in undo list of commands.
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:151
TOOL_MANAGER * GetManager() const
Function GetManager() Returns the instance of TOOL_MANAGER that takes care of the tool...
Definition: tool_base.h:144
void ClearFlags(STATUS_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: base_struct.h:254
virtual void Delete(BOARD_ITEM *aItem)
Removes an item from the containter and deletes it.
class VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:108
void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Function Add() Adds a VIEW_ITEM to the view.
Definition: view.cpp:311
bool IsType(FRAME_T aType) const
Definition: wxstruct.h:217
EDA_ITEM * m_copy
Optional copy of the item
Definition: commit.h:147
Module description (excepted pads)
Class VIEW.
Definition: view.h:58
void UpdateMsgPanel() override
>
class DRAWSEGMENT, a segment not on copper layers
Definition: typeinfo.h:103
std::shared_ptr< CONNECTIVITY_DATA > GetConnectivity() const
Function GetConnectivity() returns list of missing connections between components/tracks.
Definition: class_board.h:290
#define mod(a, n)
Definition: greymap.cpp:24
class PCB_BASE_FRAME basic PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer...
void Remove(BOARD_ITEM *aBoardItem) override
Removes an item from the container.
int m_Status_Pcb
Flags used in ratsnest calculation and update.
Definition: class_board.h:237