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