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, you may find one here:
19* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20* or you may search the http://www.gnu.org website for the version 2 license,
21* or you may write to the Free Software Foundation, Inc.,
22* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23*/
24
25#include <core/kicad_algo.h>
26#include <json_common.h>
27
29#include <settings/parameters.h>
31
32
33const int dblibSchemaVersion = 1;
34
35
36DATABASE_FIELD_MAPPING::DATABASE_FIELD_MAPPING( std::string aColumn, std::string aName,
37 bool aVisibleOnAdd, bool aVisibleInChooser,
38 bool aShowName, bool aInheritProperties ) :
39 column( aColumn ),
40 name( aName ),
41 name_wx( aName.c_str(), wxConvUTF8 ),
42 visible_on_add( aVisibleOnAdd ),
43 visible_in_chooser( aVisibleInChooser ),
44 show_name( aShowName ),
45 inherit_properties( aInheritProperties )
46{
47}
48
49
50DATABASE_LIB_SETTINGS::DATABASE_LIB_SETTINGS( const std::string& aFilename ) :
52{
53
54 m_params.emplace_back( new PARAM<std::string>( "source.dsn", &m_Source.dsn, "" ) );
55
56 m_params.emplace_back( new PARAM<std::string>( "source.username", &m_Source.username, "" ) );
57
58 m_params.emplace_back( new PARAM<std::string>( "source.password", &m_Source.password, "" ) );
59
60 m_params.emplace_back( new PARAM<std::string>( "source.connection_string",
62
63 m_params.emplace_back( new PARAM<int>( "source.timeout_seconds", &m_Source.timeout, 2 ) );
64
65 m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>(
66 "libraries",
67 [&]() -> nlohmann::json
68 {
69 // TODO: implement this; libraries are read-only from KiCad at the moment
70 return {};
71 },
72 [&]( const nlohmann::json aObj )
73 {
74 m_Tables.clear();
75
76 if( !aObj.is_array() )
77 return;
78
79 for( const nlohmann::json& entry : aObj )
80 {
81 if( entry.empty() || !entry.is_object() )
82 continue;
83
84 DATABASE_LIB_TABLE table;
85
86 table.name = entry["name"].get<std::string>();
87 table.table = entry["table"].get<std::string>();
88 table.key_col = entry["key"].get<std::string>();
89 table.symbols_col = entry["symbols"].get<std::string>();
90 table.footprints_col = entry["footprints"].get<std::string>();
91
92 // Sanitize library display names; currently only `/` is removed because we
93 // use it as a separator and allow it in symbol names.
94 alg::delete_matching( table.name, '/' );
95
96 if( entry.contains( "properties" ) && entry["properties"].is_object() )
97 {
98 const nlohmann::json& propJson = entry["properties"];
99
100 table.properties.description =
101 fetchOrDefault<std::string>( propJson, "description" );
102
103 table.properties.footprint_filters =
104 fetchOrDefault<std::string>( propJson, "footprint_filters" );
105
106 table.properties.keywords =
107 fetchOrDefault<std::string>( propJson, "keywords" );
108
109 table.properties.exclude_from_bom =
110 fetchOrDefault<std::string>( propJson, "exclude_from_bom" );
111
112 table.properties.exclude_from_board =
113 fetchOrDefault<std::string>( propJson, "exclude_from_board" );
114
115 table.properties.exclude_from_sim =
116 fetchOrDefault<std::string>( propJson, "exclude_from_sim" );
117 }
118
119 if( entry.contains( "fields" ) && entry["fields"].is_array() )
120 {
121 for( const nlohmann::json& fieldJson : entry["fields"] )
122 {
123 if( fieldJson.empty() || !fieldJson.is_object() )
124 continue;
125
126 table.fields.emplace_back( DATABASE_FIELD_MAPPING(
127 fetchOrDefault<std::string>( fieldJson, "column" ),
128 fetchOrDefault<std::string>( fieldJson, "name" ),
129 fetchOrDefault<bool>( fieldJson, "visible_on_add" ),
130 fetchOrDefault<bool>( fieldJson, "visible_in_chooser" ),
131 fetchOrDefault<bool>( fieldJson, "show_name" ),
132 fetchOrDefault<bool>( fieldJson, "inherit_properties" ) ) );
133 }
134 }
135
136 m_Tables.emplace_back( std::move( table ) );
137 }
138 },
139 {} ) );
140
141 m_params.emplace_back( new PARAM<int>( "cache.max_size", &m_Cache.max_size, 256 ) );
142
143 m_params.emplace_back( new PARAM<int>( "cache.max_age", &m_Cache.max_age, 10 ) );
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
182{
184}
const char * name
Definition: DXF_plotter.cpp:59
DATABASE_LIB_SETTINGS(const std::string &aFilename)
wxString getFileExt() const override
std::vector< DATABASE_LIB_TABLE > m_Tables
std::vector< PARAM_BASE * > m_params
The list of parameters (owned by this object)
Like a normal param, but with custom getter and setter functions.
Definition: parameters.h:295
const int dblibSchemaVersion
static const std::string DatabaseLibraryFileExtension
SETTINGS_LOC
Definition: json_settings.h:54
@ NONE
No connection to this item.
DATABASE_FIELD_MAPPING(std::string aColumn, std::string aName, bool aVisibleOnAdd, bool aVisibleInChooser, bool aShowName, bool aInheritProperties)
std::string connection_string
Definition of file extensions used in Kicad.