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 
38  PCB_TOOL_BASE( "pcbnew.GlobalEdit" )
39 {
40 }
41 
42 
44 {
45  if( aReason != RUN )
46  m_commit.reset( new BOARD_COMMIT( this ) );
47 }
48 
49 
51 {
52  // Find the selection tool, so they can cooperate
53  m_selectionTool = static_cast<SELECTION_TOOL*>( m_toolMgr->FindTool( "pcbnew.InteractiveSelection" ) );
54  wxASSERT_MSG( m_selectionTool, "pcbnew.InteractiveSelection tool is not available" );
55 
56  return true;
57 }
58 
59 
61 {
63  MODULE* mod = (selection.Empty() ? nullptr : selection.FirstOfKind<MODULE> () );
64  bool updateMode = false;
65  bool currentMode = false;
66 
67  if( aEvent.IsAction( &PCB_ACTIONS::updateFootprint ) )
68  {
69  updateMode = true;
70  currentMode = true;
71  }
72  else if( aEvent.IsAction( &PCB_ACTIONS::updateFootprints ) )
73  {
74  updateMode = true;
75  currentMode = false;
76  }
77  else if( aEvent.IsAction( &PCB_ACTIONS::changeFootprint ) )
78  {
79  updateMode = false;
80  currentMode = true;
81  }
82  else if( aEvent.IsAction( &PCB_ACTIONS::changeFootprints ) )
83  {
84  updateMode = false;
85  currentMode = false;
86  }
87  else
88  wxFAIL_MSG( "ExchangeFootprints: unexpected action" );
89 
90  // Footprint exchange could remove modules, so they have to be
91  // removed from the selection first
93 
94  // invoke the exchange dialog process
95  {
96  PCB_EDIT_FRAME* editFrame = getEditFrame<PCB_EDIT_FRAME>();
97  DIALOG_EXCHANGE_FOOTPRINTS dialog( editFrame, mod, updateMode, currentMode );
98  dialog.ShowQuasiModal();
99  }
100 
101  return 0;
102 }
103 
104 
106 {
107  if( new_layer[ aItem->GetLayer() ] != aItem->GetLayer() )
108  {
109  m_commit->Modify( aItem );
110  aItem->SetLayer( new_layer[ aItem->GetLayer() ] );
111  frame()->GetCanvas()->GetView()->Update( aItem, KIGFX::GEOMETRY );
112  return true;
113  }
114 
115  return false;
116 }
117 
118 
120 {
121  PCB_LAYER_ID new_layer[PCB_LAYER_ID_COUNT];
122 
123  DIALOG_SWAP_LAYERS dlg( frame(), new_layer );
124 
125  if( dlg.ShowModal() != wxID_OK )
126  return 0;
127 
128  bool hasChanges = false;
129 
130  // Change tracks.
131  for( auto segm : frame()->GetBoard()->Tracks() )
132  {
133  if( segm->Type() == PCB_VIA_T )
134  {
135  VIA* via = (VIA*) segm;
136  PCB_LAYER_ID top_layer, bottom_layer;
137 
138  if( via->GetViaType() == VIA_THROUGH )
139  continue;
140 
141  via->LayerPair( &top_layer, &bottom_layer );
142 
143  if( new_layer[bottom_layer] != bottom_layer || new_layer[top_layer] != top_layer )
144  {
145  m_commit->Modify( via );
146  via->SetLayerPair( new_layer[top_layer], new_layer[bottom_layer] );
147  frame()->GetCanvas()->GetView()->Update( via, KIGFX::GEOMETRY );
148  hasChanges = true;
149  }
150  }
151  else
152  {
153  hasChanges |= swapBoardItem( segm, new_layer );
154  }
155  }
156 
157  for( BOARD_ITEM* zone : frame()->GetBoard()->Zones() )
158  hasChanges |= swapBoardItem( zone, new_layer );
159 
160  for( BOARD_ITEM* drawing : frame()->GetBoard()->Drawings() )
161  hasChanges |= swapBoardItem( drawing, new_layer );
162 
163  if( hasChanges )
164  {
165  frame()->OnModify();
166  m_commit->Push( "Layers moved" );
167  frame()->GetCanvas()->Refresh();
168  }
169 
170  return 0;
171 }
172 
173 
175 {
180 
182 
187 }
188 
189 
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:303
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.
Class 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:307
static TOOL_ACTION updateFootprint
Definition: pcb_actions.h:305
static TOOL_ACTION swapLayers
Definition: pcb_actions.h:309
TOOL_BASE * FindTool(int aId) const
Function FindTool() Searches for a tool with given ID.
static TOOL_ACTION changeFootprints
Definition: pcb_actions.h:308
static TOOL_ACTION editTracksAndVias
Definition: pcb_actions.h:301
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:63
PCB_BASE_EDIT_FRAME * frame() const
static TOOL_ACTION cleanupTracksAndVias
Definition: pcb_actions.h:304
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()
Class TOOL_EVENT.
Definition: tool_event.h:168
VIATYPE_T GetViaType() const
Definition: class_track.h:346
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:119
BOARD * GetBoard()
static TOOL_ACTION updateFootprints
Definition: pcb_actions.h:306
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:302
int GlobalDeletions(const TOOL_EVENT &aEvent)
SELECTION_TOOL * m_selectionTool
Class PCB_EDIT_FRAME is the main frame for Pcbnew.
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:193
int CleanupTracksAndVias(const TOOL_EVENT &aEvent)
class VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:96
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:1097
BOARD * GetBoard() const
virtual PCB_LAYER_ID GetLayer() const
Function GetLayer returns the primary layer this item is on.
#define mod(a, n)
Definition: greymap.cpp:24
TRACKS & Tracks()
Definition: class_board.h:227
Color has changed.
Definition: view_item.h:57