KiCad PCB EDA Suite
app_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 
21 #include <class_draw_panel_gal.h>
22 #include <common.h>
24 #include <pgm_base.h>
25 #include <settings/app_settings.h>
27 #include <settings/parameters.h>
28 
29 
32 
33 
34 APP_SETTINGS_BASE::APP_SETTINGS_BASE( std::string aFilename, int aSchemaVersion ) :
35  JSON_SETTINGS( std::move( aFilename ), SETTINGS_LOC::USER, aSchemaVersion ),
36  m_Printing(), m_System(), m_Window(), m_appSettingsSchemaVersion( aSchemaVersion )
37 {
38  // Make Coverity happy:
39  m_LibTree.column_width = 360;
41 
42  // Build parameters list:
43  m_params.emplace_back( new PARAM<int>( "find_replace.flags", &m_FindReplace.flags, 1 ) );
44 
45  m_params.emplace_back( new PARAM<wxString>( "find_replace.find_string",
46  &m_FindReplace.find_string, "" ) );
47 
48  m_params.emplace_back( new PARAM_LIST<wxString>( "find_replace.find_history",
49  &m_FindReplace.find_history, {} ) );
50 
51  m_params.emplace_back( new PARAM<wxString>( "find_replace.replace_string",
53 
54  m_params.emplace_back( new PARAM_LIST<wxString>( "find_replace.replace_history",
56 
57 #ifdef __WXMAC__
58  // Cairo renderer doesn't handle Retina displays so default to OpenGL
59  m_params.emplace_back( new PARAM<int>( "graphics.canvas_type", &m_Graphics.canvas_type,
61 #else
62  m_params.emplace_back( new PARAM<int>( "graphics.canvas_type", &m_Graphics.canvas_type,
64 #endif
65 
66  m_params.emplace_back(
67  new PARAM<int>( "lib_tree.column_width", &m_LibTree.column_width, 360 ) );
68 
69  m_params.emplace_back(
70  new PARAM<bool>( "printing.background", &m_Printing.background, false ) );
71 
72  m_params.emplace_back( new PARAM<bool>( "printing.monochrome", &m_Printing.monochrome, true ) );
73 
74  m_params.emplace_back( new PARAM<double>( "printing.scale", &m_Printing.scale, 1.0 ) );
75 
76  m_params.emplace_back(
77  new PARAM<bool>( "printing.use_theme", &m_Printing.use_theme, false ) );
78 
79  m_params.emplace_back(
80  new PARAM<wxString>( "printing.color_theme", &m_Printing.color_theme, "" ) );
81 
82  m_params.emplace_back( new PARAM<bool>( "printing.title_block",
83  &m_Printing.title_block, false ) );
84 
85  m_params.emplace_back( new PARAM_LIST<int>( "printing.layers", &m_Printing.layers, {} ) );
86 
87  m_params.emplace_back( new PARAM<bool>( "system.first_run_shown",
88  &m_System.first_run_shown, false ) );
89 
90  m_params.emplace_back( new PARAM<int>( "system.max_undo_items", &m_System.max_undo_items, 0 ) );
91 
92 
93  m_params.emplace_back( new PARAM_LIST<wxString>( "system.file_history",
94  &m_System.file_history, {} ) );
95 
96  m_params.emplace_back( new PARAM<int>( "system.units", &m_System.units,
97  static_cast<int>( EDA_UNITS::MILLIMETRES ) ) );
98 
99  m_params.emplace_back( new PARAM<wxString>( "appearance.color_theme", &m_ColorTheme, "user" ) );
100 
101  addParamsForWindow( &m_Window, "window" );
102 }
103 
104 
105 bool APP_SETTINGS_BASE::MigrateFromLegacy( wxConfigBase* aCfg )
106 {
107  bool ret = true;
108 
109  const std::string f = getLegacyFrameName();
110 
111  ret &= fromLegacyString( aCfg, "LastFindString", "find_replace.find_string" );
112  ret &= fromLegacyString( aCfg, "LastReplaceString", "find_replace.replace_string" );
113 
114  migrateFindReplace( aCfg );
115 
116  ret &= fromLegacy<int>( aCfg, "canvas_type", "graphics.canvas_type" );
117 
118  ret &= fromLegacy<int>( aCfg, "P22LIB_TREE_MODEL_ADAPTERSelectorColumnWidth",
119  "lib_tree.column_width" );
120 
121  ret &= fromLegacy<bool>( aCfg, "PrintMonochrome", "printing.monochrome" );
122  ret &= fromLegacy<double>( aCfg, "PrintScale", "printing.scale" );
123  ret &= fromLegacy<bool>( aCfg, "PrintPageFrame", "printing.title_block" );
124 
125  {
126  nlohmann::json js = nlohmann::json::array();
127  wxString key;
128  bool val = false;
129 
130  for( unsigned i = 0; i < PCB_LAYER_ID_COUNT; ++i )
131  {
132  key.Printf( wxT( "PlotLayer_%d" ), i );
133 
134  if( aCfg->Read( key, &val ) && val )
135  js.push_back( i );
136  }
137 
138  ( *this )[PointerFromString( "printing.layers" ) ] = js;
139  }
140 
141  ret &= fromLegacy<bool>( aCfg, f + "FirstRunShown", "system.first_run_shown" );
142  ret &= fromLegacy<int>( aCfg, f + "DevelMaxUndoItems", "system.max_undo_items" );
143  ret &= fromLegacy<int>( aCfg, f + "Units", "system.units" );
144 
145  {
146  int max_history_size = Pgm().GetCommonSettings()->m_System.file_history_size;
147  wxString file, key;
148  nlohmann::json js = nlohmann::json::array();
149 
150  for( int i = 1; i <= max_history_size; i++ )
151  {
152  key.Printf( "file%d", i );
153  file = aCfg->Read( key, wxEmptyString );
154 
155  if( !file.IsEmpty() )
156  js.push_back( file.ToStdString() );
157  }
158 
159  ( *this )[PointerFromString( "system.file_history" )] = js;
160  }
161 
162  ret &= migrateWindowConfig( aCfg, f, "window" );
163 
164  return ret;
165 }
166 
167 
168 void APP_SETTINGS_BASE::migrateFindReplace( wxConfigBase* aCfg )
169 {
170  const int find_replace_history_size = 10;
171  nlohmann::json find_history = nlohmann::json::array();
172  nlohmann::json replace_history = nlohmann::json::array();
173  wxString tmp, find_key, replace_key;
174 
175  for( int i = 0; i < find_replace_history_size; ++i )
176  {
177  find_key.Printf( "FindStringHistoryList%d", i );
178  replace_key.Printf( "ReplaceStringHistoryList%d", i );
179 
180  if( aCfg->Read( find_key, &tmp ) )
181  find_history.push_back( tmp.ToStdString() );
182 
183  if( aCfg->Read( replace_key, &tmp ) )
184  replace_history.push_back( tmp.ToStdString() );
185  }
186 
187  ( *this )[PointerFromString( "find_replace.find_history" )] = find_history;
188  ( *this )[PointerFromString( "find_replace.replace_history" )] = replace_history;
189 }
190 
191 
192 bool APP_SETTINGS_BASE::migrateWindowConfig( wxConfigBase* aCfg, const std::string& aFrame,
193  const std::string& aJsonPath )
194 {
195  bool ret = true;
196 
197  const std::string gd = "GalDisplayOptions";
198 
199  ret &= fromLegacy<bool>( aCfg, aFrame + "Maximized", aJsonPath + ".maximized" );
200  ret &= fromLegacyString( aCfg, aFrame + "MostRecentlyUsedPath", aJsonPath + ".mru_path" );
201  ret &= fromLegacy<int>( aCfg, aFrame + "Size_x", aJsonPath + ".size_x" );
202  ret &= fromLegacy<int>( aCfg, aFrame + "Size_y", aJsonPath + ".size_y" );
203  ret &= fromLegacyString( aCfg, aFrame + "Perspective", aJsonPath + ".perspective" );
204  ret &= fromLegacy<int>( aCfg, aFrame + "Pos_x", aJsonPath + ".pos_x" );
205  ret &= fromLegacy<int>( aCfg, aFrame + "Pos_y", aJsonPath + ".pos_y" );
206 
207  ret &= fromLegacy<bool>( aCfg,
208  aFrame + gd + "ForceDisplayCursor", aJsonPath + ".cursor.always_show_cursor" );
209  ret &= fromLegacy<bool>( aCfg,
210  aFrame + gd + "CursorFullscreen", aJsonPath + ".cursor.fullscreen_cursor" );
211 
212  ret &= fromLegacy<int>( aCfg,
213  aFrame + "_LastGridSize", aJsonPath + ".grid.last_size" );
214  ret &= fromLegacy<bool>( aCfg,
215  aFrame + gd + "GridAxesEnabled", aJsonPath + ".grid.axes_enabled" );
216  ret &= fromLegacy<double>( aCfg,
217  aFrame + gd + "GridLineWidth", aJsonPath + ".grid.line_width" );
218  ret &= fromLegacy<double>( aCfg,
219  aFrame + gd + "GridMaxDensity", aJsonPath + ".grid.min_spacing" );
220  ret &= fromLegacy<bool>( aCfg, aFrame + gd + "ShowGrid", aJsonPath + ".grid.show" );
221  ret &= fromLegacy<int>( aCfg, aFrame + gd + "GridStyle", aJsonPath + ".grid.style" );
222  ret &= fromLegacyColor( aCfg, aFrame + gd + "GridColor", aJsonPath + ".grid.color" );
223 
224  return ret;
225 }
226 
227 
228 void APP_SETTINGS_BASE::addParamsForWindow( WINDOW_SETTINGS* aWindow, const std::string& aJsonPath )
229 {
230  m_params.emplace_back(
231  new PARAM<bool>( aJsonPath + ".maximized", &aWindow->maximized, false ) );
232 
233  m_params.emplace_back( new PARAM<wxString>( aJsonPath + ".mru_path", &aWindow->mru_path, "" ) );
234 
235  m_params.emplace_back( new PARAM<int>( aJsonPath + ".size_x", &aWindow->size_x, 0 ) );
236 
237  m_params.emplace_back( new PARAM<int>( aJsonPath + ".size_y", &aWindow->size_y, 0 ) );
238 
239  m_params.emplace_back(
240  new PARAM<wxString>( aJsonPath + ".perspective", &aWindow->perspective, "" ) );
241 
242  m_params.emplace_back( new PARAM<int>( aJsonPath + ".pos_x", &aWindow->pos_x, 0 ) );
243 
244  m_params.emplace_back( new PARAM<int>( aJsonPath + ".pos_y", &aWindow->pos_y, 0 ) );
245 
246  m_params.emplace_back( new PARAM<bool>( aJsonPath + ".grid.axes_enabled",
247  &aWindow->grid.axes_enabled, false ) );
248 
249  m_params.emplace_back(
250  new PARAM<int>( aJsonPath + ".grid.last_size", &aWindow->grid.last_size, 0 ) );
251 
252  m_params.emplace_back(
253  new PARAM<double>( aJsonPath + ".grid.line_width", &aWindow->grid.line_width, 1.0 ) );
254 
255  m_params.emplace_back(
256  new PARAM<double>( aJsonPath + ".grid.min_spacing", &aWindow->grid.min_spacing, 10 ) );
257 
258  m_params.emplace_back( new PARAM<bool>( aJsonPath + ".grid.show", &aWindow->grid.show, true ) );
259 
260  m_params.emplace_back( new PARAM<int>( aJsonPath + ".grid.style", &aWindow->grid.style, 0 ) );
261 
262  m_params.emplace_back( new PARAM<bool>( aJsonPath + ".cursor.always_show_cursor",
263  &aWindow->cursor.always_show_cursor, true ) );
264 
265  m_params.emplace_back( new PARAM<bool>( aJsonPath + ".cursor.fullscreen_cursor",
266  &aWindow->cursor.fullscreen_cursor, false ) );
267 }
wxString mru_path
Definition: app_settings.h:55
APP_SETTINGS_BASE(std::string aFilename, int aSchemaVersion)
void addParamsForWindow(WINDOW_SETTINGS *aWindow, const std::string &aJsonPath)
Adds parameters for the given window object.
bool fullscreen_cursor
Definition: app_settings.h:33
std::vector< wxString > file_history
Definition: app_settings.h:112
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:104
std::vector< PARAM_BASE * > m_params
The list of parameters (owned by this object)
std::vector< wxString > find_history
Definition: app_settings.h:82
double min_spacing
Definition: app_settings.h:44
virtual bool MigrateFromLegacy(wxConfigBase *aCfg) override
Migrates from wxConfig to JSON-based configuration.
const int appSettingsSchemaVersion
! Update the schema version whenever a migration is required
SETTINGS_LOC
Definition: json_settings.h:36
wxString color_theme
Color theme to use for printing.
Definition: app_settings.h:103
Template specialization to enable wxStrings for certain containers (e.g. unordered_map)
Definition: bitmap.cpp:56
bool title_block
Whether or not to print title block.
Definition: app_settings.h:104
void migrateFindReplace(wxConfigBase *aCfg)
! Migrates the find/replace history string lists
bool use_theme
If false, display color theme will be used.
Definition: app_settings.h:102
nlohmann::json json
Definition: gerbview.cpp:40
bool always_show_cursor
Definition: app_settings.h:32
bool monochrome
Whether or not to print in monochrome.
Definition: app_settings.h:100
double line_width
Definition: app_settings.h:43
virtual std::string getLegacyFrameName() const
Definition: app_settings.h:143
Stores the common settings that are saved and loaded for each window / frame.
Definition: app_settings.h:52
wxString perspective
Definition: app_settings.h:58
CURSOR_SETTINGS cursor
Definition: app_settings.h:62
FIND_REPLACE m_FindReplace
Definition: app_settings.h:123
GRID_SETTINGS grid
Definition: app_settings.h:63
std::vector< int > layers
List of enabled layers for printing.
Definition: app_settings.h:105
std::vector< wxString > replace_history
Definition: app_settings.h:84
bool background
Whether or not to print background color.
Definition: app_settings.h:99
WINDOW_SETTINGS m_Window
Definition: app_settings.h:133
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 fromLegacyColor(wxConfigBase *aConfig, const std::string &aKey, const std::string &aDest)
Translates a legacy COLOR4D stored in a wxConfig string to a given JSON pointer value.
see class PGM_BASE
Board layer functions and definitions.
double scale
Printout scale.
Definition: app_settings.h:101
The main config directory (e.g. ~/.config/kicad/)
The common library.
static nlohmann::json::json_pointer PointerFromString(std::string aPath)
Builds a JSON pointer based on a given string.
bool migrateWindowConfig(wxConfigBase *aCfg, const std::string &aFrameName, const std::string &aJsonPath)
Migrates legacy window settings into the JSON document.
wxString m_ColorTheme
Active color theme name.
Definition: app_settings.h:136