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_track.h>
26 #include <class_zone.h>
27 #include <tool/tool_manager.h>
28 #include <tools/pcb_actions.h>
29 #include <tools/selection_tool.h>
30 #include <tools/edit_tool.h>
34 #include <tools/global_edit_tool.h>
35 #include <board_commit.h>
36 
37 #include <memory>
38 
39 
41  PCB_TOOL_BASE( "pcbnew.GlobalEdit" ),
42  m_selectionTool( nullptr )
43 {
44 }
45 
46 
48 {
49  if( aReason != RUN )
50  m_commit = std::make_unique<BOARD_COMMIT>( this );
51 }
52 
53 
55 {
56  // Find the selection tool, so they can cooperate
57  m_selectionTool = static_cast<SELECTION_TOOL*>( m_toolMgr->FindTool( "pcbnew.InteractiveSelection" ) );
58  wxASSERT_MSG( m_selectionTool, "pcbnew.InteractiveSelection tool is not available" );
59 
60  return true;
61 }
62 
63 
65 {
67  MODULE* mod = (selection.Empty() ? nullptr : selection.FirstOfKind<MODULE> () );
68  bool updateMode = false;
69  bool currentMode = false;
70 
71  if( aEvent.IsAction( &PCB_ACTIONS::updateFootprint ) )
72  {
73  updateMode = true;
74  currentMode = true;
75  }
76  else if( aEvent.IsAction( &PCB_ACTIONS::updateFootprints ) )
77  {
78  updateMode = true;
79  currentMode = false;
80  }
81  else if( aEvent.IsAction( &PCB_ACTIONS::changeFootprint ) )
82  {
83  updateMode = false;
84  currentMode = true;
85  }
86  else if( aEvent.IsAction( &PCB_ACTIONS::changeFootprints ) )
87  {
88  updateMode = false;
89  currentMode = false;
90  }
91  else
92  wxFAIL_MSG( "ExchangeFootprints: unexpected action" );
93 
94  // Footprint exchange could remove modules, so they have to be
95  // removed from the selection first
97 
98  // invoke the exchange dialog process
99  {
100  PCB_EDIT_FRAME* editFrame = getEditFrame<PCB_EDIT_FRAME>();
101  DIALOG_EXCHANGE_FOOTPRINTS dialog( editFrame, mod, updateMode, currentMode );
102  dialog.ShowQuasiModal();
103  }
104 
105  return 0;
106 }
107 
108 
110 {
111  if( new_layer[ aItem->GetLayer() ] != aItem->GetLayer() )
112  {
113  m_commit->Modify( aItem );
114  aItem->SetLayer( new_layer[ aItem->GetLayer() ] );
115  frame()->GetCanvas()->GetView()->Update( aItem, KIGFX::GEOMETRY );
116  return true;
117  }
118 
119  return false;
120 }
121 
122 
124 {
125  PCB_LAYER_ID new_layer[PCB_LAYER_ID_COUNT];
126 
127  DIALOG_SWAP_LAYERS dlg( frame(), new_layer );
128 
129  if( dlg.ShowModal() != wxID_OK )
130  return 0;
131 
132  bool hasChanges = false;
133 
134  // Change tracks.
135  for( auto segm : frame()->GetBoard()->Tracks() )
136  {
137  if( segm->Type() == PCB_VIA_T )
138  {
139  VIA* via = (VIA*) segm;
140  PCB_LAYER_ID top_layer, bottom_layer;
141 
142  if( via->GetViaType() == VIATYPE::THROUGH )
143  continue;
144 
145  via->LayerPair( &top_layer, &bottom_layer );
146 
147  if( new_layer[bottom_layer] != bottom_layer || new_layer[top_layer] != top_layer )
148  {
149  m_commit->Modify( via );
150  via->SetLayerPair( new_layer[top_layer], new_layer[bottom_layer] );
151  frame()->GetCanvas()->GetView()->Update( via, KIGFX::GEOMETRY );
152  hasChanges = true;
153  }
154  }
155  else
156  {
157  hasChanges |= swapBoardItem( segm, new_layer );
158  }
159  }
160 
161  for( BOARD_ITEM* zone : frame()->GetBoard()->Zones() )
162  hasChanges |= swapBoardItem( zone, new_layer );
163 
164  for( BOARD_ITEM* drawing : frame()->GetBoard()->Drawings() )
165  hasChanges |= swapBoardItem( drawing, new_layer );
166 
167  if( hasChanges )
168  {
169  frame()->OnModify();
170  m_commit->Push( "Layers moved" );
171  frame()->GetCanvas()->Refresh();
172  }
173 
174  return 0;
175 }
176 
177 
179 {
184 
186 
191 }
192 
193 
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.
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:80
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:218
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:139
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()
Definitions for tracks, vias and zones.
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: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()
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: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
PCB_EDIT_FRAME is the main frame for Pcbnew.
VIATYPE GetViaType() const
Definition: class_track.h:416
int ExchangeFootprints(const TOOL_EVENT &aEvent)
Function ExchangeFootprints()
RESET_REASON
Determines the reason of reset for a tool
Definition: tool_base.h:78
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:1184
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:220
Color has changed.
Definition: view_item.h:57