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  {
48  excludes += " ";
49  }
50  else if( ( aFieldId == VALUE && m_isLibEditor ) || aFieldId == SHEETNAME_V )
51  {
52  excludes += " :/\\";
53  }
54 
55  long style = GetStyle();
56 
57  // The reference, value sheetname and sheetfilename fields cannot be empty.
58  if( aFieldId == REFERENCE
59  || aFieldId == VALUE
60  || aFieldId == SHEETNAME_V
61  || aFieldId == SHEETFILENAME_V
62  || aFieldId == FIELD_NAME )
63  {
64  style |= wxFILTER_EMPTY;
65  }
66 
67  SetStyle( style );
68  SetCharExcludes( excludes );
69 }
70 
71 
73  wxTextValidator( aValidator )
74 {
75  m_fieldId = aValidator.m_fieldId;
76  m_isLibEditor = aValidator.m_isLibEditor;
77 }
78 
79 
80 bool SCH_FIELD_VALIDATOR::Validate( wxWindow *aParent )
81 {
82  // If window is disabled, simply return
83  if( !m_validatorWindow->IsEnabled() || !m_validatorWindow->IsShown() )
84  return true;
85 
86  wxTextEntry * const text = GetTextEntry();
87 
88  if( !text )
89  return false;
90 
91  wxString val( text->GetValue() );
92 
93  // The format of the error message for not allowed chars
94  wxString fieldCharError;
95 
96  switch( m_fieldId )
97  {
98  case REFERENCE:
99  fieldCharError = _( "The reference designator cannot contain %s character(s)." );
100  break;
101 
102  case VALUE:
103  fieldCharError = _( "The value field cannot contain %s character(s)." );
104  break;
105 
106  case FOOTPRINT:
107  fieldCharError = _( "The footprint field cannot contain %s character(s)." );
108  break;
109 
110  case DATASHEET:
111  fieldCharError = _( "The datasheet field cannot contain %s character(s)." );
112  break;
113 
114  case SHEETNAME_V:
115  fieldCharError = _( "The sheet name cannot contain %s character(s)." );
116  break;
117 
118  case SHEETFILENAME_V:
119  fieldCharError = _( "The sheet filename cannot contain %s character(s)." );
120  break;
121 
122  default:
123  fieldCharError = _( "The field cannot contain %s character(s)." );
124  break;
125  };
126 
127  wxString msg;
128 
129  // We can only do some kinds of validation once the input is complete, so
130  // check for them here:
131  if( HasFlag( wxFILTER_EMPTY ) && val.empty() )
132  {
133  // Some fields cannot have an empty value, and user fields require a name:
134  if( m_fieldId == FIELD_NAME )
135  msg.Printf( _( "The name of the field cannot be empty." ) );
136  else // the FIELD_VALUE id or REFERENCE or VALUE
137  msg.Printf( _( "The value of the field cannot be empty." ) );
138  }
139  else if( HasFlag( wxFILTER_EXCLUDE_CHAR_LIST ) && ContainsExcludedCharacters( val ) )
140  {
141  wxArrayString whiteSpace;
142  bool spaceIllegal = m_fieldId == REFERENCE
143  || ( m_fieldId == VALUE && m_isLibEditor )
144  || m_fieldId == SHEETNAME_V
146 
147  if( val.Find( '\r' ) != wxNOT_FOUND )
148  whiteSpace.Add( _( "carriage return" ) );
149  if( val.Find( '\n' ) != wxNOT_FOUND )
150  whiteSpace.Add( _( "line feed" ) );
151  if( val.Find( '\t' ) != wxNOT_FOUND )
152  whiteSpace.Add( _( "tab" ) );
153  if( spaceIllegal && (val.Find( ' ' ) != wxNOT_FOUND) )
154  whiteSpace.Add( _( "space" ) );
155 
156  wxString badChars;
157 
158  if( whiteSpace.size() == 1 )
159  badChars = whiteSpace[0];
160  else if( whiteSpace.size() == 2 )
161  badChars.Printf( _( "%s or %s" ), whiteSpace[0], whiteSpace[1] );
162  else if( whiteSpace.size() == 3 )
163  badChars.Printf( _( "%s, %s, or %s" ), whiteSpace[0], whiteSpace[1], whiteSpace[2] );
164  else if( whiteSpace.size() == 4 )
165  badChars.Printf( _( "%s, %s, %s, or %s" ),
166  whiteSpace[0], whiteSpace[1], whiteSpace[2], whiteSpace[3] );
167  else
168  wxCHECK_MSG( false, true, "Invalid illegal character in field validator." );
169 
170  msg.Printf( fieldCharError, badChars );
171  }
172 
173  if ( !msg.empty() )
174  {
175  m_validatorWindow->SetFocus();
176 
177  wxMessageBox( msg, _( "Field Validation Error" ), wxOK | wxICON_EXCLAMATION, aParent );
178 
179  return false;
180  }
181 
182  return true;
183 }
184 
185 
187  : wxValidator(),
188  m_allowSpaces( false )
189 {
190 }
191 
192 
194  : wxValidator(),
195  m_allowSpaces( aValidator.m_allowSpaces )
196 {
197 }
198 
199 
201  : wxValidator(),
202  m_allowSpaces( aAllowSpaces )
203 {
204 }
205 
206 
208 {
209 #if wxUSE_TEXTCTRL
210  if( wxDynamicCast( m_validatorWindow, wxTextCtrl ) )
211  return static_cast<wxTextCtrl*>( m_validatorWindow );
212 #endif
213 
214 #if wxUSE_COMBOBOX
215  if( wxDynamicCast( m_validatorWindow, wxComboBox ) )
216  return static_cast<wxComboBox*>( m_validatorWindow );
217 #endif
218 
219 #if wxUSE_COMBOCTRL
220  if( wxDynamicCast( m_validatorWindow, wxComboCtrl ) )
221  return static_cast<wxComboCtrl*>( m_validatorWindow );
222 #endif
223 
224  wxFAIL_MSG( "SCH_NETNAME_VALIDATOR can only be used with wxTextCtrl, wxComboBox, or wxComboCtrl" );
225  return nullptr;
226 }
227 
228 
229 bool SCH_NETNAME_VALIDATOR::Validate( wxWindow *aParent )
230 {
231  // If window is disabled, simply return
232  if ( !m_validatorWindow->IsEnabled() )
233  return true;
234 
235  wxTextEntry * const text = GetTextEntry();
236 
237  if ( !text )
238  return false;
239 
240  const wxString& errormsg = IsValid( text->GetValue() );
241 
242  if( !errormsg.empty() )
243  {
244  m_validatorWindow->SetFocus();
245  wxMessageBox( errormsg, _( "Invalid signal name" ), wxOK | wxICON_EXCLAMATION, aParent );
246  return false;
247  }
248 
249  return true;
250 }
251 
252 
253 wxString SCH_NETNAME_VALIDATOR::IsValid( const wxString& str ) const
254 {
255  if( SCH_CONNECTION::ParseBusGroup( str, nullptr, nullptr ) )
256  return wxString();
257 
258  if( ( str.Contains( '[' ) || str.Contains( ']' ) ) &&
259  !SCH_CONNECTION::ParseBusVector( str, nullptr, nullptr ) )
260  return _( "Signal name contains '[' or ']' but is not a valid vector bus name" );
261 
262  if( str.Contains( '\r' ) || str.Contains( '\n' ) )
263  return _( "Signal names cannot contain CR or LF characters" );
264 
265  if( !m_allowSpaces && ( str.Contains( ' ' ) || str.Contains( '\t' ) ) )
266  return _( "Signal names cannot contain spaces" );
267 
268  return wxString();
269 }
name of datasheet
#define SHEETNAME_V
Field Name Module PCB, i.e. "16DIP300".
Field Reference of part, i.e. "IC21".
SCH_NETNAME_VALIDATOR(wxString *aVal=nullptr)
virtual wxString IsValid(const wxString &aVal) const
static bool ParseBusVector(const wxString &aBus, wxString *aName, std::vector< wxString > *aMemberList)
Parses a bus vector (e.g.
#define VALUE
static bool ParseBusGroup(wxString aGroup, wxString *name, std::vector< wxString > *aMemberList)
Parses a bus group label into the name and a list of components.
Definitions of control validators for schematic dialogs.
#define SHEETFILENAME_V
#define FIELD_NAME
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...
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...