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.

254 lines
10 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 <class_draw_panel_gal.h>
  21. #include <common.h>
  22. #include <layers_id_colors_and_visibility.h>
  23. #include <pgm_base.h>
  24. #include <settings/app_settings.h>
  25. #include <settings/common_settings.h>
  26. #include <settings/parameters.h>
  27. ///! Update the schema version whenever a migration is required
  28. const int appSettingsSchemaVersion = 0;
  29. APP_SETTINGS_BASE::APP_SETTINGS_BASE( std::string aFilename, int aSchemaVersion ) :
  30. JSON_SETTINGS( std::move( aFilename ), SETTINGS_LOC::USER, appSettingsSchemaVersion ),
  31. m_Printing(), m_System(), m_Window(), m_appSettingsSchemaVersion( aSchemaVersion )
  32. {
  33. m_params.emplace_back( new PARAM<int>( "find_replace.flags", &m_FindReplace.flags, 1 ) );
  34. m_params.emplace_back( new PARAM<wxString>( "find_replace.find_string",
  35. &m_FindReplace.find_string, "" ) );
  36. m_params.emplace_back( new PARAM_LIST<wxString>( "find_replace.find_history",
  37. &m_FindReplace.find_history, {} ) );
  38. m_params.emplace_back( new PARAM<wxString>( "find_replace.replace_string",
  39. &m_FindReplace.replace_string, "" ) );
  40. m_params.emplace_back( new PARAM_LIST<wxString>( "find_replace.replace_history",
  41. &m_FindReplace.replace_history, {} ) );
  42. #ifdef __WXMAC__
  43. // Cairo renderer doesn't handle Retina displays so default to OpenGL
  44. m_params.emplace_back( new PARAM<int>( "graphics.canvas_type", &m_Graphics.canvas_type,
  45. EDA_DRAW_PANEL_GAL::GAL_TYPE_OPENGL ) );
  46. #else
  47. m_params.emplace_back( new PARAM<int>( "graphics.canvas_type", &m_Graphics.canvas_type,
  48. EDA_DRAW_PANEL_GAL::GAL_TYPE_CAIRO ) );
  49. #endif
  50. m_params.emplace_back(
  51. new PARAM<int>( "lib_tree.column_width", &m_LibTree.column_width, 360 ) );
  52. m_params.emplace_back( new PARAM<bool>( "printing.monochrome", &m_Printing.monochrome, true ) );
  53. m_params.emplace_back( new PARAM<double>( "printing.scale", &m_Printing.scale, 1.0 ) );
  54. m_params.emplace_back( new PARAM<bool>( "printing.title_block",
  55. &m_Printing.title_block, false ) );
  56. m_params.emplace_back( new PARAM_LIST<int>( "printing.layers", &m_Printing.layers, {} ) );
  57. m_params.emplace_back( new PARAM<bool>( "system.first_run_shown",
  58. &m_System.first_run_shown, false ) );
  59. m_params.emplace_back( new PARAM<int>( "system.max_undo_items", &m_System.max_undo_items, 0 ) );
  60. m_params.emplace_back( new PARAM_LIST<wxString>( "system.file_history",
  61. &m_System.file_history, {} ) );
  62. m_params.emplace_back( new PARAM<int>( "system.units", &m_System.units,
  63. static_cast<int>( EDA_UNITS::MILLIMETRES ) ) );
  64. addParamsForWindow( &m_Window, "window" );
  65. }
  66. bool APP_SETTINGS_BASE::MigrateFromLegacy( wxConfigBase* aCfg )
  67. {
  68. bool ret = true;
  69. const std::string f = getLegacyFrameName();
  70. ret &= fromLegacyString( aCfg, "LastFindString", "find_replace.find_string" );
  71. ret &= fromLegacyString( aCfg, "LastReplaceString", "find_replace.replace_string" );
  72. migrateFindReplace( aCfg );
  73. ret &= fromLegacy<int>( aCfg, "canvas_type", "graphics.canvas_type" );
  74. ret &= fromLegacy<int>( aCfg, "P22LIB_TREE_MODEL_ADAPTERSelectorColumnWidth",
  75. "lib_tree.column_width" );
  76. ret &= fromLegacy<bool>( aCfg, "PrintMonochrome", "printing.monochrome" );
  77. ret &= fromLegacy<double>( aCfg, "PrintScale", "printing.scale" );
  78. ret &= fromLegacy<bool>( aCfg, "PrintPageFrame", "printing.title_block" );
  79. {
  80. nlohmann::json js = nlohmann::json::array();
  81. wxString key;
  82. bool val = false;
  83. for( unsigned i = 0; i < PCB_LAYER_ID_COUNT; ++i )
  84. {
  85. key.Printf( wxT( "PlotLayer_%d" ), i );
  86. if( aCfg->Read( key, &val ) && val )
  87. js.push_back( i );
  88. }
  89. ( *this )[PointerFromString( "printing.layers" ) ] = js;
  90. }
  91. ret &= fromLegacy<bool>( aCfg, f + "FirstRunShown", "system.first_run_shown" );
  92. ret &= fromLegacy<int>( aCfg, f + "DevelMaxUndoItems", "system.max_undo_items" );
  93. ret &= fromLegacy<int>( aCfg, f + "Units", "system.units" );
  94. {
  95. int max_history_size = Pgm().GetCommonSettings()->m_System.file_history_size;
  96. wxString file, key;
  97. nlohmann::json js = nlohmann::json::array();
  98. for( int i = 1; i <= max_history_size; i++ )
  99. {
  100. key.Printf( "file%d", i );
  101. file = aCfg->Read( key, wxEmptyString );
  102. if( !file.IsEmpty() )
  103. js.push_back( file.ToStdString() );
  104. }
  105. ( *this )[PointerFromString( "system.file_history" )] = js;
  106. }
  107. ret &= migrateWindowConfig( aCfg, f, "window" );
  108. return ret;
  109. }
  110. void APP_SETTINGS_BASE::migrateFindReplace( wxConfigBase* aCfg )
  111. {
  112. const int find_replace_history_size = 10;
  113. nlohmann::json find_history = nlohmann::json::array();
  114. nlohmann::json replace_history = nlohmann::json::array();
  115. wxString tmp, find_key, replace_key;
  116. for( int i = 0; i < find_replace_history_size; ++i )
  117. {
  118. find_key.Printf( "FindStringHistoryList%d", i );
  119. replace_key.Printf( "ReplaceStringHistoryList%d", i );
  120. if( aCfg->Read( find_key, &tmp ) )
  121. find_history.push_back( tmp.ToStdString() );
  122. if( aCfg->Read( replace_key, &tmp ) )
  123. replace_history.push_back( tmp.ToStdString() );
  124. }
  125. ( *this )[PointerFromString( "find_replace.find_history" )] = find_history;
  126. ( *this )[PointerFromString( "find_replace.replace_history" )] = replace_history;
  127. }
  128. bool APP_SETTINGS_BASE::migrateWindowConfig( wxConfigBase* aCfg, const std::string& aFrame,
  129. const std::string& aJsonPath )
  130. {
  131. bool ret = true;
  132. const std::string gd = "GalDisplayOptions";
  133. ret &= fromLegacy<bool>( aCfg, aFrame + "Maximized", aJsonPath + ".maximized" );
  134. ret &= fromLegacyString( aCfg, aFrame + "MostRecentlyUsedPath", aJsonPath + ".mru_path" );
  135. ret &= fromLegacy<int>( aCfg, aFrame + "Size_x", aJsonPath + ".size_x" );
  136. ret &= fromLegacy<int>( aCfg, aFrame + "Size_y", aJsonPath + ".size_y" );
  137. ret &= fromLegacyString( aCfg, aFrame + "Perspective", aJsonPath + ".perspective" );
  138. ret &= fromLegacy<int>( aCfg, aFrame + "Pos_x", aJsonPath + ".pos_x" );
  139. ret &= fromLegacy<int>( aCfg, aFrame + "Pos_y", aJsonPath + ".pos_y" );
  140. ret &= fromLegacy<bool>( aCfg,
  141. aFrame + gd + "ForceDisplayCursor", aJsonPath + ".cursor.always_show_cursor" );
  142. ret &= fromLegacy<bool>( aCfg,
  143. aFrame + gd + "CursorFullscreen", aJsonPath + ".cursor.fullscreen_cursor" );
  144. ret &= fromLegacy<int>( aCfg,
  145. aFrame + "_LastGridSize", aJsonPath + ".grid.last_size" );
  146. ret &= fromLegacy<bool>( aCfg,
  147. aFrame + gd + "GridAxesEnabled", aJsonPath + ".grid.axes_enabled" );
  148. ret &= fromLegacy<double>( aCfg,
  149. aFrame + gd + "GridLineWidth", aJsonPath + ".grid.line_width" );
  150. ret &= fromLegacy<double>( aCfg,
  151. aFrame + gd + "GridMaxDensity", aJsonPath + ".grid.min_spacing" );
  152. ret &= fromLegacy<bool>( aCfg, aFrame + gd + "ShowGrid", aJsonPath + ".grid.show" );
  153. ret &= fromLegacy<int>( aCfg, aFrame + gd + "GridStyle", aJsonPath + ".grid.style" );
  154. ret &= fromLegacyColor( aCfg, aFrame + gd + "GridColor", aJsonPath + ".grid.color" );
  155. ret &= fromLegacy<bool>( aCfg, aFrame + "AutoZoom", aJsonPath + ".auto_zoom" );
  156. ret &= fromLegacy<double>( aCfg, aFrame + "Zoom", aJsonPath + ".zoom" );
  157. return ret;
  158. }
  159. void APP_SETTINGS_BASE::addParamsForWindow( WINDOW_SETTINGS* aWindow, const std::string& aJsonPath )
  160. {
  161. m_params.emplace_back(
  162. new PARAM<bool>( aJsonPath + ".maximized", &aWindow->maximized, false ) );
  163. m_params.emplace_back( new PARAM<wxString>( aJsonPath + ".mru_path", &aWindow->mru_path, "" ) );
  164. m_params.emplace_back( new PARAM<int>( aJsonPath + ".size_x", &aWindow->size_x, 0 ) );
  165. m_params.emplace_back( new PARAM<int>( aJsonPath + ".size_y", &aWindow->size_y, 0 ) );
  166. m_params.emplace_back(
  167. new PARAM<wxString>( aJsonPath + ".perspective", &aWindow->perspective, "" ) );
  168. m_params.emplace_back( new PARAM<int>( aJsonPath + ".pos_x", &aWindow->pos_x, 0 ) );
  169. m_params.emplace_back( new PARAM<int>( aJsonPath + ".pos_y", &aWindow->pos_y, 0 ) );
  170. m_params.emplace_back( new PARAM<bool>( aJsonPath + ".grid.axes_enabled",
  171. &aWindow->grid.axes_enabled, false ) );
  172. m_params.emplace_back(
  173. new PARAM<int>( aJsonPath + ".grid.last_size", &aWindow->grid.last_size, 0 ) );
  174. m_params.emplace_back(
  175. new PARAM<double>( aJsonPath + ".grid.line_width", &aWindow->grid.line_width, 1.0 ) );
  176. m_params.emplace_back(
  177. new PARAM<double>( aJsonPath + ".grid.min_spacing", &aWindow->grid.min_spacing, 10 ) );
  178. m_params.emplace_back( new PARAM<bool>( aJsonPath + ".grid.show", &aWindow->grid.show, true ) );
  179. m_params.emplace_back( new PARAM<int>( aJsonPath + ".grid.style", &aWindow->grid.style, 0 ) );
  180. m_params.emplace_back( new PARAM<bool>( aJsonPath + ".cursor.always_show_cursor",
  181. &aWindow->cursor.always_show_cursor, true ) );
  182. m_params.emplace_back( new PARAM<bool>( aJsonPath + ".cursor.fullscreen_cursor",
  183. &aWindow->cursor.fullscreen_cursor, false ) );
  184. }