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 table.pins_col = fetchOrDefault<std::string>( entry, "pins" );
89
90 // Sanitize library display names; currently only `/` is removed because we
91 // use it as a separator and allow it in symbol names.
92 std::erase( table.name, '/' );
93
94 if( entry.contains( "properties" ) && entry["properties"].is_object() )
95 {
96 const nlohmann::json& propJson = entry["properties"];
97
98 table.properties.description =
99 fetchOrDefault<std::string>( propJson, "description" );
100
101 table.properties.footprint_filters =
102 fetchOrDefault<std::string>( propJson, "footprint_filters" );
103
104 table.properties.keywords =
105 fetchOrDefault<std::string>( propJson, "keywords" );
106
107 table.properties.exclude_from_bom =
108 fetchOrDefault<std::string>( propJson, "exclude_from_bom" );
109
110 table.properties.exclude_from_board =
111 fetchOrDefault<std::string>( propJson, "exclude_from_board" );
112
113 table.properties.exclude_from_sim =
114 fetchOrDefault<std::string>( propJson, "exclude_from_sim" );
115 }
116
117 if( entry.contains( "fields" ) && entry["fields"].is_array() )
118 {
119 for( const nlohmann::json& fieldJson : entry["fields"] )
120 {
121 if( fieldJson.empty() || !fieldJson.is_object() )
122 continue;
123
124 table.fields.emplace_back( DATABASE_FIELD_MAPPING(
125 fetchOrDefault<std::string>( fieldJson, "column" ),
126 fetchOrDefault<std::string>( fieldJson, "name" ),
127 fetchOrDefault<bool>( fieldJson, "visible_on_add" ),
128 fetchOrDefault<bool>( fieldJson, "visible_in_chooser" ),
129 fetchOrDefault<bool>( fieldJson, "show_name" ),
130 fetchOrDefault<bool>( fieldJson, "inherit_properties" ) ) );
131 }
132 }
133
134 m_Tables.emplace_back( std::move( table ) );
135 }
136 },
137 {} ) );
138
139 m_params.emplace_back( new PARAM<int>( "cache.max_size", &m_Cache.max_size, 256 ) );
140
141 m_params.emplace_back( new PARAM<int>( "cache.max_age", &m_Cache.max_age, 10 ) );
142
143 m_params.emplace_back( new PARAM<bool>( "globally_unique_keys", &m_GloballyUniqueKeys, false ) );
144
145 registerMigration( 0, 1,
146 [&]() -> bool
147 {
148 /*
149 * Schema 0 -> 1
150 * Move internal symbol properties from fields with special names to
151 * a separate place in the schema.
152 */
153 if( !Contains( "libraries" ) || !At( "libraries" ).is_array() )
154 return true;
155
156 for( nlohmann::json& library : At( "libraries" ) )
157 {
158 if( !library.contains( "fields" ) )
159 continue;
160
161 for( const nlohmann::json& field : library["fields"] )
162 {
163 if( !field.contains( "name" ) || !field.contains( "column" ) )
164 continue;
165
166 std::string name = field["name"].get<std::string>();
167 std::string col = field["column"].get<std::string>();
168
169 if( name == "ki_description" )
170 library["properties"]["description"] = col;
171 else if( name == "ki_fp_filters" )
172 library["properties"]["footprint_filters"] = col;
173 }
174 }
175
176 return true;
177 } );
178}
179
180
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.