KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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, [email protected]
5 * Copyright (C) 2016-2023 KiCad Developers, see AUTHORS.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>
36#include <template_fieldnames.h>
37
38
39SCH_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( wxT( "\r\n\t" ) );
47
48 // The reference and sheet name fields cannot contain spaces.
49 if( aFieldId == REFERENCE_FIELD )
50 {
51 excludes += wxT( " " );
52 }
53 else if( m_fieldId == SHEETNAME_V )
54 {
55 excludes += wxT( "/" );
56 }
57
58 long style = GetStyle();
59
60 // The reference, sheetname and sheetfilename fields cannot be empty.
61 if( aFieldId == REFERENCE_FIELD
62 || aFieldId == SHEETNAME_V
63 || aFieldId == SHEETFILENAME_V )
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
81bool SCH_FIELD_VALIDATOR::Validate( wxWindow* aParent )
82{
83 // If window is disabled, simply return
84 if( !m_validatorWindow->IsEnabled() )
85 return true;
86
87 wxTextEntry* const text = GetTextEntry();
88
89 if( !text )
90 return false;
91
92 wxString val( text->GetValue() );
93 wxString msg;
94
95 if( HasFlag( wxFILTER_EMPTY ) && val.empty() )
96 msg.Printf( _( "The value of the field cannot be empty." ) );
97
98 if( HasFlag( wxFILTER_EXCLUDE_CHAR_LIST ) && ContainsExcludedCharacters( val ) )
99 {
100 wxArrayString badCharsFound;
101
102#if wxCHECK_VERSION( 3, 1, 3 )
103 for( const wxUniCharRef& excludeChar : GetCharExcludes() )
104 {
105 if( val.Find( excludeChar ) != wxNOT_FOUND )
106 {
107 if( excludeChar == '\r' )
108 badCharsFound.Add( _( "carriage return" ) );
109 else if( excludeChar == '\n' )
110 badCharsFound.Add( _( "line feed" ) );
111 else if( excludeChar == '\t' )
112 badCharsFound.Add( _( "tab" ) );
113 else if( excludeChar == ' ' )
114 badCharsFound.Add( _( "space" ) );
115 else
116 badCharsFound.Add( wxString::Format( wxT( "'%s'" ), excludeChar ) );
117 }
118 }
119#else
120 for( const wxString& excludeChar : GetExcludes() )
121 {
122 if( val.Find( excludeChar ) != wxNOT_FOUND )
123 {
124 if( excludeChar == wxT( "\r" ) )
125 badCharsFound.Add( _( "carriage return" ) );
126 else if( excludeChar == wxT( "\n" ) )
127 badCharsFound.Add( _( "line feed" ) );
128 else if( excludeChar == wxT( "\t" ) )
129 badCharsFound.Add( _( "tab" ) );
130 else if( excludeChar == wxT( " " ) )
131 badCharsFound.Add( _( "space" ) );
132 else
133 badCharsFound.Add( wxString::Format( wxT( "'%s'" ), excludeChar ) );
134 }
135 }
136#endif
137
138 wxString badChars;
139
140 for( size_t i = 0; i < badCharsFound.GetCount(); i++ )
141 {
142 if( !badChars.IsEmpty() )
143 {
144 if( badCharsFound.GetCount() == 2 )
145 {
146 badChars += _( " or " );
147 }
148 else
149 {
150 if( i < badCharsFound.GetCount() - 2 )
151 badChars += _( ", or " );
152 else
153 badChars += wxT( ", " );
154 }
155 }
156
157 badChars += badCharsFound.Item( i );
158 }
159
160 switch( m_fieldId )
161 {
162 case REFERENCE_FIELD:
163 msg.Printf( _( "The reference designator cannot contain %s character(s)." ), badChars );
164 break;
165
166 case VALUE_FIELD:
167 msg.Printf( _( "The value field cannot contain %s character(s)." ), badChars );
168 break;
169
170 case FOOTPRINT_FIELD:
171 msg.Printf( _( "The footprint field cannot contain %s character(s)." ), badChars );
172 break;
173
174 case DATASHEET_FIELD:
175 msg.Printf( _( "The datasheet field cannot contain %s character(s)." ), badChars );
176 break;
177
178 case SHEETNAME_V:
179 msg.Printf( _( "The sheet name cannot contain %s character(s)." ), badChars );
180 break;
181
182 case SHEETFILENAME_V:
183 msg.Printf( _( "The sheet filename cannot contain %s character(s)." ), badChars );
184 break;
185
186 default:
187 msg.Printf( _( "The field cannot contain %s character(s)." ), badChars );
188 break;
189 };
190 }
191 else if( m_fieldId == REFERENCE_FIELD && val.Contains( wxT( "${" ) ) )
192 {
193 msg.Printf( _( "The reference designator cannot contain text variable references" ) );
194 }
195
196 if ( !msg.empty() )
197 {
198 m_validatorWindow->SetFocus();
199
200 wxMessageBox( msg, _( "Field Validation Error" ), wxOK | wxICON_EXCLAMATION, aParent );
201
202 return false;
203 }
204
205 return true;
206}
207
208
209// Match opening curly brace, preceeded by start-of-line or by a character not including $_^~
210wxRegEx SCH_NETNAME_VALIDATOR::m_busGroupRegex( R"((^|[^$_\^~]){)", wxRE_ADVANCED );
211
212
213wxString SCH_NETNAME_VALIDATOR::IsValid( const wxString& str ) const
214{
215 wxString msg = NETNAME_VALIDATOR::IsValid( str );
216
217 if( !msg.IsEmpty() )
218 return msg;
219
220 // We don't do single-character validation here
221 if( str.Length() == 1 )
222 return wxString();
223
224 // Figuring out if the user "meant" to make a bus group is somewhat tricky because curly
225 // braces are also used for formatting and variable expansion
226
227 if( m_busGroupRegex.Matches( str ) && str.Contains( '}' ) )
228 {
229 if( !NET_SETTINGS::ParseBusGroup( str, nullptr, nullptr ) )
230 return _( "Signal name contains '{' and '}' but is not a valid bus name" );
231 }
232 else if( str.Contains( '[' ) || str.Contains( ']' ) )
233 {
234 if( !NET_SETTINGS::ParseBusVector( str, nullptr, nullptr ) )
235 return _( "Signal name contains '[' or ']' but is not a valid bus name." );
236 }
237
238 return wxString();
239}
wxString IsValid(const wxString &aVal) const override
Definition: validators.cpp:316
static bool ParseBusGroup(const wxString &aGroup, wxString *name, std::vector< wxString > *aMemberList)
Parse a bus group label into the name and a list of components.
static bool ParseBusVector(const wxString &aBus, wxString *aName, std::vector< wxString > *aMemberList)
Parse a bus vector (e.g.
A text control validator used for validating the text allowed in library and schematic symbol fields.
SCH_FIELD_VALIDATOR(bool aIsLibEditor, int aFieldId, wxString *aValue=nullptr)
virtual bool Validate(wxWindow *aParent) override
Override the default Validate() function provided by wxTextValidator to provide better error messages...
static wxRegEx m_busGroupRegex
wxString IsValid(const wxString &aVal) const override
#define _(s)
Definitions of control validators for schematic dialogs.
#define SHEETFILENAME_V
#define SHEETNAME_V
@ DATASHEET_FIELD
name of datasheet
@ FOOTPRINT_FIELD
Field Name Module PCB, i.e. "16DIP300".
@ VALUE_FIELD
Field Value of part, i.e. "3.3K".
@ REFERENCE_FIELD
Field Reference of part, i.e. "IC21".