PCB Fields: operate on field copies in dialogs

Makes cancel/undo work properly
This commit is contained in:
Mike Williams 2023-12-11 09:37:23 -05:00
parent 484491aaea
commit 61da4fb1af
4 changed files with 68 additions and 94 deletions

View File

@ -262,7 +262,7 @@ bool DIALOG_FOOTPRINT_PROPERTIES::TransferDataToWindow()
// Footprint Fields
for( PCB_FIELD* field : m_footprint->GetFields() )
m_fields->push_back( field );
m_fields->push_back( *field );
// notify the grid
wxGridTableMessage tmsg( m_fields, wxGRIDTABLE_NOTIFY_ROWS_APPENDED,
@ -359,10 +359,10 @@ bool DIALOG_FOOTPRINT_PROPERTIES::Validate()
// Validate texts.
for( size_t i = 0; i < m_fields->size(); ++i )
{
PCB_FIELD* field = m_fields->at( i );
PCB_FIELD& field = m_fields->at( i );
// Check for missing field names.
if( field->GetName( false ).IsEmpty() )
if( field.GetName( false ).IsEmpty() )
{
m_delayedFocusGrid = m_itemsGrid;
m_delayedErrorMessage = wxString::Format( _( "Fields must have a name." ) );
@ -432,9 +432,9 @@ bool DIALOG_FOOTPRINT_PROPERTIES::Validate()
}
// Test for acceptable values for thickness and size and clamp if fails
int maxPenWidth = Clamp_Text_PenSize( field->GetTextThickness(), field->GetTextSize() );
int maxPenWidth = Clamp_Text_PenSize( field.GetTextThickness(), field.GetTextSize() );
if( field->GetTextThickness() > maxPenWidth )
if( field.GetTextThickness() > maxPenWidth )
{
wxString clamped = m_frame->StringFromValue( maxPenWidth, true );
@ -483,7 +483,7 @@ bool DIALOG_FOOTPRINT_PROPERTIES::TransferDataFromWindow()
{
// copy grid table entries till we run out, then delete any remaining texts
if( i < m_fields->size() )
field = m_fields->at( i++ );
*field = m_fields->at( i++ );
else
field->DeleteStructure();
}
@ -491,7 +491,7 @@ bool DIALOG_FOOTPRINT_PROPERTIES::TransferDataFromWindow()
// if there are still grid table entries, create new fields for them
while( i < m_fields->size() )
{
view->Add( m_footprint->AddField( *m_fields->at( i++ ) ) );
view->Add( m_footprint->AddField( m_fields->at( i++ ) ) );
}
// Initialize masks clearances
@ -582,14 +582,14 @@ void DIALOG_FOOTPRINT_PROPERTIES::OnAddField( wxCommandEvent& )
return;
int fieldId = (int) m_fields->size();
PCB_FIELD* newField =
new PCB_FIELD( m_footprint, m_fields->size(),
TEMPLATE_FIELDNAME::GetDefaultFieldName( fieldId, DO_TRANSLATE ) );
PCB_FIELD newField =
PCB_FIELD( m_footprint, m_fields->size(),
TEMPLATE_FIELDNAME::GetDefaultFieldName( fieldId, DO_TRANSLATE ) );
newField->SetVisible( false );
newField->SetLayer( m_footprint->GetLayer() == F_Cu ? F_Fab : B_Fab );
newField->SetFPRelativePosition( { 0, 0 } );
newField->StyleFromSettings( m_frame->GetDesignSettings() );
newField.SetVisible( false );
newField.SetLayer( m_footprint->GetLayer() == F_Cu ? F_Fab : B_Fab );
newField.SetFPRelativePosition( { 0, 0 } );
newField.StyleFromSettings( m_frame->GetDesignSettings() );
m_fields->push_back( newField );

View File

@ -282,7 +282,7 @@ bool DIALOG_FOOTPRINT_PROPERTIES_FP_EDITOR::TransferDataToWindow()
// Footprint Fields
for( PCB_FIELD* field : m_footprint->GetFields() )
m_fields->push_back( field );
m_fields->push_back( *field );
// Notify the grid
wxGridTableMessage tmsg( m_fields, wxGRIDTABLE_NOTIFY_ROWS_APPENDED,
@ -412,10 +412,10 @@ bool DIALOG_FOOTPRINT_PROPERTIES_FP_EDITOR::Validate()
// Check for valid field text properties
for( size_t i = 0; i < m_fields->size(); ++i )
{
PCB_FIELD* field = m_fields->at( i );
PCB_FIELD& field = m_fields->at( i );
// Check for missing field names.
if( field->GetName( false ).IsEmpty() )
if( field.GetName( false ).IsEmpty() )
{
m_delayedFocusGrid = m_itemsGrid;
m_delayedErrorMessage = wxString::Format( _( "Fields must have a name." ) );
@ -428,7 +428,7 @@ bool DIALOG_FOOTPRINT_PROPERTIES_FP_EDITOR::Validate()
int minSize = pcbIUScale.MilsToIU( TEXT_MIN_SIZE_MILS );
int maxSize = pcbIUScale.MilsToIU( TEXT_MAX_SIZE_MILS );
if( field->GetTextWidth() < minSize || field->GetTextWidth() > maxSize )
if( field.GetTextWidth() < minSize || field.GetTextWidth() > maxSize )
{
m_delayedFocusGrid = m_itemsGrid;
m_delayedErrorMessage = wxString::Format( _( "The text width must be between %s and %s." ),
@ -440,7 +440,7 @@ bool DIALOG_FOOTPRINT_PROPERTIES_FP_EDITOR::Validate()
return false;
}
if( field->GetTextHeight() < minSize || field->GetTextHeight() > maxSize )
if( field.GetTextHeight() < minSize || field.GetTextHeight() > maxSize )
{
m_delayedFocusGrid = m_itemsGrid;
m_delayedErrorMessage = wxString::Format( _( "The text height must be between %s and %s." ),
@ -453,9 +453,9 @@ bool DIALOG_FOOTPRINT_PROPERTIES_FP_EDITOR::Validate()
}
// Test for acceptable values for thickness and size and clamp if fails
int maxPenWidth = Clamp_Text_PenSize( field->GetTextThickness(), field->GetTextSize() );
int maxPenWidth = Clamp_Text_PenSize( field.GetTextThickness(), field.GetTextSize() );
if( field->GetTextThickness() > maxPenWidth )
if( field.GetTextThickness() > maxPenWidth )
{
m_itemsGrid->SetCellValue( i, FPT_THICKNESS,
m_frame->StringFromValue( maxPenWidth, true ) );
@ -510,14 +510,14 @@ bool DIALOG_FOOTPRINT_PROPERTIES_FP_EDITOR::TransferDataFromWindow()
// Update fields
std::vector<PCB_TEXT*> items_to_remove;
std::vector<PCB_FIELD*> items_to_remove;
size_t i = 0;
for( PCB_FIELD* field : m_footprint->GetFields() )
{
// copy grid table entries till we run out, then delete any remaining texts
if( i < m_fields->size() )
field = m_fields->at( i++ );
*field = m_fields->at( i++ );
else
items_to_remove.push_back( field );
}
@ -536,7 +536,7 @@ bool DIALOG_FOOTPRINT_PROPERTIES_FP_EDITOR::TransferDataFromWindow()
// if there are still grid table entries, create new fields for them
while( i < m_fields->size() )
{
view->Add( m_footprint->AddField( *m_fields->at( i++ ) ) );
view->Add( m_footprint->AddField( m_fields->at( i++ ) ) );
}
LSET privateLayers;
@ -634,19 +634,19 @@ void DIALOG_FOOTPRINT_PROPERTIES_FP_EDITOR::OnAddField( wxCommandEvent& event )
int fieldId = (int) m_fields->size();
const BOARD_DESIGN_SETTINGS& dsnSettings = m_frame->GetDesignSettings();
PCB_FIELD* newField =
new PCB_FIELD( m_footprint, m_fields->size(),
TEMPLATE_FIELDNAME::GetDefaultFieldName( fieldId, DO_TRANSLATE ) );
PCB_FIELD newField =
PCB_FIELD( m_footprint, m_fields->size(),
TEMPLATE_FIELDNAME::GetDefaultFieldName( fieldId, DO_TRANSLATE ) );
// Set active layer if legal; otherwise copy layer from previous text item
if( LSET::AllTechMask().test( m_frame->GetActiveLayer() ) )
newField->SetLayer( m_frame->GetActiveLayer() );
newField.SetLayer( m_frame->GetActiveLayer() );
else
newField->SetLayer( m_fields->at( m_fields->size() - 1 )->GetLayer() );
newField.SetLayer( m_fields->at( m_fields->size() - 1 ).GetLayer() );
newField->SetTextSize( dsnSettings.GetTextSize( newField->GetLayer() ) );
newField->SetTextThickness( dsnSettings.GetTextThickness( newField->GetLayer() ) );
newField->SetItalic( dsnSettings.GetTextItalic( newField->GetLayer() ) );
newField.SetTextSize( dsnSettings.GetTextSize( newField.GetLayer() ) );
newField.SetTextThickness( dsnSettings.GetTextThickness( newField.GetLayer() ) );
newField.SetItalic( dsnSettings.GetTextItalic( newField.GetLayer() ) );
m_fields->push_back( newField );

View File

@ -257,7 +257,7 @@ wxGridCellAttr* FP_TEXT_GRID_TABLE::GetAttr( int aRow, int aCol, wxGridCellAttr:
wxString FP_TEXT_GRID_TABLE::GetValue( int aRow, int aCol )
{
wxGrid* grid = GetView();
const PCB_FIELD* field = this->at( (size_t) aRow );
const PCB_FIELD& field = this->at( (size_t) aRow );
if( grid->GetGridCursorRow() == aRow && grid->GetGridCursorCol() == aCol
&& grid->IsCellEditControlShown() )
@ -270,35 +270,27 @@ wxString FP_TEXT_GRID_TABLE::GetValue( int aRow, int aCol )
switch( aCol )
{
case FPT_NAME:
return field->GetName();
case FPT_NAME: return field.GetName();
case FPT_VALUE:
return field->GetText();
case FPT_VALUE: return field.GetText();
case FPT_WIDTH:
return m_frame->StringFromValue( field->GetTextWidth(), true );
case FPT_WIDTH: return m_frame->StringFromValue( field.GetTextWidth(), true );
case FPT_HEIGHT:
return m_frame->StringFromValue( field->GetTextHeight(), true );
case FPT_HEIGHT: return m_frame->StringFromValue( field.GetTextHeight(), true );
case FPT_THICKNESS:
return m_frame->StringFromValue( field->GetTextThickness(), true );
case FPT_THICKNESS: return m_frame->StringFromValue( field.GetTextThickness(), true );
case FPT_LAYER:
return field->GetLayerName();
case FPT_LAYER: return field.GetLayerName();
case FPT_ORIENTATION:
{
EDA_ANGLE angle = field->GetTextAngle() - field->GetParentFootprint()->GetOrientation();
EDA_ANGLE angle = field.GetTextAngle() - field.GetParentFootprint()->GetOrientation();
return m_frame->StringFromValue( angle, true );
}
case FPT_XOFFSET:
return m_frame->StringFromValue( field->GetFPRelativePosition().x, true );
case FPT_XOFFSET: return m_frame->StringFromValue( field.GetFPRelativePosition().x, true );
case FPT_YOFFSET:
return m_frame->StringFromValue( field->GetFPRelativePosition().y, true );
case FPT_YOFFSET: return m_frame->StringFromValue( field.GetFPRelativePosition().y, true );
default:
// we can't assert here because wxWidgets sometimes calls this without checking
@ -310,14 +302,14 @@ wxString FP_TEXT_GRID_TABLE::GetValue( int aRow, int aCol )
bool FP_TEXT_GRID_TABLE::GetValueAsBool( int aRow, int aCol )
{
PCB_FIELD* field = this->at( (size_t) aRow );
PCB_FIELD& field = this->at( (size_t) aRow );
switch( aCol )
{
case FPT_SHOWN: return field->IsVisible();
case FPT_ITALIC: return field->IsItalic();
case FPT_UPRIGHT: return field->IsKeepUpright();
case FPT_KNOCKOUT: return field->IsKnockout();
case FPT_SHOWN: return field.IsVisible();
case FPT_ITALIC: return field.IsItalic();
case FPT_UPRIGHT: return field.IsKeepUpright();
case FPT_KNOCKOUT: return field.IsKnockout();
default:
wxFAIL_MSG( wxString::Format( wxT( "column %d doesn't hold a bool value" ), aCol ) );
@ -328,11 +320,11 @@ bool FP_TEXT_GRID_TABLE::GetValueAsBool( int aRow, int aCol )
long FP_TEXT_GRID_TABLE::GetValueAsLong( int aRow, int aCol )
{
PCB_FIELD* field = this->at( (size_t) aRow );
PCB_FIELD& field = this->at( (size_t) aRow );
switch( aCol )
{
case FPT_LAYER: return field->GetLayer();
case FPT_LAYER: return field.GetLayer();
default:
wxFAIL_MSG( wxString::Format( wxT( "column %d doesn't hold a long value" ), aCol ) );
@ -343,7 +335,7 @@ long FP_TEXT_GRID_TABLE::GetValueAsLong( int aRow, int aCol )
void FP_TEXT_GRID_TABLE::SetValue( int aRow, int aCol, const wxString &aValue )
{
PCB_FIELD* field = this->at( (size_t) aRow );
PCB_FIELD& field = this->at( (size_t) aRow );
VECTOR2I pos;
wxString value = aValue;
@ -370,41 +362,31 @@ void FP_TEXT_GRID_TABLE::SetValue( int aRow, int aCol, const wxString &aValue )
switch( aCol )
{
case FPT_NAME:
field->SetName( value );
break;
case FPT_NAME: field.SetName( value ); break;
case FPT_VALUE:
field->SetText( value );
break;
case FPT_VALUE: field.SetText( value ); break;
case FPT_WIDTH:
field->SetTextWidth( m_frame->ValueFromString( value ) );
break;
case FPT_WIDTH: field.SetTextWidth( m_frame->ValueFromString( value ) ); break;
case FPT_HEIGHT:
field->SetTextHeight( m_frame->ValueFromString( value ) );
break;
case FPT_HEIGHT: field.SetTextHeight( m_frame->ValueFromString( value ) ); break;
case FPT_THICKNESS:
field->SetTextThickness( m_frame->ValueFromString( value ) );
break;
case FPT_THICKNESS: field.SetTextThickness( m_frame->ValueFromString( value ) ); break;
case FPT_ORIENTATION:
field->SetTextAngle( m_frame->AngleValueFromString( value )
+ field->GetParentFootprint()->GetOrientation() );
field.SetTextAngle( m_frame->AngleValueFromString( value )
+ field.GetParentFootprint()->GetOrientation() );
break;
case FPT_XOFFSET:
case FPT_YOFFSET:
pos = field->GetFPRelativePosition();
pos = field.GetFPRelativePosition();
if( aCol == FPT_XOFFSET )
pos.x = m_frame->ValueFromString( value );
else
pos.y = m_frame->ValueFromString( value );
field->SetFPRelativePosition( pos );
field.SetFPRelativePosition( pos );
break;
default:
@ -418,25 +400,17 @@ void FP_TEXT_GRID_TABLE::SetValue( int aRow, int aCol, const wxString &aValue )
void FP_TEXT_GRID_TABLE::SetValueAsBool( int aRow, int aCol, bool aValue )
{
PCB_FIELD* field = this->at( (size_t) aRow );
PCB_FIELD& field = this->at( (size_t) aRow );
switch( aCol )
{
case FPT_SHOWN:
field->SetVisible( aValue );
break;
case FPT_SHOWN: field.SetVisible( aValue ); break;
case FPT_ITALIC:
field->SetItalic( aValue );
break;
case FPT_ITALIC: field.SetItalic( aValue ); break;
case FPT_UPRIGHT:
field->SetKeepUpright( aValue );
break;
case FPT_UPRIGHT: field.SetKeepUpright( aValue ); break;
case FPT_KNOCKOUT:
field->SetIsKnockout( aValue );
break;
case FPT_KNOCKOUT: field.SetIsKnockout( aValue ); break;
default:
wxFAIL_MSG( wxString::Format( wxT( "column %d doesn't hold a bool value" ), aCol ) );
@ -447,13 +421,13 @@ void FP_TEXT_GRID_TABLE::SetValueAsBool( int aRow, int aCol, bool aValue )
void FP_TEXT_GRID_TABLE::SetValueAsLong( int aRow, int aCol, long aValue )
{
PCB_FIELD* field = this->at( (size_t) aRow );
PCB_FIELD& field = this->at( (size_t) aRow );
switch( aCol )
{
case FPT_LAYER:
field->SetLayer( ToLAYER_ID( (int) aValue ) );
field->SetMirrored( IsBackLayer( field->GetLayer() ) );
field.SetLayer( ToLAYER_ID( (int) aValue ) );
field.SetMirrored( IsBackLayer( field.GetLayer() ) );
break;
default:

View File

@ -53,7 +53,7 @@ enum FP_TEXT_COL_ORDER
};
class FP_TEXT_GRID_TABLE : public wxGridTableBase, public std::vector<PCB_FIELD*>
class FP_TEXT_GRID_TABLE : public wxGridTableBase, public std::vector<PCB_FIELD>
{
public:
FP_TEXT_GRID_TABLE( PCB_BASE_FRAME* aFrame, DIALOG_SHIM* aDialog );