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 
40 #include <utility>
41 
42 #include "pcb_actions.h"
43 #include "selection_tool.h"
45 #include "edit_tool.h"
46 
47 // Pad tools
49  "pcbnew.PadTool.CopyPadSettings",
50  AS_GLOBAL, 0,
51  _( "Copy Pad Properties" ), _( "Copy current pad's properties to the default pad properties" ),
52  copy_pad_settings_xpm );
53 
55  "pcbnew.PadTool.ApplyPadSettings",
56  AS_GLOBAL, 0,
57  _( "Apply Pad Properties" ), _( "Copy the default pad properties to the current pad" ),
58  apply_pad_settings_xpm );
59 
61  "pcbnew.PadTool.PushPadSettings",
62  AS_GLOBAL, 0,
63  _( "Push Pad Properties..." ), _( "Copy the current pad settings to other pads" ),
64  push_pad_settings_xpm );
65 
66 
68 {
69 public:
70 
71  using SHOW_FUNCTOR = std::function<bool()>;
72 
73  PAD_CONTEXT_MENU( bool aEditingFootprint,
74  SHOW_FUNCTOR aHaveGlobalPadSetting ):
75  m_editingFootprint( aEditingFootprint ),
76  m_haveGlobalPadSettings( std::move( aHaveGlobalPadSetting ) )
77  {
78  SetIcon( pad_xpm );
79  SetTitle( _( "Pads" ) );
80 
84 
85 
86  // show modedit-specific items
87  if( m_editingFootprint )
88  {
89  AppendSeparator();
90 
92  }
93  }
94 
95 protected:
96 
97  CONTEXT_MENU* create() const override
98  {
100  }
101 
102 private:
103 
104  struct ENABLEMENTS
105  {
106  bool canImport;
107  bool canExport;
108  bool canPush;
109  };
110 
111  ENABLEMENTS getEnablements( const SELECTION& aSelection )
112  {
113  using S_C = SELECTION_CONDITIONS;
114  ENABLEMENTS enablements;
115 
116  auto anyPadSel = S_C::HasType( PCB_PAD_T );
117  auto singlePadSel = S_C::Count( 1 ) && S_C::OnlyType( PCB_PAD_T );
118 
119  // Apply pads enabled when any pads selected (it applies to each one
120  // individually), plus need a valid global pad setting
121  enablements.canImport = m_haveGlobalPadSettings() && ( anyPadSel )( aSelection );
122 
123  // Copy pads item enabled only when there is a single pad selected
124  // (otherwise how would we know which one to copy?)
125  enablements.canExport = ( singlePadSel )( aSelection );
126 
127  // Push pads available when there is a single pad to push from
128  enablements.canPush = ( singlePadSel )( aSelection );
129 
130  return enablements;
131  }
132 
133  void update() override
134  {
135  auto selTool = getToolManager()->GetTool<SELECTION_TOOL>();
136  const SELECTION& selection = selTool->GetSelection();
137 
138  auto enablements = getEnablements( selection );
139 
140  Enable( getMenuId( PCB_ACTIONS::applyPadSettings ), enablements.canImport );
141  Enable( getMenuId( PCB_ACTIONS::copyPadSettings ), enablements.canExport );
142  Enable( getMenuId( PCB_ACTIONS::pushPadSettings ), enablements.canPush );
143  }
144 
147 };
148 
149 
151  PCB_TOOL( "pcbnew.PadTool" ), m_padCopied( false )
152 {
153 }
154 
155 
157 {}
158 
159 
161 {
162  m_padCopied = false;
163 }
164 
165 
167 {
168  auto& board = *getModel<BOARD>();
169  return board.m_Modules.GetCount() > 0;
170 }
171 
172 
174 {
175  auto contextMenu = std::make_shared<PAD_CONTEXT_MENU>( EditingModules(),
176  [this]() { return m_padCopied; } );
177  contextMenu->SetTool( this );
178 
180 
181  if( selTool )
182  {
183  auto& toolMenu = selTool->GetToolMenu();
184  auto& menu = toolMenu.GetMenu();
185 
186  toolMenu.AddSubMenu( contextMenu );
187 
188  auto canShowMenuCond = [this, contextMenu] ( const SELECTION& aSel ) {
189  contextMenu->UpdateAll();
190  return frame()->GetToolId() == ID_NO_TOOL_SELECTED
191  && haveFootprints()
192  && contextMenu->HasEnabledItems();
193  };
194 
195  // show menu when there is a footprint, and the menu has any items
196  auto showCond = canShowMenuCond &&
198  || SELECTION_CONDITIONS::Count( 0 ) );
199 
200  menu.AddMenu( contextMenu.get(), false, showCond, 1000 );
201 
202  // we need a separator only when the selection is empty
203  auto separatorCond = canShowMenuCond && SELECTION_CONDITIONS::Count( 0 );
204  menu.AddSeparator( separatorCond, 1000 );
205  }
206 
207  return true;
208 }
209 
210 
212 {
213  auto& selTool = *m_toolMgr->GetTool<SELECTION_TOOL>();
214  const auto& selection = selTool.GetSelection();
215 
216  auto& frame = *getEditFrame<PCB_EDIT_FRAME>();
217 
218  const D_PAD& masterPad = frame.GetDesignSettings().m_Pad_Master;
219 
220  BOARD_COMMIT commit( &frame );
221 
222  // for every selected pad, copy global settings
223  for( auto item : selection )
224  {
225  if( item->Type() == PCB_PAD_T )
226  {
227  commit.Modify( item );
228 
229  D_PAD& destPad = static_cast<D_PAD&>( *item );
230  destPad.ImportSettingsFromMaster( masterPad );
231  }
232  }
233 
234  commit.Push( _( "Apply Pad Properties" ) );
235 
237  frame.Refresh();
238 
239  return 0;
240 }
241 
242 
244 {
245  auto& selTool = *m_toolMgr->GetTool<SELECTION_TOOL>();
246  const auto& selection = selTool.GetSelection();
247 
248  auto& frame = *getEditFrame<PCB_EDIT_FRAME>();
249 
250  D_PAD& masterPad = frame.GetDesignSettings().m_Pad_Master;
251 
252  // can only copy from a single pad
253  if( selection.Size() == 1 )
254  {
255  auto item = selection[0];
256 
257  if( item->Type() == PCB_PAD_T )
258  {
259  const auto& selPad = static_cast<const D_PAD&>( *item );
260  masterPad.ImportSettingsFromMaster( selPad );
261  m_padCopied = true;
262  }
263  }
264 
265  return 0;
266 }
267 
268 
270  const D_PAD& aSrcPad,
271  BOARD_COMMIT& commit,
272  bool aSameFootprints,
273  bool aPadShapeFilter,
274  bool aPadOrientFilter,
275  bool aPadLayerFilter )
276 {
277  const MODULE* moduleRef = aSrcPad.GetParent();
278 
279  // If there is no module, we can't make the comparisons to see which
280  // pads to apply the source pad settings to
281  if( moduleRef == nullptr )
282  {
283  wxLogDebug( "globalChangePadSettings() Error: NULL module" );
284  return;
285  }
286 
287  double pad_orient = aSrcPad.GetOrientation() - moduleRef->GetOrientation();
288 
289  for( const MODULE* module = board.m_Modules; module; module = module->Next() )
290  {
291  if( !aSameFootprints && ( module != moduleRef ) )
292  continue;
293 
294  if( module->GetFPID() != moduleRef->GetFPID() )
295  continue;
296 
297  for( D_PAD* pad = module->PadsList(); pad; pad = pad->Next() )
298  {
299  // Filters changes prohibited.
300  if( aPadShapeFilter && ( pad->GetShape() != aSrcPad.GetShape() ) )
301  continue;
302 
303  double currpad_orient = pad->GetOrientation() - module->GetOrientation();
304 
305  if( aPadOrientFilter && ( currpad_orient != pad_orient ) )
306  continue;
307 
308  if( aPadLayerFilter && ( pad->GetLayerSet() != aSrcPad.GetLayerSet() ) )
309  continue;
310 
311  if( aPadLayerFilter && ( pad->GetLayerSet() != aSrcPad.GetLayerSet() ) )
312  continue;
313 
314  commit.Modify( pad );
315 
316  // Apply source pad settings to this pad
317  pad->ImportSettingsFromMaster( aSrcPad );
318  }
319  }
320 }
321 
322 
324 {
325  auto& selTool = *m_toolMgr->GetTool<SELECTION_TOOL>();
326  const auto& selection = selTool.GetSelection();
327 
328  auto& frame = *getEditFrame<PCB_EDIT_FRAME>();
329 
330  // not const - can be changed in the dialog
331  D_PAD* srcPad = nullptr;
332 
333  // If nothing selected, use the master pad setting,
334  // otherwise, if one pad selected, use the selected pad
335  if( selection.Size() == 0 )
336  {
338  }
339  else if( selection.Size() == 1 )
340  {
341  if( selection[0]->Type() == PCB_PAD_T )
342  {
343  srcPad = static_cast<D_PAD*>( selection[0] );
344  }
345  }
346  else
347  {
348  // multiple selected what to do?
349  // maybe master->selection? same as apply multiple?
350  }
351 
352  // no valid selection, nothing to do
353  if( !srcPad )
354  {
355  return 0;
356  }
357 
358  MODULE* module = srcPad->GetParent();
359 
360  if( module != nullptr )
361  {
362  frame.SetMsgPanel( module );
363  }
364 
365  int dialogRet;
366  {
367  DIALOG_GLOBAL_PADS_EDITION dlg( &frame, srcPad );
368  dialogRet = dlg.ShowModal();
369  }
370 
371  // cancel
372  if( dialogRet == -1 )
373  {
374  return 0;
375  }
376 
377  const bool edit_Same_Modules = (dialogRet == 1);
378 
379  BOARD_COMMIT commit( &frame );
380 
381  globalChangePadSettings( *getModel<BOARD>(), *srcPad, commit,
382  edit_Same_Modules,
386 
387  commit.Push( _( "Apply Pad Properties" ) );
388 
390  frame.Refresh();
391 
392  return 0;
393 }
394 
395 
397 {
401 }
void update() override
Update menu state stub.
Definition: pad_tool.cpp:133
BOARD_ITEM_CONTAINER * GetParent() const
static void globalChangePadSettings(BOARD &board, const D_PAD &aSrcPad, BOARD_COMMIT &commit, bool aSameFootprints, bool aPadShapeFilter, bool aPadOrientFilter, bool aPadLayerFilter)
Definition: pad_tool.cpp:269
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:311
MODULE * Next() const
Definition: class_module.h:121
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:111
Template specialization to enable wxStrings for certain containers (e.g. unordered_map) ...
Definition: bitmap.cpp:54
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:160
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:111
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:339
const LIB_ID & GetFPID() const
Definition: class_module.h:191
bool m_editingFootprint
Definition: pad_tool.cpp:145
PAD_CONTEXT_MENU(bool aEditingFootprint, SHOW_FUNCTOR aHaveGlobalPadSetting)
Definition: pad_tool.cpp:73
int pushPadSettings(const TOOL_EVENT &aEvent)
Push pad settings from a pad to other pads on board or module
Definition: pad_tool.cpp:323
static TOOL_ACTION copyPadSettings
Copy the selected pad&#39;s settings to the board design settings.
Definition: pcb_actions.h:333
void SetMsgPanel(const std::vector< MSG_PANEL_ITEM > &aList)
Function SetMsgPanel clears the message panel and populates it with the contents of aList...
Definition: draw_frame.cpp:831
double GetOrientation() const
Definition: class_module.h:187
std::function< bool()> SHOW_FUNCTOR
Definition: pad_tool.cpp:71
PCB_EDIT_FRAME * frame() const
Definition: pcb_tool.h:136
Class TOOL_EVENT.
Definition: tool_event.h:162
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:336
SHOW_FUNCTOR m_haveGlobalPadSettings
Definition: pad_tool.cpp:146
D_PAD * Next() const
Definition: class_pad.h:160
MODULE * module() const
Definition: pcb_tool.h:138
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:221
void ImportSettingsFromMaster(const D_PAD &aMasterPad)
Imports the pad settings from aMasterPad.
Definition: class_pad.cpp:1393
All active tools
Definition: tool_event.h:138
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.
bool EditingModules() const
Definition: pcb_tool.h:99
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:169
DLIST< MODULE > m_Modules
Definition: class_board.h:247
int GetToolId() const
Definition: draw_frame.h:496
TOOL_MANAGER * getToolManager()
Returns an instance of TOOL_MANAGER class.
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:97
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:166
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:161
Module description (excepted pads)
bool Init() override
Basic initalization
Definition: pad_tool.cpp:173
int applyPadSettings(const TOOL_EVENT &aEvent)
Apply pad settings from board design settings to a pad
Definition: pad_tool.cpp:211
void setTransitions() override
Bind handlers to corresponding TOOL_ACTIONs
Definition: pad_tool.cpp:396
int copyPadSettings(const TOOL_EVENT &aEvent)
Copy pad settings from a pad to the board design settings
Definition: pad_tool.cpp:243
BOARD * board() const
Definition: pcb_tool.h:137