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  wxString tmp = val.Clone(); // For trailing and leading white space tests.
83  wxString fieldName;
84 
85  switch( m_fieldId )
86  {
87  case FIELD_NAME: fieldName = _( "field name" ); break;
88  case FIELD_VALUE: fieldName = _( "field value" ); break;
89  case REFERENCE: fieldName = _( "reference field" ); break;
90  case VALUE: fieldName = _( "value field" ); break;
91  case FOOTPRINT: fieldName = _( "footprint field" ); break;
92  case DATASHEET: fieldName = _( "datasheet field" ); break;
93  default: fieldName = _( "user defined field" ); break;
94  };
95 
96  wxString msg;
97 
98  // We can only do some kinds of validation once the input is complete, so
99  // check for them here:
100  if( HasFlag( wxFILTER_EMPTY ) && val.empty() )
101  msg.Printf( _( "The %s cannot be empty." ), fieldName );
102  else if( HasFlag( wxFILTER_EXCLUDE_CHAR_LIST ) && ContainsExcludedCharacters( val ) )
103  {
104  wxArrayString whiteSpace;
105  bool spaceIllegal = ( m_fieldId == REFERENCE ) ||
106  ( m_fieldId == VALUE && m_isLibEditor );
107 
108  if( val.Find( '\r' ) != wxNOT_FOUND )
109  whiteSpace.Add( _( "carriage return" ) );
110  if( val.Find( '\n' ) != wxNOT_FOUND )
111  whiteSpace.Add( _( "line feed" ) );
112  if( val.Find( '\t' ) != wxNOT_FOUND )
113  whiteSpace.Add( _( "tab" ) );
114  if( spaceIllegal && (val.Find( ' ' ) != wxNOT_FOUND) )
115  whiteSpace.Add( _( "space" ) );
116 
117  wxString badChars;
118 
119  if( whiteSpace.size() == 1 )
120  badChars = whiteSpace[0];
121  else if( whiteSpace.size() == 2 )
122  badChars.Printf( _( "%s or %s" ), whiteSpace[0], whiteSpace[1] );
123  else if( whiteSpace.size() == 3 )
124  badChars.Printf( _( "%s, %s, or %s" ), whiteSpace[0], whiteSpace[1], whiteSpace[2] );
125  else if( whiteSpace.size() == 4 )
126  badChars.Printf( _( "%s, %s, %s, or %s" ),
127  whiteSpace[0], whiteSpace[1], whiteSpace[2], whiteSpace[3] );
128  else
129  wxCHECK_MSG( false, true, wxT( "Invalid illegal character in field validator." ) );
130 
131  msg.Printf( _( "The %s cannot contain %s characters." ), fieldName, badChars );
132  }
133 
134  if ( !msg.empty() )
135  {
136  m_validatorWindow->SetFocus();
137 
138  wxMessageBox( msg, _( "Field Validation Error" ), wxOK | wxICON_EXCLAMATION, aParent );
139 
140  return false;
141  }
142 
143  return true;
144 }
145 
146 
148  wxValidator(), m_allowSpaces( false )
149 {
150 }
151 
152 
154  m_allowSpaces( aValidator.m_allowSpaces )
155 {
156 }
157 
158 
160  wxValidator(), m_allowSpaces( aAllowSpaces )
161 {
162 }
163 
164 
166 {
167 #if wxUSE_TEXTCTRL
168  if( wxDynamicCast( m_validatorWindow, wxTextCtrl ) )
169  return static_cast<wxTextCtrl*>( m_validatorWindow );
170 #endif
171 
172 #if wxUSE_COMBOBOX
173  if( wxDynamicCast( m_validatorWindow, wxComboBox ) )
174  return static_cast<wxComboBox*>( m_validatorWindow );
175 #endif
176 
177 #if wxUSE_COMBOCTRL
178  if( wxDynamicCast( m_validatorWindow, wxComboCtrl ) )
179  return static_cast<wxComboCtrl*>( m_validatorWindow );
180 #endif
181 
182  wxFAIL_MSG( "SCH_NETNAME_VALIDATOR can only be used with wxTextCtrl, wxComboBox, or wxComboCtrl" );
183  return nullptr;
184 }
185 
186 
187 bool SCH_NETNAME_VALIDATOR::Validate( wxWindow *aParent )
188 {
189  // If window is disabled, simply return
190  if ( !m_validatorWindow->IsEnabled() )
191  return true;
192 
193  wxTextEntry * const text = GetTextEntry();
194 
195  if ( !text )
196  return false;
197 
198  const wxString& errormsg = IsValid( text->GetValue() );
199 
200  if( !errormsg.empty() )
201  {
202  m_validatorWindow->SetFocus();
203  wxMessageBox( errormsg, _( "Invalid signal name" ), wxOK | wxICON_EXCLAMATION, aParent );
204  return false;
205  }
206 
207  return true;
208 }
209 
210 
211 wxString SCH_NETNAME_VALIDATOR::IsValid( const wxString& str ) const
212 {
214  return wxString();
215 
216  if( str.Contains( '{' ) || str.Contains( '}' ) )
217  return _( "Signal name contains '{' or '}' but is not a valid group bus name" );
218 
219  if( ( str.Contains( '[' ) || str.Contains( ']' ) ) &&
221  return _( "Signal name contains '[' or ']' but is not a valid vector bus name" );
222 
223  if( str.Contains( '\r' ) || str.Contains( '\n' ) )
224  return _( "Signal names cannot contain CR or LF characters" );
225 
226  if( !m_allowSpaces && ( str.Contains( ' ' ) || str.Contains( '\t' ) ) )
227  return _( "Signal names cannot contain spaces" );
228 
229  return wxString();
230 }
name of datasheet
#define FIELD_VALUE
const string & str
Definition: json11.cpp:596
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
#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)
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...