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.

174 lines
5.8 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2009 Wayne Stambaugh <stambaughw@gmail.com>
  5. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include "dialog_lib_new_symbol.h"
  25. #include <default_values.h>
  26. #include <eda_draw_frame.h>
  27. #include <sch_validators.h>
  28. #include <template_fieldnames.h>
  29. static wxString getDerivativeName( const wxString& aParentName )
  30. {
  31. return wxString::Format( "%s_1", aParentName );
  32. }
  33. DIALOG_LIB_NEW_SYMBOL::DIALOG_LIB_NEW_SYMBOL( EDA_DRAW_FRAME* aParent,
  34. const wxArrayString& aSymbolNames,
  35. const wxString& aInheritFromSymbolName,
  36. std::function<bool( wxString newName )> aValidator ) :
  37. DIALOG_LIB_NEW_SYMBOL_BASE( dynamic_cast<wxWindow*>( aParent ) ),
  38. m_pinTextPosition( aParent, m_staticPinTextPositionLabel, m_textPinTextPosition,
  39. m_staticPinTextPositionUnits, true ),
  40. m_validator( std::move( aValidator ) ),
  41. m_nameIsDefaulted( true )
  42. {
  43. if( aSymbolNames.GetCount() )
  44. {
  45. wxArrayString escapedNames;
  46. for( const wxString& name : aSymbolNames )
  47. escapedNames.Add( UnescapeString( name ) );
  48. m_comboInheritanceSelect->SetSymbolList( escapedNames );
  49. if( !aInheritFromSymbolName.IsEmpty() )
  50. {
  51. m_comboInheritanceSelect->SetSelectedSymbol( aInheritFromSymbolName );
  52. }
  53. }
  54. // Trigger the event handler to show/hide the info bar message.
  55. wxCommandEvent dummyEvent;
  56. onParentSymbolSelect( dummyEvent );
  57. m_textName->SetValidator( FIELD_VALIDATOR( VALUE_FIELD ) );
  58. m_textReference->SetValidator( FIELD_VALIDATOR( REFERENCE_FIELD ) );
  59. if( !aInheritFromSymbolName.IsEmpty() )
  60. {
  61. m_textName->ChangeValue( getDerivativeName( aInheritFromSymbolName ) );
  62. m_nameIsDefaulted = true;
  63. }
  64. m_pinTextPosition.SetValue( schIUScale.MilsToIU( DEFAULT_PIN_NAME_OFFSET ) );
  65. m_comboInheritanceSelect->Connect(
  66. FILTERED_ITEM_SELECTED,
  67. wxCommandEventHandler( DIALOG_LIB_NEW_SYMBOL::onParentSymbolSelect ), nullptr, this );
  68. m_textName->Bind( wxEVT_TEXT,
  69. [this]( wxCommandEvent& aEvent )
  70. {
  71. m_nameIsDefaulted = false;
  72. } );
  73. // initial focus should be on first editable field.
  74. m_textName->SetFocus();
  75. SetupStandardButtons();
  76. // Now all widgets have the size fixed, call FinishDialogSettings
  77. finishDialogSettings();
  78. }
  79. DIALOG_LIB_NEW_SYMBOL::~DIALOG_LIB_NEW_SYMBOL()
  80. {
  81. m_comboInheritanceSelect->Disconnect(
  82. FILTERED_ITEM_SELECTED,
  83. wxCommandEventHandler( DIALOG_LIB_NEW_SYMBOL::onParentSymbolSelect ), nullptr, this );
  84. }
  85. bool DIALOG_LIB_NEW_SYMBOL::TransferDataFromWindow()
  86. {
  87. return m_validator( GetName() );
  88. }
  89. void DIALOG_LIB_NEW_SYMBOL::onParentSymbolSelect( wxCommandEvent& aEvent )
  90. {
  91. const wxString parent = m_comboInheritanceSelect->GetValue();
  92. if( !parent.IsEmpty() )
  93. {
  94. m_infoBar->RemoveAllButtons();
  95. m_infoBar->ShowMessage( wxString::Format( _( "Deriving from symbol '%s'." ), parent ),
  96. wxICON_INFORMATION );
  97. }
  98. else
  99. {
  100. m_infoBar->Dismiss();
  101. }
  102. if( m_textName->IsEmpty() || m_nameIsDefaulted )
  103. {
  104. m_textName->SetValue( getDerivativeName( parent ) );
  105. m_textName->SetInsertionPointEnd();
  106. m_nameIsDefaulted = true;
  107. }
  108. syncControls( !parent.IsEmpty() );
  109. }
  110. void DIALOG_LIB_NEW_SYMBOL::syncControls( bool aIsDerivedPart )
  111. {
  112. m_staticTextDes->Enable( !aIsDerivedPart );
  113. m_textReference->Enable( !aIsDerivedPart );
  114. m_staticTextUnits->Enable( !aIsDerivedPart );
  115. m_spinPartCount->Enable( !aIsDerivedPart );
  116. m_checkUnitsInterchangeable->Enable( !aIsDerivedPart );
  117. m_checkHasAlternateBodyStyle->Enable( !aIsDerivedPart );
  118. m_checkIsPowerSymbol->Enable( !aIsDerivedPart );
  119. m_excludeFromBomCheckBox->Enable( !aIsDerivedPart );
  120. m_excludeFromBoardCheckBox->Enable( !aIsDerivedPart );
  121. m_staticPinTextPositionLabel->Enable( !aIsDerivedPart );
  122. m_textPinTextPosition->Enable( !aIsDerivedPart );
  123. m_staticPinTextPositionUnits->Enable( !aIsDerivedPart );
  124. m_checkShowPinNumber->Enable( !aIsDerivedPart );
  125. m_checkShowPinName->Enable( !aIsDerivedPart );
  126. m_checkShowPinNameInside->Enable( !aIsDerivedPart );
  127. }
  128. void DIALOG_LIB_NEW_SYMBOL::onPowerCheckBox( wxCommandEvent& aEvent )
  129. {
  130. if( m_checkIsPowerSymbol->IsChecked() )
  131. {
  132. m_excludeFromBomCheckBox->SetValue( true );
  133. m_excludeFromBoardCheckBox->SetValue( true );
  134. m_excludeFromBomCheckBox->Enable( false );
  135. m_excludeFromBoardCheckBox->Enable( false );
  136. }
  137. else
  138. {
  139. m_excludeFromBomCheckBox->Enable( true );
  140. m_excludeFromBoardCheckBox->Enable( true );
  141. }
  142. }