KiCad PCB EDA Suite
pad_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) 2017 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 
25 #include "pad_tool.h"
26 
27 #include <pcb_edit_frame.h>
28 #include <class_draw_panel_gal.h>
29 #include <view/view_controls.h>
30 #include <view/view.h>
31 #include <tool/tool_manager.h>
32 #include <bitmaps.h>
33 
34 #include <class_board_item.h>
35 #include <class_module.h>
36 #include <board_commit.h>
37 
39 #include "pcb_actions.h"
40 #include "selection_tool.h"
42 #include "edit_tool.h"
43 
44 // Pad tools
46  "pcbnew.PadTool.CopyPadSettings",
47  AS_GLOBAL, 0,
48  _( "Copy Pad Properties" ), _( "Copy current pad's properties" ),
49  copy_pad_settings_xpm );
50 
52  "pcbnew.PadTool.ApplyPadSettings",
53  AS_GLOBAL, 0,
54  _( "Paste Pad Properties" ), _( "Replace the current pad's properties with those copied earlier" ),
55  apply_pad_settings_xpm );
56 
58  "pcbnew.PadTool.PushPadSettings",
59  AS_GLOBAL, 0,
60  _( "Push Pad Properties..." ), _( "Copy the current pad's properties to other pads" ),
61  push_pad_settings_xpm );
62 
63 
65 {
66 public:
67 
68  using SHOW_FUNCTOR = std::function<bool()>;
69 
70  PAD_CONTEXT_MENU( bool aEditingFootprint, SHOW_FUNCTOR aHaveGlobalPadSetting ):
71  m_editingFootprint( aEditingFootprint ),
72  m_haveGlobalPadSettings( std::move( aHaveGlobalPadSetting ) )
73  {
74  SetIcon( pad_xpm );
75  SetTitle( _( "Pads" ) );
76 
80 
81  // show modedit-specific items
82  if( m_editingFootprint )
83  {
84  AppendSeparator();
86  }
87  }
88 
89 protected:
90 
91  CONTEXT_MENU* create() const override
92  {
94  }
95 
96 private:
97 
98  struct ENABLEMENTS
99  {
100  bool canImport;
101  bool canExport;
102  bool canPush;
103  };
104 
105  ENABLEMENTS getEnablements( const SELECTION& aSelection )
106  {
107  using S_C = SELECTION_CONDITIONS;
108  ENABLEMENTS enablements;
109 
110  auto anyPadSel = S_C::HasType( PCB_PAD_T );
111  auto singlePadSel = S_C::Count( 1 ) && S_C::OnlyType( PCB_PAD_T );
112 
113  // Apply pads enabled when any pads selected (it applies to each one
114  // individually), plus need a valid global pad setting
115  enablements.canImport = m_haveGlobalPadSettings() && ( anyPadSel )( aSelection );
116 
117  // Copy pads item enabled only when there is a single pad selected
118  // (otherwise how would we know which one to copy?)
119  enablements.canExport = ( singlePadSel )( aSelection );
120 
121  // Push pads available when there is a single pad to push from
122  enablements.canPush = ( singlePadSel )( aSelection );
123 
124  return enablements;
125  }
126 
127  void update() override
128  {
129  auto selTool = getToolManager()->GetTool<SELECTION_TOOL>();
130  const SELECTION& selection = selTool->GetSelection();
131 
132  auto enablements = getEnablements( selection );
133 
134  Enable( getMenuId( PCB_ACTIONS::applyPadSettings ), enablements.canImport );
135  Enable( getMenuId( PCB_ACTIONS::copyPadSettings ), enablements.canExport );
136  Enable( getMenuId( PCB_ACTIONS::pushPadSettings ), enablements.canPush );
137  }
138 
141 };
142 
143 
145  PCB_TOOL( "pcbnew.PadTool" ),
146  m_padCopied( false )
147 {}
148 
149 
151 {}
152 
153 
155 {
156  m_padCopied = false;
157 }
158 
159 
161 {
162  auto& board = *getModel<BOARD>();
163  return board.m_Modules.GetCount() > 0;
164 }
165 
166 
168 {
169  auto contextMenu = std::make_shared<PAD_CONTEXT_MENU>( EditingModules(),
170  [this]() { return m_padCopied; } );
171  contextMenu->SetTool( this );
172 
174 
175  if( selTool )
176  {
177  auto& toolMenu = selTool->GetToolMenu();
178  auto& menu = toolMenu.GetMenu();
179 
180  toolMenu.AddSubMenu( contextMenu );
181 
182  auto canShowMenuCond = [this, contextMenu] ( const SELECTION& aSel ) {
183  contextMenu->UpdateAll();
184  return frame()->GetToolId() == ID_NO_TOOL_SELECTED
185  && haveFootprints()
186  && contextMenu->HasEnabledItems();
187  };
188 
189  // show menu when there is a footprint, and the menu has any items
190  auto showCond = canShowMenuCond &&
192  || SELECTION_CONDITIONS::Count( 0 ) );
193 
194  menu.AddMenu( contextMenu.get(), false, showCond, 1000 );
195 
196  // we need a separator only when the selection is empty
197  auto separatorCond = canShowMenuCond && SELECTION_CONDITIONS::Count( 0 );
198  menu.AddSeparator( separatorCond, 1000 );
199  }
200 
201  return true;
202 }
203 
204 
206 {
207  auto& selTool = *m_toolMgr->GetTool<SELECTION_TOOL>();
208  const auto& selection = selTool.GetSelection();
209  auto& frame = *getEditFrame<PCB_EDIT_FRAME>();
210  const D_PAD& masterPad = frame.GetDesignSettings().m_Pad_Master;
211 
212  BOARD_COMMIT commit( &frame );
213 
214  // for every selected pad, paste global settings
215  for( auto item : selection )
216  {
217  if( item->Type() == PCB_PAD_T )
218  {
219  commit.Modify( item );
220  static_cast<D_PAD&>( *item ).ImportSettingsFromMaster( masterPad );
221  }
222  }
223 
224  commit.Push( _( "Paste Pad Properties" ) );
225 
227  frame.Refresh();
228 
229  return 0;
230 }
231 
232 
234 {
235  auto& selTool = *m_toolMgr->GetTool<SELECTION_TOOL>();
236  const auto& selection = selTool.GetSelection();
237 
238  auto& frame = *getEditFrame<PCB_EDIT_FRAME>();
239 
240  D_PAD& masterPad = frame.GetDesignSettings().m_Pad_Master;
241 
242  // can only copy from a single pad
243  if( selection.Size() == 1 )
244  {
245  auto item = selection[0];
246 
247  if( item->Type() == PCB_PAD_T )
248  {
249  const auto& selPad = static_cast<const D_PAD&>( *item );
250  masterPad.ImportSettingsFromMaster( selPad );
251  m_padCopied = true;
252  }
253  }
254 
255  return 0;
256 }
257 
258 
259 static void doPushPadProperties( BOARD& board, const D_PAD& aSrcPad, BOARD_COMMIT& commit,
260  bool aSameFootprints,
261  bool aPadShapeFilter,
262  bool aPadOrientFilter,
263  bool aPadLayerFilter )
264 {
265  const MODULE* moduleRef = aSrcPad.GetParent();
266 
267  double pad_orient = aSrcPad.GetOrientation() - moduleRef->GetOrientation();
268 
269  for( const MODULE* module = board.m_Modules; module; module = module->Next() )
270  {
271  if( !aSameFootprints && ( module != moduleRef ) )
272  continue;
273 
274  if( module->GetFPID() != moduleRef->GetFPID() )
275  continue;
276 
277  for( D_PAD* pad = module->PadsList(); pad; pad = pad->Next() )
278  {
279  if( aPadShapeFilter && ( pad->GetShape() != aSrcPad.GetShape() ) )
280  continue;
281 
282  double currpad_orient = pad->GetOrientation() - module->GetOrientation();
283 
284  if( aPadOrientFilter && ( currpad_orient != pad_orient ) )
285  continue;
286 
287  if( aPadLayerFilter && ( pad->GetLayerSet() != aSrcPad.GetLayerSet() ) )
288  continue;
289 
290  if( aPadLayerFilter && ( pad->GetLayerSet() != aSrcPad.GetLayerSet() ) )
291  continue;
292 
293  commit.Modify( pad );
294 
295  // Apply source pad settings to this pad
296  pad->ImportSettingsFromMaster( aSrcPad );
297  }
298  }
299 }
300 
301 
303 {
304  auto& selTool = *m_toolMgr->GetTool<SELECTION_TOOL>();
305  const auto& selection = selTool.GetSelection();
306  auto& frame = *getEditFrame<PCB_EDIT_FRAME>();
307  D_PAD* srcPad;
308 
309  if( selection.Size() == 1 && selection[0]->Type() == PCB_PAD_T )
310  srcPad = static_cast<D_PAD*>( selection[0] );
311  else
312  return 0;
313 
314  MODULE* module = srcPad->GetParent();
315 
316  if( !module )
317  return 0;
318 
319  frame.SetMsgPanel( module );
320 
322  int dialogRet = dlg.ShowModal();
323 
324  if( dialogRet == wxID_CANCEL )
325  return 0;
326 
327  const bool edit_Same_Modules = (dialogRet == 1);
328 
329  BOARD_COMMIT commit( &frame );
330 
331  doPushPadProperties( *getModel<BOARD>(), *srcPad, commit, edit_Same_Modules,
335 
336  commit.Push( _( "Paste Pad Properties" ) );
337 
339  frame.Refresh();
340 
341  return 0;
342 }
343 
344 
346 {
350 }
void update() override
Update menu state stub.
Definition: pad_tool.cpp:127
BOARD_ITEM_CONTAINER * GetParent() const
COMMIT & Modify(EDA_ITEM *aItem)
Modifies a given item in the model.
Definition: commit.h:103
Class CONTEXT_MENU.
Definition: context_menu.h:44
VIEW_CONTROLS class definition.
Classes BOARD_ITEM and BOARD_CONNECTED_ITEM.
Class SELECTION_TOOL.
CONDITIONAL_MENU & GetMenu()
Function GetMenu.
Definition: tool_menu.cpp:49
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:215
static TOOL_ACTION enumeratePads
Tool for quick pad enumeration.
Definition: pcb_actions.h:315
MODULE * Next() const
Definition: class_module.h:123
MODULE * GetParent() const
Definition: class_pad.h:162
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:125
static SELECTION_CONDITION HasType(KICAD_T aType)
Function HasType Creates a functor that tests if among the selected items there is at least one of a ...
Class that groups generic conditions for selected items.
bool m_padCopied
Flag to indicate there are valid settings stored in the Master Pad object
Definition: pad_tool.h:66
ENABLEMENTS getEnablements(const SELECTION &aSelection)
Definition: pad_tool.cpp:105
Template specialization to enable wxStrings for certain containers (e.g. unordered_map) ...
Definition: bitmap.cpp:55
class D_PAD, a pad in a footprint
Definition: typeinfo.h:90
static SELECTION_CONDITION Count(int aNumber)
Function Count Creates a functor that tests if the number of selected items is equal to the value giv...
PAD_SHAPE_T GetShape() const
Function GetShape.
Definition: class_pad.h:216
void Reset(RESET_REASON aReason) override
React to model/view changes
Definition: pad_tool.cpp:154
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Function Go()
static TOOL_ACTION selectionModified
Modified selection notification.
Definition: pcb_actions.h:114
wxMenuItem * Add(const wxString &aLabel, int aId, const BITMAP_OPAQUE *aIcon=NULL)
Function Add() Adds an entry to the menu.
static TOOL_ACTION pushPadSettings
Copy the current pad&#39;s settings to other pads in the module or on the board.
Definition: pcb_actions.h:343
const LIB_ID & GetFPID() const
Definition: class_module.h:193
bool m_editingFootprint
Definition: pad_tool.cpp:139
PAD_CONTEXT_MENU(bool aEditingFootprint, SHOW_FUNCTOR aHaveGlobalPadSetting)
Definition: pad_tool.cpp:70
int pushPadSettings(const TOOL_EVENT &aEvent)
Push pad settings from a pad to other pads on board or module
Definition: pad_tool.cpp:302
static TOOL_ACTION copyPadSettings
Copy the selected pad&#39;s settings to the board design settings.
Definition: pcb_actions.h:337
void SetMsgPanel(const std::vector< MSG_PANEL_ITEM > &aList)
Clear the message panel and populates it with the contents of aList.
double GetOrientation() const
Definition: class_module.h:189
std::function< bool()> SHOW_FUNCTOR
Definition: pad_tool.cpp:68
PCB_EDIT_FRAME * frame() const
Definition: pcb_tool.h:139
static void doPushPadProperties(BOARD &board, const D_PAD &aSrcPad, BOARD_COMMIT &commit, bool aSameFootprints, bool aPadShapeFilter, bool aPadOrientFilter, bool aPadLayerFilter)
Definition: pad_tool.cpp:259
Class TOOL_EVENT.
Definition: tool_event.h:168
LSET GetLayerSet() const override
Function GetLayerSet returns a "layer mask", which is a bitmap of all layers on which the TRACK segme...
Definition: class_pad.h:402
static TOOL_ACTION applyPadSettings
Copy the pad settings in the board design settings to the selected pad.
Definition: pcb_actions.h:340
SHOW_FUNCTOR m_haveGlobalPadSettings
Definition: pad_tool.cpp:140
int pastePadProperties(const TOOL_EVENT &aEvent)
Apply pad settings from board design settings to a pad
Definition: pad_tool.cpp:205
D_PAD * Next() const
Definition: class_pad.h:160
MODULE * module() const
Definition: pcb_tool.h:141
virtual BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Function GetDesignSettings returns the BOARD_DESIGN_SETTINGS for the BOARD owned by this frame...
const SELECTION & selection() const
Definition: pcb_tool.cpp:245
void ImportSettingsFromMaster(const D_PAD &aMasterPad)
Imports the pad settings from aMasterPad.
Definition: class_pad.cpp:1399
All active tools
Definition: tool_event.h:144
static int getMenuId(const TOOL_ACTION &aAction)
Returns the corresponding wxMenuItem identifier for a TOOL_ACTION object.
Definition: context_menu.h:155
void SetIcon(const BITMAP_OPAQUE *aIcon)
Function SetIcon() Assigns an icon for the entry.
TOOL_MANAGER * getToolManager() const
Returns an instance of TOOL_MANAGER class.
bool EditingModules() const
Definition: pcb_tool.h:102
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:170
DLIST< MODULE > m_Modules
Definition: class_board.h:248
int GetToolId() const
Definition: draw_frame.h:519
void SetTitle(const wxString &aTitle) override
Function SetTitle() Sets title for the context menu.
CONTEXT_MENU * create() const override
Returns an instance of this class. It has to be overridden in inheriting classes. ...
Definition: pad_tool.cpp:91
Class TOOL_ACTION.
Definition: tool_action.h:46
double GetOrientation() const
Function GetOrientation returns the rotation angle of the pad in tenths of degrees, but soon degrees.
Definition: class_pad.h:382
static SELECTION_CONDITION OnlyType(KICAD_T aType)
Function OnlyType Creates a functor that tests if the selected items are only of given type...
D_PAD m_Pad_Master
A dummy pad to store all default parameters.
virtual void Push(const wxString &aMessage=wxT("A commit"), bool aCreateUndoEntry=true, bool aSetDirtyBit=true) override
Executes the changes.
RESET_REASON
Determines the reason of reset for a tool
Definition: tool_base.h:80
bool haveFootprints()
Determine if there are any footprints on the board
Definition: pad_tool.cpp:160
unsigned GetCount() const
Function GetCount returns the number of elements in the list.
Definition: dlist.h:126
DLIST< D_PAD > & PadsList()
Definition: class_module.h:163
Module description (excepted pads)
bool Init() override
Basic initalization
Definition: pad_tool.cpp:167
void setTransitions() override
Bind handlers to corresponding TOOL_ACTIONs
Definition: pad_tool.cpp:345
int copyPadSettings(const TOOL_EVENT &aEvent)
Copy pad settings from a pad to the board design settings
Definition: pad_tool.cpp:233
BOARD * board() const
Definition: pcb_tool.h:140