diff --git a/include/board_design_settings.h b/include/board_design_settings.h index 0a7de517ff..34f19c2559 100644 --- a/include/board_design_settings.h +++ b/include/board_design_settings.h @@ -210,11 +210,11 @@ enum struct TEXT_ITEM_INFO { - wxString m_Text; - bool m_Visible; - int m_Layer; + wxString m_Text; + bool m_Visible; + PCB_LAYER_ID m_Layer; - TEXT_ITEM_INFO( const wxString& aText, bool aVisible, int aLayer ) + TEXT_ITEM_INFO( const wxString& aText, bool aVisible, PCB_LAYER_ID aLayer ) { m_Text = aText; m_Visible = aVisible; diff --git a/include/footprint_editor_settings.h b/include/footprint_editor_settings.h index a438fdf9fb..0f7847dfc9 100644 --- a/include/footprint_editor_settings.h +++ b/include/footprint_editor_settings.h @@ -101,6 +101,8 @@ private: bool migrateSchema2To3(); bool migrateSchema3To4(); + + bool migrateSchema4To5(); }; diff --git a/pcbnew/dialogs/panel_fp_editor_field_defaults.cpp b/pcbnew/dialogs/panel_fp_editor_field_defaults.cpp index d1954fc50c..55e2112d89 100644 --- a/pcbnew/dialogs/panel_fp_editor_field_defaults.cpp +++ b/pcbnew/dialogs/panel_fp_editor_field_defaults.cpp @@ -82,7 +82,7 @@ public: void SetValueAsLong( int row, int col, long value ) override { if( col == 0 ) - m_items[row].m_Layer = static_cast<int>( value ); + m_items[row].m_Layer = static_cast<PCB_LAYER_ID>( value ); } bool AppendRows( size_t aNumRows = 1 ) override @@ -102,7 +102,7 @@ public: if( IsUserLayer( static_cast<PCB_LAYER_ID>( layer ) ) ) { layers.insert( layer ); - m_items.emplace_back( wxT( "" ), true, layer ); + m_items.emplace_back( wxT( "" ), true, static_cast<PCB_LAYER_ID>( layer ) ); } else { @@ -205,7 +205,7 @@ public: void SetValueAsLong( int row, int col, long value ) override { if( col == 2 ) - m_items[row].m_Layer = (int) value; + m_items[row].m_Layer = static_cast<PCB_LAYER_ID>( value ); } bool AppendRows( size_t aNumRows = 1 ) override @@ -405,8 +405,8 @@ bool PANEL_FP_EDITOR_FIELD_DEFAULTS::TransferDataFromWindow() for( int i : { REFERENCE_FIELD, VALUE_FIELD } ) { wxString text = table->GetValue( i, 0 ); - bool visible = table->GetValueAsBool( i, 1 ); - int layer = (int) table->GetValueAsLong( i, 2 ); + bool visible = table->GetValueAsBool( i, 1 ); + PCB_LAYER_ID layer = static_cast<PCB_LAYER_ID>( table->GetValueAsLong( i, 2 ) ); cfg.m_DefaultFPTextItems.emplace_back( text, visible, layer ); } @@ -416,8 +416,8 @@ bool PANEL_FP_EDITOR_FIELD_DEFAULTS::TransferDataFromWindow() for( int i = 0; i < m_textItemsGrid->GetNumberRows(); ++i ) { wxString text = table->GetValue( i, 0 ); - bool visible = table->GetValueAsBool( i, 1 ); - int layer = (int) table->GetValueAsLong( i, 2 ); + bool visible = table->GetValueAsBool( i, 1 ); + PCB_LAYER_ID layer = static_cast<PCB_LAYER_ID>( table->GetValueAsLong( i, 2 ) ); cfg.m_DefaultFPTextItems.emplace_back( text, visible, layer ); } diff --git a/pcbnew/footprint_editor_settings.cpp b/pcbnew/footprint_editor_settings.cpp index 8a1f050ec7..17e7ca9d54 100644 --- a/pcbnew/footprint_editor_settings.cpp +++ b/pcbnew/footprint_editor_settings.cpp @@ -37,7 +37,7 @@ ///! Update the schema version whenever a migration is required -const int fpEditSchemaVersion = 4; +const int fpEditSchemaVersion = 5; FOOTPRINT_EDITOR_SETTINGS::FOOTPRINT_EDITOR_SETTINGS() : @@ -156,7 +156,7 @@ FOOTPRINT_EDITOR_SETTINGS::FOOTPRINT_EDITOR_SETTINGS() : { js.push_back( nlohmann::json( { item.m_Text.ToUTF8(), item.m_Visible, - item.m_Layer } ) ); + LSET::Name( item.m_Layer ) } ) ); } return js; @@ -177,15 +177,19 @@ FOOTPRINT_EDITOR_SETTINGS::FOOTPRINT_EDITOR_SETTINGS() : textInfo.m_Text = entry.at(0).get<wxString>(); textInfo.m_Visible = entry.at(1).get<bool>(); - textInfo.m_Layer = entry.at(2).get<int>(); + wxString layerName = entry.at(2).get<wxString>(); + int candidateLayer = LSET::NameToLayer( layerName ); + textInfo.m_Layer = candidateLayer >= 0 + ? static_cast<PCB_LAYER_ID>(candidateLayer) + : F_SilkS; m_DesignSettings.m_DefaultFPTextItems.push_back( std::move( textInfo ) ); } }, nlohmann::json::array( { - { "REF**", true, F_SilkS }, - { "", true, F_Fab }, - { "${REFERENCE}", true, F_Fab } + { "REF**", true, LSET::Name( F_SilkS ) }, + { "", true, LSET::Name( F_Fab ) }, + { "${REFERENCE}", true, LSET::Name( F_Fab ) } } ) ) ); m_params.emplace_back( new PARAM_MAP<wxString>( "design_settings.default_footprint_layer_names", @@ -377,6 +381,7 @@ FOOTPRINT_EDITOR_SETTINGS::FOOTPRINT_EDITOR_SETTINGS() : registerMigration( 2, 3, std::bind( &FOOTPRINT_EDITOR_SETTINGS::migrateSchema2To3, this ) ); registerMigration( 3, 4, std::bind( &FOOTPRINT_EDITOR_SETTINGS::migrateSchema3To4, this ) ); + registerMigration( 4, 5, std::bind( &FOOTPRINT_EDITOR_SETTINGS::migrateSchema4To5, this ) ); } @@ -569,3 +574,51 @@ bool FOOTPRINT_EDITOR_SETTINGS::migrateSchema3To4() return true; } + + +/** + * Schema version 5: move text defaults to used named layers + */ +bool FOOTPRINT_EDITOR_SETTINGS::migrateSchema4To5 () +{ + auto p( "/design_settings/default_footprint_text_items"_json_pointer ); + + if( !m_internals->contains( p ) || !m_internals->at( p ).is_array() ) + return true; + + nlohmann::json& defaults = m_internals->at( p ); + + bool reset = false; + + for( nlohmann::json& entry : defaults ) + { + TEXT_ITEM_INFO textInfo( wxT( "" ), true, F_SilkS ); + + textInfo.m_Text = entry.at(0).get<wxString>(); + textInfo.m_Visible = entry.at(1).get<bool>(); + textInfo.m_Layer = static_cast<PCB_LAYER_ID>( entry.at(2).get<int>() ); + + if( textInfo.m_Layer == Rescue || textInfo.m_Layer >= User_5 ) + { + // KiCad pre-9.0 nightlies would write buggy preferences out with invalid layers. + // If we detect that, reset to defaults + reset = true; + } + else + { + // Coming from 8.0 or earlier, just migrate to named layers + entry.at(2) = LSET::Name( textInfo.m_Layer ); + } + } + + if( reset ) + { + defaults = nlohmann::json::array( { + { "REF**", true, LSET::Name( F_SilkS ) }, + { "", true, LSET::Name( F_Fab ) }, + { "${REFERENCE}", true, LSET::Name( F_Fab ) } + } ); + } + + return true; +} diff --git a/pcbnew/footprint_libraries_utils.cpp b/pcbnew/footprint_libraries_utils.cpp index 6b8113382f..8e28abf731 100644 --- a/pcbnew/footprint_libraries_utils.cpp +++ b/pcbnew/footprint_libraries_utils.cpp @@ -1282,7 +1282,7 @@ FOOTPRINT* PCB_BASE_FRAME::CreateNewFootprint( wxString aFootprintName, const wx footprint->Reference().SetText( settings.m_DefaultFPTextItems[0].m_Text ); footprint->Reference().SetVisible( settings.m_DefaultFPTextItems[0].m_Visible ); - txt_layer = (PCB_LAYER_ID) settings.m_DefaultFPTextItems[0].m_Layer; + txt_layer = settings.m_DefaultFPTextItems[0].m_Layer; footprint->Reference().SetLayer( txt_layer ); default_pos.y -= settings.GetTextSize( txt_layer ).y / 2; footprint->Reference().SetPosition( default_pos ); @@ -1290,7 +1290,7 @@ FOOTPRINT* PCB_BASE_FRAME::CreateNewFootprint( wxString aFootprintName, const wx footprint->Value().SetText( settings.m_DefaultFPTextItems[1].m_Text ); footprint->Value().SetVisible( settings.m_DefaultFPTextItems[1].m_Visible ); - txt_layer = (PCB_LAYER_ID) settings.m_DefaultFPTextItems[1].m_Layer; + txt_layer = settings.m_DefaultFPTextItems[1].m_Layer; footprint->Value().SetLayer( txt_layer ); default_pos.y += settings.GetTextSize( txt_layer ).y / 2; footprint->Value().SetPosition( default_pos );