Do not allow spaces in component name (value field) in component library editor, in dialogs (Edit field, Create component).

In component libraries, a space in name breaks the library, and is not allowed.
This commit is contained in:
jean-pierre charras 2016-08-19 18:42:39 +02:00
parent 7ea120c2a1
commit c0b1e4cd38
6 changed files with 29 additions and 8 deletions

View File

@ -878,7 +878,7 @@ void DIALOG_EDIT_COMPONENT_IN_SCHEMATIC::copySelectedFieldToPanel()
// may only delete user defined fields // may only delete user defined fields
deleteFieldButton->Enable( fieldNdx >= MANDATORY_FIELDS ); deleteFieldButton->Enable( fieldNdx >= MANDATORY_FIELDS );
fieldValueTextCtrl->SetValidator( SCH_FIELD_VALIDATOR( field.GetId() ) ); fieldValueTextCtrl->SetValidator( SCH_FIELD_VALIDATOR( false, field.GetId() ) );
fieldValueTextCtrl->SetValue( field.GetText() ); fieldValueTextCtrl->SetValue( field.GetText() );
m_show_datasheet_button->Enable( fieldNdx == DATASHEET || fieldNdx == FOOTPRINT ); m_show_datasheet_button->Enable( fieldNdx == DATASHEET || fieldNdx == FOOTPRINT );

View File

@ -674,7 +674,7 @@ void DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB::copySelectedFieldToPanel()
// if fieldNdx == REFERENCE, VALUE, then disable delete button // if fieldNdx == REFERENCE, VALUE, then disable delete button
deleteFieldButton->Enable( fieldNdx >= MANDATORY_FIELDS ); deleteFieldButton->Enable( fieldNdx >= MANDATORY_FIELDS );
fieldValueTextCtrl->SetValidator( SCH_FIELD_VALIDATOR( field.GetId() ) ); fieldValueTextCtrl->SetValidator( SCH_FIELD_VALIDATOR( true, field.GetId() ) );
fieldValueTextCtrl->SetValue( field.GetText() ); fieldValueTextCtrl->SetValue( field.GetText() );
textSizeTextCtrl->SetValue( EDA_GRAPHIC_TEXT_CTRL::FormatSize( g_UserUnit, field.GetSize().x ) ); textSizeTextCtrl->SetValue( EDA_GRAPHIC_TEXT_CTRL::FormatSize( g_UserUnit, field.GetSize().x ) );

View File

@ -102,7 +102,10 @@ void DIALOG_EDIT_ONE_FIELD::init()
wxString msg; wxString msg;
m_TextValue->SetFocus(); m_TextValue->SetFocus();
m_TextValue->SetValidator( SCH_FIELD_VALIDATOR( m_fieldId, &m_text ) ); SCH_BASE_FRAME* parent = static_cast<SCH_BASE_FRAME*>( GetParent() );
m_TextValue->SetValidator( SCH_FIELD_VALIDATOR(
parent->IsType( FRAME_SCH_LIB_EDITOR ),
m_fieldId, &m_text ) );
// Disable options for graphic text editing which are not needed for fields. // Disable options for graphic text editing which are not needed for fields.
m_CommonConvert->Show( false ); m_CommonConvert->Show( false );

View File

@ -2,7 +2,7 @@
* This program source code file is part of KiCad, a free EDA CAD application. * This program source code file is part of KiCad, a free EDA CAD application.
* *
* Copyright (C) 2009 Wayne Stambaugh <stambaughw@verizon.net> * Copyright (C) 2009 Wayne Stambaugh <stambaughw@verizon.net>
* Copyright (C) 2014 KiCad Developers, see CHANGELOG.TXT for contributors. * Copyright (C) 2016 KiCad Developers, see AUTHORS.txt for contributors.
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License * modify it under the terms of the GNU General Public License
@ -23,10 +23,15 @@
*/ */
#include <dialog_lib_new_component.h> #include <dialog_lib_new_component.h>
#include <sch_validators.h>
#include <template_fieldnames.h>
DIALOG_LIB_NEW_COMPONENT::DIALOG_LIB_NEW_COMPONENT( wxWindow* parent ) : DIALOG_LIB_NEW_COMPONENT::DIALOG_LIB_NEW_COMPONENT( wxWindow* parent ) :
DIALOG_LIB_NEW_COMPONENT_BASE( parent ) DIALOG_LIB_NEW_COMPONENT_BASE( parent )
{ {
m_textName->SetValidator( SCH_FIELD_VALIDATOR( true, VALUE ) );
m_textReference->SetValidator( SCH_FIELD_VALIDATOR( true, REFERENCE ) );
// initial focus should be on first editable field. // initial focus should be on first editable field.
m_textName->SetFocus(); m_textName->SetFocus();

View File

@ -30,10 +30,12 @@
#include <sch_validators.h> #include <sch_validators.h>
#include <template_fieldnames.h> #include <template_fieldnames.h>
SCH_FIELD_VALIDATOR::SCH_FIELD_VALIDATOR( int aFieldId, wxString* aValue ) : SCH_FIELD_VALIDATOR::SCH_FIELD_VALIDATOR( bool aIsCmplibEditor,
int aFieldId, wxString* aValue ) :
wxTextValidator( wxFILTER_EXCLUDE_CHAR_LIST, aValue ) wxTextValidator( wxFILTER_EXCLUDE_CHAR_LIST, aValue )
{ {
m_fieldId = aFieldId; m_fieldId = aFieldId;
m_isLibEditor = aIsCmplibEditor;
// Fields cannot contain carriage returns, line feeds, or tabs. // Fields cannot contain carriage returns, line feeds, or tabs.
wxString excludes( "\r\n\t" ); wxString excludes( "\r\n\t" );
@ -41,6 +43,8 @@ SCH_FIELD_VALIDATOR::SCH_FIELD_VALIDATOR( int aFieldId, wxString* aValue ) :
// The reference field cannot contain spaces. // The reference field cannot contain spaces.
if( aFieldId == REFERENCE ) if( aFieldId == REFERENCE )
excludes += " "; excludes += " ";
else if( aFieldId == VALUE && m_isLibEditor )
excludes += " ";
long style = GetStyle(); long style = GetStyle();
@ -57,6 +61,7 @@ SCH_FIELD_VALIDATOR::SCH_FIELD_VALIDATOR( const SCH_FIELD_VALIDATOR& aValidator
wxTextValidator( aValidator ) wxTextValidator( aValidator )
{ {
m_fieldId = aValidator.m_fieldId; m_fieldId = aValidator.m_fieldId;
m_isLibEditor = aValidator.m_isLibEditor;
} }
@ -93,6 +98,8 @@ bool SCH_FIELD_VALIDATOR::Validate( wxWindow *aParent )
else if( HasFlag( wxFILTER_EXCLUDE_CHAR_LIST ) && ContainsExcludedCharacters( val ) ) else if( HasFlag( wxFILTER_EXCLUDE_CHAR_LIST ) && ContainsExcludedCharacters( val ) )
{ {
wxArrayString whiteSpace; wxArrayString whiteSpace;
bool spaceIllegal = ( m_fieldId == REFERENCE ) ||
( m_fieldId == VALUE && m_isLibEditor );
if( val.Find( '\r' ) != wxNOT_FOUND ) if( val.Find( '\r' ) != wxNOT_FOUND )
whiteSpace.Add( _( "carriage return" ) ); whiteSpace.Add( _( "carriage return" ) );
@ -100,7 +107,7 @@ bool SCH_FIELD_VALIDATOR::Validate( wxWindow *aParent )
whiteSpace.Add( _( "line feed" ) ); whiteSpace.Add( _( "line feed" ) );
if( val.Find( '\t' ) != wxNOT_FOUND ) if( val.Find( '\t' ) != wxNOT_FOUND )
whiteSpace.Add( _( "tab" ) ); whiteSpace.Add( _( "tab" ) );
if( (m_fieldId == REFERENCE) && (val.Find( ' ' ) != wxNOT_FOUND) ) if( spaceIllegal && (val.Find( ' ' ) != wxNOT_FOUND) )
whiteSpace.Add( _( "space" ) ); whiteSpace.Add( _( "space" ) );
wxString badChars; wxString badChars;

View File

@ -38,13 +38,19 @@
* *
* is the text control validator used for validating the text allowed in library and * is the text control validator used for validating the text allowed in library and
* schematic component fields. * schematic component fields.
* Note
* Reference field does not accept spaces
* Value field does not accept spaces in Component Library Editor, because in .lib component
* libraries, the value field is the component name in lib, and spaces are not allowed
* in component names in lib
*/ */
class SCH_FIELD_VALIDATOR : public wxTextValidator class SCH_FIELD_VALIDATOR : public wxTextValidator
{ {
int m_fieldId; int m_fieldId;
bool m_isLibEditor;
public: public:
SCH_FIELD_VALIDATOR( int aFieldId, wxString* aValue = NULL ); SCH_FIELD_VALIDATOR( bool aIsCmplibEditor, int aFieldId, wxString* aValue = NULL );
SCH_FIELD_VALIDATOR( const SCH_FIELD_VALIDATOR& aValidator ); SCH_FIELD_VALIDATOR( const SCH_FIELD_VALIDATOR& aValidator );