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.

216 lines
8.0 KiB

7 years ago
  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 - 2019 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 <gr_basic.h>
  26. #include <bitmaps.h>
  27. #include <sch_painter.h>
  28. #include <lib_edit_frame.h>
  29. #include <class_libentry.h>
  30. #include <lib_pin.h>
  31. #include <dialog_lib_edit_pin.h>
  32. #include <confirm.h>
  33. #include <widgets/tab_traversal.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. // We can't set the tab order through wxWidgets due to shortcomings in their mnemonics
  57. // implementation on MSW
  58. m_tabOrder = {
  59. m_textPinName,
  60. m_textPinNumber,
  61. m_choiceElectricalType,
  62. m_choiceStyle,
  63. m_posXCtrl,
  64. m_posYCtrl,
  65. m_choiceOrientation,
  66. m_pinLengthCtrl,
  67. m_nameSizeCtrl,
  68. m_numberSizeCtrl,
  69. m_checkApplyToAllParts,
  70. m_checkApplyToAllConversions,
  71. m_checkShow,
  72. m_sdbSizerButtonsOK,
  73. m_sdbSizerButtonsCancel
  74. };
  75. m_sdbSizerButtonsOK->SetDefault();
  76. SetInitialFocus( m_textPinName );
  77. // Now all widgets have the size fixed, call FinishDialogSettings
  78. FinishDialogSettings();
  79. // On some window managers (Unity, XFCE) the dialog is not always raised, depending on
  80. // how it is is run.
  81. Raise();
  82. }
  83. DIALOG_LIB_EDIT_PIN::~DIALOG_LIB_EDIT_PIN()
  84. {
  85. delete m_dummyPin;
  86. }
  87. bool DIALOG_LIB_EDIT_PIN::TransferDataToWindow()
  88. {
  89. if( !DIALOG_SHIM::TransferDataToWindow() )
  90. return false;
  91. m_choiceOrientation->SetSelection( LIB_PIN::GetOrientationIndex( m_pin->GetOrientation() ) );
  92. m_choiceStyle->SetSelection( m_pin->GetShape() );
  93. m_choiceElectricalType->SetSelection( m_pin->GetType() );
  94. m_textPinName->SetValue( m_pin->GetName() );
  95. m_nameSize.SetValue( m_pin->GetNameTextSize() );
  96. m_posX.SetValue( m_pin->GetPosition().x );
  97. m_posY.SetValue( -m_pin->GetPosition().y );
  98. m_textPinNumber->SetValue( m_pin->GetNumber() );
  99. m_numberSize.SetValue( m_pin->GetNumberTextSize() );
  100. m_pinLength.SetValue( m_pin->GetLength() );
  101. m_checkApplyToAllParts->SetValue( m_pin->GetUnit() == 0 );
  102. m_checkApplyToAllConversions->SetValue( m_pin->GetConvert() == 0 );
  103. m_checkShow->SetValue( m_pin->IsVisible() );
  104. m_dummyPin->SetVisible( m_pin->IsVisible() );
  105. return true;
  106. }
  107. bool DIALOG_LIB_EDIT_PIN::TransferDataFromWindow()
  108. {
  109. if( !DIALOG_SHIM::TransferDataFromWindow() )
  110. return false;
  111. const int acceptable_mingrid = 50;
  112. if( ( m_posX.GetValue() % acceptable_mingrid ) || ( m_posY.GetValue() % acceptable_mingrid ) )
  113. {
  114. auto msg = wxString::Format( _( "This pin is not on a %d mils grid which will make it\n"
  115. "difficult to connect to in the schematic.\n"
  116. "Do you want to continue?" ),
  117. acceptable_mingrid );
  118. if( !IsOK( this, msg ) )
  119. return false;
  120. }
  121. if( m_pin->GetEditFlags() == 0 )
  122. m_frame->SaveCopyInUndoList( m_pin->GetParent() );
  123. m_pin->SetName( m_textPinName->GetValue() );
  124. m_pin->SetNumber( m_textPinNumber->GetValue() );
  125. m_pin->SetNameTextSize( m_nameSize.GetValue() );
  126. m_pin->SetNumberTextSize( m_numberSize.GetValue() );
  127. m_pin->SetOrientation( LIB_PIN::GetOrientationCode( m_choiceOrientation->GetSelection() ) );
  128. m_pin->SetLength( m_pinLength.GetValue() );
  129. m_pin->SetPinPosition( wxPoint( m_posX.GetValue(), -m_posY.GetValue() ) );
  130. m_pin->SetType( m_choiceElectricalType->GetPinTypeSelection() );
  131. m_pin->SetShape( m_choiceStyle->GetPinShapeSelection() );
  132. m_pin->SetConversion( m_checkApplyToAllConversions->GetValue() ? 0 : m_frame->GetConvert() );
  133. m_pin->SetPartNumber( m_checkApplyToAllParts->GetValue() ? 0 : m_frame->GetUnit() );
  134. m_pin->SetVisible( m_checkShow->GetValue() );
  135. return true;
  136. }
  137. /*
  138. * Draw (on m_panelShowPin) the pin according to current settings in dialog
  139. */
  140. void DIALOG_LIB_EDIT_PIN::OnPaintShowPanel( wxPaintEvent& event )
  141. {
  142. wxPaintDC dc( m_panelShowPin );
  143. wxSize dc_size = dc.GetSize();
  144. dc.SetDeviceOrigin( dc_size.x / 2, dc_size.y / 2 );
  145. // Give a parent to m_dummyPin only from draw purpose.
  146. // In fact m_dummyPin should not have a parent, but draw functions need a parent
  147. // to know some options, about pin texts
  148. LIB_EDIT_FRAME* libframe = (LIB_EDIT_FRAME*) GetParent();
  149. m_dummyPin->SetParent( libframe->GetCurPart() );
  150. // Calculate a suitable scale to fit the available draw area
  151. EDA_RECT bBox = m_dummyPin->GetBoundingBox( true );
  152. double xscale = (double) dc_size.x / bBox.GetWidth();
  153. double yscale = (double) dc_size.y / bBox.GetHeight();
  154. double scale = std::min( xscale, yscale );
  155. // Give a 10% margin and limit to no more than 100% zoom
  156. scale = std::min( scale * 0.9, 1.0 );
  157. dc.SetUserScale( scale, scale );
  158. GRResetPenAndBrush( &dc );
  159. PART_DRAW_OPTIONS opts = PART_DRAW_OPTIONS::Default();
  160. opts.draw_hidden_fields = true;
  161. m_dummyPin->Print( &dc, -bBox.Centre(), (void*) &opts, DefaultTransform );
  162. m_dummyPin->SetParent( nullptr );
  163. event.Skip();
  164. }
  165. void DIALOG_LIB_EDIT_PIN::OnPropertiesChange( wxCommandEvent& event )
  166. {
  167. if( !IsShown() ) // do nothing at init time
  168. return;
  169. m_dummyPin->SetName( m_textPinName->GetValue() );
  170. m_dummyPin->SetNumber( m_textPinNumber->GetValue() );
  171. m_dummyPin->SetNameTextSize( m_nameSize.GetValue() );
  172. m_dummyPin->SetNumberTextSize( m_numberSize.GetValue() );
  173. m_dummyPin->SetOrientation( LIB_PIN::GetOrientationCode( m_choiceOrientation->GetSelection() ) );
  174. m_dummyPin->SetLength( m_pinLength.GetValue() );
  175. m_dummyPin->SetType( m_choiceElectricalType->GetPinTypeSelection() );
  176. m_dummyPin->SetShape( m_choiceStyle->GetPinShapeSelection() );
  177. m_dummyPin->SetVisible( m_checkShow->GetValue() );
  178. m_panelShowPin->Refresh();
  179. }