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.

198 lines
7.5 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2010 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2016 - 2018 KiCad Developers, see CHANGELOG.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 <fctsys.h>
  25. #include <macros.h>
  26. #include <gr_basic.h>
  27. #include <bitmaps.h>
  28. #include <sch_painter.h>
  29. #include <lib_edit_frame.h>
  30. #include <class_libentry.h>
  31. #include <lib_pin.h>
  32. #include <dialog_lib_edit_pin.h>
  33. #include <confirm.h>
  34. DIALOG_LIB_EDIT_PIN::DIALOG_LIB_EDIT_PIN( LIB_EDIT_FRAME* parent, LIB_PIN* aPin ) :
  35. DIALOG_LIB_EDIT_PIN_BASE( parent ),
  36. m_frame( parent ),
  37. m_pin( aPin ),
  38. m_posX( parent, m_posXLabel, m_posXCtrl, m_posXUnits, true ),
  39. m_posY( parent, m_posYLabel, m_posYCtrl, m_posYUnits, true ),
  40. m_pinLength( parent, m_pinLengthLabel, m_pinLengthCtrl, m_pinLengthUnits, true ),
  41. m_nameSize( parent, m_nameSizeLabel, m_nameSizeCtrl, m_nameSizeUnits, true ),
  42. m_numberSize( parent, m_numberSizeLabel, m_numberSizeCtrl, m_numberSizeUnits, true )
  43. {
  44. // Creates a dummy pin to show on a panel, inside this dialog:
  45. m_dummyPin = new LIB_PIN( *m_pin );
  46. // m_dummyPin changes do not propagate to other pins of the current lib component,
  47. // so set parent to null and clear flags
  48. m_dummyPin->SetParent( nullptr );
  49. m_dummyPin->ClearFlags();
  50. COLOR4D bgColor = parent->GetRenderSettings()->GetLayerColor( LAYER_SCHEMATIC_BACKGROUND );
  51. m_panelShowPin->SetBackgroundColour( bgColor.ToColour() );
  52. const wxArrayString& orientationNames = LIB_PIN::GetOrientationNames();
  53. const BITMAP_DEF* orientationBitmaps = LIB_PIN::GetOrientationSymbols();
  54. for ( unsigned ii = 0; ii < orientationNames.GetCount(); ii++ )
  55. m_choiceOrientation->Insert( orientationNames[ii], KiBitmap( orientationBitmaps[ii] ), ii );
  56. m_sdbSizerButtonsOK->SetDefault();
  57. SetInitialFocus( m_textPinName );
  58. // Now all widgets have the size fixed, call FinishDialogSettings
  59. FinishDialogSettings();
  60. // On some windows manager (Unity, XFCE), this dialog is
  61. // not always raised, depending on this dialog is run.
  62. // Force it to be raised
  63. Raise();
  64. }
  65. DIALOG_LIB_EDIT_PIN::~DIALOG_LIB_EDIT_PIN()
  66. {
  67. delete m_dummyPin;
  68. }
  69. bool DIALOG_LIB_EDIT_PIN::TransferDataToWindow()
  70. {
  71. if( !DIALOG_SHIM::TransferDataToWindow() )
  72. return false;
  73. m_choiceOrientation->SetSelection( LIB_PIN::GetOrientationIndex( m_pin->GetOrientation() ) );
  74. m_choiceStyle->SetSelection( m_pin->GetShape() );
  75. m_choiceElectricalType->SetSelection( m_pin->GetType() );
  76. m_textPinName->SetValue( m_pin->GetName() );
  77. m_nameSize.SetValue( m_pin->GetNameTextSize() );
  78. m_posX.SetValue( m_pin->GetPosition().x );
  79. m_posY.SetValue( -m_pin->GetPosition().y );
  80. m_textPinNumber->SetValue( m_pin->GetNumber() );
  81. m_numberSize.SetValue( m_pin->GetNumberTextSize() );
  82. m_pinLength.SetValue( m_pin->GetLength() );
  83. m_checkApplyToAllParts->SetValue( m_pin->GetUnit() == 0 );
  84. m_checkApplyToAllConversions->SetValue( m_pin->GetConvert() == 0 );
  85. m_checkShow->SetValue( m_pin->IsVisible() );
  86. return true;
  87. }
  88. bool DIALOG_LIB_EDIT_PIN::TransferDataFromWindow()
  89. {
  90. if( !DIALOG_SHIM::TransferDataFromWindow() )
  91. return false;
  92. const int acceptable_mingrid = 50;
  93. if( ( m_posX.GetValue() % acceptable_mingrid ) || ( m_posY.GetValue() % acceptable_mingrid ) )
  94. {
  95. auto msg = wxString::Format( _( "This pin is not on a %d mils grid which will make it\n"
  96. "difficult to connect to in the schematic.\n"
  97. "Do you want to continue?" ),
  98. acceptable_mingrid );
  99. if( !IsOK( this, msg ) )
  100. return false;
  101. }
  102. if( !m_pin->InEditMode() )
  103. m_frame->SaveCopyInUndoList( m_pin->GetParent() );
  104. m_pin->SetName( m_textPinName->GetValue() );
  105. m_pin->SetNumber( m_textPinNumber->GetValue() );
  106. m_pin->SetNameTextSize( m_nameSize.GetValue() );
  107. m_pin->SetNumberTextSize( m_numberSize.GetValue() );
  108. m_pin->SetOrientation( LIB_PIN::GetOrientationCode( m_choiceOrientation->GetSelection() ) );
  109. m_pin->SetLength( m_pinLength.GetValue() );
  110. m_pin->SetPinPosition( wxPoint( m_posX.GetValue(), -m_posY.GetValue() ) );
  111. m_pin->SetType( m_choiceElectricalType->GetPinTypeSelection() );
  112. m_pin->SetShape( m_choiceStyle->GetPinShapeSelection() );
  113. m_pin->SetConversion( m_checkApplyToAllConversions->GetValue() ? 0 : m_frame->GetConvert() );
  114. m_pin->SetPartNumber( m_checkApplyToAllParts->GetValue() ? 0 : m_frame->GetUnit() );
  115. m_pin->SetVisible( m_checkShow->GetValue() );
  116. return true;
  117. }
  118. /*
  119. * Draw (on m_panelShowPin) the pin currently edited accroding to current settings in dialog
  120. */
  121. void DIALOG_LIB_EDIT_PIN::OnPaintShowPanel( wxPaintEvent& event )
  122. {
  123. wxPaintDC dc( m_panelShowPin );
  124. wxSize dc_size = dc.GetSize();
  125. dc.SetDeviceOrigin( dc_size.x / 2, dc_size.y / 2 );
  126. // Give a parent to m_dummyPin only from draw purpose.
  127. // In fact m_dummyPin should not have a parent, but draw functions need a parent
  128. // to know some options, about pin texts
  129. LIB_EDIT_FRAME* libframe = (LIB_EDIT_FRAME*) GetParent();
  130. m_dummyPin->SetParent( libframe->GetCurPart() );
  131. // Calculate a suitable scale to fit the available draw area
  132. EDA_RECT bBox = m_dummyPin->GetBoundingBox();
  133. double xscale = (double) dc_size.x / bBox.GetWidth();
  134. double yscale = (double) dc_size.y / bBox.GetHeight();
  135. double scale = std::min( xscale, yscale );
  136. // Give a 10% margin
  137. scale *= 0.9;
  138. dc.SetUserScale( scale, scale );
  139. GRResetPenAndBrush( &dc );
  140. // This is a flag for m_dummyPin->Draw
  141. uintptr_t flags = uintptr_t( PIN_DRAW_TEXTS | PIN_DRAW_DANGLING );
  142. m_dummyPin->Draw( nullptr, &dc, -bBox.Centre(), COLOR4D::UNSPECIFIED, GR_COPY, (void*)flags,
  143. DefaultTransform );
  144. m_dummyPin->SetParent( nullptr );
  145. event.Skip();
  146. }
  147. void DIALOG_LIB_EDIT_PIN::OnPropertiesChange( wxCommandEvent& event )
  148. {
  149. if( !IsShown() ) // do nothing at init time
  150. return;
  151. m_dummyPin->SetName( m_textPinName->GetValue() );
  152. m_dummyPin->SetNumber( m_textPinNumber->GetValue() );
  153. m_dummyPin->SetNameTextSize( m_nameSize.GetValue() );
  154. m_dummyPin->SetNumberTextSize( m_numberSize.GetValue() );
  155. m_dummyPin->SetOrientation( LIB_PIN::GetOrientationCode( m_choiceOrientation->GetSelection() ) );
  156. m_dummyPin->SetLength( m_pinLength.GetValue() );
  157. m_dummyPin->SetType( m_choiceElectricalType->GetPinTypeSelection() );
  158. m_dummyPin->SetShape( m_choiceStyle->GetPinShapeSelection() );
  159. m_dummyPin->SetVisible( m_checkShow->GetValue() );
  160. m_panelShowPin->Refresh();
  161. }