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.

305 lines
9.5 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2020 Jon Evans <jon@craftyjon.com>
  5. * Copyright (C) 2020-2022 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 <functional>
  21. #include <project/board_project_settings.h>
  22. using namespace std::placeholders;
  23. PARAM_LAYER_PRESET::PARAM_LAYER_PRESET( const std::string& aPath,
  24. std::vector<LAYER_PRESET>* aPresetList ) :
  25. PARAM_LAMBDA<nlohmann::json>( aPath,
  26. std::bind( &PARAM_LAYER_PRESET::presetsToJson, this ),
  27. std::bind( &PARAM_LAYER_PRESET::jsonToPresets, this, _1 ),
  28. {} ),
  29. m_presets( aPresetList )
  30. {
  31. wxASSERT( aPresetList );
  32. }
  33. nlohmann::json PARAM_LAYER_PRESET::presetsToJson()
  34. {
  35. nlohmann::json ret = nlohmann::json::array();
  36. for( const LAYER_PRESET& preset : *m_presets )
  37. {
  38. nlohmann::json js = {
  39. { "name", preset.name },
  40. { "activeLayer", preset.activeLayer }
  41. };
  42. nlohmann::json layers = nlohmann::json::array();
  43. for( PCB_LAYER_ID layer : preset.layers.Seq() )
  44. layers.push_back( static_cast<int>( layer ) );
  45. js["layers"] = layers;
  46. nlohmann::json renderLayers = nlohmann::json::array();
  47. for( GAL_LAYER_ID layer : preset.renderLayers.Seq() )
  48. renderLayers.push_back( static_cast<int>( layer ) );
  49. js["renderLayers"] = renderLayers;
  50. ret.push_back( js );
  51. }
  52. return ret;
  53. }
  54. void PARAM_LAYER_PRESET::jsonToPresets( const nlohmann::json& aJson )
  55. {
  56. if( aJson.empty() || !aJson.is_array() )
  57. return;
  58. m_presets->clear();
  59. for( const nlohmann::json& preset : aJson )
  60. {
  61. if( preset.contains( "name" ) )
  62. {
  63. LAYER_PRESET p( preset.at( "name" ).get<wxString>() );
  64. if( preset.contains( "activeLayer" ) &&
  65. preset.at( "activeLayer" ).is_number_integer() )
  66. {
  67. int active = preset.at( "activeLayer" ).get<int>();
  68. if( active >= 0 && active < PCB_LAYER_ID_COUNT )
  69. p.activeLayer = static_cast<PCB_LAYER_ID>( active );
  70. }
  71. if( preset.contains( "layers" ) && preset.at( "layers" ).is_array() )
  72. {
  73. p.layers.reset();
  74. for( const nlohmann::json& layer : preset.at( "layers" ) )
  75. {
  76. if( layer.is_number_integer() )
  77. {
  78. int layerNum = layer.get<int>();
  79. if( layerNum >= 0 && layerNum < PCB_LAYER_ID_COUNT )
  80. p.layers.set( layerNum );
  81. }
  82. }
  83. }
  84. if( preset.contains( "renderLayers" )
  85. && preset.at( "renderLayers" ).is_array() )
  86. {
  87. p.renderLayers.reset();
  88. for( const nlohmann::json& layer : preset.at( "renderLayers" ) )
  89. {
  90. if( layer.is_number_integer() )
  91. {
  92. int layerNum = layer.get<int>();
  93. if( layerNum >= GAL_LAYER_ID_START
  94. && layerNum < GAL_LAYER_ID_END )
  95. p.renderLayers.set( static_cast<GAL_LAYER_ID>( layerNum ) );
  96. }
  97. }
  98. }
  99. m_presets->emplace_back( p );
  100. }
  101. }
  102. }
  103. PARAM_VIEWPORT::PARAM_VIEWPORT( const std::string& aPath, std::vector<VIEWPORT>* aViewportList ) :
  104. PARAM_LAMBDA<nlohmann::json>( aPath,
  105. std::bind( &PARAM_VIEWPORT::viewportsToJson, this ),
  106. std::bind( &PARAM_VIEWPORT::jsonToViewports, this, _1 ),
  107. {} ),
  108. m_viewports( aViewportList )
  109. {
  110. wxASSERT( aViewportList );
  111. }
  112. nlohmann::json PARAM_VIEWPORT::viewportsToJson()
  113. {
  114. nlohmann::json ret = nlohmann::json::array();
  115. for( const VIEWPORT& viewport : *m_viewports )
  116. {
  117. nlohmann::json js = {
  118. { "name", viewport.name },
  119. { "x", viewport.rect.GetX() },
  120. { "y", viewport.rect.GetY() },
  121. { "w", viewport.rect.GetWidth() },
  122. { "h", viewport.rect.GetHeight() }
  123. };
  124. ret.push_back( js );
  125. }
  126. return ret;
  127. }
  128. void PARAM_VIEWPORT::jsonToViewports( const nlohmann::json& aJson )
  129. {
  130. if( aJson.empty() || !aJson.is_array() )
  131. return;
  132. m_viewports->clear();
  133. for( const nlohmann::json& viewport : aJson )
  134. {
  135. if( viewport.contains( "name" ) )
  136. {
  137. VIEWPORT v( viewport.at( "name" ).get<wxString>() );
  138. if( viewport.contains( "x" ) )
  139. v.rect.SetX( viewport.at( "x" ).get<double>() );
  140. if( viewport.contains( "y" ) )
  141. v.rect.SetY( viewport.at( "y" ).get<double>() );
  142. if( viewport.contains( "w" ) )
  143. v.rect.SetWidth( viewport.at( "w" ).get<double>() );
  144. if( viewport.contains( "h" ) )
  145. v.rect.SetHeight( viewport.at( "h" ).get<double>() );
  146. m_viewports->emplace_back( v );
  147. }
  148. }
  149. }
  150. PARAM_VIEWPORT3D::PARAM_VIEWPORT3D( const std::string& aPath,
  151. std::vector<VIEWPORT3D>* aViewportList ) :
  152. PARAM_LAMBDA<nlohmann::json>( aPath,
  153. std::bind( &PARAM_VIEWPORT3D::viewportsToJson, this ),
  154. std::bind( &PARAM_VIEWPORT3D::jsonToViewports, this, _1 ),
  155. {} ),
  156. m_viewports( aViewportList )
  157. {
  158. wxASSERT( aViewportList );
  159. }
  160. nlohmann::json PARAM_VIEWPORT3D::viewportsToJson()
  161. {
  162. nlohmann::json ret = nlohmann::json::array();
  163. for( const VIEWPORT3D& viewport : *m_viewports )
  164. {
  165. nlohmann::json js = {
  166. { "name", viewport.name },
  167. { "xx", viewport.matrix[0].x },
  168. { "xy", viewport.matrix[0].y },
  169. { "xz", viewport.matrix[0].z },
  170. { "xw", viewport.matrix[0].w },
  171. { "yx", viewport.matrix[1].x },
  172. { "yy", viewport.matrix[1].y },
  173. { "yz", viewport.matrix[1].z },
  174. { "yw", viewport.matrix[1].w },
  175. { "zx", viewport.matrix[2].x },
  176. { "zy", viewport.matrix[2].y },
  177. { "zz", viewport.matrix[2].z },
  178. { "zw", viewport.matrix[2].w },
  179. { "wx", viewport.matrix[3].x },
  180. { "wy", viewport.matrix[3].y },
  181. { "wz", viewport.matrix[3].z },
  182. { "ww", viewport.matrix[3].w }
  183. };
  184. ret.push_back( js );
  185. }
  186. return ret;
  187. }
  188. void PARAM_VIEWPORT3D::jsonToViewports( const nlohmann::json& aJson )
  189. {
  190. if( aJson.empty() || !aJson.is_array() )
  191. return;
  192. m_viewports->clear();
  193. for( const nlohmann::json& viewport : aJson )
  194. {
  195. if( viewport.contains( "name" ) )
  196. {
  197. VIEWPORT3D v( viewport.at( "name" ).get<wxString>() );
  198. if( viewport.contains( "xx" ) )
  199. v.matrix[0].x = viewport.at( "xx" ).get<double>();
  200. if( viewport.contains( "xy" ) )
  201. v.matrix[0].y = viewport.at( "xy" ).get<double>();
  202. if( viewport.contains( "xz" ) )
  203. v.matrix[0].z = viewport.at( "xz" ).get<double>();
  204. if( viewport.contains( "xw" ) )
  205. v.matrix[0].w = viewport.at( "xw" ).get<double>();
  206. if( viewport.contains( "yx" ) )
  207. v.matrix[1].x = viewport.at( "yx" ).get<double>();
  208. if( viewport.contains( "yy" ) )
  209. v.matrix[1].y = viewport.at( "yy" ).get<double>();
  210. if( viewport.contains( "yz" ) )
  211. v.matrix[1].z = viewport.at( "yz" ).get<double>();
  212. if( viewport.contains( "yw" ) )
  213. v.matrix[1].w = viewport.at( "yw" ).get<double>();
  214. if( viewport.contains( "zx" ) )
  215. v.matrix[2].x = viewport.at( "zx" ).get<double>();
  216. if( viewport.contains( "zy" ) )
  217. v.matrix[2].y = viewport.at( "zy" ).get<double>();
  218. if( viewport.contains( "zz" ) )
  219. v.matrix[2].z = viewport.at( "zz" ).get<double>();
  220. if( viewport.contains( "zw" ) )
  221. v.matrix[2].w = viewport.at( "zw" ).get<double>();
  222. if( viewport.contains( "wx" ) )
  223. v.matrix[3].x = viewport.at( "wx" ).get<double>();
  224. if( viewport.contains( "wy" ) )
  225. v.matrix[3].y = viewport.at( "wy" ).get<double>();
  226. if( viewport.contains( "wz" ) )
  227. v.matrix[3].z = viewport.at( "wz" ).get<double>();
  228. if( viewport.contains( "ww" ) )
  229. v.matrix[3].w = viewport.at( "ww" ).get<double>();
  230. m_viewports->emplace_back( v );
  231. }
  232. }
  233. }