KiCad PCB EDA Suite
SCH_FIELD_VALIDATOR Class Reference

A text control validator used for validating the text allowed in library and schematic symbol fields. More...

#include <sch_validators.h>

Inheritance diagram for SCH_FIELD_VALIDATOR:

Public Member Functions

 SCH_FIELD_VALIDATOR (bool aIsLibEditor, int aFieldId, wxString *aValue=NULL)
 
 SCH_FIELD_VALIDATOR (const SCH_FIELD_VALIDATOR &aValidator)
 
virtual wxObject * Clone () const override
 
virtual bool Validate (wxWindow *aParent) override
 Override the default Validate() function provided by wxTextValidator to provide better error messages. More...
 

Private Attributes

int m_fieldId
 
bool m_isLibEditor
 

Detailed Description

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.

Definition at line 51 of file sch_validators.h.

Constructor & Destructor Documentation

◆ SCH_FIELD_VALIDATOR() [1/2]

SCH_FIELD_VALIDATOR::SCH_FIELD_VALIDATOR ( bool  aIsLibEditor,
int  aFieldId,
wxString *  aValue = NULL 
)

Definition at line 39 of file sch_validators.cpp.

39  :
40  wxTextValidator( wxFILTER_EXCLUDE_CHAR_LIST, aValue )
41 {
42  m_fieldId = aFieldId;
43  m_isLibEditor = aIsLibEditor;
44 
45  // Fields cannot contain carriage returns, line feeds, or tabs.
46  wxString excludes( "\r\n\t" );
47 
48  // The reference field cannot contain spaces.
49  if( aFieldId == REFERENCE_FIELD )
50  {
51  excludes += " ";
52  }
53  else if( aFieldId == VALUE_FIELD && m_isLibEditor )
54  {
55  excludes += " :/\\";
56  }
57  else if( aFieldId == SHEETFILENAME_V )
58  {
59  excludes += ":/\\";
60  }
61 
62  long style = GetStyle();
63 
64  // The reference, value sheetname and sheetfilename fields cannot be empty.
65  if( aFieldId == REFERENCE_FIELD
66  || aFieldId == VALUE_FIELD
67  || aFieldId == SHEETNAME_V
68  || aFieldId == SHEETFILENAME_V
69  || aFieldId == FIELD_NAME )
70  {
71  style |= wxFILTER_EMPTY;
72  }
73 
74  SetStyle( style );
75  SetCharExcludes( excludes );
76 }
Field Reference of part, i.e. "IC21".
#define SHEETNAME_V
Field Value of part, i.e. "3.3K".
#define SHEETFILENAME_V
#define FIELD_NAME

References FIELD_NAME, m_fieldId, m_isLibEditor, REFERENCE_FIELD, SHEETFILENAME_V, SHEETNAME_V, and VALUE_FIELD.

Referenced by Clone().

◆ SCH_FIELD_VALIDATOR() [2/2]

SCH_FIELD_VALIDATOR::SCH_FIELD_VALIDATOR ( const SCH_FIELD_VALIDATOR aValidator)

Definition at line 79 of file sch_validators.cpp.

79  :
80  wxTextValidator( aValidator )
81 {
82  m_fieldId = aValidator.m_fieldId;
83  m_isLibEditor = aValidator.m_isLibEditor;
84 }

References m_fieldId, and m_isLibEditor.

Member Function Documentation

◆ Clone()

virtual wxObject* SCH_FIELD_VALIDATOR::Clone ( ) const
inlineoverridevirtual

Definition at line 58 of file sch_validators.h.

58 { return new SCH_FIELD_VALIDATOR( *this ); }
SCH_FIELD_VALIDATOR(bool aIsLibEditor, int aFieldId, wxString *aValue=NULL)

References SCH_FIELD_VALIDATOR().

◆ Validate()

bool SCH_FIELD_VALIDATOR::Validate ( wxWindow *  aParent)
overridevirtual

Override the default Validate() function provided by wxTextValidator to provide better error messages.

Parameters
aParentis the parent window of the error message dialog.
Returns
true if the text in the control is valid otherwise false.

Definition at line 87 of file sch_validators.cpp.

88 {
89  // If window is disabled, simply return
90  if( !m_validatorWindow->IsEnabled() )
91  return true;
92 
93  wxTextEntry * const text = GetTextEntry();
94 
95  if( !text )
96  return false;
97 
98  wxString val( text->GetValue() );
99 
100  // The format of the error message for not allowed chars
101  wxString fieldCharError;
102 
103  switch( m_fieldId )
104  {
105  case REFERENCE_FIELD:
106  fieldCharError = _( "The reference designator cannot contain %s character(s)." );
107  break;
108 
109  case VALUE_FIELD:
110  fieldCharError = _( "The value field cannot contain %s character(s)." );
111  break;
112 
113  case FOOTPRINT_FIELD:
114  fieldCharError = _( "The footprint field cannot contain %s character(s)." );
115  break;
116 
117  case DATASHEET_FIELD:
118  fieldCharError = _( "The datasheet field cannot contain %s character(s)." );
119  break;
120 
121  case SHEETNAME_V:
122  fieldCharError = _( "The sheet name cannot contain %s character(s)." );
123  break;
124 
125  case SHEETFILENAME_V:
126  fieldCharError = _( "The sheet filename cannot contain %s character(s)." );
127  break;
128 
129  default:
130  fieldCharError = _( "The field cannot contain %s character(s)." );
131  break;
132  };
133 
134  wxString msg;
135 
136  // We can only do some kinds of validation once the input is complete, so
137  // check for them here:
138  if( HasFlag( wxFILTER_EMPTY ) && val.empty() )
139  {
140  // Some fields cannot have an empty value, and user fields require a name:
141  if( m_fieldId == FIELD_NAME )
142  msg.Printf( _( "The name of the field cannot be empty." ) );
143  else // the FIELD_VALUE id or REFERENCE_FIELD or VALUE_FIELD
144  msg.Printf( _( "The value of the field cannot be empty." ) );
145  }
146  else if( HasFlag( wxFILTER_EXCLUDE_CHAR_LIST ) && ContainsExcludedCharacters( val ) )
147  {
148  wxArrayString whiteSpace;
149  bool spaceIllegal = m_fieldId == REFERENCE_FIELD
150  || ( m_fieldId == VALUE_FIELD && m_isLibEditor )
151  || m_fieldId == SHEETNAME_V
153 
154  if( val.Find( '\r' ) != wxNOT_FOUND )
155  whiteSpace.Add( _( "carriage return" ) );
156  if( val.Find( '\n' ) != wxNOT_FOUND )
157  whiteSpace.Add( _( "line feed" ) );
158  if( val.Find( '\t' ) != wxNOT_FOUND )
159  whiteSpace.Add( _( "tab" ) );
160  if( spaceIllegal && (val.Find( ' ' ) != wxNOT_FOUND) )
161  whiteSpace.Add( _( "space" ) );
162 
163  wxString badChars;
164 
165  if( whiteSpace.size() == 1 )
166  badChars = whiteSpace[0];
167  else if( whiteSpace.size() == 2 )
168  badChars.Printf( _( "%s or %s" ), whiteSpace[0], whiteSpace[1] );
169  else if( whiteSpace.size() == 3 )
170  badChars.Printf( _( "%s, %s, or %s" ), whiteSpace[0], whiteSpace[1], whiteSpace[2] );
171  else if( whiteSpace.size() == 4 )
172  badChars.Printf( _( "%s, %s, %s, or %s" ),
173  whiteSpace[0], whiteSpace[1], whiteSpace[2], whiteSpace[3] );
174  else
175  wxCHECK_MSG( false, true, "Invalid illegal character in field validator." );
176 
177  msg.Printf( fieldCharError, badChars );
178  }
179  else if( m_fieldId == REFERENCE_FIELD && val.Contains( wxT( "${" ) ) )
180  {
181  msg.Printf( _( "The reference designator cannot contain text variable references" ) );
182  }
183 
184  if ( !msg.empty() )
185  {
186  m_validatorWindow->SetFocus();
187 
188  wxMessageBox( msg, _( "Field Validation Error" ), wxOK | wxICON_EXCLAMATION, aParent );
189 
190  return false;
191  }
192 
193  return true;
194 }
Field Reference of part, i.e. "IC21".
#define SHEETNAME_V
name of datasheet
Field Value of part, i.e. "3.3K".
#define SHEETFILENAME_V
#define FIELD_NAME
#define _(s)
Definition: 3d_actions.cpp:33
Field Name Module PCB, i.e. "16DIP300".

References _, DATASHEET_FIELD, FIELD_NAME, FOOTPRINT_FIELD, m_fieldId, m_isLibEditor, REFERENCE_FIELD, SHEETFILENAME_V, SHEETNAME_V, text, and VALUE_FIELD.

Member Data Documentation

◆ m_fieldId

int SCH_FIELD_VALIDATOR::m_fieldId
private

Definition at line 70 of file sch_validators.h.

Referenced by SCH_FIELD_VALIDATOR(), and Validate().

◆ m_isLibEditor

bool SCH_FIELD_VALIDATOR::m_isLibEditor
private

Definition at line 71 of file sch_validators.h.

Referenced by SCH_FIELD_VALIDATOR(), and Validate().


The documentation for this class was generated from the following files: