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 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 () const
 

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 171 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 305 of file parameters.h.

306  {
307  return m_default;
308  }

◆ GetJsonPath()

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

Definition at line 68 of file parameters.h.

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

References PARAM_BASE::m_path.

◆ 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.
std::vector< LAYER_PRESET > * m_presets
nlohmann::json json
Definition: gerbview.cpp:41

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
overridevirtualinherited

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 31 of file parameters.cpp.

32 {
33  if( m_readOnly )
34  return;
35 
36  if( std::is_same<ValueType, nlohmann::json>::value )
37  {
38  if( OPT<nlohmann::json> optval = aSettings->GetJson( m_path ) )
39  m_setter( *optval );
40  else
42  }
43  else
44  {
45  if( OPT<ValueType> optval = aSettings->Get<ValueType>( m_path ) )
46  m_setter( *optval );
47  else
49  }
50 }
std::string m_path
the string used to store the param in json files
Definition: parameters.h:74
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:322
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:77
boost::optional< T > OPT
Definition: optional.h:7

◆ MatchesFile()

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

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 54 of file parameters.cpp.

55 {
56  if( std::is_same<ValueType, nlohmann::json>::value )
57  {
58  if( OPT<nlohmann::json> optval = aSettings->GetJson( m_path ) )
59  return *optval == m_getter();
60  }
61  else
62  {
63  if( OPT<ValueType> optval = aSettings->Get<ValueType>( m_path ) )
64  return *optval == m_getter();
65  }
66 
67  // Not in file
68  return false;
69 }
std::string m_path
the string used to store the param in json files
Definition: parameters.h:74
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< nlohmann::json()> m_getter
Definition: parameters.h:320
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.
Definition: layer_ids.h:185
std::vector< LAYER_PRESET > * m_presets
nlohmann::json json
Definition: gerbview.cpp:41
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:65

References m_presets.

◆ SetDefault()

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

Implements PARAM_BASE.

Definition at line 310 of file parameters.h.

311  {
312  m_setter( m_default );
313  }
std::function< void(nlohmann::json)> m_setter
Definition: parameters.h:322

◆ 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 294 of file parameters.h.

295  {
296  try
297  {
298  aSettings->Set<ValueType>( m_path, m_getter() );
299  }
300  catch( ... )
301  {
302  }
303  }
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...
std::string m_path
the string used to store the param in json files
Definition: parameters.h:74
std::function< nlohmann::json()> m_getter
Definition: parameters.h:320

Member Data Documentation

◆ m_path

◆ m_presets

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

Definition at line 181 of file board_project_settings.h.

Referenced by jsonToPresets(), and presetsToJson().

◆ m_readOnly

bool PARAM_BASE::m_readOnly
protectedinherited

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