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(JE) implement library config editor. For now just round-trip
67 return m_librariesConfig;
68 },
69 [&]( const nlohmann::json& aObj )
70 {
71 m_Tables.clear();
72 m_librariesConfig = aObj;
73
74 if( !aObj.is_array() )
75 return;
76
77 for( const nlohmann::json& entry : aObj )
78 {
79 if( entry.empty() || !entry.is_object() )
80 continue;
81
82 DATABASE_LIB_TABLE table;
83
84 table.name = entry["name"].get<std::string>();
85 table.table = entry["table"].get<std::string>();
86 table.key_col = entry["key"].get<std::string>();
87 table.symbols_col = entry["symbols"].get<std::string>();
88 table.footprints_col = entry["footprints"].get<std::string>();
89 table.pins_col = fetchOrDefault<std::string>( entry, "pins" );
90
91 // Sanitize library display names; currently only `/` is removed because we
92 // use it as a separator and allow it in symbol names.
93 std::erase( table.name, '/' );
94
95 if( entry.contains( "properties" ) && entry["properties"].is_object() )
96 {
97 const nlohmann::json& propJson = entry["properties"];
98
99 table.properties.description =
100 fetchOrDefault<std::string>( propJson, "description" );
101
102 table.properties.footprint_filters =
103 fetchOrDefault<std::string>( propJson, "footprint_filters" );
104
105 table.properties.keywords =
106 fetchOrDefault<std::string>( propJson, "keywords" );
107
108 table.properties.exclude_from_bom =
109 fetchOrDefault<std::string>( propJson, "exclude_from_bom" );
110
111 table.properties.exclude_from_board =
112 fetchOrDefault<std::string>( propJson, "exclude_from_board" );
113
114 table.properties.exclude_from_sim =
115 fetchOrDefault<std::string>( propJson, "exclude_from_sim" );
116 }
117
118 if( entry.contains( "fields" ) && entry["fields"].is_array() )
119 {
120 for( const nlohmann::json& fieldJson : entry["fields"] )
121 {
122 if( fieldJson.empty() || !fieldJson.is_object() )
123 continue;
124
125 table.fields.emplace_back( DATABASE_FIELD_MAPPING(
126 fetchOrDefault<std::string>( fieldJson, "column" ),
127 fetchOrDefault<std::string>( fieldJson, "name" ),
128 fetchOrDefault<bool>( fieldJson, "visible_on_add" ),
129 fetchOrDefault<bool>( fieldJson, "visible_in_chooser" ),
130 fetchOrDefault<bool>( fieldJson, "show_name" ),
131 fetchOrDefault<bool>( fieldJson, "inherit_properties" ) ) );
132 }
133 }
134
135 m_Tables.emplace_back( std::move( table ) );
136 }
137 },
138 {} ) );
139
140 m_params.emplace_back( new PARAM<int>( "cache.max_size", &m_Cache.max_size, 256 ) );
141
142 m_params.emplace_back( new PARAM<int>( "cache.max_age", &m_Cache.max_age, 60 ) );
143
144 m_params.emplace_back( new PARAM<bool>( "globally_unique_keys", &m_GloballyUniqueKeys, false ) );
145
146 registerMigration( 0, 1,
147 [&]() -> bool
148 {
149 /*
150 * Schema 0 -> 1
151 * Move internal symbol properties from fields with special names to
152 * a separate place in the schema.
153 */
154 if( !Contains( "libraries" ) || !At( "libraries" ).is_array() )
155 return true;
156
157 for( nlohmann::json& library : At( "libraries" ) )
158 {
159 if( !library.contains( "fields" ) )
160 continue;
161
162 for( const nlohmann::json& field : library["fields"] )
163 {
164 if( !field.contains( "name" ) || !field.contains( "column" ) )
165 continue;
166
167 std::string name = field["name"].get<std::string>();
168 std::string col = field["column"].get<std::string>();
169
170 if( name == "ki_description" )
171 library["properties"]["description"] = col;
172 else if( name == "ki_fp_filters" )
173 library["properties"]["footprint_filters"] = col;
174 }
175 }
176
177 return true;
178 } );
179}
180
181
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:299
const int dblibSchemaVersion
@ NONE
Definition eda_fill.h:42
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)
Definition of file extensions used in Kicad.