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 
84  // show modedit-specific items
85  if( m_editingFootprint )
86  {
87  AppendSeparator();
88 
90  }
91  }
92 
93 protected:
94 
95  CONTEXT_MENU* create() const override
96  {
98  }
99 
100 private:
101 
102  struct ENABLEMENTS
103  {
104  bool canImport;
105  bool canExport;
106  bool canPush;
107  };
108 
109  ENABLEMENTS getEnablements( const SELECTION& aSelection )
110  {
111  using S_C = SELECTION_CONDITIONS;
112  ENABLEMENTS enablements;
113 
114  auto anyPadSel = S_C::HasType( PCB_PAD_T );
115  auto singlePadSel = S_C::Count( 1 ) && S_C::OnlyType( PCB_PAD_T );
116 
117  // Apply pads enabled when any pads selected (it applies to each one
118  // individually), plus need a valid global pad setting
119  enablements.canImport = m_haveGlobalPadSettings() && ( anyPadSel )( aSelection );
120 
121  // Copy pads item enabled only when there is a single pad selected
122  // (otherwise how would we know which one to copy?)
123  enablements.canExport = ( singlePadSel )( aSelection );
124 
125  // Push pads available when there is a single pad to push from
126  enablements.canPush = ( singlePadSel )( aSelection );
127 
128  return enablements;
129  }
130 
131  void update() override
132  {
133  auto selTool = getToolManager()->GetTool<SELECTION_TOOL>();
134  const SELECTION& selection = selTool->GetSelection();
135 
136  auto enablements = getEnablements( selection );
137 
138  Enable( getMenuId( PCB_ACTIONS::applyPadSettings ), enablements.canImport );
139  Enable( getMenuId( PCB_ACTIONS::copyPadSettings ), enablements.canExport );
140  Enable( getMenuId( PCB_ACTIONS::pushPadSettings ), enablements.canPush );
141  }
142 
145 };
146 
147 
149  PCB_TOOL( "pcbnew.PadTool" ), m_padCopied( false )
150 {
151 }
152 
153 
155 {}
156 
157 
159 {
160  m_padCopied = false;
161 }
162 
163 
165 {
166  auto& board = *getModel<BOARD>();
167  return board.m_Modules.GetCount() > 0;
168 }
169 
170 
172 {
173  auto contextMenu = std::make_shared<PAD_CONTEXT_MENU>( EditingModules(),
174  [this]() { return m_padCopied; } );
175  contextMenu->SetTool( this );
176 
178 
179  if( selTool )
180  {
181  auto& toolMenu = selTool->GetToolMenu();
182  auto& menu = toolMenu.GetMenu();
183 
184  toolMenu.AddSubMenu( contextMenu );
185 
186  SELECTION_CONDITION canShowMenuCond = [this, contextMenu] ( const SELECTION& aSel ) {
187  contextMenu->UpdateAll();
188  return haveFootprints() && contextMenu->HasEnabledItems();
189  };
190 
191  // show menu when there is a footprint, and the menu has any items
192  auto showCond = canShowMenuCond &&
194  || SELECTION_CONDITIONS::Count( 0 ) );
195 
196  menu.AddMenu( contextMenu.get(), false, showCond );
197  }
198 
199  return true;
200 }
201 
202 
204 {
205  auto& selTool = *m_toolMgr->GetTool<SELECTION_TOOL>();
206  const auto& selection = selTool.GetSelection();
207 
208  auto& frame = *getEditFrame<PCB_EDIT_FRAME>();
209 
210  const D_PAD& masterPad = frame.GetDesignSettings().m_Pad_Master;
211 
212  BOARD_COMMIT commit( &frame );
213 
214  // for every selected pad, copy global settings
215  for( auto item : selection )
216  {
217  if( item->Type() == PCB_PAD_T )
218  {
219  commit.Modify( item );
220 
221  D_PAD& destPad = static_cast<D_PAD&>( *item );
222  destPad.ImportSettingsFromMaster( masterPad );
223  }
224  }
225 
226  commit.Push( _( "Apply Pad Settings" ) );
227 
229  frame.Refresh();
230 
231  return 0;
232 }
233 
234 
236 {
237  auto& selTool = *m_toolMgr->GetTool<SELECTION_TOOL>();
238  const auto& selection = selTool.GetSelection();
239 
240  auto& frame = *getEditFrame<PCB_EDIT_FRAME>();
241 
242  D_PAD& masterPad = frame.GetDesignSettings().m_Pad_Master;
243 
244  // can only copy from a single pad
245  if( selection.Size() == 1 )
246  {
247  auto item = selection[0];
248 
249  if( item->Type() == PCB_PAD_T )
250  {
251  const auto& selPad = static_cast<const D_PAD&>( *item );
252  masterPad.ImportSettingsFromMaster( selPad );
253  m_padCopied = true;
254  }
255  }
256 
257  return 0;
258 }
259 
260 
261 static void globalChangePadSettings( BOARD& board,
262  const D_PAD& aSrcPad,
263  BOARD_COMMIT& commit,
264  bool aSameFootprints,
265  bool aPadShapeFilter,
266  bool aPadOrientFilter,
267  bool aPadLayerFilter )
268 {
269  const MODULE* moduleRef = aSrcPad.GetParent();
270 
271  // If there is no module, we can't make the comparisons to see which
272  // pads to apply the source pad settings to
273  if( moduleRef == nullptr )
274  {
275  wxLogDebug( "globalChangePadSettings() Error: NULL module" );
276  return;
277  }
278 
279  double pad_orient = aSrcPad.GetOrientation() - moduleRef->GetOrientation();
280 
281  for( const MODULE* module = board.m_Modules; module; module = module->Next() )
282  {
283  if( !aSameFootprints && ( module != moduleRef ) )
284  continue;
285 
286  if( module->GetFPID() != moduleRef->GetFPID() )
287  continue;
288 
289  for( D_PAD* pad = module->PadsList(); pad; pad = pad->Next() )
290  {
291  // Filters changes prohibited.
292  if( aPadShapeFilter && ( pad->GetShape() != aSrcPad.GetShape() ) )
293  continue;
294 
295  double currpad_orient = pad->GetOrientation() - module->GetOrientation();
296 
297  if( aPadOrientFilter && ( currpad_orient != pad_orient ) )
298  continue;
299 
300  if( aPadLayerFilter && ( pad->GetLayerSet() != aSrcPad.GetLayerSet() ) )
301  continue;
302 
303  if( aPadLayerFilter && ( pad->GetLayerSet() != aSrcPad.GetLayerSet() ) )
304  continue;
305 
306  commit.Modify( pad );
307 
308  // Apply source pad settings to this pad
309  pad->ImportSettingsFromMaster( aSrcPad );
310  }
311  }
312 }
313 
314 
316 {
317  auto& selTool = *m_toolMgr->GetTool<SELECTION_TOOL>();
318  const auto& selection = selTool.GetSelection();
319 
320  auto& frame = *getEditFrame<PCB_EDIT_FRAME>();
321 
322  // not const - can be changed in the dialog
323  D_PAD* srcPad = nullptr;
324 
325  // If nothing selected, use the master pad setting,
326  // otherwise, if one pad selected, use the selected pad
327  if( selection.Size() == 0 )
328  {
330  }
331  else if( selection.Size() == 1 )
332  {
333  if( selection[0]->Type() == PCB_PAD_T )
334  {
335  srcPad = static_cast<D_PAD*>( selection[0] );
336  }
337  }
338  else
339  {
340  // multiple selected what to do?
341  // maybe master->selection? same as apply multiple?
342  }
343 
344  // no valid selection, nothing to do
345  if( !srcPad )
346  {
347  return 0;
348  }
349 
350  MODULE* module = srcPad->GetParent();
351 
352  if( module != nullptr )
353  {
354  frame.SetMsgPanel( module );
355  }
356 
357  int dialogRet;
358  {
359  DIALOG_GLOBAL_PADS_EDITION dlg( &frame, srcPad );
360  dialogRet = dlg.ShowModal();
361  }
362 
363  // cancel
364  if( dialogRet == -1 )
365  {
366  return 0;
367  }
368 
369  const bool edit_Same_Modules = (dialogRet == 1);
370 
371  BOARD_COMMIT commit( &frame );
372 
373  globalChangePadSettings( *getModel<BOARD>(), *srcPad, commit,
374  edit_Same_Modules,
378 
379  commit.Push( _( "Apply Pad Settings" ) );
380 
382  frame.Refresh();
383 
384  return 0;
385 }
386 
387 
389 {
393 }
void update() override
Update menu state stub.
Definition: pad_tool.cpp:131
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:261
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:298
MODULE * Next() const
Definition: class_module.h:120
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:109
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:158
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:108
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:326
const LIB_ID & GetFPID() const
Definition: class_module.h:184
bool m_editingFootprint
Definition: pad_tool.cpp:143
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:315
static TOOL_ACTION copyPadSettings
Copy the selected pad's settings to the board design settings.
Definition: pcb_actions.h:320
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:784
double GetOrientation() const
Definition: class_module.h:180
std::function< bool()> SHOW_FUNCTOR
Definition: pad_tool.cpp:69
PCB_EDIT_FRAME * frame() const
Definition: pcb_tool.h:133
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:395
static TOOL_ACTION applyPadSettings
Copy the pad settings in the board design settings to the selected pad.
Definition: pcb_actions.h:323
SHOW_FUNCTOR m_haveGlobalPadSettings
Definition: pad_tool.cpp:144
D_PAD * Next() const
Definition: class_pad.h:160
SELECTION & GetSelection()
Function GetSelection()
MODULE * module() const
Definition: pcb_tool.h:135
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:1336
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:96
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:95
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:375
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:164
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:171
int applyPadSettings(const TOOL_EVENT &aEvent)
Apply pad settings from board design settings to a pad
Definition: pad_tool.cpp:203
void setTransitions() override
Bind handlers to corresponding TOOL_ACTIONs
Definition: pad_tool.cpp:388
int copyPadSettings(const TOOL_EVENT &aEvent)
Copy pad settings from a pad to the board design settings
Definition: pad_tool.cpp:235
BOARD * board() const
Definition: pcb_tool.h:134