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 <sch_connection.h>
31 #include <wx/combo.h>
32 #include <sch_validators.h>
33 #include <project/net_settings.h>
34 #include <template_fieldnames.h>
35 
36 
37 SCH_FIELD_VALIDATOR::SCH_FIELD_VALIDATOR( bool aIsLibEditor, int aFieldId, wxString* aValue ) :
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 }
71 
72 
74  wxTextValidator( aValidator )
75 {
76  m_fieldId = aValidator.m_fieldId;
77  m_isLibEditor = aValidator.m_isLibEditor;
78 }
79 
80 
81 bool SCH_FIELD_VALIDATOR::Validate( wxWindow *aParent )
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 }
185 
186 
187 wxString SCH_NETNAME_VALIDATOR::IsValid( const wxString& str ) const
188 {
189  if( NET_SETTINGS::ParseBusGroup( str, nullptr, nullptr ) )
190  return wxString();
191 
192  if( ( str.Contains( '[' ) || str.Contains( ']' ) ) &&
193  !NET_SETTINGS::ParseBusVector( str, nullptr, nullptr ) )
194  return _( "Signal name contains '[' or ']' but is not a valid vector bus name" );
195 
196  return NETNAME_VALIDATOR::IsValid( str );
197 }
name of datasheet
#define SHEETNAME_V
Field Name Module PCB, i.e. "16DIP300".
Field Reference of part, i.e. "IC21".
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
Definitions of control validators for schematic dialogs.
#define SHEETFILENAME_V
#define FIELD_NAME
SCH_FIELD_VALIDATOR(bool aIsLibEditor, int aFieldId, wxString *aValue=NULL)
Field Value of part, i.e. "3.3K".
#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...