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