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 <sch_draw_panel.h>
28 #include <confirm.h>
29 #include <sch_view.h>
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>
39 #include <widgets/lib_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 = _( "Edit Component Name" );
57  else
58  caption.Printf( _( "Edit %s Field" ), 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 = LIB_ID::FixIllegalChars( dlg.GetText(), LIB_ID::ID_SCH );
68  wxString oldFieldValue = aField->GetFullText( m_unit );
69  bool renamed = aField->GetId() == VALUE && newFieldValue != oldFieldValue;
70 
71  if( renamed )
72  {
73  wxString msg;
74  wxString lib = GetCurLib();
75 
76  // Test the current library for name conflicts
77  if( !lib.empty() && m_libMgr->PartExists( newFieldValue, lib ) )
78  {
79  msg.Printf( _(
80  "The name \"%s\" conflicts with an existing entry in the symbol library \"%s\"." ),
81  newFieldValue, lib );
82 
83  DisplayErrorMessage( this, msg );
84  return;
85  }
86 
88  parent->SetName( newFieldValue );
89 
90  m_libMgr->UpdatePartAfterRename( parent, oldFieldValue, lib );
91 
92  // Reselect the renamed part
93  m_treePane->GetLibTree()->SelectLibId( LIB_ID( lib, newFieldValue ) );
94  }
95 
96  if( !aField->InEditMode() && !renamed )
97  SaveCopyInUndoList( parent );
98 
99  dlg.UpdateField( aField );
100 
101  GetCanvas()->GetView()->Update( aField );
102  GetCanvas()->Refresh();
103  OnModify();
104 }
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Function DisplayErrorMessage displays an error message with aMessage.
Definition: confirm.cpp:259
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 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...
void Refresh(bool aEraseBackground=true, const wxRect *aRect=NULL) override
Update the board display after modifying it by a python script (note: it is automatically called by a...
wxString GetFullText(int unit=1) const
Return the text of a field.
Definition: lib_field.cpp:363
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
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()
SCH_DRAW_PANEL * GetCanvas() const override
Define a library symbol object.
wxString GetCurLib() const
The nickname of the current library being edited and empty string if none.
virtual void Update(VIEW_ITEM *aItem, int aUpdateFlags)
For dynamic VIEWs, informs the associated VIEW that the graphical representation of this item has cha...
Definition: view.cpp:1521
KIGFX::SCH_VIEW * GetView() const
const wxString & GetText() const
wxString GetName(bool aTranslate=true) const
Returns the field name.
Definition: lib_field.cpp:440
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 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.
LIB_TREE * GetLibTree() const
static UTF8 FixIllegalChars(const UTF8 &aLibItemName, LIB_ID_TYPE aType, bool aLib=false)
Replace illegal LIB_ID item name characters with underscores &#39;_&#39;.
Definition: lib_id.cpp:352
virtual void SetName(const wxString &aName)
Definition of class LIB_EDIT_FRAME.
void EditField(LIB_FIELD *Field)
Definition: libfield.cpp:42
Definition for part library class.
SYMBOL_TREE_PANE * m_treePane
component search tree widget
#define VALUE
void SelectLibId(const LIB_ID &aLibId)
Select an item in the tree widget.
Definition: lib_tree.cpp:162
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. ...