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 <track.h>
25 #include <tool/tool_manager.h>
26 #include <tools/pcb_actions.h>
27 #include <tools/edit_tool.h>
32 #include <tools/global_edit_tool.h>
33 #include <board_commit.h>
35 
37  PCB_TOOL_BASE( "pcbnew.GlobalEdit" ),
38  m_selectionTool( nullptr )
39 {
40 }
41 
42 
44 {
45  if( aReason != RUN )
46  m_commit = std::make_unique<BOARD_COMMIT>( this );
47 }
48 
49 
51 {
52  // Find the selection tool, so they can cooperate
54 
55  return true;
56 }
57 
58 
60 {
62  FOOTPRINT* footprint = nullptr;
63  bool updateMode = false;
64  bool currentMode = false;
65 
66  if( aEvent.HasPosition() )
68 
69  if( !selection.Empty() )
71 
72  if( aEvent.IsAction( &PCB_ACTIONS::updateFootprint ) )
73  {
74  updateMode = true;
75  currentMode = true;
76  }
77  else if( aEvent.IsAction( &PCB_ACTIONS::updateFootprints ) )
78  {
79  updateMode = true;
80  currentMode = false;
81  }
82  else if( aEvent.IsAction( &PCB_ACTIONS::changeFootprint ) )
83  {
84  updateMode = false;
85  currentMode = true;
86  }
87  else if( aEvent.IsAction( &PCB_ACTIONS::changeFootprints ) )
88  {
89  updateMode = false;
90  currentMode = false;
91  }
92  else
93  wxFAIL_MSG( "ExchangeFootprints: unexpected action" );
94 
95  // Footprint exchange could remove footprints, so they have to be
96  // removed from the selection first
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( TRACK* segm : frame()->GetBoard()->Tracks() )
137  {
138  if( segm->Type() == PCB_VIA_T )
139  {
140  VIA* via = (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 selectionClear
Clear the current selection.
Definition: pcb_actions.h:63
Definition: track.h:343
static TOOL_ACTION globalDeletions
Definition: pcb_actions.h:322
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:194
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:82
static TOOL_ACTION cleanupGraphics
Definition: pcb_actions.h:324
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:80
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:215
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Run the specified action.
Definition: tool_manager.h:141
static TOOL_ACTION changeFootprint
Definition: pcb_actions.h:327
static TOOL_ACTION updateFootprint
Definition: pcb_actions.h:325
static TOOL_ACTION swapLayers
Definition: pcb_actions.h:329
static TOOL_ACTION changeFootprints
Definition: pcb_actions.h:328
static TOOL_ACTION editTracksAndVias
Definition: pcb_actions.h:320
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:70
PCB_BASE_EDIT_FRAME * frame() const
static TOOL_ACTION cleanupTracksAndVias
Definition: pcb_actions.h:323
PCB_SELECTION & GetSelection()
Return the set of currently selected items.
int SwapLayers(const TOOL_EVENT &aEvent)
int EditTracksAndVias(const TOOL_EVENT &aEvent)
PCB_LAYER_ID
A quick note on layer IDs:
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:173
FOOTPRINT * footprint() const
virtual KIGFX::PCB_VIEW * GetView() const override
Return a pointer to the #VIEW instance used in the panel.
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=NULL) override
Update the board display after modifying it by a python script (note: it is automatically called by a...
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:122
BOARD * GetBoard()
static TOOL_ACTION updateFootprints
Definition: pcb_actions.h:326
PCB_SELECTION_TOOL * m_selectionTool
static TOOL_ACTION removeUnusedPads
Definition: pcb_actions.h:330
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:321
int GlobalDeletions(const TOOL_EVENT &aEvent)
int CleanupGraphics(const TOOL_EVENT &aEvent)
Used when the right click button is pressed, or when the select tool is in effect.
Definition: collectors.h:241
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:78
T * FirstOfKind() const
Definition: selection.h:214
int CleanupTracksAndVias(const TOOL_EVENT &aEvent)
class VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:96
bool HasPosition() const
Definition: tool_event.h:261
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:2188
BOARD * GetBoard() const
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:173
TRACKS & Tracks()
Definition: board.h:302
Definition: track.h:83
Position or shape has changed.
Definition: view_item.h:54