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 - 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 
25 #include <fctsys.h>
26 #include <macros.h>
27 #include <gr_basic.h>
28 #include <bitmaps.h>
29 #include <sch_painter.h>
30 #include <lib_edit_frame.h>
31 #include <class_libentry.h>
32 #include <lib_pin.h>
33 
34 #include <dialog_lib_edit_pin.h>
35 #include <confirm.h>
36 
37 
39  DIALOG_LIB_EDIT_PIN_BASE( parent ),
40  m_frame( parent ),
41  m_pin( aPin ),
42  m_posX( parent, m_posXLabel, m_posXCtrl, m_posXUnits, true ),
43  m_posY( parent, m_posYLabel, m_posYCtrl, m_posYUnits, true ),
44  m_pinLength( parent, m_pinLengthLabel, m_pinLengthCtrl, m_pinLengthUnits, true, 0 ),
45  m_nameSize( parent, m_nameSizeLabel, m_nameSizeCtrl, m_nameSizeUnits, true, 0 ),
46  m_numberSize( parent, m_numberSizeLabel, m_numberSizeCtrl, m_numberSizeUnits, true, 0 )
47 {
48  // Creates a dummy pin to show on a panel, inside this dialog:
49  m_dummyPin = new LIB_PIN( *m_pin );
50 
51  // m_dummyPin changes do not propagate to other pins of the current lib component,
52  // so set parent to null and clear flags
53  m_dummyPin->SetParent( nullptr );
55 
57  m_panelShowPin->SetBackgroundColour( bgColor.ToColour() );
58 
59  const wxArrayString& orientationNames = LIB_PIN::GetOrientationNames();
60  const BITMAP_DEF* orientationBitmaps = LIB_PIN::GetOrientationSymbols();
61 
62  for ( unsigned ii = 0; ii < orientationNames.GetCount(); ii++ )
63  m_choiceOrientation->Insert( orientationNames[ii], KiBitmap( orientationBitmaps[ii] ), ii );
64 
65  m_sdbSizerButtonsOK->SetDefault();
67 
68  // Now all widgets have the size fixed, call FinishDialogSettings
70 
71  // On some windows manager (Unity, XFCE), this dialog is
72  // not always raised, depending on this dialog is run.
73  // Force it to be raised
74  Raise();
75 }
76 
77 
79 {
80  delete m_dummyPin;
81 }
82 
83 
85 {
86  if( !DIALOG_SHIM::TransferDataToWindow() )
87  return false;
88 
92  m_textPinName->SetValue( m_pin->GetName() );
96  m_textPinNumber->SetValue( m_pin->GetNumber() );
99  m_checkApplyToAllParts->SetValue( m_pin->GetUnit() == 0 );
100  m_checkApplyToAllConversions->SetValue( m_pin->GetConvert() == 0 );
101  m_checkShow->SetValue( m_pin->IsVisible() );
102 
103  return true;
104 }
105 
106 
108 {
109  if( !DIALOG_SHIM::TransferDataFromWindow() )
110  return false;
111 
112  const int acceptable_mingrid = 50;
113 
114  if( ( m_posX.GetValue() % acceptable_mingrid ) || ( m_posY.GetValue() % acceptable_mingrid ) )
115  {
116  auto msg = wxString::Format( _( "This pin is not on a %d mils grid which will make it\n"
117  "difficult to connect to in the schematic.\n"
118  "Do you want to continue?" ),
119  acceptable_mingrid );
120  if( !IsOK( this, msg ) )
121  return false;
122  }
123 
124  if( !m_pin->InEditMode() )
126 
127  m_pin->SetName( m_textPinName->GetValue() );
128  m_pin->SetNumber( m_textPinNumber->GetValue() );
137  m_pin->SetPartNumber( m_checkApplyToAllParts->GetValue() ? 0 : m_frame->GetUnit() );
138  m_pin->SetVisible( m_checkShow->GetValue() );
139 
140  return true;
141 }
142 
143 
144 /*
145  * Draw (on m_panelShowPin) the pin currently edited accroding to current settings in dialog
146  */
147 void DIALOG_LIB_EDIT_PIN::OnPaintShowPanel( wxPaintEvent& event )
148 {
149  wxPaintDC dc( m_panelShowPin );
150  wxSize dc_size = dc.GetSize();
151  dc.SetDeviceOrigin( dc_size.x / 2, dc_size.y / 2 );
152 
153  // Give a parent to m_dummyPin only from draw purpose.
154  // In fact m_dummyPin should not have a parent, but draw functions need a parent
155  // to know some options, about pin texts
156  LIB_EDIT_FRAME* libframe = (LIB_EDIT_FRAME*) GetParent();
157  m_dummyPin->SetParent( libframe->GetCurPart() );
158 
159  // Calculate a suitable scale to fit the available draw area
161  double xscale = (double) dc_size.x / bBox.GetWidth();
162  double yscale = (double) dc_size.y / bBox.GetHeight();
163  double scale = std::min( xscale, yscale );
164 
165  // Give a 10% margin
166  scale *= 0.9;
167  dc.SetUserScale( scale, scale );
168  GRResetPenAndBrush( &dc );
169 
170  // This is a flag for m_dummyPin->Draw
171  uintptr_t flags = uintptr_t( PIN_DRAW_TEXTS | PIN_DRAW_DANGLING );
172 
173  m_dummyPin->Draw( nullptr, &dc, -bBox.Centre(), COLOR4D::UNSPECIFIED, GR_COPY, (void*)flags,
175 
176  m_dummyPin->SetParent( nullptr );
177 
178  event.Skip();
179 }
180 
181 
182 void DIALOG_LIB_EDIT_PIN::OnPropertiesChange( wxCommandEvent& event )
183 {
184  if( !IsShown() ) // do nothing at init time
185  return;
186 
187  m_dummyPin->SetName( m_textPinName->GetValue() );
188  m_dummyPin->SetNumber( m_textPinNumber->GetValue() );
195  m_dummyPin->SetVisible( m_checkShow->GetValue() );
196 
197  m_panelShowPin->Refresh();
198 }
void GRResetPenAndBrush(wxDC *DC)
Definition: gr_basic.cpp:123
void SetLength(int aLength, bool aTestOtherPins=true)
Set the pin length.
Definition: lib_pin.cpp:363
PNG memory record (file in memory).
Definition: bitmap_types.h:43
int GetUnit() const
static int GetOrientationIndex(int aCode)
Get the index of the orientation code.
Definition: lib_pin.cpp:1910
This file is part of the common library.
static wxArrayString GetOrientationNames()
Get a list of pin orientation names.
Definition: lib_pin.cpp:1890
Class DIALOG_LIB_EDIT_PIN_BASE.
void SetShape(GRAPHIC_PINSHAPE aShape)
Set the shape of the pin to aShape.
Definition: lib_pin.cpp:298
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:161
int GetHeight() const
Definition: eda_rect.h:118
bool InEditMode() const
Return the draw item editing mode status.
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:1952
const EDA_RECT GetBoundingBox() const override
Definition: lib_pin.h:145
int GetNameTextSize() const
Definition: lib_pin.h:192
int GetOrientation() const
Definition: lib_pin.h:221
const COLOR4D & GetLayerColor(int aLayer) const
Function GetLayerColor Returns the color used to draw a layer.
Definition: painter.h:219
void SetInitialFocus(wxWindow *aWindow)
Sets the window (usually a wxTextCtrl) that should be focused when the dialog is shown.
Definition: dialog_shim.h:115
int GetNumberTextSize() const
Definition: lib_pin.h:219
const wxString & GetNumber() const
Definition: lib_pin.h:194
This file contains miscellaneous commonly used macros and functions.
virtual int GetValue()
Function GetValue Returns the current value in Internal Units.
const wxString & GetName() const
Definition: lib_pin.h:166
void SetConversion(int aConversion)
Set the body style (conversion) of the pin.
Definition: lib_pin.cpp:450
void SetNumber(const wxString &aNumber)
Set the pin number.
Definition: lib_pin.cpp:222
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:79
LIB_PART * GetParent() const
ELECTRICAL_PINTYPE GetPinTypeSelection()
LIB_PART * GetCurPart() const
Return the current part being edited or NULL if none selected.
void OnPaintShowPanel(wxPaintEvent &event) override
void SetParent(EDA_ITEM *aParent)
Definition: base_struct.h:216
Class LIB_ITEM definition.
ELECTRICAL_PINTYPE GetType() const
Get the electrical type of the pin.
Definition: lib_pin.h:251
void SetVisible(bool aVisible)
Set or clear the visibility flag for the pin.
Definition: lib_pin.cpp:481
void SetSelection(ELECTRICAL_PINTYPE aType)
wxPoint Centre() const
Definition: eda_rect.h:60
bool TransferDataToWindow() override
bool IsVisible() const
Return the visibility status of the draw object.
Definition: lib_pin.h:362
LIB_EDIT_FRAME * m_frame
void SetPinPosition(wxPoint aPosition)
move this and all linked pins to the new position used in pin editing.
Definition: lib_pin.cpp:393
wxPoint GetPosition() const override
Return the current draw object position.
Definition: lib_pin.h:463
void SetPartNumber(int aPart)
Set the pin part number.
Definition: lib_pin.cpp:420
The symbol library editor main window.
const int scale
TRANSFORM DefaultTransform
Definition: eeschema.cpp:59
virtual void Draw(EDA_DRAW_PANEL *aPanel, wxDC *aDC, const wxPoint &aOffset, COLOR4D aColor, GR_DRAWMODE aDrawMode, void *aData, const TRANSFORM &aTransform)
Draw an item.
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...
void SaveCopyInUndoList(EDA_ITEM *ItemToCopy, UNDO_REDO_T undoType=UR_LIBEDIT)
Create a copy of the current component, and save it in the undo list.
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:327
int GetWidth() const
Definition: eda_rect.h:117
void ClearFlags(STATUS_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: base_struct.h:257
Definition of class LIB_EDIT_FRAME.
void SetOrientation(int aOrientation, bool aTestOtherPins=true)
Set orientation on the pin.
Definition: lib_pin.cpp:269
PinTypeComboBox * m_choiceElectricalType
void SetNameTextSize(int aSize, bool aTestOtherPins=true)
Set the aSize of the pin name text.
Definition: lib_pin.cpp:194
int GetLength()
Definition: lib_pin.h:301
GRAPHIC_PINSHAPE GetPinShapeSelection()
GRAPHIC_PINSHAPE GetShape() const
Definition: lib_pin.h:235
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Function IsOK displays a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:295
bool TransferDataFromWindow() override
void OnPropertiesChange(wxCommandEvent &event) override
void SetNumberTextSize(int aSize, bool aTestOtherPins=true)
Set the size of the pin number text.
Definition: lib_pin.cpp:241
#define min(a, b)
Definition: auxiliary.h:85
int GetConvert() const
static int GetOrientationCode(int aIndex)
Get the orientation code by index used to set the pin orientation.
Definition: lib_pin.cpp:1901
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39