KiCad PCB EDA Suite
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 <jon@craftyjon.com>
5  * Copyright (C) 2020 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 
22 #include <settings/parameters.h>
23 #include <view/view_controls.h>
24 #include <view/zoom_controller.h>
25 #include <wx/config.h>
26 #include <wx/log.h>
27 
29 
30 
32 const int commonSchemaVersion = 1;
33 
36  m_Appearance(),
37  m_Backup(),
38  m_Env(),
39  m_Input(),
40  m_Graphics(),
41  m_Session(),
42  m_System()
43 {
44  // This only effect the first time KiCad is run. The user's setting will be used for all
45  // subsequent runs.
46  // Menu icons are off by default on OSX and on for all other platforms.
47  // Use automatic canvas scaling on OSX, but not on the other platforms (their detection isn't as good).
48 #if defined( __WXMAC__ )
49  bool defaultUseIconsInMenus = false;
50  double canvasScale = 0.0;
51 #else
52  bool defaultUseIconsInMenus = true;
53  double canvasScale = 1.0;
54 #endif
55 
56  m_params.emplace_back( new PARAM<double>( "appearance.canvas_scale",
57  &m_Appearance.canvas_scale, canvasScale ) );
58 
59  m_params.emplace_back( new PARAM<int>( "appearance.icon_scale",
60  &m_Appearance.icon_scale, 0 ) );
61 
62  m_params.emplace_back( new PARAM<bool>( "appearance.use_icons_in_menus",
63  &m_Appearance.use_icons_in_menus, defaultUseIconsInMenus ) );
64 
65  m_params.emplace_back( new PARAM<bool>( "auto_backup.enabled", &m_Backup.enabled, true ) );
66 
67  m_params.emplace_back( new PARAM<bool>(
68  "auto_backup.backup_on_autosave", &m_Backup.backup_on_autosave, false ) );
69 
70  m_params.emplace_back(
71  new PARAM<int>( "auto_backup.limit_total_files", &m_Backup.limit_total_files, 25 ) );
72 
73  m_params.emplace_back( new PARAM<unsigned long long>(
74  "auto_backup.limit_total_size", &m_Backup.limit_total_size, 104857600 ) );
75 
76  m_params.emplace_back(
77  new PARAM<int>( "auto_backup.limit_daily_files", &m_Backup.limit_daily_files, 5 ) );
78 
79  m_params.emplace_back(
80  new PARAM<int>( "auto_backup.min_interval", &m_Backup.min_interval, 300 ) );
81 
82  m_params.emplace_back( new PARAM<bool>( "environment.show_warning_dialog",
83  &m_Env.show_warning_dialog, false ) );
84 
85  m_params.emplace_back( new PARAM_MAP<wxString>( "environment.vars", &m_Env.vars, {} ) );
86 
87  m_params.emplace_back( new PARAM<bool>( "input.auto_pan", &m_Input.auto_pan, false ) );
88 
89  m_params.emplace_back(
90  new PARAM<int>( "input.auto_pan_acceleration", &m_Input.auto_pan_acceleration, 5 ) );
91 
92  m_params.emplace_back(
93  new PARAM<bool>( "input.center_on_zoom", &m_Input.center_on_zoom, true ) );
94 
95  m_params.emplace_back( new PARAM<bool>( "input.immediate_actions",
96  &m_Input.immediate_actions, true ) );
97 
98  m_params.emplace_back( new PARAM<bool>( "input.prefer_select_to_drag",
99  &m_Input.prefer_select_to_drag, false ) );
100 
101  m_params.emplace_back( new PARAM<bool>( "input.warp_mouse_on_move",
102  &m_Input.warp_mouse_on_move, true ) );
103 
104  m_params.emplace_back(
105  new PARAM<bool>( "input.horizontal_pan", &m_Input.horizontal_pan, false ) );
106 
107 #if defined(__WXMAC__) || defined(__WXGTK3__)
108  bool default_zoom_acceleration = false;
109 #else
110  bool default_zoom_acceleration = true;
111 #endif
112 
113  m_params.emplace_back( new PARAM<bool>(
114  "input.zoom_acceleration", &m_Input.zoom_acceleration, default_zoom_acceleration ) );
115 
116 #ifdef __WXMAC__
117  int default_zoom_speed = 5;
118 #else
119  int default_zoom_speed = 1;
120 #endif
121 
122  m_params.emplace_back(
123  new PARAM<int>( "input.zoom_speed", &m_Input.zoom_speed, default_zoom_speed ) );
124 
125  m_params.emplace_back(
126  new PARAM<bool>( "input.zoom_speed_auto", &m_Input.zoom_speed_auto, true ) );
127 
128  m_params.emplace_back(
129  new PARAM<int>( "input.scroll_modifier_zoom", &m_Input.scroll_modifier_zoom, 0 ) );
130 
131  m_params.emplace_back( new PARAM<int>(
132  "input.scroll_modifier_pan_h", &m_Input.scroll_modifier_pan_h, WXK_CONTROL ) );
133 
134  m_params.emplace_back( new PARAM<int>(
135  "input.scroll_modifier_pan_v", &m_Input.scroll_modifier_pan_v, WXK_SHIFT ) );
136 
137  m_params.emplace_back( new PARAM<int>( "input.mouse_middle", &m_Input.drag_middle,
138  static_cast<int>( MOUSE_DRAG_ACTION::PAN ),
139  static_cast<int>( MOUSE_DRAG_ACTION::SELECT ),
140  static_cast<int>( MOUSE_DRAG_ACTION::NONE ) ) );
141 
142  m_params.emplace_back( new PARAM<int>( "input.mouse_right", &m_Input.drag_right,
143  static_cast<int>( MOUSE_DRAG_ACTION::PAN ),
144  static_cast<int>( MOUSE_DRAG_ACTION::SELECT ),
145  static_cast<int>( MOUSE_DRAG_ACTION::NONE ) ) );
146 
147  m_params.emplace_back( new PARAM<int>( "graphics.opengl_antialiasing_mode",
148  &m_Graphics.opengl_aa_mode, 0, 0, 4 ) );
149 
150  m_params.emplace_back( new PARAM<int>( "graphics.cairo_antialiasing_mode",
151  &m_Graphics.cairo_aa_mode, 0, 0, 3 ) );
152 
153  m_params.emplace_back( new PARAM<int>( "system.autosave_interval",
154  &m_System.autosave_interval, 600 ) );
155 
156  m_params.emplace_back( new PARAM<wxString>( "system.editor_name",
157  &m_System.editor_name, "" ) );
158 
159  m_params.emplace_back( new PARAM<int>( "system.file_history_size",
160  &m_System.file_history_size, 9 ) );
161 
162  m_params.emplace_back( new PARAM<wxString>( "system.language",
163  &m_System.language, "Default" ) );
164 
165  m_params.emplace_back( new PARAM<wxString>( "system.pdf_viewer_name",
166  &m_System.pdf_viewer_name, "" ) );
167 
168  m_params.emplace_back( new PARAM<bool>( "system.use_system_pdf_viewer",
169  &m_System.use_system_pdf_viewer, true ) );
170 
171  m_params.emplace_back( new PARAM<wxString>( "system.working_dir",
172  &m_System.working_dir, "" ) );
173 
174  m_params.emplace_back( new PARAM<int>( "system.clear_3d_cache_interval",
176 
177  m_params.emplace_back( new PARAM<bool>( "session.remember_open_files",
178  &m_Session.remember_open_files, false ) );
179 
180  registerMigration( 0, 1, std::bind( &COMMON_SETTINGS::migrateSchema0to1, this ) );
181 }
182 
183 
185 {
192  nlohmann::json::json_pointer mwp_pointer( "/input/mousewheel_pan"_json_pointer );
193 
194  bool mwp = false;
195 
196  try
197  {
198  mwp = at( mwp_pointer );
199  at( nlohmann::json::json_pointer( "/input"_json_pointer ) ).erase( "mousewheel_pan" );
200  }
201  catch( ... )
202  {
203  wxLogTrace( traceSettings, "COMMON_SETTINGS::Migrate 0->1: mousewheel_pan not found" );
204  }
205 
206  if( mwp )
207  {
208  ( *this )[nlohmann::json::json_pointer( "/input/horizontal_pan" )] = true;
209 
210  ( *this )[nlohmann::json::json_pointer( "/input/scroll_modifier_pan_h" )] = WXK_SHIFT;
211  ( *this )[nlohmann::json::json_pointer( "/input/scroll_modifier_pan_v" )] = 0;
212  ( *this )[nlohmann::json::json_pointer( "/input/scroll_modifier_zoom" )] = WXK_CONTROL;
213  }
214  else
215  {
216  ( *this )[nlohmann::json::json_pointer( "/input/horizontal_pan" )] = false;
217 
218  ( *this )[nlohmann::json::json_pointer( "/input/scroll_modifier_pan_h" )] = WXK_CONTROL;
219  ( *this )[nlohmann::json::json_pointer( "/input/scroll_modifier_pan_v" )] = WXK_SHIFT;
220  ( *this )[nlohmann::json::json_pointer( "/input/scroll_modifier_zoom" )] = 0;
221  }
222 
223  return true;
224 }
225 
226 
227 bool COMMON_SETTINGS::MigrateFromLegacy( wxConfigBase* aCfg )
228 {
229  bool ret = true;
230 
231  ret &= fromLegacy<double>( aCfg, "CanvasScale", "appearance.canvas_scale" );
232  ret &= fromLegacy<int>( aCfg, "IconScale", "appearance.icon_scale" );
233  ret &= fromLegacy<bool>( aCfg, "UseIconsInMenus", "appearance.use_icons_in_menus" );
234 
235 // Force OSX to automatically scale the canvas. Before v6, the user setting wasn't used on OSX and was
236 // set to 1.0. In v6, the setting is now used by OSX and should default to automatic scaling.
237 #ifdef __WXMAC__
238  ( *this )[PointerFromString( "appearance.canvas_scale" )] = 0.0;
239 #endif
240 
241  ret &= fromLegacy<bool>( aCfg, "ShowEnvVarWarningDialog", "environment.show_warning_dialog" );
242 
243  auto load_env_vars = [&] () {
244  wxString key, value;
245  long index = 0;
246  nlohmann::json::json_pointer ptr = PointerFromString( "environment.vars" );
247 
248  aCfg->SetPath( "EnvironmentVariables" );
249  ( *this )[ptr] = nlohmann::json( {} );
250 
251  while( aCfg->GetNextEntry( key, index ) )
252  {
253  value = aCfg->Read( key, wxEmptyString );
254 
255  if( !value.IsEmpty() )
256  {
257  ptr.push_back( key.ToStdString() );
258 
259  wxLogTrace( traceSettings, "Migrate Env: %s=%s", ptr.to_string(), value );
260  ( *this )[ptr] = value.ToUTF8();
261 
262  ptr.pop_back();
263  }
264  }
265 
266  aCfg->SetPath( ".." );
267  };
268 
269  load_env_vars();
270 
271  bool mousewheel_pan = false;
272 
273  if( aCfg->Read( "MousewheelPAN", &mousewheel_pan ) && mousewheel_pan )
274  {
275  ( *this )[PointerFromString( "input.horizontal_pan" )] = true;
276 
277  ( *this )[PointerFromString( "input.scroll_modifier_pan_h" )] = WXK_SHIFT;
278  ( *this )[PointerFromString( "input.scroll_modifier_pan_v" )] = 0;
279  ( *this )[PointerFromString( "input.scroll_modifier_zoom" )] = WXK_CONTROL;
280  }
281 
282  ret &= fromLegacy<bool>( aCfg, "AutoPAN", "input.auto_pan" );
283  ret &= fromLegacy<bool>( aCfg, "ImmediateActions", "input.immediate_actions" );
284  ret &= fromLegacy<bool>( aCfg, "PreferSelectionToDragging", "input.prefer_select_to_drag" );
285  ret &= fromLegacy<bool>( aCfg, "MoveWarpsCursor", "input.warp_mouse_on_move" );
286  ret &= fromLegacy<bool>( aCfg, "ZoomNoCenter", "input.center_on_zoom" );
287 
288  // This was stored inverted in legacy config
289  if( ret )
290  {
291  auto p = PointerFromString( "input.center_on_zoom" );
292  ( *this )[p] = !( *this )[p];
293  }
294 
295  ret &= fromLegacy<int>( aCfg, "OpenGLAntialiasingMode", "graphics.opengl_antialiasing_mode" );
296  ret &= fromLegacy<int>( aCfg, "CairoAntialiasingMode", "graphics.cairo_antialiasing_mode" );
297 
298  ret &= fromLegacy<int>( aCfg, "AutoSaveInterval", "system.autosave_interval" );
299  ret &= fromLegacyString( aCfg, "Editor", "system.editor_name" );
300  ret &= fromLegacy<int>( aCfg, "FileHistorySize", "system.file_history_size" );
301  ret &= fromLegacyString( aCfg, "LanguageID", "system.language" );
302  ret &= fromLegacyString( aCfg, "PdfBrowserName", "system.pdf_viewer_name" );
303  ret &= fromLegacy<bool>( aCfg, "UseSystemBrowser", "system.use_system_pdf_viewer" );
304  ret &= fromLegacyString( aCfg, "WorkingDir", "system.working_dir" );
305 
306  return ret;
307 }
std::vector< PARAM_BASE * > m_params
The list of parameters (owned by this object)
MOUSE_DRAG_ACTION
Action to perform when the mouse is dragged
Definition: view_controls.h:45
unsigned long long limit_total_size
Maximum total size of backups (bytes), 0 for unlimited.
VIEW_CONTROLS class definition.
Represents a map of <std::string, Value>.
Definition: parameters.h:643
const int commonSchemaVersion
! Update the schema version whenever a migration is required
bool enabled
Automatically back up the project when files are saved.
SETTINGS_LOC
Definition: json_settings.h:44
AUTO_BACKUP m_Backup
APPEARANCE m_Appearance
virtual bool MigrateFromLegacy(wxConfigBase *aLegacyConfig) override
Migrates from wxConfig to JSON-based configuration.
nlohmann::json json
Definition: gerbview.cpp:39
std::map< std::string, wxString > vars
ENVIRONMENT m_Env
int min_interval
Minimum time, in seconds, between subsequent backups.
bool fromLegacyString(wxConfigBase *aConfig, const std::string &aKey, const std::string &aDest)
Translates a legacy wxConfig string value to a given JSON pointer value.
void registerMigration(int aOldSchemaVersion, int aNewSchemaVersion, std::function< bool(void)> aMigrator)
Registers a migration from one schema version to another.
The main config directory (e.g. ~/.config/kicad/)
bool backup_on_autosave
Trigger a backup on autosave.
int limit_daily_files
Maximum files to keep per day, 0 for unlimited.
const wxChar *const traceSettings
Flag to enable debug output of settings operations and management.
static nlohmann::json::json_pointer PointerFromString(std::string aPath)
Builds a JSON pointer based on a given string.
int limit_total_files
Maximum number of backup archives to retain.