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 
33 
34 SCH_FIELD_VALIDATOR::SCH_FIELD_VALIDATOR( bool aIsLibEditor, int aFieldId, wxString* aValue ) :
35  wxTextValidator( wxFILTER_EXCLUDE_CHAR_LIST, aValue )
36 {
37  m_fieldId = aFieldId;
38  m_isLibEditor = aIsLibEditor;
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 || aFieldId == FIELD_NAME )
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 FIELD_NAME: fieldName = _( "field name" ); break;
86  case REFERENCE: fieldName = _( "reference field" ); break;
87  case VALUE: fieldName = _( "value field" ); break;
88  case FOOTPRINT: fieldName = _( "footprint field" ); break;
89  case DATASHEET: fieldName = _( "datasheet field" ); break;
90  default: fieldName = _( "user defined field" ); break;
91  };
92 
93  wxString msg;
94 
95  // We can only do some kinds of validation once the input is complete, so
96  // check for them here:
97  if( HasFlag( wxFILTER_EMPTY ) && val.empty() )
98  msg.Printf( _( "The %s cannot be empty." ), fieldName );
99  else if( HasFlag( wxFILTER_EXCLUDE_CHAR_LIST ) && ContainsExcludedCharacters( val ) )
100  {
101  wxArrayString whiteSpace;
102  bool spaceIllegal = ( m_fieldId == REFERENCE ) ||
103  ( m_fieldId == VALUE && m_isLibEditor );
104 
105  if( val.Find( '\r' ) != wxNOT_FOUND )
106  whiteSpace.Add( _( "carriage return" ) );
107  if( val.Find( '\n' ) != wxNOT_FOUND )
108  whiteSpace.Add( _( "line feed" ) );
109  if( val.Find( '\t' ) != wxNOT_FOUND )
110  whiteSpace.Add( _( "tab" ) );
111  if( spaceIllegal && (val.Find( ' ' ) != wxNOT_FOUND) )
112  whiteSpace.Add( _( "space" ) );
113 
114  wxString badChars;
115 
116  if( whiteSpace.size() == 1 )
117  badChars = whiteSpace[0];
118  else if( whiteSpace.size() == 2 )
119  badChars.Printf( _( "%s or %s" ), whiteSpace[0], whiteSpace[1] );
120  else if( whiteSpace.size() == 3 )
121  badChars.Printf( _( "%s, %s, or %s" ), whiteSpace[0], whiteSpace[1], whiteSpace[2] );
122  else if( whiteSpace.size() == 4 )
123  badChars.Printf( _( "%s, %s, %s, or %s" ),
124  whiteSpace[0], whiteSpace[1], whiteSpace[2], whiteSpace[3] );
125  else
126  wxCHECK_MSG( false, true, wxT( "Invalid illegal character in field validator." ) );
127 
128  msg.Printf( _( "The %s cannot contain %s characters." ), fieldName, badChars );
129  }
130 
131  if ( !msg.empty() )
132  {
133  m_validatorWindow->SetFocus();
134 
135  wxMessageBox( msg, _( "Field Validation Error" ), wxOK | wxICON_EXCLAMATION, aParent );
136 
137  return false;
138  }
139 
140  return true;
141 }
name of datasheet
Field Name Module PCB, i.e. "16DIP300".
Field Reference of part, i.e. "IC21".
Defintions of control validators for schematic dialogs.
#define FIELD_NAME
SCH_FIELD_VALIDATOR(bool aIsLibEditor, int aFieldId, wxString *aValue=NULL)
class SCH_FILED_VALIDATOR
virtual bool Validate(wxWindow *aParent) override
Function Validate.
#define VALUE