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( PROJECT* aProject, const wxString& aFilename ) :
30  /* aCreateIfMissing = */ true, /* aCreateIfDefault = */ false,
31  /* aWriteFile = */ true ),
32  m_project( aProject ),
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  m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "board.visible_items",
47  [&]() -> nlohmann::json
48  {
49  nlohmann::json ret = nlohmann::json::array();
50 
51  for( size_t i = 0; i < m_VisibleItems.size(); i++ )
52  if( m_VisibleItems.test( i ) )
53  ret.push_back( i );
54 
55  return ret;
56  },
57  [&]( const nlohmann::json& aVal )
58  {
59  if( !aVal.is_array() || aVal.empty() )
60  {
62  return;
63  }
64 
65  m_VisibleItems.reset();
66 
67  for( const nlohmann::json& entry : aVal )
68  {
69  try
70  {
71  int i = entry.get<int>();
72  m_VisibleItems.set( i );
73  }
74  catch( ... )
75  {
76  // Non-integer or out of range entry in the array; ignore
77  }
78  }
79  },
80  {} ) );
81 
82  m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "board.selection_filter",
83  [&]() -> nlohmann::json
84  {
85  nlohmann::json ret;
86 
87  ret["lockedItems"] = m_SelectionFilter.lockedItems;
88  ret["footprints"] = m_SelectionFilter.footprints;
89  ret["text"] = m_SelectionFilter.text;
90  ret["tracks"] = m_SelectionFilter.tracks;
91  ret["vias"] = m_SelectionFilter.vias;
92  ret["pads"] = m_SelectionFilter.pads;
93  ret["graphics"] = m_SelectionFilter.graphics;
94  ret["zones"] = m_SelectionFilter.zones;
95  ret["keepouts"] = m_SelectionFilter.keepouts;
96  ret["dimensions"] = m_SelectionFilter.dimensions;
97  ret["otherItems"] = m_SelectionFilter.otherItems;
98 
99  return ret;
100  },
101  [&]( const nlohmann::json& aVal )
102  {
103  if( aVal.empty() || !aVal.is_object() )
104  return;
105 
106  SetIfPresent( aVal, "lockedItems", m_SelectionFilter.lockedItems );
107  SetIfPresent( aVal, "footprints", m_SelectionFilter.footprints );
108  SetIfPresent( aVal, "text", m_SelectionFilter.text );
109  SetIfPresent( aVal, "tracks", m_SelectionFilter.tracks );
110  SetIfPresent( aVal, "vias", m_SelectionFilter.vias );
111  SetIfPresent( aVal, "pads", m_SelectionFilter.pads );
112  SetIfPresent( aVal, "graphics", m_SelectionFilter.graphics );
113  SetIfPresent( aVal, "zones", m_SelectionFilter.zones );
114  SetIfPresent( aVal, "keepouts", m_SelectionFilter.keepouts );
115  SetIfPresent( aVal, "dimensions", m_SelectionFilter.dimensions );
116  SetIfPresent( aVal, "otherItems", m_SelectionFilter.otherItems );
117  },
118  {
119  { "lockedItems", true },
120  { "footprints", true },
121  { "text", true },
122  { "tracks", true },
123  { "vias", true },
124  { "pads", true },
125  { "graphics", true },
126  { "zones", true },
127  { "keepouts", true },
128  { "dimensions", true },
129  { "otherItems", true }
130  } ) );
131 
132  m_params.emplace_back( new PARAM_ENUM<PCB_LAYER_ID>( "board.active_layer",
134 
135  m_params.emplace_back( new PARAM<wxString>( "board.active_layer_preset",
136  &m_ActiveLayerPreset, "" ) );
137 
138  m_params.emplace_back( new PARAM_ENUM<HIGH_CONTRAST_MODE>( "board.high_contrast_mode",
141 
142  m_params.emplace_back( new PARAM<double>( "board.opacity.tracks", &m_TrackOpacity, 1.0 ) );
143  m_params.emplace_back( new PARAM<double>( "board.opacity.vias", &m_ViaOpacity, 1.0 ) );
144  m_params.emplace_back( new PARAM<double>( "board.opacity.pads", &m_PadOpacity, 1.0 ) );
145  m_params.emplace_back( new PARAM<double>( "board.opacity.zones", &m_ZoneOpacity, 0.6 ) );
146 
147  m_params.emplace_back( new PARAM_LIST<wxString>( "board.hidden_nets", &m_HiddenNets, {} ) );
148 
149  m_params.emplace_back( new PARAM_ENUM<NET_COLOR_MODE>( "board.net_color_mode",
152 
153  m_params.emplace_back( new PARAM_ENUM<RATSNEST_MODE>( "board.ratsnest_display_mode",
156 
157  m_params.emplace_back( new PARAM<bool>( "board.auto_track_width",
158  &m_AutoTrackWidth, true ) );
159 
160  m_params.emplace_back( new PARAM_ENUM<ZONE_DISPLAY_MODE>( "board.zone_display_mode",
164 
165  m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "project.files",
166  [&]() -> nlohmann::json
167  {
168  nlohmann::json ret = nlohmann::json::array();
169 
170  for( PROJECT_FILE_STATE& fileState : m_files )
171  {
172  nlohmann::json file;
173  file["name"] = fileState.fileName;
174  file["open"] = fileState.open;
175 
176  nlohmann::json window;
177  window["maximized"] = fileState.window.maximized;
178  window["size_x"] = fileState.window.size_x;
179  window["size_y"] = fileState.window.size_y;
180  window["pos_x"] = fileState.window.pos_x;
181  window["pos_y"] = fileState.window.pos_y;
182  window["display"] = fileState.window.display;
183 
184  file["window"] = window;
185 
186  ret.push_back( file );
187  }
188 
189  return ret;
190  },
191  [&]( const nlohmann::json& aVal )
192  {
193  if( !aVal.is_array() || aVal.empty() )
194  {
195  return;
196  }
197 
198  for( const nlohmann::json& file : aVal )
199  {
200  PROJECT_FILE_STATE fileState;
201 
202  try
203  {
204  SetIfPresent( file, "name", fileState.fileName );
205  SetIfPresent( file, "open", fileState.open );
206  SetIfPresent( file, "window.size_x", fileState.window.size_x );
207  SetIfPresent( file, "window.size_y", fileState.window.size_y );
208  SetIfPresent( file, "window.pos_x", fileState.window.pos_x );
209  SetIfPresent( file, "window.pos_y", fileState.window.pos_y );
210  SetIfPresent( file, "window.maximized", fileState.window.maximized );
211  SetIfPresent( file, "window.display", fileState.window.display );
212 
213  m_files.push_back( fileState );
214  }
215  catch( ... )
216  {
217  // Non-integer or out of range entry in the array; ignore
218  }
219  }
220 
221  },
222  {
223  } ) );
224 
225  registerMigration( 1, 2,
226  [&]()
227  {
233  nlohmann::json::json_pointer ptr( "/board/visible_items"_json_pointer );
234 
235  if( contains( ptr ) )
236  {
237  if( ( *this )[ptr].is_array() )
238  {
239  ( *this )[ptr].push_back( LAYER_PADS );
240  ( *this )[ptr].push_back( LAYER_ZONES );
241  }
242  else
243  {
244  at( "board" ).erase( "visible_items" );
245  }
246  }
247 
248  return true;
249  } );
250 
251  registerMigration( 2, 3,
252  [&]()
253  {
260  const std::map<int, int> offsets = {
262  { 22, 34 }, // LAYER_PAD_HOLEWALLS
263  { 23, 22 }, // LAYER_VIA_HOLES
264  { 24, 35 }, // LAYER_VIA_HOLEWALLS
265  { 25, 23 }, // LAYER_DRC_ERROR
266  { 26, 36 }, // LAYER_DRC_WARNING
267  { 27, 37 }, // LAYER_DRC_EXCLUSION
268  { 28, 38 }, // LAYER_MARKER_SHADOWS
269  { 29, 24 }, // LAYER_DRAWINGSHEET
270  { 30, 25 }, // LAYER_GP_OVERLAY
271  { 31, 26 }, // LAYER_SELECT_OVERLAY
272  { 32, 27 }, // LAYER_PCB_BACKGROUND
273  { 33, 28 }, // LAYER_CURSOR
274  { 34, 29 }, // LAYER_AUX_ITEM
275  { 35, 30 }, // LAYER_DRAW_BITMAPS
276  { 39, 32 }, // LAYER_PADS
277  { 40, 33 }, // LAYER_ZONES
278  };
279 
280  nlohmann::json::json_pointer ptr( "/board/visible_items"_json_pointer );
281 
282  if( contains( ptr ) && at( ptr ).is_array() )
283  {
284  nlohmann::json visible = nlohmann::json::array();
285 
286  for( const nlohmann::json& val : at( ptr ) )
287  {
288  try
289  {
290  int layer = val.get<int>();
291 
292  if( offsets.count( layer ) )
293  visible.push_back( offsets.at( layer ) );
294  else
295  visible.push_back( layer );
296  }
297  catch( ... )
298  {
299  // skip invalid value
300  }
301  }
302 
303  at( "board" )["visible_items"] = visible;
304  }
305 
306  return true;
307  } );
308 }
309 
310 
311 bool PROJECT_LOCAL_SETTINGS::MigrateFromLegacy( wxConfigBase* aLegacyConfig )
312 {
318  return true;
319 }
320 
321 
322 bool PROJECT_LOCAL_SETTINGS::SaveToFile( const wxString& aDirectory, bool aForce )
323 {
324  wxASSERT( m_project );
325 
326  ( *this )[PointerFromString( "meta.filename" )] =
328 
329  return JSON_SETTINGS::SaveToFile( aDirectory, aForce );
330 }
331 
332 
333 const PROJECT_FILE_STATE* PROJECT_LOCAL_SETTINGS::GetFileState( const wxString& aFileName )
334 {
335  auto it = std::find_if( m_files.begin(), m_files.end(),
336  [&aFileName]( const PROJECT_FILE_STATE &a )
337  {
338  return a.fileName == aFileName;
339  } );
340 
341  if( it != m_files.end() )
342  {
343  return &( *it );
344  }
345 
346  return nullptr;
347 }
348 
349 
350 void PROJECT_LOCAL_SETTINGS::SaveFileState( const wxString& aFileName,
351  const WINDOW_SETTINGS* aWindowCfg, bool aOpen )
352 {
353  auto it = std::find_if( m_files.begin(), m_files.end(),
354  [&aFileName]( const PROJECT_FILE_STATE& a )
355  {
356  return a.fileName == aFileName;
357  } );
358 
359  if( it == m_files.end() )
360  {
361  PROJECT_FILE_STATE fileState;
362  fileState.fileName = aFileName;
363 
364  m_files.push_back( fileState );
365 
366  it = m_files.end() - 1;
367  }
368 
369  ( *it ).window = aWindowCfg->state;
370  ( *it ).open = aOpen;
371 }
372 
373 
375 {
376  m_files.clear();
377 }
LSET m_VisibleLayers
Board settings.
Filled polygons are shown.
bool otherItems
Anything not fitting one of the above categories.
std::vector< PARAM_BASE * > m_params
The list of parameters (owned by this object)
void SaveFileState(const wxString &aFileName, const WINDOW_SETTINGS *aWindowCfg, bool aOpen)
Container for project specific data.
Definition: project.h:62
const int projectLocalSettingsVersion
Like a normal param, but with custom getter and setter functions.
Definition: parameters.h:296
Ratsnest lines are drawn to items on visible layers only.
double m_TrackOpacity
Opacity override for all tracks.
Outlines of filled polygons are shown.
ZONE_DISPLAY_MODE m_ZoneDisplayMode
How zones are drawn.
Control for copper zone opacity/visibility (color ignored)
bool MigrateFromLegacy(wxConfigBase *aLegacyConfig) override
Migrates from wxConfig to JSON-based configuration.
Net/netclass colors are shown on all net copper.
Net/netclass colors are shown on ratsnest lines only.
SETTINGS_LOC
Definition: json_settings.h:44
double m_ViaOpacity
Opacity override for all types of via.
bool SaveToFile(const wxString &aDirectory="", bool aForce=false) override
Stores an enum as an integer.
Definition: parameters.h:226
NET_COLOR_MODE m_NetColorMode
The current net color mode.
PCB_LAYER_ID m_ActiveLayer
The current (active) board layer for editing.
nlohmann::json json
Definition: gerbview.cpp:40
Ratsnest lines are drawn to items on all layers (default)
const std::string ProjectLocalSettingsFileExtension
double m_PadOpacity
Opacity override for SMD pads and PTH.
Stores the common settings that are saved and loaded for each window / frame.
Definition: app_settings.h:81
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.
bool graphics
Graphic lines, shapes, polygons.
static bool SetIfPresent(const nlohmann::json &aObj, const std::string &aPath, wxString &aTarget)
Sets the given string if the given key/path is present.
struct WINDOW_STATE window
PROJECT * m_project
A link to the owning project.
Meta control for all pads opacity/visibility (color ignored)
Non-active layers are shown normally (no high-contrast mode)
static GAL_SET DefaultVisible()
Definition: lset.cpp:933
RATSNEST_MODE m_RatsnestMode
The current ratsnest draw mode.
static LSET AllLayersMask()
Definition: lset.cpp:787
bool m_AutoTrackWidth
The current setting for whether to automatically adjust track widths to match.
double m_ZoneOpacity
Opacity override for filled zones.
bool footprints
Allow selecting entire footprints.
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition: kicad_algo.h:81
const PROJECT_FILE_STATE * GetFileState(const wxString &aFileName)
bool lockedItems
Allow selecting locked items.
HIGH_CONTRAST_MODE m_ContrastModeDisplay
The current contrast mode.
void registerMigration(int aOldSchemaVersion, int aNewSchemaVersion, std::function< bool(void)> aMigrator)
Registers a migration from one schema version to another.
Net (and netclass) colors are not shown.
WINDOW_STATE state
Definition: app_settings.h:83
std::string FmtHex() const
Return a hex string showing contents of this LSEQ.
Definition: lset.cpp:321
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.
virtual const wxString GetProjectName() const
Return the short name of the project.
PROJECT_LOCAL_SETTINGS(PROJECT *aProject, const wxString &aFilename)
Non-active layers are hidden.
unsigned int display
Definition: app_settings.h:75
int ParseHex(const char *aStart, int aCount)
Convert the output of FmtHex() and replaces this set's values with those given in the input string.
Definition: lset.cpp:359
std::vector< PROJECT_FILE_STATE > m_files
Project scope.
static nlohmann::json::json_pointer PointerFromString(std::string aPath)
Builds a JSON pointer based on a given string.