KiCad PCB EDA Suite
Loading...
Searching...
No Matches
common_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 Jon Evans <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
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
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <set>
22#include <fstream>
23#include <sstream>
24
26#include <env_vars.h>
27#include <paths.h>
28#include <search_stack.h>
34#include <settings/parameters.h>
35#include <systemdirsappend.h>
36#include <trace_helpers.h>
37#include <wx/config.h>
38#include <wx/log.h>
39#include <wx/regex.h>
40#include <wx/tokenzr.h>
41#include <wx/window.h>
42
43
45const wxRegEx versionedEnvVarRegex( wxS( "KICAD[0-9]+_[A-Z0-9_]+(_DIR)?" ) );
46
48const int commonSchemaVersion = 6;
49
51
55 m_Backup(),
56 m_Env(),
57 m_Input(),
59 m_Graphics(),
60 m_Session(),
61 m_System(),
64 m_Api(),
66{
67 /*
68 * Automatic dark mode detection works fine on Mac.
69 */
70#if defined( __WXGTK__ ) || defined( __WXMSW__ )
71 m_params.emplace_back( new PARAM_ENUM<ICON_THEME>( "appearance.icon_theme",
73#else
74 m_Appearance.icon_theme = ICON_THEME::AUTO;
75#endif
76
77#if defined( __WXMSW__ )
78 m_params.emplace_back( new PARAM_ENUM<APP_THEME>( "appearance.app_theme", &m_Appearance.app_theme,
80#else
81 m_Appearance.app_theme = APP_THEME::AUTO;
82#endif
83
84 /*
85 * Automatic canvas scaling works fine on all supported platforms, so it's no longer exposed as
86 * a configuration option.
87 */
88 m_Appearance.canvas_scale = 0.0;
89
90 /*
91 * Menu icons are off by default on OSX and on for all other platforms.
92 */
93#ifdef __WXMAC__
94 m_params.emplace_back( new PARAM<bool>( "appearance.use_icons_in_menus",
95 &m_Appearance.use_icons_in_menus, false ) );
96#else
97 m_params.emplace_back( new PARAM<bool>( "appearance.use_icons_in_menus",
98 &m_Appearance.use_icons_in_menus, true ) );
99#endif
100
101 /*
102 * Font scaling hacks are only needed on GTK under wxWidgets 3.0.
103 */
104 m_Appearance.apply_icon_scale_to_fonts = false;
105
106 m_params.emplace_back( new PARAM<bool>( "appearance.show_scrollbars",
107 &m_Appearance.show_scrollbars, false ) );
108
109 m_params.emplace_back( new PARAM<double>( "appearance.hicontrast_dimming_factor",
110 &m_Appearance.hicontrast_dimming_factor, 0.8f ) );
111
112 m_params.emplace_back( new PARAM<int>( "appearance.text_editor_zoom",
113 &m_Appearance.text_editor_zoom, 0 ) );
114
115 m_params.emplace_back( new PARAM<int>( "appearance.toolbar_icon_size",
116 &m_Appearance.toolbar_icon_size, 24, 16, 64 ) );
117
118 m_params.emplace_back( new PARAM<bool>( "appearance.grid_striping",
119 &m_Appearance.grid_striping, false ) );
120
121 m_params.emplace_back( new PARAM<bool>( "appearance.use_custom_cursors",
122 &m_Appearance.use_custom_cursors, true ) );
123
124 m_Appearance.zoom_correction_factor = 1.0;
125 m_params.emplace_back( new PARAM<double>( "appearance.zoom_correction_factor",
126 &m_Appearance.zoom_correction_factor, 1.0, 0.1, 10.0 ) );
127
128 m_params.emplace_back( new PARAM<bool>( "auto_backup.enabled", &m_Backup.enabled, true ) );
129
130 m_params.emplace_back( new PARAM_ENUM<BACKUP_FORMAT>( "auto_backup.format", &m_Backup.format,
132
133 m_params.emplace_back( new PARAM_ENUM<BACKUP_LOCATION>( "auto_backup.location",
136
137 m_params.emplace_back( new PARAM<unsigned long long>( "auto_backup.limit_total_size",
138 &m_Backup.limit_total_size, 104857600 ) );
139
140 auto envVarsParam = m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "environment.vars",
141 [&]() -> nlohmann::json
142 {
143 nlohmann::json ret = {};
144
145 for( const std::pair<wxString, ENV_VAR_ITEM> entry : m_Env.vars )
146 {
147 const ENV_VAR_ITEM& var = entry.second;
148
149 wxASSERT( entry.first == var.GetKey() );
150
151 // Default values are never persisted
152 if( var.IsDefault() )
153 {
154 wxLogTrace( traceEnvVars,
155 wxS( "COMMON_SETTINGS: Env var %s skipping save (default)" ),
156 var.GetKey() );
157 continue;
158 }
159
160 wxString value = var.GetValue();
161
162 value.Trim( true ).Trim( false ); // Trim from both sides
163
164 // Vars that existed in JSON are persisted, but if they were overridden
165 // externally, we persist the old value (i.e. the one that was loaded from JSON)
166 if( var.GetDefinedExternally() )
167 {
168 if( var.GetDefinedInSettings() )
169 {
170 wxLogTrace( traceEnvVars,
171 wxS( "COMMON_SETTINGS: Env var %s was overridden "
172 "externally, saving previously-loaded value %s" ),
173 var.GetKey(), var.GetSettingsValue() );
174 value = var.GetSettingsValue();
175 }
176 else
177 {
178 wxLogTrace( traceEnvVars,
179 wxS( "COMMON_SETTINGS: Env var %s skipping save "
180 "(external)" ),
181 var.GetKey() );
182 continue;
183 }
184 }
185
186 wxLogTrace( traceEnvVars,
187 wxS( "COMMON_SETTINGS: Saving env var %s = %s" ),
188 var.GetKey(), value);
189
190 std::string key( var.GetKey().Trim( true ).Trim( false ).ToUTF8() );
191 ret[ std::move( key ) ] = value;
192 }
193
194 return ret;
195 },
196 [&]( const nlohmann::json& aJson )
197 {
198 if( !aJson.is_object() )
199 return;
200
201 for( const auto& entry : aJson.items() )
202 {
203 wxString key = wxString( entry.key().c_str(), wxConvUTF8 ).Trim( true ).Trim( false );
204 wxString val = entry.value().get<wxString>().Trim( true ).Trim( false );
205
206 if( m_Env.vars.count( key ) )
207 {
208 if( m_Env.vars[key].GetDefinedExternally() )
209 {
210 wxLogTrace( traceEnvVars,
211 wxS( "COMMON_SETTINGS: %s is defined externally" ),
212 key );
213 m_Env.vars[key].SetDefinedInSettings();
214 m_Env.vars[key].SetSettingsValue( val );
215 continue;
216 }
217 else
218 {
219 wxLogTrace( traceEnvVars,
220 wxS( "COMMON_SETTINGS: Updating %s: %s -> %s"),
221 key, m_Env.vars[key].GetValue(), val );
222 m_Env.vars[key].SetValue( val );
223 }
224 }
225 else
226 {
227 wxLogTrace( traceEnvVars,
228 wxS( "COMMON_SETTINGS: Loaded new var: %s = %s" ),
229 key, val );
230 m_Env.vars[key] = ENV_VAR_ITEM( key, val );
231 }
232
233 m_Env.vars[key].SetDefinedInSettings();
234 m_Env.vars[key].SetSettingsValue( val );
235 }
236 },
237 {} ) );
238 envVarsParam->SetClearUnknownKeys();
239
240 m_params.emplace_back( new PARAM<bool>( "input.focus_follow_sch_pcb",
241 &m_Input.focus_follow_sch_pcb, false ) );
242
243 m_params.emplace_back( new PARAM<bool>( "input.auto_pan", &m_Input.auto_pan, false ) );
244
245 m_params.emplace_back( new PARAM<int>( "input.auto_pan_acceleration",
246 &m_Input.auto_pan_acceleration, 5 ) );
247
248 m_params.emplace_back( new PARAM<bool>( "input.center_on_zoom",
249 &m_Input.center_on_zoom, true ) );
250
251 m_params.emplace_back( new PARAM<bool>( "input.immediate_actions",
252 &m_Input.immediate_actions, true ) );
253
254 m_params.emplace_back( new PARAM<bool>( "input.warp_mouse_on_move",
255 &m_Input.warp_mouse_on_move, true ) );
256
257 m_params.emplace_back( new PARAM<bool>( "input.horizontal_pan",
258 &m_Input.horizontal_pan, false ) );
259
260 m_params.emplace_back( new PARAM<bool>( "input.hotkey_feedback",
261 &m_Input.hotkey_feedback, true ) );
262
263 m_params.emplace_back( new PARAM<bool>( "input.zoom_acceleration",
264 &m_Input.zoom_acceleration, false ) );
265
266#ifdef __WXMAC__
267 int default_zoom_speed = 5;
268#else
269 int default_zoom_speed = 1;
270#endif
271
272 m_params.emplace_back( new PARAM<int>( "input.zoom_speed",
273 &m_Input.zoom_speed, default_zoom_speed ) );
274
275 m_params.emplace_back( new PARAM<bool>( "input.zoom_speed_auto",
276 &m_Input.zoom_speed_auto, true ) );
277
278 m_params.emplace_back( new PARAM<int>( "input.scroll_modifier_zoom",
279 &m_Input.scroll_modifier_zoom, 0 ) );
280
281 m_params.emplace_back( new PARAM<int>( "input.scroll_modifier_pan_h",
282 &m_Input.scroll_modifier_pan_h, WXK_CONTROL ) );
283
284 m_params.emplace_back( new PARAM<int>( "input.scroll_modifier_pan_v",
285 &m_Input.scroll_modifier_pan_v, WXK_SHIFT ) );
286
287 m_params.emplace_back( new PARAM<int>( "input.motion_pan_modifier",
288 &m_Input.motion_pan_modifier, 0 ) );
289
290 m_params.emplace_back( new PARAM<bool>( "input.reverse_scroll_zoom",
291 &m_Input.reverse_scroll_zoom, false ) );
292
293 m_params.emplace_back( new PARAM<bool>( "input.reverse_scroll_pan_h",
294 &m_Input.reverse_scroll_pan_h, false ) );
295
296 m_params.emplace_back( new PARAM_ENUM<MOUSE_DRAG_ACTION>( "input.mouse_left",
299
300 m_params.emplace_back( new PARAM_ENUM<MOUSE_DRAG_ACTION>( "input.mouse_middle",
303
304 m_params.emplace_back( new PARAM_ENUM<MOUSE_DRAG_ACTION>( "input.mouse_right",
307
308 m_params.emplace_back( new PARAM<int>( "spacemouse.rotate_speed",
309 &m_SpaceMouse.rotate_speed, 5, 1, 10 ) );
310
311 m_params.emplace_back( new PARAM<int>( "spacemouse.pan_speed",
312 &m_SpaceMouse.pan_speed, 5, 1, 10 ) );
313
314 m_params.emplace_back( new PARAM<bool>( "spacemouse.reverse_rotate",
315 &m_SpaceMouse.reverse_rotate, false ) );
316
317 m_params.emplace_back( new PARAM<bool>( "spacemouse.reverse_pan_x",
318 &m_SpaceMouse.reverse_pan_x, false ) );
319
320 m_params.emplace_back( new PARAM<bool>( "spacemouse.reverse_pan_y",
321 &m_SpaceMouse.reverse_pan_y, false ) );
322
323 m_params.emplace_back( new PARAM<bool>( "spacemouse.reverse_zoom",
324 &m_SpaceMouse.reverse_zoom, false ) );
325
326 m_params.emplace_back( new PARAM<int>( "graphics.canvas_type",
327 &m_Graphics.canvas_type, EDA_DRAW_PANEL_GAL::GAL_TYPE_OPENGL ) );
328
329 m_params.emplace_back( new PARAM<int>( "graphics.antialiasing_mode",
330 &m_Graphics.aa_mode, 2, 0, 2 ) );
331
332 m_params.emplace_back( new PARAM<int>( "system.local_history_debounce",
333 &m_System.local_history_debounce, 5, 0, 100000 ) );
334
335#ifdef __WXMAC__
336 m_params.emplace_back( new PARAM<wxString>( "system.text_editor",
337 &m_System.text_editor, wxS( "/usr/bin/open -e" ) ) );
338#else
339 m_params.emplace_back( new PARAM<wxString>( "system.text_editor",
340 &m_System.text_editor, wxS( "" ) ) );
341#endif
342
343#if defined( __WINDOWS__ )
344 m_params.emplace_back( new PARAM<wxString>( "system.file_explorer",
345 &m_System.file_explorer, wxS( "explorer.exe /n,/select,%F" ) ) );
346#else
347 m_params.emplace_back( new PARAM<wxString>( "system.file_explorer",
348 &m_System.file_explorer, wxS( "" ) ) );
349#endif
350
351 m_params.emplace_back( new PARAM<int>( "system.file_history_size",
352 &m_System.file_history_size, 9 ) );
353
354 m_params.emplace_back( new PARAM<wxString>( "system.language",
355 &m_System.language, wxS( "Default" ) ) );
356
357 m_params.emplace_back( new PARAM<wxString>( "system.pdf_viewer_name",
358 &m_System.pdf_viewer_name, wxS( "" ) ) );
359
360 m_params.emplace_back( new PARAM<bool>( "system.use_system_pdf_viewer",
361 &m_System.use_system_pdf_viewer, true ) );
362
363 m_params.emplace_back( new PARAM<wxString>( "system.working_dir",
364 &m_System.working_dir, wxS( "" ) ) );
365
366 m_params.emplace_back( new PARAM<int>( "system.clear_3d_cache_interval",
367 &m_System.clear_3d_cache_interval, 30 ) );
368
369 m_params.emplace_back( new PARAM<bool>( "do_not_show_again.zone_fill_warning",
370 &m_DoNotShowAgain.zone_fill_warning, false ) );
371
372 m_params.emplace_back( new PARAM<bool>( "do_not_show_again.env_var_overwrite_warning",
373 &m_DoNotShowAgain.env_var_overwrite_warning, false ) );
374
375 m_params.emplace_back( new PARAM<bool>( "do_not_show_again.scaled_3d_models_warning",
376 &m_DoNotShowAgain.scaled_3d_models_warning, false ) );
377
378 m_params.emplace_back( new PARAM<bool>( "do_not_show_again.data_collection_prompt",
379 &m_DoNotShowAgain.data_collection_prompt, false ) );
380
381 m_params.emplace_back( new PARAM<bool>( "do_not_show_again.update_check_prompt",
382 &m_DoNotShowAgain.update_check_prompt, false ) );
383
384 m_params.emplace_back( new PARAM<bool>( "do_not_show_again.migrate_wrl_prompt",
385 &m_DoNotShowAgain.migrate_wrl_prompt, false ) );
386
387 m_params.emplace_back( new PARAM<bool>( "embed_file_defaults.datasheet", &m_EmbedFileDefaults.datasheet, true ) );
388
389 m_params.emplace_back(
390 new PARAM<bool>( "embed_file_defaults.drawing_sheet", &m_EmbedFileDefaults.drawing_sheet, true ) );
391
392 m_params.emplace_back( new PARAM<bool>( "embed_file_defaults.model_3d", &m_EmbedFileDefaults.model_3d, false ) );
393
394 m_params.emplace_back( new PARAM<bool>( "embed_file_defaults.sim_model", &m_EmbedFileDefaults.sim_model, false ) );
395
396 m_params.emplace_back( new PARAM_LIST<wxString>( "system.extra_3d_search_dirs",
397 &m_Extra3DSearchDirs, {} ) );
398
399 m_params.emplace_back( new PARAM<bool>( "session.remember_open_files",
400 &m_Session.remember_open_files, false ) );
401
402 m_params.emplace_back( new PARAM_LIST<wxString>( "session.pinned_symbol_libs",
403 &m_Session.pinned_symbol_libs, {} ) );
404
405 m_params.emplace_back( new PARAM_LIST<wxString>( "session.pinned_fp_libs",
406 &m_Session.pinned_fp_libs, {} ) );
407
408 m_params.emplace_back( new PARAM_LIST<wxString>( "session.pinned_design_block_libs",
409 &m_Session.pinned_design_block_libs, {} ) );
410
411 m_params.emplace_back( new PARAM<int>( "package_manager.sash_pos",
412 &m_PackageManager.sash_pos, 380 ) );
413
414 m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "git.repositories",
415 [&]() -> nlohmann::json
416 {
417 nlohmann::json ret = {};
418
419 for( const GIT_REPOSITORY& repo : m_Git.repositories )
420 {
421 nlohmann::json repoJson = {};
422
423 repoJson["name"] = repo.name;
424 repoJson["path"] = repo.path;
425 repoJson["authType"] = repo.authType;
426 repoJson["username"] = repo.username;
427 repoJson["ssh_path"] = repo.ssh_path;
428 repoJson["active"] = repo.active;
429
430 ret.push_back( repoJson );
431 }
432
433 return ret;
434 },
435 [&]( const nlohmann::json& aJson )
436 {
437 if( !aJson.is_array() )
438 return;
439
440 m_Git.repositories.clear();
441
442 for( const auto& repoJson : aJson )
443 {
444 GIT_REPOSITORY repo;
445
446 repo.name = repoJson["name"].get<wxString>();
447 repo.path = repoJson["path"].get<wxString>();
448 repo.authType = repoJson["authType"].get<wxString>();
449 repo.username = repoJson["username"].get<wxString>();
450 repo.ssh_path = repoJson["ssh_path"].get<wxString>();
451 repo.active = repoJson["active"].get<bool>();
452 repo.checkValid = true;
453
454 m_Git.repositories.push_back( repo );
455 }
456 },
457 {} ) );
458
459 m_params.emplace_back( new PARAM<wxString>( "git.authorName",
460 &m_Git.authorName, wxS( "" ) ) );
461
462 m_params.emplace_back( new PARAM<wxString>( "git.authorEmail",
463 &m_Git.authorEmail, wxS( "" ) ) );
464
465 m_params.emplace_back( new PARAM<bool>( "git.useDefaultAuthor",
466 &m_Git.useDefaultAuthor, true ) );
467
468 m_params.emplace_back( new PARAM<bool>( "git.enableGit",
469 &m_Git.enableGit, true ) );
470
471 m_params.emplace_back( new PARAM<int>( "git.updatInterval",
472 &m_Git.updatInterval, 5 ) );
473
474 m_params.emplace_back( new PARAM<wxString>( "api.interpreter_path",
475 &m_Api.python_interpreter, wxS( "" ) ) );
476
477 m_params.emplace_back( new PARAM<bool>( "api.enable_server",
478 &m_Api.enable_server, false ) );
479
480 m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "dialog.controls",
481 [&]() -> nlohmann::json
482 {
483 nlohmann::json ret = nlohmann::json::object();
484
485 for( const auto& dlg : m_csInternals->m_dialogControlValues )
486 ret[ dlg.first ] = dlg.second;
487
488 return ret;
489 },
490 [&]( const nlohmann::json& aVal )
491 {
492 m_csInternals->m_dialogControlValues.clear();
493
494 if( !aVal.is_object() )
495 return;
496
497 for( auto& [dlgKey, dlgVal] : aVal.items() )
498 {
499 if( !dlgVal.is_object() )
500 continue;
501
502 for( auto& [ctrlKey, ctrlVal] : dlgVal.items() )
503 m_csInternals->m_dialogControlValues[ dlgKey ][ ctrlKey ] = ctrlVal;
504 }
505 },
506 nlohmann::json::object() ) );
507
508
509 registerMigration( 0, 1, std::bind( &COMMON_SETTINGS::migrateSchema0to1, this ) );
510 registerMigration( 1, 2, std::bind( &COMMON_SETTINGS::migrateSchema1to2, this ) );
511 registerMigration( 2, 3, std::bind( &COMMON_SETTINGS::migrateSchema2to3, this ) );
512 registerMigration( 3, 4, std::bind( &COMMON_SETTINGS::migrateSchema3to4, this ) );
513 registerMigration( 4, 5, std::bind( &COMMON_SETTINGS::migrateSchema4to5, this ) );
514 registerMigration( 5, 6, std::bind( &COMMON_SETTINGS::migrateSchema5to6, this ) );
515}
516
517
519{
525
526 nlohmann::json::json_pointer mwp_pointer( "/input/mousewheel_pan"_json_pointer );
527
528 bool mwp = false;
529
530 try
531 {
532 mwp = m_internals->at( mwp_pointer );
533 m_internals->At( "input" ).erase( "mousewheel_pan" );
534 }
535 catch( ... )
536 {
537 wxLogTrace( traceSettings,
538 wxT( "COMMON_SETTINGS::Migrate 0->1: mousewheel_pan not found" ) );
539 }
540
541 if( mwp )
542 {
543 ( *m_internals )[nlohmann::json::json_pointer( "/input/horizontal_pan" )] = true;
544 ( *m_internals )[nlohmann::json::json_pointer( "/input/scroll_modifier_pan_h" )] = WXK_SHIFT;
545 ( *m_internals )[nlohmann::json::json_pointer( "/input/scroll_modifier_pan_v" )] = 0;
546 ( *m_internals )[nlohmann::json::json_pointer( "/input/scroll_modifier_zoom" )] = WXK_CONTROL;
547 }
548 else
549 {
550 ( *m_internals )[nlohmann::json::json_pointer( "/input/horizontal_pan" )] = false;
551 ( *m_internals )[nlohmann::json::json_pointer( "/input/scroll_modifier_pan_h" )] = WXK_CONTROL;
552 ( *m_internals )[nlohmann::json::json_pointer( "/input/scroll_modifier_pan_v" )] = WXK_SHIFT;
553 ( *m_internals )[nlohmann::json::json_pointer( "/input/scroll_modifier_zoom" )] = 0;
554 }
555
556 return true;
557}
558
559
561{
562 nlohmann::json::json_pointer v1_pointer( "/input/prefer_select_to_drag"_json_pointer );
563
564 bool prefer_selection = false;
565
566 try
567 {
568 prefer_selection = m_internals->at( v1_pointer );
569 m_internals->at( nlohmann::json::json_pointer( "/input"_json_pointer ) )
570 .erase( "prefer_select_to_drag" );
571 }
572 catch( ... )
573 {
574 wxLogTrace( traceSettings,
575 wxT( "COMMON_SETTINGS::Migrate 1->2: prefer_select_to_drag not found" ) );
576 }
577
578 if( prefer_selection )
579 ( *m_internals )[nlohmann::json::json_pointer( "/input/mouse_left" )] = MOUSE_DRAG_ACTION::SELECT;
580 else
581 ( *m_internals )[nlohmann::json::json_pointer( "/input/mouse_left" )] = MOUSE_DRAG_ACTION::DRAG_ANY;
582
583 return true;
584}
585
586
588{
589 wxFileName cfgpath;
590 cfgpath.AssignDir( PATHS::GetUserSettingsPath() );
591 cfgpath.AppendDir( wxT( "3d" ) );
592 cfgpath.SetFullName( wxS( "3Dresolver.cfg" ) );
593 cfgpath.MakeAbsolute();
594
595 std::vector<LEGACY_3D_SEARCH_PATH> legacyPaths;
596 readLegacy3DResolverCfg( cfgpath.GetFullPath(), legacyPaths );
597
598 // env variables have a limited allowed character set for names
599 wxRegEx nonValidCharsRegex( wxS( "[^A-Z0-9_]+" ), wxRE_ADVANCED );
600
601 for( const LEGACY_3D_SEARCH_PATH& path : legacyPaths )
602 {
603 wxString key = path.m_Alias;
604 const wxString& val = path.m_Pathvar;
605
606 // The 3d alias config didn't use the same naming restrictions as real env variables
607 // We need to sanitize them
608
609 // upper case only
610 key.MakeUpper();
611
612 // logically swap - with _
613 key.Replace( wxS( "-" ), wxS( "_" ) );
614
615 // remove any other chars
616 nonValidCharsRegex.Replace( &key, wxEmptyString );
617
618 if( !m_Env.vars.count( key ) )
619 {
620 wxLogTrace( traceEnvVars, wxS( "COMMON_SETTINGS: Loaded new var: %s = %s" ), key, val );
621 m_Env.vars[key] = ENV_VAR_ITEM( key, val );
622 }
623 }
624
625 if( cfgpath.FileExists() )
626 {
627 wxRemoveFile( cfgpath.GetFullPath() );
628 }
629
630 return true;
631}
632
633
635{
636 // >= 10 = add 1
637 try
638 {
639 // Update netclass panel shown columns for eeschema
640 const nlohmann::json::json_pointer v3_pointer_eeschema( "/netclass_panel/eeschema_shown_columns"_json_pointer );
641 wxString eeSchemaColumnList_old = m_internals->at( v3_pointer_eeschema );
642
643 wxStringTokenizer eeSchemaShownTokens( eeSchemaColumnList_old, " \t\r\n" );
644 wxString eeSchemaColumnList_new;
645
646 while( eeSchemaShownTokens.HasMoreTokens() )
647 {
648 long colNumber;
649 eeSchemaShownTokens.GetNextToken().ToLong( &colNumber );
650
651 if( colNumber >= 10 )
652 ++colNumber;
653
654 eeSchemaColumnList_new += wxString::Format( wxT( "%ld " ), colNumber );
655 }
656
657 eeSchemaColumnList_new.Trim( true );
658 eeSchemaColumnList_new.Trim( false );
659
660 m_internals->at( v3_pointer_eeschema ) = eeSchemaColumnList_new.ToUTF8();
661
662 // Update netclass panel shown columns for pcbnew
663 const nlohmann::json::json_pointer v3_pointer_pcbnew( "/netclass_panel/pcbnew_shown_columns"_json_pointer );
664 wxString pcbnewColumnList_old = m_internals->at( v3_pointer_pcbnew );
665
666 wxStringTokenizer pcbnewShownTokens( pcbnewColumnList_old, " \t\r\n" );
667 wxString pcbnewColumnList_new;
668
669 while( pcbnewShownTokens.HasMoreTokens() )
670 {
671 long colNumber;
672 pcbnewShownTokens.GetNextToken().ToLong( &colNumber );
673
674 if( colNumber >= 10 )
675 ++colNumber;
676
677 pcbnewColumnList_new += wxString::Format( wxT( "%ld " ), colNumber );
678 }
679
680 pcbnewColumnList_new.Trim( true );
681 pcbnewColumnList_new.Trim( false );
682
683 m_internals->at( v3_pointer_pcbnew ) = pcbnewColumnList_new.ToUTF8();
684 }
685 catch( ... )
686 {
687 wxLogTrace( traceSettings, wxT( "COMMON_SETTINGS::Migrate 3->4: /netclass_panel/shown_columns not found" ) );
688 }
689
690 return true;
691}
692
693
695{
696 try
697 {
698 nlohmann::json& controls = m_internals->At( "dialog" ).at( "controls" );
699
700 for( auto& [dlgKey, dlgVal] : controls.items() )
701 {
702 if( !dlgVal.is_object() )
703 continue;
704
705 auto geoIt = dlgVal.find( "__geometry" );
706
707 if( geoIt == dlgVal.end() || !geoIt->is_object() )
708 continue;
709
710 nlohmann::json& geom = *geoIt;
711
712 // Legacy values were stored in logical pixels. Convert to DIP using the
713 // primary display's scale factor (best approximation without window context).
714 int w = geom.value( "w", 0 );
715 int h = geom.value( "h", 0 );
716
717 wxSize dipSize = wxWindow::ToDIP( wxSize( w, h ), nullptr );
718 geom[ "w" ] = dipSize.x;
719 geom[ "h" ] = dipSize.y;
720
721 geom.erase( "dip" );
722 }
723 }
724 catch( ... )
725 {
726 wxLogTrace( traceSettings,
727 wxT( "COMMON_SETTINGS::Migrate 4->5: dialog.controls not found" ) );
728 }
729
730 return true;
731}
732
733
735{
736 // Schema 6 introduces auto_backup.format and auto_backup.location. Pre-schema-6
737 // installs unconditionally produced timestamped zip archives whenever a save was
738 // eligible for backup, so the new INCREMENTAL default would silently disable archive
739 // creation for users who already had auto_backup.enabled set. Write ZIP into the
740 // upgraded config to preserve their backup behavior; new installs skip the migration
741 // path entirely and keep the new default. The location default (PROJECT_DIR) already
742 // matches the legacy on-disk layout, so we leave it absent.
743 try
744 {
745 if( !Contains( "auto_backup.format" ) )
746 Set<int>( "auto_backup.format", static_cast<int>( BACKUP_FORMAT::ZIP ) );
747 }
748 catch( ... )
749 {
750 wxLogTrace( traceSettings,
751 wxT( "COMMON_SETTINGS::Migrate 5->6: failed to set auto_backup.format" ) );
752 }
753
754 return true;
755}
756
757
758bool COMMON_SETTINGS::MigrateFromLegacy( wxConfigBase* aCfg )
759{
760 bool ret = true;
761
762 ret &= fromLegacy<double>( aCfg, "CanvasScale", "appearance.canvas_scale" );
763 ret &= fromLegacy<int>( aCfg, "IconScale", "appearance.icon_scale" );
764 ret &= fromLegacy<bool>( aCfg, "UseIconsInMenus", "appearance.use_icons_in_menus" );
765 ret &= fromLegacy<bool>( aCfg, "ShowEnvVarWarningDialog", "environment.show_warning_dialog" );
766
767 auto load_env_vars =
768 [&]()
769 {
770 wxString key, value;
771 long index = 0;
772 nlohmann::json::json_pointer ptr = m_internals->PointerFromString( "environment.vars" );
773
774 aCfg->SetPath( "EnvironmentVariables" );
775 ( *m_internals )[ptr] = nlohmann::json( {} );
776
777 while( aCfg->GetNextEntry( key, index ) )
778 {
779 if( versionedEnvVarRegex.Matches( key ) )
780 {
781 wxLogTrace( traceSettings,
782 wxT( "Migrate Env: %s is blacklisted; skipping." ), key );
783 continue;
784 }
785
786 value = aCfg->Read( key, wxEmptyString );
787
788 if( !value.IsEmpty() )
789 {
790 ptr.push_back( key.ToStdString() );
791
792 wxLogTrace( traceSettings, wxT( "Migrate Env: %s=%s" ),
793 ptr.to_string(), value );
794 ( *m_internals )[ptr] = value.ToUTF8();
795
796 ptr.pop_back();
797 }
798 }
799
800 aCfg->SetPath( ".." );
801 };
802
803 load_env_vars();
804
805 bool mousewheel_pan = false;
806
807 if( aCfg->Read( "MousewheelPAN", &mousewheel_pan ) && mousewheel_pan )
808 {
809 Set( "input.horizontal_pan", true );
810 Set( "input.scroll_modifier_pan_h", static_cast<int>( WXK_SHIFT ) );
811 Set( "input.scroll_modifier_pan_v", 0 );
812 Set( "input.scroll_modifier_zoom", static_cast<int>( WXK_CONTROL ) );
813 }
814
815 ret &= fromLegacy<bool>( aCfg, "AutoPAN", "input.auto_pan" );
816 ret &= fromLegacy<bool>( aCfg, "ImmediateActions", "input.immediate_actions" );
817 ret &= fromLegacy<bool>( aCfg, "PreferSelectionToDragging", "input.prefer_select_to_drag" );
818 ret &= fromLegacy<bool>( aCfg, "MoveWarpsCursor", "input.warp_mouse_on_move" );
819 ret &= fromLegacy<bool>( aCfg, "ZoomNoCenter", "input.center_on_zoom" );
820
821 // This was stored inverted in legacy config
822 if( std::optional<bool> value = Get<bool>( "input.center_on_zoom" ) )
823 Set( "input.center_on_zoom", !( *value ) );
824
825 ret &= fromLegacy<int>( aCfg, "OpenGLAntialiasingMode", "graphics.opengl_antialiasing_mode" );
826 ret &= fromLegacy<int>( aCfg, "CairoAntialiasingMode", "graphics.cairo_antialiasing_mode" );
827
828 ret &= fromLegacy<int>( aCfg, "AutoSaveInterval", "system.local_history_debounce" );
829 ret &= fromLegacyString( aCfg, "Editor", "system.editor_name" );
830 ret &= fromLegacy<int>( aCfg, "FileHistorySize", "system.file_history_size" );
831 ret &= fromLegacyString( aCfg, "LanguageID", "system.language" );
832 ret &= fromLegacyString( aCfg, "PdfBrowserName", "system.pdf_viewer_name" );
833 ret &= fromLegacy<bool>( aCfg, "UseSystemBrowser", "system.use_system_pdf_viewer" );
834 ret &= fromLegacyString( aCfg, "WorkingDir", "system.working_dir" );
835
836 return ret;
837}
838
839
841{
842 auto addVar =
843 [&]( const wxString& aKey, const wxString& aDefault )
844 {
845 m_Env.vars[aKey] = ENV_VAR_ITEM( aKey, aDefault, aDefault );
846
847 wxString envValue;
848
849 if( wxGetEnv( aKey, &envValue ) == true && !envValue.IsEmpty() )
850 {
851 m_Env.vars[aKey].SetValue( envValue );
852 m_Env.vars[aKey].SetDefinedExternally();
853 wxLogTrace( traceEnvVars,
854 wxS( "InitializeEnvironment: Entry %s defined externally as %s" ), aKey,
855 envValue );
856 }
857 else
858 {
859 wxLogTrace( traceEnvVars, wxS( "InitializeEnvironment: Setting entry %s to "
860 "default %s" ),
861 aKey, aDefault );
862 }
863 };
864
865 addVar( ENV_VAR::GetVersionedEnvVarName( wxS( "FOOTPRINT_DIR" ) ), PATHS::GetStockFootprintsPath() );
866 addVar( ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ), PATHS::GetStock3dmodelsPath() );
867 addVar( ENV_VAR::GetVersionedEnvVarName( wxS( "TEMPLATE_DIR" ) ), PATHS::GetStockTemplatesPath() );
868 addVar( wxT( "KICAD_USER_TEMPLATE_DIR" ), PATHS::GetUserTemplatesPath() );
869 addVar( ENV_VAR::GetVersionedEnvVarName( wxS( "3RD_PARTY" ) ), PATHS::GetDefault3rdPartyPath() );
870 addVar( ENV_VAR::GetVersionedEnvVarName( wxS( "SYMBOL_DIR" ) ), PATHS::GetStockSymbolsPath() );
871 addVar( ENV_VAR::GetVersionedEnvVarName( wxS( "DESIGN_BLOCK_DIR" ) ), PATHS::GetStockDesignBlocksPath() );
872}
873
874
876 std::vector<LEGACY_3D_SEARCH_PATH>& aSearchPaths )
877{
878 wxFileName cfgpath( path );
879
880 // This should be the same as wxWidgets 3.0 wxPATH_NORM_ALL which is deprecated in 3.1.
881 // There are known issues with environment variable expansion so maybe we should be using
882 // our own ExpandEnvVarSubstitutions() here instead.
883 cfgpath.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS );
884 wxString cfgname = cfgpath.GetFullPath();
885
886 std::ifstream cfgFile;
887 std::string cfgLine;
888
889 if( !wxFileName::Exists( cfgname ) )
890 {
891 std::ostringstream ostr;
892 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
893 wxString errmsg = "no 3D configuration file";
894 ostr << " * " << errmsg.ToUTF8() << " '";
895 ostr << cfgname.ToUTF8() << "'";
896 wxLogTrace( traceSettings, "%s\n", ostr.str().c_str() );
897 return false;
898 }
899
900 cfgFile.open( cfgname.ToUTF8() );
901
902 if( !cfgFile.is_open() )
903 {
904 std::ostringstream ostr;
905 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
906 wxString errmsg = wxS( "Could not open configuration file" );
907 ostr << " * " << errmsg.ToUTF8() << " '" << cfgname.ToUTF8() << "'";
908 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
909 return false;
910 }
911
912 int lineno = 0;
914 size_t idx;
915 int vnum = 0; // version number
916
917 while( cfgFile.good() )
918 {
919 cfgLine.clear();
920 std::getline( cfgFile, cfgLine );
921 ++lineno;
922
923 if( cfgLine.empty() )
924 {
925 if( cfgFile.eof() )
926 break;
927
928 continue;
929 }
930
931 if( 1 == lineno && cfgLine.compare( 0, 2, "#V" ) == 0 )
932 {
933 // extract the version number and parse accordingly
934 if( cfgLine.size() > 2 )
935 {
936 std::istringstream istr;
937 istr.str( cfgLine.substr( 2 ) );
938 istr >> vnum;
939 }
940
941 continue;
942 }
943
944 idx = 0;
945
946 if( !getLegacy3DHollerith( cfgLine, idx, al.m_Alias ) )
947 continue;
948
949 // Don't add KICADn_3DMODEL_DIR, one of its legacy equivalents, or KIPRJMOD from a
950 // config file. They're system variables which are defined at runtime.
951 wxString versionedPath = wxString::Format( wxS( "${%s}" ),
952 ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ) );
953
954 if( al.m_Alias == versionedPath || al.m_Alias == wxS( "${KIPRJMOD}" )
955 || al.m_Alias == wxS( "$(KIPRJMOD)" ) || al.m_Alias == wxS( "${KISYS3DMOD}" )
956 || al.m_Alias == wxS( "$(KISYS3DMOD)" ) )
957 {
958 continue;
959 }
960
961 if( !getLegacy3DHollerith( cfgLine, idx, al.m_Pathvar ) )
962 continue;
963
964 if( !getLegacy3DHollerith( cfgLine, idx, al.m_Description ) )
965 continue;
966
967 aSearchPaths.push_back( al );
968 }
969
970 cfgFile.close();
971
972 return true;
973}
974
975
976bool COMMON_SETTINGS::getLegacy3DHollerith( const std::string& aString, size_t& aIndex,
977 wxString& aResult )
978{
979 aResult.clear();
980
981 if( aIndex >= aString.size() )
982 {
983 std::ostringstream ostr;
984 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
985 wxString errmsg = wxS( "bad Hollerith string on line" );
986 ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
987 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
988
989 return false;
990 }
991
992 size_t i2 = aString.find( '"', aIndex );
993
994 if( std::string::npos == i2 )
995 {
996 std::ostringstream ostr;
997 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
998 wxString errmsg = wxS( "missing opening quote mark in config file" );
999 ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
1000 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
1001
1002 return false;
1003 }
1004
1005 ++i2;
1006
1007 if( i2 >= aString.size() )
1008 {
1009 std::ostringstream ostr;
1010 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
1011 wxString errmsg = wxS( "invalid entry (unexpected end of line)" );
1012 ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
1013 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
1014
1015 return false;
1016 }
1017
1018 std::string tnum;
1019
1020 while( aString[i2] >= '0' && aString[i2] <= '9' )
1021 tnum.append( 1, aString[i2++] );
1022
1023 if( tnum.empty() || aString[i2++] != ':' )
1024 {
1025 std::ostringstream ostr;
1026 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
1027 wxString errmsg = wxS( "bad Hollerith string on line" );
1028 ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
1029 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
1030
1031 return false;
1032 }
1033
1034 std::istringstream istr;
1035 istr.str( tnum );
1036 size_t nchars;
1037 istr >> nchars;
1038
1039 if( ( i2 + nchars ) >= aString.size() )
1040 {
1041 std::ostringstream ostr;
1042 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
1043 wxString errmsg = wxS( "invalid entry (unexpected end of line)" );
1044 ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
1045 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
1046
1047 return false;
1048 }
1049
1050 if( nchars > 0 )
1051 {
1052 aResult = wxString::FromUTF8( aString.substr( i2, nchars ).c_str() );
1053 i2 += nchars;
1054 }
1055
1056 if( i2 >= aString.size() || aString[i2] != '"' )
1057 {
1058 std::ostringstream ostr;
1059 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
1060 wxString errmsg = wxS( "missing closing quote mark in config file" );
1061 ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
1062 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
1063
1064 return false;
1065 }
1066
1067 aIndex = i2 + 1;
1068 return true;
1069}
int index
SPACEMOUSE m_SpaceMouse
std::unique_ptr< COMMON_SETTINGS_INTERNALS > m_csInternals
APPEARANCE m_Appearance
virtual ~COMMON_SETTINGS()
static bool getLegacy3DHollerith(const std::string &aString, size_t &aIndex, wxString &aResult)
bool readLegacy3DResolverCfg(const wxString &aPath, std::vector< LEGACY_3D_SEARCH_PATH > &aSearchPaths)
PACKAGE_MANAGER m_PackageManager
void InitializeEnvironment()
Creates the built-in environment variables and sets their default values.
AUTO_BACKUP m_Backup
DO_NOT_SHOW_AGAIN m_DoNotShowAgain
virtual bool MigrateFromLegacy(wxConfigBase *aLegacyConfig) override
Migrates from wxConfig to JSON-based configuration.
@ GAL_TYPE_OPENGL
OpenGL implementation.
KiCad uses environment variables internally for determining the base paths for libraries,...
bool fromLegacyString(wxConfigBase *aConfig, const std::string &aKey, const std::string &aDest)
Translates a legacy wxConfig string value to a given JSON pointer value.
bool fromLegacy(wxConfigBase *aConfig, const std::string &aKey, const std::string &aDest)
Translates a legacy wxConfig value to a given JSON pointer value.
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 Contains(const std::string &aPath) const
std::optional< ValueType > Get(const std::string &aPath) const
Fetches a value from within the JSON document.
std::vector< PARAM_BASE * > m_params
The list of parameters (owned by this object)
JSON_SETTINGS(const wxString &aFilename, SETTINGS_LOC aLocation, int aSchemaVersion)
std::unique_ptr< JSON_SETTINGS_INTERNALS > m_internals
Stores an enum as an integer.
Definition parameters.h:232
Like a normal param, but with custom getter and setter functions.
Definition parameters.h:299
static wxString GetStockSymbolsPath()
Gets the stock (install) symbols path.
Definition paths.cpp:300
static wxString GetUserTemplatesPath()
Gets the user path for custom templates.
Definition paths.cpp:71
static wxString GetDefault3rdPartyPath()
Gets the default path for PCM packages.
Definition paths.cpp:126
static wxString GetStock3dmodelsPath()
Gets the stock (install) 3dmodels path.
Definition paths.cpp:333
static wxString GetStockTemplatesPath()
Gets the stock (install) templates path.
Definition paths.cpp:355
static wxString GetUserSettingsPath()
Return the user configuration path used to store KiCad's configuration files.
Definition paths.cpp:634
static wxString GetStockDesignBlocksPath()
Gets the stock (install) footprints path.
Definition paths.cpp:322
static wxString GetStockFootprintsPath()
Gets the stock (install) footprints path.
Definition paths.cpp:311
const int commonSchemaVersion
! Update the schema version whenever a migration is required
const wxRegEx versionedEnvVarRegex(wxS("KICAD[0-9]+_[A-Z0-9_]+(_DIR)?"))
! The following environment variables will never be migrated from a previous version
@ ZIP
Zip archive snapshots; autosave uses recovery files.
@ INCREMENTAL
Git-based local history (default)
@ USER_DIR
Under the KiCad user data directory.
@ PROJECT_DIR
Inside the project directory (default)
Functions related to environment variables, including help functions.
const wxChar *const traceEnvVars
Flag to enable debug output of environment variable operations.
SETTINGS_LOC
@ USER
The main config directory (e.g. ~/.config/kicad/)
#define traceSettings
KICOMMON_API wxString GetVersionedEnvVarName(const wxString &aBaseName)
Construct a versioned environment variable based on this KiCad major version.
Definition env_vars.cpp:78
STL namespace.
System directories search utilities.
std::string path
wxLogTrace helper definitions.
#define FN_NORMALIZE_FLAGS
Default flags to pass to wxFileName::Normalize().
Definition wx_filename.h:35