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
 
nlohmann::json m_default
 
std::function< nlohmann::json()> m_getter
 
std::function< void(nlohmann::json)> m_setter
 

Detailed Description

Definition at line 173 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}
nlohmann::json presetsToJson()
std::vector< LAYER_PRESET > * m_presets
void jsonToPresets(const nlohmann::json &aJson)

Member Function Documentation

◆ GetDefault()

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

Definition at line 306 of file parameters.h.

307 {
308 return m_default;
309 }

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

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

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}
nlohmann::json json
Definition: gerbview.cpp:44
GAL_LAYER_ID
GAL layers are "virtual" layers, i.e.
Definition: layer_ids.h:190
@ GAL_LAYER_ID_START
Definition: layer_ids.h:191
@ GAL_LAYER_ID_END
Definition: layer_ids.h:260
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:59
@ PCB_LAYER_ID_COUNT
Definition: layer_ids.h:137
A saved set of layers that are visible.

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

32{
33 if( m_readOnly )
34 return;
35
36 if( std::is_same<ValueType, nlohmann::json>::value )
37 {
38 if( std::optional<nlohmann::json> optval = aSettings->GetJson( m_path ) )
39 m_setter( *optval );
40 else
42 }
43 else
44 {
45 if( std::optional<ValueType> optval = aSettings->Get<ValueType>( m_path ) )
46 m_setter( *optval );
47 else
49 }
50}
std::optional< 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::optional< 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:78
std::function< void(nlohmann::json)> m_setter
Definition: parameters.h:323

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

55{
56 if( std::is_same<ValueType, nlohmann::json>::value )
57 {
58 if( std::optional<nlohmann::json> optval = aSettings->GetJson( m_path ) )
59 return *optval == m_getter();
60 }
61 else
62 {
63 if( std::optional<ValueType> optval = aSettings->Get<ValueType>( m_path ) )
64 return *optval == m_getter();
65 }
66
67 // Not in file
68 return false;
69}
std::function< nlohmann::json()> m_getter
Definition: parameters.h:321

◆ 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}

References m_presets.

◆ SetDefault()

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

Implements PARAM_BASE.

Definition at line 311 of file parameters.h.

312 {
314 }

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

296 {
297 try
298 {
299 aSettings->Set<ValueType>( m_path, m_getter() );
300 }
301 catch( ... )
302 {
303 }
304 }
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_default

nlohmann::json PARAM_LAMBDA< nlohmann::json >::m_default
privateinherited

Definition at line 319 of file parameters.h.

◆ m_getter

std::function<nlohmann::json ()> PARAM_LAMBDA< nlohmann::json >::m_getter
privateinherited

Definition at line 321 of file parameters.h.

◆ m_path

◆ m_presets

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

Definition at line 183 of file board_project_settings.h.

Referenced by jsonToPresets(), and presetsToJson().

◆ m_readOnly

bool PARAM_BASE::m_readOnly
protectedinherited

◆ m_setter

std::function<void( nlohmann::json )> PARAM_LAMBDA< nlohmann::json >::m_setter
privateinherited

Definition at line 323 of file parameters.h.


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