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_data.h>
34 
35 #include <functional>
36 using namespace std::placeholders;
37 
38 #include "pcb_draw_panel_gal.h"
39 
41 {
42  m_toolMgr = aTool->GetManager();
43  m_editModules = aTool->EditingModules();
44 }
45 
46 
48 {
49  m_toolMgr = aFrame->GetToolManager();
50  m_editModules = aFrame->IsType( FRAME_PCB_MODULE_EDITOR );
51 }
52 
53 
55 {
56 }
57 
58 
59 void BOARD_COMMIT::Push( const wxString& aMessage, bool aCreateUndoEntry )
60 {
61  // Objects potentially interested in changes:
62  PICKED_ITEMS_LIST undoList;
63  KIGFX::VIEW* view = m_toolMgr->GetView();
64  BOARD* board = (BOARD*) m_toolMgr->GetModel();
65  PCB_BASE_FRAME* frame = (PCB_BASE_FRAME*) m_toolMgr->GetEditFrame();
66  auto connectivity = board->GetConnectivity();
67  std::set<EDA_ITEM*> savedModules;
68 
69  if( Empty() )
70  return;
71 
72  for( COMMIT_LINE& ent : m_changes )
73  {
74  int changeType = ent.m_type & CHT_TYPE;
75  int changeFlags = ent.m_type & CHT_FLAGS;
76  BOARD_ITEM* boardItem = static_cast<BOARD_ITEM*>( ent.m_item );
77 
78  // Module items need to be saved in the undo buffer before modification
79  if( m_editModules )
80  {
81  // Be sure that we are storing a module
82  if( ent.m_item->Type() != PCB_MODULE_T )
83  ent.m_item = ent.m_item->GetParent();
84 
85  // We have not saved the module yet, so let's create an entry
86  if( savedModules.count( ent.m_item ) == 0 )
87  {
88  if( !ent.m_copy )
89  {
90  wxASSERT( changeType != CHT_MODIFY ); // too late to make a copy..
91  ent.m_copy = ent.m_item->Clone();
92  }
93 
94  wxASSERT( ent.m_item->Type() == PCB_MODULE_T );
95  wxASSERT( ent.m_copy->Type() == PCB_MODULE_T );
96 
97  if( aCreateUndoEntry )
98  {
99  ITEM_PICKER itemWrapper( ent.m_item, UR_CHANGED );
100  itemWrapper.SetLink( ent.m_copy );
101  undoList.PushItem( itemWrapper );
102  frame->SaveCopyInUndoList( undoList, UR_CHANGED );
103  }
104 
105  savedModules.insert( ent.m_item );
106  static_cast<MODULE*>( ent.m_item )->SetLastEditTime();
107  }
108  }
109 
110  switch( changeType )
111  {
112  case CHT_ADD:
113  {
114  if( !m_editModules )
115  {
116  if( aCreateUndoEntry )
117  {
118  undoList.PushItem( ITEM_PICKER( boardItem, UR_NEW ) );
119  }
120 
121  if( !( changeFlags & CHT_DONE ) )
122  board->Add( boardItem );
123 
124  }
125  else
126  {
127  // modules inside modules are not supported yet
128  wxASSERT( boardItem->Type() != PCB_MODULE_T );
129 
130  boardItem->SetParent( board->m_Modules.GetFirst() );
131  if( !( changeFlags & CHT_DONE ) )
132  board->m_Modules->Add( boardItem );
133  }
134 
135  view->Add( boardItem );
136  break;
137  }
138 
139  case CHT_REMOVE:
140  {
141  if( !m_editModules && aCreateUndoEntry )
142  {
143  undoList.PushItem( ITEM_PICKER( boardItem, UR_DELETED ) );
144  }
145 
146  switch( boardItem->Type() )
147  {
148  // Module items
149  case PCB_PAD_T:
150  case PCB_MODULE_EDGE_T:
151  case PCB_MODULE_TEXT_T:
152  {
153  // Do not allow footprint text removal when not editing a module
154  if( !m_editModules )
155  break;
156 
157  bool remove = true;
158 
159  if( boardItem->Type() == PCB_MODULE_TEXT_T )
160  {
161  TEXTE_MODULE* text = static_cast<TEXTE_MODULE*>( boardItem );
162 
163  switch( text->GetType() )
164  {
166  //DisplayError( frame, _( "Cannot delete component reference." ) );
167  remove = false;
168  break;
169 
171  //DisplayError( frame, _( "Cannot delete component value." ) );
172  remove = false;
173  break;
174 
175  case TEXTE_MODULE::TEXT_is_DIVERS: // suppress warnings
176  break;
177 
178  default:
179  wxASSERT( false );
180  break;
181  }
182  }
183 
184  if( remove )
185  {
186  view->Remove( boardItem );
187 
188  if( !( changeFlags & CHT_DONE ) )
189  {
190  MODULE* module = static_cast<MODULE*>( boardItem->GetParent() );
191  wxASSERT( module && module->Type() == PCB_MODULE_T );
192  module->Delete( boardItem );
193  }
194 
195  board->m_Status_Pcb = 0; // it is done in the legacy view (ratsnest perhaps?)
196  }
197 
198  break;
199  }
200 
201  // Board items
202  case PCB_LINE_T: // a segment not on copper layers
203  case PCB_TEXT_T: // a text on a layer
204  case PCB_TRACE_T: // a track segment (segment on a copper layer)
205  case PCB_VIA_T: // a via (like track segment on a copper layer)
206  case PCB_DIMENSION_T: // a dimension (graphic item)
207  case PCB_TARGET_T: // a target (graphic item)
208  case PCB_MARKER_T: // a marker used to show something
209  case PCB_ZONE_T: // SEG_ZONE items are now deprecated
210  case PCB_ZONE_AREA_T:
211  view->Remove( boardItem );
212 
213  if( !( changeFlags & CHT_DONE ) )
214  board->Remove( boardItem );
215 
216  break;
217 
218  case PCB_MODULE_T:
219  {
220  // There are no modules inside a module yet
221  wxASSERT( !m_editModules );
222 
223  MODULE* module = static_cast<MODULE*>( boardItem );
224  module->ClearFlags();
225 
226  view->Remove( module );
227 
228  if( !( changeFlags & CHT_DONE ) )
229  board->Remove( module );
230 
231  // Clear flags to indicate, that the ratsnest, list of nets & pads are not valid anymore
232  board->m_Status_Pcb = 0;
233  }
234  break;
235 
236  default: // other types do not need to (or should not) be handled
237  wxASSERT( false );
238  break;
239  }
240  break;
241  }
242 
243  case CHT_MODIFY:
244  {
245  if( !m_editModules && aCreateUndoEntry )
246  {
247  ITEM_PICKER itemWrapper( boardItem, UR_CHANGED );
248  wxASSERT( ent.m_copy );
249  itemWrapper.SetLink( ent.m_copy );
250  undoList.PushItem( itemWrapper );
251  }
252 
253  if( ent.m_copy )
254  connectivity->MarkItemNetAsDirty( static_cast<BOARD_ITEM*>( ent.m_copy ) );
255 
256  connectivity->Update( boardItem );
257  view->Update( boardItem );
258 
259  // if no undo entry is needed, the copy would create a memory leak
260  if( !aCreateUndoEntry )
261  delete ent.m_copy;
262 
263  break;
264  }
265 
266  default:
267  wxASSERT( false );
268  break;
269  }
270  }
271 
272  if( !m_editModules && aCreateUndoEntry )
273  frame->SaveCopyInUndoList( undoList, UR_UNSPECIFIED );
274 
275  if( TOOL_MANAGER* toolMgr = frame->GetToolManager() )
276  toolMgr->PostEvent( { TC_MESSAGE, TA_MODEL_CHANGE, AS_GLOBAL } );
277 
278  if ( !m_editModules )
279  {
280  auto panel = static_cast<PCB_DRAW_PANEL_GAL*>( frame->GetGalCanvas() );
281  connectivity->RecalculateRatsnest();
282  panel->RedrawRatsnest();
283  }
284 
285  frame->OnModify();
286  frame->UpdateMsgPanel();
287 
288  clear();
289 }
290 
291 
293 {
294  switch( aItem->Type() )
295  {
296  case PCB_PAD_T:
297  case PCB_MODULE_EDGE_T:
298  case PCB_MODULE_TEXT_T:
299  return aItem->GetParent();
300  default:
301  return aItem;
302  }
303 
304  return aItem;
305 }
306 
307 
309 {
310  PICKED_ITEMS_LIST undoList;
311  KIGFX::VIEW* view = m_toolMgr->GetView();
312  BOARD* board = (BOARD*) m_toolMgr->GetModel();
313  auto connectivity = board->GetConnectivity();
314 
315  for( auto it = m_changes.rbegin(); it != m_changes.rend(); ++it )
316  {
317  COMMIT_LINE& ent = *it;
318  BOARD_ITEM* item = static_cast<BOARD_ITEM*>( ent.m_item );
319  BOARD_ITEM* copy = static_cast<BOARD_ITEM*>( ent.m_copy );
320  int changeType = ent.m_type & CHT_TYPE;
321  int changeFlags = ent.m_type & CHT_FLAGS;
322 
323  switch( changeType )
324  {
325  case CHT_ADD:
326  if( !( changeFlags & CHT_DONE ) )
327  break;
328 
329  view->Remove( item );
330  connectivity->Remove( item );
331  board->Remove( item );
332  break;
333 
334  case CHT_REMOVE:
335  if( !( changeFlags & CHT_DONE ) )
336  break;
337 
338  if( item->Type() == PCB_MODULE_T )
339  {
340  MODULE* newModule = static_cast<MODULE*>( item );
341  newModule->RunOnChildren( std::bind( &EDA_ITEM::ClearFlags, _1, SELECTED ) );
342  }
343 
344  view->Add( item );
345  connectivity->Add( item );
346  board->Add( item );
347  break;
348 
349  case CHT_MODIFY:
350  {
351  view->Remove( item );
352  connectivity->Remove( item );
353 
354  item->SwapData( copy );
355  item->ClearFlags( SELECTED );
356 
357  // Update all pads/drawings/texts, as they become invalid
358  // for the VIEW after SwapData() called for modules
359  if( item->Type() == PCB_MODULE_T )
360  {
361  MODULE* newModule = static_cast<MODULE*>( item );
362  newModule->RunOnChildren( std::bind( &EDA_ITEM::ClearFlags, _1, SELECTED ) );
363  }
364 
365  view->Add( item );
366  connectivity->Add( item );
367  delete copy;
368  break;
369  }
370 
371  default:
372  wxASSERT( false );
373  break;
374  }
375  }
376 
377  if ( !m_editModules )
378  connectivity->RecalculateRatsnest();
379 
380  clear();
381 }
KICAD_T Type() const
Function Type()
Definition: base_struct.h:225
BOARD_ITEM_CONTAINER * GetParent() const
void RunOnChildren(const 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:156
Definition: commit.h:38
class ZONE_CONTAINER, a zone area
Definition: typeinfo.h:102
virtual void Remove(VIEW_ITEM *aItem)
Function Remove() Removes a VIEW_ITEM from the view.
Definition: view.cpp:342
EDA_ITEM * GetParent() const
Definition: base_struct.h:235
class TEXTE_PCB, text on a layer
Definition: typeinfo.h:92
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:90
EDA_ITEM * m_item
Main item that is added/deleted/modified
Definition: commit.h:150
BOARD_COMMIT(EDA_DRAW_FRAME *aFrame)
Class EDA_DRAW_FRAME is the base class for create windows for drawing purpose.
Definition: draw_frame.h:54
virtual ~BOARD_COMMIT()
void SetLink(EDA_ITEM *aItem)
class EDGE_MODULE, a footprint edge
Definition: typeinfo.h:94
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.
void RedrawRatsnest()
Forces refresh of the ratsnest visual representation
class TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:95
Class TOOL_MANAGER.
Definition: tool_manager.h:49
virtual void SwapData(BOARD_ITEM *aImage)
Swap data between aItem and aImage.
#define SELECTED
Definition: base_struct.h:134
class MODULE, a footprint
Definition: typeinfo.h:89
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_INSERT) override
>
void SetParent(EDA_ITEM *aParent)
Definition: base_struct.h:240
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_INSERT) override
Adds an item to the container.
T * GetFirst() const
Function GetFirst returns the first T* in the list without removing it, or NULL if the list is empty...
Definition: dlist.h:163
class SEGZONE, a segment used to fill a zone area (segment on a copper layer)
Definition: typeinfo.h:97
Class PICKED_ITEMS_LIST is a holder to handle information on schematic or board items.
virtual 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:100
class PCB_TARGET, a target (graphic item)
Definition: typeinfo.h:101
class TEXTE_MODULE, text in a footprint
Definition: typeinfo.h:93
bool EditingModules() const
Definition: pcb_tool.h:97
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:886
class MARKER_PCB, a marker used to show something
Definition: typeinfo.h:99
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:178
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:281
virtual void Delete(BOARD_ITEM *aItem)
Removes an item from the containter and deletes it.
EDA_DRAW_PANEL_GAL * GetGalCanvas() const
Function GetGalCanvas returns a pointer to GAL-based canvas of given EDA draw frame.
Definition: draw_frame.h:879
class VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:96
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Function Add() Adds a VIEW_ITEM to the view.
Definition: view.cpp:312
bool IsType(FRAME_T aType) const
Definition: wxstruct.h:227
EDA_ITEM * m_copy
Optional copy of the item
Definition: commit.h:153
Module description (excepted pads)
Class VIEW.
Definition: view.h:58
void UpdateMsgPanel() override
>
class DRAWSEGMENT, a segment not on copper layers
Definition: typeinfo.h:91
std::shared_ptr< CONNECTIVITY_DATA > GetConnectivity() const
Function GetConnectivity() returns list of missing connections between components/tracks.
Definition: class_board.h:290
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