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.

275 lines
8.5 KiB

  1. /**
  2. * @file dialog_edit_one_field.cpp
  3. * @brief dialog to editing a field ( not a graphic text) in current component.
  4. */
  5. /*
  6. * This program source code file is part of KiCad, a free EDA CAD application.
  7. *
  8. * Copyright (C) 2012 Jean-Pierre Charras, jean-pierre.charras@gipsa-lab.inpg.com
  9. * Copyright (C) 2016 Wayne Stambaugh, stambaughw@gmail.com
  10. * Copyright (C) 2004-2016 KiCad Developers, see change_log.txt for contributors.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version 2
  15. * of the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, you may find one here:
  24. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  25. * or you may search the http://www.gnu.org website for the version 2 license,
  26. * or you may write to the Free Software Foundation, Inc.,
  27. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  28. */
  29. #include <fctsys.h>
  30. #include <common.h>
  31. #include <base_units.h>
  32. #include <kiway.h>
  33. #include <confirm.h>
  34. #include <general.h>
  35. #include <sch_base_frame.h>
  36. #include <sch_component.h>
  37. #include <class_libentry.h>
  38. #include <lib_field.h>
  39. #include <sch_component.h>
  40. #include <template_fieldnames.h>
  41. #include <class_library.h>
  42. #include <sch_validators.h>
  43. #include <dialog_edit_one_field.h>
  44. // These should probably moved into some other file as helpers.
  45. EDA_TEXT_HJUSTIFY_T IntToEdaTextHorizJustify( int aHorizJustify )
  46. {
  47. wxASSERT( aHorizJustify >= GR_TEXT_HJUSTIFY_LEFT && aHorizJustify <= GR_TEXT_HJUSTIFY_RIGHT );
  48. if( aHorizJustify > GR_TEXT_HJUSTIFY_RIGHT )
  49. return GR_TEXT_HJUSTIFY_RIGHT;
  50. if( aHorizJustify < GR_TEXT_HJUSTIFY_LEFT )
  51. return GR_TEXT_HJUSTIFY_LEFT;
  52. return (EDA_TEXT_HJUSTIFY_T) aHorizJustify;
  53. }
  54. EDA_TEXT_VJUSTIFY_T IntToEdaTextVertJustify( int aVertJustify )
  55. {
  56. wxASSERT( aVertJustify >= GR_TEXT_VJUSTIFY_TOP && aVertJustify <= GR_TEXT_VJUSTIFY_BOTTOM );
  57. if( aVertJustify > GR_TEXT_VJUSTIFY_BOTTOM )
  58. return GR_TEXT_VJUSTIFY_BOTTOM;
  59. if( aVertJustify < GR_TEXT_VJUSTIFY_TOP )
  60. return GR_TEXT_VJUSTIFY_TOP;
  61. return (EDA_TEXT_VJUSTIFY_T) aVertJustify;
  62. }
  63. DIALOG_EDIT_ONE_FIELD::DIALOG_EDIT_ONE_FIELD( SCH_BASE_FRAME* aParent, const wxString& aTitle,
  64. const EDA_TEXT* aTextItem ) :
  65. DIALOG_LIB_EDIT_TEXT_BASE( aParent )
  66. {
  67. SetTitle( aTitle );
  68. // The field ID and power status are Initialized in the derived object's ctor.
  69. m_fieldId = VALUE;
  70. m_isPower = false;
  71. m_text = aTextItem->GetText();
  72. m_style = aTextItem->IsItalic() ? 1 : 0;
  73. m_style += aTextItem->IsBold() ? 2 : 0;
  74. m_size = aTextItem->GetSize().x;
  75. m_orientation = ( aTextItem->GetOrientation() == TEXT_ORIENT_VERT );
  76. m_verticalJustification = aTextItem->GetVertJustify() + 1;
  77. m_horizontalJustification = aTextItem->GetHorizJustify() + 1;
  78. m_isVisible = aTextItem->IsVisible();
  79. }
  80. void DIALOG_EDIT_ONE_FIELD::init()
  81. {
  82. wxString msg;
  83. m_TextValue->SetFocus();
  84. m_TextValue->SetValidator( SCH_FIELD_VALIDATOR( m_fieldId, &m_text ) );
  85. // Disable options for graphic text editing which are not needed for fields.
  86. m_CommonConvert->Show( false );
  87. m_CommonUnit->Show( false );
  88. // Show the footprint selection dialog if this is the footprint field.
  89. if( m_fieldId == FOOTPRINT )
  90. {
  91. m_TextValueSelectButton->Show();
  92. m_TextValueSelectButton->Enable();
  93. }
  94. else
  95. {
  96. m_TextValueSelectButton->Hide();
  97. m_TextValueSelectButton->Disable();
  98. }
  99. msg = m_TextSizeText->GetLabel() + ReturnUnitSymbol();
  100. m_TextSizeText->SetLabel( msg );
  101. // Value fields of power components cannot be modified. This will grey out
  102. // the text box and display an explanation.
  103. if( m_fieldId == VALUE && m_isPower )
  104. {
  105. m_PowerComponentValues->Show( true );
  106. m_TextValue->Enable( false );
  107. }
  108. else
  109. {
  110. m_PowerComponentValues->Show( false );
  111. m_TextValue->Enable( true );
  112. }
  113. m_sdbSizerButtonsOK->SetDefault();
  114. FixOSXCancelButtonIssue();
  115. // Now all widgets have the size fixed, call FinishDialogSettings
  116. FinishDialogSettings();
  117. }
  118. void DIALOG_EDIT_ONE_FIELD::OnTextValueSelectButtonClick( wxCommandEvent& aEvent )
  119. {
  120. // pick a footprint using the footprint picker.
  121. wxString fpid;
  122. KIWAY_PLAYER* frame = Kiway().Player( FRAME_PCB_MODULE_VIEWER_MODAL, true );
  123. if( frame->ShowModal( &fpid, this ) )
  124. {
  125. m_TextValue->SetValue( fpid );
  126. }
  127. frame->Destroy();
  128. }
  129. bool DIALOG_EDIT_ONE_FIELD::TransferDataToWindow()
  130. {
  131. wxLogDebug( "In DIALOG_EDIT_ONE_FIELD::TransferDataToWindow()" );
  132. m_TextValue->SetValue( m_text );
  133. m_Orient->SetValue( m_orientation );
  134. m_TextSize->SetValue( StringFromValue( g_UserUnit, m_size ) );
  135. m_TextHJustificationOpt->SetSelection( m_horizontalJustification );
  136. m_TextVJustificationOpt->SetSelection( m_verticalJustification );
  137. m_Invisible->SetValue( !m_isVisible );
  138. m_TextShapeOpt->SetSelection( m_style );
  139. return true;
  140. }
  141. bool DIALOG_EDIT_ONE_FIELD::TransferDataFromWindow()
  142. {
  143. wxLogDebug( "In DIALOG_EDIT_ONE_FIELD::TransferDataFromWindow()" );
  144. m_text = m_TextValue->GetValue();
  145. // There are lots of specific tests required to validate field text.
  146. if( m_fieldId == REFERENCE )
  147. {
  148. // Test if the reference string is valid:
  149. if( !SCH_COMPONENT::IsReferenceStringValid( m_text ) )
  150. {
  151. DisplayError( this, _( "Illegal reference field value!" ) );
  152. return false;
  153. }
  154. }
  155. m_orientation = m_Orient->GetValue();
  156. m_size = ValueFromString( g_UserUnit, m_TextSize->GetValue() );
  157. m_horizontalJustification = m_TextHJustificationOpt->GetSelection();
  158. m_verticalJustification = m_TextVJustificationOpt->GetSelection();
  159. m_isVisible = !m_Invisible->GetValue();
  160. m_style = m_TextShapeOpt->GetSelection();
  161. return true;
  162. }
  163. void DIALOG_EDIT_ONE_FIELD::updateText( EDA_TEXT* aText )
  164. {
  165. aText->SetSize( wxSize( m_size, m_size ) );
  166. aText->SetVisible( m_isVisible );
  167. aText->SetOrientation( m_orientation ? TEXT_ORIENT_VERT : TEXT_ORIENT_HORIZ );
  168. aText->SetItalic( (m_style & 1) != 0 );
  169. aText->SetBold( (m_style & 2) != 0 );
  170. aText->SetHorizJustify( IntToEdaTextHorizJustify( m_horizontalJustification - 1 ) );
  171. aText->SetVertJustify( IntToEdaTextVertJustify( m_verticalJustification - 1 ) );
  172. }
  173. DIALOG_LIB_EDIT_ONE_FIELD::DIALOG_LIB_EDIT_ONE_FIELD( SCH_BASE_FRAME* aParent,
  174. const wxString& aTitle,
  175. const LIB_FIELD* aField ) :
  176. DIALOG_EDIT_ONE_FIELD( aParent, aTitle, dynamic_cast< const EDA_TEXT* >( aField ) )
  177. {
  178. m_fieldId = aField->GetId();
  179. // When in the library editor, power components can be renamed.
  180. m_isPower = false;
  181. init();
  182. }
  183. DIALOG_SCH_EDIT_ONE_FIELD::DIALOG_SCH_EDIT_ONE_FIELD( SCH_BASE_FRAME* aParent,
  184. const wxString& aTitle,
  185. const SCH_FIELD* aField ) :
  186. DIALOG_EDIT_ONE_FIELD( aParent, aTitle, dynamic_cast< const EDA_TEXT* >( aField ) )
  187. {
  188. m_fieldId = aField->GetId();
  189. const SCH_COMPONENT* component = (SCH_COMPONENT*) aField->GetParent();
  190. wxASSERT_MSG( component != NULL && component->Type() == SCH_COMPONENT_T,
  191. wxT( "Invalid schematic field parent item." ) );
  192. const LIB_PART* part = GetParent()->Prj().SchLibs()->FindLibPart( component->GetPartName() );
  193. wxASSERT_MSG( part, wxT( "Library part for component <" ) +
  194. component->GetPartName() + wxT( "> could not be found." ) );
  195. m_isPower = part->IsPower();
  196. init();
  197. }
  198. void DIALOG_SCH_EDIT_ONE_FIELD::UpdateField( SCH_FIELD* aField, SCH_SHEET_PATH* aSheetPath )
  199. {
  200. wxASSERT( aField != NULL || aField->Type() != SCH_FIELD_T );
  201. if( aField->GetId() == REFERENCE )
  202. {
  203. wxASSERT( aSheetPath != NULL );
  204. SCH_COMPONENT* component = dynamic_cast< SCH_COMPONENT* >( aField->GetParent() );
  205. wxASSERT( component != NULL );
  206. if( component != NULL )
  207. component->SetRef( aSheetPath, m_text );
  208. }
  209. aField->SetText( m_text );
  210. updateText( aField );
  211. }