You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
7.4 KiB

6 years ago
6 years ago
6 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #include <gerbview_settings.h>
  24. #include <layer_ids.h>
  25. #include <pgm_base.h>
  26. #include <settings/common_settings.h>
  27. #include <settings/json_settings_internals.h>
  28. #include <settings/parameters.h>
  29. #include <settings/settings_manager.h>
  30. #include <wx/config.h>
  31. ///! Update the schema version whenever a migration is required
  32. const int gerbviewSchemaVersion = 0;
  33. GERBVIEW_SETTINGS::GERBVIEW_SETTINGS() :
  34. APP_SETTINGS_BASE( "gerbview", gerbviewSchemaVersion ),
  35. m_Appearance(),
  36. m_BoardLayersCount( 2 )
  37. {
  38. // Init settings:
  39. m_params.emplace_back( new PARAM<bool>( "appearance.show_border_and_titleblock",
  40. &m_Appearance.show_border_and_titleblock, false ) );
  41. m_params.emplace_back( new PARAM<bool>( "appearance.show_dcodes",
  42. &m_Appearance.show_dcodes, false ) );
  43. m_params.emplace_back( new PARAM<bool>( "appearance.show_negative_objects",
  44. &m_Appearance.show_negative_objects, false ) );
  45. m_params.emplace_back( new PARAM<wxString>( "appearance.page_type",
  46. &m_Appearance.page_type, "GERBER" ) );
  47. m_params.emplace_back( new PARAM<bool>( "appearance.show_page_limit",
  48. &m_Display.m_DisplayPageLimits, false ) );
  49. m_params.emplace_back( new PARAM<double>( "appearance.mode_opacity_value",
  50. &m_Display.m_OpacityModeAlphaValue, 0.6 ) );
  51. // WARNING: any change to this key MUST be reflected in GetFileHistories()
  52. m_params.emplace_back( new PARAM_LIST<wxString>( "system.drill_file_history",
  53. &m_DrillFileHistory, {} ) );
  54. // WARNING: any change to this key MUST be reflected in GetFileHistories()
  55. m_params.emplace_back( new PARAM_LIST<wxString>( "system.zip_file_history",
  56. &m_ZipFileHistory, {} ) );
  57. // WARNING: any change to this key MUST be reflected in GetFileHistories()
  58. m_params.emplace_back( new PARAM_LIST<wxString>( "system.job_file_history",
  59. &m_JobFileHistory, {} ) );
  60. m_params.emplace_back( new PARAM_LIST<int>( "gerber_to_pcb_layers",
  61. &m_GerberToPcbLayerMapping, {} ) );
  62. m_params.emplace_back( new PARAM<int>( "gerber_to_pcb_copperlayers_count",
  63. &m_BoardLayersCount, 2 ) );
  64. m_params.emplace_back( new PARAM<bool>( "excellon_defaults.unit_mm",
  65. &m_ExcellonDefaults.m_UnitsMM, false ) );
  66. m_params.emplace_back( new PARAM<bool>( "excellon_defaults.lz_format",
  67. &m_ExcellonDefaults.m_LeadingZero, true ) );
  68. m_params.emplace_back( new PARAM<int>( "excellon_defaults.mm_integer_len",
  69. &m_ExcellonDefaults.m_MmIntegerLen, FMT_INTEGER_MM, 2 , 6 ) );
  70. m_params.emplace_back( new PARAM<int>( "excellon_defaults.mm_mantissa_len",
  71. &m_ExcellonDefaults.m_MmMantissaLen, FMT_MANTISSA_MM, 2 , 6 ) );
  72. m_params.emplace_back( new PARAM<int>( "excellon_defaults.inch_integer_len",
  73. &m_ExcellonDefaults.m_InchIntegerLen, FMT_INTEGER_INCH, 2 , 6 ) );
  74. m_params.emplace_back( new PARAM<int>( "excellon_defaults.inch_mantissa_len",
  75. &m_ExcellonDefaults.m_InchMantissaLen, FMT_MANTISSA_INCH, 2 , 6 ) );
  76. }
  77. bool GERBVIEW_SETTINGS::MigrateFromLegacy( wxConfigBase* aCfg )
  78. {
  79. bool ret = APP_SETTINGS_BASE::MigrateFromLegacy( aCfg );
  80. ret &= fromLegacy<bool>( aCfg,
  81. "ShowBorderAndTitleBlock", "appearance.show_border_and_titleblock" );
  82. ret &= fromLegacy<bool>( aCfg, "ShowDCodesOpt", "appearance.show_dcodes" );
  83. ret &= fromLegacy<bool>( aCfg, "ShowNegativeObjectsOpt", "appearance.show_negative_objects" );
  84. ret &= fromLegacyString( aCfg, "PageSizeOpt", "appearance.page_type" );
  85. auto migrate_files = [&] ( const std::string& aPath, const std::string& aDest )
  86. {
  87. int max_history_size = Pgm().GetCommonSettings()->m_System.file_history_size;
  88. wxString file, key;
  89. nlohmann::json js = nlohmann::json::array();
  90. aCfg->SetPath( aPath );
  91. for( int i = 0; i < max_history_size; i++ )
  92. {
  93. key.Printf( wxT( "file%d" ), i );
  94. file = aCfg->Read( key, wxEmptyString );
  95. if( !file.IsEmpty() )
  96. js.emplace_back( file.ToStdString() );
  97. }
  98. aCfg->SetPath( wxT( ".." ) );
  99. Set( aDest, js );
  100. };
  101. migrate_files( "drl_files", "system.drill_file_history" );
  102. migrate_files( "zip_files", "system.zip_file_history" );
  103. migrate_files( "job_files", "system.job_file_history" );
  104. {
  105. wxString key;
  106. int value = 0;
  107. Set( "gerber_to_pcb_layers", nlohmann::json::array() );
  108. for( int i = 0; i < GERBER_DRAWLAYERS_COUNT; i++ )
  109. {
  110. key.Printf( wxT( "GbrLyr%dToPcb" ), i );
  111. aCfg->Read( key, &value, UNSELECTED_LAYER );
  112. At( "gerber_to_pcb_layers" ).emplace_back( value );
  113. }
  114. }
  115. COLOR_SETTINGS* cs = Pgm().GetSettingsManager().GetMigratedColorSettings();
  116. auto migrateLegacyColor = [&] ( const std::string& aKey, int aLayerId ) {
  117. wxString str;
  118. if( aCfg->Read( aKey, &str ) )
  119. cs->SetColor( aLayerId, COLOR4D( str ) );
  120. };
  121. migrateLegacyColor( "BackgroundColorEx", LAYER_GERBVIEW_BACKGROUND );
  122. migrateLegacyColor( "DCodeColorEx", LAYER_DCODES );
  123. migrateLegacyColor( "GridColorEx", LAYER_GERBVIEW_GRID );
  124. migrateLegacyColor( "NegativeObjectsColorEx", LAYER_NEGATIVE_OBJECTS );
  125. migrateLegacyColor( "WorksheetColorEx", LAYER_GERBVIEW_DRAWINGSHEET );
  126. migrateLegacyColor( "GridColorEx", LAYER_GERBVIEW_PAGE_LIMITS );
  127. wxString key;
  128. for( int i = 0, id = GERBVIEW_LAYER_ID_START;
  129. id < GERBER_DRAWLAYERS_COUNT + GERBVIEW_LAYER_ID_START; ++i, ++id )
  130. {
  131. key.Printf( wxT( "ColorLayer%dEx" ), i );
  132. migrateLegacyColor( key.ToStdString(), id );
  133. }
  134. Pgm().GetSettingsManager().SaveColorSettings( cs, "gerbview" );
  135. Set( "appearance.color_theme", cs->GetFilename() );
  136. return ret;
  137. }
  138. std::map<std::string, nlohmann::json> GERBVIEW_SETTINGS::GetFileHistories()
  139. {
  140. std::map<std::string, nlohmann::json> histories = JSON_SETTINGS::GetFileHistories();
  141. for( const std::string& candidate : { std::string("system.drill_file_history"),
  142. std::string("system.zip_file_history"),
  143. std::string("system.job_file_history") } )
  144. {
  145. if( Contains( candidate ) )
  146. histories[candidate] = GetJson( candidate ).value();
  147. }
  148. return histories;
  149. }