KiCad PCB EDA Suite
project_local_settings.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2020 CERN
5  * @author Jon Evans <jon@craftyjon.com>
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 
21 #include <project.h>
23 #include <settings/parameters.h>
24 
26 
27 
28 PROJECT_LOCAL_SETTINGS::PROJECT_LOCAL_SETTINGS( const wxString& aFilename ) :
30  /* aCreateIfMissing = */ true, /* aCreateIfDefault = */ false,
31  /* aWriteFile = */ true ),
32  m_project( nullptr ),
33  m_SelectionFilter()
34 {
35  m_params.emplace_back( new PARAM_LAMBDA<std::string>( "board.visible_layers",
36  [&]() -> std::string
37  {
38  return m_VisibleLayers.FmtHex();
39  },
40  [&]( const std::string& aString )
41  {
42  m_VisibleLayers.ParseHex( aString.c_str(), aString.size() );
43  },
45 
46  static GAL_SET defaultVisible;
47  defaultVisible.set().reset( GAL_LAYER_INDEX( LAYER_MOD_TEXT_INVISIBLE ) );
48 
49  m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "board.visible_items",
50  [&]() -> nlohmann::json
51  {
52  nlohmann::json ret = nlohmann::json::array();
53 
54  for( size_t i = 0; i < m_VisibleItems.size(); i++ )
55  if( m_VisibleItems.test( i ) )
56  ret.push_back( i );
57 
58  return ret;
59  },
60  [&]( const nlohmann::json& aVal )
61  {
62  if( !aVal.is_array() || aVal.empty() )
63  {
64  m_VisibleItems = defaultVisible;
65  return;
66  }
67 
68  m_VisibleItems.reset();
69 
70  for( const nlohmann::json& entry : aVal )
71  {
72  try
73  {
74  int i = entry.get<int>();
75  m_VisibleItems.set( i );
76  }
77  catch( ... )
78  {
79  // Non-integer or out of range entry in the array; ignore
80  }
81  }
82  },
83  {} ) );
84 
85  m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "board.selection_filter",
86  [&]() -> nlohmann::json
87  {
88  nlohmann::json ret;
89 
90  ret["lockedItems"] = m_SelectionFilter.lockedItems;
91  ret["footprints"] = m_SelectionFilter.footprints;
92  ret["text"] = m_SelectionFilter.text;
93  ret["tracks"] = m_SelectionFilter.tracks;
94  ret["vias"] = m_SelectionFilter.vias;
95  ret["pads"] = m_SelectionFilter.pads;
96  ret["graphics"] = m_SelectionFilter.graphics;
97  ret["zones"] = m_SelectionFilter.zones;
98  ret["keepouts"] = m_SelectionFilter.keepouts;
99  ret["dimensions"] = m_SelectionFilter.dimensions;
100  ret["otherItems"] = m_SelectionFilter.otherItems;
101 
102  return ret;
103  },
104  [&]( const nlohmann::json& aVal )
105  {
106  if( aVal.empty() || !aVal.is_object() )
107  return;
108 
109  auto setIfPresent =
110  [&aVal]( const std::string& aKey, bool& aTarget )
111  {
112  if( aVal.contains( aKey ) && aVal.at( aKey ).is_boolean() )
113  aTarget = aVal.at( aKey ).get<bool>();
114  };
115 
116  setIfPresent( "lockedItems", m_SelectionFilter.lockedItems );
117  setIfPresent( "footprints", m_SelectionFilter.footprints );
118  setIfPresent( "text", m_SelectionFilter.text );
119  setIfPresent( "tracks", m_SelectionFilter.tracks );
120  setIfPresent( "vias", m_SelectionFilter.vias );
121  setIfPresent( "pads", m_SelectionFilter.pads );
122  setIfPresent( "graphics", m_SelectionFilter.graphics );
123  setIfPresent( "zones", m_SelectionFilter.zones );
124  setIfPresent( "keepouts", m_SelectionFilter.keepouts );
125  setIfPresent( "dimensions", m_SelectionFilter.dimensions );
126  setIfPresent( "otherItems", m_SelectionFilter.otherItems );
127  },
128  {
129  { "lockedItems", true },
130  { "footprints", true },
131  { "text", true },
132  { "tracks", true },
133  { "vias", true },
134  { "pads", true },
135  { "graphics", true },
136  { "zones", true },
137  { "keepouts", true },
138  { "dimensions", true },
139  { "otherItems", true }
140  } ) );
141 
142  m_params.emplace_back( new PARAM_ENUM<PCB_LAYER_ID>(
143  "board.active_layer", &m_ActiveLayer, F_Cu, PCBNEW_LAYER_ID_START, F_Fab ) );
144 
145  m_params.emplace_back( new PARAM<wxString>( "board.active_layer_preset",
146  &m_ActiveLayerPreset, "" ) );
147 
148  m_params.emplace_back( new PARAM_ENUM<HIGH_CONTRAST_MODE>( "board.high_contrast_mode",
151 
152  m_params.emplace_back( new PARAM_LIST<wxString>( "board.hidden_nets", &m_HiddenNets, {} ) );
153 }
154 
155 
156 bool PROJECT_LOCAL_SETTINGS::MigrateFromLegacy( wxConfigBase* aLegacyConfig )
157 {
163  return true;
164 }
165 
166 
167 bool PROJECT_LOCAL_SETTINGS::SaveToFile( const wxString& aDirectory, bool aForce )
168 {
169  wxASSERT( m_project );
170 
171  ( *this )[PointerFromString( "meta.filename" )] =
173 
174  return JSON_SETTINGS::SaveToFile( aDirectory, aForce );
175 }
LSET m_VisibleLayers
Board settings.
bool otherItems
Anything not fitting one of the above categories.
std::vector< PARAM_BASE * > m_params
The list of parameters (owned by this object)
PROJECT holds project specific data.
Definition: project.h:61
const int projectLocalSettingsVersion
Like a normal param, but with custom getter and setter functions.
Definition: parameters.h:296
bool MigrateFromLegacy(wxConfigBase *aLegacyConfig) override
Migrates from wxConfig to JSON-based configuration.
SETTINGS_LOC
Definition: json_settings.h:36
bool SaveToFile(const wxString &aDirectory="", bool aForce=false) override
Stores an enum as an integer.
Definition: parameters.h:226
PCB_LAYER_ID m_ActiveLayer
The current (active) board layer for editing.
PROJECT_LOCAL_SETTINGS(const wxString &aFilename)
nlohmann::json json
Definition: gerbview.cpp:40
const std::string ProjectLocalSettingsFileExtension
virtual bool SaveToFile(const wxString &aDirectory="", bool aForce=false)
bool text
Text (free or attached to a footprint)
GAL_SET m_VisibleItems
The GAL layers (aka items) that are turned on for viewing (.
wxString m_ActiveLayerPreset
The name of a LAYER_PRESET that is currently activated (or blank if none)
bool dimensions
Dimension items.
#define GAL_LAYER_INDEX(x)
Use this macro to convert a GAL layer to a 0-indexed offset from LAYER_VIAS.
bool graphics
Graphic lines, shapes, polygons.
PROJECT * m_project
A link to the owning project.
Helper for storing and iterating over GAL_LAYER_IDs.
static LSET AllLayersMask()
Definition: lset.cpp:749
bool footprints
Allow selecting entire footprints.
bool lockedItems
Allow selecting locked items.
HIGH_CONTRAST_MODE m_ContrastModeDisplay
The current contrast mode.
std::string FmtHex() const
Function FmtHex returns a hex string showing contents of this LSEQ.
Definition: lset.cpp:288
std::vector< wxString > m_HiddenNets
A list of netnames that have been manually hidden in the board editor.
SELECTION_FILTER_OPTIONS m_SelectionFilter
State of the selection filter widget.
VTBL_ENTRY const wxString GetProjectName() const
Function GetProjectName returns the short name of the project.
Definition: project.cpp:129
Non-active layers are dimmed (old high-contrast mode)
int ParseHex(const char *aStart, int aCount)
Function ParseHex understands the output of FmtHex() and replaces this set's values with those given ...
Definition: lset.cpp:325
static nlohmann::json::json_pointer PointerFromString(std::string aPath)
Builds a JSON pointer based on a given string.