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.

173 lines
5.0 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2020 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 <tool/tool_manager.h>
  24. #include <tools/pcb_actions.h>
  25. #include <tools/pcb_picker_tool.h>
  26. #include <pcb_base_edit_frame.h>
  27. #include <pcb_group.h>
  28. #include <status_popup.h>
  29. #include <board_commit.h>
  30. #include <bitmaps.h>
  31. #include <dialogs/dialog_group_properties.h>
  32. DIALOG_GROUP_PROPERTIES::DIALOG_GROUP_PROPERTIES( PCB_BASE_EDIT_FRAME* aParent,
  33. PCB_GROUP* aGroup ) :
  34. DIALOG_GROUP_PROPERTIES_BASE( aParent ),
  35. m_brdEditor( aParent ),
  36. m_toolMgr( aParent->GetToolManager() ),
  37. m_group( aGroup )
  38. {
  39. m_bpAddMember->SetBitmap( KiBitmap( BITMAPS::small_plus ) );
  40. m_bpRemoveMember->SetBitmap( KiBitmap( BITMAPS::small_trash ) );
  41. m_nameCtrl->SetValue( m_group->GetName() );
  42. m_locked->SetValue( m_group->IsLocked() );
  43. m_locked->Show( dynamic_cast<PCB_EDIT_FRAME*>( aParent ) != nullptr );
  44. for( BOARD_ITEM* item : m_group->GetItems() )
  45. m_membersList->Append( item->GetSelectMenuText( m_brdEditor->GetUserUnits() ), item );
  46. m_sdbSizerOK->SetDefault();
  47. SetInitialFocus( m_nameCtrl );
  48. // Now all widgets have the size fixed, call FinishDialogSettings
  49. finishDialogSettings();
  50. }
  51. DIALOG_GROUP_PROPERTIES::~DIALOG_GROUP_PROPERTIES()
  52. {
  53. if( m_brdEditor->IsBeingDeleted() )
  54. return;
  55. m_brdEditor->FocusOnItem( nullptr );
  56. m_brdEditor->GetCanvas()->Refresh();
  57. }
  58. bool DIALOG_GROUP_PROPERTIES::TransferDataToWindow()
  59. {
  60. // Don't do anything here; it gets called every time we re-show the dialog after
  61. // picking a new member.
  62. return true;
  63. }
  64. bool DIALOG_GROUP_PROPERTIES::TransferDataFromWindow()
  65. {
  66. BOARD_COMMIT commit( m_brdEditor );
  67. commit.Modify( m_group );
  68. m_group->RunOnDescendants(
  69. [&]( BOARD_ITEM* descendant )
  70. {
  71. commit.Modify( descendant );
  72. } );
  73. for( size_t ii = 0; ii < m_membersList->GetCount(); ++ii )
  74. {
  75. BOARD_ITEM* item = static_cast<BOARD_ITEM*>( m_membersList->GetClientData( ii ) );
  76. PCB_GROUP* existingGroup = item->GetParentGroup();
  77. if( existingGroup != m_group )
  78. {
  79. commit.Modify( item );
  80. if( existingGroup )
  81. commit.Modify( existingGroup );
  82. }
  83. }
  84. m_group->SetName( m_nameCtrl->GetValue() );
  85. m_group->SetLocked( m_locked->GetValue() );
  86. m_toolMgr->RunAction( PCB_ACTIONS::selectionClear, true );
  87. m_group->RemoveAll();
  88. for( size_t ii = 0; ii < m_membersList->GetCount(); ++ii )
  89. {
  90. BOARD_ITEM* item = static_cast<BOARD_ITEM*>( m_membersList->GetClientData( ii ) );
  91. m_group->AddItem( item );
  92. }
  93. m_toolMgr->RunAction( PCB_ACTIONS::selectItem, true, m_group );
  94. commit.Push( _( "Modified group" ) );
  95. return true;
  96. }
  97. void DIALOG_GROUP_PROPERTIES::OnMemberSelected( wxCommandEvent& aEvent )
  98. {
  99. int selected = m_membersList->GetSelection();
  100. if( selected >= 0 )
  101. {
  102. WINDOW_THAWER thawer( m_brdEditor );
  103. BOARD_ITEM* item = static_cast<BOARD_ITEM*>( m_membersList->GetClientData( selected ) );
  104. m_brdEditor->FocusOnItem( item );
  105. m_brdEditor->GetCanvas()->Refresh();
  106. }
  107. aEvent.Skip();
  108. }
  109. void DIALOG_GROUP_PROPERTIES::OnAddMember( wxCommandEvent& event )
  110. {
  111. m_toolMgr->RunAction( PCB_ACTIONS::pickNewGroupMember, true );
  112. }
  113. void DIALOG_GROUP_PROPERTIES::DoAddMember( EDA_ITEM* aItem )
  114. {
  115. for( size_t ii = 0; ii < m_membersList->GetCount(); ++ii )
  116. {
  117. if( aItem == static_cast<BOARD_ITEM*>( m_membersList->GetClientData( ii ) ) )
  118. return;
  119. }
  120. if( aItem == m_group )
  121. return;
  122. m_membersList->Append( aItem->GetSelectMenuText( m_brdEditor->GetUserUnits() ), aItem );
  123. }
  124. void DIALOG_GROUP_PROPERTIES::OnRemoveMember( wxCommandEvent& event )
  125. {
  126. int selected = m_membersList->GetSelection();
  127. if( selected >= 0 )
  128. m_membersList->Delete( selected );
  129. m_brdEditor->FocusOnItem( nullptr );
  130. m_brdEditor->GetCanvas()->Refresh();
  131. }