KiCad PCB EDA Suite
lib_pin_tool.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) 2019 CERN
5  * Copyright (C) 2019 KiCad Developers, see AUTHORS.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 <tool/tool_manager.h>
27 #include <lib_edit_frame.h>
28 #include <eeschema_id.h>
29 #include <confirm.h>
30 #include <ee_actions.h>
31 #include <sch_view.h>
34 #include "lib_pin_tool.h"
35 
36 
40 static bool g_LastPinCommonConvert = false;
41 static bool g_LastPinCommonUnit = false;
42 static bool g_LastPinVisible = true;
43 
44 // The -1 is a non-valid value to trigger delayed initialization
45 static int g_LastPinLength = -1;
46 static int g_LastPinNameSize = -1;
47 static int g_LastPinNumSize = -1;
48 
49 static int GetLastPinLength()
50 {
51  if( g_LastPinLength == -1 )
53 
54  return g_LastPinLength;
55 }
56 
57 static int GetLastPinNameSize()
58 {
59  if( g_LastPinNameSize == -1 )
61 
62  return g_LastPinNameSize;
63 }
64 
65 static int GetLastPinNumSize()
66 {
67  if( g_LastPinNumSize == -1 )
69 
70  return g_LastPinNumSize;
71 }
72 
73 
74 extern void IncrementLabelMember( wxString& name, int aIncrement );
75 
76 
78  EE_TOOL_BASE<LIB_EDIT_FRAME>( "eeschema.PinEditing" )
79 {
80 }
81 
82 
84 {
86 
87  auto singlePinCondition = EE_CONDITIONS::Count( 1 ) && EE_CONDITIONS::OnlyType( LIB_PIN_T );
88 
90 
91  selToolMenu.AddSeparator( 400 );
92  selToolMenu.AddItem( EE_ACTIONS::pushPinLength, singlePinCondition, 400 );
93  selToolMenu.AddItem( EE_ACTIONS::pushPinNameSize, singlePinCondition, 400 );
94  selToolMenu.AddItem( EE_ACTIONS::pushPinNumSize, singlePinCondition, 400 );
95 
96  return true;
97 }
98 
99 
101 {
102  aPin->EnableEditMode( true, !m_frame->SynchronizePins() );
103 
104  DIALOG_LIB_EDIT_PIN dlg( m_frame, aPin );
105 
106  if( dlg.ShowModal() == wxID_CANCEL )
107  {
108  return false;
109  }
110 
111  m_frame->RefreshItem( aPin );
112  m_frame->OnModify( );
113 
114  MSG_PANEL_ITEMS items;
115  aPin->GetMsgPanelInfo( m_frame->GetUserUnits(), items );
116  m_frame->SetMsgPanel( items );
117 
118  aPin->EnableEditMode( false );
119 
120  // Save the pin properties to use for the next new pin.
124  g_LastPinLength = aPin->GetLength();
125  g_LastPinShape = aPin->GetShape();
126  g_LastPinType = aPin->GetType();
127  g_LastPinCommonConvert = aPin->GetConvert() == 0;
128  g_LastPinCommonUnit = aPin->GetUnit() == 0;
129  g_LastPinVisible = aPin->IsVisible();
130 
131  return true;
132 }
133 
134 
136 {
137  LIB_PART* part = m_frame->GetCurPart();
138  bool ask_for_pin = true; // Test for another pin in same position in other units
139 
140  for( LIB_PIN* test = part->GetNextPin(); test; test = part->GetNextPin( test ) )
141  {
142  if( test == aPin || aPin->GetPosition() != test->GetPosition() || test->GetEditFlags() )
143  continue;
144 
145  // test for same body style
146  if( test->GetConvert() && test->GetConvert() != aPin->GetConvert() )
147  continue;
148 
149  if( ask_for_pin && m_frame->SynchronizePins() )
150  {
151  wxString msg;
152  msg.Printf( _( "This position is already occupied by another pin, in unit %d." ),
153  test->GetUnit() );
154 
155  KIDIALOG dlg( m_frame, msg, _( "Confirmation" ), wxOK | wxCANCEL | wxICON_WARNING );
156  dlg.SetOKLabel( _( "Place Pin Anyway" ) );
157  dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
158 
159  bool status = dlg.ShowModal() == wxID_OK;
160 
161  if( !status )
162  {
163  if( aPin->IsNew() )
164  delete aPin;
165 
166  return false;
167  }
168  else
169  {
170  ask_for_pin = false;
171  }
172  }
173  }
174 
175  if( aPin->IsNew() && !( aPin->GetFlags() & IS_PASTED ) )
176  {
178  g_LastPinType = aPin->GetType();
179  g_LastPinShape = aPin->GetShape();
180 
181  if( m_frame->SynchronizePins() )
182  CreateImagePins( aPin );
183 
184  part->AddDrawItem( aPin );
185  aPin->ClearFlags( IS_NEW );
186  }
187 
188  // Put linked pins in new position, and clear flags
189  for( LIB_PIN* pin = part->GetNextPin(); pin; pin = part->GetNextPin( pin ) )
190  {
191  if( ( pin->GetEditFlags() & IS_LINKED ) == 0 )
192  continue;
193 
194  pin->MoveTo( aPin->GetPosition() );
195  pin->ClearFlags();
196  }
197 
198  m_frame->RebuildView();
199  m_frame->OnModify();
200 
201  return true;
202 }
203 
204 
205 /*
206  * Create a new pin.
207  */
208 LIB_PIN* LIB_PIN_TOOL::CreatePin( const VECTOR2I& aPosition, LIB_PART* aPart )
209 {
210  aPart->ClearTempFlags();
211 
212  LIB_PIN* pin = new LIB_PIN( aPart );
213 
214  pin->SetFlags( IS_NEW );
215 
216  // Flag pins to consider
217  if( m_frame->SynchronizePins() )
218  pin->SetFlags( IS_LINKED );
219 
220  pin->MoveTo((wxPoint) aPosition );
221  pin->SetLength( GetLastPinLength() );
223  pin->SetType( g_LastPinType );
224  pin->SetShape( g_LastPinShape );
228  pin->SetUnit( g_LastPinCommonUnit ? 0 : m_frame->GetUnit() );
230 
231  if( !EditPinProperties( pin ) )
232  {
233  delete pin;
234  pin = nullptr;
235  }
236 
237  return pin;
238 }
239 
240 
242 {
243  int ii;
244  LIB_PIN* newPin;
245 
246  // if "synchronize pins editing" option is off, do not create any similar pin for other
247  // units and/or shapes: each unit is edited regardless other units or body
248  if( !m_frame->SynchronizePins() )
249  return;
250 
251  if( aPin->GetUnit() == 0 ) // Pin common to all units: no need to create similar pins.
252  return;
253 
254  // When units are interchangeable, all units are expected to have similar pins
255  // at the same position
256  // to facilitate pin editing, create pins for all other units for the current body style
257  // at the same position as aPin
258 
259  for( ii = 1; ii <= aPin->GetParent()->GetUnitCount(); ii++ )
260  {
261  if( ii == aPin->GetUnit() )
262  continue;
263 
264  newPin = (LIB_PIN*) aPin->Clone();
265 
266  // To avoid mistakes, gives this pin a new pin number because
267  // it does no have the save pin number as the master pin
268  // Because we do not know the actual number, give it a temporary number
269  wxString unknownNum;
270  unknownNum.Printf( "%s-U%c", aPin->GetNumber(), wxChar( 'A' + ii - 1 ) );
271  newPin->SetNumber( unknownNum );
272 
273  newPin->SetUnit( ii );
274  aPin->GetParent()->AddDrawItem( newPin );
275  }
276 }
277 
278 
280 {
281  LIB_PART* part = m_frame->GetCurPart();
282  EE_SELECTION& selection = m_selectionTool->GetSelection();
283  LIB_PIN* sourcePin = dynamic_cast<LIB_PIN*>( selection.Front() );
284 
285  if( !sourcePin )
286  return 0;
287 
289 
290  for( LIB_PIN* pin = part->GetNextPin(); pin; pin = part->GetNextPin( pin ) )
291  {
292  if( pin->GetConvert() && pin->GetConvert() != m_frame->GetConvert() )
293  continue;
294 
295  if( pin == sourcePin )
296  continue;
297 
298  if( aEvent.IsAction( &EE_ACTIONS::pushPinLength ) )
299  pin->SetLength( sourcePin->GetLength() );
300  else if( aEvent.IsAction( &EE_ACTIONS::pushPinNameSize ) )
301  pin->SetNameTextSize( sourcePin->GetNameTextSize() );
302  else if( aEvent.IsAction( &EE_ACTIONS::pushPinNumSize ) )
303  pin->SetNumberTextSize( sourcePin->GetNumberTextSize() );
304  }
305 
306  m_frame->RebuildView();
307  m_frame->OnModify();
308 
309  return 0;
310 }
311 
312 
313 // Create a new pin based on the previous pin with an incremented pin number.
315 {
316  LIB_PIN* pin = (LIB_PIN*) aSourcePin->Clone();
317  wxPoint step;
318 
319  pin->ClearFlags();
320  pin->SetFlags( IS_NEW );
321 
322  switch( pin->GetOrientation() )
323  {
324  case PIN_UP: step.x = m_frame->GetRepeatPinStep(); break;
325  case PIN_DOWN: step.x = m_frame->GetRepeatPinStep(); break;
326  case PIN_LEFT: step.y = -m_frame->GetRepeatPinStep(); break;
327  case PIN_RIGHT: step.y = -m_frame->GetRepeatPinStep(); break;
328  }
329 
330  pin->Offset( step );
331 
332  wxString nextName = pin->GetName();
334  pin->SetName( nextName );
335 
336  wxString nextNumber = pin->GetNumber();
338  pin->SetNumber( nextNumber );
339 
340  if( m_frame->SynchronizePins() )
341  pin->SetFlags( IS_LINKED );
342 
343  PlacePin( pin );
344 
345  return pin;
346 }
347 
348 
350 {
354 }
355 
#define IS_LINKED
Used in calculation to mark linked items (temporary use)
Definition: base_struct.h:117
LIB_PART * GetCurPart() const
Return the current part being edited or NULL if none selected.
int GetUnitCount() const
void SetLength(int aLength, bool aTestOtherPins=true)
Set the pin length.
Definition: lib_pin.cpp:364
EDA_ITEM * Clone() const override
Function Clone creates a duplicate of this item with linked list members set to NULL.
Definition: lib_pin.cpp:1258
void DoNotShowCheckbox(wxString file, int line)
Shows the 'do not show again' checkbox
Definition: confirm.cpp:60
bool Init() override
Function Init() Init() is called once upon a registration of the tool.
int GetConvert() const
Helper class to create more flexible dialogs, including 'do not show again' checkbox handling.
Definition: confirm.h:44
This file is part of the common library.
int GetOrientation() const
Definition: lib_pin.h:204
static bool g_LastPinCommonConvert
LIB_PART * GetParent() const
Definition: lib_item.h:177
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
CONDITIONAL_MENU & GetMenu()
Function GetMenu.
Definition: tool_menu.cpp:46
static int GetDefaultPinLength()
void SetName(const wxString &aName, bool aTestOtherPins=true)
Set the pin name.
Definition: lib_pin.cpp:162
Implementing DIALOG_LIB_EDIT_PIN_BASE.
TOOL_MENU & GetToolMenu()
static int GetLastPinLength()
static int g_LastPinNameSize
static int g_LastPinNumSize
Definition: lib_pin.h:55
void EnableEditMode(bool aEnable, bool aEditPinByPin=false)
Enable or clear pin editing mode.
Definition: lib_pin.cpp:515
static SELECTION_CONDITION Count(int aNumber)
Function Count Creates a functor that tests if the number of selected items is equal to the value giv...
static TOOL_ACTION pushPinNameSize
Definition: ee_actions.h:188
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Function Go()
void ClearTempFlags()
Clears the status flag all draw objects in this part.
bool IsAction(const TOOL_ACTION *aAction) const
Function IsAction() Tests if the event contains an action issued upon activation of the given TOOL_AC...
Definition: tool_event.cpp:63
bool IsNew() const
Definition: base_struct.h:228
bool Init() override
Function Init() Init() is called once upon a registration of the tool.
Definition: ee_tool_base.h:69
void AddDrawItem(LIB_ITEM *aItem)
Add a new draw aItem to the draw object list.
EE_SELECTION & GetSelection()
Function GetSelection()
#define IS_NEW
New item, just created.
Definition: base_struct.h:120
void SetNumber(const wxString &aNumber)
Set the pin number.
Definition: lib_pin.cpp:223
static int GetPinNumDefaultSize()
int GetUnit() const
Definition: lib_item.h:301
GRAPHIC_PINSHAPE
Definition: pin_shape.h:35
static int g_LastPinOrient
int GetUnit() const
void SetFlags(STATUS_FLAGS aMask)
Definition: base_struct.h:265
static int g_LastPinLength
void SetMsgPanel(const std::vector< MSG_PANEL_ITEM > &aList)
Clear the message panel and populates it with the contents of aList.
const wxString & GetName() const
Definition: lib_pin.h:149
Class TOOL_EVENT.
Definition: tool_event.h:168
bool EditPinProperties(LIB_PIN *aPin)
Define a library symbol object.
void SetVisible(bool aVisible)
Set or clear the visibility flag for the pin.
Definition: lib_pin.cpp:482
static TOOL_ACTION pushPinLength
Definition: ee_actions.h:187
EE_SELECTION_TOOL * m_selectionTool
Definition: ee_tool_base.h:140
void IncrementLabelMember(wxString &name, int aIncrement)
Definition: edit_label.cpp:208
void GetMsgPanelInfo(EDA_UNITS_T aUnits, std::vector< MSG_PANEL_ITEM > &aList) override
Display basic info (type, part and convert) about the current item in message panel.
bool IsVisible() const
Return the visibility status of the draw object.
Definition: lib_pin.h:343
int GetNameTextSize() const
Definition: lib_pin.h:175
int GetConvert() const
Definition: lib_item.h:304
LIB_PIN * RepeatPin(const LIB_PIN *aSourcePin)
#define _(s)
static int GetLastPinNameSize()
void CreateImagePins(LIB_PIN *aPin)
void AddSeparator(int aOrder=ANY_ORDER)
Function AddSeparator()
void SetConvert(int aConvert)
Definition: lib_item.h:303
wxPoint GetPosition() const override
Definition: lib_pin.h:427
static GRAPHIC_PINSHAPE g_LastPinShape
LIB_PIN * GetNextPin(LIB_PIN *aItem=NULL)
Return the next pin object from the draw list.
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 SetUnit(int aUnit)
Definition: lib_item.h:300
void MoveTo(const wxPoint &aPosition) override
Move a draw object to aPosition.
Definition: lib_pin.cpp:1302
static int GetLastPinNumSize()
int GetNumberTextSize() const
Definition: lib_pin.h:202
The symbol library editor main window.
const char * name
Definition: DXF_plotter.cpp:61
static int GetPinNameDefaultSize()
ELECTRICAL_PINTYPE
The component library pin object electrical types used in ERC tests.
Definition: pin_type.h:37
EDA_UNITS_T GetUserUnits() const
Return the user units currently in use.
void RefreshItem(EDA_ITEM *aItem, bool isAddOrDelete=false)
Mark an item for refresh.
static SELECTION_CONDITION OnlyType(KICAD_T aType)
Function OnlyType Creates a functor that tests if the selected items are only of given type.
void SetType(ELECTRICAL_PINTYPE aType, bool aTestOtherPins=true)
Set the electrical type of the pin.
Definition: lib_pin.cpp:328
int GetRepeatDeltaLabel() const
void saveCopyInUndoList(EDA_ITEM *aItem, UNDO_REDO_T aType, bool aAppend=false)
Similar to m_frame->SaveCopyInUndoList(), but handles items that are owned by their parents.
Definition: ee_tool_base.h:117
int PushPinProperties(const TOOL_EVENT &aEvent)
void OnModify() override
Must be called after a schematic change in order to set the "modify" flag of the current symbol.
std::vector< MSG_PANEL_ITEM > MSG_PANEL_ITEMS
Definition: msgpanel.h:102
void setTransitions() override
Sets up handlers for various events.
int ShowModal() override
Definition: confirm.cpp:102
void ClearFlags(STATUS_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: base_struct.h:266
static TOOL_ACTION pushPinNumSize
Definition: ee_actions.h:189
void SetOrientation(int aOrientation, bool aTestOtherPins=true)
Set orientation on the pin.
Definition: lib_pin.cpp:270
Class EE_TOOL_BASE.
Definition: ee_tool_base.h:50
LIB_PIN * CreatePin(const VECTOR2I &aPosition, LIB_PART *aPart)
int GetRepeatPinStep() const
void SetNameTextSize(int aSize, bool aTestOtherPins=true)
Set the aSize of the pin name text.
Definition: lib_pin.cpp:195
void Offset(const wxPoint &aOffset) override
Set the drawing object by aOffset from the current position.
Definition: lib_pin.cpp:1288
STATUS_FLAGS GetFlags() const
Definition: base_struct.h:267
int GetLength()
Definition: lib_pin.h:282
void AddItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
Function AddItem()
bool PlacePin(LIB_PIN *aPin)
static ELECTRICAL_PINTYPE g_LastPinType
static bool g_LastPinCommonUnit
#define IS_PASTED
Modifier on IS_NEW which indicates it came from clipboard.
Definition: base_struct.h:133
EDA_ITEM * Front() const
Definition: selection.h:182
void SetNumberTextSize(int aSize, bool aTestOtherPins=true)
Set the size of the pin number text.
Definition: lib_pin.cpp:242
static bool g_LastPinVisible