2014-10-19 22:17:43 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
|
2020-03-10 18:46:57 +00:00
|
|
|
* Copyright (C) 2015-2020 KiCad Developers, see CHANGELOG.TXT for contributors.
|
2014-10-19 22:17:43 +00:00
|
|
|
*
|
|
|
|
* 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 2
|
|
|
|
* 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, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
2010-06-17 16:30:10 +00:00
|
|
|
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <template_fieldnames.h>
|
|
|
|
#include <dsnlexer.h>
|
|
|
|
#include <fctsys.h>
|
|
|
|
#include <macros.h>
|
2018-01-25 23:49:04 +00:00
|
|
|
#include <pgm_base.h>
|
2010-06-17 16:30:10 +00:00
|
|
|
|
2011-01-17 17:26:19 +00:00
|
|
|
using namespace TFIELD_T;
|
2010-06-17 16:30:10 +00:00
|
|
|
|
2018-01-25 23:49:04 +00:00
|
|
|
|
2015-04-26 16:32:16 +00:00
|
|
|
const wxString TEMPLATE_FIELDNAME::GetDefaultFieldName( int aFieldNdx )
|
2010-06-17 16:30:10 +00:00
|
|
|
{
|
2018-01-25 23:49:04 +00:00
|
|
|
static void* locale = nullptr;
|
|
|
|
static wxString referenceDefault;
|
|
|
|
static wxString valueDefault;
|
|
|
|
static wxString footprintDefault;
|
|
|
|
static wxString datasheetDefault;
|
|
|
|
static wxString fieldDefault;
|
|
|
|
|
|
|
|
// Fetching translations can take a surprising amount of time when loading libraries,
|
|
|
|
// so only do it when necessary.
|
|
|
|
if( Pgm().GetLocale() != locale )
|
|
|
|
{
|
|
|
|
referenceDefault = _( "Reference" );
|
|
|
|
valueDefault = _( "Value" );
|
|
|
|
footprintDefault = _( "Footprint" );
|
|
|
|
datasheetDefault = _( "Datasheet" );
|
2020-03-06 12:05:21 +00:00
|
|
|
fieldDefault = _( "Field%d" );
|
2018-01-25 23:49:04 +00:00
|
|
|
locale = Pgm().GetLocale();
|
|
|
|
}
|
|
|
|
|
2020-03-06 12:05:21 +00:00
|
|
|
// Fixed values for the mandatory fields
|
2015-04-26 16:32:16 +00:00
|
|
|
switch( aFieldNdx )
|
|
|
|
{
|
2020-03-06 12:05:21 +00:00
|
|
|
case REFERENCE: return referenceDefault; // The component reference, R1, C1, etc.
|
|
|
|
case VALUE: return valueDefault; // The component value + name
|
|
|
|
case FOOTPRINT: return footprintDefault; // The footprint for use with Pcbnew
|
|
|
|
case DATASHEET: return datasheetDefault; // Link to a datasheet for component
|
|
|
|
default: return wxString::Format( fieldDefault, aFieldNdx );
|
2010-06-17 16:30:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
void TEMPLATE_FIELDNAME::Format( OUTPUTFORMATTER* out, int nestLevel ) const
|
2010-06-17 16:30:10 +00:00
|
|
|
{
|
2011-02-02 19:41:35 +00:00
|
|
|
out->Print( nestLevel, "(field (name %s)", out->Quotew( m_Name ).c_str() );
|
2010-06-17 16:30:10 +00:00
|
|
|
|
|
|
|
if( m_Visible )
|
|
|
|
out->Print( 0, " visible" );
|
|
|
|
|
2018-08-08 20:57:53 +00:00
|
|
|
if( m_URL )
|
|
|
|
out->Print( 0, " url" );
|
|
|
|
|
2010-06-17 16:30:10 +00:00
|
|
|
out->Print( 0, ")\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
void TEMPLATE_FIELDNAME::Parse( TEMPLATE_FIELDNAMES_LEXER* in )
|
2010-06-17 16:30:10 +00:00
|
|
|
{
|
2011-01-17 17:26:19 +00:00
|
|
|
T tok;
|
2010-06-17 16:30:10 +00:00
|
|
|
|
2010-06-18 16:00:16 +00:00
|
|
|
in->NeedLEFT(); // begin (name ...)
|
2010-06-17 16:30:10 +00:00
|
|
|
|
2010-08-09 02:03:16 +00:00
|
|
|
if( (tok = in->NextTok()) != T_name )
|
2010-06-17 16:30:10 +00:00
|
|
|
in->Expecting( T_name );
|
|
|
|
|
2010-06-18 16:00:16 +00:00
|
|
|
in->NeedSYMBOLorNUMBER();
|
2010-06-17 16:30:10 +00:00
|
|
|
|
2011-02-02 15:31:48 +00:00
|
|
|
m_Name = FROM_UTF8( in->CurText() );
|
2010-06-17 16:30:10 +00:00
|
|
|
|
2010-06-18 16:00:16 +00:00
|
|
|
in->NeedRIGHT(); // end (name ...)
|
2010-06-17 16:30:10 +00:00
|
|
|
|
2010-08-09 02:03:16 +00:00
|
|
|
while( (tok = in->NextTok() ) != T_RIGHT && tok != T_EOF )
|
2010-06-17 16:30:10 +00:00
|
|
|
{
|
2010-06-18 16:00:16 +00:00
|
|
|
// "visible" has no '(' prefix, "value" does, so T_LEFT is optional.
|
2010-06-17 16:30:10 +00:00
|
|
|
if( tok == T_LEFT )
|
2010-08-09 02:03:16 +00:00
|
|
|
tok = in->NextTok();
|
2010-06-17 16:30:10 +00:00
|
|
|
|
|
|
|
switch( tok )
|
|
|
|
{
|
|
|
|
case T_value:
|
2018-08-08 20:57:53 +00:00
|
|
|
// older format; silently skip
|
2010-06-18 16:00:16 +00:00
|
|
|
in->NeedSYMBOLorNUMBER();
|
|
|
|
in->NeedRIGHT();
|
2010-06-17 16:30:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_visible:
|
|
|
|
m_Visible = true;
|
|
|
|
break;
|
|
|
|
|
2018-08-08 20:57:53 +00:00
|
|
|
case T_url:
|
|
|
|
m_URL = true;
|
|
|
|
break;
|
|
|
|
|
2010-06-17 16:30:10 +00:00
|
|
|
default:
|
2018-08-08 20:57:53 +00:00
|
|
|
in->Expecting( "value|url|visible" );
|
2010-06-17 16:30:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-10 18:46:57 +00:00
|
|
|
void TEMPLATES::Format( OUTPUTFORMATTER* out, int nestLevel, bool aGlobal ) const
|
2010-06-17 16:30:10 +00:00
|
|
|
{
|
2010-08-09 02:03:16 +00:00
|
|
|
// We'll keep this general, and include the \n, even though the only known
|
|
|
|
// use at this time will not want the newlines or the indentation.
|
2010-06-17 16:30:10 +00:00
|
|
|
out->Print( nestLevel, "(templatefields" );
|
2014-10-19 22:17:43 +00:00
|
|
|
|
2020-03-10 18:46:57 +00:00
|
|
|
const TEMPLATE_FIELDNAMES& source = aGlobal ? m_globals : m_project;
|
|
|
|
|
|
|
|
for( const TEMPLATE_FIELDNAME& temp : source )
|
|
|
|
temp.Format( out, nestLevel+1 );
|
2014-10-19 22:17:43 +00:00
|
|
|
|
2010-06-17 16:30:10 +00:00
|
|
|
out->Print( 0, ")\n" );
|
|
|
|
}
|
|
|
|
|
2011-01-17 17:26:19 +00:00
|
|
|
|
2020-03-10 18:46:57 +00:00
|
|
|
void TEMPLATES::Parse( TEMPLATE_FIELDNAMES_LEXER* in, bool aGlobal )
|
2010-06-17 16:30:10 +00:00
|
|
|
{
|
2011-01-17 17:26:19 +00:00
|
|
|
T tok;
|
2010-06-17 16:30:10 +00:00
|
|
|
|
2012-10-08 19:34:04 +00:00
|
|
|
while( ( tok = in->NextTok() ) != T_RIGHT && tok != T_EOF )
|
2010-06-17 16:30:10 +00:00
|
|
|
{
|
|
|
|
if( tok == T_LEFT )
|
2010-08-09 02:03:16 +00:00
|
|
|
tok = in->NextTok();
|
2010-06-17 16:30:10 +00:00
|
|
|
|
|
|
|
switch( tok )
|
|
|
|
{
|
|
|
|
case T_templatefields: // a token indicating class TEMPLATES.
|
|
|
|
|
2010-08-09 02:03:16 +00:00
|
|
|
// Be flexible regarding the starting point of the TEMPLATE_FIELDNAMES_LEXER
|
2010-06-17 16:30:10 +00:00
|
|
|
// stream. Caller may not have read the first two tokens out of the
|
|
|
|
// stream: T_LEFT and T_templatefields, so ignore them if seen here.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_field:
|
|
|
|
{
|
|
|
|
// instantiate on stack, so if exception is thrown,
|
|
|
|
// destructor runs
|
|
|
|
TEMPLATE_FIELDNAME field;
|
|
|
|
|
|
|
|
field.Parse( in );
|
|
|
|
|
|
|
|
// add the field
|
2020-03-10 18:46:57 +00:00
|
|
|
AddTemplateFieldName( field, aGlobal );
|
2010-06-17 16:30:10 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2011-01-19 20:46:07 +00:00
|
|
|
in->Unexpected( in->CurText() );
|
2010-06-17 16:30:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-06 09:15:57 +00:00
|
|
|
/*
|
|
|
|
* Flatten project and global templates into a single list. (Project templates take
|
|
|
|
* precedence.)
|
|
|
|
*/
|
2020-03-10 18:46:57 +00:00
|
|
|
void TEMPLATES::resolveTemplates()
|
|
|
|
{
|
|
|
|
m_resolved = m_project;
|
|
|
|
|
|
|
|
// Note: order N^2 algorithm. Would need changing if fieldname template sets ever
|
|
|
|
// get large.
|
|
|
|
|
|
|
|
for( const TEMPLATE_FIELDNAME& global : m_globals )
|
|
|
|
{
|
|
|
|
for( const TEMPLATE_FIELDNAME& project : m_project )
|
|
|
|
{
|
|
|
|
if( global.m_Name == project.m_Name )
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_resolved.push_back( global );
|
|
|
|
}
|
|
|
|
|
|
|
|
m_resolvedDirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TEMPLATES::AddTemplateFieldName( const TEMPLATE_FIELDNAME& aFieldName, bool aGlobal )
|
2010-06-17 16:30:10 +00:00
|
|
|
{
|
|
|
|
// Ensure that the template fieldname does not match a fixed fieldname.
|
2020-04-06 09:15:57 +00:00
|
|
|
for( int i = 0; i < MANDATORY_FIELDS; ++i )
|
2010-06-17 16:30:10 +00:00
|
|
|
{
|
2014-10-19 22:17:43 +00:00
|
|
|
if( TEMPLATE_FIELDNAME::GetDefaultFieldName( i ) == aFieldName.m_Name )
|
2020-03-10 18:46:57 +00:00
|
|
|
return;
|
2010-06-17 16:30:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-10 18:46:57 +00:00
|
|
|
TEMPLATE_FIELDNAMES& target = aGlobal ? m_globals : m_project;
|
|
|
|
|
2010-06-17 16:30:10 +00:00
|
|
|
// ensure uniqueness, overwrite any template fieldname by the same name.
|
2020-03-10 18:46:57 +00:00
|
|
|
for( TEMPLATE_FIELDNAME& temp : target )
|
2010-06-17 16:30:10 +00:00
|
|
|
{
|
2020-03-10 18:46:57 +00:00
|
|
|
if( temp.m_Name == aFieldName.m_Name )
|
2010-06-17 16:30:10 +00:00
|
|
|
{
|
2020-03-10 18:46:57 +00:00
|
|
|
temp = aFieldName;
|
|
|
|
m_resolvedDirty = true;
|
|
|
|
return;
|
2010-06-17 16:30:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// the name is legal and not previously added to the config container, append
|
|
|
|
// it and return its index within the container.
|
2020-03-10 18:46:57 +00:00
|
|
|
target.push_back( aFieldName );
|
|
|
|
m_resolvedDirty = true;
|
|
|
|
}
|
2010-06-17 16:30:10 +00:00
|
|
|
|
2020-03-10 18:46:57 +00:00
|
|
|
|
|
|
|
void TEMPLATES::DeleteAllFieldNameTemplates( bool aGlobal )
|
|
|
|
{
|
|
|
|
if( aGlobal )
|
|
|
|
{
|
|
|
|
m_globals.clear();
|
|
|
|
m_resolved = m_project;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_project.clear();
|
|
|
|
m_resolved = m_globals;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_resolvedDirty = false;
|
2010-06-17 16:30:10 +00:00
|
|
|
}
|
|
|
|
|
2011-08-30 19:24:28 +00:00
|
|
|
|
2020-03-10 18:46:57 +00:00
|
|
|
const TEMPLATE_FIELDNAMES& TEMPLATES::GetTemplateFieldNames()
|
2011-08-30 19:24:28 +00:00
|
|
|
{
|
2020-03-10 18:46:57 +00:00
|
|
|
if( m_resolvedDirty )
|
|
|
|
resolveTemplates();
|
|
|
|
|
|
|
|
return m_resolved;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const TEMPLATE_FIELDNAMES& TEMPLATES::GetTemplateFieldNames( bool aGlobal )
|
|
|
|
{
|
|
|
|
if( aGlobal )
|
|
|
|
return m_globals;
|
|
|
|
else
|
|
|
|
return m_project;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const TEMPLATE_FIELDNAME* TEMPLATES::GetFieldName( const wxString& aName )
|
|
|
|
{
|
|
|
|
if( m_resolvedDirty )
|
|
|
|
resolveTemplates();
|
|
|
|
|
|
|
|
for( const TEMPLATE_FIELDNAME& field : m_resolved )
|
2011-08-30 19:24:28 +00:00
|
|
|
{
|
2018-08-08 20:57:53 +00:00
|
|
|
if( field.m_Name == aName )
|
|
|
|
return &field;
|
2011-08-30 19:24:28 +00:00
|
|
|
}
|
|
|
|
|
2018-08-08 20:57:53 +00:00
|
|
|
return nullptr;
|
2011-08-30 19:24:28 +00:00
|
|
|
}
|
|
|
|
|