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