KiCad PCB EDA Suite
global_edit_tool.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) 2019-2020 KiCad Developers, see AUTHORS.TXT for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 #include <footprint.h>
25 #include <pcb_track.h>
26 #include <zone.h>
27 #include <tool/tool_manager.h>
28 #include <tools/pcb_actions.h>
29 #include <tools/edit_tool.h>
34 #include <tools/global_edit_tool.h>
35 #include <board_commit.h>
37 
39  PCB_TOOL_BASE( "pcbnew.GlobalEdit" ),
40  m_selectionTool( nullptr )
41 {
42 }
43 
44 
46 {
47  if( aReason != RUN )
48  m_commit = std::make_unique<BOARD_COMMIT>( this );
49 }
50 
51 
53 {
54  // Find the selection tool, so they can cooperate
56 
57  return true;
58 }
59 
60 
62 {
64  FOOTPRINT* footprint = nullptr;
65  bool updateMode = false;
66  bool currentMode = false;
67 
68  if( aEvent.HasPosition() )
70 
71  if( !selection.Empty() )
73 
74  if( aEvent.IsAction( &PCB_ACTIONS::updateFootprint ) )
75  {
76  updateMode = true;
77  currentMode = true;
78  }
79  else if( aEvent.IsAction( &PCB_ACTIONS::updateFootprints ) )
80  {
81  updateMode = true;
82  currentMode = false;
83  }
84  else if( aEvent.IsAction( &PCB_ACTIONS::changeFootprint ) )
85  {
86  updateMode = false;
87  currentMode = true;
88  }
89  else if( aEvent.IsAction( &PCB_ACTIONS::changeFootprints ) )
90  {
91  updateMode = false;
92  currentMode = false;
93  }
94  else
95  {
96  wxFAIL_MSG( "ExchangeFootprints: unexpected action" );
97  }
98 
99  // invoke the exchange dialog process
100  {
101  PCB_EDIT_FRAME* editFrame = getEditFrame<PCB_EDIT_FRAME>();
102  DIALOG_EXCHANGE_FOOTPRINTS dialog( editFrame, footprint, updateMode, currentMode );
103  dialog.ShowQuasiModal();
104  }
105 
106  return 0;
107 }
108 
109 
111 {
112  if( aLayerMap[ aItem->GetLayer() ] != aItem->GetLayer() )
113  {
114  m_commit->Modify( aItem );
115  aItem->SetLayer( aLayerMap[ aItem->GetLayer() ] );
116  frame()->GetCanvas()->GetView()->Update( aItem, KIGFX::GEOMETRY );
117  return true;
118  }
119 
120  return false;
121 }
122 
123 
125 {
127 
128  DIALOG_SWAP_LAYERS dlg( frame(), layerMap );
129 
130  if( dlg.ShowModal() != wxID_OK )
131  return 0;
132 
133  bool hasChanges = false;
134 
135  // Change tracks.
136  for( PCB_TRACK* segm : frame()->GetBoard()->Tracks() )
137  {
138  if( segm->Type() == PCB_VIA_T )
139  {
140  PCB_VIA* via = static_cast<PCB_VIA*>( segm );
141  PCB_LAYER_ID top_layer, bottom_layer;
142 
143  if( via->GetViaType() == VIATYPE::THROUGH )
144  continue;
145 
146  via->LayerPair( &top_layer, &bottom_layer );
147 
148  if( layerMap[bottom_layer] != bottom_layer || layerMap[top_layer] != top_layer )
149  {
150  m_commit->Modify( via );
151  via->SetLayerPair( layerMap[top_layer], layerMap[bottom_layer] );
153  hasChanges = true;
154  }
155  }
156  else
157  {
158  hasChanges |= swapBoardItem( segm, layerMap );
159  }
160  }
161 
162  for( BOARD_ITEM* zone : frame()->GetBoard()->Zones() )
163  hasChanges |= swapBoardItem( zone, layerMap );
164 
165  for( BOARD_ITEM* drawing : frame()->GetBoard()->Drawings() )
166  hasChanges |= swapBoardItem( drawing, layerMap );
167 
168  if( hasChanges )
169  {
170  frame()->OnModify();
171  m_commit->Push( "Layers moved" );
172  frame()->GetCanvas()->Refresh();
173  }
174 
175  return 0;
176 }
177 
178 
180 {
181  PCB_EDIT_FRAME* editFrame = getEditFrame<PCB_EDIT_FRAME>();
182  DIALOG_CLEANUP_TRACKS_AND_VIAS dlg( editFrame );
183 
184  dlg.ShowModal();
185  return 0;
186 }
187 
188 
190 {
191  PCB_EDIT_FRAME* editFrame = getEditFrame<PCB_EDIT_FRAME>();
192  DIALOG_CLEANUP_GRAPHICS dlg( editFrame, false );
193 
194  dlg.ShowModal();
195  return 0;
196 }
197 
198 
200 {
201  PCB_EDIT_FRAME* editFrame = getEditFrame<PCB_EDIT_FRAME>();
203  []( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector, PCB_SELECTION_TOOL* sTool )
204  {
205  sTool->FilterCollectorForHierarchy( aCollector, true );
206  } );
207  DIALOG_UNUSED_PAD_LAYERS dlg( editFrame, selection, *m_commit );
208 
209  dlg.ShowModal();
210 
211  return 0;
212 }
213 
214 
216 {
221 
223 
230 }
231 
232 
static TOOL_ACTION globalDeletions
Definition: pcb_actions.h:321
void setTransitions() override
This method is meant to be overridden in order to specify handlers for events.
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition: board_item.h:161
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:49
static TOOL_ACTION cleanupGraphics
Definition: pcb_actions.h:323
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
Tool is invoked after being inactive.
Definition: tool_base.h:79
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:214
static TOOL_ACTION changeFootprint
Definition: pcb_actions.h:326
static TOOL_ACTION updateFootprint
Definition: pcb_actions.h:324
static TOOL_ACTION swapLayers
Definition: pcb_actions.h:328
static TOOL_ACTION changeFootprints
Definition: pcb_actions.h:327
static TOOL_ACTION editTracksAndVias
Definition: pcb_actions.h:319
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Define which state (aStateFunc) to go when a certain event arrives (aConditions).
bool IsAction(const TOOL_ACTION *aAction) const
Test if the event contains an action issued upon activation of the given TOOL_ACTION.
Definition: tool_event.cpp:88
PCB_BASE_EDIT_FRAME * frame() const
static TOOL_ACTION cleanupTracksAndVias
Definition: pcb_actions.h:322
PCB_SELECTION & GetSelection()
Return the set of currently selected items.
int SwapLayers(const TOOL_EVENT &aEvent)
int EditTracksAndVias(const TOOL_EVENT &aEvent)
virtual void Update(const VIEW_ITEM *aItem, int aUpdateFlags) const override
For dynamic VIEWs, inform the associated VIEW that the graphical representation of this item has chan...
Definition: pcb_view.cpp:92
const PCB_SELECTION & selection() const
int RemoveUnusedPads(const TOOL_EVENT &aEvent)
int ShowQuasiModal()
Generic, UI-independent tool event.
Definition: tool_event.h:152
FOOTPRINT * footprint() const
virtual KIGFX::PCB_VIEW * GetView() const override
Return a pointer to the #VIEW instance used in the panel.
bool swapBoardItem(BOARD_ITEM *aItem, PCB_LAYER_ID *aLayerMap)
Set up handlers for various events.
bool Empty() const
Checks if there is anything selected.
Definition: selection.h:98
BOARD * GetBoard()
static TOOL_ACTION updateFootprints
Definition: pcb_actions.h:325
PCB_SELECTION_TOOL * m_selectionTool
static TOOL_ACTION removeUnusedPads
Definition: pcb_actions.h:329
bool Init() override
Init() is called once upon a registration of the tool.
void FilterCollectorForHierarchy(GENERAL_COLLECTOR &aCollector, bool aMultiselect) const
In general we don't want to select both a parent and any of it's children.
virtual void OnModify()
Must be called after a change in order to set the "modify" flag of the current screen and update the ...
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
PCB_SELECTION & RequestSelection(CLIENT_SELECTION_FILTER aClientFilter, bool aConfirmLockedItems=false)
Return the current selection set, filtered according to aFlags and aClientFilter.
static TOOL_ACTION editTextAndGraphics
Definition: pcb_actions.h:320
int GlobalDeletions(const TOOL_EVENT &aEvent)
int CleanupGraphics(const TOOL_EVENT &aEvent)
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:65
Used when the right click button is pressed, or when the select tool is in effect.
Definition: collectors.h:240
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=nullptr) override
Update the board display after modifying it by a python script (note: it is automatically called by a...
The main frame for Pcbnew.
The selection tool: currently supports:
int ExchangeFootprints(const TOOL_EVENT &aEvent)
Invoke the dialog used to update or exchange the footprint definitions used for footprints.
RESET_REASON
Determine the reason of reset for a tool.
Definition: tool_base.h:77
T * FirstOfKind() const
Definition: selection.h:156
int CleanupTracksAndVias(const TOOL_EVENT &aEvent)
class PCB_VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:96
bool HasPosition() const
Definition: tool_event.h:240
std::unique_ptr< BOARD_COMMIT > m_commit
static void FootprintFilter(const VECTOR2I &, GENERAL_COLLECTOR &aCollector, PCB_SELECTION_TOOL *sTool)
A selection filter which prunes the selection to contain only items of type #PCB_MODULE_T.
Definition: edit_tool.cpp:2303
BOARD * GetBoard() const
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:140
TRACKS & Tracks()
Definition: board.h:230
Position or shape has changed.
Definition: view_item.h:49