KiCad PCB EDA Suite
dialog_edit_libentry_fields_in_lib.cpp
Go to the documentation of this file.
1 
2 /*
3  * This program source code file is part of KiCad, a free EDA CAD application.
4  *
5  * Copyright (C) 2011-2013 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
6  * Copyright (C) 2007-2017 KiCad Developers, see AUTHORS.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 
27 #include <algorithm>
28 
29 #include <fctsys.h>
30 #include <pgm_base.h>
31 #include <kiway.h>
32 #include <confirm.h>
33 #include <class_drawpanel.h>
34 #include <sch_edit_frame.h>
35 #include <id.h>
36 #include <base_units.h>
37 
38 #include <general.h>
39 #include <lib_edit_frame.h>
40 #include <class_library.h>
41 #include <sch_component.h>
42 #include <sch_field.h>
43 #include <template_fieldnames.h>
44 #include <dialog_helpers.h>
45 #include <sch_validators.h>
46 
47 #include <bitmaps.h>
48 #include "eda_doc.h"
49 
51 #ifdef KICAD_SPICE
52 #include <dialog_spice_model.h>
54 #endif /* KICAD_SPICE */
55 
56 // Local variables:
57 static int s_SelectedRow;
58 
59 #define COLUMN_FIELD_NAME 0
60 #define COLUMN_TEXT 1
61 
63 {
64 public:
66  //~DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB() {}
67 
68 private:
69  // Events handlers:
70  void OnInitDialog( wxInitDialogEvent& event ) override;
71  void OnCloseDialog( wxCloseEvent& event ) override;
72  void OnSizeFieldsList( wxSizeEvent& event ) override;
73 
74  void OnListItemDeselected( wxListEvent& event ) override;
75  void OnListItemSelected( wxListEvent& event ) override;
76  void addFieldButtonHandler( wxCommandEvent& event ) override;
77  void EditSpiceModel( wxCommandEvent& event ) override;
78 
86  void deleteFieldButtonHandler( wxCommandEvent& event ) override;
87 
88  void moveUpButtonHandler( wxCommandEvent& event ) override;
89  void moveDownButtonHandler( wxCommandEvent& event ) override;
90  void OnCancelButtonClick( wxCommandEvent& event ) override;
91  void OnOKButtonClick( wxCommandEvent& event ) override;
92  void showButtonHandler( wxCommandEvent& event ) override;
93 
94  // internal functions:
95  void setSelectedFieldNdx( int aFieldNdx );
96 
97  int getSelectedFieldNdx();
98 
103  void initBuffers();
104 
110  LIB_FIELD* findField( const wxString& aFieldName );
111 
118 
126 
127  void setRowItem( int aFieldNdx, const wxString& aName, const wxString& aValue );
128  void setRowItem( int aFieldNdx, const LIB_FIELD& aField )
129  {
130  setRowItem( aFieldNdx, aField.GetName(), aField.GetText() );
131  }
132 
139  {
140  for( unsigned ii = MANDATORY_FIELDS; ii<m_FieldsBuf.size(); ii++ )
141  setRowItem( ii, m_FieldsBuf[ii] );
142  }
143 
147 
149  std::vector <LIB_FIELD> m_FieldsBuf;
150 };
151 
152 
153 void LIB_EDIT_FRAME::InstallFieldsEditorDialog( wxCommandEvent& event )
154 {
155  if( !GetCurPart() )
156  return;
157 
158  m_canvas->EndMouseCapture( ID_NO_TOOL_SELECTED, m_canvas->GetDefaultCursor() );
159 
160  DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB dlg( this, GetCurPart() );
161 
162  if( GetDrawItem() && GetDrawItem()->Type() == LIB_FIELD_T )
163  SetDrawItem( nullptr ); // selected LIB_FIELD might be deleted
164 
165  // This dialog itself subsequently can invoke a KIWAY_PLAYER as a quasimodal
166  // frame. Therefore this dialog as a modal frame parent, MUST be run under
167  // quasimodal mode for the quasimodal frame support to work. So don't use
168  // the QUASIMODAL macros here.
169  if( dlg.ShowQuasiModal() != wxID_OK )
170  return;
171 
172  UpdateAliasSelectList();
173  UpdatePartSelectList();
174  DisplayLibInfos();
175  Refresh();
176 }
177 
178 
180  LIB_EDIT_FRAME* aParent,
181  LIB_PART* aLibEntry ) :
183 {
184  m_parent = aParent;
185  m_libEntry = aLibEntry;
186  m_skipCopyFromPanel = false;
187 
188 #ifndef KICAD_SPICE
189  m_spiceFieldsButton->Show(false);
190 #endif
191 
192  // Configure button logos
193  addFieldButton->SetBitmap( KiBitmap( plus_xpm ) );
194  deleteFieldButton->SetBitmap( KiBitmap( minus_xpm ) );
195  moveUpButton->SetBitmap( KiBitmap( go_up_xpm ) );
196  moveDownButton->SetBitmap( KiBitmap( go_down_xpm ) );
197 }
198 
199 
200 void DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB::OnInitDialog( wxInitDialogEvent& event )
201 {
202  m_skipCopyFromPanel = false;
203  wxListItem columnLabel;
204 
205  columnLabel.SetImage( -1 );
206 
207  columnLabel.SetText( _( "Name" ) );
208  fieldListCtrl->InsertColumn( COLUMN_FIELD_NAME, columnLabel );
209 
210  columnLabel.SetText( _( "Value" ) );
211  fieldListCtrl->InsertColumn( COLUMN_TEXT, columnLabel );
212 
216 
217  initBuffers();
219 
220  stdDialogButtonSizerOK->SetDefault();
221 
222  // Now all widgets have the size fixed, call FinishDialogSettings
224 }
225 
226 
228 {
229  if( !m_skipCopyFromPanel )
230  {
231  if( !copyPanelToSelectedField() )
232  event.Skip(); // do not go to the next row
233  }
234 }
235 
236 
238 {
239  // remember the selected row, statically
240  s_SelectedRow = event.GetIndex();
241 
243 }
244 
245 
247 {
248  EndQuasiModal( wxID_CANCEL );
249 }
250 
251 
253 {
254  // On wxWidgets 2.8, and on Linux, call EndQuasiModal here is mandatory
255  // Otherwise, the main event loop is never restored, and Eeschema does not
256  // respond to any event, because the DIALOG_SHIM destructor is never called.
257  // on wxWidgets 3.0, or on Windows, the DIALOG_SHIM destructor is called,
258  // and calls EndQuasiModal.
259  // Therefore calling EndQuasiModal here is not mandatory but it creates no issues.
260  EndQuasiModal( wxID_CANCEL );
261 }
262 
263 
265 {
266  int newWidth = event.GetSize().GetX();
267  fieldListCtrl->SetColumnWidth( COLUMN_TEXT, newWidth - fieldListCtrl->GetColumnWidth( 0 ) );
268  event.Skip();
269 }
270 
271 
273 {
274  if( !copyPanelToSelectedField() )
275  return;
276 
277  // test if reference prefix is acceptable
279  {
280  DisplayError( NULL, _( "Illegal reference. References must start with a letter." ) );
281  return;
282  }
283 
284  /* Note: this code is now (2010-dec-04) not used, because the value field is no more editable
285  * because changing the value is equivalent to create a new component or alias.
286  * This is now handled in libedit main frame, and no more in this dialog
287  * but this code is not removed, just in case
288  */
289  /* If a new name entered in the VALUE field, that it not an existing alias name
290  * or root alias of the component */
291  wxString newvalue = m_FieldsBuf[VALUE].GetText();
292 
293  if( m_libEntry->HasAlias( newvalue ) && !m_libEntry->GetAlias( newvalue )->IsRoot() )
294  {
295  wxString msg = wxString::Format(
296  _( "A new name is entered for this component\n"
297  "An alias %s already exists!\n"
298  "Cannot update this component" ),
299  GetChars( newvalue )
300  );
301  DisplayError( this, msg );
302  return;
303  }
304  /* End unused code */
305 
306  // save old cmp in undo list
308 
309  // delete any fields with no name or no value before we copy all of m_FieldsBuf
310  // back into the component
311  for( unsigned i = MANDATORY_FIELDS; i < m_FieldsBuf.size(); )
312  {
313  if( m_FieldsBuf[i].GetName().IsEmpty() || m_FieldsBuf[i].GetText().IsEmpty() )
314  {
315  m_FieldsBuf.erase( m_FieldsBuf.begin() + i );
316  continue;
317  }
318 
319  ++i;
320  }
321 
322  // copy all the fields back, fully replacing any previous fields
324 
325  // We need to keep the name and the value the same at the moment!
326  SetName( m_libEntry->GetValueField().GetText() );
327 
328  m_parent->OnModify();
329 
330  EndQuasiModal( wxID_OK );
331 }
332 
333 
335 {
336 #ifdef KICAD_SPICE
337  // DIALOG_SPICE_MODEL expects a SCH_COMPONENT,
338  // and a list of SCH_FIELDS to create/edit/delete Spice fields.
339  SCH_COMPONENT component; // This dummy component
340 
341  // Build fields list from the m_FieldsBuf fields buffer dialog
342  // to be sure to use the current fields.
343  SCH_FIELDS schfields;
344 
345  for( unsigned ii = 0; ii < m_FieldsBuf.size(); ++ii )
346  {
347  LIB_FIELD& libfield = m_FieldsBuf[ii];
348  SCH_FIELD schfield( libfield.GetTextPos(), libfield.GetId(),
349  &component, libfield.GetName() );
350  schfield.ImportValues( m_FieldsBuf[ii] );
351  schfield.SetText( m_FieldsBuf[ii].GetText() );
352 
353  schfields.push_back( schfield );
354  }
355 
356  component.SetFields( schfields );
357 
358  DIALOG_SPICE_MODEL dialog( this, component, schfields );
359 
360  if( dialog.ShowModal() != wxID_OK )
361  return;
362 
363  // Transfer sch fields to the m_FieldsBuf fields buffer dialog:
364  m_FieldsBuf.clear();
365 
366  for( unsigned ii = 0; ii < schfields.size(); ii++ )
367  {
368  LIB_FIELD libfield;
369  schfields[ii].ExportValues( libfield );
370  m_FieldsBuf.push_back( libfield );
371  }
372 
373  // ... and to the panel:
375 
376  updateDisplay();
377 #endif /* KICAD_SPICE */
378 }
379 
380 
382 {
383  // in case m_FieldsBuf[REFERENCE].m_Orient has changed on screen only, grab
384  // screen contents.
385  if( !copyPanelToSelectedField() )
386  return;
387 
388  unsigned fieldNdx = m_FieldsBuf.size();
389 
390  LIB_FIELD blank( fieldNdx );
391 
392  m_FieldsBuf.push_back( blank );
393  m_FieldsBuf[fieldNdx].SetName( TEMPLATE_FIELDNAME::GetDefaultFieldName( fieldNdx ) );
394 
395  setRowItem( fieldNdx, m_FieldsBuf[fieldNdx] );
396 
397  m_skipCopyFromPanel = true;
398  setSelectedFieldNdx( fieldNdx );
399  m_skipCopyFromPanel = false;
400 }
401 
402 
404 {
405  unsigned fieldNdx = getSelectedFieldNdx();
406 
407  if( fieldNdx >= m_FieldsBuf.size() ) // traps the -1 case too
408  return;
409 
410  if( fieldNdx < MANDATORY_FIELDS )
411  {
412  wxBell();
413  return;
414  }
415 
416  m_skipCopyFromPanel = true;
417 
418  if( m_FieldsBuf[fieldNdx].GetText().IsEmpty() )
419  {
420  m_FieldsBuf.erase( m_FieldsBuf.begin() + fieldNdx );
421  fieldListCtrl->DeleteItem( fieldNdx );
422 
423  if( fieldNdx >= m_FieldsBuf.size() )
424  --fieldNdx;
425  }
426  else
427  {
428  m_FieldsBuf[fieldNdx].Empty();
430  }
431 
432  updateDisplay( );
433 
434  setRowItem( fieldNdx, m_FieldsBuf[fieldNdx] );
435  setSelectedFieldNdx( fieldNdx );
436  m_skipCopyFromPanel = false;
437 }
438 
439 
441 {
442  unsigned int fieldNdx = getSelectedFieldNdx();
443 
444  // Ensure there is at least one field after this one
445  if( fieldNdx >= ( m_FieldsBuf.size() - 1 ) )
446  {
447  return;
448  }
449 
450  // The first field which can be moved up is the second user field
451  // so any field which id < MANDATORY_FIELDS cannot be moved down
452  if( fieldNdx < MANDATORY_FIELDS )
453  return;
454 
455  if( !copyPanelToSelectedField() )
456  return;
457 
458  // swap the fieldNdx field with the one before it, in both the vector
459  // and in the fieldListCtrl
460  LIB_FIELD tmp = m_FieldsBuf[fieldNdx + 1];
461 
462  m_FieldsBuf[fieldNdx + 1] = m_FieldsBuf[fieldNdx];
463  setRowItem( fieldNdx + 1, m_FieldsBuf[fieldNdx] );
464  m_FieldsBuf[fieldNdx + 1].SetId( fieldNdx + 1 );
465 
466  m_FieldsBuf[fieldNdx] = tmp;
467  setRowItem( fieldNdx, tmp );
468  m_FieldsBuf[fieldNdx].SetId( fieldNdx );
469 
470  updateDisplay( );
471 
472  m_skipCopyFromPanel = true;
473  setSelectedFieldNdx( fieldNdx + 1 );
474  m_skipCopyFromPanel = false;
475 }
476 
477 
479 {
480  unsigned fieldNdx = getSelectedFieldNdx();
481 
482  if( fieldNdx >= m_FieldsBuf.size() ) // traps the -1 case too
483  return;
484 
485  // The first field which can be moved up is the second user field
486  // so any field which id <= MANDATORY_FIELDS cannot be moved up
487  if( fieldNdx <= MANDATORY_FIELDS )
488  return;
489 
490  if( !copyPanelToSelectedField() )
491  return;
492 
493  // swap the fieldNdx field with the one before it, in both the vector
494  // and in the fieldListCtrl
495  LIB_FIELD tmp = m_FieldsBuf[fieldNdx - 1];
496 
497  m_FieldsBuf[fieldNdx - 1] = m_FieldsBuf[fieldNdx];
498  setRowItem( fieldNdx - 1, m_FieldsBuf[fieldNdx] );
499  m_FieldsBuf[fieldNdx - 1].SetId(fieldNdx - 1);
500 
501  m_FieldsBuf[fieldNdx] = tmp;
502  setRowItem( fieldNdx, tmp );
503  m_FieldsBuf[fieldNdx].SetId(fieldNdx);
504 
505  updateDisplay( );
506 
507  m_skipCopyFromPanel = true;
508  setSelectedFieldNdx( fieldNdx - 1 );
509  m_skipCopyFromPanel = false;
510 }
511 
512 
514 {
515  unsigned fieldNdx = getSelectedFieldNdx();
516 
517  if( fieldNdx == DATASHEET )
518  {
519  wxString datasheet_uri = fieldValueTextCtrl->GetValue();
520  datasheet_uri = ResolveUriByEnvVars( datasheet_uri );
521  GetAssociatedDocument( this, datasheet_uri );
522  }
523  else if( fieldNdx == FOOTPRINT )
524  {
525  // pick a footprint using the footprint picker.
526  wxString fpid;
527 
528  KIWAY_PLAYER* frame = Kiway().Player( FRAME_PCB_MODULE_VIEWER_MODAL, true, this );
529 
530  if( frame->ShowModal( &fpid, this ) )
531  {
532  fieldValueTextCtrl->SetValue( fpid );
533  setRowItem( fieldNdx, m_FieldsBuf[fieldNdx].GetName( false ), fpid );
534  }
535 
536  frame->Destroy();
537  }
538 }
539 
540 
542 {
543  // deselect old selection, but I think this is done by single selection
544  // flag within fieldListCtrl
545  // fieldListCtrl->SetItemState( s_SelectedRow, 0, wxLIST_STATE_SELECTED|wxLIST_STATE_FOCUSED);
546 
547  if( aFieldNdx >= (int) m_FieldsBuf.size() )
548  aFieldNdx = m_FieldsBuf.size() - 1;
549 
550  if( aFieldNdx < 0 )
551  aFieldNdx = 0;
552 
553  fieldListCtrl->SetItemState( aFieldNdx, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
554  fieldListCtrl->EnsureVisible( aFieldNdx );
555 
556  s_SelectedRow = aFieldNdx;
557 }
558 
559 
561 {
562  return s_SelectedRow;
563 }
564 
565 
570 static LIB_FIELD* findfield( const LIB_FIELDS& aList, const wxString& aFieldName )
571 {
572  const LIB_FIELD* field = NULL;
573 
574  for( unsigned i=0; i<aList.size(); ++i )
575  {
576  if( aFieldName == aList[i].GetName() )
577  {
578  field = &aList[i]; // best to avoid casting here.
579  break;
580  }
581  }
582  return (LIB_FIELD*) field; // remove const-ness last
583 }
584 
585 
587 {
588  for( unsigned i=0; i<m_FieldsBuf.size(); ++i )
589  {
590  if( aFieldName == m_FieldsBuf[i].GetName() )
591  return &m_FieldsBuf[i];
592  }
593  return NULL;
594 }
595 
596 
598 {
599  LIB_FIELDS cmpFields;
600 
601  m_libEntry->GetFields( cmpFields );
602 
603  /* We have 3 component related field lists to be aware of: 1) UI
604  presentation (m_FieldsBuf), 2) fields in component ram copy, and 3)
605  fields recorded with component on disk. m_FieldsBuf is the list of UI
606  fields, and this list is not the same as the list which is in the
607  component, which is also not the same as the list on disk. All 3 lists
608  are potentially different. In the UI we choose to preserve the order of
609  the first MANDATORY_FIELDS which are sometimes called fixed fields. Then
610  we append the template fieldnames in the exact same order as the
611  template fieldname editor shows them. Then we append any user defined
612  fieldnames which came from the component, and user can modify it during
613  editing, but cannot delete or move a fixed field.
614  */
615 
616  m_FieldsBuf.clear();
617 
618  /* When this code was written, all field constructors ensured that the
619  MANDATORY_FIELDS are all present within a component (in ram only). So we can
620  knowingly copy them over in the normal order. Copy only the fixed fields
621  at first. Please do not break the field constructors.
622  */
623 
624  // fixed fields:
625  for( int i=0; i<MANDATORY_FIELDS; ++i )
626  {
627  m_FieldsBuf.push_back( cmpFields[i] );
628  }
629 
630  // Add template fieldnames:
631  // Now copy in the template fields, in the order that they are present in the
632  // template field editor UI.
633  SCH_EDIT_FRAME* editor = (SCH_EDIT_FRAME*) Kiway().Player( FRAME_SCH, true );
634 
635  const TEMPLATE_FIELDNAMES& tfnames = editor->GetTemplateFieldNames();
636 
637  for( TEMPLATE_FIELDNAMES::const_iterator it = tfnames.begin(); it!=tfnames.end(); ++it )
638  {
639  // add a new field unconditionally to the UI only for this template fieldname
640 
641  // field id must not be in range 0 - MANDATORY_FIELDS, set before saving to disk
642  LIB_FIELD fld( m_libEntry, -1 );
643 
644  // See if field by same name already exists in component.
645  LIB_FIELD* libField = findfield( cmpFields, it->m_Name );
646 
647  // If the field does not already exist in the component, then we
648  // use defaults from the template fieldname, otherwise the original
649  // values from the component will be set.
650  if( !libField )
651  {
652  fld.SetName( it->m_Name );
653  fld.SetVisible( it->m_Visible );
654  }
655  else
656  {
657  fld = *libField; // copy values from component, m_Name too
658  }
659 
660  m_FieldsBuf.push_back( fld );
661  }
662 
663  // Lastly, append any original fields from the component which were not added
664  // from the set of fixed fields nor from the set of template fields.
665  for( unsigned i=MANDATORY_FIELDS; i<cmpFields.size(); ++i )
666  {
667  LIB_FIELD* cmp = &cmpFields[i];
668  LIB_FIELD* buf = findField( cmp->GetName() );
669 
670  if( !buf )
671  {
672  m_FieldsBuf.push_back( *cmp );
673  }
674  }
675 
676  /* field names have become more important than field ids, so we cannot
677  mangle the names in the buffer, but can do so in the panel, see elsewhere.
678  m_FieldsBuf[VALUE].m_Name << wxT( "/" ) << _( "Chip Name" );
679  */
680 
681  for( unsigned ii = 0; ii < m_FieldsBuf.size(); ++ii )
682  {
683  setRowItem( ii, m_FieldsBuf[ii] );
684  }
685 
686  // put focus on the list ctrl
687  fieldListCtrl->SetFocus();
688 
689  // resume editing at the last row edited, last time dialog was up.
690  if ( s_SelectedRow < (int) m_FieldsBuf.size() )
691  s_SelectedRow = 0;
692 
694 }
695 
696 
697 void DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB::setRowItem( int aFieldNdx, const wxString& aName, const wxString& aValue )
698 {
699  wxASSERT( aFieldNdx >= 0 );
700 
701  // insert blanks if aFieldNdx is referencing a "yet to be defined" row
702  while( aFieldNdx >= fieldListCtrl->GetItemCount() )
703  {
704  long ndx = fieldListCtrl->InsertItem( fieldListCtrl->GetItemCount(), wxEmptyString );
705 
706  wxASSERT( ndx >= 0 );
707 
708  fieldListCtrl->SetItem( ndx, COLUMN_TEXT, wxEmptyString );
709  }
710 
711  fieldListCtrl->SetItem( aFieldNdx, COLUMN_FIELD_NAME, aName );
712  fieldListCtrl->SetItem( aFieldNdx, COLUMN_TEXT, aValue );
713 
714  // recompute the column widths here, after setting texts
715  int totalWidth = fieldListCtrl->GetSize().GetWidth();
716  fieldListCtrl->SetColumnWidth( COLUMN_FIELD_NAME, wxLIST_AUTOSIZE );
717  fieldListCtrl->SetColumnWidth( COLUMN_TEXT, totalWidth - fieldListCtrl->GetColumnWidth( 0 ) );
718 }
719 
720 
722 {
723  unsigned fieldNdx = getSelectedFieldNdx();
724 
725  if( fieldNdx >= m_FieldsBuf.size() ) // traps the -1 case too
726  return;
727 
728  LIB_FIELD& field = m_FieldsBuf[fieldNdx];
729 
730  showCheckBox->SetValue( field.IsVisible() );
731 
732  rotateCheckBox->SetValue( field.GetTextAngle() == TEXT_ANGLE_VERT );
733 
734  int style = 0;
735 
736  if( field.IsItalic() )
737  style = 1;
738 
739  if( field.IsBold() )
740  style |= 2;
741 
742  m_StyleRadioBox->SetSelection( style );
743 
744  // Select the right text justification
745  if( field.GetHorizJustify() == GR_TEXT_HJUSTIFY_LEFT )
746  m_FieldHJustifyCtrl->SetSelection(0);
747  else if( field.GetHorizJustify() == GR_TEXT_HJUSTIFY_RIGHT )
748  m_FieldHJustifyCtrl->SetSelection(2);
749  else
750  m_FieldHJustifyCtrl->SetSelection(1);
751 
752  if( field.GetVertJustify() == GR_TEXT_VJUSTIFY_BOTTOM )
753  m_FieldVJustifyCtrl->SetSelection(0);
754  else if( field.GetVertJustify() == GR_TEXT_VJUSTIFY_TOP )
755  m_FieldVJustifyCtrl->SetSelection(2);
756  else
757  m_FieldVJustifyCtrl->SetSelection(1);
758 
759 
760  // Field names have become more important than field ids, so we cannot
761  // mangle the names in the buffer but we can do so in the panel.
762  if( field.GetId() == VALUE )
763  {
764  // This field is the lib name and the default value when loading this component in
765  // schematic. The value is now not editable here (in this dialog) because changing
766  // it is equivalent to create a new component or alias. This is handles in libedir,
767  // not in this dialog.
768  fieldNameTextCtrl->SetValue( field.GetName() + wxT( " / " ) + _( "Chip Name" ) );
769  fieldValueTextCtrl->Enable( false );
770  }
771  else
772  {
773  fieldValueTextCtrl->Enable( true );
774  fieldNameTextCtrl->SetValue( field.GetName() );
775  }
776 
777  // if fieldNdx == REFERENCE, VALUE, FOOTPRINT, or DATASHEET, then disable field name editing
778  fieldNameTextCtrl->Enable( fieldNdx >= MANDATORY_FIELDS );
779  fieldNameTextCtrl->SetEditable( fieldNdx >= MANDATORY_FIELDS );
780 
781  // only user defined fields may be moved, and not the top most user defined
782  // field since it would be moving up into the fixed fields, > not >=
783  moveUpButton->Enable( fieldNdx > MANDATORY_FIELDS );
784  moveDownButton->Enable( ( fieldNdx >= MANDATORY_FIELDS ) && ( fieldNdx < ( m_FieldsBuf.size() - 1 ) ) );
785 
786  // if fieldNdx == REFERENCE, VALUE, then disable delete button
787  deleteFieldButton->Enable( fieldNdx >= MANDATORY_FIELDS );
788 
789  fieldValueTextCtrl->SetValidator( SCH_FIELD_VALIDATOR( true, field.GetId() ) );
790  fieldValueTextCtrl->SetValue( field.GetText() );
791 
793 
794  m_show_datasheet_button->Enable( fieldNdx == DATASHEET || fieldNdx == FOOTPRINT );
795 
796  if( fieldNdx == DATASHEET )
797  {
798  m_show_datasheet_button->SetLabel( _( "Show Datasheet" ) );
799  m_show_datasheet_button->SetToolTip(
800  _("If your datasheet is given as an http:// link,"
801  " then pressing this button should bring it up in your webbrowser.") );
802  }
803  else if( fieldNdx == FOOTPRINT )
804  {
805  m_show_datasheet_button->SetLabel( _( "Browse Footprints" ) );
806  m_show_datasheet_button->SetToolTip(
807  _("Open the footprint browser to choose a footprint and assign it.") );
808  }
809  else
810  {
811  m_show_datasheet_button->SetLabel( wxEmptyString );
812  m_show_datasheet_button->SetToolTip(
813  _("Used only for fields Footprint and Datasheet.") );
814  }
815 
816  wxPoint coord = field.GetTextPos();
817  wxPoint zero;
818 
819  // If the field value is empty and the position is at relative zero, we set the
820  // initial position as a small offset from the ref field, and orient
821  // it the same as the ref field. That is likely to put it at least
822  // close to the desired position.
823  if( coord == zero && field.GetText().IsEmpty() )
824  {
825  rotateCheckBox->SetValue( m_FieldsBuf[REFERENCE].GetTextAngle() == TEXT_ANGLE_VERT );
826 
827  coord.x = m_FieldsBuf[REFERENCE].GetTextPos().x +
828  (fieldNdx - MANDATORY_FIELDS + 1) * 100;
829  coord.y = m_FieldsBuf[REFERENCE].GetTextPos().y +
830  (fieldNdx - MANDATORY_FIELDS + 1) * 100;
831 
832  // coord can compute negative if field is < MANDATORY_FIELDS, e.g. FOOTPRINT.
833  // That is ok, we basically don't want all the new empty fields on
834  // top of each other.
835  }
836 
837  wxString coordText = StringFromValue( g_UserUnit, coord.x );
838  posXTextCtrl->SetValue( coordText );
839 
840  // Note: the Y axis for components in lib is from bottom to top
841  // and the screen axis is top to bottom: we must change the y coord sign for editing
842  coord.y = -coord.y;
843  coordText = StringFromValue( g_UserUnit, coord.y );
844  posYTextCtrl->SetValue( coordText );
845 }
846 
847 
849 {
850  unsigned fieldNdx = getSelectedFieldNdx();
851 
852  if( fieldNdx >= m_FieldsBuf.size() ) // traps the -1 case too
853  return true;
854 
855  // Check for illegal field text.
856  if( fieldValueTextCtrl->GetValidator()
857  && !fieldValueTextCtrl->GetValidator()->Validate( this ) )
858  return false;
859 
860  LIB_FIELD& field = m_FieldsBuf[fieldNdx];
861 
862  if( showCheckBox->GetValue() )
863  field.SetVisible( true );
864  else
865  field.SetVisible( false );
866 
867  if( rotateCheckBox->GetValue() )
868  field.SetTextAngle( TEXT_ANGLE_VERT );
869  else
871 
872  // Copy the text justification
873  static const EDA_TEXT_HJUSTIFY_T hjustify[3] = {
876  };
877 
878  static const EDA_TEXT_VJUSTIFY_T vjustify[3] = {
881  };
882 
883  field.SetHorizJustify( hjustify[m_FieldHJustifyCtrl->GetSelection()] );
884  field.SetVertJustify( vjustify[m_FieldVJustifyCtrl->GetSelection()] );
885 
886  // Blank/empty field texts for REFERENCE and VALUE are not allowed.
887  // (Value is the name of the component in lib!)
888  // Change them only if user provided a non blank value
889  if( !fieldValueTextCtrl->GetValue().IsEmpty() || fieldNdx > VALUE )
890  field.SetText( fieldValueTextCtrl->GetValue() );
891 
892  // FieldNameTextCtrl has a tricked value in it for VALUE index, do not copy it back.
893  // It has the "Chip Name" appended.
894  if( field.GetId() >= MANDATORY_FIELDS )
895  {
896  wxString name = fieldNameTextCtrl->GetValue();
897  field.SetName( name );
898  }
899 
900  setRowItem( fieldNdx, field ); // update fieldListCtrl
901 
903 
904  field.SetTextSize( wxSize( tmp, tmp ) );
905 
906  int style = m_StyleRadioBox->GetSelection();
907 
908  field.SetItalic( (style & 1 ) != 0 );
909  field.SetBold( (style & 2 ) != 0 );
910 
911  wxPoint pos( ValueFromString( g_UserUnit, posXTextCtrl->GetValue() ),
912  ValueFromString( g_UserUnit, posYTextCtrl->GetValue() ) );
913 
914  // Note: the Y axis for components in lib is from bottom to top
915  // and the screen axis is top to bottom: we must change the y coord sign for editing
916  pos.y = -pos.y;
917 
918  field.SetTextPos( pos );
919 
920  return true;
921 }
void SetTextAngle(double aAngle)
Definition: eda_text.h:154
#define TEXT_ANGLE_HORIZ
Frequent text rotations, used with {Set,Get}TextAngle(), in 0.1 degrees for now, hoping to migrate to...
Definition: common.h:91
LIB_FIELD * findField(const wxString &aFieldName)
Function findField searches m_FieldsBuf and returns a LIB_FIELD with aFieldName or NULL if not found...
Class SCH_FIELD instances are attached to a component and provide a place for the component&#39;s value...
Definition: sch_field.h:56
Class KIWAY_PLAYER is a wxFrame capable of the OpenProjectFiles function, meaning it can load a porti...
Definition: kiway_player.h:111
EDA_TEXT_HJUSTIFY_T
Definition: eda_text.h:47
KIWAY & Kiway() const
Function Kiway returns a reference to the KIWAY that this object has an opportunity to participate in...
Definition: kiway_player.h:60
name of datasheet
bool copyPanelToSelectedField()
Function copyPanelToSelectedField copies the values displayed on the panel fields to the currently se...
void setRowItem(int aFieldNdx, const wxString &aName, const wxString &aValue)
const wxPoint & GetTextPos() const
Definition: eda_text.h:222
bool IsItalic() const
Definition: eda_text.h:168
const wxString ResolveUriByEnvVars(const wxString &aUri)
Replace any environment variables in file-path uris (leaving network-path URIs alone).
Definition: common.cpp:269
LIB_ALIAS * GetAlias(size_t aIndex)
Implementation of conversion functions that require both schematic and board internal units...
This file is part of the common library.
This file is part of the common library.
void ImportValues(const LIB_FIELD &aSource)
Function ImportValues copy parameters from a LIB_FIELD source.
Definition: sch_field.cpp:222
wxString StringFromValue(EDA_UNITS_T aUnit, int aValue, bool aAddUnitSymbol)
Function StringFromValue returns the string from aValue according to units (inch, mm ...
Definition: base_units.cpp:205
void SetItalic(bool isItalic)
Definition: eda_text.h:167
void setRowItem(int aFieldNdx, const LIB_FIELD &aField)
The first 4 are mandatory, and must be instantiated in SCH_COMPONENT and LIB_PART constructors...
Field object used in symbol libraries.
Definition: lib_field.h:59
void FinishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
bool HasAlias(const wxString &aName) const
Test if alias aName is in part alias list.
void SetTextPos(const wxPoint &aPoint)
Definition: eda_text.h:221
void OnInitDialog(wxInitDialogEvent &event) override
void SetVisible(bool aVisible)
Definition: eda_text.h:173
LIB_FIELD & GetValueField()
Return reference to the value field.
static int ParseSize(const wxString &sizeText, EDA_UNITS_T user_unit)
bool GetAssociatedDocument(wxWindow *aParent, const wxString &aDocName, const wxPathList *aPaths)
Function GetAssociatedDocument open a document (file) with the suitable browser.
Definition: eda_doc.cpp:87
void OnModify()
Must be called after a schematic change in order to set the "modify" flag of the current screen...
void deleteFieldButtonHandler(wxCommandEvent &event) override
Function deleteFieldButtonHandler deletes a field.
void OnListItemSelected(wxListEvent &event) override
static LIB_FIELD * findfield(const LIB_FIELDS &aList, const wxString &aFieldName)
Function findfield searches a LIB_FIELD_LIST for aFieldName.
static const wxString GetDefaultFieldName(int aFieldNdx)
Function GetDefaultFieldName returns a default symbol field name for field aFieldNdx for all componen...
Schematic editor (Eeschema) main window.
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:212
std::vector< LIB_FIELD > m_FieldsBuf
a copy of the edited component&#39;s LIB_FIELDs
void SetName(const wxString &aName)
Set a user definable field name to aName.
Definition: lib_field.cpp:482
EDA_TEXT_HJUSTIFY_T GetHorizJustify() const
Definition: eda_text.h:188
#define TEXT_ANGLE_VERT
Definition: common.h:92
Field Name Module PCB, i.e. "16DIP300".
Field Reference of part, i.e. "IC21".
bool IsBold() const
Definition: eda_text.h:171
double GetTextAngle() const
Definition: eda_text.h:162
DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB(LIB_EDIT_FRAME *aParent, LIB_PART *aLibEntry)
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Function KiBitmap constructs a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:78
bool IsRoot() const
void updateDisplay()
Function updateDisplay update the listbox showing fields, according to the fields texts must be calle...
std::vector< TEMPLATE_FIELDNAME > TEMPLATE_FIELDNAMES
void SetFields(const SCH_FIELDS &aFields)
Set multiple schematic fields.
VTBL_ENTRY KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, wxTopLevelWindow *aParent=NULL)
Function Player returns the KIWAY_PLAYER* given a FRAME_T.
Definition: kiway.cpp:300
void SetText(const wxString &aText) override
Sets the field text to aText.
Definition: lib_field.cpp:502
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
int GetId() const
Definition: lib_field.h:138
int ShowQuasiModal()
const wxString & GetText() const
Function GetText returns the string associated with the text object.
Definition: eda_text.h:128
std::vector< SCH_FIELD > SCH_FIELDS
A container for several SCH_FIELD items.
Definition: sch_component.h:53
void SetVertJustify(EDA_TEXT_VJUSTIFY_T aType)
Definition: eda_text.h:192
wxString GetAbbreviatedUnitsLabel(EDA_UNITS_T aUnit)
Definition: base_units.cpp:485
Defintions of control validators for schematic dialogs.
int ValueFromString(EDA_UNITS_T aUnits, const wxString &aTextValue)
Function ValueFromString converts aTextValue in aUnits to internal units used by the application...
Definition: base_units.cpp:370
Define a library symbol object.
Helper dialog and control classes.
wxString GetText(GRAPHIC_PINSHAPE shape)
Definition: pin_shape.cpp:33
void addFieldButtonHandler(wxCommandEvent &event) override
void InstallFieldsEditorDialog(wxCommandEvent &event)
void EditSpiceModel(wxCommandEvent &event) override
void initBuffers()
Function initBuffers sets up to edit the given component.
wxString GetName(bool aTranslate=true) const
Returns the field name.
Definition: lib_field.cpp:432
void showButtonHandler(wxCommandEvent &event) override
EDA_TEXT_VJUSTIFY_T
Definition: eda_text.h:54
EDA_UNITS_T g_UserUnit
Global variables definitions.
Definition: common.cpp:57
EDA_TEXT_VJUSTIFY_T GetVertJustify() const
Definition: eda_text.h:189
void moveDownButtonHandler(wxCommandEvent &event) override
void EndQuasiModal(int retCode)
Definition the SCH_COMPONENT class for Eeschema.
The symbol library editor main window.
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
const TEMPLATE_FIELDNAMES & GetTemplateFieldNames()
Return a template field names list for read only access.
see class PGM_BASE
void OnListItemDeselected(wxListEvent &event) override
const char * name
Definition: DXF_plotter.cpp:61
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
bool IsVisible() const
Definition: lib_field.h:164
void SetHorizJustify(EDA_TEXT_HJUSTIFY_T aType)
Definition: eda_text.h:191
void OnOKButtonClick(wxCommandEvent &event) override
void moveUpButtonHandler(wxCommandEvent &event) override
std::vector< LIB_FIELD > LIB_FIELDS
Definition: lib_field.h:257
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.
size_t i
Definition: json11.cpp:597
Class SCH_COMPONENT describes a real schematic component.
Definition: sch_component.h:69
bool Destroy() override
Our version of Destroy() which is virtual from wxWidgets.
Definition of class LIB_EDIT_FRAME.
const wxSize & GetTextSize() const
Definition: eda_text.h:213
class SCH_FILED_VALIDATOR
Definition for part library class.
Definition of the SCH_FIELD class for Eeschema.
static bool IsReferenceStringValid(const wxString &aReferenceString)
Tests for an acceptable reference string.
void SetFields(const std::vector< LIB_FIELD > &aFieldsList)
Overwrite all the existing fields in this symbol with fields supplied in aFieldsList.
#define VALUE
void SetBold(bool aBold)
Definition: eda_text.h:170
static wxString FormatSize(EDA_UNITS_T user_unit, int textSize)
Function FormatSize formats a string containing the size in the desired units.
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:185
VTBL_ENTRY bool ShowModal(wxString *aResult=NULL, wxWindow *aResultantFocusWindow=NULL)
Function ShowModal puts up this wxFrame as if it were a modal dialog, with all other instantiated wxF...
Class DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB_BASE.
static int s_SelectedRow
void OnCancelButtonClick(wxCommandEvent &event) override
void GetFields(LIB_FIELDS &aList)
Return a list of fields withing this part.
void copySelectedFieldToPanel()
Function copySelectedFieldToPanel sets the values displayed on the panel according to the currently s...