Common: move SCH_FIELD_VALIDATORS to FIELD_VALIDATORS

Fields are now common across PCB/SCH
This commit is contained in:
Mike Williams 2023-06-13 10:10:48 -04:00
parent ad7d9ec956
commit ddafa2e75d
8 changed files with 222 additions and 222 deletions

View File

@ -31,6 +31,7 @@
#include <string_utils.h>
#include <confirm.h>
#include <validators.h>
#include <template_fieldnames.h>
#include <wx/grid.h>
#include <wx/textctrl.h>
@ -334,3 +335,171 @@ void KIUI::ValidatorTransferToWindowWithoutEvents( wxValidator& aValidator )
wxEventBlocker orient_update_blocker( ctrl, wxEVT_ANY );
aValidator.TransferToWindow();
}
FIELD_VALIDATOR::FIELD_VALIDATOR( bool aIsLibEditor, int aFieldId, wxString* aValue ) :
wxTextValidator( wxFILTER_EXCLUDE_CHAR_LIST, aValue )
{
m_fieldId = aFieldId;
m_isLibEditor = aIsLibEditor;
// Fields cannot contain carriage returns, line feeds, or tabs.
wxString excludes( wxT( "\r\n\t" ) );
// The reference and sheet name fields cannot contain spaces.
if( aFieldId == REFERENCE_FIELD )
{
excludes += wxT( " " );
}
else if( m_fieldId == SHEETNAME_V )
{
excludes += wxT( "/" );
}
long style = GetStyle();
// The reference, sheetname and sheetfilename fields cannot be empty.
if( aFieldId == REFERENCE_FIELD || aFieldId == SHEETNAME_V || aFieldId == SHEETFILENAME_V )
{
style |= wxFILTER_EMPTY;
}
SetStyle( style );
SetCharExcludes( excludes );
}
FIELD_VALIDATOR::FIELD_VALIDATOR( const FIELD_VALIDATOR& aValidator ) :
wxTextValidator( aValidator )
{
m_fieldId = aValidator.m_fieldId;
m_isLibEditor = aValidator.m_isLibEditor;
}
bool FIELD_VALIDATOR::Validate( wxWindow* aParent )
{
// If window is disabled, simply return
if( !m_validatorWindow->IsEnabled() )
return true;
wxTextEntry* const text = GetTextEntry();
if( !text )
return false;
wxString val( text->GetValue() );
wxString msg;
if( HasFlag( wxFILTER_EMPTY ) && val.empty() )
msg.Printf( _( "The value of the field cannot be empty." ) );
if( HasFlag( wxFILTER_EXCLUDE_CHAR_LIST ) && ContainsExcludedCharacters( val ) )
{
wxArrayString badCharsFound;
#if wxCHECK_VERSION( 3, 1, 3 )
for( const wxUniCharRef& excludeChar : GetCharExcludes() )
{
if( val.Find( excludeChar ) != wxNOT_FOUND )
{
if( excludeChar == '\r' )
badCharsFound.Add( _( "carriage return" ) );
else if( excludeChar == '\n' )
badCharsFound.Add( _( "line feed" ) );
else if( excludeChar == '\t' )
badCharsFound.Add( _( "tab" ) );
else if( excludeChar == ' ' )
badCharsFound.Add( _( "space" ) );
else
badCharsFound.Add( wxString::Format( wxT( "'%s'" ), excludeChar ) );
}
}
#else
for( const wxString& excludeChar : GetExcludes() )
{
if( val.Find( excludeChar ) != wxNOT_FOUND )
{
if( excludeChar == wxT( "\r" ) )
badCharsFound.Add( _( "carriage return" ) );
else if( excludeChar == wxT( "\n" ) )
badCharsFound.Add( _( "line feed" ) );
else if( excludeChar == wxT( "\t" ) )
badCharsFound.Add( _( "tab" ) );
else if( excludeChar == wxT( " " ) )
badCharsFound.Add( _( "space" ) );
else
badCharsFound.Add( wxString::Format( wxT( "'%s'" ), excludeChar ) );
}
}
#endif
wxString badChars;
for( size_t i = 0; i < badCharsFound.GetCount(); i++ )
{
if( !badChars.IsEmpty() )
{
if( badCharsFound.GetCount() == 2 )
{
badChars += _( " or " );
}
else
{
if( i < badCharsFound.GetCount() - 2 )
badChars += _( ", or " );
else
badChars += wxT( ", " );
}
}
badChars += badCharsFound.Item( i );
}
switch( m_fieldId )
{
case REFERENCE_FIELD:
msg.Printf( _( "The reference designator cannot contain %s character(s)." ), badChars );
break;
case VALUE_FIELD:
msg.Printf( _( "The value field cannot contain %s character(s)." ), badChars );
break;
case FOOTPRINT_FIELD:
msg.Printf( _( "The footprint field cannot contain %s character(s)." ), badChars );
break;
case DATASHEET_FIELD:
msg.Printf( _( "The datasheet field cannot contain %s character(s)." ), badChars );
break;
case SHEETNAME_V:
msg.Printf( _( "The sheet name cannot contain %s character(s)." ), badChars );
break;
case SHEETFILENAME_V:
msg.Printf( _( "The sheet filename cannot contain %s character(s)." ), badChars );
break;
default:
msg.Printf( _( "The field cannot contain %s character(s)." ), badChars );
break;
};
}
else if( m_fieldId == REFERENCE_FIELD && val.Contains( wxT( "${" ) ) )
{
msg.Printf( _( "The reference designator cannot contain text variable references" ) );
}
if( !msg.empty() )
{
m_validatorWindow->SetFocus();
wxMessageBox( msg, _( "Field Validation Error" ), wxOK | wxICON_EXCLAMATION, aParent );
return false;
}
return true;
}

View File

@ -168,7 +168,7 @@ void DIALOG_FIELD_PROPERTIES::init()
if( use_validator )
{
m_TextCtrl->SetValidator( SCH_FIELD_VALIDATOR( isSymbolEditor, m_fieldId, &m_text ) );
m_TextCtrl->SetValidator( FIELD_VALIDATOR( isSymbolEditor, m_fieldId, &m_text ) );
SetInitialFocus( m_TextCtrl );
m_StyledTextCtrl->Show( false );

View File

@ -58,8 +58,8 @@ DIALOG_LIB_NEW_SYMBOL::DIALOG_LIB_NEW_SYMBOL( EDA_DRAW_FRAME* aParent,
m_infoBar->ShowMessage( message );
}
m_textName->SetValidator( SCH_FIELD_VALIDATOR( true, VALUE_FIELD ) );
m_textReference->SetValidator( SCH_FIELD_VALIDATOR( true, REFERENCE_FIELD ) );
m_textName->SetValidator( FIELD_VALIDATOR( true, VALUE_FIELD ) );
m_textReference->SetValidator( FIELD_VALIDATOR( true, REFERENCE_FIELD ) );
m_pinTextPosition.SetValue( schIUScale.MilsToIU( DEFAULT_PIN_NAME_OFFSET ) );

View File

@ -78,7 +78,7 @@ DIALOG_LIB_SYMBOL_PROPERTIES::DIALOG_LIB_SYMBOL_PROPERTIES( SYMBOL_EDIT_FRAME* a
attr->SetEditor( new GRID_CELL_URL_EDITOR( this, Prj().SchSearchS() ) );
m_grid->SetAttr( DATASHEET_FIELD, FDC_VALUE, attr );
m_SymbolNameCtrl->SetValidator( SCH_FIELD_VALIDATOR( true, VALUE_FIELD ) );
m_SymbolNameCtrl->SetValidator( FIELD_VALIDATOR( true, VALUE_FIELD ) );
// Configure button logos
m_bpAdd->SetBitmap( KiBitmap( BITMAPS::small_plus ) );

View File

@ -125,12 +125,12 @@ private:
wxString m_symbolNetlist;
wxString m_curdir;
SCH_FIELD_VALIDATOR m_fieldNameValidator;
SCH_FIELD_VALIDATOR m_referenceValidator;
SCH_FIELD_VALIDATOR m_valueValidator;
SCH_FIELD_VALIDATOR m_urlValidator;
SCH_FIELD_VALIDATOR m_nonUrlValidator;
SCH_FIELD_VALIDATOR m_filepathValidator;
FIELD_VALIDATOR m_fieldNameValidator;
FIELD_VALIDATOR m_referenceValidator;
FIELD_VALIDATOR m_valueValidator;
FIELD_VALIDATOR m_urlValidator;
FIELD_VALIDATOR m_nonUrlValidator;
FIELD_VALIDATOR m_filepathValidator;
wxGridCellAttr* m_readOnlyAttr;
wxGridCellAttr* m_fieldNameAttr;

View File

@ -33,177 +33,6 @@
#include <sch_connection.h>
#include <sch_validators.h>
#include <project/net_settings.h>
#include <template_fieldnames.h>
SCH_FIELD_VALIDATOR::SCH_FIELD_VALIDATOR( bool aIsLibEditor, int aFieldId, wxString* aValue ) :
wxTextValidator( wxFILTER_EXCLUDE_CHAR_LIST, aValue )
{
m_fieldId = aFieldId;
m_isLibEditor = aIsLibEditor;
// Fields cannot contain carriage returns, line feeds, or tabs.
wxString excludes( wxT( "\r\n\t" ) );
// The reference and sheet name fields cannot contain spaces.
if( aFieldId == REFERENCE_FIELD )
{
excludes += wxT( " " );
}
else if( m_fieldId == SHEETNAME_V )
{
excludes += wxT( "/" );
}
long style = GetStyle();
// The reference, sheetname and sheetfilename fields cannot be empty.
if( aFieldId == REFERENCE_FIELD
|| aFieldId == SHEETNAME_V
|| aFieldId == SHEETFILENAME_V )
{
style |= wxFILTER_EMPTY;
}
SetStyle( style );
SetCharExcludes( excludes );
}
SCH_FIELD_VALIDATOR::SCH_FIELD_VALIDATOR( const SCH_FIELD_VALIDATOR& aValidator ) :
wxTextValidator( aValidator )
{
m_fieldId = aValidator.m_fieldId;
m_isLibEditor = aValidator.m_isLibEditor;
}
bool SCH_FIELD_VALIDATOR::Validate( wxWindow* aParent )
{
// If window is disabled, simply return
if( !m_validatorWindow->IsEnabled() )
return true;
wxTextEntry* const text = GetTextEntry();
if( !text )
return false;
wxString val( text->GetValue() );
wxString msg;
if( HasFlag( wxFILTER_EMPTY ) && val.empty() )
msg.Printf( _( "The value of the field cannot be empty." ) );
if( HasFlag( wxFILTER_EXCLUDE_CHAR_LIST ) && ContainsExcludedCharacters( val ) )
{
wxArrayString badCharsFound;
#if wxCHECK_VERSION( 3, 1, 3 )
for( const wxUniCharRef& excludeChar : GetCharExcludes() )
{
if( val.Find( excludeChar ) != wxNOT_FOUND )
{
if( excludeChar == '\r' )
badCharsFound.Add( _( "carriage return" ) );
else if( excludeChar == '\n' )
badCharsFound.Add( _( "line feed" ) );
else if( excludeChar == '\t' )
badCharsFound.Add( _( "tab" ) );
else if( excludeChar == ' ' )
badCharsFound.Add( _( "space" ) );
else
badCharsFound.Add( wxString::Format( wxT( "'%s'" ), excludeChar ) );
}
}
#else
for( const wxString& excludeChar : GetExcludes() )
{
if( val.Find( excludeChar ) != wxNOT_FOUND )
{
if( excludeChar == wxT( "\r" ) )
badCharsFound.Add( _( "carriage return" ) );
else if( excludeChar == wxT( "\n" ) )
badCharsFound.Add( _( "line feed" ) );
else if( excludeChar == wxT( "\t" ) )
badCharsFound.Add( _( "tab" ) );
else if( excludeChar == wxT( " " ) )
badCharsFound.Add( _( "space" ) );
else
badCharsFound.Add( wxString::Format( wxT( "'%s'" ), excludeChar ) );
}
}
#endif
wxString badChars;
for( size_t i = 0; i < badCharsFound.GetCount(); i++ )
{
if( !badChars.IsEmpty() )
{
if( badCharsFound.GetCount() == 2 )
{
badChars += _( " or " );
}
else
{
if( i < badCharsFound.GetCount() - 2 )
badChars += _( ", or " );
else
badChars += wxT( ", " );
}
}
badChars += badCharsFound.Item( i );
}
switch( m_fieldId )
{
case REFERENCE_FIELD:
msg.Printf( _( "The reference designator cannot contain %s character(s)." ), badChars );
break;
case VALUE_FIELD:
msg.Printf( _( "The value field cannot contain %s character(s)." ), badChars );
break;
case FOOTPRINT_FIELD:
msg.Printf( _( "The footprint field cannot contain %s character(s)." ), badChars );
break;
case DATASHEET_FIELD:
msg.Printf( _( "The datasheet field cannot contain %s character(s)." ), badChars );
break;
case SHEETNAME_V:
msg.Printf( _( "The sheet name cannot contain %s character(s)." ), badChars );
break;
case SHEETFILENAME_V:
msg.Printf( _( "The sheet filename cannot contain %s character(s)." ), badChars );
break;
default:
msg.Printf( _( "The field cannot contain %s character(s)." ), badChars );
break;
};
}
else if( m_fieldId == REFERENCE_FIELD && val.Contains( wxT( "${" ) ) )
{
msg.Printf( _( "The reference designator cannot contain text variable references" ) );
}
if ( !msg.empty() )
{
m_validatorWindow->SetFocus();
wxMessageBox( msg, _( "Field Validation Error" ), wxOK | wxICON_EXCLAMATION, aParent );
return false;
}
return true;
}
// Match opening curly brace, preceeded by start-of-line or by a character not including $_^~

View File

@ -33,47 +33,6 @@
#include <wx/valtext.h>
#include <validators.h>
#define FIELD_NAME -1
#define FIELD_VALUE -2
#define SHEETNAME_V 100 // We can't use SHEETNAME and SHEETFILENAME because they
#define SHEETFILENAME_V 101 // overlap with REFERENCE_FIELD and VALUE_FIELD
#define SHEETUSERFIELD_V 102
#define LABELUSERFIELD_V 200
/**
* A text control validator used for validating the text allowed in library and
* schematic symbol fields.
*
* - The reference field does not accept spaces.
* - The value field does not accept spaces in the symbol library editor because in symbol
* libraries, the value field is the symbol name in the library.
*/
class SCH_FIELD_VALIDATOR : public wxTextValidator
{
public:
SCH_FIELD_VALIDATOR( bool aIsLibEditor, int aFieldId, wxString* aValue = nullptr );
SCH_FIELD_VALIDATOR( const SCH_FIELD_VALIDATOR& aValidator );
virtual wxObject* Clone() const override { return new SCH_FIELD_VALIDATOR( *this ); }
/**
* Override the default Validate() function provided by wxTextValidator to provide
* better error messages.
*
* @param aParent is the parent window of the error message dialog.
* @return true if the text in the control is valid otherwise false.
*/
virtual bool Validate( wxWindow *aParent ) override;
private:
int m_fieldId;
bool m_isLibEditor;
};
/*
* A refinement of the NETNAME_VALIDATOR which also allows (and checks) bus definitions.
*/

View File

@ -39,6 +39,17 @@
#include <lib_id.h>
#define FIELD_NAME -1
#define FIELD_VALUE -2
#define SHEETNAME_V 100 // We can't use SHEETNAME and SHEETFILENAME because they
#define SHEETFILENAME_V 101 // overlap with REFERENCE_FIELD and VALUE_FIELD
#define SHEETUSERFIELD_V 102
#define LABELUSERFIELD_V 200
/**
* This class works around a bug in wxGrid where the first keystroke doesn't get sent through
* the validator if the editor wasn't already open.
@ -215,4 +226,36 @@ void ValidatorTransferToWindowWithoutEvents( wxValidator& aValidator );
} // namespace KIUI
/**
* A text control validator used for validating the text allowed in fields.
*
* - The reference field does not accept spaces.
* - The value field does not accept spaces in the symbol library editor because in symbol
* libraries, the value field is the symbol name in the library.
*/
class FIELD_VALIDATOR : public wxTextValidator
{
public:
FIELD_VALIDATOR( bool aIsLibEditor, int aFieldId, wxString* aValue = nullptr );
FIELD_VALIDATOR( const FIELD_VALIDATOR& aValidator );
virtual wxObject* Clone() const override { return new FIELD_VALIDATOR( *this ); }
/**
* Override the default Validate() function provided by wxTextValidator to provide
* better error messages.
*
* @param aParent is the parent window of the error message dialog.
* @return true if the text in the control is valid otherwise false.
*/
virtual bool Validate( wxWindow* aParent ) override;
private:
int m_fieldId;
bool m_isLibEditor;
};
#endif // #ifndef VALIDATORS_H