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 change_log.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 <schframe.h>
35 #include <id.h>
36 #include <base_units.h>
37 
38 #include <general.h>
39 #include <libeditframe.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 
50 #ifdef KICAD_SPICE
51 #include <dialog_spice_model.h>
53 #endif /* KICAD_SPICE */
54 
55 // Local variables:
56 static int s_SelectedRow;
57 
58 #define COLUMN_FIELD_NAME 0
59 #define COLUMN_TEXT 1
60 
62 {
63 public:
65  //~DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB() {}
66 
67 private:
68  // Events handlers:
69  void OnInitDialog( wxInitDialogEvent& event ) override;
70  void OnCloseDialog( wxCloseEvent& event ) override;
71 
72  void OnListItemDeselected( wxListEvent& event ) override;
73  void OnListItemSelected( wxListEvent& event ) override;
74  void addFieldButtonHandler( wxCommandEvent& event ) override;
75  void EditSpiceModel( wxCommandEvent& event ) override;
76 
84  void deleteFieldButtonHandler( wxCommandEvent& event ) override;
85 
86  void moveUpButtonHandler( wxCommandEvent& event ) override;
87  void moveDownButtonHandler( wxCommandEvent& event ) override;
88  void OnCancelButtonClick( wxCommandEvent& event ) override;
89  void OnOKButtonClick( wxCommandEvent& event ) override;
90  void showButtonHandler( wxCommandEvent& event ) override;
91 
92  // internal functions:
93  void setSelectedFieldNdx( int aFieldNdx );
94 
95  int getSelectedFieldNdx();
96 
101  void initBuffers();
102 
108  LIB_FIELD* findField( const wxString& aFieldName );
109 
116 
124 
125  void setRowItem( int aFieldNdx, const wxString& aName, const wxString& aValue );
126  void setRowItem( int aFieldNdx, const LIB_FIELD& aField )
127  {
128  setRowItem( aFieldNdx, aField.GetName(), aField.GetText() );
129  }
130 
137  {
138  for( unsigned ii = MANDATORY_FIELDS; ii<m_FieldsBuf.size(); ii++ )
139  setRowItem( ii, m_FieldsBuf[ii] );
140  }
141 
145 
147  std::vector <LIB_FIELD> m_FieldsBuf;
148 };
149 
150 
151 void LIB_EDIT_FRAME::InstallFieldsEditorDialog( wxCommandEvent& event )
152 {
153  if( !GetCurPart() )
154  return;
155 
157 
159 
160  // This dialog itself subsequently can invoke a KIWAY_PLAYER as a quasimodal
161  // frame. Therefore this dialog as a modal frame parent, MUST be run under
162  // quasimodal mode for the quasimodal frame support to work. So don't use
163  // the QUASIMODAL macros here.
164  if( dlg.ShowQuasiModal() != wxID_OK )
165  return;
166 
169  DisplayLibInfos();
170  Refresh();
171 }
172 
173 
175  LIB_EDIT_FRAME* aParent,
176  LIB_PART* aLibEntry ) :
178 {
179  m_parent = aParent;
180  m_libEntry = aLibEntry;
181  m_skipCopyFromPanel = false;
182 
183 #ifndef KICAD_SPICE
184  m_spiceFieldsButton->Show(false);
185 #endif
186 
187  // Configure button logos
188  addFieldButton->SetBitmap( KiBitmap( plus_xpm ) );
189  deleteFieldButton->SetBitmap( KiBitmap( minus_xpm ) );
190  moveUpButton->SetBitmap( KiBitmap( go_up_xpm ) );
191  moveDownButton->SetBitmap( KiBitmap( go_down_xpm ) );
192 }
193 
194 
195 void DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB::OnInitDialog( wxInitDialogEvent& event )
196 {
197  m_skipCopyFromPanel = false;
198  wxListItem columnLabel;
199 
200  columnLabel.SetImage( -1 );
201 
202  columnLabel.SetText( _( "Name" ) );
203  fieldListCtrl->InsertColumn( COLUMN_FIELD_NAME, columnLabel );
204 
205  columnLabel.SetText( _( "Value" ) );
206  fieldListCtrl->InsertColumn( COLUMN_TEXT, columnLabel );
207 
211 
212  initBuffers();
214 
215  stdDialogButtonSizerOK->SetDefault();
216 
217  // Now all widgets have the size fixed, call FinishDialogSettings
219 }
220 
221 
223 {
224  if( !m_skipCopyFromPanel )
225  {
226  if( !copyPanelToSelectedField() )
227  event.Skip(); // do not go to the next row
228  }
229 }
230 
231 
233 {
234  // remember the selected row, statically
235  s_SelectedRow = event.GetIndex();
236 
238 }
239 
240 
242 {
243  EndQuasiModal( wxID_CANCEL );
244 }
245 
246 
248 {
249  // On wxWidgets 2.8, and on Linux, call EndQuasiModal here is mandatory
250  // Otherwise, the main event loop is never restored, and Eeschema does not
251  // respond to any event, because the DIALOG_SHIM destructor is never called.
252  // on wxWidgets 3.0, or on Windows, the DIALOG_SHIM destructor is called,
253  // and calls EndQuasiModal.
254  // Therefore calling EndQuasiModal here is not mandatory but it creates no issues.
255  EndQuasiModal( wxID_CANCEL );
256 }
257 
258 
260 {
261  if( !copyPanelToSelectedField() )
262  return;
263 
264  // test if reference prefix is acceptable
266  {
267  DisplayError( NULL, _( "Illegal reference. References must start with a letter." ) );
268  return;
269  }
270 
271  /* Note: this code is now (2010-dec-04) not used, because the value field is no more editable
272  * because changing the value is equivalent to create a new component or alias.
273  * This is now handled in libedit main frame, and no more in this dialog
274  * but this code is not removed, just in case
275  */
276  /* If a new name entered in the VALUE field, that it not an existing alias name
277  * or root alias of the component */
278  wxString newvalue = m_FieldsBuf[VALUE].GetText();
279 
280  if( m_libEntry->HasAlias( newvalue ) && !m_libEntry->GetAlias( newvalue )->IsRoot() )
281  {
282  wxString msg = wxString::Format(
283  _( "A new name is entered for this component\n"
284  "An alias %s already exists!\n"
285  "Cannot update this component" ),
286  GetChars( newvalue )
287  );
288  DisplayError( this, msg );
289  return;
290  }
291  /* End unused code */
292 
293  // save old cmp in undo list
295 
296  // delete any fields with no name or no value before we copy all of m_FieldsBuf
297  // back into the component
298  for( unsigned i = MANDATORY_FIELDS; i < m_FieldsBuf.size(); )
299  {
300  if( m_FieldsBuf[i].GetName().IsEmpty() || m_FieldsBuf[i].GetText().IsEmpty() )
301  {
302  m_FieldsBuf.erase( m_FieldsBuf.begin() + i );
303  continue;
304  }
305 
306  ++i;
307  }
308 
309  // copy all the fields back, fully replacing any previous fields
311 
312  // We need to keep the name and the value the same at the moment!
313  SetName( m_libEntry->GetValueField().GetText() );
314 
315  m_parent->OnModify();
316 
317  EndQuasiModal( wxID_OK );
318 }
319 
320 
322 {
323 #ifdef KICAD_SPICE
324  // DIALOG_SPICE_MODEL expects a SCH_COMPONENT,
325  // and a list of SCH_FIELDS to create/edit/delete Spice fields.
326  SCH_COMPONENT component; // This dummy component
327 
328  // Build fields list from the m_FieldsBuf fields buffer dialog
329  // to be sure to use the current fields.
330  SCH_FIELDS schfields;
331 
332  for( unsigned ii = 0; ii < m_FieldsBuf.size(); ++ii )
333  {
334  LIB_FIELD& libfield = m_FieldsBuf[ii];
335  SCH_FIELD schfield( libfield.GetTextPos(), libfield.GetId(),
336  &component, libfield.GetName() );
337  schfield.ImportValues( m_FieldsBuf[ii] );
338  schfield.SetText( m_FieldsBuf[ii].GetText() );
339 
340  schfields.push_back( schfield );
341  }
342 
343  component.SetFields( schfields );
344 
345  DIALOG_SPICE_MODEL dialog( this, component, schfields );
346 
347  if( dialog.ShowModal() != wxID_OK )
348  return;
349 
350  // Transfert sch fields to the m_FieldsBuf fields buffer dialog:
351  m_FieldsBuf.clear();
352 
353  for( unsigned ii = 0; ii < schfields.size(); ii++ )
354  {
355  LIB_FIELD libfield;
356  schfields[ii].ExportValues( libfield );
357  m_FieldsBuf.push_back( libfield );
358  }
359 
360  updateDisplay();
361 #endif /* KICAD_SPICE */
362 }
363 
364 
366 {
367  // in case m_FieldsBuf[REFERENCE].m_Orient has changed on screen only, grab
368  // screen contents.
369  if( !copyPanelToSelectedField() )
370  return;
371 
372  unsigned fieldNdx = m_FieldsBuf.size();
373 
374  LIB_FIELD blank( fieldNdx );
375 
376  m_FieldsBuf.push_back( blank );
377  m_FieldsBuf[fieldNdx].SetName( TEMPLATE_FIELDNAME::GetDefaultFieldName( fieldNdx ) );
378 
379  setRowItem( fieldNdx, m_FieldsBuf[fieldNdx] );
380 
381  m_skipCopyFromPanel = true;
382  setSelectedFieldNdx( fieldNdx );
383  m_skipCopyFromPanel = false;
384 }
385 
386 
388 {
389  unsigned fieldNdx = getSelectedFieldNdx();
390 
391  if( fieldNdx >= m_FieldsBuf.size() ) // traps the -1 case too
392  return;
393 
394  if( fieldNdx < MANDATORY_FIELDS )
395  {
396  wxBell();
397  return;
398  }
399 
400  m_skipCopyFromPanel = true;
401 
402  if( m_FieldsBuf[fieldNdx].GetText().IsEmpty() )
403  {
404  m_FieldsBuf.erase( m_FieldsBuf.begin() + fieldNdx );
405  fieldListCtrl->DeleteItem( fieldNdx );
406 
407  if( fieldNdx >= m_FieldsBuf.size() )
408  --fieldNdx;
409  }
410  else
411  {
412  m_FieldsBuf[fieldNdx].Empty();
414  }
415 
416  updateDisplay( );
417 
418  setRowItem( fieldNdx, m_FieldsBuf[fieldNdx] );
419  setSelectedFieldNdx( fieldNdx );
420  m_skipCopyFromPanel = false;
421 }
422 
423 
425 {
426  unsigned int fieldNdx = getSelectedFieldNdx();
427 
428  // Ensure there is at least one field after this one
429  if( fieldNdx >= ( m_FieldsBuf.size() - 1 ) )
430  {
431  return;
432  }
433 
434  // The first field which can be moved up is the second user field
435  // so any field which id < MANDATORY_FIELDS cannot be moved down
436  if( fieldNdx < MANDATORY_FIELDS )
437  return;
438 
439  if( !copyPanelToSelectedField() )
440  return;
441 
442  // swap the fieldNdx field with the one before it, in both the vector
443  // and in the fieldListCtrl
444  LIB_FIELD tmp = m_FieldsBuf[fieldNdx + 1];
445 
446  m_FieldsBuf[fieldNdx + 1] = m_FieldsBuf[fieldNdx];
447  setRowItem( fieldNdx + 1, m_FieldsBuf[fieldNdx] );
448  m_FieldsBuf[fieldNdx + 1].SetId( fieldNdx + 1 );
449 
450  m_FieldsBuf[fieldNdx] = tmp;
451  setRowItem( fieldNdx, tmp );
452  m_FieldsBuf[fieldNdx].SetId( fieldNdx );
453 
454  updateDisplay( );
455 
456  m_skipCopyFromPanel = true;
457  setSelectedFieldNdx( fieldNdx + 1 );
458  m_skipCopyFromPanel = false;
459 }
460 
461 
463 {
464  unsigned fieldNdx = getSelectedFieldNdx();
465 
466  if( fieldNdx >= m_FieldsBuf.size() ) // traps the -1 case too
467  return;
468 
469  // The first field which can be moved up is the second user field
470  // so any field which id <= MANDATORY_FIELDS cannot be moved up
471  if( fieldNdx <= MANDATORY_FIELDS )
472  return;
473 
474  if( !copyPanelToSelectedField() )
475  return;
476 
477  // swap the fieldNdx field with the one before it, in both the vector
478  // and in the fieldListCtrl
479  LIB_FIELD tmp = m_FieldsBuf[fieldNdx - 1];
480 
481  m_FieldsBuf[fieldNdx - 1] = m_FieldsBuf[fieldNdx];
482  setRowItem( fieldNdx - 1, m_FieldsBuf[fieldNdx] );
483  m_FieldsBuf[fieldNdx - 1].SetId(fieldNdx - 1);
484 
485  m_FieldsBuf[fieldNdx] = tmp;
486  setRowItem( fieldNdx, tmp );
487  m_FieldsBuf[fieldNdx].SetId(fieldNdx);
488 
489  updateDisplay( );
490 
491  m_skipCopyFromPanel = true;
492  setSelectedFieldNdx( fieldNdx - 1 );
493  m_skipCopyFromPanel = false;
494 }
495 
496 
498 {
499  unsigned fieldNdx = getSelectedFieldNdx();
500 
501  if( fieldNdx == DATASHEET )
502  {
503  wxString datasheet_uri = fieldValueTextCtrl->GetValue();
504  ::wxLaunchDefaultBrowser( datasheet_uri );
505  }
506  else if( fieldNdx == FOOTPRINT )
507  {
508  // pick a footprint using the footprint picker.
509  wxString fpid;
510 
512 
513  if( frame->ShowModal( &fpid, this ) )
514  {
515  fieldValueTextCtrl->SetValue( fpid );
516  setRowItem( fieldNdx, m_FieldsBuf[fieldNdx].GetName( false ), fpid );
517  }
518 
519  frame->Destroy();
520  }
521 }
522 
523 
525 {
526  // deselect old selection, but I think this is done by single selection
527  // flag within fieldListCtrl
528  // fieldListCtrl->SetItemState( s_SelectedRow, 0, wxLIST_STATE_SELECTED|wxLIST_STATE_FOCUSED);
529 
530  if( aFieldNdx >= (int) m_FieldsBuf.size() )
531  aFieldNdx = m_FieldsBuf.size() - 1;
532 
533  if( aFieldNdx < 0 )
534  aFieldNdx = 0;
535 
536  fieldListCtrl->SetItemState( aFieldNdx, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
537  fieldListCtrl->EnsureVisible( aFieldNdx );
538 
539  s_SelectedRow = aFieldNdx;
540 }
541 
542 
544 {
545  return s_SelectedRow;
546 }
547 
548 
553 static LIB_FIELD* findfield( const LIB_FIELDS& aList, const wxString& aFieldName )
554 {
555  const LIB_FIELD* field = NULL;
556 
557  for( unsigned i=0; i<aList.size(); ++i )
558  {
559  if( aFieldName == aList[i].GetName() )
560  {
561  field = &aList[i]; // best to avoid casting here.
562  break;
563  }
564  }
565  return (LIB_FIELD*) field; // remove const-ness last
566 }
567 
568 
570 {
571  for( unsigned i=0; i<m_FieldsBuf.size(); ++i )
572  {
573  if( aFieldName == m_FieldsBuf[i].GetName() )
574  return &m_FieldsBuf[i];
575  }
576  return NULL;
577 }
578 
579 
581 {
582  LIB_FIELDS cmpFields;
583 
584  m_libEntry->GetFields( cmpFields );
585 
586  /* We have 3 component related field lists to be aware of: 1) UI
587  presentation (m_FieldsBuf), 2) fields in component ram copy, and 3)
588  fields recorded with component on disk. m_FieldsBuf is the list of UI
589  fields, and this list is not the same as the list which is in the
590  component, which is also not the same as the list on disk. All 3 lists
591  are potentially different. In the UI we choose to preserve the order of
592  the first MANDATORY_FIELDS which are sometimes called fixed fields. Then
593  we append the template fieldnames in the exact same order as the
594  template fieldname editor shows them. Then we append any user defined
595  fieldnames which came from the component, and user can modify it during
596  editing, but cannot delete or move a fixed field.
597  */
598 
599  m_FieldsBuf.clear();
600 
601  /* When this code was written, all field constructors ensured that the
602  MANDATORY_FIELDS are all present within a component (in ram only). So we can
603  knowingly copy them over in the normal order. Copy only the fixed fields
604  at first. Please do not break the field constructors.
605  */
606 
607  // fixed fields:
608  for( int i=0; i<MANDATORY_FIELDS; ++i )
609  {
610  m_FieldsBuf.push_back( cmpFields[i] );
611  }
612 
613  // Add template fieldnames:
614  // Now copy in the template fields, in the order that they are present in the
615  // template field editor UI.
616  SCH_EDIT_FRAME* editor = (SCH_EDIT_FRAME*) Kiway().Player( FRAME_SCH, true );
617 
618  const TEMPLATE_FIELDNAMES& tfnames = editor->GetTemplateFieldNames();
619 
620  for( TEMPLATE_FIELDNAMES::const_iterator it = tfnames.begin(); it!=tfnames.end(); ++it )
621  {
622  // add a new field unconditionally to the UI only for this template fieldname
623 
624  // field id must not be in range 0 - MANDATORY_FIELDS, set before saving to disk
625  LIB_FIELD fld( m_libEntry, -1 );
626 
627  // See if field by same name already exists in component.
628  LIB_FIELD* libField = findfield( cmpFields, it->m_Name );
629 
630  // If the field does not already exist in the component, then we
631  // use defaults from the template fieldname, otherwise the original
632  // values from the component will be set.
633  if( !libField )
634  {
635  fld.SetName( it->m_Name );
636  fld.SetText( it->m_Value ); // empty? ok too.
637 
638  if( !it->m_Visible )
639  fld.SetVisible( false );
640  else
641  fld.SetVisible( true );
642  }
643  else
644  {
645  fld = *libField; // copy values from component, m_Name too
646  }
647 
648  m_FieldsBuf.push_back( fld );
649  }
650 
651  // Lastly, append any original fields from the component which were not added
652  // from the set of fixed fields nor from the set of template fields.
653  for( unsigned i=MANDATORY_FIELDS; i<cmpFields.size(); ++i )
654  {
655  LIB_FIELD* cmp = &cmpFields[i];
656  LIB_FIELD* buf = findField( cmp->GetName() );
657 
658  if( !buf )
659  {
660  m_FieldsBuf.push_back( *cmp );
661  }
662  }
663 
664  /* field names have become more important than field ids, so we cannot
665  mangle the names in the buffer, but can do so in the panel, see elsewhere.
666  m_FieldsBuf[VALUE].m_Name << wxT( "/" ) << _( "Chip Name" );
667  */
668 
669  for( unsigned ii = 0; ii < m_FieldsBuf.size(); ++ii )
670  {
671  setRowItem( ii, m_FieldsBuf[ii] );
672  }
673 
674  // put focus on the list ctrl
675  fieldListCtrl->SetFocus();
676 
677  // resume editing at the last row edited, last time dialog was up.
678  if ( s_SelectedRow < (int) m_FieldsBuf.size() )
679  s_SelectedRow = 0;
680 
682 }
683 
684 
685 void DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB::setRowItem( int aFieldNdx, const wxString& aName, const wxString& aValue )
686 {
687  wxASSERT( aFieldNdx >= 0 );
688 
689  // insert blanks if aFieldNdx is referencing a "yet to be defined" row
690  while( aFieldNdx >= fieldListCtrl->GetItemCount() )
691  {
692  long ndx = fieldListCtrl->InsertItem( fieldListCtrl->GetItemCount(), wxEmptyString );
693 
694  wxASSERT( ndx >= 0 );
695 
696  fieldListCtrl->SetItem( ndx, COLUMN_TEXT, wxEmptyString );
697  }
698 
699  fieldListCtrl->SetItem( aFieldNdx, COLUMN_FIELD_NAME, aName );
700  fieldListCtrl->SetItem( aFieldNdx, COLUMN_TEXT, aValue );
701 
702  // recompute the column widths here, after setting texts
703  fieldListCtrl->SetColumnWidth( COLUMN_FIELD_NAME, wxLIST_AUTOSIZE );
704  fieldListCtrl->SetColumnWidth( COLUMN_TEXT, wxLIST_AUTOSIZE );
705 }
706 
707 
709 {
710  unsigned fieldNdx = getSelectedFieldNdx();
711 
712  if( fieldNdx >= m_FieldsBuf.size() ) // traps the -1 case too
713  return;
714 
715  LIB_FIELD& field = m_FieldsBuf[fieldNdx];
716 
717  showCheckBox->SetValue( field.IsVisible() );
718 
719  rotateCheckBox->SetValue( field.GetTextAngle() == TEXT_ANGLE_VERT );
720 
721  int style = 0;
722 
723  if( field.IsItalic() )
724  style = 1;
725 
726  if( field.IsBold() )
727  style |= 2;
728 
729  m_StyleRadioBox->SetSelection( style );
730 
731  // Select the right text justification
732  if( field.GetHorizJustify() == GR_TEXT_HJUSTIFY_LEFT )
733  m_FieldHJustifyCtrl->SetSelection(0);
734  else if( field.GetHorizJustify() == GR_TEXT_HJUSTIFY_RIGHT )
735  m_FieldHJustifyCtrl->SetSelection(2);
736  else
737  m_FieldHJustifyCtrl->SetSelection(1);
738 
739  if( field.GetVertJustify() == GR_TEXT_VJUSTIFY_BOTTOM )
740  m_FieldVJustifyCtrl->SetSelection(0);
741  else if( field.GetVertJustify() == GR_TEXT_VJUSTIFY_TOP )
742  m_FieldVJustifyCtrl->SetSelection(2);
743  else
744  m_FieldVJustifyCtrl->SetSelection(1);
745 
746 
747  // Field names have become more important than field ids, so we cannot
748  // mangle the names in the buffer but we can do so in the panel.
749  if( field.GetId() == VALUE )
750  {
751  // This field is the lib name and the default value when loading this component in
752  // schematic. The value is now not editable here (in this dialog) because changing
753  // it is equivalent to create a new component or alias. This is handles in libedir,
754  // not in this dialog.
755  fieldNameTextCtrl->SetValue( field.GetName() + wxT( " / " ) + _( "Chip Name" ) );
756  fieldValueTextCtrl->Enable( false );
757  }
758  else
759  {
760  fieldValueTextCtrl->Enable( true );
761  fieldNameTextCtrl->SetValue( field.GetName() );
762  }
763 
764  // if fieldNdx == REFERENCE, VALUE, FOOTPRINT, or DATASHEET, then disable field name editing
765  fieldNameTextCtrl->Enable( fieldNdx >= MANDATORY_FIELDS );
766  fieldNameTextCtrl->SetEditable( fieldNdx >= MANDATORY_FIELDS );
767 
768  // only user defined fields may be moved, and not the top most user defined
769  // field since it would be moving up into the fixed fields, > not >=
770  moveUpButton->Enable( fieldNdx > MANDATORY_FIELDS );
771  moveDownButton->Enable( ( fieldNdx >= MANDATORY_FIELDS ) && ( fieldNdx < ( m_FieldsBuf.size() - 1 ) ) );
772 
773  // if fieldNdx == REFERENCE, VALUE, then disable delete button
774  deleteFieldButton->Enable( fieldNdx >= MANDATORY_FIELDS );
775 
776  fieldValueTextCtrl->SetValidator( SCH_FIELD_VALIDATOR( true, field.GetId() ) );
777  fieldValueTextCtrl->SetValue( field.GetText() );
778 
780 
781  m_show_datasheet_button->Enable( fieldNdx == DATASHEET || fieldNdx == FOOTPRINT );
782 
783  if( fieldNdx == DATASHEET )
784  {
785  m_show_datasheet_button->SetLabel( _( "Show Datasheet" ) );
786  m_show_datasheet_button->SetToolTip(
787  _("If your datasheet is given as an http:// link,"
788  " then pressing this button should bring it up in your webbrowser.") );
789  }
790  else if( fieldNdx == FOOTPRINT )
791  {
792  m_show_datasheet_button->SetLabel( _( "Browse Footprints" ) );
793  m_show_datasheet_button->SetToolTip(
794  _("Open the footprint browser to choose a footprint and assign it.") );
795  }
796  else
797  {
798  m_show_datasheet_button->SetLabel( wxEmptyString );
799  m_show_datasheet_button->SetToolTip(
800  _("Used only for fields Footprint and Datasheet.") );
801  }
802 
803  wxPoint coord = field.GetTextPos();
804  wxPoint zero;
805 
806  // If the field value is empty and the position is at relative zero, we set the
807  // initial position as a small offset from the ref field, and orient
808  // it the same as the ref field. That is likely to put it at least
809  // close to the desired position.
810  if( coord == zero && field.GetText().IsEmpty() )
811  {
812  rotateCheckBox->SetValue( m_FieldsBuf[REFERENCE].GetTextAngle() == TEXT_ANGLE_VERT );
813 
814  coord.x = m_FieldsBuf[REFERENCE].GetTextPos().x +
815  (fieldNdx - MANDATORY_FIELDS + 1) * 100;
816  coord.y = m_FieldsBuf[REFERENCE].GetTextPos().y +
817  (fieldNdx - MANDATORY_FIELDS + 1) * 100;
818 
819  // coord can compute negative if field is < MANDATORY_FIELDS, e.g. FOOTPRINT.
820  // That is ok, we basically don't want all the new empty fields on
821  // top of each other.
822  }
823 
824  wxString coordText = StringFromValue( g_UserUnit, coord.x );
825  posXTextCtrl->SetValue( coordText );
826 
827  // Note: the Y axis for components in lib is from bottom to top
828  // and the screen axis is top to bottom: we must change the y coord sign for editing
829  coord.y = -coord.y;
830  coordText = StringFromValue( g_UserUnit, coord.y );
831  posYTextCtrl->SetValue( coordText );
832 }
833 
834 
836 {
837  unsigned fieldNdx = getSelectedFieldNdx();
838 
839  if( fieldNdx >= m_FieldsBuf.size() ) // traps the -1 case too
840  return true;
841 
842  // Check for illegal field text.
843  if( fieldValueTextCtrl->GetValidator()
844  && !fieldValueTextCtrl->GetValidator()->Validate( this ) )
845  return false;
846 
847  LIB_FIELD& field = m_FieldsBuf[fieldNdx];
848 
849  if( showCheckBox->GetValue() )
850  field.SetVisible( true );
851  else
852  field.SetVisible( false );
853 
854  if( rotateCheckBox->GetValue() )
855  field.SetTextAngle( TEXT_ANGLE_VERT );
856  else
858 
859  // Copy the text justification
860  static const EDA_TEXT_HJUSTIFY_T hjustify[3] = {
863  };
864 
865  static const EDA_TEXT_VJUSTIFY_T vjustify[3] = {
868  };
869 
870  field.SetHorizJustify( hjustify[m_FieldHJustifyCtrl->GetSelection()] );
871  field.SetVertJustify( vjustify[m_FieldVJustifyCtrl->GetSelection()] );
872 
873  // Blank/empty field texts for REFERENCE and VALUE are not allowed.
874  // (Value is the name of the component in lib!)
875  // Change them only if user provided a non blank value
876  if( !fieldValueTextCtrl->GetValue().IsEmpty() || fieldNdx > VALUE )
877  field.SetText( fieldValueTextCtrl->GetValue() );
878 
879  // FieldNameTextCtrl has a tricked value in it for VALUE index, do not copy it back.
880  // It has the "Chip Name" appended.
881  if( field.GetId() >= MANDATORY_FIELDS )
882  {
883  wxString name = fieldNameTextCtrl->GetValue();
884  field.SetName( name );
885  }
886 
887  setRowItem( fieldNdx, field ); // update fieldListCtrl
888 
890 
891  field.SetTextSize( wxSize( tmp, tmp ) );
892 
893  int style = m_StyleRadioBox->GetSelection();
894 
895  field.SetItalic( (style & 1 ) != 0 );
896  field.SetBold( (style & 2 ) != 0 );
897 
898  wxPoint pos( ValueFromString( g_UserUnit, posXTextCtrl->GetValue() ),
899  ValueFromString( g_UserUnit, posYTextCtrl->GetValue() ) );
900 
901  // Note: the Y axis for components in lib is from bottom to top
902  // and the screen axis is top to bottom: we must change the y coord sign for editing
903  pos.y = -pos.y;
904 
905  field.SetTextPos( pos );
906 
907  return true;
908 }
void SetTextAngle(double aAngle)
Definition: eda_text.h:156
#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'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:224
bool IsItalic() const
Definition: eda_text.h:170
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.
void ImportValues(const LIB_FIELD &aSource)
Function ImportValues copy parameters from a LIB_FIELD source.
Definition: sch_field.cpp:221
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:203
void SetItalic(bool isItalic)
Definition: eda_text.h:169
void setRowItem(int aFieldNdx, const LIB_FIELD &aField)
The first 4 are mandatory, and must be instantiated in SCH_COMPONENT and LIB_PART constructors...
Class LIB_FIELD is used in symbol libraries.
Definition: lib_field.h:60
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:223
void OnInitDialog(wxInitDialogEvent &event) override
void SetVisible(bool aVisible)
Definition: eda_text.h:175
void EndMouseCapture(int aId=-1, int aCursorId=-1, const wxString &aTitle=wxEmptyString, bool aCallEndFunc=true)
Function EndMouseCapture ends mouse a capture.
LIB_FIELD & GetValueField()
Return reference to the value field.
static int ParseSize(const wxString &sizeText, EDA_UNITS_T user_unit)
Definition: wxwineda.cpp:123
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
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.
Definition: schframe.h:117
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:214
std::vector< LIB_FIELD > m_FieldsBuf
a copy of the edited component's LIB_FIELDs
void SetName(const wxString &aName)
Function SetName.
Definition: lib_field.cpp:655
EDA_TEXT_HJUSTIFY_T GetHorizJustify() const
Definition: eda_text.h:190
void UpdateAliasSelectList()
#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:173
double GetTextAngle() const
Definition: eda_text.h:164
void DisplayLibInfos()
Function DisplayLibInfos updates the main window title bar with the current library name and read onl...
Definition: libedit.cpp:55
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:36
bool IsRoot() const
void updateDisplay()
Function updateDisplay update the listbox showing fields, according to the fields texts must be calle...
VTBL_ENTRY KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, KIWAY_PLAYER *aParent=NULL)
Function Player returns the KIWAY_PLAYER* given a FRAME_T.
Definition: kiway.cpp:302
std::vector< TEMPLATE_FIELDNAME > TEMPLATE_FIELDNAMES
void SetFields(const SCH_FIELDS &aFields)
Set multiple schematic fields.
void SetText(const wxString &aText) override
Sets the field text to aText.
Definition: lib_field.cpp:675
void Refresh()
Update the board display after modifying it bu a python script (note: it is automatically called by a...
int GetId() const
Definition: lib_field.h:136
int ShowQuasiModal()
const wxString & GetText() const
Function GetText returns the string associated with the text object.
Definition: eda_text.h:130
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:194
wxString GetAbbreviatedUnitsLabel(EDA_UNITS_T aUnit)
Definition: base_units.cpp:479
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:368
Class LIB_PART defines a library part 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:605
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:56
Field Value of part, i.e. "3.3K".
EDA_TEXT_VJUSTIFY_T GetVertJustify() const
Definition: eda_text.h:191
EDA_DRAW_PANEL * m_canvas
The area to draw on.
Definition: draw_frame.h:93
void moveDownButtonHandler(wxCommandEvent &event) override
void EndQuasiModal(int retCode)
Definition the SCH_COMPONENT class for Eeschema.
The component library editor main window.
Definition: libeditframe.h:51
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()
Function GetTemplateFieldName returns a template field names list for read only access.
Definition: schframe.h:326
see class PGM_BASE
void OnListItemDeselected(wxListEvent &event) override
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
Function IsVisible.
Definition: lib_field.h:168
void SetHorizJustify(EDA_TEXT_HJUSTIFY_T aType)
Definition: eda_text.h:193
void OnOKButtonClick(wxCommandEvent &event) override
void moveUpButtonHandler(wxCommandEvent &event) override
std::vector< LIB_FIELD > LIB_FIELDS
Definition: lib_field.h:261
LIB_PART * GetCurPart()
Function GetCurPart returns the current part being edited, or NULL if none selected.
Class SCH_COMPONENT describes a real schematic component.
Definition: sch_component.h:69
const char * name
bool Destroy() override
Our version of Destroy() which is virtual from wxWidgets.
const wxSize & GetTextSize() const
Definition: eda_text.h:215
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.
int GetDefaultCursor() const
Function GetDefaultCursor.
void SetFields(const std::vector< LIB_FIELD > &aFieldsList)
Function SetFields overwrites all the existing in this part with fields supplied in aFieldsList...
void SetBold(bool aBold)
Definition: eda_text.h:172
static wxString FormatSize(EDA_UNITS_T user_unit, int textSize)
Function FormatSize formats a string containing the size in the desired units.
Definition: wxwineda.cpp:84
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:71
void UpdatePartSelectList()
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
Definition of class LIB_EDIT_FRAME.
void OnCancelButtonClick(wxCommandEvent &event) override
void GetFields(LIB_FIELDS &aList)
Function GetFields returns a list of fields withing this part.
void copySelectedFieldToPanel()
Function copySelectedFieldToPanel sets the values displayed on the panel according to the currently s...
void SaveCopyInUndoList(EDA_ITEM *ItemToCopy)
Function SaveCopyInUndoList.