7
mirror of https://gitlab.com/kicad/code/kicad.git synced 2025-04-21 00:21:25 +00:00

Centralize ref-des error checking.

This commit is contained in:
Jeff Young 2024-10-28 18:39:58 +00:00
parent 8874a2cda7
commit 905eaa79a9
7 changed files with 30 additions and 47 deletions

View File

@ -39,6 +39,7 @@
#include <wx/log.h>
#include <wx/combo.h>
#include <wx/msgdlg.h>
#include <refdes_utils.h>
FOOTPRINT_NAME_VALIDATOR::FOOTPRINT_NAME_VALIDATOR( wxString* aValue ) :
@ -356,18 +357,24 @@ bool FIELD_VALIDATOR::Validate( wxWindow* aParent )
return false;
wxString val( text->GetValue() );
return DoValidate( val, aParent );
}
bool FIELD_VALIDATOR::DoValidate( const wxString& aValue, wxWindow* aParent )
{
wxString msg;
if( HasFlag( wxFILTER_EMPTY ) && val.empty() )
if( HasFlag( wxFILTER_EMPTY ) && aValue.empty() )
msg.Printf( _( "The value of the field cannot be empty." ) );
if( HasFlag( wxFILTER_EXCLUDE_CHAR_LIST ) && ContainsExcludedCharacters( val ) )
if( HasFlag( wxFILTER_EXCLUDE_CHAR_LIST ) && ContainsExcludedCharacters( aValue ) )
{
wxArrayString badCharsFound;
for( const wxUniCharRef& excludeChar : GetCharExcludes() )
{
if( val.Find( excludeChar ) != wxNOT_FOUND )
if( aValue.Find( excludeChar ) != wxNOT_FOUND )
{
if( excludeChar == '\r' )
badCharsFound.Add( _( "carriage return" ) );
@ -435,14 +442,19 @@ bool FIELD_VALIDATOR::Validate( wxWindow* aParent )
break;
};
}
else if( m_fieldId == REFERENCE_FIELD && val.Contains( wxT( "${" ) ) )
else if( m_fieldId == REFERENCE_FIELD && aValue.Contains( wxT( "${" ) ) )
{
msg.Printf( _( "The reference designator cannot contain text variable references" ) );
}
else if( m_fieldId == REFERENCE_FIELD && UTIL::GetRefDesPrefix( aValue ).IsEmpty() )
{
msg.Printf( _( "References must start with a letter." ) );
}
if( !msg.empty() )
{
m_validatorWindow->SetFocus();
if( m_validatorWindow )
m_validatorWindow->SetFocus();
wxMessageBox( msg, _( "Field Validation Error" ), wxOK | wxICON_EXCLAMATION, aParent );

View File

@ -502,19 +502,8 @@ bool DIALOG_FIELD_PROPERTIES::TransferDataFromWindow()
else if( m_StyledTextCtrl->IsShown() )
m_text = UnescapeString( m_StyledTextCtrl->GetValue() );
if( m_fieldId == REFERENCE_FIELD )
{
// Test if the reference string is valid:
if( !SCH_SYMBOL::IsReferenceStringValid( m_text ) )
{
DisplayError( this, _( "Illegal reference designator value!" ) );
return false;
}
}
else if( m_fieldId == SHEETFILENAME_V )
{
if( m_fieldId == SHEETFILENAME_V )
m_text = EnsureFileExtension( m_text, FILEEXT::KiCadSchematicFileExtension );
}
m_position = VECTOR2I( m_posX.GetIntValue(), m_posY.GetIntValue() );
m_size = m_textSize.GetIntValue();

View File

@ -39,7 +39,7 @@
#include <widgets/std_bitmap_button.h>
#include <string_utils.h>
#include <project_sch.h>
#include <refdes_utils.h>
#include <dialog_sim_model.h>
#include <panel_embedded_files.h>
@ -294,8 +294,8 @@ bool DIALOG_LIB_SYMBOL_PROPERTIES::Validate()
return false;
// Alias symbol reference can be empty because it inherits from the parent symbol.
if( m_libEntry->IsRoot() &&
!SCH_SYMBOL::IsReferenceStringValid( m_fields->at( REFERENCE_FIELD ).GetText() ) )
if( m_libEntry->IsRoot()
&& UTIL::GetRefDesPrefix( m_fields->at( REFERENCE_FIELD ).GetText() ).IsEmpty() )
{
if( m_NoteBook->GetSelection() != 0 )
m_NoteBook->SetSelection( 0 );

View File

@ -634,17 +634,6 @@ bool DIALOG_SYMBOL_PROPERTIES::Validate()
if( !m_fieldsGrid->CommitPendingChanges() || !m_fieldsGrid->Validate() )
return false;
if( !SCH_SYMBOL::IsReferenceStringValid( m_fields->at( REFERENCE_FIELD ).GetText() ) )
{
DisplayErrorMessage( this, _( "References must start with a letter." ) );
wxCommandEvent *evt = new wxCommandEvent( SYMBOL_DELAY_FOCUS );
evt->SetClientData( new VECTOR2I( REFERENCE_FIELD, FDC_VALUE ) );
QueueEvent( evt );
return false;
}
// Check for missing field names.
for( size_t i = MANDATORY_FIELDS; i < m_fields->size(); ++i )
{

View File

@ -43,6 +43,7 @@
#include <sch_rule_area.h>
#include <utility>
#include <validators.h>
std::unordered_map<TRANSFORM, int> SCH_SYMBOL::s_transformToOrientationCache;
@ -766,9 +767,12 @@ const wxString SCH_SYMBOL::GetRef( const SCH_SHEET_PATH* sheet, bool aIncludeUni
}
bool SCH_SYMBOL::IsReferenceStringValid( const wxString& aReferenceString )
void SCH_SYMBOL::SetRefProp( const wxString& aRef )
{
return !UTIL::GetRefDesPrefix( aReferenceString ).IsEmpty();
FIELD_VALIDATOR validator( REFERENCE_FIELD );
if( validator.DoValidate( aRef, nullptr ) )
SetRef( &Schematic()->CurrentSheet(), aRef );
}

View File

@ -542,10 +542,7 @@ public:
{
return GetRef( &Schematic()->CurrentSheet() );
}
void SetRefProp( const wxString& aRef )
{
SetRef( &Schematic()->CurrentSheet(), aRef );
}
void SetRefProp( const wxString& aRef );
wxString GetValueProp() const
{
return GetValue( false, &Schematic()->CurrentSheet(), false );
@ -681,16 +678,6 @@ public:
void SwapData( SCH_ITEM* aItem ) override;
/**
* Test for an acceptable reference string.
*
* An acceptable reference string must support unannotation i.e starts by letter
*
* @param aReferenceString is the reference string to validate
* @return true if reference string is valid.
*/
static bool IsReferenceStringValid( const wxString& aReferenceString );
/**
* Set the reference for the given sheet path for this symbol.
*

View File

@ -235,6 +235,8 @@ public:
*/
virtual bool Validate( wxWindow* aParent ) override;
bool DoValidate( const wxString& aValue, wxWindow* aParent );
private:
int m_fieldId;
};