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