KiCad PCB EDA Suite
Loading...
Searching...
No Matches
database_lib_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) 2022 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
8* modify it under the terms of the GNU General Public License
9* as published by the Free Software Foundation; either version 2
10* of the License, or (at your option) any later version.
11*
12* This program is distributed in the hope that it will be useful,
13* but WITHOUT ANY WARRANTY; without even the implied warranty of
14* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15* GNU General Public License for more details.
16*
17* You should have received a copy of the GNU General Public License
18* along with this program. If not, see <https://www.gnu.org/licenses/>.
19*/
20
21#include <core/kicad_algo.h>
22#include <json_common.h>
23#include <algorithm>
24
26#include <settings/parameters.h>
28
29
30const int dblibSchemaVersion = 1;
31
32
33DATABASE_FIELD_MAPPING::DATABASE_FIELD_MAPPING( const std::string& aColumn, const std::string& aName,
34 bool aVisibleOnAdd, bool aVisibleInChooser,
35 bool aShowName, bool aInheritProperties ) :
36 column( aColumn ),
37 name( aName ),
38 name_wx( aName.c_str(), wxConvUTF8 ),
39 visible_on_add( aVisibleOnAdd ),
40 visible_in_chooser( aVisibleInChooser ),
41 show_name( aShowName ),
42 inherit_properties( aInheritProperties )
43{
44}
45
46
47DATABASE_LIB_SETTINGS::DATABASE_LIB_SETTINGS( const std::string& aFilename ) :
49{
50
51 m_params.emplace_back( new PARAM<std::string>( "source.dsn", &m_Source.dsn, "" ) );
52
53 m_params.emplace_back( new PARAM<std::string>( "source.username", &m_Source.username, "" ) );
54
55 m_params.emplace_back( new PARAM<std::string>( "source.password", &m_Source.password, "" ) );
56
57 m_params.emplace_back( new PARAM<std::string>( "source.connection_string",
58 &m_Source.connection_string, "" ) );
59
60 m_params.emplace_back( new PARAM<int>( "source.timeout_seconds", &m_Source.timeout, 2 ) );
61
62 m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>(
63 "libraries",
64 [&]() -> nlohmann::json
65 {
66 // TODO: implement this; libraries are read-only from KiCad at the moment
67 return {};
68 },
69 [&]( const nlohmann::json aObj )
70 {
71 m_Tables.clear();
72
73 if( !aObj.is_array() )
74 return;
75
76 for( const nlohmann::json& entry : aObj )
77 {
78 if( entry.empty() || !entry.is_object() )
79 continue;
80
82
83 table.name = entry["name"].get<std::string>();
84 table.table = entry["table"].get<std::string>();
85 table.key_col = entry["key"].get<std::string>();
86 table.symbols_col = entry["symbols"].get<std::string>();
87 table.footprints_col = entry["footprints"].get<std::string>();
88
89 // Sanitize library display names; currently only `/` is removed because we
90 // use it as a separator and allow it in symbol names.
91 std::erase( table.name, '/' );
92
93 if( entry.contains( "properties" ) && entry["properties"].is_object() )
94 {
95 const nlohmann::json& propJson = entry["properties"];
96
97 table.properties.description =
98 fetchOrDefault<std::string>( propJson, "description" );
99
100 table.properties.footprint_filters =
101 fetchOrDefault<std::string>( propJson, "footprint_filters" );
102
103 table.properties.keywords =
104 fetchOrDefault<std::string>( propJson, "keywords" );
105
106 table.properties.exclude_from_bom =
107 fetchOrDefault<std::string>( propJson, "exclude_from_bom" );
108
109 table.properties.exclude_from_board =
110 fetchOrDefault<std::string>( propJson, "exclude_from_board" );
111
112 table.properties.exclude_from_sim =
113 fetchOrDefault<std::string>( propJson, "exclude_from_sim" );
114 }
115
116 if( entry.contains( "fields" ) && entry["fields"].is_array() )
117 {
118 for( const nlohmann::json& fieldJson : entry["fields"] )
119 {
120 if( fieldJson.empty() || !fieldJson.is_object() )
121 continue;
122
123 table.fields.emplace_back( DATABASE_FIELD_MAPPING(
124 fetchOrDefault<std::string>( fieldJson, "column" ),
125 fetchOrDefault<std::string>( fieldJson, "name" ),
126 fetchOrDefault<bool>( fieldJson, "visible_on_add" ),
127 fetchOrDefault<bool>( fieldJson, "visible_in_chooser" ),
128 fetchOrDefault<bool>( fieldJson, "show_name" ),
129 fetchOrDefault<bool>( fieldJson, "inherit_properties" ) ) );
130 }
131 }
132
133 m_Tables.emplace_back( std::move( table ) );
134 }
135 },
136 {} ) );
137
138 m_params.emplace_back( new PARAM<int>( "cache.max_size", &m_Cache.max_size, 256 ) );
139
140 m_params.emplace_back( new PARAM<int>( "cache.max_age", &m_Cache.max_age, 10 ) );
141
142 m_params.emplace_back( new PARAM<bool>( "globally_unique_keys", &m_GloballyUniqueKeys, false ) );
143
144 registerMigration( 0, 1,
145 [&]() -> bool
146 {
147 /*
148 * Schema 0 -> 1
149 * Move internal symbol properties from fields with special names to
150 * a separate place in the schema.
151 */
152 if( !Contains( "libraries" ) || !At( "libraries" ).is_array() )
153 return true;
154
155 for( nlohmann::json& library : At( "libraries" ) )
156 {
157 if( !library.contains( "fields" ) )
158 continue;
159
160 for( const nlohmann::json& field : library["fields"] )
161 {
162 if( !field.contains( "name" ) || !field.contains( "column" ) )
163 continue;
164
165 std::string name = field["name"].get<std::string>();
166 std::string col = field["column"].get<std::string>();
167
168 if( name == "ki_description" )
169 library["properties"]["description"] = col;
170 else if( name == "ki_fp_filters" )
171 library["properties"]["footprint_filters"] = col;
172 }
173 }
174
175 return true;
176 } );
177}
178
179
const char * name
DATABASE_LIB_SETTINGS(const std::string &aFilename)
wxString getFileExt() const override
std::vector< PARAM_BASE * > m_params
The list of parameters (owned by this object)
JSON_SETTINGS(const wxString &aFilename, SETTINGS_LOC aLocation, int aSchemaVersion)
Like a normal param, but with custom getter and setter functions.
Definition parameters.h:297
const int dblibSchemaVersion
@ NONE
Definition eda_shape.h:72
static const std::string DatabaseLibraryFileExtension
SETTINGS_LOC
bool visible_in_chooser
Whether the column is shown by default in the chooser.
std::string column
Database column name.
std::string name
KiCad field name.
bool inherit_properties
Whether or not to inherit properties from symbol field.
DATABASE_FIELD_MAPPING(const std::string &aColumn, const std::string &aName, bool aVisibleOnAdd, bool aVisibleInChooser, bool aShowName, bool aInheritProperties)
bool visible_on_add
Whether to show the field when placing the symbol.
bool show_name
Whether or not to show the field name as well as its value.
wxString name_wx
KiCad field name (converted)
A database library table will be mapped to a sub-library provided by the database library entry in th...
std::vector< std::vector< std::string > > table
Definition of file extensions used in Kicad.