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_FOOTPRINT_EDITOR );
52 }
53 
54 
56 {
57 }
58 
60 {
61  // if aItem belongs a footprint, the full footprint will be saved
62  // because undo/redo does not handle "sub items" modifications
63  if( aItem && aItem->Type() != PCB_MODULE_T && aChangeType == CHT_MODIFY )
64  {
65  EDA_ITEM* item = aItem->GetParent();
66 
67  if( item && item->Type() == PCB_MODULE_T ) // means aItem belongs a footprint
68  aItem = item;
69  }
70 
71  return COMMIT::Stage( aItem, aChangeType );
72 }
73 
74 COMMIT& BOARD_COMMIT::Stage( std::vector<EDA_ITEM*>& container, CHANGE_TYPE aChangeType )
75 {
76  return COMMIT::Stage( container, aChangeType );
77 }
78 
80 {
81  return COMMIT::Stage( aItems, aModFlag );
82 }
83 
84 void BOARD_COMMIT::Push( const wxString& aMessage, bool aCreateUndoEntry, bool aSetDirtyBit )
85 {
86  // Objects potentially interested in changes:
87  PICKED_ITEMS_LIST undoList;
88  KIGFX::VIEW* view = m_toolMgr->GetView();
89  BOARD* board = (BOARD*) m_toolMgr->GetModel();
90  PCB_BASE_FRAME* frame = (PCB_BASE_FRAME*) m_toolMgr->GetEditFrame();
91  auto connectivity = board->GetConnectivity();
92  std::set<EDA_ITEM*> savedModules;
93  SELECTION_TOOL* selTool = m_toolMgr->GetTool<SELECTION_TOOL>();
94  bool itemsDeselected = false;
95 
96  if( Empty() )
97  return;
98 
99  for( COMMIT_LINE& ent : m_changes )
100  {
101  int changeType = ent.m_type & CHT_TYPE;
102  int changeFlags = ent.m_type & CHT_FLAGS;
103  BOARD_ITEM* boardItem = static_cast<BOARD_ITEM*>( ent.m_item );
104 
105  // Module items need to be saved in the undo buffer before modification
106  if( m_editModules )
107  {
108  // Be sure that we are storing a module
109  if( ent.m_item->Type() != PCB_MODULE_T )
110  ent.m_item = ent.m_item->GetParent();
111 
112  // We have not saved the module yet, so let's create an entry
113  if( savedModules.count( ent.m_item ) == 0 )
114  {
115  if( !ent.m_copy )
116  {
117  wxASSERT( changeType != CHT_MODIFY ); // too late to make a copy..
118  ent.m_copy = ent.m_item->Clone();
119  }
120 
121  wxASSERT( ent.m_item->Type() == PCB_MODULE_T );
122  wxASSERT( ent.m_copy->Type() == PCB_MODULE_T );
123 
124  if( aCreateUndoEntry )
125  {
126  ITEM_PICKER itemWrapper( ent.m_item, UR_CHANGED );
127  itemWrapper.SetLink( ent.m_copy );
128  undoList.PushItem( itemWrapper );
129  frame->SaveCopyInUndoList( undoList, UR_CHANGED );
130  }
131 
132  savedModules.insert( ent.m_item );
133  static_cast<MODULE*>( ent.m_item )->SetLastEditTime();
134  }
135  }
136 
137  switch( changeType )
138  {
139  case CHT_ADD:
140  {
141  if( !m_editModules )
142  {
143  if( aCreateUndoEntry )
144  undoList.PushItem( ITEM_PICKER( boardItem, UR_NEW ) );
145 
146  if( !( changeFlags & CHT_DONE ) )
147  board->Add( boardItem ); // handles connectivity
148  }
149  else
150  {
151  // modules inside modules are not supported yet
152  wxASSERT( boardItem->Type() != PCB_MODULE_T );
153 
154  boardItem->SetParent( board->Modules().front() );
155 
156  if( !( changeFlags & CHT_DONE ) )
157  board->Modules().front()->Add( boardItem );
158  }
159 
160  view->Add( boardItem );
161  break;
162  }
163 
164  case CHT_REMOVE:
165  {
166  if( !m_editModules && aCreateUndoEntry )
167  undoList.PushItem( ITEM_PICKER( boardItem, UR_DELETED ) );
168 
169  if( boardItem->IsSelected() )
170  {
171  selTool->RemoveItemFromSel( boardItem, true /* quiet mode */ );
172  itemsDeselected = true;
173  }
174 
175  switch( boardItem->Type() )
176  {
177  // Module items
178  case PCB_PAD_T:
179  case PCB_MODULE_EDGE_T:
180  case PCB_MODULE_TEXT_T:
182  // This level can only handle module items when editing modules
183  wxASSERT( m_editModules );
184 
185  if( boardItem->Type() == PCB_MODULE_TEXT_T )
186  {
187  TEXTE_MODULE* text = static_cast<TEXTE_MODULE*>( boardItem );
188 
189  // don't allow deletion of Reference or Value
190  if( text->GetType() != TEXTE_MODULE::TEXT_is_DIVERS )
191  break;
192  }
193 
194  view->Remove( boardItem );
195 
196  if( !( changeFlags & CHT_DONE ) )
197  {
198  MODULE* module = static_cast<MODULE*>( boardItem->GetParent() );
199  wxASSERT( module && module->Type() == PCB_MODULE_T );
200  module->Delete( boardItem );
201  }
202 
203  break;
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_AREA_T:
214  view->Remove( boardItem );
215 
216  if( !( changeFlags & CHT_DONE ) )
217  board->Remove( boardItem );
218 
219  break;
220 
221  case PCB_MODULE_T:
222  {
223  // There are no modules inside a module yet
224  wxASSERT( !m_editModules );
225 
226  MODULE* module = static_cast<MODULE*>( boardItem );
227  view->Remove( module );
228  module->ClearFlags();
229 
230  if( !( changeFlags & CHT_DONE ) )
231  board->Remove( module ); // handles connectivity
232  }
233  break;
234 
235  default: // other types do not need to (or should not) be handled
236  wxASSERT( false );
237  break;
238  }
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 )
273  {
274  size_t num_changes = m_changes.size();
275 
276  connectivity->RecalculateRatsnest( this );
277  connectivity->ClearDynamicRatsnest();
278  frame->GetCanvas()->RedrawRatsnest();
279 
280  if( m_changes.size() > num_changes )
281  {
282  for( size_t i = num_changes; i < m_changes.size(); ++i )
283  {
284  COMMIT_LINE& ent = m_changes[i];
285 
286  // This should only be modifications from the connectivity algo
287  wxASSERT( ( ent.m_type & CHT_TYPE ) == CHT_MODIFY );
288 
289  auto boardItem = static_cast<BOARD_ITEM*>( ent.m_item );
290 
291  if( aCreateUndoEntry )
292  {
293  ITEM_PICKER itemWrapper( boardItem, UR_CHANGED );
294  wxASSERT( ent.m_copy );
295  itemWrapper.SetLink( ent.m_copy );
296  undoList.PushItem( itemWrapper );
297  }
298  else
299  {
300  delete ent.m_copy;
301  }
302 
303  view->Update( boardItem );
304  }
305  }
306  }
307 
308  if( !m_editModules && aCreateUndoEntry )
309  frame->SaveCopyInUndoList( undoList, UR_UNSPECIFIED );
310 
311  m_toolMgr->PostEvent( { TC_MESSAGE, TA_MODEL_CHANGE, AS_GLOBAL } );
312 
313  if( itemsDeselected )
314  m_toolMgr->PostEvent( EVENTS::UnselectedEvent );
315 
316  if( aSetDirtyBit )
317  frame->OnModify();
318 
319  frame->UpdateMsgPanel();
320 
321  clear();
322 }
323 
324 
326 {
327  switch( aItem->Type() )
328  {
329  case PCB_PAD_T:
330  case PCB_MODULE_EDGE_T:
331  case PCB_MODULE_TEXT_T:
333  return aItem->GetParent();
334 
335  case PCB_ZONE_AREA_T:
336  wxASSERT( !dynamic_cast<MODULE*>( aItem->GetParent() ) );
337  return aItem;
338 
339  default:
340  break;
341  }
342 
343  return aItem;
344 }
345 
346 
348 {
349  PICKED_ITEMS_LIST undoList;
350  KIGFX::VIEW* view = m_toolMgr->GetView();
351  BOARD* board = (BOARD*) m_toolMgr->GetModel();
352  auto connectivity = board->GetConnectivity();
353 
354  for( auto it = m_changes.rbegin(); it != m_changes.rend(); ++it )
355  {
356  COMMIT_LINE& ent = *it;
357  BOARD_ITEM* item = static_cast<BOARD_ITEM*>( ent.m_item );
358  BOARD_ITEM* copy = static_cast<BOARD_ITEM*>( ent.m_copy );
359  int changeType = ent.m_type & CHT_TYPE;
360  int changeFlags = ent.m_type & CHT_FLAGS;
361 
362  switch( changeType )
363  {
364  case CHT_ADD:
365  if( !( changeFlags & CHT_DONE ) )
366  break;
367 
368  view->Remove( item );
369  connectivity->Remove( item );
370  board->Remove( item );
371  break;
372 
373  case CHT_REMOVE:
374  if( !( changeFlags & CHT_DONE ) )
375  break;
376 
377  view->Add( item );
378  connectivity->Add( item );
379  board->Add( item );
380  break;
381 
382  case CHT_MODIFY:
383  {
384  view->Remove( item );
385  connectivity->Remove( item );
386 
387  item->SwapData( copy );
388 
389  view->Add( item );
390  connectivity->Add( item );
391  delete copy;
392  break;
393  }
394 
395  default:
396  wxASSERT( false );
397  break;
398  }
399  }
400 
401  if ( !m_editModules )
402  connectivity->RecalculateRatsnest();
403 
404  SELECTION_TOOL* selTool = m_toolMgr->GetTool<SELECTION_TOOL>();
405  selTool->RebuildSelection();
406 
407  clear();
408 }
virtual COMMIT & Stage(EDA_ITEM *aItem, CHANGE_TYPE aChangeType)
Adds a change of the item aItem of type aChangeType to the change list.
Definition: commit.cpp:46
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
bool IsSelected() const
Definition: base_struct.h:233
static const TOOL_EVENT UnselectedEvent
Definition: actions.h:198
Class BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
Class SELECTION_TOOL.
UNDO_REDO_T
Undo Redo considerations: Basically we have 3 cases New item Deleted item Modified item there is also...
virtual void Revert() override
Revertes the commit by restoring the modifed items state.
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
CHANGE_TYPE m_type
Modification type
Definition: commit.h:157
Definition: commit.h:38
class ZONE_CONTAINER, a zone area
Definition: typeinfo.h:101
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
Class COMMIT.
Definition: commit.h:71
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.
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
void RedrawRatsnest()
Forces refresh of the ratsnest visual representation
class TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:96
TOOL_MANAGER * GetManager() const
Function GetManager() Returns the instance of TOOL_MANAGER that takes care of the tool.
Definition: tool_base.h:143
virtual void SwapData(BOARD_ITEM *aImage)
Swap data between aItem and aImage.
class MODULE, a footprint
Definition: typeinfo.h:89
MODULES & Modules()
Definition: class_board.h:227
TOOL_TYPE m_type
Stores the type of the tool.
Definition: tool_base.h:211
void SetParent(EDA_ITEM *aParent)
Definition: base_struct.h:225
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:301
void RemoveItemFromSel(BOARD_ITEM *aItem, bool aQuietMode=false)
EDA_ITEM * GetParent() const
Definition: base_struct.h:220
Class PICKED_ITEMS_LIST is a holder to handle information on schematic or board items.
All active tools
Definition: tool_event.h:147
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:1540
class DIMENSION, a dimension (graphic item)
Definition: typeinfo.h:99
class PCB_TARGET, a target (graphic item)
Definition: typeinfo.h:100
bool EditingModules() const
class TEXTE_MODULE, text in a footprint
Definition: typeinfo.h:93
CHANGE_TYPE
Types of changes
Definition: commit.h:37
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:98
bool IsType(FRAME_T aType) const
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:161
size_t i
Definition: json11.cpp:649
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:163
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:266
virtual void Delete(BOARD_ITEM *aItem)
Removes an item from the container and deletes it.
class VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:97
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 ZONE_CONTAINER, managed by a footprint
Definition: typeinfo.h:95
Class VIEW.
Definition: view.h:61
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.
COMMIT & Stage(EDA_ITEM *aItem, CHANGE_TYPE aChangeType) override
Adds a change of the item aItem of type aChangeType to the change list.
KICAD_T Type() const
Function Type()
Definition: base_struct.h:210
virtual void UpdateMsgPanel()
Redraw the message panel.