KiCad PCB EDA Suite
SCH_FIELD_VALIDATOR Class Reference

A text control validator used for validating the text allowed in library and schematic component 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 component 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 37 of file sch_validators.cpp.

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

References FIELD_NAME, m_fieldId, m_isLibEditor, REFERENCE, SHEETFILENAME_V, SHEETNAME_V, and VALUE.

Referenced by Clone().

◆ SCH_FIELD_VALIDATOR() [2/2]

SCH_FIELD_VALIDATOR::SCH_FIELD_VALIDATOR ( const SCH_FIELD_VALIDATOR aValidator)

Definition at line 73 of file sch_validators.cpp.

73  :
74  wxTextValidator( aValidator )
75 {
76  m_fieldId = aValidator.m_fieldId;
77  m_isLibEditor = aValidator.m_isLibEditor;
78 }

References m_fieldId, and m_isLibEditor.

Member Function Documentation

◆ Clone()

virtual wxObject* SCH_FIELD_VALIDATOR::Clone ( ) const
inlineoverridevirtual

Definition at line 61 of file sch_validators.h.

61 { 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
aParent- a pointer to the parent window of the error message dialog.
Returns
true if the text in the control is valid otherwise false.

Definition at line 81 of file sch_validators.cpp.

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

References _, DATASHEET, FIELD_NAME, FOOTPRINT, m_fieldId, m_isLibEditor, REFERENCE, SHEETFILENAME_V, SHEETNAME_V, and VALUE.

Member Data Documentation

◆ m_fieldId

int SCH_FIELD_VALIDATOR::m_fieldId
private

Definition at line 53 of file sch_validators.h.

Referenced by SCH_FIELD_VALIDATOR(), and Validate().

◆ m_isLibEditor

bool SCH_FIELD_VALIDATOR::m_isLibEditor
private

Definition at line 54 of file sch_validators.h.

Referenced by SCH_FIELD_VALIDATOR(), and Validate().


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