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.

139 lines
3.9 KiB

2 years ago
2 years ago
2 years ago
2 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2021 Jon Evans <jon@craftyjon.com>
  5. * Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software: you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation, either version 3 of the License, or (at your
  10. * option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <wx/string.h>
  21. #include <nlohmann/json.hpp>
  22. #include <project/project_file.h>
  23. #include <settings/parameters.h>
  24. void PARAM_PATH_LIST::Store( JSON_SETTINGS* aSettings ) const
  25. {
  26. nlohmann::json js = nlohmann::json::array();
  27. for( const auto& el : *m_ptr )
  28. js.push_back( toFileFormat( el ) );
  29. aSettings->Set<nlohmann::json>( m_path, js );
  30. }
  31. bool PARAM_PATH_LIST::MatchesFile( JSON_SETTINGS* aSettings ) const
  32. {
  33. if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
  34. {
  35. if( js->is_array() )
  36. {
  37. std::vector<wxString> val;
  38. for( const auto& el : js->items() )
  39. val.emplace_back( fromFileFormat( el.value().get<wxString>() ) );
  40. return val == *m_ptr;
  41. }
  42. }
  43. return false;
  44. }
  45. void PARAM_WXSTRING_MAP::Load( JSON_SETTINGS* aSettings, bool aResetIfMissing ) const
  46. {
  47. if( m_readOnly )
  48. return;
  49. if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
  50. {
  51. if( js->is_object() )
  52. {
  53. m_ptr->clear();
  54. for( const auto& el : js->items() )
  55. ( *m_ptr )[wxString( el.key().c_str(), wxConvUTF8 )] = el.value().get<wxString>();
  56. }
  57. }
  58. else if( aResetIfMissing )
  59. {
  60. *m_ptr = m_default;
  61. }
  62. }
  63. void PARAM_WXSTRING_MAP::Store( JSON_SETTINGS* aSettings ) const
  64. {
  65. nlohmann::json js( {} );
  66. for( const auto& el : *m_ptr )
  67. {
  68. std::string key( el.first.ToUTF8() );
  69. js[key] = el.second;
  70. }
  71. aSettings->Set<nlohmann::json>( m_path, js );
  72. }
  73. bool PARAM_WXSTRING_MAP::MatchesFile( JSON_SETTINGS* aSettings ) const
  74. {
  75. if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
  76. {
  77. if( js->is_object() )
  78. {
  79. if( m_ptr->size() != js->size() )
  80. return false;
  81. std::map<wxString, wxString> val;
  82. for( const auto& el : js->items() )
  83. {
  84. wxString key( el.key().c_str(), wxConvUTF8 );
  85. val[key] = el.value().get<wxString>();
  86. }
  87. return val == *m_ptr;
  88. }
  89. }
  90. return false;
  91. }
  92. #if !defined( __MINGW32__ )
  93. // Instantiate all required templates here and export
  94. template class KICOMMON_API PARAM_LAMBDA<bool>;
  95. template class KICOMMON_API PARAM_LAMBDA<int>;
  96. template class KICOMMON_API PARAM_LAMBDA<nlohmann::json>;
  97. template class KICOMMON_API PARAM_LAMBDA<std::string>;
  98. template class KICOMMON_API PARAM_LIST<bool>;
  99. template class KICOMMON_API PARAM_LIST<int>;
  100. template class KICOMMON_API PARAM_LIST<double>;
  101. template class KICOMMON_API PARAM_LIST<wxString>;
  102. template class KICOMMON_API PARAM_LIST<KIGFX::COLOR4D>;
  103. //template KICOMMON_API class PARAM_LIST<FILE_INFO_PAIR>;
  104. template class KICOMMON_API PARAM_LIST<struct BOM_PRESET>;
  105. template class KICOMMON_API PARAM_LIST<struct BOM_FMT_PRESET>;
  106. template class KICOMMON_API PARAM_LIST<GRID>;
  107. template class KICOMMON_API PARAM_SET<wxString>;
  108. template class KICOMMON_API PARAM_MAP<int>;
  109. template class KICOMMON_API PARAM_MAP<double>;
  110. template class KICOMMON_API PARAM_MAP<bool>;
  111. #endif