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  wxFAIL_MSG( "ExchangeFootprints: unexpected action" );
96 
97  // invoke the exchange dialog process
98  {
99  PCB_EDIT_FRAME* editFrame = getEditFrame<PCB_EDIT_FRAME>();
100  DIALOG_EXCHANGE_FOOTPRINTS dialog( editFrame, footprint, updateMode, currentMode );
101  dialog.ShowQuasiModal();
102  }
103 
104  return 0;
105 }
106 
107 
109 {
110  if( aLayerMap[ aItem->GetLayer() ] != aItem->GetLayer() )
111  {
112  m_commit->Modify( aItem );
113  aItem->SetLayer( aLayerMap[ aItem->GetLayer() ] );
114  frame()->GetCanvas()->GetView()->Update( aItem, KIGFX::GEOMETRY );
115  return true;
116  }
117 
118  return false;
119 }
120 
121 
123 {
125 
126  DIALOG_SWAP_LAYERS dlg( frame(), layerMap );
127 
128  if( dlg.ShowModal() != wxID_OK )
129  return 0;
130 
131  bool hasChanges = false;
132 
133  // Change tracks.
134  for( PCB_TRACK* segm : frame()->GetBoard()->Tracks() )
135  {
136  if( segm->Type() == PCB_VIA_T )
137  {
138  PCB_VIA* via = static_cast<PCB_VIA*>( segm );
139  PCB_LAYER_ID top_layer, bottom_layer;
140 
141  if( via->GetViaType() == VIATYPE::THROUGH )
142  continue;
143 
144  via->LayerPair( &top_layer, &bottom_layer );
145 
146  if( layerMap[bottom_layer] != bottom_layer || layerMap[top_layer] != top_layer )
147  {
148  m_commit->Modify( via );
149  via->SetLayerPair( layerMap[top_layer], layerMap[bottom_layer] );
151  hasChanges = true;
152  }
153  }
154  else
155  {
156  hasChanges |= swapBoardItem( segm, layerMap );
157  }
158  }
159 
160  for( BOARD_ITEM* zone : frame()->GetBoard()->Zones() )
161  hasChanges |= swapBoardItem( zone, layerMap );
162 
163  for( BOARD_ITEM* drawing : frame()->GetBoard()->Drawings() )
164  hasChanges |= swapBoardItem( drawing, layerMap );
165 
166  if( hasChanges )
167  {
168  frame()->OnModify();
169  m_commit->Push( "Layers moved" );
170  frame()->GetCanvas()->Refresh();
171  }
172 
173  return 0;
174 }
175 
176 
178 {
179  PCB_EDIT_FRAME* editFrame = getEditFrame<PCB_EDIT_FRAME>();
180  DIALOG_CLEANUP_TRACKS_AND_VIAS dlg( editFrame );
181 
182  dlg.ShowModal();
183  return 0;
184 }
185 
186 
188 {
189  PCB_EDIT_FRAME* editFrame = getEditFrame<PCB_EDIT_FRAME>();
190  DIALOG_CLEANUP_GRAPHICS dlg( editFrame, false );
191 
192  dlg.ShowModal();
193  return 0;
194 }
195 
196 
198 {
199  PCB_EDIT_FRAME* editFrame = getEditFrame<PCB_EDIT_FRAME>();
201  []( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector, PCB_SELECTION_TOOL* sTool )
202  {
203  sTool->FilterCollectorForHierarchy( aCollector, true );
204  } );
205  DIALOG_UNUSED_PAD_LAYERS dlg( editFrame, selection, *m_commit );
206 
207  dlg.ShowModal();
208 
209  return 0;
210 }
211 
212 
214 {
219 
221 
228 }
229 
230 
static TOOL_ACTION globalDeletions
Definition: pcb_actions.h:323
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:192
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:80
static TOOL_ACTION cleanupGraphics
Definition: pcb_actions.h:325
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:328
static TOOL_ACTION updateFootprint
Definition: pcb_actions.h:326
static TOOL_ACTION swapLayers
Definition: pcb_actions.h:330
static TOOL_ACTION changeFootprints
Definition: pcb_actions.h:329
static TOOL_ACTION editTracksAndVias
Definition: pcb_actions.h:321
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:324
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: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:97
BOARD * GetBoard()
static TOOL_ACTION updateFootprints
Definition: pcb_actions.h:327
PCB_SELECTION_TOOL * m_selectionTool
static TOOL_ACTION removeUnusedPads
Definition: pcb_actions.h:331
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:322
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: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:155
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:2203
BOARD * GetBoard() const
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:171
TRACKS & Tracks()
Definition: board.h:230
Position or shape has changed.
Definition: view_item.h:54