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  * Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
6  * @author Jon Evans <jon@craftyjon.com>
7  *
8  * This program is free software: you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation, either version 3 of the License, or (at your
11  * option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include <project.h>
25 #include <settings/parameters.h>
26 
28 
29 
30 PROJECT_LOCAL_SETTINGS::PROJECT_LOCAL_SETTINGS( PROJECT* aProject, const wxString& aFilename ) :
32  /* aCreateIfMissing = */ true, /* aCreateIfDefault = */ false,
33  /* aWriteFile = */ true ),
34  m_SelectionFilter(),
35  m_project( aProject )
36 {
37  m_params.emplace_back( new PARAM_LAMBDA<std::string>( "board.visible_layers",
38  [&]() -> std::string
39  {
40  return m_VisibleLayers.FmtHex();
41  },
42  [&]( const std::string& aString )
43  {
44  m_VisibleLayers.ParseHex( aString.c_str(), aString.size() );
45  },
47 
48  m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "board.visible_items",
49  [&]() -> nlohmann::json
50  {
51  nlohmann::json ret = nlohmann::json::array();
52 
53  for( size_t i = 0; i < m_VisibleItems.size(); i++ )
54  if( m_VisibleItems.test( i ) )
55  ret.push_back( i );
56 
57  return ret;
58  },
59  [&]( const nlohmann::json& aVal )
60  {
61  if( !aVal.is_array() || aVal.empty() )
62  {
64  return;
65  }
66 
67  m_VisibleItems.reset();
68 
69  for( const nlohmann::json& entry : aVal )
70  {
71  try
72  {
73  int i = entry.get<int>();
74  m_VisibleItems.set( i );
75  }
76  catch( ... )
77  {
78  // Non-integer or out of range entry in the array; ignore
79  }
80  }
81  },
82  {} ) );
83 
84  m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "board.selection_filter",
85  [&]() -> nlohmann::json
86  {
87  nlohmann::json ret;
88 
89  ret["lockedItems"] = m_SelectionFilter.lockedItems;
90  ret["footprints"] = m_SelectionFilter.footprints;
91  ret["text"] = m_SelectionFilter.text;
92  ret["tracks"] = m_SelectionFilter.tracks;
93  ret["vias"] = m_SelectionFilter.vias;
94  ret["pads"] = m_SelectionFilter.pads;
95  ret["graphics"] = m_SelectionFilter.graphics;
96  ret["zones"] = m_SelectionFilter.zones;
97  ret["keepouts"] = m_SelectionFilter.keepouts;
98  ret["dimensions"] = m_SelectionFilter.dimensions;
99  ret["otherItems"] = m_SelectionFilter.otherItems;
100 
101  return ret;
102  },
103  [&]( const nlohmann::json& aVal )
104  {
105  if( aVal.empty() || !aVal.is_object() )
106  return;
107 
108  SetIfPresent( aVal, "lockedItems", m_SelectionFilter.lockedItems );
109  SetIfPresent( aVal, "footprints", m_SelectionFilter.footprints );
110  SetIfPresent( aVal, "text", m_SelectionFilter.text );
111  SetIfPresent( aVal, "tracks", m_SelectionFilter.tracks );
112  SetIfPresent( aVal, "vias", m_SelectionFilter.vias );
113  SetIfPresent( aVal, "pads", m_SelectionFilter.pads );
114  SetIfPresent( aVal, "graphics", m_SelectionFilter.graphics );
115  SetIfPresent( aVal, "zones", m_SelectionFilter.zones );
116  SetIfPresent( aVal, "keepouts", m_SelectionFilter.keepouts );
117  SetIfPresent( aVal, "dimensions", m_SelectionFilter.dimensions );
118  SetIfPresent( aVal, "otherItems", m_SelectionFilter.otherItems );
119  },
120  {
121  { "lockedItems", true },
122  { "footprints", true },
123  { "text", true },
124  { "tracks", true },
125  { "vias", true },
126  { "pads", true },
127  { "graphics", true },
128  { "zones", true },
129  { "keepouts", true },
130  { "dimensions", true },
131  { "otherItems", true }
132  } ) );
133 
134  m_params.emplace_back( new PARAM_ENUM<PCB_LAYER_ID>( "board.active_layer",
136 
137  m_params.emplace_back( new PARAM<wxString>( "board.active_layer_preset",
138  &m_ActiveLayerPreset, "" ) );
139 
140  m_params.emplace_back( new PARAM_ENUM<HIGH_CONTRAST_MODE>( "board.high_contrast_mode",
143 
144  m_params.emplace_back( new PARAM<double>( "board.opacity.tracks", &m_TrackOpacity, 1.0 ) );
145  m_params.emplace_back( new PARAM<double>( "board.opacity.vias", &m_ViaOpacity, 1.0 ) );
146  m_params.emplace_back( new PARAM<double>( "board.opacity.pads", &m_PadOpacity, 1.0 ) );
147  m_params.emplace_back( new PARAM<double>( "board.opacity.zones", &m_ZoneOpacity, 0.6 ) );
148 
149  m_params.emplace_back( new PARAM_LIST<wxString>( "board.hidden_nets", &m_HiddenNets, {} ) );
150 
151  m_params.emplace_back( new PARAM_ENUM<NET_COLOR_MODE>( "board.net_color_mode",
154 
155  m_params.emplace_back( new PARAM_ENUM<RATSNEST_MODE>( "board.ratsnest_display_mode",
158 
159  m_params.emplace_back( new PARAM<bool>( "board.auto_track_width",
160  &m_AutoTrackWidth, true ) );
161 
162  m_params.emplace_back( new PARAM_ENUM<ZONE_DISPLAY_MODE>( "board.zone_display_mode",
166 
167  m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "project.files",
168  [&]() -> nlohmann::json
169  {
170  nlohmann::json ret = nlohmann::json::array();
171 
172  for( PROJECT_FILE_STATE& fileState : m_files )
173  {
174  nlohmann::json file;
175  file["name"] = fileState.fileName;
176  file["open"] = fileState.open;
177 
178  nlohmann::json window;
179  window["maximized"] = fileState.window.maximized;
180  window["size_x"] = fileState.window.size_x;
181  window["size_y"] = fileState.window.size_y;
182  window["pos_x"] = fileState.window.pos_x;
183  window["pos_y"] = fileState.window.pos_y;
184  window["display"] = fileState.window.display;
185 
186  file["window"] = window;
187 
188  ret.push_back( file );
189  }
190 
191  return ret;
192  },
193  [&]( const nlohmann::json& aVal )
194  {
195  if( !aVal.is_array() || aVal.empty() )
196  {
197  return;
198  }
199 
200  for( const nlohmann::json& file : aVal )
201  {
202  PROJECT_FILE_STATE fileState;
203 
204  try
205  {
206  SetIfPresent( file, "name", fileState.fileName );
207  SetIfPresent( file, "open", fileState.open );
208  SetIfPresent( file, "window.size_x", fileState.window.size_x );
209  SetIfPresent( file, "window.size_y", fileState.window.size_y );
210  SetIfPresent( file, "window.pos_x", fileState.window.pos_x );
211  SetIfPresent( file, "window.pos_y", fileState.window.pos_y );
212  SetIfPresent( file, "window.maximized", fileState.window.maximized );
213  SetIfPresent( file, "window.display", fileState.window.display );
214 
215  m_files.push_back( fileState );
216  }
217  catch( ... )
218  {
219  // Non-integer or out of range entry in the array; ignore
220  }
221  }
222 
223  },
224  {
225  } ) );
226 
227  registerMigration( 1, 2,
228  [&]()
229  {
235  std::string ptr( "board.visible_items" );
236 
237  if( Contains( ptr ) )
238  {
239  if( At( ptr ).is_array() )
240  {
241  At( ptr ).push_back( LAYER_PADS );
242  At( ptr ).push_back( LAYER_ZONES );
243  }
244  else
245  {
246  At( "board" ).erase( "visible_items" );
247  }
248  }
249 
250  return true;
251  } );
252 
253  registerMigration( 2, 3,
254  [&]()
255  {
262  const std::map<int, int> offsets = {
264  { 22, 34 }, // LAYER_PAD_HOLEWALLS
265  { 23, 22 }, // LAYER_VIA_HOLES
266  { 24, 35 }, // LAYER_VIA_HOLEWALLS
267  { 25, 23 }, // LAYER_DRC_ERROR
268  { 26, 36 }, // LAYER_DRC_WARNING
269  { 27, 37 }, // LAYER_DRC_EXCLUSION
270  { 28, 38 }, // LAYER_MARKER_SHADOWS
271  { 29, 24 }, // LAYER_DRAWINGSHEET
272  { 30, 25 }, // LAYER_GP_OVERLAY
273  { 31, 26 }, // LAYER_SELECT_OVERLAY
274  { 32, 27 }, // LAYER_PCB_BACKGROUND
275  { 33, 28 }, // LAYER_CURSOR
276  { 34, 29 }, // LAYER_AUX_ITEM
277  { 35, 30 }, // LAYER_DRAW_BITMAPS
278  { 39, 32 }, // LAYER_PADS
279  { 40, 33 }, // LAYER_ZONES
280  };
281 
282  std::string ptr( "board.visible_items" );
283 
284  if( Contains( ptr ) && At( ptr ).is_array() )
285  {
286  nlohmann::json visible = nlohmann::json::array();
287 
288  for( const nlohmann::json& val : At( ptr ) )
289  {
290  try
291  {
292  int layer = val.get<int>();
293 
294  if( offsets.count( layer ) )
295  visible.push_back( offsets.at( layer ) );
296  else
297  visible.push_back( layer );
298  }
299  catch( ... )
300  {
301  // skip invalid value
302  }
303  }
304 
305  At( "board" )["visible_items"] = visible;
306  }
307 
308  return true;
309  } );
310 }
311 
312 
313 bool PROJECT_LOCAL_SETTINGS::MigrateFromLegacy( wxConfigBase* aLegacyConfig )
314 {
320  return true;
321 }
322 
323 
324 bool PROJECT_LOCAL_SETTINGS::SaveToFile( const wxString& aDirectory, bool aForce )
325 {
326  wxASSERT( m_project );
327 
328  Set( "meta.filename", m_project->GetProjectName() + "." + ProjectLocalSettingsFileExtension );
329 
330  return JSON_SETTINGS::SaveToFile( aDirectory, aForce );
331 }
332 
333 
334 const PROJECT_FILE_STATE* PROJECT_LOCAL_SETTINGS::GetFileState( const wxString& aFileName )
335 {
336  auto it = std::find_if( m_files.begin(), m_files.end(),
337  [&aFileName]( const PROJECT_FILE_STATE &a )
338  {
339  return a.fileName == aFileName;
340  } );
341 
342  if( it != m_files.end() )
343  {
344  return &( *it );
345  }
346 
347  return nullptr;
348 }
349 
350 
351 void PROJECT_LOCAL_SETTINGS::SaveFileState( const wxString& aFileName,
352  const WINDOW_SETTINGS* aWindowCfg, bool aOpen )
353 {
354  auto it = std::find_if( m_files.begin(), m_files.end(),
355  [&aFileName]( const PROJECT_FILE_STATE& a )
356  {
357  return a.fileName == aFileName;
358  } );
359 
360  if( it == m_files.end() )
361  {
362  PROJECT_FILE_STATE fileState;
363  fileState.fileName = aFileName;
364 
365  m_files.push_back( fileState );
366 
367  it = m_files.end() - 1;
368  }
369 
370  ( *it ).window = aWindowCfg->state;
371  ( *it ).open = aOpen;
372 }
373 
374 
376 {
377  m_files.clear();
378 }
LSET m_VisibleLayers
Board settings.
Filled polygons are shown.
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...
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:46
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:41
nlohmann::json & At(const std::string &aPath)
Wrappers for the underlying JSON API so that most consumers don't need json.hpp All of these function...
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)
Inactive 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.
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.
Definition: project.cpp:129
bool Contains(const std::string &aPath) const
PROJECT_LOCAL_SETTINGS(PROJECT *aProject, const wxString &aFilename)
Inactive 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.