mirror of
https://gitlab.com/kicad/code/kicad.git
synced 2025-09-15 10:43:15 +02:00
254 lines
8.4 KiB
C++
254 lines
8.4 KiB
C++
|
/*
|
||
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
||
|
*
|
||
|
* Copyright (C) 2023 Mike Williams <mike@mikebwilliams.com>
|
||
|
* Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors.
|
||
|
*
|
||
|
* This program is free software: you can redistribute it and/or modify it
|
||
|
* under the terms of the GNU General Public License as published by the
|
||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||
|
* option) any later version.
|
||
|
*
|
||
|
* This program is distributed in the hope that it will be useful, but
|
||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
|
* General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU General Public License along
|
||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
|
*/
|
||
|
|
||
|
#include <settings/bom_settings.h>
|
||
|
#include <nlohmann/json.hpp>
|
||
|
#include <wx/translation.h>
|
||
|
|
||
|
namespace nlohmann
|
||
|
{
|
||
|
template <>
|
||
|
struct adl_serializer<wxString>
|
||
|
{
|
||
|
static void to_json( json& j, const wxString& s ) { j = s.ToUTF8(); }
|
||
|
|
||
|
static void from_json( const json& j, wxString& s )
|
||
|
{
|
||
|
s = wxString::FromUTF8( j.get<std::string>().c_str() );
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
|
||
|
|
||
|
// Implementations for BOM_FMT_PRESET
|
||
|
bool BOM_FIELD::operator==( const BOM_FIELD& rhs ) const
|
||
|
{
|
||
|
return this->name == rhs.name && this->label == rhs.label && this->show == rhs.show
|
||
|
&& this->groupBy == rhs.groupBy;
|
||
|
}
|
||
|
|
||
|
|
||
|
const bool operator!=( const BOM_FIELD& lhs, const BOM_FIELD& rhs )
|
||
|
{
|
||
|
return !( lhs == rhs );
|
||
|
}
|
||
|
|
||
|
|
||
|
const bool operator<( const BOM_FIELD& lhs, const BOM_FIELD& rhs )
|
||
|
{
|
||
|
return lhs.name < rhs.name;
|
||
|
}
|
||
|
|
||
|
|
||
|
void to_json( nlohmann::json& j, const BOM_FIELD& f )
|
||
|
{
|
||
|
j = nlohmann::json{
|
||
|
{ "name", f.name }, { "label", f.label }, { "show", f.show }, { "group_by", f.groupBy }
|
||
|
};
|
||
|
}
|
||
|
|
||
|
|
||
|
void from_json( const nlohmann::json& j, BOM_FIELD& f )
|
||
|
{
|
||
|
j.at( "name" ).get_to( f.name );
|
||
|
j.at( "label" ).get_to( f.label );
|
||
|
j.at( "show" ).get_to( f.show );
|
||
|
j.at( "group_by" ).get_to( f.groupBy );
|
||
|
}
|
||
|
|
||
|
|
||
|
const bool operator!=( const BOM_PRESET& lhs, const BOM_PRESET& rhs )
|
||
|
{
|
||
|
return !( lhs == rhs );
|
||
|
}
|
||
|
|
||
|
|
||
|
const bool operator<( const BOM_PRESET& lhs, const BOM_PRESET& rhs )
|
||
|
{
|
||
|
return lhs.name < rhs.name;
|
||
|
}
|
||
|
|
||
|
|
||
|
void to_json( nlohmann::json& j, const BOM_PRESET& p )
|
||
|
{
|
||
|
j = nlohmann::json{ { "name", p.name },
|
||
|
{ "sort_field", p.sortField },
|
||
|
{ "sort_asc", p.sortAsc },
|
||
|
{ "filter_string", p.filterString },
|
||
|
{ "group_symbols", p.groupSymbols } };
|
||
|
|
||
|
if( p.fieldsOrdered.size() > 0 )
|
||
|
j["fields_ordered"] = p.fieldsOrdered;
|
||
|
}
|
||
|
|
||
|
|
||
|
void from_json( const nlohmann::json& j, BOM_PRESET& f )
|
||
|
{
|
||
|
j.at( "name" ).get_to( f.name );
|
||
|
j.at( "fields_ordered" ).get_to( f.fieldsOrdered );
|
||
|
j.at( "sort_field" ).get_to( f.sortField );
|
||
|
j.at( "sort_asc" ).get_to( f.sortAsc );
|
||
|
j.at( "filter_string" ).get_to( f.filterString );
|
||
|
j.at( "group_symbols" ).get_to( f.groupSymbols );
|
||
|
}
|
||
|
|
||
|
|
||
|
// Implementations for BOM_PRESET
|
||
|
bool BOM_PRESET::operator==( const BOM_PRESET& rhs ) const
|
||
|
{
|
||
|
return this->name == rhs.name && this->readOnly == rhs.readOnly
|
||
|
&& this->fieldsOrdered == rhs.fieldsOrdered && this->sortField == rhs.sortField
|
||
|
&& this->sortAsc == rhs.sortAsc && this->groupSymbols == rhs.groupSymbols;
|
||
|
}
|
||
|
|
||
|
|
||
|
BOM_PRESET BOM_PRESET::GroupedByValue()
|
||
|
{
|
||
|
BOM_PRESET p = { .name = _HKI( "Grouped By Value" ),
|
||
|
.readOnly = true,
|
||
|
.sortField = _( "Reference" ),
|
||
|
.sortAsc = true,
|
||
|
.groupSymbols = true };
|
||
|
|
||
|
p.fieldsOrdered = std::vector<BOM_FIELD>( {
|
||
|
( BOM_FIELD ){
|
||
|
.name = "Reference", .label = "Reference", .show = true, .groupBy = false },
|
||
|
( BOM_FIELD ){
|
||
|
.name = "Value", .label = "Value", .show = true, .groupBy = true },
|
||
|
( BOM_FIELD ){
|
||
|
.name = "Datasheet", .label = "Datasheet", .show = true, .groupBy = false },
|
||
|
( BOM_FIELD ){
|
||
|
.name = "Footprint", .label = "Footprint", .show = true, .groupBy = false },
|
||
|
( BOM_FIELD ){
|
||
|
.name = "Quantity", .label = "Qty", .show = true, .groupBy = false },
|
||
|
} );
|
||
|
|
||
|
return p;
|
||
|
}
|
||
|
|
||
|
|
||
|
BOM_PRESET BOM_PRESET::GroupedByValueFootprint()
|
||
|
{
|
||
|
BOM_PRESET p = { .name = _HKI( "Grouped By Value and Footprint" ),
|
||
|
.readOnly = true,
|
||
|
.sortField = _( "Reference" ),
|
||
|
.sortAsc = true,
|
||
|
.groupSymbols = true };
|
||
|
|
||
|
p.fieldsOrdered = std::vector<BOM_FIELD>( {
|
||
|
( BOM_FIELD ){
|
||
|
.name = "Reference", .label = "Reference", .show = true, .groupBy = false },
|
||
|
( BOM_FIELD ){
|
||
|
.name = "Value", .label = "Value", .show = true, .groupBy = true },
|
||
|
( BOM_FIELD ){
|
||
|
.name = "Datasheet", .label = "Datasheet", .show = true, .groupBy = false },
|
||
|
( BOM_FIELD ){
|
||
|
.name = "Footprint", .label = "Footprint", .show = true, .groupBy = true },
|
||
|
( BOM_FIELD ){
|
||
|
.name = "Quantity", .label = "Qty", .show = true, .groupBy = false },
|
||
|
} );
|
||
|
|
||
|
return p;
|
||
|
}
|
||
|
|
||
|
|
||
|
//Implementations for BOM_FMT_PRESET
|
||
|
bool BOM_FMT_PRESET::operator==( const BOM_FMT_PRESET& rhs ) const
|
||
|
{
|
||
|
return this->name == rhs.name && this->readOnly == rhs.readOnly
|
||
|
&& this->fieldDelimiter == rhs.fieldDelimiter
|
||
|
&& this->stringDelimiter == rhs.stringDelimiter && this->refDelimiter == rhs.refDelimiter
|
||
|
&& this->refRangeDelimiter == rhs.refRangeDelimiter && this->keepTabs == rhs.keepTabs
|
||
|
&& this->keepLineBreaks == rhs.keepLineBreaks;
|
||
|
}
|
||
|
|
||
|
|
||
|
const bool operator!=( const BOM_FMT_PRESET& lhs, const BOM_FMT_PRESET& rhs )
|
||
|
{
|
||
|
return !( lhs == rhs );
|
||
|
}
|
||
|
|
||
|
|
||
|
const bool operator<( const BOM_FMT_PRESET& lhs, const BOM_FMT_PRESET& rhs )
|
||
|
{
|
||
|
return lhs.name < rhs.name;
|
||
|
}
|
||
|
|
||
|
|
||
|
void to_json( nlohmann::json& j, const BOM_FMT_PRESET& p )
|
||
|
{
|
||
|
j = nlohmann::json{ { "name", p.name },
|
||
|
{ "field_delimiter", p.fieldDelimiter },
|
||
|
{ "string_delimiter", p.stringDelimiter },
|
||
|
{ "ref_delimiter", p.refDelimiter },
|
||
|
{ "ref_range_delimiter", p.refRangeDelimiter },
|
||
|
{ "keep_tabs", p.keepTabs },
|
||
|
{ "keep_line_breaks", p.keepLineBreaks } };
|
||
|
}
|
||
|
|
||
|
|
||
|
void from_json( const nlohmann::json& j, BOM_FMT_PRESET& f )
|
||
|
{
|
||
|
j.at( "name" ).get_to( f.name );
|
||
|
j.at( "field_delimiter" ).get_to( f.fieldDelimiter );
|
||
|
j.at( "string_delimiter" ).get_to( f.stringDelimiter );
|
||
|
j.at( "ref_delimiter" ).get_to( f.refDelimiter );
|
||
|
j.at( "ref_range_delimiter" ).get_to( f.refRangeDelimiter );
|
||
|
j.at( "keep_tabs" ).get_to( f.keepTabs );
|
||
|
j.at( "keep_line_breaks" ).get_to( f.keepLineBreaks );
|
||
|
}
|
||
|
|
||
|
|
||
|
BOM_FMT_PRESET BOM_FMT_PRESET::CSV()
|
||
|
{
|
||
|
return ( BOM_FMT_PRESET ){ .name = _HKI( "CSV" ),
|
||
|
.readOnly = true,
|
||
|
.fieldDelimiter = wxS( "," ),
|
||
|
.stringDelimiter = wxT( "\"" ),
|
||
|
.refDelimiter = wxT( "," ),
|
||
|
.refRangeDelimiter = wxT( "" ),
|
||
|
.keepTabs = false,
|
||
|
.keepLineBreaks = false };
|
||
|
}
|
||
|
|
||
|
BOM_FMT_PRESET BOM_FMT_PRESET::TSV()
|
||
|
{
|
||
|
return ( BOM_FMT_PRESET ){ .name = _HKI( "TSV" ),
|
||
|
.readOnly = true,
|
||
|
.fieldDelimiter = wxS( "\t" ),
|
||
|
.stringDelimiter = wxT( "" ),
|
||
|
.refDelimiter = wxT( "," ),
|
||
|
.refRangeDelimiter = wxT( "" ),
|
||
|
.keepTabs = false,
|
||
|
.keepLineBreaks = false };
|
||
|
}
|
||
|
|
||
|
BOM_FMT_PRESET BOM_FMT_PRESET::Semicolons()
|
||
|
{
|
||
|
return ( BOM_FMT_PRESET ){ .name = _HKI( "Semicolons" ),
|
||
|
.readOnly = true,
|
||
|
.fieldDelimiter = wxS( ";" ),
|
||
|
.stringDelimiter = wxT( "'" ),
|
||
|
.refDelimiter = wxT( "," ),
|
||
|
.refRangeDelimiter = wxT( "" ),
|
||
|
.keepTabs = false,
|
||
|
.keepLineBreaks = false };
|
||
|
}
|