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 #include <class_sch_screen.h>
30 
31 #include <general.h>
32 #include <sch_component.h>
33 #include <libeditframe.h>
34 #include <class_library.h>
35 #include <symbol_lib_table.h>
36 #include <template_fieldnames.h>
37 #include <dialog_edit_one_field.h>
38 #include <lib_manager.h>
39 
40 
42 {
43  wxString newFieldValue;
44  wxString title;
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  {
57  caption = _( "Component Name" );
58  title = _( "Enter a name to create a new component based on this one." );
59  }
60  else
61  {
62  caption.Printf( _( "Edit Field %s" ), GetChars( aField->GetName() ) );
63  title.Printf( _( "Enter a new value for the %s field." ),
64  GetChars( aField->GetName().Lower() ) );
65  }
66 
67  DIALOG_LIB_EDIT_ONE_FIELD dlg( this, caption, aField );
68 
69  // The dialog may invoke a kiway player for footprint fields
70  // so we must use a quasimodal dialog.
71  if( dlg.ShowQuasiModal() != wxID_OK )
72  return;
73 
74  newFieldValue = dlg.GetText();
75  wxString fieldText = aField->GetFullText( m_unit );
76 
77  bool creatingNewComponent = aField->GetId() == VALUE && newFieldValue != aField->GetText();
78 
79  /* If the value field is changed, this is equivalent to creating a new component from
80  * the old one. Rename the component and remove any conflicting aliases to prevent name
81  * errors when updating the library.
82  */
83  if( creatingNewComponent )
84  {
85  wxString msg;
86  wxString lib = GetCurLib();
87 
88  // Test the current library for name conflicts.
89  if( !lib.empty() && m_libMgr->PartExists( newFieldValue, lib ) )
90  {
91  msg.Printf( _(
92  "The name '%s' conflicts with an existing entry in the component library '%s'.\n\n"
93  "Do you wish to replace the current component in the library with this one?" ),
94  newFieldValue,
95  lib
96  );
97 
98  int rsp = wxMessageBox( msg, _( "Confirm" ),
99  wxYES_NO | wxICON_QUESTION | wxNO_DEFAULT, this );
100 
101  if( rsp == wxNO )
102  return;
103  }
104 
105  // Test the current component for name conflicts.
106  if( parent->HasAlias( newFieldValue ) )
107  {
108  msg.Printf( _( "The current component already has an alias named '%s'.\n\n"
109  "Do you wish to remove this alias from the component?" ),
110  GetChars( newFieldValue ) );
111 
112  int rsp = wxMessageBox( msg, _( "Confirm" ), wxYES_NO | wxICON_QUESTION, this );
113 
114  if( rsp == wxNO )
115  return;
116 
117  parent->RemoveAlias( newFieldValue );
118  }
119 
120  parent->SetName( newFieldValue );
121 
122  // Test the library for any conflicts with the any aliases in the current component.
123  if( parent->GetAliasCount() > 1 && !lib.empty() )
124  {
125  bool conflicts = false;
126  wxArrayString libAliasNames, symbolAliasNames;
127 
128  libAliasNames = m_libMgr->GetAliasNames( lib );
129  symbolAliasNames = parent->GetAliasNames();
130 
131  for( size_t i = 0; i < symbolAliasNames.GetCount(); i++ )
132  {
133  if( libAliasNames.Index( symbolAliasNames[i] ) != wxNOT_FOUND )
134  {
135  conflicts = true;
136  break;
137  }
138  }
139 
140  if( conflicts )
141  {
142  msg.Printf( _( "The new symbol contains alias names that conflict with "
143  "entries in the library '%s'.\n\n"
144  "Do you wish to remove all of the conflicting aliases from "
145  "this symbol?" ),
146  lib );
147 
148  int rsp = wxMessageBox( msg, _( "Confirm" ), wxYES_NO | wxICON_QUESTION, this );
149 
150  if( rsp == wxNO )
151  {
152  parent->SetName( fieldText );
153  return;
154  }
155 
156  wxArrayString aliases = parent->GetAliasNames( false );
157 
158  for( size_t i = 0; i < aliases.GetCount(); i++ )
159  {
160  if( m_libMgr->GetAlias( aliases[i], lib ) != NULL )
161  parent->RemoveAlias( aliases[ i ] );
162  }
163  }
164  }
165 
166  if( !parent->HasAlias( m_aliasName ) )
167  m_aliasName = newFieldValue;
168 
169  //m_libMgr->RemovePart( fieldText, lib );
170  m_libMgr->UpdatePart( parent, lib, fieldText );
171  }
172 
173 
174  if( !aField->InEditMode() && !creatingNewComponent )
175  {
176  SaveCopyInUndoList( parent );
177  }
178 
179  dlg.UpdateField( aField );
180  m_canvas->Refresh();
181 
182  OnModify();
184 }
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:325
This file is part of the common library.
Class LIB_FIELD is used in symbol libraries.
Definition: lib_field.h:60
LIB_MANAGER * m_libMgr
manager taking care of temporary modificatoins
Definition: libeditframe.h:66
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:529
size_t GetAliasCount() const
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
bool UpdatePart(LIB_PART *aPart, const wxString &aLibrary, wxString aOldName=wxEmptyString)
Updates the part buffer with a new version of the part.
int GetId() const
Definition: lib_field.h:136
Definitions for the Eeschema program SCH_SCREEN class.
int ShowQuasiModal()
const wxString & GetText() const
Function GetText returns the string associated with the text object.
Definition: eda_text.h:130
Class LIB_PART defines a library part object.
wxArrayString GetAliasNames(const wxString &aLibrary) const
Returns a set containing all part names for a specific library.
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:605
Field Value of part, i.e. "3.3K".
EDA_DRAW_PANEL * m_canvas
The area to draw on.
Definition: draw_frame.h:93
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
void RemoveAlias(const wxString &aName)
static wxString m_aliasName
Definition: libeditframe.h:101
static int m_unit
Definition: libeditframe.h:104
wxArrayString GetAliasNames(bool aIncludeRoot=true) const
LIB_ALIAS * GetAlias(const wxString &aAlias, const wxString &aLibrary) const
Returns either an alias of a working LIB_PART copy, or alias of the original part if there is no work...
virtual void SetName(const wxString &aName)
void EditField(LIB_FIELD *Field)
Definition: libfield.cpp:41
Definition for part library class.
Definition of class LIB_EDIT_FRAME.
void SaveCopyInUndoList(EDA_ITEM *ItemToCopy)
Create a copy of the current component, and save it in the undo list.