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 <wx/msgdlg.h>
32 
33 #include <sch_connection.h>
34 #include <sch_validators.h>
35 #include <project/net_settings.h>
36 #include <template_fieldnames.h>
37 
38 
39 SCH_FIELD_VALIDATOR::SCH_FIELD_VALIDATOR( bool aIsLibEditor, int aFieldId, wxString* aValue ) :
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 }
77 
78 
80  wxTextValidator( aValidator )
81 {
82  m_fieldId = aValidator.m_fieldId;
83  m_isLibEditor = aValidator.m_isLibEditor;
84 }
85 
86 
87 bool SCH_FIELD_VALIDATOR::Validate( wxWindow *aParent )
88 {
89  // If window is disabled, simply return
90  if( !m_validatorWindow->IsEnabled() || !m_validatorWindow->IsShown() )
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 }
195 
196 
197 wxString SCH_NETNAME_VALIDATOR::IsValid( const wxString& str ) const
198 {
199  if( NET_SETTINGS::ParseBusGroup( str, nullptr, nullptr ) )
200  return wxString();
201 
202  if( ( str.Contains( '[' ) || str.Contains( ']' ) ) &&
203  !NET_SETTINGS::ParseBusVector( str, nullptr, nullptr ) )
204  return _( "Signal name contains '[' or ']' but is not a valid vector bus name" );
205 
206  return NETNAME_VALIDATOR::IsValid( str );
207 }
Field Reference of part, i.e. "IC21".
#define SHEETNAME_V
name of datasheet
static bool ParseBusGroup(wxString aGroup, wxString *name, std::vector< wxString > *aMemberList)
Parses a bus group label into the name and a list of components.
wxString IsValid(const wxString &aVal) const override
Field Value of part, i.e. "3.3K".
Definitions of control validators for schematic dialogs.
#define SHEETFILENAME_V
#define FIELD_NAME
SCH_FIELD_VALIDATOR(bool aIsLibEditor, int aFieldId, wxString *aValue=NULL)
#define _(s)
Definition: 3d_actions.cpp:33
static bool ParseBusVector(const wxString &aBus, wxString *aName, std::vector< wxString > *aMemberList)
Parses a bus vector (e.g.
A text control validator used for validating the text allowed in library and schematic component fiel...
wxString IsValid(const wxString &aVal) const override
Definition: validators.cpp:364
virtual bool Validate(wxWindow *aParent) override
Override the default Validate() function provided by wxTextValidator to provide better error messages...
Field Name Module PCB, i.e. "16DIP300".