Commit 16131a50 authored by Wayne Stambaugh's avatar Wayne Stambaugh

Encapsulate SCH_FIELD class.

parent bcbde5d8
......@@ -236,14 +236,14 @@ void DIALOG_EDIT_COMPONENT_IN_SCHEMATIC::OnOKButtonClick( wxCommandEvent& event
// Delete any fields with no name before we copy all of m_FieldsBuf back into the component.
for( unsigned i = MANDATORY_FIELDS; i<m_FieldsBuf.size(); )
{
if( m_FieldsBuf[i].m_Name.IsEmpty() || m_FieldsBuf[i].m_Text.IsEmpty() )
if( m_FieldsBuf[i].GetName( false ).IsEmpty() || m_FieldsBuf[i].GetText().IsEmpty() )
{
// If a field has no value and is not it the field template list, warn the user
// that it will be remove from the component. This gives the user a chance to
// correct the problem before removing the undefined fields. It should also
// resolve most of the bug reports and questions regarding missing fields.
if( !m_FieldsBuf[i].m_Name.IsEmpty() && m_FieldsBuf[i].m_Text.IsEmpty()
&& !m_Parent->GetTemplates().HasFieldName( m_FieldsBuf[i].m_Name )
if( !m_FieldsBuf[i].GetName( false ).IsEmpty() && m_FieldsBuf[i].GetText().IsEmpty()
&& !m_Parent->GetTemplates().HasFieldName( m_FieldsBuf[i].GetName( false ) )
&& !removeRemainingFields )
{
wxString msg;
......@@ -251,7 +251,7 @@ void DIALOG_EDIT_COMPONENT_IN_SCHEMATIC::OnOKButtonClick( wxCommandEvent& event
msg.Printf( _( "The field name <%s> does not have a value and is not defined in \
the field template list. Empty field values are invalid an will be removed from the component. \
Do you wish to remove this and all remaining undefined fields?" ),
GetChars( m_FieldsBuf[i].m_Name ) );
GetChars( m_FieldsBuf[i].GetName( false ) ) );
wxMessageDialog dlg( this, msg, _( "Remove Fields" ), wxYES_NO | wxNO_DEFAULT );
......@@ -303,7 +303,7 @@ void DIALOG_EDIT_COMPONENT_IN_SCHEMATIC::addFieldButtonHandler( wxCommandEvent&
blank.m_Orient = m_FieldsBuf[REFERENCE].m_Orient;
m_FieldsBuf.push_back( blank );
m_FieldsBuf[fieldNdx].m_Name = TEMPLATE_FIELDNAME::GetDefaultFieldName( fieldNdx );
m_FieldsBuf[fieldNdx].SetName( TEMPLATE_FIELDNAME::GetDefaultFieldName( fieldNdx ) );
m_skipCopyFromPanel = true;
setRowItem( fieldNdx, m_FieldsBuf[fieldNdx] );
......@@ -361,7 +361,7 @@ void DIALOG_EDIT_COMPONENT_IN_SCHEMATIC::moveUpButtonHandler( wxCommandEvent& ev
SCH_FIELD tmp = m_FieldsBuf[fieldNdx - 1];
D( printf( "tmp.m_Text=\"%s\" tmp.m_Name=\"%s\"\n",
TO_UTF8( tmp.m_Text ), TO_UTF8( tmp.m_Name ) ); )
TO_UTF8( tmp.m_Text ), TO_UTF8( tmp.GetName( false ) ) ); )
m_FieldsBuf[fieldNdx - 1] = m_FieldsBuf[fieldNdx];
setRowItem( fieldNdx - 1, m_FieldsBuf[fieldNdx] );
......@@ -408,7 +408,7 @@ SCH_FIELD* DIALOG_EDIT_COMPONENT_IN_SCHEMATIC::findField( const wxString& aField
{
for( unsigned i=0; i<m_FieldsBuf.size(); ++i )
{
if( aFieldName == m_FieldsBuf[i].m_Name )
if( aFieldName == m_FieldsBuf[i].GetName( false ) )
return &m_FieldsBuf[i];
}
......@@ -499,7 +499,7 @@ void DIALOG_EDIT_COMPONENT_IN_SCHEMATIC::InitBuffers( SCH_COMPONENT* aComponent
for( unsigned i=MANDATORY_FIELDS; i<aComponent->m_Fields.size(); ++i )
{
SCH_FIELD* cmp = &aComponent->m_Fields[i];
SCH_FIELD* buf = findField( cmp->m_Name );
SCH_FIELD* buf = findField( cmp->GetName( false ) );
if( !buf )
{
......@@ -516,7 +516,7 @@ void DIALOG_EDIT_COMPONENT_IN_SCHEMATIC::InitBuffers( SCH_COMPONENT* aComponent
for( unsigned i = 0; i<m_FieldsBuf.size(); ++i )
{
printf( "m_FieldsBuf[%d] (x=%-3d, y=%-3d) name:%s\n", i, m_FieldsBuf[i].m_Pos.x,
m_FieldsBuf[i].m_Pos.y, TO_UTF8(m_FieldsBuf[i].m_Name) );
m_FieldsBuf[i].m_Pos.y, TO_UTF8(m_FieldsBuf[i].GetName( false ) ) );
}
#endif
......@@ -560,8 +560,8 @@ void DIALOG_EDIT_COMPONENT_IN_SCHEMATIC::setRowItem( int aFieldNdx, const SCH_FI
fieldListCtrl->SetItem( ndx, 1, wxEmptyString );
}
fieldListCtrl->SetItem( aFieldNdx, 0, aField.m_Name );
fieldListCtrl->SetItem( aFieldNdx, 1, aField.m_Text );
fieldListCtrl->SetItem( aFieldNdx, 0, aField.GetName( false ) );
fieldListCtrl->SetItem( aFieldNdx, 1, aField.GetText() );
// recompute the column widths here, after setting texts
fieldListCtrl->SetColumnWidth( 0, wxLIST_AUTOSIZE );
......@@ -608,7 +608,7 @@ void DIALOG_EDIT_COMPONENT_IN_SCHEMATIC::copySelectedFieldToPanel()
m_FieldVJustifyCtrl->SetSelection(1);
fieldNameTextCtrl->SetValue( field.m_Name );
fieldNameTextCtrl->SetValue( field.GetName( false ) );
// the names of the fixed fields are not editable, others are.
fieldNameTextCtrl->Enable( fieldNdx >= MANDATORY_FIELDS );
......@@ -698,7 +698,7 @@ bool DIALOG_EDIT_COMPONENT_IN_SCHEMATIC::copyPanelToSelectedField()
field.m_HJustify = hjustify[m_FieldHJustifyCtrl->GetSelection()];
field.m_VJustify = vjustify[m_FieldVJustifyCtrl->GetSelection()];
field.m_Name = fieldNameTextCtrl->GetValue();
field.SetName( fieldNameTextCtrl->GetValue() );
/* Void fields texts for REFERENCE and VALUE (value is the name of the
* component in lib ! ) are not allowed
......
......@@ -77,7 +77,7 @@ create a new power component with the new value." ), GetChars( entry->GetName()
DrawPanel->m_IgnoreMouseEvents = true;
wxString title;
title.Printf( _( "Edit %s Field" ), GetChars( aField->m_Name ) );
title.Printf( _( "Edit %s Field" ), GetChars( aField->GetName() ) );
wxTextEntryDialog dlg( this, wxEmptyString , title, newtext );
int response = dlg.ShowModal();
......@@ -95,7 +95,7 @@ create a new power component with the new value." ), GetChars( entry->GetName()
if( !newtext.IsEmpty() )
{
if( aField->m_Text.IsEmpty() ) // Means the field was not already in use
if( aField->GetText().IsEmpty() ) // Means the field was not already in use
{
aField->m_Pos = component->GetPosition();
aField->m_Size.x = aField->m_Size.y = m_TextFieldSize;
......@@ -107,7 +107,7 @@ create a new power component with the new value." ), GetChars( entry->GetName()
if( SCH_COMPONENT::IsReferenceStringValid( newtext ) )
{
component->SetRef( GetSheet(), newtext );
aField->m_Text = newtext;
aField->SetText( newtext );
}
else
{
......
......@@ -1010,11 +1010,11 @@ XNODE* EXPORT_HELP::makeGenericComponents()
SCH_FIELD* f = comp->GetField( fldNdx );
// only output a field if non empty
if( !f->m_Text.IsEmpty() )
if( !f->GetText().IsEmpty() )
{
XNODE* xfield;
xfields->AddChild( xfield = node( sField, f->m_Text ) );
xfield->AddAttribute( sName, f->m_Name );
xfield->AddAttribute( sName, f->GetName() );
}
}
}
......
......@@ -424,6 +424,7 @@ const wxString SCH_COMPONENT::GetRef( SCH_SHEET_PATH* sheet )
SetRef( sheet, GetField( REFERENCE )->m_Text );
return GetField( REFERENCE )->m_Text;
}
return m_prefix;
}
......@@ -440,8 +441,7 @@ bool SCH_COMPONENT::IsReferenceStringValid( const wxString & aReferenceString )
bool ok = true;
// Try to unannotate this reference
while( !text.IsEmpty() &&
( text.Last() == '?' || isdigit( text.Last() ) ) )
while( !text.IsEmpty() && ( text.Last() == '?' || isdigit( text.Last() ) ) )
text.RemoveLast();
if( text.IsEmpty() )
......@@ -502,13 +502,16 @@ void SCH_COMPONENT::SetRef( SCH_SHEET_PATH* sheet, const wxString& ref )
// Reinit the m_prefix member if needed
wxString prefix = ref;
if( IsReferenceStringValid( prefix ) )
{
while( prefix.Last() == '?' || isdigit( prefix.Last() ) )
prefix.RemoveLast();
}
else
{
prefix = wxT("U"); // Set to default ref prefix
}
if( m_prefix != prefix )
m_prefix = prefix;
......@@ -582,6 +585,7 @@ void SCH_COMPONENT::SetUnitSelection( SCH_SHEET_PATH* aSheet, int aUnitSelection
h_ref += tokenizer.GetNextToken(); // Add reference
h_ref += wxT( " " );
h_ref << aUnitSelection; // Add part selection
// Ann the part selection
m_PathsAndReferences[ii] = h_ref;
notInArray = false;
......@@ -622,7 +626,7 @@ SCH_FIELD* SCH_COMPONENT::FindField( const wxString& aFieldName )
{
for( unsigned i = 0; i<m_Fields.size(); ++i )
{
if( aFieldName == m_Fields[i].m_Name )
if( aFieldName == m_Fields[i].GetName( false ) )
return &m_Fields[i];
}
......@@ -690,6 +694,7 @@ void SCH_COMPONENT::ClearAnnotation( SCH_SHEET_PATH* aSheetPath )
// Build a reference with no annotation,
// i.e. a reference ended by only one '?'
wxString defRef = m_prefix;
if( IsReferenceStringValid( defRef ) )
{
while( defRef.Last() == '?' )
......@@ -966,8 +971,7 @@ bool SCH_COMPONENT::Save( FILE* f ) const
//files backwards-compatible.
if( m_PathsAndReferences.GetCount() > 0 )
{
reference_fields = wxStringTokenize( m_PathsAndReferences[0],
delimiters );
reference_fields = wxStringTokenize( m_PathsAndReferences[0], delimiters );
name1 = toUTFTildaText( reference_fields[1] );
}
......@@ -984,7 +988,9 @@ bool SCH_COMPONENT::Save( FILE* f ) const
name2 = toUTFTildaText( m_ChipName );
}
else
{
name2 = NULL_STRING;
}
if( fprintf( f, "$Comp\n" ) == EOF )
return false;
......@@ -1031,7 +1037,7 @@ bool SCH_COMPONENT::Save( FILE* f ) const
for( unsigned i = 0; i<m_Fields.size(); ++i )
{
SCH_FIELD* fld = GetField( i );
fld->m_FieldId = i; // we don't need field Ids, please be gone.
fld->SetId( i ); // we don't need field Ids, please be gone.
}
// Fixed fields:
......@@ -1108,8 +1114,10 @@ bool SCH_COMPONENT::Load( LINE_READER& aLine, wxString& aErrorMsg )
if( strcmp( name1, NULL_STRING ) != 0 )
{
for( ii = 0; ii < (int) strlen( name1 ); ii++ )
{
if( name1[ii] == '~' )
name1[ii] = ' ';
}
m_ChipName = FROM_UTF8( name1 );
......@@ -1139,6 +1147,7 @@ bool SCH_COMPONENT::Load( LINE_READER& aLine, wxString& aErrorMsg )
isDigit = true;
name1[ii] = 0; //null-terminate.
}
if( !isDigit )
{
name1[ii] = name2[ii];
......@@ -1226,11 +1235,15 @@ bool SCH_COMPONENT::Load( LINE_READER& aLine, wxString& aErrorMsg )
// copy the multi, if exists
ii = ReadDelimitedText( name1, ptcar, 255 );
if( name1[0] == 0 ) // Nothing read, put a default value
sprintf( name1, "%d", m_unit );
int multi = atoi( name1 );
if( multi < 0 || multi > 25 )
multi = 1;
AddHierarchicalReference( path, ref, multi );
GetField( REFERENCE )->m_Text = ref;
}
......@@ -1255,6 +1268,7 @@ bool SCH_COMPONENT::Load( LINE_READER& aLine, wxString& aErrorMsg )
}
ptcar += ReadDelimitedText( &fieldText, ptcar );
if( *ptcar == 0 )
{
aErrorMsg.Printf( wxT( "Component field F at line %d, aborted" ),
......@@ -1290,11 +1304,12 @@ bool SCH_COMPONENT::Load( LINE_READER& aLine, wxString& aErrorMsg )
}
else
{
GetField( fieldNdx )->m_Name = fieldName;
GetField( fieldNdx )->SetName( fieldName );
}
GetField( fieldNdx )->m_Text = fieldText;
memset( char3, 0, sizeof(char3) );
if( ( ii = sscanf( ptcar, "%s %d %d %d %X %s %s", char1,
&GetField( fieldNdx )->m_Pos.x,
&GetField( fieldNdx )->m_Pos.y,
......@@ -1322,14 +1337,17 @@ bool SCH_COMPONENT::Load( LINE_READER& aLine, wxString& aErrorMsg )
hjustify = GR_TEXT_HJUSTIFY_LEFT;
else if( *char2 == 'R' )
hjustify = GR_TEXT_HJUSTIFY_RIGHT;
if( char3[0] == 'B' )
vjustify = GR_TEXT_VJUSTIFY_BOTTOM;
else if( char3[0] == 'T' )
vjustify = GR_TEXT_VJUSTIFY_TOP;
if( char3[1] == 'I' )
GetField( fieldNdx )->m_Italic = true;
else
GetField( fieldNdx )->m_Italic = false;
if( char3[2] == 'B' )
GetField( fieldNdx )->m_Bold = true;
else
......@@ -1344,7 +1362,9 @@ bool SCH_COMPONENT::Load( LINE_READER& aLine, wxString& aErrorMsg )
GetField( fieldNdx )->m_Attributs |= TEXT_NO_VISIBLE;
}
else
{
break;
}
}
if( sscanf( line, "%d %d %d", &m_unit, &m_Pos.x, &m_Pos.y ) != 3 )
......
......@@ -58,10 +58,10 @@ SCH_FIELD::SCH_FIELD( const wxPoint& aPos, int aFieldId, SCH_COMPONENT* aParent,
SCH_ITEM( aParent, SCH_FIELD_T ),
EDA_TEXT()
{
m_Pos = aPos;
m_FieldId = aFieldId;
m_Attributs = TEXT_NO_VISIBLE;
m_Name = aName;
m_Pos = aPos;
m_id = aFieldId;
m_Attributs = TEXT_NO_VISIBLE;
m_name = aName;
SetLayer( LAYER_FIELDS );
}
......@@ -71,8 +71,8 @@ SCH_FIELD::SCH_FIELD( const SCH_FIELD& aField ) :
SCH_ITEM( aField ),
EDA_TEXT( aField )
{
m_FieldId = aField.m_FieldId;
m_Name = aField.m_Name;
m_id = aField.m_id;
m_name = aField.m_name;
}
......@@ -93,7 +93,7 @@ const wxString SCH_FIELD::GetText() const
/* For more than one part per package, we must add the part selection
* A, B, ... or 1, 2, .. to the reference. */
if( m_FieldId == REFERENCE )
if( m_id == REFERENCE )
{
SCH_COMPONENT* component = (SCH_COMPONENT*) m_Parent;
......@@ -178,9 +178,9 @@ void SCH_FIELD::Draw( EDA_DRAW_PANEL* panel, wxDC* DC,
EDA_RECT boundaryBox = GetBoundingBox();
textpos = boundaryBox.Centre();
if( m_FieldId == REFERENCE )
if( m_id == REFERENCE )
color = ReturnLayerColor( LAYER_REFERENCEPART );
else if( m_FieldId == VALUE )
else if( m_id == VALUE )
color = ReturnLayerColor( LAYER_VALUEPART );
else
color = ReturnLayerColor( LAYER_FIELDS );
......@@ -306,7 +306,7 @@ bool SCH_FIELD::Save( FILE* aFile ) const
vjustify = 'T';
if( fprintf( aFile, "F %d %s %c %-3d %-3d %-3d %4.4X %c %c%c%c",
m_FieldId,
m_id,
EscapedUTF8( m_Text ).c_str(), // wraps in quotes too
m_Orient == TEXT_ORIENT_HORIZ ? 'H' : 'V',
m_Pos.x, m_Pos.y,
......@@ -320,9 +320,9 @@ bool SCH_FIELD::Save( FILE* aFile ) const
}
// Save field name, if the name is user definable
if( m_FieldId >= FIELD1 )
if( m_id >= FIELD1 )
{
if( fprintf( aFile, " %s", EscapedUTF8( m_Name ).c_str() ) == EOF )
if( fprintf( aFile, " %s", EscapedUTF8( m_name ).c_str() ) == EOF )
{
return false;
}
......@@ -358,14 +358,14 @@ bool SCH_FIELD::Matches( wxFindReplaceData& aSearchData, void* aAuxData, wxPoint
bool match;
wxString text = GetText();
if( (m_FieldId > VALUE) && !(aSearchData.GetFlags() & FR_SEARCH_ALL_FIELDS) )
if( (m_id > VALUE) && !(aSearchData.GetFlags() & FR_SEARCH_ALL_FIELDS) )
return false;
wxLogTrace( traceFindReplace, wxT( " child item " ) + GetSelectMenuText() );
// Take sheet path into account which effects the reference field and the unit for
// components with multiple parts.
if( m_FieldId == REFERENCE && aAuxData != NULL )
if( m_id == REFERENCE && aAuxData != NULL )
{
SCH_COMPONENT* component = (SCH_COMPONENT*) m_Parent;
......@@ -407,24 +407,26 @@ wxString SCH_FIELD::GetSelectMenuText() const
}
wxString SCH_FIELD::GetName() const
wxString SCH_FIELD::GetName( bool aUseDefaultName ) const
{
if( !m_Name.IsEmpty() )
return m_Name;
else
return TEMPLATE_FIELDNAME::GetDefaultFieldName( m_FieldId );
if( !m_name.IsEmpty() )
return m_name;
else if( aUseDefaultName )
return TEMPLATE_FIELDNAME::GetDefaultFieldName( m_id );
return wxEmptyString;
}
BITMAP_DEF SCH_FIELD::GetMenuImage() const
{
if( m_FieldId == REFERENCE )
if( m_id == REFERENCE )
return edit_comp_ref_xpm;
if( m_FieldId == VALUE )
if( m_id == VALUE )
return edit_comp_value_xpm;
if( m_FieldId == FOOTPRINT )
if( m_id == FOOTPRINT )
return edit_comp_footprint_xpm;
return edit_text_xpm;
......@@ -462,15 +464,6 @@ bool SCH_FIELD::doHitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy
}
/* Plot field text.
* Input:
* DrawLibItem: pointer to the component
* FieldNumber: Number Field
* IsMulti: true flag if there are several parts per package.
* Only useful for the field to add a reference to this one
* The identification from (A, B ...)
* DrawMode: trace mode
*/
void SCH_FIELD::doPlot( PLOTTER* aPlotter )
{
SCH_COMPONENT* parent = ( SCH_COMPONENT* ) GetParent();
......@@ -518,7 +511,7 @@ void SCH_FIELD::doPlot( PLOTTER* aPlotter )
int thickness = GetPenSize();
if( (parent->GetPartCount() <= 1) || (m_FieldId != REFERENCE) )
if( (parent->GetPartCount() <= 1) || (m_id != REFERENCE) )
{
aPlotter->text( textpos, color, m_Text, orient, m_Size, hjustify, vjustify,
thickness, m_Italic, m_Bold );
......
......@@ -55,10 +55,9 @@ class LIB_FIELD;
class SCH_FIELD : public SCH_ITEM, public EDA_TEXT
{
public:
int m_FieldId; ///< Field index, @see enum NumFieldType
int m_id; ///< Field index, @see enum NumFieldType
wxString m_Name;
wxString m_name;
public:
SCH_FIELD( const wxPoint& aPos, int aFieldId, SCH_COMPONENT* aParent,
......@@ -71,17 +70,23 @@ public:
virtual wxString GetClass() const
{
return wxT( "SCH_FIELD" );
}
}
/**
* Function GetName
* returns the field name. If the field name is empty, the default field name is
* returned. Field names are VALUE, REFERENCE, etc.
* @return The name of the field.
* returns the field name.
*
* @param aUseDefaultName When true return the default field name if the field name is
* empty. Otherwise the default field name is returned.
* @return A wxString object containing the name of the field.
*/
wxString GetName() const;
wxString GetName( bool aUseDefaultName = true ) const;
int GetId() const { return m_FieldId; }
void SetName( const wxString& aName ) { m_name = aName; }
int GetId() const { return m_id; }
void SetId( int aId ) { m_id = aId; }
/**
* Function GetText
......@@ -109,7 +114,7 @@ public:
/**
* Function SwapData
* exchanges the date between the field and \a aField.
* exchanges the date between the field and \a aItem
*
* @param aItem The field to exchange data with.
*/
......@@ -192,15 +197,7 @@ public:
}
/**
* Compare schematic field text against search string.
*
* @param aSearchData - Criteria to search against.
* @param aAuxData - a pointer on auxiliary data, if needed.
* the sheet path is needed for REFERENCE field because m_Text
* value is just the value displayed, and in complex hierarchies
* this is only one of all references (one per sheet path)
* @param aFindLocation - a wxPoint where to put the location of matched item. can be NULL.
* @return True if this field text matches the search criteria.
* @copydoc EDA_ITEM::Matches(wxFindReplaceData&,void*,wxPoint*)
*/
virtual bool Matches( wxFindReplaceData& aSearchData,
void* aAuxData, wxPoint* aFindLocation );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment