diff --git a/common/settings/nested_settings.cpp b/common/settings/nested_settings.cpp index 048a8d3159..e98ff4893e 100644 --- a/common/settings/nested_settings.cpp +++ b/common/settings/nested_settings.cpp @@ -64,11 +64,45 @@ bool NESTED_SETTINGS::LoadFromFile( const wxString& aDirectory ) catch( ... ) { wxLogTrace( traceSettings, "NESTED_SETTINGS %s: Could not load from %s at %s", - m_filename, m_parent->GetFilename(), m_path ); + m_filename, m_parent->GetFilename(), m_path ); } } } + if( success ) + { + int filever = -1; + + try + { + filever = at( PointerFromString( "meta.version" ) ).get(); + } + catch( ... ) + { + wxLogTrace( traceSettings, "%s: nested settings version could not be read!", + m_filename ); + success = false; + } + + if( filever >= 0 && filever < m_schemaVersion ) + { + wxLogTrace( traceSettings, "%s: attempting migration from version %d to %d", + m_filename, filever, m_schemaVersion ); + + if( !Migrate() ) + { + wxLogTrace( traceSettings, "%s: migration failed!", GetFullFilename() ); + success = false; + } + } + else if( filever > m_schemaVersion ) + { + wxLogTrace( traceSettings, + "%s: warning: nested settings version %d is newer than latest (%d)", + m_filename, filever, m_schemaVersion ); + } + } + Load(); return success; diff --git a/include/board_design_settings.h b/include/board_design_settings.h index 7f04c05623..65efe3188f 100644 --- a/include/board_design_settings.h +++ b/include/board_design_settings.h @@ -327,6 +327,8 @@ private: void initFromOther( const BOARD_DESIGN_SETTINGS& aOther ); + bool migrateSchema0to1(); + public: BOARD_DESIGN_SETTINGS( JSON_SETTINGS* aParent, const std::string& aPath ); @@ -338,6 +340,8 @@ public: bool LoadFromFile( const wxString& aDirectory = "" ) override; + bool Migrate() override; + BOARD_STACKUP& GetStackupDescriptor() { return m_stackup; } int GetSeverity( int aDRCErrorCode ); diff --git a/pcbnew/board_design_settings.cpp b/pcbnew/board_design_settings.cpp index 133ef9f852..4e44ca6411 100644 --- a/pcbnew/board_design_settings.cpp +++ b/pcbnew/board_design_settings.cpp @@ -35,7 +35,7 @@ #include #include -const int bdsSchemaVersion = 0; +const int bdsSchemaVersion = 1; BOARD_DESIGN_SETTINGS::BOARD_DESIGN_SETTINGS( JSON_SETTINGS* aParent, const std::string& aPath ) : @@ -393,7 +393,7 @@ BOARD_DESIGN_SETTINGS::BOARD_DESIGN_SETTINGS( JSON_SETTINGS* aParent, const std: if( !aObj.is_array() ) return; - m_DiffPairDimensionsList.clear(); + m_DiffPairDimensionsList.clear(); for( const nlohmann::json& entry : aObj ) { @@ -514,7 +514,7 @@ BOARD_DESIGN_SETTINGS::BOARD_DESIGN_SETTINGS( JSON_SETTINGS* aParent, const std: &m_DimensionUnits, 0, 0, 2 ) ); m_params.emplace_back( new PARAM( "defaults.dimension_precision", - &m_DimensionPrecision, 1, 0, 2 ) ); + &m_DimensionPrecision, 4, 0, 5 ) ); m_params.emplace_back( new PARAM( "defaults.zones.45_degree_only", &m_defaultZoneSettings.m_Zone_45_Only, false ) ); @@ -665,6 +665,76 @@ void BOARD_DESIGN_SETTINGS::initFromOther( const BOARD_DESIGN_SETTINGS& aOther ) } +bool BOARD_DESIGN_SETTINGS::Migrate() +{ + bool ret = true; + int filever = at( PointerFromString( "meta.version" ) ).get(); + + if( filever == 0 ) + { + ret &= migrateSchema0to1(); + + if( ret ) + { + ( *this )[PointerFromString( "meta.version" )] = 1; + } + } + + return ret; +} + + +bool BOARD_DESIGN_SETTINGS::migrateSchema0to1() +{ + /** + * Schema 0 to 1: default dimension precision changed in meaning. + * Previously it was an enum with the following meaning: + * + * 0: 0.01mm / 1 mil / 0.001 in + * 1: 0.001mm / 0.1 mil / 0.0001 in + * 2: 0.0001mm / 0.01 mil / 0.00001 in + * + * Now it is indepenent of display units and is an integer meaning the number of digits + * displayed after the decimal point, so we have to migrate based on the default units. + * + * The units is an integer with the following mapping: + * + * 0: Inches + * 1: Mils + * 2: Millimetres + */ + nlohmann::json::json_pointer units_ptr( "/defaults/dimension_units" ); + nlohmann::json::json_pointer precision_ptr( "/defaults/dimension_precision" ); + + if( !( contains( units_ptr ) && contains( precision_ptr ) && + at( units_ptr ).is_number_integer() && + at( precision_ptr ).is_number_integer() ) ) + { + // if either is missing or invalid, migration doesn't make sense + return true; + } + + int units = at( units_ptr ).get(); + int precision = at( precision_ptr ).get(); + + // The enum maps directly to precision if the units is mils + int extraDigits = 0; + + switch( units ) + { + case 0: extraDigits = 3; break; + case 2: extraDigits = 2; break; + default: break; + } + + precision += extraDigits; + + ( *this )[precision_ptr] = precision; + + return true; +} + + bool BOARD_DESIGN_SETTINGS::LoadFromFile( const wxString& aDirectory ) { bool ret = NESTED_SETTINGS::LoadFromFile( aDirectory ); diff --git a/pcbnew/dialogs/dialog_dimension_properties_base.cpp b/pcbnew/dialogs/dialog_dimension_properties_base.cpp index 2ba5008149..e339bab023 100644 --- a/pcbnew/dialogs/dialog_dimension_properties_base.cpp +++ b/pcbnew/dialogs/dialog_dimension_properties_base.cpp @@ -102,7 +102,7 @@ DIALOG_DIMENSION_PROPERTIES_BASE::DIALOG_DIMENSION_PROPERTIES_BASE( wxWindow* pa gbSizerFormat->Add( m_lblPrecision, wxGBPosition( 2, 4 ), wxGBSpan( 1, 1 ), wxALIGN_CENTER_VERTICAL|wxALL, 5 ); - wxString m_cbPrecisionChoices[] = { _("0.0000"), _("0.000"), _("0.00"), _("0.0"), _("0") }; + wxString m_cbPrecisionChoices[] = { _("0"), _("0.0"), _("0.00"), _("0.000"), _("0.0000"), _("0.00000") }; int m_cbPrecisionNChoices = sizeof( m_cbPrecisionChoices ) / sizeof( wxString ); m_cbPrecision = new wxChoice( sbSizerFormat->GetStaticBox(), wxID_ANY, wxDefaultPosition, wxDefaultSize, m_cbPrecisionNChoices, m_cbPrecisionChoices, 0 ); m_cbPrecision->SetSelection( 0 ); diff --git a/pcbnew/dialogs/dialog_dimension_properties_base.fbp b/pcbnew/dialogs/dialog_dimension_properties_base.fbp index 90d16e782a..6b7d532835 100644 --- a/pcbnew/dialogs/dialog_dimension_properties_base.fbp +++ b/pcbnew/dialogs/dialog_dimension_properties_base.fbp @@ -908,7 +908,7 @@ 1 0 - "0.0000" "0.000" "0.00" "0.0" "0" + "0" "0.0" "0.00" "0.000" "0.0000" "0.00000" 1 1