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>(
133  "board.active_layer", &m_ActiveLayer, F_Cu, PCBNEW_LAYER_ID_START, F_Fab ) );
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  // TODO: move the rest of PCB_DISPLAY_OPTIONS that are project-specific in here
158 #if 0
159  m_params.emplace_back( new PARAM_ENUM<ZONE_DISPLAY_MODE>( "board.zone_display_mode",
162 #endif
163 
164  m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "project.files",
165  [&]() -> nlohmann::json
166  {
167  nlohmann::json ret = nlohmann::json::array();
168 
169  for( PROJECT_FILE_STATE& fileState : m_files )
170  {
171  nlohmann::json file;
172  file["name"] = fileState.fileName;
173  file["open"] = fileState.open;
174 
175  nlohmann::json window;
176  window["maximized"] = fileState.window.maximized;
177  window["size_x"] = fileState.window.size_x;
178  window["size_y"] = fileState.window.size_y;
179  window["pos_x"] = fileState.window.pos_x;
180  window["pos_y"] = fileState.window.pos_y;
181  window["display"] = fileState.window.display;
182 
183  file["window"] = window;
184 
185  ret.push_back( file );
186  }
187 
188  return ret;
189  },
190  [&]( const nlohmann::json& aVal )
191  {
192  if( !aVal.is_array() || aVal.empty() )
193  {
194  return;
195  }
196 
197  for( const nlohmann::json& file : aVal )
198  {
199  PROJECT_FILE_STATE fileState;
200  try
201  {
202  SetIfPresent( file, "name", fileState.fileName );
203  SetIfPresent( file, "open", fileState.open );
204  SetIfPresent( file, "window.size_x", fileState.window.size_x );
205  SetIfPresent( file, "window.size_y", fileState.window.size_y );
206  SetIfPresent( file, "window.pos_x", fileState.window.pos_x );
207  SetIfPresent( file, "window.pos_y", fileState.window.pos_y );
208  SetIfPresent( file, "window.maximized", fileState.window.maximized );
209  SetIfPresent( file, "window.display", fileState.window.display );
210 
211  m_files.push_back( fileState );
212  }
213  catch( ... )
214  {
215  // Non-integer or out of range entry in the array; ignore
216  }
217  }
218 
219  },
220  {
221  } ) );
222 
223  registerMigration( 1, 2,
224  [&]()
225  {
231  nlohmann::json::json_pointer ptr( "/board/visible_items"_json_pointer );
232 
233  if( contains( ptr ) )
234  {
235  if( ( *this )[ptr].is_array() )
236  {
237  ( *this )[ptr].push_back( LAYER_PADS );
238  ( *this )[ptr].push_back( LAYER_ZONES );
239  }
240  else
241  {
242  at( "board" ).erase( "visible_items" );
243  }
244  }
245 
246  return true;
247  } );
248 }
249 
250 
251 bool PROJECT_LOCAL_SETTINGS::MigrateFromLegacy( wxConfigBase* aLegacyConfig )
252 {
258  return true;
259 }
260 
261 
262 bool PROJECT_LOCAL_SETTINGS::SaveToFile( const wxString& aDirectory, bool aForce )
263 {
264  wxASSERT( m_project );
265 
266  ( *this )[PointerFromString( "meta.filename" )] =
268 
269  return JSON_SETTINGS::SaveToFile( aDirectory, aForce );
270 }
271 
272 
273 const PROJECT_FILE_STATE* PROJECT_LOCAL_SETTINGS::GetFileState( const wxString& aFileName )
274 {
275  auto it = std::find_if( m_files.begin(), m_files.end(),
276  [&aFileName]( const PROJECT_FILE_STATE &a )
277  {
278  return a.fileName == aFileName;
279  } );
280 
281  if( it != m_files.end() )
282  {
283  return &( *it );
284  }
285 
286  return nullptr;
287 }
288 
289 
290 void PROJECT_LOCAL_SETTINGS::SaveFileState( const wxString& aFileName,
291  const WINDOW_SETTINGS* aWindowCfg, bool aOpen )
292 {
293  auto it = std::find_if( m_files.begin(), m_files.end(),
294  [&aFileName]( const PROJECT_FILE_STATE& a )
295  {
296  return a.fileName == aFileName;
297  } );
298 
299  if( it == m_files.end() )
300  {
301  PROJECT_FILE_STATE fileState;
302  fileState.fileName = aFileName;
303 
304  m_files.push_back( fileState );
305 
306  it = m_files.end() - 1;
307  }
308 
309  ( *it ).window = aWindowCfg->state;
310  ( *it ).open = aOpen;
311 }
312 
313 
315 {
316  m_files.clear();
317 }
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)
PROJECT holds project specific data.
Definition: project.h:63
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.
ZONE_DISPLAY_MODE m_ZoneDisplayMode
How zones are drawn (TODO: not yet used)
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:39
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)
static GAL_SET DefaultVisible()
Definition: lset.cpp:926
RATSNEST_MODE m_RatsnestMode
The current ratsnest draw mode.
static LSET AllLayersMask()
Definition: lset.cpp:787
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.
VTBL_ENTRY const wxString GetProjectName() const
Function GetProjectName returns the short name of the project.
Definition: project.cpp:128
PROJECT_LOCAL_SETTINGS(PROJECT *aProject, const wxString &aFilename)
Non-active layers are dimmed (old high-contrast mode)
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.
Outlines of filled polygons are shown.