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_validators.h>
31 #include <template_fieldnames.h>
32 
34  int aFieldId, wxString* aValue ) :
35  wxTextValidator( wxFILTER_EXCLUDE_CHAR_LIST, aValue )
36 {
37  m_fieldId = aFieldId;
38  m_isLibEditor = aIsCmplibEditor;
39 
40  // Fields cannot contain carriage returns, line feeds, or tabs.
41  wxString excludes( "\r\n\t" );
42 
43  // The reference field cannot contain spaces.
44  if( aFieldId == REFERENCE )
45  excludes += " ";
46  else if( aFieldId == VALUE && m_isLibEditor )
47  excludes += " ";
48 
49  long style = GetStyle();
50 
51  // The reference and value fields cannot be empty.
52  if( aFieldId == REFERENCE || aFieldId == VALUE )
53  style |= wxFILTER_EMPTY;
54 
55  SetStyle( style );
56  SetCharExcludes( excludes );
57 }
58 
59 
61  wxTextValidator( aValidator )
62 {
63  m_fieldId = aValidator.m_fieldId;
64  m_isLibEditor = aValidator.m_isLibEditor;
65 }
66 
67 
68 bool SCH_FIELD_VALIDATOR::Validate( wxWindow *aParent )
69 {
70  // If window is disabled, simply return
71  if( !m_validatorWindow->IsEnabled() )
72  return true;
73 
74  wxTextEntry * const text = GetTextEntry();
75 
76  if( !text )
77  return false;
78 
79  wxString val( text->GetValue() );
80  wxString tmp = val.Clone(); // For trailing and leading white space tests.
81  wxString fieldName;
82 
83  switch( m_fieldId )
84  {
85  case REFERENCE: fieldName = _( "reference designator" ); break;
86  case VALUE: fieldName = _( "value" ); break;
87  case FOOTPRINT: fieldName = _( "footprint" ); break;
88  case DATASHEET: fieldName = _( "data sheet" ); break;
89  default: fieldName = _( "user defined" ); break;
90  };
91 
92  wxString errorMsg;
93 
94  // We can only do some kinds of validation once the input is complete, so
95  // check for them here:
96  if( HasFlag( wxFILTER_EMPTY ) && val.empty() )
97  errorMsg.Printf( _( "The %s field cannot be empty." ), fieldName );
98  else if( HasFlag( wxFILTER_EXCLUDE_CHAR_LIST ) && ContainsExcludedCharacters( val ) )
99  {
100  wxArrayString whiteSpace;
101  bool spaceIllegal = ( m_fieldId == REFERENCE ) ||
102  ( m_fieldId == VALUE && m_isLibEditor );
103 
104  if( val.Find( '\r' ) != wxNOT_FOUND )
105  whiteSpace.Add( _( "carriage return" ) );
106  if( val.Find( '\n' ) != wxNOT_FOUND )
107  whiteSpace.Add( _( "line feed" ) );
108  if( val.Find( '\t' ) != wxNOT_FOUND )
109  whiteSpace.Add( _( "tab" ) );
110  if( spaceIllegal && (val.Find( ' ' ) != wxNOT_FOUND) )
111  whiteSpace.Add( _( "space" ) );
112 
113  wxString badChars;
114 
115  if( whiteSpace.size() == 1 )
116  badChars = whiteSpace[0];
117  else if( whiteSpace.size() == 2 )
118  badChars.Printf( _( "%s or %s" ), whiteSpace[0], whiteSpace[1] );
119  else if( whiteSpace.size() == 3 )
120  badChars.Printf( _( "%s, %s, or %s" ), whiteSpace[0], whiteSpace[1], whiteSpace[2] );
121  else if( whiteSpace.size() == 4 )
122  badChars.Printf( _( "%s, %s, %s, or %s" ),
123  whiteSpace[0], whiteSpace[1], whiteSpace[2], whiteSpace[3] );
124  else
125  wxCHECK_MSG( false, true, wxT( "Invalid illegal character in field validator." ) );
126 
127  errorMsg.Printf( _( "The %s field cannot contain %s characters." ), fieldName, badChars );
128  }
129 
130  if ( !errorMsg.empty() )
131  {
132  m_validatorWindow->SetFocus();
133 
134  wxMessageBox( errorMsg, _( "Field Validation Error" ),
135  wxOK | wxICON_EXCLAMATION, aParent );
136 
137  return false;
138  }
139 
140  return true;
141 }
name of datasheet
SCH_FIELD_VALIDATOR(bool aIsCmplibEditor, int aFieldId, wxString *aValue=NULL)
Field Name Module PCB, i.e. "16DIP300".
Field Reference of part, i.e. "IC21".
Defintions of control validators for schematic dialogs.
Field Value of part, i.e. "3.3K".
class SCH_FILED_VALIDATOR
virtual bool Validate(wxWindow *aParent) override
Function Validate.