KiCad PCB EDA Suite
PARAM_LAYER_PRESET Class Reference

#include <board_project_settings.h>

Inheritance diagram for PARAM_LAYER_PRESET:
PARAM_LAMBDA< nlohmann::json > PARAM_BASE

Public Member Functions

 PARAM_LAYER_PRESET (const std::string &aPath, std::vector< LAYER_PRESET > *aPresetList)
 
void Load (JSON_SETTINGS *aSettings, bool aResetIfMissing=true) const override
 Loads the value of this parameter from JSON to the underlying storage. More...
 
void Store (JSON_SETTINGS *aSettings) const override
 Stores the value of this parameter to the given JSON_SETTINGS object. More...
 
nlohmann::json GetDefault () const
 
void SetDefault () override
 
bool IsDefault () const override
 Checks whether or not this param has been changed from its default value. More...
 
bool MatchesFile (JSON_SETTINGS *aSettings) const override
 Checks whether the parameter in memory matches the one in a given JSON file. More...
 
const std::string & GetJsonPath ()
 

Protected Attributes

std::string m_path
 the string used to store the param in json files More...
 
bool m_readOnly
 ! True if the parameter pointer should never be overwritten More...
 

Private Member Functions

nlohmann::json presetsToJson ()
 
void jsonToPresets (const nlohmann::json &aJson)
 

Private Attributes

std::vector< LAYER_PRESET > * m_presets
 

Detailed Description

Definition at line 164 of file board_project_settings.h.

Constructor & Destructor Documentation

◆ PARAM_LAYER_PRESET()

PARAM_LAYER_PRESET::PARAM_LAYER_PRESET ( const std::string &  aPath,
std::vector< LAYER_PRESET > *  aPresetList 
)

Definition at line 27 of file board_project_settings.cpp.

28  :
30  std::bind( &PARAM_LAYER_PRESET::presetsToJson, this ),
31  std::bind( &PARAM_LAYER_PRESET::jsonToPresets, this, _1 ),
32  {} ),
33  m_presets( aPresetList )
34 {
35  wxASSERT( aPresetList );
36 }
std::vector< LAYER_PRESET > * m_presets
void jsonToPresets(const nlohmann::json &aJson)
nlohmann::json presetsToJson()

Member Function Documentation

◆ GetDefault()

nlohmann::json PARAM_LAMBDA< nlohmann::json >::GetDefault ( ) const
inlineinherited

Definition at line 340 of file parameters.h.

341  {
342  return m_default;
343  }

◆ GetJsonPath()

const std::string& PARAM_BASE::GetJsonPath ( )
inlineinherited
Returns
the path name of the parameter used to store it in the json file mainly usefull in error messages

Definition at line 74 of file parameters.h.

74 { return m_path; }
std::string m_path
the string used to store the param in json files
Definition: parameters.h:80

References PARAM_BASE::m_path.

◆ IsDefault()

bool PARAM_LAMBDA< nlohmann::json >::IsDefault ( ) const
inlineoverridevirtualinherited

Checks whether or not this param has been changed from its default value.

Returns
true if the parameter in memory matches its default value

Implements PARAM_BASE.

Definition at line 350 of file parameters.h.

351  {
352  return m_getter() == m_default;
353  }
std::function< nlohmann::json()> m_getter
Definition: parameters.h:375

◆ jsonToPresets()

void PARAM_LAYER_PRESET::jsonToPresets ( const nlohmann::json aJson)
private

Definition at line 71 of file board_project_settings.cpp.

72 {
73  if( aJson.empty() || !aJson.is_array() )
74  return;
75 
76  m_presets->clear();
77 
78  for( const nlohmann::json& preset : aJson )
79  {
80  if( preset.contains( "name" ) )
81  {
82  LAYER_PRESET p( preset.at( "name" ).get<wxString>() );
83 
84  if( preset.contains( "activeLayer" ) &&
85  preset.at( "activeLayer" ).is_number_integer() )
86  {
87  int active = preset.at( "activeLayer" ).get<int>();
88 
89  if( active >= 0 && active < PCB_LAYER_ID_COUNT )
90  p.activeLayer = static_cast<PCB_LAYER_ID>( active );
91  }
92 
93  if( preset.contains( "layers" ) && preset.at( "layers" ).is_array() )
94  {
95  p.layers.reset();
96 
97  for( const nlohmann::json& layer : preset.at( "layers" ) )
98  {
99  if( layer.is_number_integer() )
100  {
101  int layerNum = layer.get<int>();
102 
103  if( layerNum >= 0 && layerNum < PCB_LAYER_ID_COUNT )
104  p.layers.set( layerNum );
105  }
106  }
107  }
108 
109  if( preset.contains( "renderLayers" )
110  && preset.at( "renderLayers" ).is_array() )
111  {
112  p.renderLayers.reset();
113 
114  for( const nlohmann::json& layer : preset.at( "renderLayers" ) )
115  {
116  if( layer.is_number_integer() )
117  {
118  int layerNum = layer.get<int>();
119 
120  if( layerNum >= GAL_LAYER_ID_START
121  && layerNum < GAL_LAYER_ID_END )
122  p.renderLayers.set( static_cast<GAL_LAYER_ID>( layerNum ) );
123  }
124  }
125  }
126 
127  m_presets->emplace_back( p );
128  }
129  }
130 }
A saved set of layers that are visible.
Add new GAL layers here.
std::vector< LAYER_PRESET > * m_presets
nlohmann::json json
Definition: gerbview.cpp:39

References LAYER_PRESET::activeLayer, GAL_LAYER_ID_END, GAL_LAYER_ID_START, LAYER_PRESET::layers, m_presets, PCB_LAYER_ID_COUNT, LAYER_PRESET::renderLayers, and GAL_SET::set().

◆ Load()

void PARAM_LAMBDA< nlohmann::json >::Load ( JSON_SETTINGS aSettings,
bool  aResetIfMissing = true 
) const
inlineoverridevirtualinherited

Loads the value of this parameter from JSON to the underlying storage.

Parameters
aSettingsis the JSON_SETTINGS object to load from.
aResetIfMissingif true will set the parameter to its default value if load fails

Implements PARAM_BASE.

Definition at line 308 of file parameters.h.

309  {
310  if( m_readOnly )
311  return;
312 
313  if( std::is_same<ValueType, nlohmann::json>::value )
314  {
315  if( OPT<nlohmann::json> optval = aSettings->GetJson( m_path ) )
316  m_setter( *optval );
317  else
318  m_setter( m_default );
319  }
320  else
321  {
322  if( OPT<ValueType> optval = aSettings->Get<ValueType>( m_path ) )
323  m_setter( *optval );
324  else
325  m_setter( m_default );
326  }
327  }
std::string m_path
the string used to store the param in json files
Definition: parameters.h:80
OPT< nlohmann::json > GetJson(const std::string &aPath) const
Fetches a JSON object that is a subset of this JSON_SETTINGS object, using a path of the form "key1....
std::function< void(nlohmann::json)> m_setter
Definition: parameters.h:377
OPT< ValueType > Get(const std::string &aPath) const
Fetches a value from within the JSON document.
bool m_readOnly
! True if the parameter pointer should never be overwritten
Definition: parameters.h:83
boost::optional< T > OPT
Definition: optional.h:7

◆ MatchesFile()

bool PARAM_LAMBDA< nlohmann::json >::MatchesFile ( JSON_SETTINGS aSettings) const
inlineoverridevirtualinherited

Checks whether the parameter in memory matches the one in a given JSON file.

Parameters
aSettingsis a JSON_SETTINGS to check the JSON file contents of
Returns
true if the parameter in memory matches its value in the file

Implements PARAM_BASE.

Definition at line 355 of file parameters.h.

356  {
357  if( std::is_same<ValueType, nlohmann::json>::value )
358  {
359  if( OPT<nlohmann::json> optval = aSettings->GetJson( m_path ) )
360  return *optval == m_default;
361  }
362  else
363  {
364  if( OPT<ValueType> optval = aSettings->Get<ValueType>( m_path ) )
365  return *optval == m_default;
366  }
367 
368  // Not in file
369  return false;
370  }
std::string m_path
the string used to store the param in json files
Definition: parameters.h:80
OPT< nlohmann::json > GetJson(const std::string &aPath) const
Fetches a JSON object that is a subset of this JSON_SETTINGS object, using a path of the form "key1....
OPT< ValueType > Get(const std::string &aPath) const
Fetches a value from within the JSON document.
boost::optional< T > OPT
Definition: optional.h:7

◆ presetsToJson()

nlohmann::json PARAM_LAYER_PRESET::presetsToJson ( )
private

Definition at line 39 of file board_project_settings.cpp.

40 {
41  nlohmann::json ret = nlohmann::json::array();
42 
43  for( const LAYER_PRESET& preset : *m_presets )
44  {
45  nlohmann::json js = {
46  { "name", preset.name },
47  { "activeLayer", preset.activeLayer }
48  };
49 
50  nlohmann::json layers = nlohmann::json::array();
51 
52  for( PCB_LAYER_ID layer : preset.layers.Seq() )
53  layers.push_back( static_cast<int>( layer ) );
54 
55  js["layers"] = layers;
56 
57  nlohmann::json renderLayers = nlohmann::json::array();
58 
59  for( GAL_LAYER_ID layer : preset.renderLayers.Seq() )
60  renderLayers.push_back( static_cast<int>( layer ) );
61 
62  js["renderLayers"] = renderLayers;
63 
64  ret.push_back( js );
65  }
66 
67  return ret;
68 }
A saved set of layers that are visible.
GAL_LAYER_ID
GAL layers are "virtual" layers, i.e.
std::vector< LAYER_PRESET > * m_presets
nlohmann::json json
Definition: gerbview.cpp:39
PCB_LAYER_ID
A quick note on layer IDs:

References m_presets.

◆ SetDefault()

void PARAM_LAMBDA< nlohmann::json >::SetDefault ( )
inlineoverridevirtualinherited

Implements PARAM_BASE.

Definition at line 345 of file parameters.h.

346  {
347  m_setter( m_default );
348  }
std::function< void(nlohmann::json)> m_setter
Definition: parameters.h:377

◆ Store()

void PARAM_LAMBDA< nlohmann::json >::Store ( JSON_SETTINGS aSettings) const
inlineoverridevirtualinherited

Stores the value of this parameter to the given JSON_SETTINGS object.

Parameters
aSettingsis the JSON_SETTINGS object to store into.

Implements PARAM_BASE.

Definition at line 329 of file parameters.h.

330  {
331  try
332  {
333  aSettings->Set<ValueType>( m_path, m_getter() );
334  }
335  catch( ... )
336  {
337  }
338  }
std::string m_path
the string used to store the param in json files
Definition: parameters.h:80
std::function< nlohmann::json()> m_getter
Definition: parameters.h:375
void Set(const std::string &aPath, ValueType aVal)
Stores a value into the JSON document Will throw an exception if ValueType isn't something that the l...

Member Data Documentation

◆ m_path

◆ m_presets

std::vector<LAYER_PRESET>* PARAM_LAYER_PRESET::m_presets
private

Definition at line 174 of file board_project_settings.h.

Referenced by jsonToPresets(), and presetsToJson().

◆ m_readOnly


The documentation for this class was generated from the following files: