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 <confirm.h>
29 #include <ee_actions.h>
30 #include <sch_view.h>
33 #include "lib_pin_tool.h"
34 
35 
39 static bool g_LastPinCommonConvert = false;
40 static bool g_LastPinCommonUnit = false;
41 static bool g_LastPinVisible = true;
42 
43 // The -1 is a non-valid value to trigger delayed initialization
44 static int g_LastPinLength = -1;
45 static int g_LastPinNameSize = -1;
46 static int g_LastPinNumSize = -1;
47 
48 static int GetLastPinLength()
49 {
50  if( g_LastPinLength == -1 )
52 
53  return g_LastPinLength;
54 }
55 
56 static int GetLastPinNameSize()
57 {
58  if( g_LastPinNameSize == -1 )
60 
61  return g_LastPinNameSize;
62 }
63 
64 static int GetLastPinNumSize()
65 {
66  if( g_LastPinNumSize == -1 )
68 
69  return g_LastPinNumSize;
70 }
71 
72 
73 extern void IncrementLabelMember( wxString& name, int aIncrement );
74 
75 
77  EE_TOOL_BASE<LIB_EDIT_FRAME>( "eeschema.PinEditing" )
78 {
79 }
80 
81 
83 {
85 
86  auto singlePinCondition = EE_CONDITIONS::Count( 1 ) && EE_CONDITIONS::OnlyType( LIB_PIN_T );
87 
89 
90  selToolMenu.AddSeparator( 400 );
91  selToolMenu.AddItem( EE_ACTIONS::pushPinLength, singlePinCondition, 400 );
92  selToolMenu.AddItem( EE_ACTIONS::pushPinNameSize, singlePinCondition, 400 );
93  selToolMenu.AddItem( EE_ACTIONS::pushPinNumSize, singlePinCondition, 400 );
94 
95  return true;
96 }
97 
98 
100 {
101  aPin->EnableEditMode( true, !m_frame->SynchronizePins() );
102 
103  DIALOG_LIB_EDIT_PIN dlg( m_frame, aPin );
104 
105  if( dlg.ShowModal() == wxID_CANCEL )
106  {
107  return false;
108  }
109 
110  m_frame->RefreshItem( aPin );
111  m_frame->OnModify( );
112 
113  MSG_PANEL_ITEMS items;
114  aPin->GetMsgPanelInfo( m_frame->GetUserUnits(), items );
115  m_frame->SetMsgPanel( items );
116 
117  aPin->EnableEditMode( false );
118 
119  // Save the pin properties to use for the next new pin.
123  g_LastPinLength = aPin->GetLength();
124  g_LastPinShape = aPin->GetShape();
125  g_LastPinType = aPin->GetType();
126  g_LastPinCommonConvert = aPin->GetConvert() == 0;
127  g_LastPinCommonUnit = aPin->GetUnit() == 0;
128  g_LastPinVisible = aPin->IsVisible();
129 
130  return true;
131 }
132 
133 
135 {
136  LIB_PART* part = m_frame->GetCurPart();
137  bool ask_for_pin = true; // Test for another pin in same position in other units
138 
139  for( LIB_PIN* test = part->GetNextPin(); test; test = part->GetNextPin( test ) )
140  {
141  if( test == aPin || aPin->GetPosition() != test->GetPosition() || test->GetEditFlags() )
142  continue;
143 
144  // test for same body style
145  if( test->GetConvert() && test->GetConvert() != aPin->GetConvert() )
146  continue;
147 
148  if( ask_for_pin && m_frame->SynchronizePins() )
149  {
150  wxString msg;
151  msg.Printf( _( "This position is already occupied by another pin, in unit %d." ),
152  test->GetUnit() );
153 
154  KIDIALOG dlg( m_frame, msg, _( "Confirmation" ), wxOK | wxCANCEL | wxICON_WARNING );
155  dlg.SetOKLabel( _( "Place Pin Anyway" ) );
156  dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
157 
158  bool status = dlg.ShowModal() == wxID_OK;
159 
160  if( !status )
161  {
162  if( aPin->IsNew() )
163  delete aPin;
164 
165  return false;
166  }
167  else
168  {
169  ask_for_pin = false;
170  }
171  }
172  }
173 
174  if( aPin->IsNew() && !aPin->HasFlag( IS_PASTED ) )
175  {
177  g_LastPinType = aPin->GetType();
178  g_LastPinShape = aPin->GetShape();
179 
180  if( m_frame->SynchronizePins() )
181  CreateImagePins( aPin );
182 
183  part->AddDrawItem( aPin );
184  aPin->ClearFlags( IS_NEW );
185  }
186 
187  // Put linked pins in new position, and clear flags
188  for( LIB_PIN* pin = part->GetNextPin(); pin; pin = part->GetNextPin( pin ) )
189  {
190  if( ( pin->GetEditFlags() & IS_LINKED ) == 0 )
191  continue;
192 
193  pin->MoveTo( aPin->GetPosition() );
194  pin->ClearFlags();
195  }
196 
197  m_frame->RebuildView();
198  m_frame->OnModify();
199 
200  return true;
201 }
202 
203 
204 /*
205  * Create a new pin.
206  */
207 LIB_PIN* LIB_PIN_TOOL::CreatePin( const VECTOR2I& aPosition, LIB_PART* aPart )
208 {
209  aPart->ClearTempFlags();
210 
211  LIB_PIN* pin = new LIB_PIN( aPart );
212 
213  pin->SetFlags( IS_NEW );
214 
215  // Flag pins to consider
216  if( m_frame->SynchronizePins() )
217  pin->SetFlags( IS_LINKED );
218 
219  pin->MoveTo((wxPoint) aPosition );
220  pin->SetLength( GetLastPinLength() );
222  pin->SetType( g_LastPinType );
223  pin->SetShape( g_LastPinShape );
227  pin->SetUnit( g_LastPinCommonUnit ? 0 : m_frame->GetUnit() );
229 
230  if( !EditPinProperties( pin ) )
231  {
232  delete pin;
233  pin = nullptr;
234  }
235 
236  return pin;
237 }
238 
239 
241 {
242  int ii;
243  LIB_PIN* newPin;
244 
245  // if "synchronize pins editing" option is off, do not create any similar pin for other
246  // units and/or shapes: each unit is edited regardless other units or body
247  if( !m_frame->SynchronizePins() )
248  return;
249 
250  if( aPin->GetUnit() == 0 ) // Pin common to all units: no need to create similar pins.
251  return;
252 
253  // When units are interchangeable, all units are expected to have similar pins
254  // at the same position
255  // to facilitate pin editing, create pins for all other units for the current body style
256  // at the same position as aPin
257 
258  for( ii = 1; ii <= aPin->GetParent()->GetUnitCount(); ii++ )
259  {
260  if( ii == aPin->GetUnit() )
261  continue;
262 
263  newPin = (LIB_PIN*) aPin->Clone();
264 
265  // To avoid mistakes, gives this pin a new pin number because
266  // it does no have the save pin number as the master pin
267  // Because we do not know the actual number, give it a temporary number
268  wxString unknownNum;
269  unknownNum.Printf( "%s-U%c", aPin->GetNumber(), wxChar( 'A' + ii - 1 ) );
270  newPin->SetNumber( unknownNum );
271 
272  newPin->SetUnit( ii );
273  aPin->GetParent()->AddDrawItem( newPin );
274  }
275 }
276 
277 
279 {
280  LIB_PART* part = m_frame->GetCurPart();
281  EE_SELECTION& selection = m_selectionTool->GetSelection();
282  LIB_PIN* sourcePin = dynamic_cast<LIB_PIN*>( selection.Front() );
283 
284  if( !sourcePin )
285  return 0;
286 
288 
289  for( LIB_PIN* pin = part->GetNextPin(); pin; pin = part->GetNextPin( pin ) )
290  {
291  if( pin->GetConvert() && pin->GetConvert() != m_frame->GetConvert() )
292  continue;
293 
294  if( pin == sourcePin )
295  continue;
296 
297  if( aEvent.IsAction( &EE_ACTIONS::pushPinLength ) )
298  pin->SetLength( sourcePin->GetLength() );
299  else if( aEvent.IsAction( &EE_ACTIONS::pushPinNameSize ) )
300  pin->SetNameTextSize( sourcePin->GetNameTextSize() );
301  else if( aEvent.IsAction( &EE_ACTIONS::pushPinNumSize ) )
302  pin->SetNumberTextSize( sourcePin->GetNumberTextSize() );
303  }
304 
305  m_frame->RebuildView();
306  m_frame->OnModify();
307 
308  return 0;
309 }
310 
311 
312 // Create a new pin based on the previous pin with an incremented pin number.
314 {
315  LIB_PIN* pin = (LIB_PIN*) aSourcePin->Clone();
316  wxPoint step;
317 
318  pin->ClearFlags();
319  pin->SetFlags( IS_NEW );
320 
321  switch( pin->GetOrientation() )
322  {
323  case PIN_UP: step.x = m_frame->GetRepeatPinStep(); break;
324  case PIN_DOWN: step.x = m_frame->GetRepeatPinStep(); break;
325  case PIN_LEFT: step.y = -m_frame->GetRepeatPinStep(); break;
326  case PIN_RIGHT: step.y = -m_frame->GetRepeatPinStep(); break;
327  }
328 
329  pin->Offset( step );
330 
331  wxString nextName = pin->GetName();
333  pin->SetName( nextName );
334 
335  wxString nextNumber = pin->GetNumber();
337  pin->SetNumber( nextNumber );
338 
339  if( m_frame->SynchronizePins() )
340  pin->SetFlags( IS_LINKED );
341 
342  if( PlacePin( pin ) )
343  return pin;
344 
345  return nullptr;
346 }
347 
348 
350 {
354 }
355 
#define IS_LINKED
Used in calculation to mark linked items (temporary use)
Definition: base_struct.h:117
void SetLength(int aLength, bool aTestOtherPins=true)
Set the pin length.
Definition: lib_pin.cpp:352
EDA_ITEM * Clone() const override
Function Clone creates a duplicate of this item with linked list members set to NULL.
Definition: lib_pin.cpp:1235
void DoNotShowCheckbox(wxString file, int line)
Shows the 'do not show again' checkbox
Definition: confirm.cpp:53
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:207
static bool g_LastPinCommonConvert
LIB_PART * GetParent() const
Definition: lib_item.h:185
void SetShape(GRAPHIC_PINSHAPE aShape)
Set the shape of the pin to aShape.
Definition: lib_pin.cpp:297
GRAPHIC_PINSHAPE GetShape() const
Definition: lib_pin.h:219
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:160
Implementing DIALOG_LIB_EDIT_PIN_BASE.
TOOL_MENU & GetToolMenu()
static int GetLastPinLength()
static int g_LastPinNameSize
static int g_LastPinNumSize
LIB_PART * GetCurPart()
Return the current part being edited or NULL if none selected.
Definition: lib_pin.h:58
void EnableEditMode(bool aEnable, bool aEditPinByPin=false)
Enable or clear pin editing mode.
Definition: lib_pin.cpp:503
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:67
bool IsNew() const
Definition: base_struct.h:220
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:221
static int GetPinNumDefaultSize()
int GetUnit() const
Definition: lib_item.h:309
GRAPHIC_PINSHAPE
Definition: pin_shape.h:35
static int g_LastPinOrient
int GetUnit() const
void SetFlags(STATUS_FLAGS aMask)
Definition: base_struct.h:257
static int g_LastPinLength
int GetUnitCount() const override
For items with units, return the number of units.
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:152
TOOL_EVENT.
Definition: tool_event.h:171
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:470
static TOOL_ACTION pushPinLength
Definition: ee_actions.h:187
EE_SELECTION_TOOL * m_selectionTool
Definition: ee_tool_base.h:153
void IncrementLabelMember(wxString &name, int aIncrement)
Definition: edit_label.cpp:281
bool IsVisible() const
Return the visibility status of the draw object.
Definition: lib_pin.h:346
int GetNameTextSize() const
Definition: lib_pin.h:178
int GetConvert() const
Definition: lib_item.h:312
LIB_PIN * RepeatPin(const LIB_PIN *aSourcePin)
static int GetLastPinNameSize()
void CreateImagePins(LIB_PIN *aPin)
void GetMsgPanelInfo(EDA_UNITS aUnits, std::vector< MSG_PANEL_ITEM > &aList) override
Display basic info (type, part and convert) about the current item in message panel.
void AddSeparator(int aOrder=ANY_ORDER)
Function AddSeparator()
void SetConvert(int aConvert)
Definition: lib_item.h:311
wxPoint GetPosition() const override
Definition: lib_pin.h:432
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:180
ELECTRICAL_PINTYPE GetType() const
Get the electrical type of the pin.
Definition: lib_pin.h:235
void SetUnit(int aUnit)
Definition: lib_item.h:308
void MoveTo(const wxPoint &aPosition) override
Move a draw object to aPosition.
Definition: lib_pin.cpp:1310
static int GetLastPinNumSize()
int GetNumberTextSize() const
Definition: lib_pin.h:205
The symbol library editor main window.
const char * name
Definition: DXF_plotter.cpp:60
static int GetPinNameDefaultSize()
ELECTRICAL_PINTYPE
The component library pin object electrical types used in ERC tests.
Definition: pin_type.h:37
void RefreshItem(EDA_ITEM *aItem, bool isAddOrDelete=false)
Mark an item for refresh.
#define _(s)
Definition: 3d_actions.cpp:33
usual pin input: must be connected
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:324
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:95
void ClearFlags(STATUS_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: base_struct.h:258
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:268
EE_TOOL_BASE.
Definition: ee_tool_base.h:50
bool HasFlag(STATUS_FLAGS aFlag)
Definition: base_struct.h:260
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:193
void Offset(const wxPoint &aOffset) override
Set the drawing object by aOffset from the current position.
Definition: lib_pin.cpp:1296
int GetLength()
Definition: lib_pin.h:285
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
EDA_UNITS GetUserUnits() const
Return the user units currently in use.
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:183
void SetNumberTextSize(int aSize, bool aTestOtherPins=true)
Set the size of the pin number text.
Definition: lib_pin.cpp:240
static bool g_LastPinVisible