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 <view/view.h>
30 #include <board_commit.h>
31 #include <tools/pcb_tool.h>
32 #include <tools/pcb_actions.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, bool aSetDirtyBit )
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  std::vector<BOARD_ITEM*> itemsToDeselect;
69 
70  if( Empty() )
71  return;
72 
73  for( COMMIT_LINE& ent : m_changes )
74  {
75  int changeType = ent.m_type & CHT_TYPE;
76  int changeFlags = ent.m_type & CHT_FLAGS;
77  BOARD_ITEM* boardItem = static_cast<BOARD_ITEM*>( ent.m_item );
78 
79  // Module items need to be saved in the undo buffer before modification
80  if( m_editModules )
81  {
82  // Be sure that we are storing a module
83  if( ent.m_item->Type() != PCB_MODULE_T )
84  ent.m_item = ent.m_item->GetParent();
85 
86  // We have not saved the module yet, so let's create an entry
87  if( savedModules.count( ent.m_item ) == 0 )
88  {
89  if( !ent.m_copy )
90  {
91  wxASSERT( changeType != CHT_MODIFY ); // too late to make a copy..
92  ent.m_copy = ent.m_item->Clone();
93  }
94 
95  wxASSERT( ent.m_item->Type() == PCB_MODULE_T );
96  wxASSERT( ent.m_copy->Type() == PCB_MODULE_T );
97 
98  if( aCreateUndoEntry )
99  {
100  ITEM_PICKER itemWrapper( ent.m_item, UR_CHANGED );
101  itemWrapper.SetLink( ent.m_copy );
102  undoList.PushItem( itemWrapper );
103  frame->SaveCopyInUndoList( undoList, UR_CHANGED );
104  }
105 
106  savedModules.insert( ent.m_item );
107  static_cast<MODULE*>( ent.m_item )->SetLastEditTime();
108  }
109  }
110 
111  switch( changeType )
112  {
113  case CHT_ADD:
114  {
115  if( !m_editModules )
116  {
117  if( aCreateUndoEntry )
118  {
119  undoList.PushItem( ITEM_PICKER( boardItem, UR_NEW ) );
120  }
121 
122  if( !( changeFlags & CHT_DONE ) )
123  board->Add( boardItem ); // handles connectivity
124 
125  }
126  else
127  {
128  // modules inside modules are not supported yet
129  wxASSERT( boardItem->Type() != PCB_MODULE_T );
130 
131  boardItem->SetParent( board->m_Modules.GetFirst() );
132 
133  if( !( changeFlags & CHT_DONE ) )
134  board->m_Modules->Add( boardItem );
135  }
136 
137  view->Add( boardItem );
138  break;
139  }
140 
141  case CHT_REMOVE:
142  {
143  if( !m_editModules && aCreateUndoEntry )
144  undoList.PushItem( ITEM_PICKER( boardItem, UR_DELETED ) );
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  // This level can only handle module items when editing modules
153  if( !m_editModules )
154  break;
155 
156  if( boardItem->Type() == PCB_MODULE_TEXT_T )
157  {
158  TEXTE_MODULE* text = static_cast<TEXTE_MODULE*>( boardItem );
159 
160  // don't allow deletion of Reference or Value
161  if( text->GetType() != TEXTE_MODULE::TEXT_is_DIVERS )
162  break;
163  }
164 
165  view->Remove( boardItem );
166 
167  if( !( changeFlags & CHT_DONE ) )
168  {
169  MODULE* module = static_cast<MODULE*>( boardItem->GetParent() );
170  wxASSERT( module && module->Type() == PCB_MODULE_T );
171  module->Delete( boardItem );
172  }
173 
174  board->m_Status_Pcb = 0; // it is done in the legacy view (ratsnest perhaps?)
175 
176  break;
177 
178  // Board items
179  case PCB_LINE_T: // a segment not on copper layers
180  case PCB_TEXT_T: // a text on a layer
181  case PCB_TRACE_T: // a track segment (segment on a copper layer)
182  case PCB_VIA_T: // a via (like track segment on a copper layer)
183  case PCB_DIMENSION_T: // a dimension (graphic item)
184  case PCB_TARGET_T: // a target (graphic item)
185  case PCB_MARKER_T: // a marker used to show something
186  case PCB_SEGZONE_T: // SEG_ZONE items are now deprecated
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  panel->RedrawRatsnest();
270  }
271 
272  if( aSetDirtyBit )
273  frame->OnModify();
274 
275  frame->UpdateMsgPanel();
276 
277  clear();
278 }
279 
280 
282 {
283  switch( aItem->Type() )
284  {
285  case PCB_PAD_T:
286  case PCB_MODULE_EDGE_T:
287  case PCB_MODULE_TEXT_T:
288  return aItem->GetParent();
289  default:
290  return aItem;
291  }
292 
293  return aItem;
294 }
295 
296 
298 {
299  PICKED_ITEMS_LIST undoList;
300  KIGFX::VIEW* view = m_toolMgr->GetView();
301  BOARD* board = (BOARD*) m_toolMgr->GetModel();
302  auto connectivity = board->GetConnectivity();
303 
304  for( auto it = m_changes.rbegin(); it != m_changes.rend(); ++it )
305  {
306  COMMIT_LINE& ent = *it;
307  BOARD_ITEM* item = static_cast<BOARD_ITEM*>( ent.m_item );
308  BOARD_ITEM* copy = static_cast<BOARD_ITEM*>( ent.m_copy );
309  int changeType = ent.m_type & CHT_TYPE;
310  int changeFlags = ent.m_type & CHT_FLAGS;
311 
312  switch( changeType )
313  {
314  case CHT_ADD:
315  if( !( changeFlags & CHT_DONE ) )
316  break;
317 
318  view->Remove( item );
319  connectivity->Remove( item );
320  board->Remove( item );
321  break;
322 
323  case CHT_REMOVE:
324  if( !( changeFlags & CHT_DONE ) )
325  break;
326 
327  if( item->Type() == PCB_MODULE_T )
328  {
329  MODULE* newModule = static_cast<MODULE*>( item );
330  newModule->RunOnChildren( std::bind( &EDA_ITEM::ClearFlags, _1, SELECTED ) );
331  }
332 
333  view->Add( item );
334  connectivity->Add( item );
335  board->Add( item );
336  break;
337 
338  case CHT_MODIFY:
339  {
340  view->Remove( item );
341  connectivity->Remove( item );
342 
343  item->SwapData( copy );
344  item->ClearFlags( SELECTED );
345 
346  // Update all pads/drawings/texts, as they become invalid
347  // for the VIEW after SwapData() called for modules
348  if( item->Type() == PCB_MODULE_T )
349  {
350  MODULE* newModule = static_cast<MODULE*>( item );
351  newModule->RunOnChildren( std::bind( &EDA_ITEM::ClearFlags, _1, SELECTED ) );
352  }
353 
354  view->Add( item );
355  connectivity->Add( item );
356  delete copy;
357  break;
358  }
359 
360  default:
361  wxASSERT( false );
362  break;
363  }
364  }
365 
366  if ( !m_editModules )
367  connectivity->RecalculateRatsnest();
368 
369  clear();
370 }
KICAD_T Type() const
Function Type()
Definition: base_struct.h:198
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.
CHANGE_TYPE m_type
Modification type
Definition: commit.h:157
static TOOL_ACTION unselectItems
Unselects a list of items (specified as the event parameter)
Definition: pcb_actions.h:65
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:366
EDA_ITEM * GetParent() const
Definition: base_struct.h:208
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)
Class EDA_DRAW_FRAME is the base class for create windows for drawing purpose.
Definition: draw_frame.h:63
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: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:118
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:213
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 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:1538
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:102
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:931
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: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.
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:151
virtual void Push(const wxString &aMessage=wxT("A commit"), bool aCreateUndoEntry=true, bool aSetDirtyBit=true) override
Executes the changes.
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.
EDA_DRAW_PANEL_GAL * GetGalCanvas() const
Function GetGalCanvas returns a pointer to GAL-based canvas of given EDA draw frame.
Definition: draw_frame.h:924
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:336
bool IsType(FRAME_T aType) const
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
class SEGZONE, a segment used to fill a zone area (segment on a
Definition: typeinfo.h:97
std::shared_ptr< CONNECTIVITY_DATA > GetConnectivity() const
Function GetConnectivity() returns list of missing connections between components/tracks.
Definition: class_board.h:296
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:240