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 <pcb_edit_frame.h>
28 #include <tool/tool_manager.h>
29 #include <tools/selection_tool.h>
30 #include <view/view.h>
31 #include <board_commit.h>
32 #include <tools/pcb_tool_base.h>
33 #include <tools/pcb_actions.h>
35 
36 #include <functional>
37 using namespace std::placeholders;
38 
39 #include "pcb_draw_panel_gal.h"
40 
42 {
43  m_toolMgr = aTool->GetManager();
44  m_editModules = aTool->EditingModules();
45 }
46 
47 
49 {
50  m_toolMgr = aFrame->GetToolManager();
51  m_editModules = aFrame->IsType( FRAME_PCB_MODULE_EDITOR );
52 }
53 
54 
56 {
57 }
58 
59 
60 void BOARD_COMMIT::Push( const wxString& aMessage, bool aCreateUndoEntry, bool aSetDirtyBit )
61 {
62  // Objects potentially interested in changes:
63  PICKED_ITEMS_LIST undoList;
64  KIGFX::VIEW* view = m_toolMgr->GetView();
65  BOARD* board = (BOARD*) m_toolMgr->GetModel();
66  PCB_BASE_FRAME* frame = (PCB_BASE_FRAME*) m_toolMgr->GetEditFrame();
67  auto connectivity = board->GetConnectivity();
68  std::set<EDA_ITEM*> savedModules;
69  std::vector<BOARD_ITEM*> itemsToDeselect;
70 
71  if( Empty() )
72  return;
73 
74  for( COMMIT_LINE& ent : m_changes )
75  {
76  int changeType = ent.m_type & CHT_TYPE;
77  int changeFlags = ent.m_type & CHT_FLAGS;
78  BOARD_ITEM* boardItem = static_cast<BOARD_ITEM*>( ent.m_item );
79 
80  // Module items need to be saved in the undo buffer before modification
81  if( m_editModules )
82  {
83  // Be sure that we are storing a module
84  if( ent.m_item->Type() != PCB_MODULE_T )
85  ent.m_item = ent.m_item->GetParent();
86 
87  // We have not saved the module yet, so let's create an entry
88  if( savedModules.count( ent.m_item ) == 0 )
89  {
90  if( !ent.m_copy )
91  {
92  wxASSERT( changeType != CHT_MODIFY ); // too late to make a copy..
93  ent.m_copy = ent.m_item->Clone();
94  }
95 
96  wxASSERT( ent.m_item->Type() == PCB_MODULE_T );
97  wxASSERT( ent.m_copy->Type() == PCB_MODULE_T );
98 
99  if( aCreateUndoEntry )
100  {
101  ITEM_PICKER itemWrapper( ent.m_item, UR_CHANGED );
102  itemWrapper.SetLink( ent.m_copy );
103  undoList.PushItem( itemWrapper );
104  frame->SaveCopyInUndoList( undoList, UR_CHANGED );
105  }
106 
107  savedModules.insert( ent.m_item );
108  static_cast<MODULE*>( ent.m_item )->SetLastEditTime();
109  }
110  }
111 
112  switch( changeType )
113  {
114  case CHT_ADD:
115  {
116  if( !m_editModules )
117  {
118  if( aCreateUndoEntry )
119  {
120  undoList.PushItem( ITEM_PICKER( boardItem, UR_NEW ) );
121  }
122 
123  if( !( changeFlags & CHT_DONE ) )
124  board->Add( boardItem ); // handles connectivity
125 
126  }
127  else
128  {
129  // modules inside modules are not supported yet
130  wxASSERT( boardItem->Type() != PCB_MODULE_T );
131 
132  boardItem->SetParent( board->m_Modules.GetFirst() );
133 
134  if( !( changeFlags & CHT_DONE ) )
135  board->m_Modules->Add( boardItem );
136  }
137 
138  view->Add( boardItem );
139  break;
140  }
141 
142  case CHT_REMOVE:
143  {
144  if( !m_editModules && aCreateUndoEntry )
145  undoList.PushItem( ITEM_PICKER( boardItem, UR_DELETED ) );
146 
147  switch( boardItem->Type() )
148  {
149  // Module items
150  case PCB_PAD_T:
151  case PCB_MODULE_EDGE_T:
152  case PCB_MODULE_TEXT_T:
153  // This level can only handle module items when editing modules
154  if( !m_editModules )
155  break;
156 
157  if( boardItem->Type() == PCB_MODULE_TEXT_T )
158  {
159  TEXTE_MODULE* text = static_cast<TEXTE_MODULE*>( boardItem );
160 
161  // don't allow deletion of Reference or Value
162  if( text->GetType() != TEXTE_MODULE::TEXT_is_DIVERS )
163  break;
164  }
165 
166  view->Remove( boardItem );
167 
168  if( !( changeFlags & CHT_DONE ) )
169  {
170  MODULE* module = static_cast<MODULE*>( boardItem->GetParent() );
171  wxASSERT( module && module->Type() == PCB_MODULE_T );
172  module->Delete( boardItem );
173  }
174 
175  board->m_Status_Pcb = 0; // it is done in the legacy view (ratsnest perhaps?)
176 
177  break;
178 
179  // Board items
180  case PCB_LINE_T: // a segment not on copper layers
181  case PCB_TEXT_T: // a text on a layer
182  case PCB_TRACE_T: // a track segment (segment on a copper layer)
183  case PCB_VIA_T: // a via (like track segment on a copper layer)
184  case PCB_DIMENSION_T: // a dimension (graphic item)
185  case PCB_TARGET_T: // a target (graphic item)
186  case PCB_MARKER_T: // a marker used to show something
187  case PCB_ZONE_AREA_T:
188  itemsToDeselect.push_back( boardItem );
189 
190  view->Remove( boardItem );
191 
192  if( !( changeFlags & CHT_DONE ) )
193  board->Remove( boardItem );
194 
195  break;
196 
197  case PCB_MODULE_T:
198  {
199  itemsToDeselect.push_back( boardItem );
200 
201  // There are no modules inside a module yet
202  wxASSERT( !m_editModules );
203 
204  MODULE* module = static_cast<MODULE*>( boardItem );
205  view->Remove( module );
206  module->ClearFlags();
207 
208  if( !( changeFlags & CHT_DONE ) )
209  board->Remove( module ); // handles connectivity
210 
211  // Clear flags to indicate, that the ratsnest, list of nets & pads are not valid anymore
212  board->m_Status_Pcb = 0;
213  }
214  break;
215 
216  default: // other types do not need to (or should not) be handled
217  wxASSERT( false );
218  break;
219  }
220 
221  break;
222  }
223 
224  case CHT_MODIFY:
225  {
226  if( !m_editModules && aCreateUndoEntry )
227  {
228  ITEM_PICKER itemWrapper( boardItem, UR_CHANGED );
229  wxASSERT( ent.m_copy );
230  itemWrapper.SetLink( ent.m_copy );
231  undoList.PushItem( itemWrapper );
232  }
233 
234  if( ent.m_copy )
235  connectivity->MarkItemNetAsDirty( static_cast<BOARD_ITEM*>( ent.m_copy ) );
236 
237  connectivity->Update( boardItem );
238  view->Update( boardItem );
239 
240  // if no undo entry is needed, the copy would create a memory leak
241  if( !aCreateUndoEntry )
242  delete ent.m_copy;
243 
244  break;
245  }
246 
247  default:
248  wxASSERT( false );
249  break;
250  }
251  }
252 
253  // Removing an item should trigger the unselect action
254  // but only after all items are removed otherwise we can get
255  // flickering depending on the system
256  if( itemsToDeselect.size() > 0 )
257  m_toolMgr->RunAction( PCB_ACTIONS::unselectItems, true, &itemsToDeselect );
258 
259  if( !m_editModules && aCreateUndoEntry )
260  frame->SaveCopyInUndoList( undoList, UR_UNSPECIFIED );
261 
262  if( TOOL_MANAGER* toolMgr = frame->GetToolManager() )
263  toolMgr->PostEvent( { TC_MESSAGE, TA_MODEL_CHANGE, AS_GLOBAL } );
264 
265  if ( !m_editModules )
266  {
267  auto panel = static_cast<PCB_DRAW_PANEL_GAL*>( frame->GetGalCanvas() );
268  connectivity->RecalculateRatsnest();
269  connectivity->ClearDynamicRatsnest();
270  panel->RedrawRatsnest();
271  }
272 
273  if( aSetDirtyBit )
274  frame->OnModify();
275 
276  frame->UpdateMsgPanel();
277 
278  clear();
279 }
280 
281 
283 {
284  switch( aItem->Type() )
285  {
286  case PCB_PAD_T:
287  case PCB_MODULE_EDGE_T:
288  case PCB_MODULE_TEXT_T:
289  return aItem->GetParent();
290  default:
291  return aItem;
292  }
293 
294  return aItem;
295 }
296 
297 
299 {
300  PICKED_ITEMS_LIST undoList;
301  KIGFX::VIEW* view = m_toolMgr->GetView();
302  BOARD* board = (BOARD*) m_toolMgr->GetModel();
303  auto connectivity = board->GetConnectivity();
304 
305  for( auto it = m_changes.rbegin(); it != m_changes.rend(); ++it )
306  {
307  COMMIT_LINE& ent = *it;
308  BOARD_ITEM* item = static_cast<BOARD_ITEM*>( ent.m_item );
309  BOARD_ITEM* copy = static_cast<BOARD_ITEM*>( ent.m_copy );
310  int changeType = ent.m_type & CHT_TYPE;
311  int changeFlags = ent.m_type & CHT_FLAGS;
312 
313  switch( changeType )
314  {
315  case CHT_ADD:
316  if( !( changeFlags & CHT_DONE ) )
317  break;
318 
319  view->Remove( item );
320  connectivity->Remove( item );
321  board->Remove( item );
322  break;
323 
324  case CHT_REMOVE:
325  if( !( changeFlags & CHT_DONE ) )
326  break;
327 
328  view->Add( item );
329  connectivity->Add( item );
330  board->Add( item );
331  break;
332 
333  case CHT_MODIFY:
334  {
335  view->Remove( item );
336  connectivity->Remove( item );
337 
338  item->SwapData( copy );
339 
340  // Update all pads/drawings/texts, as they become invalid
341  // for the VIEW after SwapData() called for modules
342  if( item->Type() == PCB_MODULE_T )
343  {
344  MODULE* newModule = static_cast<MODULE*>( item );
345  newModule->RunOnChildren( std::bind( &EDA_ITEM::ClearFlags, _1, SELECTED ) );
346  }
347 
348  view->Add( item );
349  connectivity->Add( item );
350  delete copy;
351  break;
352  }
353 
354  default:
355  wxASSERT( false );
356  break;
357  }
358  }
359 
360  if ( !m_editModules )
361  connectivity->RecalculateRatsnest();
362 
363  SELECTION_TOOL* selTool = m_toolMgr->GetTool<SELECTION_TOOL>();
364  selTool->RebuildSelection();
365 
366  clear();
367 }
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,...
Class SELECTION_TOOL.
Class BOARD to handle a board.
virtual void Revert() override
Revertes the commit by restoring the modifed items state.
EDA_DRAW_PANEL_GAL * GetGalCanvas() const
Return a pointer to GAL-based canvas of given EDA draw frame.
Definition: draw_frame.h:931
CHANGE_TYPE m_type
Modification type
Definition: commit.h:157
static TOOL_ACTION unselectItems
Definition: pcb_actions.h:61
Definition: commit.h:38
class ZONE_CONTAINER, a zone area
Definition: typeinfo.h:100
virtual void Remove(VIEW_ITEM *aItem)
Function Remove() Removes a VIEW_ITEM from the view.
Definition: view.cpp:376
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:151
BOARD_COMMIT(EDA_DRAW_FRAME *aFrame)
The base class for create windows for drawing purpose.
Definition: draw_frame.h:82
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
class TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:95
TOOL_MANAGER * GetManager() const
Function GetManager() Returns the instance of TOOL_MANAGER that takes care of the tool.
Definition: tool_base.h:144
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:121
class MODULE, a footprint
Definition: typeinfo.h:89
void SetParent(EDA_ITEM *aParent)
Definition: base_struct.h:219
TOOL_MANAGER * GetToolManager() const override
Return the tool manager instance, if any.
Definition: draw_frame.h:937
TEXT_TYPE GetType() const
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_INSERT) override
Adds an item to the container.
std::shared_ptr< CONNECTIVITY_DATA > GetConnectivity() const
Function GetConnectivity() returns list of missing connections between components/tracks.
Definition: class_board.h:292
EDA_ITEM * GetParent() const
Definition: base_struct.h:214
Class PICKED_ITEMS_LIST is a holder to handle information on schematic or board items.
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
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:1539
class DIMENSION, a dimension (graphic item)
Definition: typeinfo.h:98
class PCB_TARGET, a target (graphic item)
Definition: typeinfo.h:99
bool EditingModules() const
class TEXTE_MODULE, text in a footprint
Definition: typeinfo.h:93
virtual void OnModify()
Function OnModify Virtual Must be called after a change in order to set the "modify" flag of the curr...
class MARKER_PCB, a marker used to show something
Definition: typeinfo.h:97
bool IsType(FRAME_T aType) const
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:170
DLIST< MODULE > m_Modules
Definition: class_board.h:248
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.
void RebuildSelection()
Rebuilds the selection from the EDA_ITEMs' selection flags.
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:157
virtual void Push(const wxString &aMessage=wxT("A commit"), bool aCreateUndoEntry=true, bool aSetDirtyBit=true) override
Executes the changes.
void ClearFlags(STATUS_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: base_struct.h:260
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:96
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Function Add() Adds a VIEW_ITEM to the view.
Definition: view.cpp:346
EDA_ITEM * m_copy
Optional copy of the item
Definition: commit.h:154
Module description (excepted pads)
Class VIEW.
Definition: view.h:61
void UpdateMsgPanel() override
class DRAWSEGMENT, a segment not on copper layers
Definition: typeinfo.h:91
BOARD_ITEM_CONTAINER * GetParent() const
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.
KICAD_T Type() const
Function Type()
Definition: base_struct.h:204
int m_Status_Pcb
Flags used in ratsnest calculation and update.
Definition: class_board.h:240