From ddafa2e75db44894552163250a222440b2e01ee5 Mon Sep 17 00:00:00 2001 From: Mike Williams Date: Tue, 13 Jun 2023 10:10:48 -0400 Subject: [PATCH] Common: move SCH_FIELD_VALIDATORS to FIELD_VALIDATORS Fields are now common across PCB/SCH --- common/validators.cpp | 169 +++++++++++++++++ eeschema/dialogs/dialog_field_properties.cpp | 2 +- eeschema/dialogs/dialog_lib_new_symbol.cpp | 4 +- .../dialogs/dialog_lib_symbol_properties.cpp | 2 +- eeschema/fields_grid_table.h | 12 +- eeschema/sch_validators.cpp | 171 ------------------ eeschema/sch_validators.h | 41 ----- include/validators.h | 43 +++++ 8 files changed, 222 insertions(+), 222 deletions(-) diff --git a/common/validators.cpp b/common/validators.cpp index 6c5fbb15e8..e908c9734c 100644 --- a/common/validators.cpp +++ b/common/validators.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #include @@ -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; +} diff --git a/eeschema/dialogs/dialog_field_properties.cpp b/eeschema/dialogs/dialog_field_properties.cpp index 3263d96541..05e2365338 100644 --- a/eeschema/dialogs/dialog_field_properties.cpp +++ b/eeschema/dialogs/dialog_field_properties.cpp @@ -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 ); diff --git a/eeschema/dialogs/dialog_lib_new_symbol.cpp b/eeschema/dialogs/dialog_lib_new_symbol.cpp index a4674925c7..cc1d594ed4 100644 --- a/eeschema/dialogs/dialog_lib_new_symbol.cpp +++ b/eeschema/dialogs/dialog_lib_new_symbol.cpp @@ -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 ) ); diff --git a/eeschema/dialogs/dialog_lib_symbol_properties.cpp b/eeschema/dialogs/dialog_lib_symbol_properties.cpp index 66aa0aba0b..c0c2488d9e 100644 --- a/eeschema/dialogs/dialog_lib_symbol_properties.cpp +++ b/eeschema/dialogs/dialog_lib_symbol_properties.cpp @@ -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 ) ); diff --git a/eeschema/fields_grid_table.h b/eeschema/fields_grid_table.h index c63bed16f9..1eeb3c7004 100644 --- a/eeschema/fields_grid_table.h +++ b/eeschema/fields_grid_table.h @@ -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; diff --git a/eeschema/sch_validators.cpp b/eeschema/sch_validators.cpp index 2103e79986..c98aeacc15 100644 --- a/eeschema/sch_validators.cpp +++ b/eeschema/sch_validators.cpp @@ -33,177 +33,6 @@ #include #include #include -#include - - -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 $_^~ diff --git a/eeschema/sch_validators.h b/eeschema/sch_validators.h index 30dbab04e6..40c0d85127 100644 --- a/eeschema/sch_validators.h +++ b/eeschema/sch_validators.h @@ -33,47 +33,6 @@ #include #include -#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. */ diff --git a/include/validators.h b/include/validators.h index dffa9e8d21..03c5773228 100644 --- a/include/validators.h +++ b/include/validators.h @@ -39,6 +39,17 @@ #include + +#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