You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

324 lines
12 KiB

  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. #include <settings/common_settings.h>
  21. #include <settings/parameters.h>
  22. #include <view/view_controls.h>
  23. #include <view/zoom_controller.h>
  24. #include <wx/config.h>
  25. #include <wx/log.h>
  26. using KIGFX::MOUSE_DRAG_ACTION;
  27. extern const char* traceSettings;
  28. ///! Update the schema version whenever a migration is required
  29. const int commonSchemaVersion = 1;
  30. COMMON_SETTINGS::COMMON_SETTINGS() :
  31. JSON_SETTINGS( "kicad_common", SETTINGS_LOC::USER, commonSchemaVersion ),
  32. m_Appearance(),
  33. m_Backup(),
  34. m_Env(),
  35. m_Input(),
  36. m_Graphics(),
  37. m_System()
  38. {
  39. // This only effect the first time KiCad is run. The user's setting will be used for all
  40. // subsequent runs.
  41. // Menu icons are off by default on OSX and on for all other platforms.
  42. // Use automatic canvas scaling on OSX, but not on the other platforms (their detection isn't as good).
  43. #if defined( __WXMAC__ )
  44. bool defaultUseIconsInMenus = false;
  45. double canvasScale = 0.0;
  46. #else
  47. bool defaultUseIconsInMenus = true;
  48. double canvasScale = 1.0;
  49. #endif
  50. m_params.emplace_back( new PARAM<double>( "appearance.canvas_scale",
  51. &m_Appearance.canvas_scale, canvasScale ) );
  52. m_params.emplace_back( new PARAM<int>( "appearance.icon_scale",
  53. &m_Appearance.icon_scale, 0 ) );
  54. m_params.emplace_back( new PARAM<bool>( "appearance.use_icons_in_menus",
  55. &m_Appearance.use_icons_in_menus, defaultUseIconsInMenus ) );
  56. m_params.emplace_back( new PARAM<bool>( "auto_backup.enabled", &m_Backup.enabled, true ) );
  57. m_params.emplace_back( new PARAM<bool>(
  58. "auto_backup.backup_on_autosave", &m_Backup.backup_on_autosave, false ) );
  59. m_params.emplace_back(
  60. new PARAM<int>( "auto_backup.limit_total_files", &m_Backup.limit_total_files, 25 ) );
  61. m_params.emplace_back( new PARAM<unsigned long long>(
  62. "auto_backup.limit_total_size", &m_Backup.limit_total_size, 104857600 ) );
  63. m_params.emplace_back(
  64. new PARAM<int>( "auto_backup.limit_daily_files", &m_Backup.limit_daily_files, 5 ) );
  65. m_params.emplace_back(
  66. new PARAM<int>( "auto_backup.min_interval", &m_Backup.min_interval, 300 ) );
  67. m_params.emplace_back( new PARAM<bool>( "environment.show_warning_dialog",
  68. &m_Env.show_warning_dialog, false ) );
  69. m_params.emplace_back( new PARAM_MAP<wxString>( "environment.vars", &m_Env.vars, {} ) );
  70. m_params.emplace_back( new PARAM<bool>( "input.auto_pan", &m_Input.auto_pan, false ) );
  71. m_params.emplace_back(
  72. new PARAM<int>( "input.auto_pan_acceleration", &m_Input.auto_pan_acceleration, 5 ) );
  73. m_params.emplace_back(
  74. new PARAM<bool>( "input.center_on_zoom", &m_Input.center_on_zoom, true ) );
  75. m_params.emplace_back( new PARAM<bool>( "input.immediate_actions",
  76. &m_Input.immediate_actions, true ) );
  77. m_params.emplace_back( new PARAM<bool>( "input.prefer_select_to_drag",
  78. &m_Input.prefer_select_to_drag, true ) );
  79. m_params.emplace_back( new PARAM<bool>( "input.warp_mouse_on_move",
  80. &m_Input.warp_mouse_on_move, true ) );
  81. m_params.emplace_back(
  82. new PARAM<bool>( "input.horizontal_pan", &m_Input.horizontal_pan, false ) );
  83. #if defined(__WXMAC__) || defined(__WXGTK3__)
  84. bool default_zoom_acceleration = false;
  85. #else
  86. bool default_zoom_acceleration = true;
  87. #endif
  88. m_params.emplace_back( new PARAM<bool>(
  89. "input.zoom_acceleration", &m_Input.zoom_acceleration, default_zoom_acceleration ) );
  90. #ifdef __WXMAC__
  91. int default_zoom_speed = 5;
  92. #else
  93. int default_zoom_speed = 1;
  94. #endif
  95. m_params.emplace_back(
  96. new PARAM<int>( "input.zoom_speed", &m_Input.zoom_speed, default_zoom_speed ) );
  97. m_params.emplace_back(
  98. new PARAM<bool>( "input.zoom_speed_auto", &m_Input.zoom_speed_auto, true ) );
  99. m_params.emplace_back(
  100. new PARAM<int>( "input.scroll_modifier_zoom", &m_Input.scroll_modifier_zoom, 0 ) );
  101. m_params.emplace_back( new PARAM<int>(
  102. "input.scroll_modifier_pan_h", &m_Input.scroll_modifier_pan_h, WXK_CONTROL ) );
  103. m_params.emplace_back( new PARAM<int>(
  104. "input.scroll_modifier_pan_v", &m_Input.scroll_modifier_pan_v, WXK_SHIFT ) );
  105. m_params.emplace_back( new PARAM<int>( "input.mouse_middle", &m_Input.drag_middle,
  106. static_cast<int>( MOUSE_DRAG_ACTION::PAN ),
  107. static_cast<int>( MOUSE_DRAG_ACTION::SELECT ),
  108. static_cast<int>( MOUSE_DRAG_ACTION::NONE ) ) );
  109. m_params.emplace_back( new PARAM<int>( "input.mouse_right", &m_Input.drag_right,
  110. static_cast<int>( MOUSE_DRAG_ACTION::PAN ),
  111. static_cast<int>( MOUSE_DRAG_ACTION::SELECT ),
  112. static_cast<int>( MOUSE_DRAG_ACTION::NONE ) ) );
  113. m_params.emplace_back( new PARAM<int>( "graphics.opengl_antialiasing_mode",
  114. &m_Graphics.opengl_aa_mode, 0, 0, 4 ) );
  115. m_params.emplace_back( new PARAM<int>( "graphics.cairo_antialiasing_mode",
  116. &m_Graphics.cairo_aa_mode, 0, 0, 3 ) );
  117. m_params.emplace_back( new PARAM<int>( "system.autosave_interval",
  118. &m_System.autosave_interval, 600 ) );
  119. m_params.emplace_back( new PARAM<wxString>( "system.editor_name",
  120. &m_System.editor_name, "" ) );
  121. m_params.emplace_back( new PARAM<int>( "system.file_history_size",
  122. &m_System.file_history_size, 9 ) );
  123. m_params.emplace_back( new PARAM<wxString>( "system.language",
  124. &m_System.language, "Default" ) );
  125. m_params.emplace_back( new PARAM<wxString>( "system.pdf_viewer_name",
  126. &m_System.pdf_viewer_name, "" ) );
  127. m_params.emplace_back( new PARAM<bool>( "system.use_system_pdf_viewer",
  128. &m_System.use_system_pdf_viewer, true ) );
  129. m_params.emplace_back( new PARAM<wxString>( "system.working_dir",
  130. &m_System.working_dir, "" ) );
  131. m_params.emplace_back( new PARAM<int>( "system.clear_3d_cache_interval",
  132. &m_System.clear_3d_cache_interval, 30 ) );
  133. }
  134. bool COMMON_SETTINGS::Migrate()
  135. {
  136. bool ret = true;
  137. int filever = at( PointerFromString( "meta.version" ) ).get<int>();
  138. if( filever == 0 )
  139. {
  140. ret &= migrateSchema0to1();
  141. if( ret )
  142. {
  143. ( *this )[PointerFromString( "meta.version" )] = 1;
  144. }
  145. }
  146. return ret;
  147. }
  148. bool COMMON_SETTINGS::migrateSchema0to1()
  149. {
  150. /**
  151. * Schema version 0 to 1:
  152. *
  153. * mousewheel_pan is replaced by explicit settings for scroll wheel behavior
  154. */
  155. nlohmann::json::json_pointer mwp_pointer( "/input/mousewheel_pan"_json_pointer );
  156. bool mwp = false;
  157. try
  158. {
  159. mwp = at( mwp_pointer );
  160. at( nlohmann::json::json_pointer( "/input"_json_pointer ) ).erase( "mousewheel_pan" );
  161. }
  162. catch( ... )
  163. {
  164. wxLogTrace( traceSettings, "COMMON_SETTINGS::Migrate 0->1: mousewheel_pan not found" );
  165. }
  166. if( mwp )
  167. {
  168. ( *this )[nlohmann::json::json_pointer( "/input/horizontal_pan" )] = true;
  169. ( *this )[nlohmann::json::json_pointer( "/input/scroll_modifier_pan_h" )] = WXK_SHIFT;
  170. ( *this )[nlohmann::json::json_pointer( "/input/scroll_modifier_pan_v" )] = 0;
  171. ( *this )[nlohmann::json::json_pointer( "/input/scroll_modifier_zoom" )] = WXK_CONTROL;
  172. }
  173. else
  174. {
  175. ( *this )[nlohmann::json::json_pointer( "/input/horizontal_pan" )] = false;
  176. ( *this )[nlohmann::json::json_pointer( "/input/scroll_modifier_pan_h" )] = WXK_CONTROL;
  177. ( *this )[nlohmann::json::json_pointer( "/input/scroll_modifier_pan_v" )] = WXK_SHIFT;
  178. ( *this )[nlohmann::json::json_pointer( "/input/scroll_modifier_zoom" )] = 0;
  179. }
  180. return true;
  181. }
  182. bool COMMON_SETTINGS::MigrateFromLegacy( wxConfigBase* aCfg )
  183. {
  184. bool ret = true;
  185. ret &= fromLegacy<double>( aCfg, "CanvasScale", "appearance.canvas_scale" );
  186. ret &= fromLegacy<int>( aCfg, "IconScale", "appearance.icon_scale" );
  187. ret &= fromLegacy<bool>( aCfg, "UseIconsInMenus", "appearance.use_icons_in_menus" );
  188. // Force OSX to automatically scale the canvas. Before v6, the user setting wasn't used on OSX and was
  189. // set to 1.0. In v6, the setting is now used by OSX and should default to automatic scaling.
  190. #ifdef __WXMAC__
  191. ( *this )[PointerFromString( "appearance.canvas_scale" )] = 0.0;
  192. #endif
  193. ret &= fromLegacy<bool>( aCfg, "ShowEnvVarWarningDialog", "environment.show_warning_dialog" );
  194. auto load_env_vars = [&] () {
  195. wxString key, value;
  196. long index = 0;
  197. nlohmann::json::json_pointer ptr = PointerFromString( "environment.vars" );
  198. aCfg->SetPath( "EnvironmentVariables" );
  199. ( *this )[ptr] = nlohmann::json( {} );
  200. while( aCfg->GetNextEntry( key, index ) )
  201. {
  202. value = aCfg->Read( key, wxEmptyString );
  203. if( !value.IsEmpty() )
  204. {
  205. ptr.push_back( key.ToStdString() );
  206. wxLogTrace( traceSettings, "Migrate Env: %s=%s", ptr.to_string(), value );
  207. ( *this )[ptr] = value.ToUTF8();
  208. ptr.pop_back();
  209. }
  210. }
  211. aCfg->SetPath( ".." );
  212. };
  213. load_env_vars();
  214. bool mousewheel_pan = false;
  215. if( aCfg->Read( "MousewheelPAN", &mousewheel_pan ) && mousewheel_pan )
  216. {
  217. ( *this )[PointerFromString( "input.horizontal_pan" )] = true;
  218. ( *this )[PointerFromString( "input.scroll_modifier_pan_h" )] = WXK_SHIFT;
  219. ( *this )[PointerFromString( "input.scroll_modifier_pan_v" )] = 0;
  220. ( *this )[PointerFromString( "input.scroll_modifier_zoom" )] = WXK_CONTROL;
  221. }
  222. ret &= fromLegacy<bool>( aCfg, "AutoPAN", "input.auto_pan" );
  223. ret &= fromLegacy<bool>( aCfg, "ImmediateActions", "input.immediate_actions" );
  224. ret &= fromLegacy<bool>( aCfg, "PreferSelectionToDragging", "input.prefer_select_to_drag" );
  225. ret &= fromLegacy<bool>( aCfg, "MoveWarpsCursor", "input.warp_mouse_on_move" );
  226. ret &= fromLegacy<bool>( aCfg, "ZoomNoCenter", "input.center_on_zoom" );
  227. // This was stored inverted in legacy config
  228. if( ret )
  229. {
  230. auto p = PointerFromString( "input.center_on_zoom" );
  231. ( *this )[p] = !( *this )[p];
  232. }
  233. ret &= fromLegacy<int>( aCfg, "OpenGLAntialiasingMode", "graphics.opengl_antialiasing_mode" );
  234. ret &= fromLegacy<int>( aCfg, "CairoAntialiasingMode", "graphics.cairo_antialiasing_mode" );
  235. ret &= fromLegacy<int>( aCfg, "AutoSaveInterval", "system.autosave_interval" );
  236. ret &= fromLegacyString( aCfg, "Editor", "system.editor_name" );
  237. ret &= fromLegacy<int>( aCfg, "FileHistorySize", "system.file_history_size" );
  238. ret &= fromLegacyString( aCfg, "LanguageID", "system.language" );
  239. ret &= fromLegacyString( aCfg, "PdfBrowserName", "system.pdf_viewer_name" );
  240. ret &= fromLegacy<bool>( aCfg, "UseSystemBrowser", "system.use_system_pdf_viewer" );
  241. ret &= fromLegacyString( aCfg, "WorkingDir", "system.working_dir" );
  242. return ret;
  243. }