KiCad PCB EDA Suite
libfield.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) 2007 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2007-2017 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 <class_drawpanel.h>
28 #include <confirm.h>
29 
30 #include <sch_component.h>
31 #include <lib_edit_frame.h>
32 #include <class_library.h>
33 #include <symbol_lib_table.h>
34 #include <template_fieldnames.h>
35 #include <dialog_edit_one_field.h>
36 
37 #include <lib_manager.h>
38 #include <widgets/cmp_tree_pane.h>
39 #include <component_tree.h>
40 
41 
43 {
44  wxString newFieldValue;
45  wxString caption;
46 
47  if( aField == NULL )
48  return;
49 
50  LIB_PART* parent = aField->GetParent();
51  wxCHECK( parent, /* void */ );
52 
53  // Editing the component value field is equivalent to creating a new component based
54  // on the current component. Set the dialog message to inform the user.
55  if( aField->GetId() == VALUE )
56  caption = _( "Component Name" );
57  else
58  caption.Printf( _( "Edit Field %s" ), GetChars( aField->GetName() ) );
59 
60  DIALOG_LIB_EDIT_ONE_FIELD dlg( this, caption, aField );
61 
62  // The dialog may invoke a kiway player for footprint fields
63  // so we must use a quasimodal dialog.
64  if( dlg.ShowQuasiModal() != wxID_OK )
65  return;
66 
67  newFieldValue = dlg.GetText();
68  LIB_ALIAS::ValidateName( newFieldValue );
69  wxString oldFieldValue = aField->GetFullText( m_unit );
70  bool renamed = aField->GetId() == VALUE && newFieldValue != oldFieldValue;
71 
72  if( renamed )
73  {
74  wxString msg;
75  wxString lib = GetCurLib();
76 
77  // Test the current library for name conflicts
78  if( !lib.empty() && m_libMgr->PartExists( newFieldValue, lib ) )
79  {
80  msg.Printf( _(
81  "The name \"%s\" conflicts with an existing entry in the symbol library \"%s\"." ),
82  newFieldValue, lib );
83 
84  DisplayErrorMessage( this, msg );
85  return;
86  }
87 
89  parent->SetName( newFieldValue );
90 
91  if( !parent->HasAlias( m_aliasName ) )
92  m_aliasName = newFieldValue;
93 
94  m_libMgr->UpdatePartAfterRename( parent, oldFieldValue, lib );
95 
96  // Reselect the renamed part
97  m_treePane->GetCmpTree()->SelectLibId( LIB_ID( lib, newFieldValue ) );
98  }
99 
100  if( !aField->InEditMode() && !renamed )
101  SaveCopyInUndoList( parent );
102 
103  dlg.UpdateField( aField );
104  m_canvas->Refresh();
105 
106  OnModify();
108 }
static void ValidateName(wxString &aName)
Helper function to replace illegal chars in symbol names
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Function DisplayErrorMessage displays an error message with aMessage.
Definition: confirm.cpp:199
virtual void Refresh(bool eraseBackground=true, const wxRect *rect=NULL) override
Update the board display after modifying it bu a python script (note: it is automatically called by a...
Definition: draw_panel.cpp:338
This file is part of the common library.
Field object used in symbol libraries.
Definition: lib_field.h:59
LIB_MANAGER * m_libMgr
manager taking care of temporary modificatoins
void UpdateField(LIB_FIELD *aField)
bool HasAlias(const wxString &aName) const
Test if alias aName is in part alias list.
bool InEditMode() const
Return the draw item editing mode status.
void OnModify()
Must be called after a schematic change in order to set the "modify" flag of the current screen...
wxString GetFullText(int unit=1) const
Return the text of a field.
Definition: lib_field.cpp:356
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
void UpdateAliasSelectList()
Class DIALOG_LIB_EDIT_ONE_FIELD is a the class to handle editing a single component field in the libr...
LIB_PART * GetParent() const
int GetId() const
Definition: lib_field.h:138
int ShowQuasiModal()
Define a library symbol object.
wxString GetCurLib() const
The nickname of the current library being edited and empty string if none.
const wxString & GetText() const
wxString GetName(bool aTranslate=true) const
Returns the field name.
Definition: lib_field.cpp:432
void SelectLibId(const LIB_ID &aLibId)
Select a part in the tree widget.
EDA_DRAW_PANEL * m_canvas
The area to draw on.
Definition: draw_frame.h:98
Definition the SCH_COMPONENT class for Eeschema.
bool PartExists(const wxString &aAlias, const wxString &aLibrary) const
Returns true if part with a specific alias exists in library (either original one or buffered)...
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
static wxString m_aliasName
static int m_unit
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.
virtual void SetName(const wxString &aName)
Definition of class LIB_EDIT_FRAME.
void EditField(LIB_FIELD *Field)
Definition: libfield.cpp:42
COMPONENT_TREE * GetCmpTree() const
Definition: cmp_tree_pane.h:46
Definition for part library class.
#define VALUE
CMP_TREE_PANE * m_treePane
component search tree widget
bool UpdatePartAfterRename(LIB_PART *aPart, const wxString &oldAlias, const wxString &aLibrary)
Updates the part buffer with a new version of the part when the name has changed. ...