KiCad PCB EDA Suite
dialog_lib_edit_pin.cpp
Go to the documentation of this file.
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 
25 #include <fctsys.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 #include <widgets/tab_traversal.h>
35 
37  DIALOG_LIB_EDIT_PIN_BASE( parent ),
38  m_frame( parent ),
39  m_pin( aPin ),
40  m_posX( parent, m_posXLabel, m_posXCtrl, m_posXUnits, true ),
41  m_posY( parent, m_posYLabel, m_posYCtrl, m_posYUnits, true ),
42  m_pinLength( parent, m_pinLengthLabel, m_pinLengthCtrl, m_pinLengthUnits, true ),
43  m_nameSize( parent, m_nameSizeLabel, m_nameSizeCtrl, m_nameSizeUnits, true ),
44  m_numberSize( parent, m_numberSizeLabel, m_numberSizeCtrl, m_numberSizeUnits, true )
45 {
46  // Creates a dummy pin to show on a panel, inside this dialog:
47  m_dummyPin = new LIB_PIN( *m_pin );
48 
49  // m_dummyPin changes do not propagate to other pins of the current lib component,
50  // so set parent to null and clear flags
51  m_dummyPin->SetParent( nullptr );
53 
55  m_panelShowPin->SetBackgroundColour( bgColor.ToColour() );
56 
57  const wxArrayString& orientationNames = LIB_PIN::GetOrientationNames();
58  const BITMAP_DEF* orientationBitmaps = LIB_PIN::GetOrientationSymbols();
59 
60  for ( unsigned ii = 0; ii < orientationNames.GetCount(); ii++ )
61  m_choiceOrientation->Insert( orientationNames[ii], KiBitmap( orientationBitmaps[ii] ), ii );
62 
63  // We can't set the tab order through wxWidgets due to shortcomings in their mnemonics
64  // implementation on MSW
65  m_tabOrder = {
70  m_posXCtrl,
71  m_posYCtrl,
81  };
82 
83  m_sdbSizerButtonsOK->SetDefault();
85 
86  // Now all widgets have the size fixed, call FinishDialogSettings
88 
89  // On some window managers (Unity, XFCE) the dialog is not always raised, depending on
90  // how it is is run.
91  Raise();
92 }
93 
94 
96 {
97  delete m_dummyPin;
98 }
99 
100 
102 {
103  if( !DIALOG_SHIM::TransferDataToWindow() )
104  return false;
105 
109  m_textPinName->SetValue( m_pin->GetName() );
112  m_posY.SetValue( -m_pin->GetPosition().y );
113  m_textPinNumber->SetValue( m_pin->GetNumber() );
116  m_checkApplyToAllParts->SetValue( m_pin->GetUnit() == 0 );
117  m_checkApplyToAllConversions->SetValue( m_pin->GetConvert() == 0 );
118  m_checkShow->SetValue( m_pin->IsVisible() );
119 
121 
122  return true;
123 }
124 
125 
127 {
128  if( !DIALOG_SHIM::TransferDataFromWindow() )
129  return false;
130 
131  const int acceptable_mingrid = 50;
132 
133  if( ( m_posX.GetValue() % acceptable_mingrid ) || ( m_posY.GetValue() % acceptable_mingrid ) )
134  {
135  auto msg = wxString::Format( _( "This pin is not on a %d mils grid which will make it\n"
136  "difficult to connect to in the schematic.\n"
137  "Do you want to continue?" ),
138  acceptable_mingrid );
139  if( !IsOK( this, msg ) )
140  return false;
141  }
142 
143  if( m_pin->GetEditFlags() == 0 )
145 
146  m_pin->SetName( m_textPinName->GetValue() );
147  m_pin->SetNumber( m_textPinNumber->GetValue() );
152  m_pin->SetPinPosition( wxPoint( m_posX.GetValue(), -m_posY.GetValue() ) );
156  m_pin->SetPartNumber( m_checkApplyToAllParts->GetValue() ? 0 : m_frame->GetUnit() );
157  m_pin->SetVisible( m_checkShow->GetValue() );
158 
159  return true;
160 }
161 
162 
163 /*
164  * Draw (on m_panelShowPin) the pin according to current settings in dialog
165  */
166 void DIALOG_LIB_EDIT_PIN::OnPaintShowPanel( wxPaintEvent& event )
167 {
168  wxPaintDC dc( m_panelShowPin );
169  wxSize dc_size = dc.GetSize();
170  dc.SetDeviceOrigin( dc_size.x / 2, dc_size.y / 2 );
171 
172  // Give a parent to m_dummyPin only from draw purpose.
173  // In fact m_dummyPin should not have a parent, but draw functions need a parent
174  // to know some options, about pin texts
175  LIB_EDIT_FRAME* libframe = (LIB_EDIT_FRAME*) GetParent();
176  m_dummyPin->SetParent( libframe->GetCurPart() );
177 
178  // Calculate a suitable scale to fit the available draw area
179  EDA_RECT bBox = m_dummyPin->GetBoundingBox( true );
180  double xscale = (double) dc_size.x / bBox.GetWidth();
181  double yscale = (double) dc_size.y / bBox.GetHeight();
182  double scale = std::min( xscale, yscale );
183 
184  // Give a 10% margin and limit to no more than 100% zoom
185  scale = std::min( scale * 0.9, 1.0 );
186  dc.SetUserScale( scale, scale );
187  GRResetPenAndBrush( &dc );
188 
190  opts.draw_hidden_fields = true;
191 
192  m_dummyPin->Print( &dc, -bBox.Centre(), (void*) &opts, DefaultTransform );
193 
194  m_dummyPin->SetParent( nullptr );
195 
196  event.Skip();
197 }
198 
199 
200 void DIALOG_LIB_EDIT_PIN::OnPropertiesChange( wxCommandEvent& event )
201 {
202  if( !IsShown() ) // do nothing at init time
203  return;
204 
205  m_dummyPin->SetName( m_textPinName->GetValue() );
206  m_dummyPin->SetNumber( m_textPinNumber->GetValue() );
213  m_dummyPin->SetVisible( m_checkShow->GetValue() );
214 
215  m_panelShowPin->Refresh();
216 }
void GRResetPenAndBrush(wxDC *DC)
Definition: gr_basic.cpp:119
Functions for manipulating tab traversal in forms and dialogs.
void SetLength(int aLength, bool aTestOtherPins=true)
Set the pin length.
Definition: lib_pin.cpp:364
int GetConvert() const
static int GetOrientationIndex(int aCode)
Get the index of the orientation code.
Definition: lib_pin.cpp:1580
This file is part of the common library.
static wxArrayString GetOrientationNames()
Get a list of pin orientation names.
Definition: lib_pin.cpp:1560
Class DIALOG_LIB_EDIT_PIN_BASE.
int GetOrientation() const
Definition: lib_pin.h:204
LIB_PART * GetParent() const
Definition: lib_item.h:177
std::vector< wxWindow * > m_tabOrder
Definition: dialog_shim.h:207
void SetShape(GRAPHIC_PINSHAPE aShape)
Set the shape of the pin to aShape.
Definition: lib_pin.cpp:299
GRAPHIC_PINSHAPE GetShape() const
Definition: lib_pin.h:216
DIALOG_LIB_EDIT_PIN(LIB_EDIT_FRAME *parent, LIB_PIN *aPin)
Constructor.
void SetSelection(GRAPHIC_PINSHAPE aShape)
void FinishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
void SetName(const wxString &aName, bool aTestOtherPins=true)
Set the pin name.
Definition: lib_pin.cpp:162
int GetWidth() const
Definition: eda_rect.h:119
const COLOR4D & GetLayerColor(int aLayer) const
Function GetLayerColor Returns the color used to draw a layer.
Definition: painter.h:221
KIGFX::SCH_RENDER_SETTINGS * GetRenderSettings()
static const BITMAP_DEF * GetOrientationSymbols()
Get a list of pin orientation bitmaps for menus and dialogs.
Definition: lib_pin.cpp:1594
LIB_PART * GetCurPart()
Return the current part being edited or NULL if none selected.
const EDA_RECT GetBoundingBox() const override
Definition: lib_pin.h:128
void SetInitialFocus(wxWindow *aWindow)
Sets the window (usually a wxTextCtrl) that should be focused when the dialog is shown.
Definition: dialog_shim.h:115
bool draw_hidden_fields
Whether to draw "hidden" fields.
void SetConversion(int aConversion)
Set the body style (conversion) of the pin.
Definition: lib_pin.cpp:451
void SetNumber(const wxString &aNumber)
Set the pin number.
Definition: lib_pin.cpp:223
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:79
int GetUnit() const
Definition: lib_item.h:301
ELECTRICAL_PINTYPE GetPinTypeSelection()
int GetUnit() const
void OnPaintShowPanel(wxPaintEvent &event) override
void SetParent(EDA_ITEM *aParent)
Definition: base_struct.h:225
STATUS_FLAGS GetEditFlags() const
Definition: base_struct.h:270
const wxString & GetName() const
Definition: lib_pin.h:149
Class LIB_PIN definition.
void SetVisible(bool aVisible)
Set or clear the visibility flag for the pin.
Definition: lib_pin.cpp:482
void SetSelection(ELECTRICAL_PINTYPE aType)
bool IsVisible() const
Return the visibility status of the draw object.
Definition: lib_pin.h:343
static PART_DRAW_OPTIONS Default()
int GetNameTextSize() const
Definition: lib_pin.h:175
bool TransferDataToWindow() override
int GetConvert() const
Definition: lib_item.h:304
LIB_EDIT_FRAME * m_frame
#define _(s)
int GetHeight() const
Definition: eda_rect.h:120
void SetPinPosition(wxPoint aPosition)
move this and all linked pins to the new position used in pin editing.
Definition: lib_pin.cpp:394
wxPoint GetPosition() const override
Definition: lib_pin.h:427
const wxString & GetNumber() const
Definition: lib_pin.h:177
ELECTRICAL_PINTYPE GetType() const
Get the electrical type of the pin.
Definition: lib_pin.h:232
void SetPartNumber(int aPart)
Set the pin part number.
Definition: lib_pin.cpp:421
int GetNumberTextSize() const
Definition: lib_pin.h:202
The symbol library editor main window.
const int scale
TRANSFORM DefaultTransform
Definition: eeschema.cpp:50
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
wxBitmapComboBox * m_choiceOrientation
virtual void SetValue(int aValue)
Function SetValue Sets new value (in Internal Units) for the text field, taking care of units convers...
virtual void Print(wxDC *aDC, const wxPoint &aOffset, void *aData, const TRANSFORM &aTransform)
Draw an item.
Definition: lib_item.cpp:123
Class EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
void SetType(ELECTRICAL_PINTYPE aType, bool aTestOtherPins=true)
Set the electrical type of the pin.
Definition: lib_pin.cpp:328
wxPoint Centre() const
Definition: eda_rect.h:62
virtual long long int GetValue()
Function GetValue Returns the current value in Internal Units.
void ClearFlags(STATUS_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: base_struct.h:266
void SetOrientation(int aOrientation, bool aTestOtherPins=true)
Set orientation on the pin.
Definition: lib_pin.cpp:270
PinTypeComboBox * m_choiceElectricalType
void SetNameTextSize(int aSize, bool aTestOtherPins=true)
Set the aSize of the pin name text.
Definition: lib_pin.cpp:195
int GetLength()
Definition: lib_pin.h:282
GRAPHIC_PINSHAPE GetPinShapeSelection()
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:280
bool TransferDataFromWindow() override
void SaveCopyInUndoList(EDA_ITEM *ItemToCopy, UNDO_REDO_T undoType=UR_LIBEDIT, bool aAppend=false)
Create a copy of the current component, and save it in the undo list.
void OnPropertiesChange(wxCommandEvent &event) override
void SetNumberTextSize(int aSize, bool aTestOtherPins=true)
Set the size of the pin number text.
Definition: lib_pin.cpp:242
#define min(a, b)
Definition: auxiliary.h:85
static int GetOrientationCode(int aIndex)
Get the orientation code by index used to set the pin orientation.
Definition: lib_pin.cpp:1571
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39