KiCad PCB EDA Suite
sch_validators.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2016 Wayne Stambaugh, stambaughw@gmail.com
5  * Copyright (C) 2016-2017 KiCad Developers, see change_log.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
30 #include <wx/combo.h>
31 #include <sch_connection.h>
32 #include <sch_validators.h>
33 #include <template_fieldnames.h>
34 
35 
36 SCH_FIELD_VALIDATOR::SCH_FIELD_VALIDATOR( bool aIsLibEditor, int aFieldId, wxString* aValue ) :
37  wxTextValidator( wxFILTER_EXCLUDE_CHAR_LIST, aValue )
38 {
39  m_fieldId = aFieldId;
40  m_isLibEditor = aIsLibEditor;
41 
42  // Fields cannot contain carriage returns, line feeds, or tabs.
43  wxString excludes( "\r\n\t" );
44 
45  // The reference field cannot contain spaces.
46  if( aFieldId == REFERENCE )
47  excludes += " ";
48  else if( aFieldId == VALUE && m_isLibEditor )
49  excludes += " :/\\";
50 
51  long style = GetStyle();
52 
53  // The reference and value fields cannot be empty.
54  if( aFieldId == REFERENCE || aFieldId == VALUE || aFieldId == FIELD_NAME )
55  style |= wxFILTER_EMPTY;
56 
57  SetStyle( style );
58  SetCharExcludes( excludes );
59 }
60 
61 
63  wxTextValidator( aValidator )
64 {
65  m_fieldId = aValidator.m_fieldId;
66  m_isLibEditor = aValidator.m_isLibEditor;
67 }
68 
69 
70 bool SCH_FIELD_VALIDATOR::Validate( wxWindow *aParent )
71 {
72  // If window is disabled, simply return
73  if( !m_validatorWindow->IsEnabled() )
74  return true;
75 
76  wxTextEntry * const text = GetTextEntry();
77 
78  if( !text )
79  return false;
80 
81  wxString val( text->GetValue() );
82 
83  // The format of the error message for not allowed chars
84  wxString fieldCharError;
85 
86  switch( m_fieldId )
87  {
88  case REFERENCE:
89  fieldCharError = _( "The reference designator cannot contain %s character(s)." );
90  break;
91 
92  case VALUE:
93  fieldCharError = _( "The value field cannot contain %s character(s)." );
94  break;
95 
96  case FOOTPRINT:
97  fieldCharError = _( "The footprint field cannot contain %s character(s)." );
98  break;
99 
100  case DATASHEET:
101  fieldCharError = _( "The datasheet field cannot contain %s character(s)." );
102  break;
103 
104  default:
105  fieldCharError = _( "The field cannot contain %s character(s)." );
106  break;
107  };
108 
109  wxString msg;
110 
111  // We can only do some kinds of validation once the input is complete, so
112  // check for them here:
113  if( HasFlag( wxFILTER_EMPTY ) && val.empty() )
114  {
115  // Some fields cannot have an empty value, and user fields require a name:
116  if( m_fieldId == FIELD_NAME )
117  msg.Printf( _( "The name of the field cannot be empty." ) );
118  else // the FIELD_VALUE id or REFERENCE or VALUE
119  msg.Printf( _( "The value of the field cannot be empty." ) );
120  }
121  else if( HasFlag( wxFILTER_EXCLUDE_CHAR_LIST ) && ContainsExcludedCharacters( val ) )
122  {
123  wxArrayString whiteSpace;
124  bool spaceIllegal = ( m_fieldId == REFERENCE ) ||
125  ( m_fieldId == VALUE && m_isLibEditor );
126 
127  if( val.Find( '\r' ) != wxNOT_FOUND )
128  whiteSpace.Add( _( "carriage return" ) );
129  if( val.Find( '\n' ) != wxNOT_FOUND )
130  whiteSpace.Add( _( "line feed" ) );
131  if( val.Find( '\t' ) != wxNOT_FOUND )
132  whiteSpace.Add( _( "tab" ) );
133  if( spaceIllegal && (val.Find( ' ' ) != wxNOT_FOUND) )
134  whiteSpace.Add( _( "space" ) );
135 
136  wxString badChars;
137 
138  if( whiteSpace.size() == 1 )
139  badChars = whiteSpace[0];
140  else if( whiteSpace.size() == 2 )
141  badChars.Printf( _( "%s or %s" ), whiteSpace[0], whiteSpace[1] );
142  else if( whiteSpace.size() == 3 )
143  badChars.Printf( _( "%s, %s, or %s" ), whiteSpace[0], whiteSpace[1], whiteSpace[2] );
144  else if( whiteSpace.size() == 4 )
145  badChars.Printf( _( "%s, %s, %s, or %s" ),
146  whiteSpace[0], whiteSpace[1], whiteSpace[2], whiteSpace[3] );
147  else
148  wxCHECK_MSG( false, true, "Invalid illegal character in field validator." );
149 
150  msg.Printf( fieldCharError, badChars );
151  }
152 
153  if ( !msg.empty() )
154  {
155  m_validatorWindow->SetFocus();
156 
157  wxMessageBox( msg, _( "Field Validation Error" ), wxOK | wxICON_EXCLAMATION, aParent );
158 
159  return false;
160  }
161 
162  return true;
163 }
164 
165 
167  : wxValidator(),
168  m_allowSpaces( false )
169 {
170 }
171 
172 
174  : wxValidator(),
175  m_allowSpaces( aValidator.m_allowSpaces )
176 {
177 }
178 
179 
181  : wxValidator(),
182  m_allowSpaces( aAllowSpaces )
183 {
184 }
185 
186 
188 {
189 #if wxUSE_TEXTCTRL
190  if( wxDynamicCast( m_validatorWindow, wxTextCtrl ) )
191  return static_cast<wxTextCtrl*>( m_validatorWindow );
192 #endif
193 
194 #if wxUSE_COMBOBOX
195  if( wxDynamicCast( m_validatorWindow, wxComboBox ) )
196  return static_cast<wxComboBox*>( m_validatorWindow );
197 #endif
198 
199 #if wxUSE_COMBOCTRL
200  if( wxDynamicCast( m_validatorWindow, wxComboCtrl ) )
201  return static_cast<wxComboCtrl*>( m_validatorWindow );
202 #endif
203 
204  wxFAIL_MSG( "SCH_NETNAME_VALIDATOR can only be used with wxTextCtrl, wxComboBox, or wxComboCtrl" );
205  return nullptr;
206 }
207 
208 
209 bool SCH_NETNAME_VALIDATOR::Validate( wxWindow *aParent )
210 {
211  // If window is disabled, simply return
212  if ( !m_validatorWindow->IsEnabled() )
213  return true;
214 
215  wxTextEntry * const text = GetTextEntry();
216 
217  if ( !text )
218  return false;
219 
220  const wxString& errormsg = IsValid( text->GetValue() );
221 
222  if( !errormsg.empty() )
223  {
224  m_validatorWindow->SetFocus();
225  wxMessageBox( errormsg, _( "Invalid signal name" ), wxOK | wxICON_EXCLAMATION, aParent );
226  return false;
227  }
228 
229  return true;
230 }
231 
232 
233 wxString SCH_NETNAME_VALIDATOR::IsValid( const wxString& str ) const
234 {
236  return wxString();
237 
238  if( str.Contains( '{' ) || str.Contains( '}' ) )
239  return _( "Signal name contains '{' or '}' but is not a valid group bus name" );
240 
241  if( ( str.Contains( '[' ) || str.Contains( ']' ) ) &&
243  return _( "Signal name contains '[' or ']' but is not a valid vector bus name" );
244 
245  if( str.Contains( '\r' ) || str.Contains( '\n' ) )
246  return _( "Signal names cannot contain CR or LF characters" );
247 
248  if( !m_allowSpaces && ( str.Contains( ' ' ) || str.Contains( '\t' ) ) )
249  return _( "Signal names cannot contain spaces" );
250 
251  return wxString();
252 }
name of datasheet
Field Reference of part, i.e. "IC21".
SCH_NETNAME_VALIDATOR(wxString *aVal=nullptr)
virtual wxString IsValid(const wxString &aVal) const
#define VALUE
Definitions of control validators for schematic dialogs.
#define FIELD_NAME
static bool IsBusVectorLabel(const wxString &aLabel)
Test if aLabel has a bus vector notation (simple bus, e.g.
SCH_FIELD_VALIDATOR(bool aIsLibEditor, int aFieldId, wxString *aValue=NULL)
wxTextEntry * GetTextEntry()
#define _(s)
Definition: 3d_actions.cpp:33
A text control validator used for validating the text allowed in library and schematic component fiel...
static bool IsBusGroupLabel(const wxString &aLabel)
Test if aLabel has a bus group notation.
virtual bool Validate(wxWindow *aParent) override
virtual bool Validate(wxWindow *aParent) override
Override the default Validate() function provided by wxTextValidator to provide better error messages...