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 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 <bitmaps.h>
25 #include <class_zone.h>
26 #include <tool/tool_manager.h>
27 #include <tools/pcb_actions.h>
28 #include <tools/selection_tool.h>
29 #include <tools/edit_tool.h>
33 #include <tools/global_edit_tool.h>
34 #include <board_commit.h>
35 
36 #include <memory>
37 
38 
40  PCB_TOOL_BASE( "pcbnew.GlobalEdit" ),
41  m_selectionTool( nullptr )
42 {
43 }
44 
45 
47 {
48  if( aReason != RUN )
49  m_commit = std::make_unique<BOARD_COMMIT>( this );
50 }
51 
52 
54 {
55  // Find the selection tool, so they can cooperate
56  m_selectionTool = static_cast<SELECTION_TOOL*>( m_toolMgr->FindTool( "pcbnew.InteractiveSelection" ) );
57  wxASSERT_MSG( m_selectionTool, "pcbnew.InteractiveSelection tool is not available" );
58 
59  return true;
60 }
61 
62 
64 {
66  MODULE* mod = (selection.Empty() ? nullptr : selection.FirstOfKind<MODULE> () );
67  bool updateMode = false;
68  bool currentMode = false;
69 
70  if( aEvent.IsAction( &PCB_ACTIONS::updateFootprint ) )
71  {
72  updateMode = true;
73  currentMode = true;
74  }
75  else if( aEvent.IsAction( &PCB_ACTIONS::updateFootprints ) )
76  {
77  updateMode = true;
78  currentMode = false;
79  }
80  else if( aEvent.IsAction( &PCB_ACTIONS::changeFootprint ) )
81  {
82  updateMode = false;
83  currentMode = true;
84  }
85  else if( aEvent.IsAction( &PCB_ACTIONS::changeFootprints ) )
86  {
87  updateMode = false;
88  currentMode = false;
89  }
90  else
91  wxFAIL_MSG( "ExchangeFootprints: unexpected action" );
92 
93  // Footprint exchange could remove modules, so they have to be
94  // removed from the selection first
96 
97  // invoke the exchange dialog process
98  {
99  PCB_EDIT_FRAME* editFrame = getEditFrame<PCB_EDIT_FRAME>();
100  DIALOG_EXCHANGE_FOOTPRINTS dialog( editFrame, mod, updateMode, currentMode );
101  dialog.ShowQuasiModal();
102  }
103 
104  return 0;
105 }
106 
107 
109 {
110  if( new_layer[ aItem->GetLayer() ] != aItem->GetLayer() )
111  {
112  m_commit->Modify( aItem );
113  aItem->SetLayer( new_layer[ aItem->GetLayer() ] );
114  frame()->GetCanvas()->GetView()->Update( aItem, KIGFX::GEOMETRY );
115  return true;
116  }
117 
118  return false;
119 }
120 
121 
123 {
124  PCB_LAYER_ID new_layer[PCB_LAYER_ID_COUNT];
125 
126  DIALOG_SWAP_LAYERS dlg( frame(), new_layer );
127 
128  if( dlg.ShowModal() != wxID_OK )
129  return 0;
130 
131  bool hasChanges = false;
132 
133  // Change tracks.
134  for( auto segm : frame()->GetBoard()->Tracks() )
135  {
136  if( segm->Type() == PCB_VIA_T )
137  {
138  VIA* via = (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( new_layer[bottom_layer] != bottom_layer || new_layer[top_layer] != top_layer )
147  {
148  m_commit->Modify( via );
149  via->SetLayerPair( new_layer[top_layer], new_layer[bottom_layer] );
150  frame()->GetCanvas()->GetView()->Update( via, KIGFX::GEOMETRY );
151  hasChanges = true;
152  }
153  }
154  else
155  {
156  hasChanges |= swapBoardItem( segm, new_layer );
157  }
158  }
159 
160  for( BOARD_ITEM* zone : frame()->GetBoard()->Zones() )
161  hasChanges |= swapBoardItem( zone, new_layer );
162 
163  for( BOARD_ITEM* drawing : frame()->GetBoard()->Drawings() )
164  hasChanges |= swapBoardItem( drawing, new_layer );
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 {
183 
185 
190 }
191 
192 
static TOOL_ACTION selectionClear
Clears the current selection.
Definition: pcb_actions.h:73
void LayerPair(PCB_LAYER_ID *top_layer, PCB_LAYER_ID *bottom_layer) const
Function LayerPair Return the 2 layers used by the via (the via actually uses all layers between thes...
static TOOL_ACTION globalDeletions
Definition: pcb_actions.h:301
void setTransitions() override
Sets up handlers for various events.
bool swapBoardItem(BOARD_ITEM *aItem, PCB_LAYER_ID *new_layer)
virtual void Update(VIEW_ITEM *aItem, int aUpdateFlags) override
For dynamic VIEWs, informs the associated VIEW that the graphical representation of this item has cha...
Definition: pcb_view.cpp:91
virtual void SetLayer(PCB_LAYER_ID aLayer)
Function SetLayer sets the layer this item is on.
BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
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:81
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:219
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:109
static TOOL_ACTION changeFootprint
Definition: pcb_actions.h:305
static TOOL_ACTION updateFootprint
Definition: pcb_actions.h:303
static TOOL_ACTION swapLayers
Definition: pcb_actions.h:307
TOOL_BASE * FindTool(int aId) const
Function FindTool() Searches for a tool with given ID.
static TOOL_ACTION changeFootprints
Definition: pcb_actions.h:306
static TOOL_ACTION editTracksAndVias
Definition: pcb_actions.h:299
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Function Go()
bool IsAction(const TOOL_ACTION *aAction) const
Function IsAction() Tests if the event contains an action issued upon activation of the given TOOL_AC...
Definition: tool_event.cpp:67
PCB_BASE_EDIT_FRAME * frame() const
static TOOL_ACTION cleanupTracksAndVias
Definition: pcb_actions.h:302
int SwapLayers(const TOOL_EVENT &aEvent)
int EditTracksAndVias(const TOOL_EVENT &aEvent)
PCB_LAYER_ID
A quick note on layer IDs:
const PCBNEW_SELECTION & selection() const
int ShowQuasiModal()
TOOL_EVENT.
Definition: tool_event.h:171
void SetLayerPair(PCB_LAYER_ID aTopLayer, PCB_LAYER_ID aBottomLayer)
Function SetLayerPair For a via m_Layer contains the top layer, the other layer is in m_BottomLayer.
virtual KIGFX::PCB_VIEW * GetView() const override
Function GetView() Returns 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 Empty() const
Checks if there is anything selected.
Definition: selection.h:120
BOARD * GetBoard()
static TOOL_ACTION updateFootprints
Definition: pcb_actions.h:304
bool Init() override
Function Init() Init() is called once upon a registration of the tool.
virtual void OnModify()
Function OnModify Virtual Must be called after a change in order to set the "modify" flag of the curr...
void Reset(RESET_REASON aReason) override
Function Reset() Brings the tool to a known, initial state.
PCBNEW_SELECTION & RequestSelection(CLIENT_SELECTION_FILTER aClientFilter, std::vector< BOARD_ITEM * > *aFiltered=nullptr, bool aConfirmLockedItems=false)
Function RequestSelection()
static TOOL_ACTION editTextAndGraphics
Definition: pcb_actions.h:300
int GlobalDeletions(const TOOL_EVENT &aEvent)
SELECTION_TOOL * m_selectionTool
PCB_EDIT_FRAME is the main frame for Pcbnew.
VIATYPE GetViaType() const
Definition: class_track.h:336
int ExchangeFootprints(const TOOL_EVENT &aEvent)
Function ExchangeFootprints()
RESET_REASON
Determines the reason of reset for a tool
Definition: tool_base.h:79
T * FirstOfKind() const
Definition: selection.h:194
int CleanupTracksAndVias(const TOOL_EVENT &aEvent)
class VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:97
std::unique_ptr< BOARD_COMMIT > m_commit
static void FootprintFilter(const VECTOR2I &, GENERAL_COLLECTOR &aCollector)
Function FootprintFilter() A selection filter which prunes the selection to contain only items of typ...
Definition: edit_tool.cpp:1129
BOARD * GetBoard() const
virtual PCB_LAYER_ID GetLayer() const
Function GetLayer returns the primary layer this item is on.
TRACKS & Tracks()
Definition: class_board.h:217
Color has changed.
Definition: view_item.h:57