You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

544 lines
17 KiB

  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. #include "pad_tool.h"
  24. #include <pcb_base_edit_frame.h>
  25. #include <class_draw_panel_gal.h>
  26. #include <view/view_controls.h>
  27. #include <view/view.h>
  28. #include <tool/tool_manager.h>
  29. #include <bitmaps.h>
  30. #include <class_board_item.h>
  31. #include <class_module.h>
  32. #include <board_commit.h>
  33. #include <dialogs/dialog_push_pad_properties.h>
  34. #include <tools/pcb_actions.h>
  35. #include <tools/pcbnew_selection.h>
  36. #include <tools/selection_tool.h>
  37. #include <tools/pcb_selection_conditions.h>
  38. #include <tools/edit_tool.h>
  39. #include <dialogs/dialog_enum_pads.h>
  40. #include "pcbnew_id.h"
  41. // Pad tools
  42. TOOL_ACTION PCB_ACTIONS::copyPadSettings( "pcbnew.PadTool.CopyPadSettings",
  43. AS_GLOBAL, 0, "",
  44. _( "Copy Pad Properties" ), _( "Copy current pad's properties" ),
  45. copy_pad_settings_xpm );
  46. TOOL_ACTION PCB_ACTIONS::applyPadSettings( "pcbnew.PadTool.ApplyPadSettings",
  47. AS_GLOBAL, 0, "",
  48. _( "Paste Pad Properties" ), _( "Replace the current pad's properties with those copied earlier" ),
  49. apply_pad_settings_xpm );
  50. TOOL_ACTION PCB_ACTIONS::pushPadSettings( "pcbnew.PadTool.PushPadSettings",
  51. AS_GLOBAL, 0, "",
  52. _( "Push Pad Properties..." ), _( "Copy the current pad's properties to other pads" ),
  53. push_pad_settings_xpm );
  54. TOOL_ACTION PCB_ACTIONS::enumeratePads( "pcbnew.PadTool.enumeratePads",
  55. AS_GLOBAL, 0, "",
  56. _( "Renumber Pads..." ), _( "Renumber pads by clicking on them in the desired order" ),
  57. pad_enumerate_xpm, AF_ACTIVATE );
  58. class PAD_CONTEXT_MENU : public ACTION_MENU
  59. {
  60. public:
  61. using SHOW_FUNCTOR = std::function<bool()>;
  62. PAD_CONTEXT_MENU( bool aEditingFootprint, SHOW_FUNCTOR aHaveGlobalPadSetting ) :
  63. ACTION_MENU( true ),
  64. m_editingFootprint( aEditingFootprint ),
  65. m_haveGlobalPadSettings( std::move( aHaveGlobalPadSetting ) )
  66. {
  67. SetIcon( pad_xpm );
  68. SetTitle( _( "Pads" ) );
  69. Add( PCB_ACTIONS::copyPadSettings );
  70. Add( PCB_ACTIONS::applyPadSettings );
  71. Add( PCB_ACTIONS::pushPadSettings );
  72. // show modedit-specific items
  73. if( m_editingFootprint )
  74. {
  75. AppendSeparator();
  76. Add( PCB_ACTIONS::enumeratePads );
  77. }
  78. }
  79. protected:
  80. ACTION_MENU* create() const override
  81. {
  82. return new PAD_CONTEXT_MENU( m_editingFootprint, m_haveGlobalPadSettings );
  83. }
  84. private:
  85. struct ENABLEMENTS
  86. {
  87. bool canImport;
  88. bool canExport;
  89. bool canPush;
  90. };
  91. ENABLEMENTS getEnablements( const SELECTION& aSelection )
  92. {
  93. using S_C = SELECTION_CONDITIONS;
  94. ENABLEMENTS enablements;
  95. auto anyPadSel = S_C::HasType( PCB_PAD_T );
  96. auto singlePadSel = S_C::Count( 1 ) && S_C::OnlyType( PCB_PAD_T );
  97. // Apply pads enabled when any pads selected (it applies to each one
  98. // individually), plus need a valid global pad setting
  99. enablements.canImport = m_haveGlobalPadSettings() && ( anyPadSel )( aSelection );
  100. // Copy pads item enabled only when there is a single pad selected
  101. // (otherwise how would we know which one to copy?)
  102. enablements.canExport = ( singlePadSel )( aSelection );
  103. // Push pads available when there is a single pad to push from
  104. enablements.canPush = ( singlePadSel )( aSelection );
  105. return enablements;
  106. }
  107. void update() override
  108. {
  109. auto selTool = getToolManager()->GetTool<SELECTION_TOOL>();
  110. const PCBNEW_SELECTION& selection = selTool->GetSelection();
  111. auto enablements = getEnablements( selection );
  112. Enable( getMenuId( PCB_ACTIONS::applyPadSettings ), enablements.canImport );
  113. Enable( getMenuId( PCB_ACTIONS::copyPadSettings ), enablements.canExport );
  114. Enable( getMenuId( PCB_ACTIONS::pushPadSettings ), enablements.canPush );
  115. }
  116. bool m_editingFootprint;
  117. SHOW_FUNCTOR m_haveGlobalPadSettings;
  118. };
  119. PAD_TOOL::PAD_TOOL() :
  120. PCB_TOOL_BASE( "pcbnew.PadTool" ),
  121. m_padCopied( false )
  122. {}
  123. PAD_TOOL::~PAD_TOOL()
  124. {}
  125. void PAD_TOOL::Reset( RESET_REASON aReason )
  126. {
  127. m_padCopied = false;
  128. }
  129. bool PAD_TOOL::haveFootprints()
  130. {
  131. auto& board = *getModel<BOARD>();
  132. return board.Modules().size() > 0;
  133. }
  134. bool PAD_TOOL::Init()
  135. {
  136. auto contextMenu = std::make_shared<PAD_CONTEXT_MENU>( EditingModules(),
  137. [this]() { return m_padCopied; } );
  138. contextMenu->SetTool( this );
  139. SELECTION_TOOL* selTool = m_toolMgr->GetTool<SELECTION_TOOL>();
  140. if( selTool )
  141. {
  142. auto& toolMenu = selTool->GetToolMenu();
  143. auto& menu = toolMenu.GetMenu();
  144. toolMenu.AddSubMenu( contextMenu );
  145. auto canShowMenuCond = [this, contextMenu] ( const SELECTION& aSel ) {
  146. contextMenu->UpdateAll();
  147. return frame()->GetToolId() == ID_NO_TOOL_SELECTED
  148. && haveFootprints()
  149. && contextMenu->HasEnabledItems();
  150. };
  151. // show menu when there is a footprint, and the menu has any items
  152. auto showCond = canShowMenuCond &&
  153. ( SELECTION_CONDITIONS::HasType( PCB_PAD_T )
  154. || SELECTION_CONDITIONS::Count( 0 ) );
  155. menu.AddMenu( contextMenu.get(), showCond, 1000 );
  156. // we need a separator only when the selection is empty
  157. auto separatorCond = canShowMenuCond && SELECTION_CONDITIONS::Count( 0 );
  158. menu.AddSeparator( 1000 );
  159. }
  160. return true;
  161. }
  162. int PAD_TOOL::pastePadProperties( const TOOL_EVENT& aEvent )
  163. {
  164. auto& selTool = *m_toolMgr->GetTool<SELECTION_TOOL>();
  165. const auto& selection = selTool.GetSelection();
  166. const D_PAD& masterPad = frame()->GetDesignSettings().m_Pad_Master;
  167. BOARD_COMMIT commit( frame() );
  168. // for every selected pad, paste global settings
  169. for( auto item : selection )
  170. {
  171. if( item->Type() == PCB_PAD_T )
  172. {
  173. commit.Modify( item );
  174. static_cast<D_PAD&>( *item ).ImportSettingsFrom( masterPad );
  175. }
  176. }
  177. commit.Push( _( "Paste Pad Properties" ) );
  178. m_toolMgr->ProcessEvent( EVENTS::SelectedItemsModified );
  179. frame()->Refresh();
  180. return 0;
  181. }
  182. int PAD_TOOL::copyPadSettings( const TOOL_EVENT& aEvent )
  183. {
  184. auto& selTool = *m_toolMgr->GetTool<SELECTION_TOOL>();
  185. const auto& selection = selTool.GetSelection();
  186. D_PAD& masterPad = frame()->GetDesignSettings().m_Pad_Master;
  187. // can only copy from a single pad
  188. if( selection.Size() == 1 )
  189. {
  190. auto item = selection[0];
  191. if( item->Type() == PCB_PAD_T )
  192. {
  193. const auto& selPad = static_cast<const D_PAD&>( *item );
  194. masterPad.ImportSettingsFrom( selPad );
  195. m_padCopied = true;
  196. }
  197. }
  198. return 0;
  199. }
  200. static void doPushPadProperties( BOARD& board, const D_PAD& aSrcPad, BOARD_COMMIT& commit,
  201. bool aSameFootprints,
  202. bool aPadShapeFilter,
  203. bool aPadOrientFilter,
  204. bool aPadLayerFilter )
  205. {
  206. const MODULE* moduleRef = aSrcPad.GetParent();
  207. double pad_orient = aSrcPad.GetOrientation() - moduleRef->GetOrientation();
  208. for( auto module : board.Modules() )
  209. {
  210. if( !aSameFootprints && ( module != moduleRef ) )
  211. continue;
  212. if( module->GetFPID() != moduleRef->GetFPID() )
  213. continue;
  214. for( auto pad : module->Pads() )
  215. {
  216. if( aPadShapeFilter && ( pad->GetShape() != aSrcPad.GetShape() ) )
  217. continue;
  218. double currpad_orient = pad->GetOrientation() - module->GetOrientation();
  219. if( aPadOrientFilter && ( currpad_orient != pad_orient ) )
  220. continue;
  221. if( aPadLayerFilter && ( pad->GetLayerSet() != aSrcPad.GetLayerSet() ) )
  222. continue;
  223. if( aPadLayerFilter && ( pad->GetLayerSet() != aSrcPad.GetLayerSet() ) )
  224. continue;
  225. commit.Modify( pad );
  226. // Apply source pad settings to this pad
  227. pad->ImportSettingsFrom( aSrcPad );
  228. }
  229. }
  230. }
  231. int PAD_TOOL::pushPadSettings( const TOOL_EVENT& aEvent )
  232. {
  233. auto& selTool = *m_toolMgr->GetTool<SELECTION_TOOL>();
  234. const auto& selection = selTool.GetSelection();
  235. D_PAD* srcPad;
  236. if( selection.Size() == 1 && selection[0]->Type() == PCB_PAD_T )
  237. srcPad = static_cast<D_PAD*>( selection[0] );
  238. else
  239. return 0;
  240. MODULE* module = srcPad->GetParent();
  241. if( !module )
  242. return 0;
  243. frame()->SetMsgPanel( module );
  244. DIALOG_PUSH_PAD_PROPERTIES dlg( frame() );
  245. int dialogRet = dlg.ShowModal();
  246. if( dialogRet == wxID_CANCEL )
  247. return 0;
  248. const bool edit_Same_Modules = (dialogRet == 1);
  249. BOARD_COMMIT commit( frame() );
  250. doPushPadProperties( *getModel<BOARD>(), *srcPad, commit, edit_Same_Modules,
  251. DIALOG_PUSH_PAD_PROPERTIES::m_Pad_Shape_Filter,
  252. DIALOG_PUSH_PAD_PROPERTIES::m_Pad_Orient_Filter,
  253. DIALOG_PUSH_PAD_PROPERTIES::m_Pad_Layer_Filter );
  254. commit.Push( _( "Push Pad Settings" ) );
  255. m_toolMgr->ProcessEvent( EVENTS::SelectedItemsModified );
  256. frame()->Refresh();
  257. return 0;
  258. }
  259. int PAD_TOOL::EnumeratePads( const TOOL_EVENT& aEvent )
  260. {
  261. if( !board()->GetFirstModule() || !board()->GetFirstModule()->Pads().empty() )
  262. return 0;
  263. DIALOG_ENUM_PADS settingsDlg( frame() );
  264. if( settingsDlg.ShowModal() != wxID_OK )
  265. return 0;
  266. Activate();
  267. GENERAL_COLLECTOR collector;
  268. const KICAD_T types[] = { PCB_PAD_T, EOT };
  269. GENERAL_COLLECTORS_GUIDE guide = frame()->GetCollectorsGuide();
  270. guide.SetIgnoreMTextsMarkedNoShow( true );
  271. guide.SetIgnoreMTextsOnBack( true );
  272. guide.SetIgnoreMTextsOnFront( true );
  273. guide.SetIgnoreModulesVals( true );
  274. guide.SetIgnoreModulesRefs( true );
  275. int seqPadNum = settingsDlg.GetStartNumber();
  276. wxString padPrefix = settingsDlg.GetPrefix();
  277. std::deque<int> storedPadNumbers;
  278. frame()->SetToolID( ID_MODEDIT_PAD_TOOL, wxCURSOR_HAND,
  279. _( "Click on successive pads to renumber them" ) );
  280. m_toolMgr->RunAction( PCB_ACTIONS::selectionClear, true );
  281. getViewControls()->ShowCursor( true );
  282. KIGFX::VIEW* view = m_toolMgr->GetView();
  283. VECTOR2I oldCursorPos; // store the previous mouse cursor position, during mouse drag
  284. std::list<D_PAD*> selectedPads;
  285. BOARD_COMMIT commit( frame() );
  286. std::map<wxString, std::pair<int, wxString>> oldNames;
  287. bool isFirstPoint = true; // used to be sure oldCursorPos will be initialized at least once.
  288. STATUS_TEXT_POPUP statusPopup( frame() );
  289. statusPopup.SetText( wxString::Format(
  290. _( "Click on pad %s%d\nPress Escape to cancel or double-click to commit" ),
  291. padPrefix.c_str(), seqPadNum ) );
  292. statusPopup.Popup();
  293. statusPopup.Move( wxGetMousePosition() + wxPoint( 20, 20 ) );
  294. while( OPT_TOOL_EVENT evt = Wait() )
  295. {
  296. if( evt->IsDrag( BUT_LEFT ) || evt->IsClick( BUT_LEFT ) )
  297. {
  298. selectedPads.clear();
  299. VECTOR2I cursorPos = getViewControls()->GetCursorPosition();
  300. // Be sure the old cursor mouse position was initialized:
  301. if( isFirstPoint )
  302. {
  303. oldCursorPos = cursorPos;
  304. isFirstPoint = false;
  305. }
  306. // wxWidgets deliver mouse move events not frequently enough, resulting in skipping
  307. // pads if the user moves cursor too fast. To solve it, create a line that approximates
  308. // the mouse move and search pads that are on the line.
  309. int distance = ( cursorPos - oldCursorPos ).EuclideanNorm();
  310. // Search will be made every 0.1 mm:
  311. int segments = distance / int( 0.1*IU_PER_MM ) + 1;
  312. const wxPoint line_step( ( cursorPos - oldCursorPos ) / segments );
  313. collector.Empty();
  314. for( int j = 0; j < segments; ++j )
  315. {
  316. wxPoint testpoint( cursorPos.x - j * line_step.x,
  317. cursorPos.y - j * line_step.y );
  318. collector.Collect( board(), types, testpoint, guide );
  319. for( int i = 0; i < collector.GetCount(); ++i )
  320. {
  321. selectedPads.push_back( static_cast<D_PAD*>( collector[i] ) );
  322. }
  323. }
  324. selectedPads.unique();
  325. for( D_PAD* pad : selectedPads )
  326. {
  327. // If pad was not selected, then enumerate it
  328. if( !pad->IsSelected() )
  329. {
  330. commit.Modify( pad );
  331. // Rename pad and store the old name
  332. int newval;
  333. if( storedPadNumbers.size() > 0 )
  334. {
  335. newval = storedPadNumbers.front();
  336. storedPadNumbers.pop_front();
  337. }
  338. else
  339. newval = seqPadNum++;
  340. wxString newName = wxString::Format( wxT( "%s%d" ), padPrefix.c_str(), newval );
  341. oldNames[newName] = { newval, pad->GetName() };
  342. pad->SetName( newName );
  343. pad->SetSelected();
  344. getView()->Update( pad );
  345. // Ensure the popup text shows the correct next value
  346. if( storedPadNumbers.size() > 0 )
  347. newval = storedPadNumbers.front();
  348. else
  349. newval = seqPadNum;
  350. statusPopup.SetText( wxString::Format(
  351. _( "Click on pad %s%d\nPress Escape to cancel or double-click to commit" ),
  352. padPrefix.c_str(), newval ) );
  353. }
  354. // ..or restore the old name if it was enumerated and clicked again
  355. else if( pad->IsSelected() && evt->IsClick( BUT_LEFT ) )
  356. {
  357. auto it = oldNames.find( pad->GetName() );
  358. wxASSERT( it != oldNames.end() );
  359. if( it != oldNames.end() )
  360. {
  361. storedPadNumbers.push_back( it->second.first );
  362. pad->SetName( it->second.second );
  363. oldNames.erase( it );
  364. statusPopup.SetText( wxString::Format(
  365. _( "Click on pad %s%d\nPress Escape to cancel or double-click to commit" ),
  366. padPrefix.c_str(), storedPadNumbers.front() ) );
  367. }
  368. pad->ClearSelected();
  369. getView()->Update( pad );
  370. }
  371. }
  372. }
  373. else if( ( evt->IsKeyPressed() && evt->KeyCode() == WXK_RETURN ) ||
  374. evt->IsDblClick( BUT_LEFT ) )
  375. {
  376. commit.Push( _( "Renumber pads" ) );
  377. break;
  378. }
  379. // This is a cancel-current-action (ie: <esc>).
  380. // Note that this must go before IsCancelInteractive() as it also checks IsCancel().
  381. else if( evt->IsCancel() )
  382. {
  383. // Clear current selection list to avoid selection of deleted items
  384. m_toolMgr->RunAction( PCB_ACTIONS::selectionClear, true );
  385. commit.Revert();
  386. break;
  387. }
  388. // Now that cancel-current-action has been handled, check for cancel-tool.
  389. else if( TOOL_EVT_UTILS::IsCancelInteractive( *evt ) )
  390. {
  391. commit.Push( _( "Renumber pads" ) );
  392. break;
  393. }
  394. else if( evt->IsClick( BUT_RIGHT ) )
  395. {
  396. m_menu.ShowContextMenu( selection() );
  397. }
  398. // Prepare the next loop by updating the old cursor mouse position
  399. // to this last mouse cursor position
  400. oldCursorPos = getViewControls()->GetCursorPosition();
  401. statusPopup.Move( wxGetMousePosition() + wxPoint( 20, 20 ) );
  402. }
  403. for( auto p : board()->GetFirstModule()->Pads() )
  404. {
  405. p->ClearSelected();
  406. view->Update( p );
  407. }
  408. statusPopup.Hide();
  409. frame()->SetNoToolSelected();
  410. frame()->GetCanvas()->SetCursor( wxCURSOR_ARROW );
  411. return 0;
  412. }
  413. void PAD_TOOL::setTransitions()
  414. {
  415. Go( &PAD_TOOL::pastePadProperties, PCB_ACTIONS::applyPadSettings.MakeEvent() );
  416. Go( &PAD_TOOL::copyPadSettings, PCB_ACTIONS::copyPadSettings.MakeEvent() );
  417. Go( &PAD_TOOL::pushPadSettings, PCB_ACTIONS::pushPadSettings.MakeEvent() );
  418. Go( &PAD_TOOL::EnumeratePads, PCB_ACTIONS::enumeratePads.MakeEvent() );
  419. }