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 = 5;
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<unsigned long long>( "auto_backup.limit_total_size",
131 &m_Backup.limit_total_size, 104857600 ) );
132
133 auto envVarsParam = m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "environment.vars",
134 [&]() -> nlohmann::json
135 {
136 nlohmann::json ret = {};
137
138 for( const std::pair<wxString, ENV_VAR_ITEM> entry : m_Env.vars )
139 {
140 const ENV_VAR_ITEM& var = entry.second;
141
142 wxASSERT( entry.first == var.GetKey() );
143
144 // Default values are never persisted
145 if( var.IsDefault() )
146 {
147 wxLogTrace( traceEnvVars,
148 wxS( "COMMON_SETTINGS: Env var %s skipping save (default)" ),
149 var.GetKey() );
150 continue;
151 }
152
153 wxString value = var.GetValue();
154
155 value.Trim( true ).Trim( false ); // Trim from both sides
156
157 // Vars that existed in JSON are persisted, but if they were overridden
158 // externally, we persist the old value (i.e. the one that was loaded from JSON)
159 if( var.GetDefinedExternally() )
160 {
161 if( var.GetDefinedInSettings() )
162 {
163 wxLogTrace( traceEnvVars,
164 wxS( "COMMON_SETTINGS: Env var %s was overridden "
165 "externally, saving previously-loaded value %s" ),
166 var.GetKey(), var.GetSettingsValue() );
167 value = var.GetSettingsValue();
168 }
169 else
170 {
171 wxLogTrace( traceEnvVars,
172 wxS( "COMMON_SETTINGS: Env var %s skipping save "
173 "(external)" ),
174 var.GetKey() );
175 continue;
176 }
177 }
178
179 wxLogTrace( traceEnvVars,
180 wxS( "COMMON_SETTINGS: Saving env var %s = %s" ),
181 var.GetKey(), value);
182
183 std::string key( var.GetKey().Trim( true ).Trim( false ).ToUTF8() );
184 ret[ std::move( key ) ] = value;
185 }
186
187 return ret;
188 },
189 [&]( const nlohmann::json& aJson )
190 {
191 if( !aJson.is_object() )
192 return;
193
194 for( const auto& entry : aJson.items() )
195 {
196 wxString key = wxString( entry.key().c_str(), wxConvUTF8 ).Trim( true ).Trim( false );
197 wxString val = entry.value().get<wxString>().Trim( true ).Trim( false );
198
199 if( m_Env.vars.count( key ) )
200 {
201 if( m_Env.vars[key].GetDefinedExternally() )
202 {
203 wxLogTrace( traceEnvVars,
204 wxS( "COMMON_SETTINGS: %s is defined externally" ),
205 key );
206 m_Env.vars[key].SetDefinedInSettings();
207 m_Env.vars[key].SetSettingsValue( val );
208 continue;
209 }
210 else
211 {
212 wxLogTrace( traceEnvVars,
213 wxS( "COMMON_SETTINGS: Updating %s: %s -> %s"),
214 key, m_Env.vars[key].GetValue(), val );
215 m_Env.vars[key].SetValue( val );
216 }
217 }
218 else
219 {
220 wxLogTrace( traceEnvVars,
221 wxS( "COMMON_SETTINGS: Loaded new var: %s = %s" ),
222 key, val );
223 m_Env.vars[key] = ENV_VAR_ITEM( key, val );
224 }
225
226 m_Env.vars[key].SetDefinedInSettings();
227 m_Env.vars[key].SetSettingsValue( val );
228 }
229 },
230 {} ) );
231 envVarsParam->SetClearUnknownKeys();
232
233 m_params.emplace_back( new PARAM<bool>( "input.focus_follow_sch_pcb",
234 &m_Input.focus_follow_sch_pcb, false ) );
235
236 m_params.emplace_back( new PARAM<bool>( "input.auto_pan", &m_Input.auto_pan, false ) );
237
238 m_params.emplace_back( new PARAM<int>( "input.auto_pan_acceleration",
239 &m_Input.auto_pan_acceleration, 5 ) );
240
241 m_params.emplace_back( new PARAM<bool>( "input.center_on_zoom",
242 &m_Input.center_on_zoom, true ) );
243
244 m_params.emplace_back( new PARAM<bool>( "input.immediate_actions",
245 &m_Input.immediate_actions, true ) );
246
247 m_params.emplace_back( new PARAM<bool>( "input.warp_mouse_on_move",
248 &m_Input.warp_mouse_on_move, true ) );
249
250 m_params.emplace_back( new PARAM<bool>( "input.horizontal_pan",
251 &m_Input.horizontal_pan, false ) );
252
253 m_params.emplace_back( new PARAM<bool>( "input.hotkey_feedback",
254 &m_Input.hotkey_feedback, true ) );
255
256 m_params.emplace_back( new PARAM<bool>( "input.zoom_acceleration",
257 &m_Input.zoom_acceleration, false ) );
258
259#ifdef __WXMAC__
260 int default_zoom_speed = 5;
261#else
262 int default_zoom_speed = 1;
263#endif
264
265 m_params.emplace_back( new PARAM<int>( "input.zoom_speed",
266 &m_Input.zoom_speed, default_zoom_speed ) );
267
268 m_params.emplace_back( new PARAM<bool>( "input.zoom_speed_auto",
269 &m_Input.zoom_speed_auto, true ) );
270
271 m_params.emplace_back( new PARAM<int>( "input.scroll_modifier_zoom",
272 &m_Input.scroll_modifier_zoom, 0 ) );
273
274 m_params.emplace_back( new PARAM<int>( "input.scroll_modifier_pan_h",
275 &m_Input.scroll_modifier_pan_h, WXK_CONTROL ) );
276
277 m_params.emplace_back( new PARAM<int>( "input.scroll_modifier_pan_v",
278 &m_Input.scroll_modifier_pan_v, WXK_SHIFT ) );
279
280 m_params.emplace_back( new PARAM<int>( "input.motion_pan_modifier",
281 &m_Input.motion_pan_modifier, 0 ) );
282
283 m_params.emplace_back( new PARAM<bool>( "input.reverse_scroll_zoom",
284 &m_Input.reverse_scroll_zoom, false ) );
285
286 m_params.emplace_back( new PARAM<bool>( "input.reverse_scroll_pan_h",
287 &m_Input.reverse_scroll_pan_h, false ) );
288
289 m_params.emplace_back( new PARAM_ENUM<MOUSE_DRAG_ACTION>( "input.mouse_left",
292
293 m_params.emplace_back( new PARAM_ENUM<MOUSE_DRAG_ACTION>( "input.mouse_middle",
296
297 m_params.emplace_back( new PARAM_ENUM<MOUSE_DRAG_ACTION>( "input.mouse_right",
300
301 m_params.emplace_back( new PARAM<int>( "spacemouse.rotate_speed",
302 &m_SpaceMouse.rotate_speed, 5, 1, 10 ) );
303
304 m_params.emplace_back( new PARAM<int>( "spacemouse.pan_speed",
305 &m_SpaceMouse.pan_speed, 5, 1, 10 ) );
306
307 m_params.emplace_back( new PARAM<bool>( "spacemouse.reverse_rotate",
308 &m_SpaceMouse.reverse_rotate, false ) );
309
310 m_params.emplace_back( new PARAM<bool>( "spacemouse.reverse_pan_x",
311 &m_SpaceMouse.reverse_pan_x, false ) );
312
313 m_params.emplace_back( new PARAM<bool>( "spacemouse.reverse_pan_y",
314 &m_SpaceMouse.reverse_pan_y, false ) );
315
316 m_params.emplace_back( new PARAM<bool>( "spacemouse.reverse_zoom",
317 &m_SpaceMouse.reverse_zoom, false ) );
318
319 m_params.emplace_back( new PARAM<int>( "graphics.canvas_type",
320 &m_Graphics.canvas_type, EDA_DRAW_PANEL_GAL::GAL_TYPE_OPENGL ) );
321
322 m_params.emplace_back( new PARAM<int>( "graphics.antialiasing_mode",
323 &m_Graphics.aa_mode, 2, 0, 2 ) );
324
325 m_params.emplace_back( new PARAM<bool>( "system.local_history_enabled",
326 &m_System.local_history_enabled, true ) );
327 m_params.emplace_back( new PARAM<int>( "system.local_history_debounce",
328 &m_System.local_history_debounce, 5, 0, 100000 ) );
329
330#ifdef __WXMAC__
331 m_params.emplace_back( new PARAM<wxString>( "system.text_editor",
332 &m_System.text_editor, wxS( "/usr/bin/open -e" ) ) );
333#else
334 m_params.emplace_back( new PARAM<wxString>( "system.text_editor",
335 &m_System.text_editor, wxS( "" ) ) );
336#endif
337
338#if defined( __WINDOWS__ )
339 m_params.emplace_back( new PARAM<wxString>( "system.file_explorer",
340 &m_System.file_explorer, wxS( "explorer.exe /n,/select,%F" ) ) );
341#else
342 m_params.emplace_back( new PARAM<wxString>( "system.file_explorer",
343 &m_System.file_explorer, wxS( "" ) ) );
344#endif
345
346 m_params.emplace_back( new PARAM<int>( "system.file_history_size",
347 &m_System.file_history_size, 9 ) );
348
349 m_params.emplace_back( new PARAM<wxString>( "system.language",
350 &m_System.language, wxS( "Default" ) ) );
351
352 m_params.emplace_back( new PARAM<wxString>( "system.pdf_viewer_name",
353 &m_System.pdf_viewer_name, wxS( "" ) ) );
354
355 m_params.emplace_back( new PARAM<bool>( "system.use_system_pdf_viewer",
356 &m_System.use_system_pdf_viewer, true ) );
357
358 m_params.emplace_back( new PARAM<wxString>( "system.working_dir",
359 &m_System.working_dir, wxS( "" ) ) );
360
361 m_params.emplace_back( new PARAM<int>( "system.clear_3d_cache_interval",
362 &m_System.clear_3d_cache_interval, 30 ) );
363
364 m_params.emplace_back( new PARAM<bool>( "do_not_show_again.zone_fill_warning",
365 &m_DoNotShowAgain.zone_fill_warning, false ) );
366
367 m_params.emplace_back( new PARAM<bool>( "do_not_show_again.env_var_overwrite_warning",
368 &m_DoNotShowAgain.env_var_overwrite_warning, false ) );
369
370 m_params.emplace_back( new PARAM<bool>( "do_not_show_again.scaled_3d_models_warning",
371 &m_DoNotShowAgain.scaled_3d_models_warning, false ) );
372
373 m_params.emplace_back( new PARAM<bool>( "do_not_show_again.data_collection_prompt",
374 &m_DoNotShowAgain.data_collection_prompt, false ) );
375
376 m_params.emplace_back( new PARAM<bool>( "do_not_show_again.update_check_prompt",
377 &m_DoNotShowAgain.update_check_prompt, false ) );
378
379 m_params.emplace_back( new PARAM<bool>( "do_not_show_again.migrate_wrl_prompt",
380 &m_DoNotShowAgain.migrate_wrl_prompt, false ) );
381
382 m_params.emplace_back( new PARAM_LIST<wxString>( "system.extra_3d_search_dirs",
383 &m_Extra3DSearchDirs, {} ) );
384
385 m_params.emplace_back( new PARAM<bool>( "session.remember_open_files",
386 &m_Session.remember_open_files, false ) );
387
388 m_params.emplace_back( new PARAM_LIST<wxString>( "session.pinned_symbol_libs",
389 &m_Session.pinned_symbol_libs, {} ) );
390
391 m_params.emplace_back( new PARAM_LIST<wxString>( "session.pinned_fp_libs",
392 &m_Session.pinned_fp_libs, {} ) );
393
394 m_params.emplace_back( new PARAM_LIST<wxString>( "session.pinned_design_block_libs",
395 &m_Session.pinned_design_block_libs, {} ) );
396
397 m_params.emplace_back( new PARAM<int>( "package_manager.sash_pos",
398 &m_PackageManager.sash_pos, 380 ) );
399
400 m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "git.repositories",
401 [&]() -> nlohmann::json
402 {
403 nlohmann::json ret = {};
404
405 for( const GIT_REPOSITORY& repo : m_Git.repositories )
406 {
407 nlohmann::json repoJson = {};
408
409 repoJson["name"] = repo.name;
410 repoJson["path"] = repo.path;
411 repoJson["authType"] = repo.authType;
412 repoJson["username"] = repo.username;
413 repoJson["ssh_path"] = repo.ssh_path;
414 repoJson["active"] = repo.active;
415
416 ret.push_back( repoJson );
417 }
418
419 return ret;
420 },
421 [&]( const nlohmann::json& aJson )
422 {
423 if( !aJson.is_array() )
424 return;
425
426 m_Git.repositories.clear();
427
428 for( const auto& repoJson : aJson )
429 {
430 GIT_REPOSITORY repo;
431
432 repo.name = repoJson["name"].get<wxString>();
433 repo.path = repoJson["path"].get<wxString>();
434 repo.authType = repoJson["authType"].get<wxString>();
435 repo.username = repoJson["username"].get<wxString>();
436 repo.ssh_path = repoJson["ssh_path"].get<wxString>();
437 repo.active = repoJson["active"].get<bool>();
438 repo.checkValid = true;
439
440 m_Git.repositories.push_back( repo );
441 }
442 },
443 {} ) );
444
445 m_params.emplace_back( new PARAM<wxString>( "git.authorName",
446 &m_Git.authorName, wxS( "" ) ) );
447
448 m_params.emplace_back( new PARAM<wxString>( "git.authorEmail",
449 &m_Git.authorEmail, wxS( "" ) ) );
450
451 m_params.emplace_back( new PARAM<bool>( "git.useDefaultAuthor",
452 &m_Git.useDefaultAuthor, true ) );
453
454 m_params.emplace_back( new PARAM<bool>( "git.enableGit",
455 &m_Git.enableGit, true ) );
456
457 m_params.emplace_back( new PARAM<int>( "git.updatInterval",
458 &m_Git.updatInterval, 5 ) );
459
460 m_params.emplace_back( new PARAM<wxString>( "api.interpreter_path",
461 &m_Api.python_interpreter, wxS( "" ) ) );
462
463 m_params.emplace_back( new PARAM<bool>( "api.enable_server",
464 &m_Api.enable_server, false ) );
465
466 m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "dialog.controls",
467 [&]() -> nlohmann::json
468 {
469 nlohmann::json ret = nlohmann::json::object();
470
471 for( const auto& dlg : m_csInternals->m_dialogControlValues )
472 ret[ dlg.first ] = dlg.second;
473
474 return ret;
475 },
476 [&]( const nlohmann::json& aVal )
477 {
478 m_csInternals->m_dialogControlValues.clear();
479
480 if( !aVal.is_object() )
481 return;
482
483 for( auto& [dlgKey, dlgVal] : aVal.items() )
484 {
485 if( !dlgVal.is_object() )
486 continue;
487
488 for( auto& [ctrlKey, ctrlVal] : dlgVal.items() )
489 m_csInternals->m_dialogControlValues[ dlgKey ][ ctrlKey ] = ctrlVal;
490 }
491 },
492 nlohmann::json::object() ) );
493
494
495 registerMigration( 0, 1, std::bind( &COMMON_SETTINGS::migrateSchema0to1, this ) );
496 registerMigration( 1, 2, std::bind( &COMMON_SETTINGS::migrateSchema1to2, this ) );
497 registerMigration( 2, 3, std::bind( &COMMON_SETTINGS::migrateSchema2to3, this ) );
498 registerMigration( 3, 4, std::bind( &COMMON_SETTINGS::migrateSchema3to4, this ) );
499 registerMigration( 4, 5, std::bind( &COMMON_SETTINGS::migrateSchema4to5, this ) );
500}
501
502
504{
510
511 nlohmann::json::json_pointer mwp_pointer( "/input/mousewheel_pan"_json_pointer );
512
513 bool mwp = false;
514
515 try
516 {
517 mwp = m_internals->at( mwp_pointer );
518 m_internals->At( "input" ).erase( "mousewheel_pan" );
519 }
520 catch( ... )
521 {
522 wxLogTrace( traceSettings,
523 wxT( "COMMON_SETTINGS::Migrate 0->1: mousewheel_pan not found" ) );
524 }
525
526 if( mwp )
527 {
528 ( *m_internals )[nlohmann::json::json_pointer( "/input/horizontal_pan" )] = true;
529 ( *m_internals )[nlohmann::json::json_pointer( "/input/scroll_modifier_pan_h" )] = WXK_SHIFT;
530 ( *m_internals )[nlohmann::json::json_pointer( "/input/scroll_modifier_pan_v" )] = 0;
531 ( *m_internals )[nlohmann::json::json_pointer( "/input/scroll_modifier_zoom" )] = WXK_CONTROL;
532 }
533 else
534 {
535 ( *m_internals )[nlohmann::json::json_pointer( "/input/horizontal_pan" )] = false;
536 ( *m_internals )[nlohmann::json::json_pointer( "/input/scroll_modifier_pan_h" )] = WXK_CONTROL;
537 ( *m_internals )[nlohmann::json::json_pointer( "/input/scroll_modifier_pan_v" )] = WXK_SHIFT;
538 ( *m_internals )[nlohmann::json::json_pointer( "/input/scroll_modifier_zoom" )] = 0;
539 }
540
541 return true;
542}
543
544
546{
547 nlohmann::json::json_pointer v1_pointer( "/input/prefer_select_to_drag"_json_pointer );
548
549 bool prefer_selection = false;
550
551 try
552 {
553 prefer_selection = m_internals->at( v1_pointer );
554 m_internals->at( nlohmann::json::json_pointer( "/input"_json_pointer ) )
555 .erase( "prefer_select_to_drag" );
556 }
557 catch( ... )
558 {
559 wxLogTrace( traceSettings,
560 wxT( "COMMON_SETTINGS::Migrate 1->2: prefer_select_to_drag not found" ) );
561 }
562
563 if( prefer_selection )
564 ( *m_internals )[nlohmann::json::json_pointer( "/input/mouse_left" )] = MOUSE_DRAG_ACTION::SELECT;
565 else
566 ( *m_internals )[nlohmann::json::json_pointer( "/input/mouse_left" )] = MOUSE_DRAG_ACTION::DRAG_ANY;
567
568 return true;
569}
570
571
573{
574 wxFileName cfgpath;
575 cfgpath.AssignDir( PATHS::GetUserSettingsPath() );
576 cfgpath.AppendDir( wxT( "3d" ) );
577 cfgpath.SetFullName( wxS( "3Dresolver.cfg" ) );
578 cfgpath.MakeAbsolute();
579
580 std::vector<LEGACY_3D_SEARCH_PATH> legacyPaths;
581 readLegacy3DResolverCfg( cfgpath.GetFullPath(), legacyPaths );
582
583 // env variables have a limited allowed character set for names
584 wxRegEx nonValidCharsRegex( wxS( "[^A-Z0-9_]+" ), wxRE_ADVANCED );
585
586 for( const LEGACY_3D_SEARCH_PATH& path : legacyPaths )
587 {
588 wxString key = path.m_Alias;
589 const wxString& val = path.m_Pathvar;
590
591 // The 3d alias config didn't use the same naming restrictions as real env variables
592 // We need to sanitize them
593
594 // upper case only
595 key.MakeUpper();
596
597 // logically swap - with _
598 key.Replace( wxS( "-" ), wxS( "_" ) );
599
600 // remove any other chars
601 nonValidCharsRegex.Replace( &key, wxEmptyString );
602
603 if( !m_Env.vars.count( key ) )
604 {
605 wxLogTrace( traceEnvVars, wxS( "COMMON_SETTINGS: Loaded new var: %s = %s" ), key, val );
606 m_Env.vars[key] = ENV_VAR_ITEM( key, val );
607 }
608 }
609
610 if( cfgpath.FileExists() )
611 {
612 wxRemoveFile( cfgpath.GetFullPath() );
613 }
614
615 return true;
616}
617
618
620{
621 // >= 10 = add 1
622 try
623 {
624 // Update netclass panel shown columns for eeschema
625 const nlohmann::json::json_pointer v3_pointer_eeschema( "/netclass_panel/eeschema_shown_columns"_json_pointer );
626 wxString eeSchemaColumnList_old = m_internals->at( v3_pointer_eeschema );
627
628 wxStringTokenizer eeSchemaShownTokens( eeSchemaColumnList_old, " \t\r\n" );
629 wxString eeSchemaColumnList_new;
630
631 while( eeSchemaShownTokens.HasMoreTokens() )
632 {
633 long colNumber;
634 eeSchemaShownTokens.GetNextToken().ToLong( &colNumber );
635
636 if( colNumber >= 10 )
637 ++colNumber;
638
639 eeSchemaColumnList_new += wxString::Format( wxT( "%ld " ), colNumber );
640 }
641
642 eeSchemaColumnList_new.Trim( true );
643 eeSchemaColumnList_new.Trim( false );
644
645 m_internals->at( v3_pointer_eeschema ) = eeSchemaColumnList_new.ToUTF8();
646
647 // Update netclass panel shown columns for pcbnew
648 const nlohmann::json::json_pointer v3_pointer_pcbnew( "/netclass_panel/pcbnew_shown_columns"_json_pointer );
649 wxString pcbnewColumnList_old = m_internals->at( v3_pointer_pcbnew );
650
651 wxStringTokenizer pcbnewShownTokens( pcbnewColumnList_old, " \t\r\n" );
652 wxString pcbnewColumnList_new;
653
654 while( pcbnewShownTokens.HasMoreTokens() )
655 {
656 long colNumber;
657 pcbnewShownTokens.GetNextToken().ToLong( &colNumber );
658
659 if( colNumber >= 10 )
660 ++colNumber;
661
662 pcbnewColumnList_new += wxString::Format( wxT( "%ld " ), colNumber );
663 }
664
665 pcbnewColumnList_new.Trim( true );
666 pcbnewColumnList_new.Trim( false );
667
668 m_internals->at( v3_pointer_pcbnew ) = pcbnewColumnList_new.ToUTF8();
669 }
670 catch( ... )
671 {
672 wxLogTrace( traceSettings, wxT( "COMMON_SETTINGS::Migrate 3->4: /netclass_panel/shown_columns not found" ) );
673 }
674
675 return true;
676}
677
678
680{
681 try
682 {
683 nlohmann::json& controls = m_internals->At( "dialog" ).at( "controls" );
684
685 for( auto& [dlgKey, dlgVal] : controls.items() )
686 {
687 if( !dlgVal.is_object() )
688 continue;
689
690 auto geoIt = dlgVal.find( "__geometry" );
691
692 if( geoIt == dlgVal.end() || !geoIt->is_object() )
693 continue;
694
695 nlohmann::json& geom = *geoIt;
696
697 // Legacy values were stored in logical pixels. Convert to DIP using the
698 // primary display's scale factor (best approximation without window context).
699 int w = geom.value( "w", 0 );
700 int h = geom.value( "h", 0 );
701
702 wxSize dipSize = wxWindow::ToDIP( wxSize( w, h ), nullptr );
703 geom[ "w" ] = dipSize.x;
704 geom[ "h" ] = dipSize.y;
705
706 geom.erase( "dip" );
707 }
708 }
709 catch( ... )
710 {
711 wxLogTrace( traceSettings,
712 wxT( "COMMON_SETTINGS::Migrate 4->5: dialog.controls not found" ) );
713 }
714
715 return true;
716}
717
718
719bool COMMON_SETTINGS::MigrateFromLegacy( wxConfigBase* aCfg )
720{
721 bool ret = true;
722
723 ret &= fromLegacy<double>( aCfg, "CanvasScale", "appearance.canvas_scale" );
724 ret &= fromLegacy<int>( aCfg, "IconScale", "appearance.icon_scale" );
725 ret &= fromLegacy<bool>( aCfg, "UseIconsInMenus", "appearance.use_icons_in_menus" );
726 ret &= fromLegacy<bool>( aCfg, "ShowEnvVarWarningDialog", "environment.show_warning_dialog" );
727
728 auto load_env_vars =
729 [&]()
730 {
731 wxString key, value;
732 long index = 0;
733 nlohmann::json::json_pointer ptr = m_internals->PointerFromString( "environment.vars" );
734
735 aCfg->SetPath( "EnvironmentVariables" );
736 ( *m_internals )[ptr] = nlohmann::json( {} );
737
738 while( aCfg->GetNextEntry( key, index ) )
739 {
740 if( versionedEnvVarRegex.Matches( key ) )
741 {
742 wxLogTrace( traceSettings,
743 wxT( "Migrate Env: %s is blacklisted; skipping." ), key );
744 continue;
745 }
746
747 value = aCfg->Read( key, wxEmptyString );
748
749 if( !value.IsEmpty() )
750 {
751 ptr.push_back( key.ToStdString() );
752
753 wxLogTrace( traceSettings, wxT( "Migrate Env: %s=%s" ),
754 ptr.to_string(), value );
755 ( *m_internals )[ptr] = value.ToUTF8();
756
757 ptr.pop_back();
758 }
759 }
760
761 aCfg->SetPath( ".." );
762 };
763
764 load_env_vars();
765
766 bool mousewheel_pan = false;
767
768 if( aCfg->Read( "MousewheelPAN", &mousewheel_pan ) && mousewheel_pan )
769 {
770 Set( "input.horizontal_pan", true );
771 Set( "input.scroll_modifier_pan_h", static_cast<int>( WXK_SHIFT ) );
772 Set( "input.scroll_modifier_pan_v", 0 );
773 Set( "input.scroll_modifier_zoom", static_cast<int>( WXK_CONTROL ) );
774 }
775
776 ret &= fromLegacy<bool>( aCfg, "AutoPAN", "input.auto_pan" );
777 ret &= fromLegacy<bool>( aCfg, "ImmediateActions", "input.immediate_actions" );
778 ret &= fromLegacy<bool>( aCfg, "PreferSelectionToDragging", "input.prefer_select_to_drag" );
779 ret &= fromLegacy<bool>( aCfg, "MoveWarpsCursor", "input.warp_mouse_on_move" );
780 ret &= fromLegacy<bool>( aCfg, "ZoomNoCenter", "input.center_on_zoom" );
781
782 // This was stored inverted in legacy config
783 if( std::optional<bool> value = Get<bool>( "input.center_on_zoom" ) )
784 Set( "input.center_on_zoom", !( *value ) );
785
786 ret &= fromLegacy<int>( aCfg, "OpenGLAntialiasingMode", "graphics.opengl_antialiasing_mode" );
787 ret &= fromLegacy<int>( aCfg, "CairoAntialiasingMode", "graphics.cairo_antialiasing_mode" );
788
789 ret &= fromLegacy<int>( aCfg, "AutoSaveInterval", "system.local_history_debounce" );
790 ret &= fromLegacyString( aCfg, "Editor", "system.editor_name" );
791 ret &= fromLegacy<int>( aCfg, "FileHistorySize", "system.file_history_size" );
792 ret &= fromLegacyString( aCfg, "LanguageID", "system.language" );
793 ret &= fromLegacyString( aCfg, "PdfBrowserName", "system.pdf_viewer_name" );
794 ret &= fromLegacy<bool>( aCfg, "UseSystemBrowser", "system.use_system_pdf_viewer" );
795 ret &= fromLegacyString( aCfg, "WorkingDir", "system.working_dir" );
796
797 return ret;
798}
799
800
802{
803 auto addVar =
804 [&]( const wxString& aKey, const wxString& aDefault )
805 {
806 m_Env.vars[aKey] = ENV_VAR_ITEM( aKey, aDefault, aDefault );
807
808 wxString envValue;
809
810 if( wxGetEnv( aKey, &envValue ) == true && !envValue.IsEmpty() )
811 {
812 m_Env.vars[aKey].SetValue( envValue );
813 m_Env.vars[aKey].SetDefinedExternally();
814 wxLogTrace( traceEnvVars,
815 wxS( "InitializeEnvironment: Entry %s defined externally as %s" ), aKey,
816 envValue );
817 }
818 else
819 {
820 wxLogTrace( traceEnvVars, wxS( "InitializeEnvironment: Setting entry %s to "
821 "default %s" ),
822 aKey, aDefault );
823 }
824 };
825
826 addVar( ENV_VAR::GetVersionedEnvVarName( wxS( "FOOTPRINT_DIR" ) ), PATHS::GetStockFootprintsPath() );
827 addVar( ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ), PATHS::GetStock3dmodelsPath() );
828 addVar( ENV_VAR::GetVersionedEnvVarName( wxS( "TEMPLATE_DIR" ) ), PATHS::GetStockTemplatesPath() );
829 addVar( wxT( "KICAD_USER_TEMPLATE_DIR" ), PATHS::GetUserTemplatesPath() );
830 addVar( ENV_VAR::GetVersionedEnvVarName( wxS( "3RD_PARTY" ) ), PATHS::GetDefault3rdPartyPath() );
831 addVar( ENV_VAR::GetVersionedEnvVarName( wxS( "SYMBOL_DIR" ) ), PATHS::GetStockSymbolsPath() );
832 addVar( ENV_VAR::GetVersionedEnvVarName( wxS( "DESIGN_BLOCK_DIR" ) ), PATHS::GetStockDesignBlocksPath() );
833}
834
835
837 std::vector<LEGACY_3D_SEARCH_PATH>& aSearchPaths )
838{
839 wxFileName cfgpath( path );
840
841 // This should be the same as wxWidgets 3.0 wxPATH_NORM_ALL which is deprecated in 3.1.
842 // There are known issues with environment variable expansion so maybe we should be using
843 // our own ExpandEnvVarSubstitutions() here instead.
844 cfgpath.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS );
845 wxString cfgname = cfgpath.GetFullPath();
846
847 std::ifstream cfgFile;
848 std::string cfgLine;
849
850 if( !wxFileName::Exists( cfgname ) )
851 {
852 std::ostringstream ostr;
853 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
854 wxString errmsg = "no 3D configuration file";
855 ostr << " * " << errmsg.ToUTF8() << " '";
856 ostr << cfgname.ToUTF8() << "'";
857 wxLogTrace( traceSettings, "%s\n", ostr.str().c_str() );
858 return false;
859 }
860
861 cfgFile.open( cfgname.ToUTF8() );
862
863 if( !cfgFile.is_open() )
864 {
865 std::ostringstream ostr;
866 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
867 wxString errmsg = wxS( "Could not open configuration file" );
868 ostr << " * " << errmsg.ToUTF8() << " '" << cfgname.ToUTF8() << "'";
869 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
870 return false;
871 }
872
873 int lineno = 0;
875 size_t idx;
876 int vnum = 0; // version number
877
878 while( cfgFile.good() )
879 {
880 cfgLine.clear();
881 std::getline( cfgFile, cfgLine );
882 ++lineno;
883
884 if( cfgLine.empty() )
885 {
886 if( cfgFile.eof() )
887 break;
888
889 continue;
890 }
891
892 if( 1 == lineno && cfgLine.compare( 0, 2, "#V" ) == 0 )
893 {
894 // extract the version number and parse accordingly
895 if( cfgLine.size() > 2 )
896 {
897 std::istringstream istr;
898 istr.str( cfgLine.substr( 2 ) );
899 istr >> vnum;
900 }
901
902 continue;
903 }
904
905 idx = 0;
906
907 if( !getLegacy3DHollerith( cfgLine, idx, al.m_Alias ) )
908 continue;
909
910 // Don't add KICADn_3DMODEL_DIR, one of its legacy equivalents, or KIPRJMOD from a
911 // config file. They're system variables which are defined at runtime.
912 wxString versionedPath = wxString::Format( wxS( "${%s}" ),
913 ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ) );
914
915 if( al.m_Alias == versionedPath || al.m_Alias == wxS( "${KIPRJMOD}" )
916 || al.m_Alias == wxS( "$(KIPRJMOD)" ) || al.m_Alias == wxS( "${KISYS3DMOD}" )
917 || al.m_Alias == wxS( "$(KISYS3DMOD)" ) )
918 {
919 continue;
920 }
921
922 if( !getLegacy3DHollerith( cfgLine, idx, al.m_Pathvar ) )
923 continue;
924
925 if( !getLegacy3DHollerith( cfgLine, idx, al.m_Description ) )
926 continue;
927
928 aSearchPaths.push_back( al );
929 }
930
931 cfgFile.close();
932
933 return true;
934}
935
936
937bool COMMON_SETTINGS::getLegacy3DHollerith( const std::string& aString, size_t& aIndex,
938 wxString& aResult )
939{
940 aResult.clear();
941
942 if( aIndex >= aString.size() )
943 {
944 std::ostringstream ostr;
945 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
946 wxString errmsg = wxS( "bad Hollerith string on line" );
947 ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
948 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
949
950 return false;
951 }
952
953 size_t i2 = aString.find( '"', aIndex );
954
955 if( std::string::npos == i2 )
956 {
957 std::ostringstream ostr;
958 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
959 wxString errmsg = wxS( "missing opening quote mark in config file" );
960 ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
961 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
962
963 return false;
964 }
965
966 ++i2;
967
968 if( i2 >= aString.size() )
969 {
970 std::ostringstream ostr;
971 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
972 wxString errmsg = wxS( "invalid entry (unexpected end of line)" );
973 ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
974 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
975
976 return false;
977 }
978
979 std::string tnum;
980
981 while( aString[i2] >= '0' && aString[i2] <= '9' )
982 tnum.append( 1, aString[i2++] );
983
984 if( tnum.empty() || aString[i2++] != ':' )
985 {
986 std::ostringstream ostr;
987 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
988 wxString errmsg = wxS( "bad Hollerith string on line" );
989 ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
990 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
991
992 return false;
993 }
994
995 std::istringstream istr;
996 istr.str( tnum );
997 size_t nchars;
998 istr >> nchars;
999
1000 if( ( i2 + nchars ) >= 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 if( nchars > 0 )
1012 {
1013 aResult = wxString::FromUTF8( aString.substr( i2, nchars ).c_str() );
1014 i2 += nchars;
1015 }
1016
1017 if( i2 >= aString.size() || aString[i2] != '"' )
1018 {
1019 std::ostringstream ostr;
1020 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
1021 wxString errmsg = wxS( "missing closing quote mark in config file" );
1022 ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
1023 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
1024
1025 return false;
1026 }
1027
1028 aIndex = i2 + 1;
1029 return true;
1030}
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...
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
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