Schematic fields: implement fields with variables as names

Special case that always fills in the value with the value of the
variable
This commit is contained in:
Mike Williams 2023-07-12 11:28:23 -04:00
parent e2f47ecabb
commit 7cb8d3d1c9
5 changed files with 82 additions and 14 deletions

View File

@ -500,7 +500,7 @@ DIALOG_SCH_FIELD_PROPERTIES::DIALOG_SCH_FIELD_PROPERTIES( SCH_BASE_FRAME* aParen
init();
if( m_isSheetFilename )
if( m_isSheetFilename || m_field->IsNamedVariable() )
{
m_StyledTextCtrl->Enable( false );
m_TextCtrl->Enable( false );

View File

@ -341,6 +341,11 @@ void DIALOG_SYMBOL_FIELDS_TABLE::SetupColumnProperties()
m_grid->SetColAttr( col, attr );
m_grid->SetColFormatNumber( col );
}
else if( m_dataModel->GetColFieldName( col ).StartsWith( wxS( "${" ) ) )
{
attr->SetReadOnly();
m_grid->SetColAttr( col, attr );
}
else
{
attr->SetEditor( m_grid->GetDefaultEditor() );
@ -574,14 +579,15 @@ void DIALOG_SYMBOL_FIELDS_TABLE::LoadFieldNames()
}
for( const wxString& fieldName : userFieldNames )
AddField( fieldName, fieldName, true, false );
AddField( fieldName, GetTextVars( fieldName ), true, false );
// Add any templateFieldNames which aren't already present in the userFieldNames
for( const TEMPLATE_FIELDNAME& templateFieldname :
m_schSettings.m_TemplateFieldNames.GetTemplateFieldNames() )
{
if( userFieldNames.count( templateFieldname.m_Name ) == 0 )
AddField( templateFieldname.m_Name, templateFieldname.m_Name, false, false );
AddField( templateFieldname.m_Name, GetTextVars( templateFieldname.m_Name ), false,
false );
}
}
@ -611,7 +617,7 @@ void DIALOG_SYMBOL_FIELDS_TABLE::OnAddField( wxCommandEvent& event )
}
}
AddField( fieldName, fieldName, true, false, true );
AddField( fieldName, GetTextVars( fieldName ), true, false, true );
wxGridCellAttr* attr = new wxGridCellAttr;
m_grid->SetColAttr( m_dataModel->GetColsCount() - 1, attr );
@ -1536,7 +1542,7 @@ void DIALOG_SYMBOL_FIELDS_TABLE::doApplyBomPreset( const BOM_PRESET& aPreset )
// Properties like label, etc. will be added in the next loop
if( !found )
AddField( fieldName, fieldName, false, false );
AddField( fieldName, GetTextVars( fieldName ), false, false );
}
// Sync all fields

View File

@ -19,6 +19,7 @@
*/
#include "eeschema_jobs_handler.h"
#include <common.h>
#include <pgm_base.h>
#include <cli/exit_codes.h>
#include <sch_plotter.h>
@ -301,14 +302,15 @@ int EESCHEMA_JOBS_HANDLER::JobExportBom( JOB* aJob )
}
for( const wxString& fieldName : userFieldNames )
dataModel.AddColumn( fieldName, fieldName, true );
dataModel.AddColumn( fieldName, GetTextVars( fieldName ), true );
// Add any templateFieldNames which aren't already present in the userFieldNames
for( const TEMPLATE_FIELDNAME& templateFieldname :
sch->Settings().m_TemplateFieldNames.GetTemplateFieldNames() )
{
if( userFieldNames.count( templateFieldname.m_Name ) == 0 )
dataModel.AddColumn( templateFieldname.m_Name, templateFieldname.m_Name, false );
dataModel.AddColumn( templateFieldname.m_Name, GetTextVars( templateFieldname.m_Name ),
false );
}
BOM_PRESET preset;

View File

@ -61,12 +61,12 @@ SCH_FIELD::SCH_FIELD( const VECTOR2I& aPos, int aFieldId, SCH_ITEM* aParent,
SCH_ITEM( aParent, SCH_FIELD_T ),
EDA_TEXT( schIUScale, wxEmptyString ),
m_id( 0 ),
m_name( aName ),
m_showName( false ),
m_allowAutoPlace( true ),
m_renderCacheValid( false ),
m_lastResolvedColor( COLOR4D::UNSPECIFIED )
{
SetName( aName );
SetTextPos( aPos );
SetId( aFieldId ); // will also set the layer
SetVisible( false );
@ -87,6 +87,7 @@ SCH_FIELD::SCH_FIELD( const SCH_FIELD& aField ) :
m_name = aField.m_name;
m_showName = aField.m_showName;
m_allowAutoPlace = aField.m_allowAutoPlace;
m_isNamedVariable = aField.m_isNamedVariable;
m_renderCache.clear();
@ -111,6 +112,7 @@ SCH_FIELD& SCH_FIELD::operator=( const SCH_FIELD& aField )
m_name = aField.m_name;
m_showName = aField.m_showName;
m_allowAutoPlace = aField.m_allowAutoPlace;
m_isNamedVariable = aField.m_isNamedVariable;
m_renderCache.clear();
@ -172,6 +174,12 @@ void SCH_FIELD::SetId( int aId )
}
wxString SCH_FIELD::GetShownName() const
{
return m_isNamedVariable ? GetTextVars( GetName() ) : GetName();
}
wxString SCH_FIELD::GetShownText( const SCH_SHEET_PATH* aPath, bool aAllowExtraText,
int aDepth ) const
{
@ -197,7 +205,7 @@ wxString SCH_FIELD::GetShownText( const SCH_SHEET_PATH* aPath, bool aAllowExtraT
wxString text = EDA_TEXT::GetShownText( aAllowExtraText, aDepth );
if( IsNameShown() && aAllowExtraText )
text = GetName() << wxS( ": " ) << text;
text = GetShownName() << wxS( ": " ) << text;
if( text == wxS( "~" ) ) // Legacy placeholder for empty string
{
@ -417,6 +425,7 @@ void SCH_FIELD::SwapData( SCH_ITEM* aItem )
std::swap( m_layer, item->m_layer );
std::swap( m_showName, item->m_showName );
std::swap( m_allowAutoPlace, item->m_allowAutoPlace );
std::swap( m_isNamedVariable, item->m_isNamedVariable );
SwapText( *item );
SwapAttributes( *item );
@ -835,6 +844,27 @@ void SCH_FIELD::DoHypertextAction( EDA_DRAW_FRAME* aFrame ) const
}
void SCH_FIELD::SetName( const wxString& aName )
{
m_name = aName;
m_isNamedVariable = m_name.StartsWith( wxT( "${" ) );
if( m_isNamedVariable )
EDA_TEXT::SetText( aName );
}
void SCH_FIELD::SetText( const wxString& aText )
{
// Don't allow modification of text value when using named variables
// as field name.
if( m_isNamedVariable )
return;
EDA_TEXT::SetText( aText );
}
wxString SCH_FIELD::GetName( bool aUseDefaultName ) const
{
if( m_parent && m_parent->Type() == SCH_SYMBOL_T )
@ -1163,5 +1193,17 @@ static struct SCH_FIELD_DESC
propMgr.Mask( TYPE_HASH( SCH_FIELD ), TYPE_HASH( EDA_TEXT ), _HKI( "Visible" ) );
propMgr.Mask( TYPE_HASH( SCH_FIELD ), TYPE_HASH( EDA_TEXT ), _HKI( "Width" ) );
propMgr.Mask( TYPE_HASH( SCH_FIELD ), TYPE_HASH( EDA_TEXT ), _HKI( "Height" ) );
auto isNotNamedVariable =
[]( INSPECTABLE* aItem ) -> bool
{
if( SCH_FIELD* field = dynamic_cast<SCH_FIELD*>( aItem ) )
return !field->IsNamedVariable();
return true;
};
propMgr.OverrideWriteability( TYPE_HASH( SCH_FIELD ), TYPE_HASH( EDA_TEXT ), _HKI( "Text" ),
isNotNamedVariable );
}
} _SCH_FIELD_DESC;

View File

@ -114,7 +114,9 @@ public:
*/
wxString GetCanonicalName() const;
void SetName( const wxString& aName ) { m_name = aName; }
void SetName( const wxString& aName );
void SetText( const wxString& aText ) override;
/**
* Get the initial name of the field set at creation (or set by SetName()).
@ -126,6 +128,12 @@ public:
void SetId( int aId );
/**
* Gets the fields name as displayed on the schematic or
* in the symbol fields table. This is either the same as GetName() or
* if the field has a variable for name, the variable namer with the ${} stripped.
*/
wxString GetShownName() const;
wxString GetShownText( const SCH_SHEET_PATH* aPath, bool aAllowExtraText,
int aDepth = 0 ) const;
@ -163,6 +171,14 @@ public:
bool IsNameShown() const { return m_showName; }
void SetNameShown( bool aShown = true ) { m_showName = aShown; }
/**
* Named variables are fields whose names are variables like ${VAR}.
*
* The shown name of these fields is VAR and the value is resolved from
* ${VAR}
*/
bool IsNamedVariable() const { return m_isNamedVariable; }
bool CanAutoplace() const { return m_allowAutoPlace; }
void SetCanAutoplace( bool aCanPlace ) { m_allowAutoPlace = aCanPlace; }
@ -258,6 +274,8 @@ private:
bool m_showName; ///< Render the field name in addition to its value
bool m_allowAutoPlace; ///< This field can be autoplaced
bool m_isNamedVariable; ///< If the field name is a variable name, e.g. ${DNP}
///< then the value field is forced to be the same as the name
mutable bool m_renderCacheValid;
mutable VECTOR2I m_renderCachePos;