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 along
18 * with this program. If not, see <http://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<bool>( "system.local_history_enabled",
333 &m_System.local_history_enabled, true ) );
334 m_params.emplace_back( new PARAM<int>( "system.local_history_debounce",
335 &m_System.local_history_debounce, 5, 0, 100000 ) );
336
337#ifdef __WXMAC__
338 m_params.emplace_back( new PARAM<wxString>( "system.text_editor",
339 &m_System.text_editor, wxS( "/usr/bin/open -e" ) ) );
340#else
341 m_params.emplace_back( new PARAM<wxString>( "system.text_editor",
342 &m_System.text_editor, wxS( "" ) ) );
343#endif
344
345#if defined( __WINDOWS__ )
346 m_params.emplace_back( new PARAM<wxString>( "system.file_explorer",
347 &m_System.file_explorer, wxS( "explorer.exe /n,/select,%F" ) ) );
348#else
349 m_params.emplace_back( new PARAM<wxString>( "system.file_explorer",
350 &m_System.file_explorer, wxS( "" ) ) );
351#endif
352
353 m_params.emplace_back( new PARAM<int>( "system.file_history_size",
354 &m_System.file_history_size, 9 ) );
355
356 m_params.emplace_back( new PARAM<wxString>( "system.language",
357 &m_System.language, wxS( "Default" ) ) );
358
359 m_params.emplace_back( new PARAM<wxString>( "system.pdf_viewer_name",
360 &m_System.pdf_viewer_name, wxS( "" ) ) );
361
362 m_params.emplace_back( new PARAM<bool>( "system.use_system_pdf_viewer",
363 &m_System.use_system_pdf_viewer, true ) );
364
365 m_params.emplace_back( new PARAM<wxString>( "system.working_dir",
366 &m_System.working_dir, wxS( "" ) ) );
367
368 m_params.emplace_back( new PARAM<int>( "system.clear_3d_cache_interval",
369 &m_System.clear_3d_cache_interval, 30 ) );
370
371 m_params.emplace_back( new PARAM<bool>( "do_not_show_again.zone_fill_warning",
372 &m_DoNotShowAgain.zone_fill_warning, false ) );
373
374 m_params.emplace_back( new PARAM<bool>( "do_not_show_again.env_var_overwrite_warning",
375 &m_DoNotShowAgain.env_var_overwrite_warning, false ) );
376
377 m_params.emplace_back( new PARAM<bool>( "do_not_show_again.scaled_3d_models_warning",
378 &m_DoNotShowAgain.scaled_3d_models_warning, false ) );
379
380 m_params.emplace_back( new PARAM<bool>( "do_not_show_again.data_collection_prompt",
381 &m_DoNotShowAgain.data_collection_prompt, false ) );
382
383 m_params.emplace_back( new PARAM<bool>( "do_not_show_again.update_check_prompt",
384 &m_DoNotShowAgain.update_check_prompt, false ) );
385
386 m_params.emplace_back( new PARAM<bool>( "do_not_show_again.migrate_wrl_prompt",
387 &m_DoNotShowAgain.migrate_wrl_prompt, false ) );
388
389 m_params.emplace_back( new PARAM_LIST<wxString>( "system.extra_3d_search_dirs",
390 &m_Extra3DSearchDirs, {} ) );
391
392 m_params.emplace_back( new PARAM<bool>( "session.remember_open_files",
393 &m_Session.remember_open_files, false ) );
394
395 m_params.emplace_back( new PARAM_LIST<wxString>( "session.pinned_symbol_libs",
396 &m_Session.pinned_symbol_libs, {} ) );
397
398 m_params.emplace_back( new PARAM_LIST<wxString>( "session.pinned_fp_libs",
399 &m_Session.pinned_fp_libs, {} ) );
400
401 m_params.emplace_back( new PARAM_LIST<wxString>( "session.pinned_design_block_libs",
402 &m_Session.pinned_design_block_libs, {} ) );
403
404 m_params.emplace_back( new PARAM<int>( "package_manager.sash_pos",
405 &m_PackageManager.sash_pos, 380 ) );
406
407 m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "git.repositories",
408 [&]() -> nlohmann::json
409 {
410 nlohmann::json ret = {};
411
412 for( const GIT_REPOSITORY& repo : m_Git.repositories )
413 {
414 nlohmann::json repoJson = {};
415
416 repoJson["name"] = repo.name;
417 repoJson["path"] = repo.path;
418 repoJson["authType"] = repo.authType;
419 repoJson["username"] = repo.username;
420 repoJson["ssh_path"] = repo.ssh_path;
421 repoJson["active"] = repo.active;
422
423 ret.push_back( repoJson );
424 }
425
426 return ret;
427 },
428 [&]( const nlohmann::json& aJson )
429 {
430 if( !aJson.is_array() )
431 return;
432
433 m_Git.repositories.clear();
434
435 for( const auto& repoJson : aJson )
436 {
437 GIT_REPOSITORY repo;
438
439 repo.name = repoJson["name"].get<wxString>();
440 repo.path = repoJson["path"].get<wxString>();
441 repo.authType = repoJson["authType"].get<wxString>();
442 repo.username = repoJson["username"].get<wxString>();
443 repo.ssh_path = repoJson["ssh_path"].get<wxString>();
444 repo.active = repoJson["active"].get<bool>();
445 repo.checkValid = true;
446
447 m_Git.repositories.push_back( repo );
448 }
449 },
450 {} ) );
451
452 m_params.emplace_back( new PARAM<wxString>( "git.authorName",
453 &m_Git.authorName, wxS( "" ) ) );
454
455 m_params.emplace_back( new PARAM<wxString>( "git.authorEmail",
456 &m_Git.authorEmail, wxS( "" ) ) );
457
458 m_params.emplace_back( new PARAM<bool>( "git.useDefaultAuthor",
459 &m_Git.useDefaultAuthor, true ) );
460
461 m_params.emplace_back( new PARAM<bool>( "git.enableGit",
462 &m_Git.enableGit, true ) );
463
464 m_params.emplace_back( new PARAM<int>( "git.updatInterval",
465 &m_Git.updatInterval, 5 ) );
466
467 m_params.emplace_back( new PARAM<wxString>( "api.interpreter_path",
468 &m_Api.python_interpreter, wxS( "" ) ) );
469
470 m_params.emplace_back( new PARAM<bool>( "api.enable_server",
471 &m_Api.enable_server, false ) );
472
473 m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "dialog.controls",
474 [&]() -> nlohmann::json
475 {
476 nlohmann::json ret = nlohmann::json::object();
477
478 for( const auto& dlg : m_csInternals->m_dialogControlValues )
479 ret[ dlg.first ] = dlg.second;
480
481 return ret;
482 },
483 [&]( const nlohmann::json& aVal )
484 {
485 m_csInternals->m_dialogControlValues.clear();
486
487 if( !aVal.is_object() )
488 return;
489
490 for( auto& [dlgKey, dlgVal] : aVal.items() )
491 {
492 if( !dlgVal.is_object() )
493 continue;
494
495 for( auto& [ctrlKey, ctrlVal] : dlgVal.items() )
496 m_csInternals->m_dialogControlValues[ dlgKey ][ ctrlKey ] = ctrlVal;
497 }
498 },
499 nlohmann::json::object() ) );
500
501
502 registerMigration( 0, 1, std::bind( &COMMON_SETTINGS::migrateSchema0to1, this ) );
503 registerMigration( 1, 2, std::bind( &COMMON_SETTINGS::migrateSchema1to2, this ) );
504 registerMigration( 2, 3, std::bind( &COMMON_SETTINGS::migrateSchema2to3, this ) );
505 registerMigration( 3, 4, std::bind( &COMMON_SETTINGS::migrateSchema3to4, this ) );
506 registerMigration( 4, 5, std::bind( &COMMON_SETTINGS::migrateSchema4to5, this ) );
507 registerMigration( 5, 6, std::bind( &COMMON_SETTINGS::migrateSchema5to6, this ) );
508}
509
510
512{
518
519 nlohmann::json::json_pointer mwp_pointer( "/input/mousewheel_pan"_json_pointer );
520
521 bool mwp = false;
522
523 try
524 {
525 mwp = m_internals->at( mwp_pointer );
526 m_internals->At( "input" ).erase( "mousewheel_pan" );
527 }
528 catch( ... )
529 {
530 wxLogTrace( traceSettings,
531 wxT( "COMMON_SETTINGS::Migrate 0->1: mousewheel_pan not found" ) );
532 }
533
534 if( mwp )
535 {
536 ( *m_internals )[nlohmann::json::json_pointer( "/input/horizontal_pan" )] = true;
537 ( *m_internals )[nlohmann::json::json_pointer( "/input/scroll_modifier_pan_h" )] = WXK_SHIFT;
538 ( *m_internals )[nlohmann::json::json_pointer( "/input/scroll_modifier_pan_v" )] = 0;
539 ( *m_internals )[nlohmann::json::json_pointer( "/input/scroll_modifier_zoom" )] = WXK_CONTROL;
540 }
541 else
542 {
543 ( *m_internals )[nlohmann::json::json_pointer( "/input/horizontal_pan" )] = false;
544 ( *m_internals )[nlohmann::json::json_pointer( "/input/scroll_modifier_pan_h" )] = WXK_CONTROL;
545 ( *m_internals )[nlohmann::json::json_pointer( "/input/scroll_modifier_pan_v" )] = WXK_SHIFT;
546 ( *m_internals )[nlohmann::json::json_pointer( "/input/scroll_modifier_zoom" )] = 0;
547 }
548
549 return true;
550}
551
552
554{
555 nlohmann::json::json_pointer v1_pointer( "/input/prefer_select_to_drag"_json_pointer );
556
557 bool prefer_selection = false;
558
559 try
560 {
561 prefer_selection = m_internals->at( v1_pointer );
562 m_internals->at( nlohmann::json::json_pointer( "/input"_json_pointer ) )
563 .erase( "prefer_select_to_drag" );
564 }
565 catch( ... )
566 {
567 wxLogTrace( traceSettings,
568 wxT( "COMMON_SETTINGS::Migrate 1->2: prefer_select_to_drag not found" ) );
569 }
570
571 if( prefer_selection )
572 ( *m_internals )[nlohmann::json::json_pointer( "/input/mouse_left" )] = MOUSE_DRAG_ACTION::SELECT;
573 else
574 ( *m_internals )[nlohmann::json::json_pointer( "/input/mouse_left" )] = MOUSE_DRAG_ACTION::DRAG_ANY;
575
576 return true;
577}
578
579
581{
582 wxFileName cfgpath;
583 cfgpath.AssignDir( PATHS::GetUserSettingsPath() );
584 cfgpath.AppendDir( wxT( "3d" ) );
585 cfgpath.SetFullName( wxS( "3Dresolver.cfg" ) );
586 cfgpath.MakeAbsolute();
587
588 std::vector<LEGACY_3D_SEARCH_PATH> legacyPaths;
589 readLegacy3DResolverCfg( cfgpath.GetFullPath(), legacyPaths );
590
591 // env variables have a limited allowed character set for names
592 wxRegEx nonValidCharsRegex( wxS( "[^A-Z0-9_]+" ), wxRE_ADVANCED );
593
594 for( const LEGACY_3D_SEARCH_PATH& path : legacyPaths )
595 {
596 wxString key = path.m_Alias;
597 const wxString& val = path.m_Pathvar;
598
599 // The 3d alias config didn't use the same naming restrictions as real env variables
600 // We need to sanitize them
601
602 // upper case only
603 key.MakeUpper();
604
605 // logically swap - with _
606 key.Replace( wxS( "-" ), wxS( "_" ) );
607
608 // remove any other chars
609 nonValidCharsRegex.Replace( &key, wxEmptyString );
610
611 if( !m_Env.vars.count( key ) )
612 {
613 wxLogTrace( traceEnvVars, wxS( "COMMON_SETTINGS: Loaded new var: %s = %s" ), key, val );
614 m_Env.vars[key] = ENV_VAR_ITEM( key, val );
615 }
616 }
617
618 if( cfgpath.FileExists() )
619 {
620 wxRemoveFile( cfgpath.GetFullPath() );
621 }
622
623 return true;
624}
625
626
628{
629 // >= 10 = add 1
630 try
631 {
632 // Update netclass panel shown columns for eeschema
633 const nlohmann::json::json_pointer v3_pointer_eeschema( "/netclass_panel/eeschema_shown_columns"_json_pointer );
634 wxString eeSchemaColumnList_old = m_internals->at( v3_pointer_eeschema );
635
636 wxStringTokenizer eeSchemaShownTokens( eeSchemaColumnList_old, " \t\r\n" );
637 wxString eeSchemaColumnList_new;
638
639 while( eeSchemaShownTokens.HasMoreTokens() )
640 {
641 long colNumber;
642 eeSchemaShownTokens.GetNextToken().ToLong( &colNumber );
643
644 if( colNumber >= 10 )
645 ++colNumber;
646
647 eeSchemaColumnList_new += wxString::Format( wxT( "%ld " ), colNumber );
648 }
649
650 eeSchemaColumnList_new.Trim( true );
651 eeSchemaColumnList_new.Trim( false );
652
653 m_internals->at( v3_pointer_eeschema ) = eeSchemaColumnList_new.ToUTF8();
654
655 // Update netclass panel shown columns for pcbnew
656 const nlohmann::json::json_pointer v3_pointer_pcbnew( "/netclass_panel/pcbnew_shown_columns"_json_pointer );
657 wxString pcbnewColumnList_old = m_internals->at( v3_pointer_pcbnew );
658
659 wxStringTokenizer pcbnewShownTokens( pcbnewColumnList_old, " \t\r\n" );
660 wxString pcbnewColumnList_new;
661
662 while( pcbnewShownTokens.HasMoreTokens() )
663 {
664 long colNumber;
665 pcbnewShownTokens.GetNextToken().ToLong( &colNumber );
666
667 if( colNumber >= 10 )
668 ++colNumber;
669
670 pcbnewColumnList_new += wxString::Format( wxT( "%ld " ), colNumber );
671 }
672
673 pcbnewColumnList_new.Trim( true );
674 pcbnewColumnList_new.Trim( false );
675
676 m_internals->at( v3_pointer_pcbnew ) = pcbnewColumnList_new.ToUTF8();
677 }
678 catch( ... )
679 {
680 wxLogTrace( traceSettings, wxT( "COMMON_SETTINGS::Migrate 3->4: /netclass_panel/shown_columns not found" ) );
681 }
682
683 return true;
684}
685
686
688{
689 try
690 {
691 nlohmann::json& controls = m_internals->At( "dialog" ).at( "controls" );
692
693 for( auto& [dlgKey, dlgVal] : controls.items() )
694 {
695 if( !dlgVal.is_object() )
696 continue;
697
698 auto geoIt = dlgVal.find( "__geometry" );
699
700 if( geoIt == dlgVal.end() || !geoIt->is_object() )
701 continue;
702
703 nlohmann::json& geom = *geoIt;
704
705 // Legacy values were stored in logical pixels. Convert to DIP using the
706 // primary display's scale factor (best approximation without window context).
707 int w = geom.value( "w", 0 );
708 int h = geom.value( "h", 0 );
709
710 wxSize dipSize = wxWindow::ToDIP( wxSize( w, h ), nullptr );
711 geom[ "w" ] = dipSize.x;
712 geom[ "h" ] = dipSize.y;
713
714 geom.erase( "dip" );
715 }
716 }
717 catch( ... )
718 {
719 wxLogTrace( traceSettings,
720 wxT( "COMMON_SETTINGS::Migrate 4->5: dialog.controls not found" ) );
721 }
722
723 return true;
724}
725
726
728{
729 // Schema 6 introduces auto_backup.format and auto_backup.location. Pre-schema-6
730 // installs unconditionally produced timestamped zip archives whenever a save was
731 // eligible for backup, so the new INCREMENTAL default would silently disable archive
732 // creation for users who already had auto_backup.enabled set. Write ZIP into the
733 // upgraded config to preserve their backup behavior; new installs skip the migration
734 // path entirely and keep the new default. The location default (PROJECT_DIR) already
735 // matches the legacy on-disk layout, so we leave it absent.
736 try
737 {
738 if( !Contains( "auto_backup.format" ) )
739 Set<int>( "auto_backup.format", static_cast<int>( BACKUP_FORMAT::ZIP ) );
740 }
741 catch( ... )
742 {
743 wxLogTrace( traceSettings,
744 wxT( "COMMON_SETTINGS::Migrate 5->6: failed to set auto_backup.format" ) );
745 }
746
747 return true;
748}
749
750
751bool COMMON_SETTINGS::MigrateFromLegacy( wxConfigBase* aCfg )
752{
753 bool ret = true;
754
755 ret &= fromLegacy<double>( aCfg, "CanvasScale", "appearance.canvas_scale" );
756 ret &= fromLegacy<int>( aCfg, "IconScale", "appearance.icon_scale" );
757 ret &= fromLegacy<bool>( aCfg, "UseIconsInMenus", "appearance.use_icons_in_menus" );
758 ret &= fromLegacy<bool>( aCfg, "ShowEnvVarWarningDialog", "environment.show_warning_dialog" );
759
760 auto load_env_vars =
761 [&]()
762 {
763 wxString key, value;
764 long index = 0;
765 nlohmann::json::json_pointer ptr = m_internals->PointerFromString( "environment.vars" );
766
767 aCfg->SetPath( "EnvironmentVariables" );
768 ( *m_internals )[ptr] = nlohmann::json( {} );
769
770 while( aCfg->GetNextEntry( key, index ) )
771 {
772 if( versionedEnvVarRegex.Matches( key ) )
773 {
774 wxLogTrace( traceSettings,
775 wxT( "Migrate Env: %s is blacklisted; skipping." ), key );
776 continue;
777 }
778
779 value = aCfg->Read( key, wxEmptyString );
780
781 if( !value.IsEmpty() )
782 {
783 ptr.push_back( key.ToStdString() );
784
785 wxLogTrace( traceSettings, wxT( "Migrate Env: %s=%s" ),
786 ptr.to_string(), value );
787 ( *m_internals )[ptr] = value.ToUTF8();
788
789 ptr.pop_back();
790 }
791 }
792
793 aCfg->SetPath( ".." );
794 };
795
796 load_env_vars();
797
798 bool mousewheel_pan = false;
799
800 if( aCfg->Read( "MousewheelPAN", &mousewheel_pan ) && mousewheel_pan )
801 {
802 Set( "input.horizontal_pan", true );
803 Set( "input.scroll_modifier_pan_h", static_cast<int>( WXK_SHIFT ) );
804 Set( "input.scroll_modifier_pan_v", 0 );
805 Set( "input.scroll_modifier_zoom", static_cast<int>( WXK_CONTROL ) );
806 }
807
808 ret &= fromLegacy<bool>( aCfg, "AutoPAN", "input.auto_pan" );
809 ret &= fromLegacy<bool>( aCfg, "ImmediateActions", "input.immediate_actions" );
810 ret &= fromLegacy<bool>( aCfg, "PreferSelectionToDragging", "input.prefer_select_to_drag" );
811 ret &= fromLegacy<bool>( aCfg, "MoveWarpsCursor", "input.warp_mouse_on_move" );
812 ret &= fromLegacy<bool>( aCfg, "ZoomNoCenter", "input.center_on_zoom" );
813
814 // This was stored inverted in legacy config
815 if( std::optional<bool> value = Get<bool>( "input.center_on_zoom" ) )
816 Set( "input.center_on_zoom", !( *value ) );
817
818 ret &= fromLegacy<int>( aCfg, "OpenGLAntialiasingMode", "graphics.opengl_antialiasing_mode" );
819 ret &= fromLegacy<int>( aCfg, "CairoAntialiasingMode", "graphics.cairo_antialiasing_mode" );
820
821 ret &= fromLegacy<int>( aCfg, "AutoSaveInterval", "system.local_history_debounce" );
822 ret &= fromLegacyString( aCfg, "Editor", "system.editor_name" );
823 ret &= fromLegacy<int>( aCfg, "FileHistorySize", "system.file_history_size" );
824 ret &= fromLegacyString( aCfg, "LanguageID", "system.language" );
825 ret &= fromLegacyString( aCfg, "PdfBrowserName", "system.pdf_viewer_name" );
826 ret &= fromLegacy<bool>( aCfg, "UseSystemBrowser", "system.use_system_pdf_viewer" );
827 ret &= fromLegacyString( aCfg, "WorkingDir", "system.working_dir" );
828
829 return ret;
830}
831
832
834{
835 auto addVar =
836 [&]( const wxString& aKey, const wxString& aDefault )
837 {
838 m_Env.vars[aKey] = ENV_VAR_ITEM( aKey, aDefault, aDefault );
839
840 wxString envValue;
841
842 if( wxGetEnv( aKey, &envValue ) == true && !envValue.IsEmpty() )
843 {
844 m_Env.vars[aKey].SetValue( envValue );
845 m_Env.vars[aKey].SetDefinedExternally();
846 wxLogTrace( traceEnvVars,
847 wxS( "InitializeEnvironment: Entry %s defined externally as %s" ), aKey,
848 envValue );
849 }
850 else
851 {
852 wxLogTrace( traceEnvVars, wxS( "InitializeEnvironment: Setting entry %s to "
853 "default %s" ),
854 aKey, aDefault );
855 }
856 };
857
858 addVar( ENV_VAR::GetVersionedEnvVarName( wxS( "FOOTPRINT_DIR" ) ), PATHS::GetStockFootprintsPath() );
859 addVar( ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ), PATHS::GetStock3dmodelsPath() );
860 addVar( ENV_VAR::GetVersionedEnvVarName( wxS( "TEMPLATE_DIR" ) ), PATHS::GetStockTemplatesPath() );
861 addVar( wxT( "KICAD_USER_TEMPLATE_DIR" ), PATHS::GetUserTemplatesPath() );
862 addVar( ENV_VAR::GetVersionedEnvVarName( wxS( "3RD_PARTY" ) ), PATHS::GetDefault3rdPartyPath() );
863 addVar( ENV_VAR::GetVersionedEnvVarName( wxS( "SYMBOL_DIR" ) ), PATHS::GetStockSymbolsPath() );
864 addVar( ENV_VAR::GetVersionedEnvVarName( wxS( "DESIGN_BLOCK_DIR" ) ), PATHS::GetStockDesignBlocksPath() );
865}
866
867
869 std::vector<LEGACY_3D_SEARCH_PATH>& aSearchPaths )
870{
871 wxFileName cfgpath( path );
872
873 // This should be the same as wxWidgets 3.0 wxPATH_NORM_ALL which is deprecated in 3.1.
874 // There are known issues with environment variable expansion so maybe we should be using
875 // our own ExpandEnvVarSubstitutions() here instead.
876 cfgpath.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS );
877 wxString cfgname = cfgpath.GetFullPath();
878
879 std::ifstream cfgFile;
880 std::string cfgLine;
881
882 if( !wxFileName::Exists( cfgname ) )
883 {
884 std::ostringstream ostr;
885 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
886 wxString errmsg = "no 3D configuration file";
887 ostr << " * " << errmsg.ToUTF8() << " '";
888 ostr << cfgname.ToUTF8() << "'";
889 wxLogTrace( traceSettings, "%s\n", ostr.str().c_str() );
890 return false;
891 }
892
893 cfgFile.open( cfgname.ToUTF8() );
894
895 if( !cfgFile.is_open() )
896 {
897 std::ostringstream ostr;
898 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
899 wxString errmsg = wxS( "Could not open configuration file" );
900 ostr << " * " << errmsg.ToUTF8() << " '" << cfgname.ToUTF8() << "'";
901 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
902 return false;
903 }
904
905 int lineno = 0;
907 size_t idx;
908 int vnum = 0; // version number
909
910 while( cfgFile.good() )
911 {
912 cfgLine.clear();
913 std::getline( cfgFile, cfgLine );
914 ++lineno;
915
916 if( cfgLine.empty() )
917 {
918 if( cfgFile.eof() )
919 break;
920
921 continue;
922 }
923
924 if( 1 == lineno && cfgLine.compare( 0, 2, "#V" ) == 0 )
925 {
926 // extract the version number and parse accordingly
927 if( cfgLine.size() > 2 )
928 {
929 std::istringstream istr;
930 istr.str( cfgLine.substr( 2 ) );
931 istr >> vnum;
932 }
933
934 continue;
935 }
936
937 idx = 0;
938
939 if( !getLegacy3DHollerith( cfgLine, idx, al.m_Alias ) )
940 continue;
941
942 // Don't add KICADn_3DMODEL_DIR, one of its legacy equivalents, or KIPRJMOD from a
943 // config file. They're system variables which are defined at runtime.
944 wxString versionedPath = wxString::Format( wxS( "${%s}" ),
945 ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ) );
946
947 if( al.m_Alias == versionedPath || al.m_Alias == wxS( "${KIPRJMOD}" )
948 || al.m_Alias == wxS( "$(KIPRJMOD)" ) || al.m_Alias == wxS( "${KISYS3DMOD}" )
949 || al.m_Alias == wxS( "$(KISYS3DMOD)" ) )
950 {
951 continue;
952 }
953
954 if( !getLegacy3DHollerith( cfgLine, idx, al.m_Pathvar ) )
955 continue;
956
957 if( !getLegacy3DHollerith( cfgLine, idx, al.m_Description ) )
958 continue;
959
960 aSearchPaths.push_back( al );
961 }
962
963 cfgFile.close();
964
965 return true;
966}
967
968
969bool COMMON_SETTINGS::getLegacy3DHollerith( const std::string& aString, size_t& aIndex,
970 wxString& aResult )
971{
972 aResult.clear();
973
974 if( aIndex >= aString.size() )
975 {
976 std::ostringstream ostr;
977 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
978 wxString errmsg = wxS( "bad Hollerith string on line" );
979 ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
980 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
981
982 return false;
983 }
984
985 size_t i2 = aString.find( '"', aIndex );
986
987 if( std::string::npos == i2 )
988 {
989 std::ostringstream ostr;
990 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
991 wxString errmsg = wxS( "missing opening quote mark in config file" );
992 ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
993 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
994
995 return false;
996 }
997
998 ++i2;
999
1000 if( i2 >= aString.size() )
1001 {
1002 std::ostringstream ostr;
1003 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
1004 wxString errmsg = wxS( "invalid entry (unexpected end of line)" );
1005 ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
1006 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
1007
1008 return false;
1009 }
1010
1011 std::string tnum;
1012
1013 while( aString[i2] >= '0' && aString[i2] <= '9' )
1014 tnum.append( 1, aString[i2++] );
1015
1016 if( tnum.empty() || aString[i2++] != ':' )
1017 {
1018 std::ostringstream ostr;
1019 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
1020 wxString errmsg = wxS( "bad Hollerith string on line" );
1021 ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
1022 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
1023
1024 return false;
1025 }
1026
1027 std::istringstream istr;
1028 istr.str( tnum );
1029 size_t nchars;
1030 istr >> nchars;
1031
1032 if( ( i2 + nchars ) >= aString.size() )
1033 {
1034 std::ostringstream ostr;
1035 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
1036 wxString errmsg = wxS( "invalid entry (unexpected end of line)" );
1037 ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
1038 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
1039
1040 return false;
1041 }
1042
1043 if( nchars > 0 )
1044 {
1045 aResult = wxString::FromUTF8( aString.substr( i2, nchars ).c_str() );
1046 i2 += nchars;
1047 }
1048
1049 if( i2 >= aString.size() || aString[i2] != '"' )
1050 {
1051 std::ostringstream ostr;
1052 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
1053 wxString errmsg = wxS( "missing closing quote mark in config file" );
1054 ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
1055 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
1056
1057 return false;
1058 }
1059
1060 aIndex = i2 + 1;
1061 return true;
1062}
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:230
Like a normal param, but with custom getter and setter functions.
Definition parameters.h:297
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:77
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:39