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 (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#ifndef _JSON_SETTINGS_H
22#define _JSON_SETTINGS_H
23
24#include <core/wx_stl_compat.h>
25
26#include <utility>
27#include <wx/string.h>
28
29#include <functional>
30#include <optional>
32#include <json_conversions.h>
33
34#include <kicommon.h>
35
36class wxConfigBase;
37class NESTED_SETTINGS;
38class PARAM_BASE;
40
41class wxAuiPaneInfo;
42struct BOM_FIELD;
43struct BOM_PRESET;
44struct BOM_FMT_PRESET;
45struct GRID;
46
47namespace KIGFX
48{
49class COLOR4D;
50};
51
52#define traceSettings wxT( "KICAD_SETTINGS" )
53
54enum class SETTINGS_LOC {
55 USER,
56 PROJECT,
57 COLORS,
58 NESTED,
59 NONE,
60};
61
62
65
67{
68public:
69 friend class NESTED_SETTINGS;
70
71 JSON_SETTINGS( const wxString& aFilename, SETTINGS_LOC aLocation, int aSchemaVersion ) :
72 JSON_SETTINGS( aFilename, aLocation, aSchemaVersion, true, true, true )
73 {}
74
75 JSON_SETTINGS( const wxString& aFilename, SETTINGS_LOC aLocation, int aSchemaVersion,
76 bool aCreateIfMissing, bool aCreateIfDefault, bool aWriteFile );
77
78 virtual ~JSON_SETTINGS();
79
80 wxString GetFilename() const { return m_filename; }
81
82 wxString GetFullFilename() const;
83
84 void SetFilename( const wxString& aFilename ) { m_filename = aFilename; }
85
86 void SetLocation( SETTINGS_LOC aLocation ) { m_location = aLocation; }
87 SETTINGS_LOC GetLocation() const { return m_location; }
88
89 void SetLegacyFilename( const wxString& aFilename ) { m_legacy_filename = aFilename; }
90
91 bool IsReadOnly() const { return !m_writeFile; }
92 void SetReadOnly( bool aReadOnly ) { m_writeFile = !aReadOnly; }
93
99 nlohmann::json& At( const std::string& aPath );
100 bool Contains( const std::string& aPath ) const;
101
102 JSON_SETTINGS_INTERNALS* Internals();
103
107 virtual void Load();
108
114 virtual bool Store();
115
121 virtual bool LoadFromFile( const wxString& aDirectory = "" );
122
128 virtual bool SaveToFile( const wxString& aDirectory = "", bool aForce = false );
129
133 void ResetToDefaults();
134
141 std::optional<nlohmann::json> GetJson( const std::string& aPath ) const;
142
150 template<typename ValueType>
151 std::optional<ValueType> Get( const std::string& aPath ) const;
152
160 template<typename ValueType>
161 void Set( const std::string& aPath, ValueType aVal );
162
172 bool Migrate();
173
180 virtual bool MigrateFromLegacy( wxConfigBase* aLegacyConfig );
181
190 void AddNestedSettings( NESTED_SETTINGS* aSettings );
191
196 void ReleaseNestedSettings( NESTED_SETTINGS* aSettings );
197
198 void SetManager( SETTINGS_MANAGER* aManager )
199 {
200 m_manager = aManager;
201 }
202
209 static bool SetIfPresent( const nlohmann::json& aObj, const std::string& aPath,
210 wxString& aTarget );
211
218 static bool SetIfPresent( const nlohmann::json& aObj, const std::string& aPath, bool& aTarget );
219
226 static bool SetIfPresent( const nlohmann::json& aObj, const std::string& aPath, int& aTarget );
227
234 static bool SetIfPresent( const nlohmann::json& aObj, const std::string& aPath ,
235 unsigned int& aTarget );
236
237 const std::string FormatAsString();
238
239 bool LoadFromRawFile( const wxString& aPath );
240
241protected:
242
251 void registerMigration( int aOldSchemaVersion, int aNewSchemaVersion,
252 std::function<bool(void)> aMigrator );
253
261 template<typename ValueType>
262 bool fromLegacy( wxConfigBase* aConfig, const std::string& aKey, const std::string& aDest );
263
270 bool fromLegacyString( wxConfigBase* aConfig, const std::string& aKey,
271 const std::string& aDest );
272
279 bool fromLegacyColor( wxConfigBase* aConfig, const std::string& aKey,
280 const std::string& aDest );
281
282 virtual wxString getFileExt() const
283 {
284 return wxT( "json" );
285 }
286
287 virtual wxString getLegacyFileExt() const
288 {
289 return wxEmptyString;
290 }
291
299 template<typename ResultType>
300 static ResultType fetchOrDefault( const nlohmann::json& aJson, const std::string& aKey,
301 ResultType aDefault = ResultType() );
302
304 wxString m_filename;
305
308
311
313 std::vector<PARAM_BASE*> m_params;
314
316 std::vector<NESTED_SETTINGS*> m_nested_settings;
317
320
326
329
332
335
338
341
344
346 std::map<int, std::pair<int, std::function<bool()>>> m_migrators;
347
348 std::unique_ptr<JSON_SETTINGS_INTERNALS> m_internals;
349};
350
351// Specializations to allow conversion between wxString and std::string via JSON_SETTINGS API
352
353template<> KICOMMON_API std::optional<wxString> JSON_SETTINGS::Get( const std::string& aPath ) const;
354
355template<> KICOMMON_API void JSON_SETTINGS::Set<wxString>( const std::string& aPath, wxString aVal );
356
357// Specializations to allow directly reading/writing wxStrings from JSON
358
359KICOMMON_API void to_json( nlohmann::json& aJson, const wxString& aString );
360
361KICOMMON_API void from_json( const nlohmann::json& aJson, wxString& aString );
362
363extern template std::optional<bool> JSON_SETTINGS::Get<bool>( const std::string& aPath ) const;
364extern template std::optional<double> JSON_SETTINGS::Get<double>( const std::string& aPath ) const;
365extern template std::optional<float> JSON_SETTINGS::Get<float>( const std::string& aPath ) const;
366extern template std::optional<int> JSON_SETTINGS::Get<int>( const std::string& aPath ) const;
367extern template std::optional<unsigned int> JSON_SETTINGS::Get<unsigned int>( const std::string& aPath ) const;
368extern template std::optional<unsigned long long> JSON_SETTINGS::Get<unsigned long long>( const std::string& aPath ) const;
369extern template std::optional<std::string> JSON_SETTINGS::Get<std::string>( const std::string& aPath ) const;
370extern template std::optional<nlohmann::json> JSON_SETTINGS::Get<nlohmann::json>( const std::string& aPath ) const;
371extern template std::optional<KIGFX::COLOR4D> JSON_SETTINGS::Get<KIGFX::COLOR4D>( const std::string& aPath ) const;
372extern template std::optional<BOM_FIELD> JSON_SETTINGS::Get<BOM_FIELD>( const std::string& aPath ) const;
373extern template std::optional<BOM_PRESET> JSON_SETTINGS::Get<BOM_PRESET>( const std::string& aPath ) const;
374extern template std::optional<BOM_FMT_PRESET> JSON_SETTINGS::Get<BOM_FMT_PRESET>( const std::string& aPath ) const;
375extern template std::optional<GRID> JSON_SETTINGS::Get<GRID>( const std::string& aPath ) const;
376extern template std::optional<wxPoint> JSON_SETTINGS::Get<wxPoint>( const std::string& aPath ) const;
377extern template std::optional<wxSize> JSON_SETTINGS::Get<wxSize>( const std::string& aPath ) const;
378extern template std::optional<wxRect> JSON_SETTINGS::Get<wxRect>( const std::string& aPath ) const;
379extern template std::optional<wxAuiPaneInfo> JSON_SETTINGS::Get<wxAuiPaneInfo>( const std::string& aPath ) const;
380
381#endif
KICAD_PLUGIN_EXPORT SCENEGRAPH * Load(char const *aFileName)
reads a model file and creates a generic display structure
virtual wxString getFileExt() const
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
Definition: json_settings.h:87
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 ...
void SetLocation(SETTINGS_LOC aLocation)
Definition: json_settings.h:86
bool m_writeFile
Whether or not the backing store file should be written.
void SetManager(SETTINGS_MANAGER *aManager)
bool IsReadOnly() const
Definition: json_settings.h:91
void SetReadOnly(bool aReadOnly)
Definition: json_settings.h:92
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)
Definition: json_settings.h:71
bool m_deleteLegacyAfterMigration
Whether or not to delete legacy file after migration.
std::unique_ptr< JSON_SETTINGS_INTERNALS > m_internals
void SetFilename(const wxString &aFilename)
Definition: json_settings.h:84
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)
Definition: json_settings.h:89
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
Definition: json_settings.h:80
NESTED_SETTINGS is a JSON_SETTINGS that lives inside a JSON_SETTINGS.
KICOMMON_API void to_json(nlohmann::json &aJson, const wxString &aString)
SETTINGS_LOC
Definition: json_settings.h:54
@ PROJECT
The settings directory inside a project folder.
@ 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.
@ NONE
No directory prepended, full path in filename (used for PROJECT_FILE)
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: color4d.cpp:247
Common grid settings, available to every frame.
Definition: grid_settings.h:34