KiCad PCB EDA Suite
Loading...
Searching...
No Matches
json_settings.h
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#ifndef _JSON_SETTINGS_H
22#define _JSON_SETTINGS_H
23
24#include <core/wx_stl_compat.h>
26
27#include <utility>
28#include <wx/string.h>
29
30#include <functional>
31#include <optional>
33#include <json_conversions.h>
34
35#include <kicommon.h>
36
37class wxConfigBase;
38class NESTED_SETTINGS;
39class PARAM_BASE;
41
42class wxAuiPaneInfo;
43struct BOM_FIELD;
44struct BOM_PRESET;
45struct BOM_FMT_PRESET;
46struct GRID;
47
48namespace KIGFX
49{
50class COLOR4D;
51};
52
53#define traceSettings wxT( "KICAD_SETTINGS" )
54
63
64
67
69{
70public:
71 friend class NESTED_SETTINGS;
72
73 JSON_SETTINGS( const wxString& aFilename, SETTINGS_LOC aLocation, int aSchemaVersion ) :
74 JSON_SETTINGS( aFilename, aLocation, aSchemaVersion, true, true, true )
75 {}
76
77 JSON_SETTINGS( const wxString& aFilename, SETTINGS_LOC aLocation, int aSchemaVersion,
78 bool aCreateIfMissing, bool aCreateIfDefault, bool aWriteFile );
79
80 virtual ~JSON_SETTINGS();
81
82 // We own at least one list of raw pointers. Don't let the compiler fill in copy c'tors that
83 // will only land us in trouble.
84 JSON_SETTINGS( const JSON_SETTINGS& ) = delete;
86
87 wxString GetFilename() const { return m_filename; }
88
89 wxString GetFullFilename() const;
90
91 void SetFilename( const wxString& aFilename ) { m_filename = aFilename; }
92
93 void SetLocation( SETTINGS_LOC aLocation ) { m_location = aLocation; }
95
96 void SetLegacyFilename( const wxString& aFilename ) { m_legacy_filename = aFilename; }
97
98 bool IsReadOnly() const { return !m_writeFile; }
99 void SetReadOnly( bool aReadOnly ) { m_writeFile = !aReadOnly; }
100
105
106 nlohmann::json& At( const std::string& aPath );
107 bool Contains( const std::string& aPath ) const;
108
109 JSON_SETTINGS_INTERNALS* Internals();
110
114 virtual void Load();
115
121 virtual bool Store();
122
128 virtual bool LoadFromFile( const wxString& aDirectory = "" );
129
135 virtual bool SaveToFile( const wxString& aDirectory = "", bool aForce = false );
136
140 void ResetToDefaults();
141
148 std::optional<nlohmann::json> GetJson( const std::string& aPath ) const;
149
157 template<typename ValueType>
158 std::optional<ValueType> Get( const std::string& aPath ) const;
159
167 template<typename ValueType>
168 void Set( const std::string& aPath, ValueType aVal );
169
170 virtual std::map<std::string, nlohmann::json> GetFileHistories();
171
181 bool Migrate();
182
189 virtual bool MigrateFromLegacy( wxConfigBase* aLegacyConfig );
190
199 void AddNestedSettings( NESTED_SETTINGS* aSettings );
200
205 void ReleaseNestedSettings( NESTED_SETTINGS* aSettings );
206
207 void SetManager( SETTINGS_MANAGER* aManager )
208 {
209 m_manager = aManager;
210 }
211
218 static bool SetIfPresent( const nlohmann::json& aObj, const std::string& aPath,
219 wxString& aTarget );
220
227 static bool SetIfPresent( const nlohmann::json& aObj, const std::string& aPath, bool& aTarget );
228
235 static bool SetIfPresent( const nlohmann::json& aObj, const std::string& aPath, int& aTarget );
236
243 static bool SetIfPresent( const nlohmann::json& aObj, const std::string& aPath ,
244 unsigned int& aTarget );
245
246 const std::string FormatAsString();
247
248 bool LoadFromRawFile( const wxString& aPath );
249
250protected:
251
260 void registerMigration( int aOldSchemaVersion, int aNewSchemaVersion,
261 std::function<bool(void)> aMigrator );
262
270 template<typename ValueType>
271 bool fromLegacy( wxConfigBase* aConfig, const std::string& aKey, const std::string& aDest );
272
279 bool fromLegacyString( wxConfigBase* aConfig, const std::string& aKey,
280 const std::string& aDest );
281
288 bool fromLegacyColor( wxConfigBase* aConfig, const std::string& aKey,
289 const std::string& aDest );
290
291 virtual wxString getFileExt() const
292 {
293 return wxT( "json" );
294 }
295
296 virtual wxString getLegacyFileExt() const
297 {
298 return wxEmptyString;
299 }
300
308 template<typename ResultType>
309 static ResultType fetchOrDefault( const nlohmann::json& aJson, const std::string& aKey,
310 ResultType aDefault = ResultType() );
311
313 wxString m_filename;
314
317
320
322 std::vector<PARAM_BASE*> m_params;
323
325 std::vector<NESTED_SETTINGS*> m_nested_settings;
326
329
335
338
341
344
347
350
353
356
358 std::map<int, std::pair<int, std::function<bool()>>> m_migrators;
359
360 std::unique_ptr<JSON_SETTINGS_INTERNALS> m_internals;
361};
362
363// Specializations to allow conversion between wxString and std::string via JSON_SETTINGS API
364
365template<> KICOMMON_API std::optional<wxString> JSON_SETTINGS::Get( const std::string& aPath ) const;
366
367template<> KICOMMON_API void JSON_SETTINGS::Set<wxString>( const std::string& aPath, wxString aVal );
368
369// Specializations to allow directly reading/writing wxStrings from JSON
370
371KICOMMON_API void to_json( nlohmann::json& aJson, const wxString& aString );
372
373KICOMMON_API void from_json( const nlohmann::json& aJson, wxString& aString );
374
375extern template std::optional<bool> JSON_SETTINGS::Get<bool>( const std::string& aPath ) const;
376extern template std::optional<double> JSON_SETTINGS::Get<double>( const std::string& aPath ) const;
377extern template std::optional<float> JSON_SETTINGS::Get<float>( const std::string& aPath ) const;
378extern template std::optional<int> JSON_SETTINGS::Get<int>( const std::string& aPath ) const;
379extern template std::optional<unsigned int> JSON_SETTINGS::Get<unsigned int>( const std::string& aPath ) const;
380extern template std::optional<unsigned long long> JSON_SETTINGS::Get<unsigned long long>( const std::string& aPath ) const;
381extern template std::optional<std::string> JSON_SETTINGS::Get<std::string>( const std::string& aPath ) const;
382extern template std::optional<nlohmann::json> JSON_SETTINGS::Get<nlohmann::json>( const std::string& aPath ) const;
383extern template std::optional<KIGFX::COLOR4D> JSON_SETTINGS::Get<KIGFX::COLOR4D>( const std::string& aPath ) const;
384extern template std::optional<BOM_FIELD> JSON_SETTINGS::Get<BOM_FIELD>( const std::string& aPath ) const;
385extern template std::optional<BOM_PRESET> JSON_SETTINGS::Get<BOM_PRESET>( const std::string& aPath ) const;
386extern template std::optional<BOM_FMT_PRESET> JSON_SETTINGS::Get<BOM_FMT_PRESET>( const std::string& aPath ) const;
387extern template std::optional<GRID> JSON_SETTINGS::Get<GRID>( const std::string& aPath ) const;
388extern template std::optional<wxPoint> JSON_SETTINGS::Get<wxPoint>( const std::string& aPath ) const;
389extern template std::optional<wxSize> JSON_SETTINGS::Get<wxSize>( const std::string& aPath ) const;
390extern template std::optional<wxRect> JSON_SETTINGS::Get<wxRect>( const std::string& aPath ) const;
391extern template std::optional<wxAuiPaneInfo> JSON_SETTINGS::Get<wxAuiPaneInfo>( const std::string& aPath ) const;
392extern template std::optional<KIGFX::CROSS_HAIR_MODE> JSON_SETTINGS::Get<KIGFX::CROSS_HAIR_MODE>( const std::string& aPath ) const;
393
394#endif
KICAD_PLUGIN_EXPORT SCENEGRAPH * Load(char const *aFileName)
Read a model file and creates a generic display structure.
virtual wxString getFileExt() const
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...
JSON_SETTINGS & operator=(const JSON_SETTINGS &)=delete
wxString m_filename
The filename (not including path) of this settings file (inicode)
SETTINGS_MANAGER * m_manager
A pointer to the settings manager managing this file (may be null)
SETTINGS_LOC GetLocation() const
bool m_isFutureFormat
Set to true if this settings is loaded from a file with a newer schema version than is known.
std::vector< NESTED_SETTINGS * > m_nested_settings
Nested settings files that live inside this one, if any.
bool m_modified
True if the JSON data store has been written to since the last file write.
bool m_createIfDefault
Whether or not the backing store file should be created if all parameters are still at their default ...
JSON_SETTINGS(const JSON_SETTINGS &)=delete
void SetLocation(SETTINGS_LOC aLocation)
bool m_writeFile
Whether or not the backing store file should be written.
void SetManager(SETTINGS_MANAGER *aManager)
bool IsReadOnly() const
void SetReadOnly(bool aReadOnly)
bool m_createIfMissing
Whether or not the backing store file should be created it if doesn't exist.
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)
bool m_deleteLegacyAfterMigration
Whether or not to delete legacy file after migration.
std::unique_ptr< JSON_SETTINGS_INTERNALS > m_internals
friend class NESTED_SETTINGS
void SetFilename(const wxString &aFilename)
SETTINGS_LOC m_location
The location of this settings file (.
virtual wxString getLegacyFileExt() const
bool m_resetParamsIfMissing
Whether or not to set parameters to their default value if missing from JSON on Load()
void SetLegacyFilename(const wxString &aFilename)
std::map< int, std::pair< int, std::function< bool()> > > m_migrators
A map of starting schema version to a pair of <ending version, migrator function>
int m_schemaVersion
Version of this settings schema.
wxString m_legacy_filename
The filename of the wxConfig legacy file (if different from m_filename)
wxString GetFilename() const
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:104
NESTED_SETTINGS is a JSON_SETTINGS that lives inside a JSON_SETTINGS.
Container for project specific data.
Definition project.h:65
@ NONE
Definition eda_shape.h:69
KICOMMON_API void to_json(nlohmann::json &aJson, const wxString &aString)
SETTINGS_LOC
@ TOOLBARS
The toolbar directory (e.g. ~/.config/kicad/toolbars/)
@ USER
The main config directory (e.g. ~/.config/kicad/)
@ COLORS
The color scheme directory (e.g. ~/.config/kicad/colors/)
@ NESTED
Not stored in a file, but inside another JSON_SETTINGS.
KICOMMON_API void from_json(const nlohmann::json &aJson, wxString &aString)
#define KICOMMON_API
Definition kicommon.h:28
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:33
Common grid settings, available to every frame.