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-2016 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 <template_fieldnames.h>
36 #include <dialog_edit_one_field.h>
37 
38 
40 {
41  wxString newFieldValue;
42  wxString title;
43  wxString caption;
44  wxString oldName;
45 
46  if( aField == NULL )
47  return;
48 
49  LIB_PART* parent = aField->GetParent();
50 
51  wxASSERT( parent );
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  /* If the value field is changed, this is equivalent to creating a new component from
78  * the old one. Rename the component and remove any conflicting aliases to prevent name
79  * errors when updating the library.
80  */
81  if( aField->GetId() == VALUE && newFieldValue != aField->GetText() )
82  {
83  wxString msg;
84 
85  PART_LIB* lib = GetCurLib();
86 
87  // Test the current library for name conflicts.
88  if( lib && lib->FindAlias( newFieldValue ) )
89  {
90  msg.Printf( _(
91  "The name '%s' conflicts with an existing entry in the component library '%s'.\n\n"
92  "Do you wish to replace the current component in the library with this one?" ),
93  GetChars( newFieldValue ),
94  GetChars( lib->GetName() )
95  );
96 
97  int rsp = wxMessageBox( msg, _( "Confirm" ),
98  wxYES_NO | wxICON_QUESTION | wxNO_DEFAULT, this );
99 
100  if( rsp == wxNO )
101  return;
102  }
103 
104  // Test the current component for name conflicts.
105  if( parent->HasAlias( newFieldValue ) )
106  {
107  msg.Printf( _( "The current component already has an alias named '%s'.\n\n"
108  "Do you wish to remove this alias from the component?" ),
109  GetChars( newFieldValue ) );
110 
111  int rsp = wxMessageBox( msg, _( "Confirm" ), wxYES_NO | wxICON_QUESTION, this );
112 
113  if( rsp == wxNO )
114  return;
115 
116  parent->RemoveAlias( newFieldValue );
117  }
118 
119  parent->SetName( newFieldValue );
120 
121  // Test the library for any conflicts with the any aliases in the current component.
122  if( parent->GetAliasCount() > 1 && lib )
123  {
124  bool conflicts = false;
125  wxArrayString libAliasNames, symbolAliasNames;
126 
127  lib->GetAliasNames( libAliasNames );
128  symbolAliasNames = parent->GetAliasNames();
129 
130  for( size_t i = 0; i < symbolAliasNames.GetCount(); i++ )
131  {
132  if( libAliasNames.Index( symbolAliasNames[i] ) != wxNOT_FOUND )
133  {
134  conflicts = true;
135  break;
136  }
137  }
138 
139  if( conflicts )
140  {
141  msg.Printf( _( "The new component contains alias names that conflict with "
142  "entries in the component library '%s'.\n\n"
143  "Do you wish to remove all of the conflicting aliases from "
144  "this component?" ),
145  GetChars( lib->GetName() )
146  );
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( lib->FindAlias( aliases[ i ] ) != NULL )
161  parent->RemoveAlias( aliases[ i ] );
162  }
163  }
164  }
165 
166  if( !parent->HasAlias( m_aliasName ) )
167  m_aliasName = newFieldValue;
168  }
169 
170  dlg.UpdateField( aField );
171 
172  if( !aField->InEditMode() )
173  SaveCopyInUndoList( parent );
174 
175  m_canvas->Refresh();
176 
177  OnModify();
179 }
void GetAliasNames(wxArrayString &aNames)
Load a string array with the names of all the entries in this library.
PART_LIB * GetCurLib()
The current library being edited, or NULL if none.
virtual void Refresh(bool eraseBackground=true, const wxRect *rect=NULL) override
Definition: draw_panel.cpp:326
This file is part of the common library.
Class LIB_FIELD is used in symbol libraries.
Definition: lib_field.h:60
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()
Function OnModify Must be called after a schematic change in order to set the "modify" flag of the cu...
Definition: libeditframe.h:369
wxString GetFullText(int unit=1) const
Return the text of a field.
Definition: lib_field.cpp:533
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
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
const wxString GetName() const
Return the file name without path or extension.
Class LIB_PART defines a library part object.
const wxString & GetText() const
wxString GetName(bool aTranslate=true) const
Returns the field name.
Definition: lib_field.cpp:609
Field Value of part, i.e. "3.3K".
EDA_DRAW_PANEL * m_canvas
The area to draw on.
Definition: draw_frame.h:92
Definition the SCH_COMPONENT class for Eeschema.
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:92
static int m_unit
Definition: libeditframe.h:95
wxArrayString GetAliasNames(bool aIncludeRoot=true) const
virtual void SetName(const wxString &aName)
void EditField(LIB_FIELD *Field)
Definition: libfield.cpp:39
Definition for part library class.
Class PART_LIB is used to load, save, search, and otherwise manipulate part library files...
LIB_ALIAS * FindAlias(const wxString &aName)
Find LIB_ALIAS by aName.
Definition of class LIB_EDIT_FRAME.
void SaveCopyInUndoList(EDA_ITEM *ItemToCopy)
Function SaveCopyInUndoList.