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