KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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) 2013 Wayne Stambaugh <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 * Copyright (C) 2018 CERN
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
26
27#include <string_utils.h>
28#include <confirm.h>
29#include <name_validation.h>
30#include <validators.h>
31#include <template_fieldnames.h>
32
33#include <wx/grid.h>
34#include <wx/textctrl.h>
35#include <wx/textentry.h>
36#include <wx/log.h>
37#include <wx/combo.h>
38#include <wx/msgdlg.h>
39#include <refdes_utils.h>
40
41
43 wxTextValidator( wxFILTER_EXCLUDE_CHAR_LIST, aValue )
44{
45 SetCharExcludes( GetLibFilenameForbiddenChars() );
46}
47
48
50 wxTextValidator()
51{
52 Connect( wxEVT_CHAR, wxKeyEventHandler( ENV_VAR_NAME_VALIDATOR::OnChar ) );
53}
54
55
57 : wxTextValidator()
58{
59 wxValidator::Copy( val );
60
61 Connect( wxEVT_CHAR, wxKeyEventHandler( ENV_VAR_NAME_VALIDATOR::OnChar ) );
62}
63
64
66{
67 Disconnect( wxEVT_CHAR, wxKeyEventHandler( ENV_VAR_NAME_VALIDATOR::OnChar ) );
68}
69
70
71void ENV_VAR_NAME_VALIDATOR::OnChar( wxKeyEvent& aEvent )
72{
73 if( !m_validatorWindow )
74 {
75 aEvent.Skip();
76 return;
77 }
78
79 int keyCode = aEvent.GetKeyCode();
80
81 // we don't filter special keys and delete
82 if( keyCode < WXK_SPACE || keyCode == WXK_DELETE || keyCode >= WXK_START )
83 {
84 aEvent.Skip();
85 return;
86 }
87
88 wxUniChar c = (wxUChar) keyCode;
89
90 if( c == wxT( '_' ) )
91 {
92 // OK anywhere
93 aEvent.Skip();
94 }
95 else if( wxIsdigit( c ) )
96 {
97 // not as first character
98 long from, to;
99 GetTextEntry()->GetSelection( &from, &to );
100
101 if( from < 1 )
102 wxBell();
103 else
104 aEvent.Skip();
105 }
106 else if( wxIsalpha( c ) )
107 {
108 // Capitals only.
109
110 if( wxIslower( c ) )
111 {
112 // You may wonder why this scope is so twisted, so make yourself comfortable and read:
113 // 1. Changing the keyCode and/or uniChar in the event and passing it on
114 // doesn't work. Some platforms look at the original copy as long as the event
115 // isn't vetoed.
116 // 2. Inserting characters by hand does not move the cursor, meaning either you insert
117 // text backwards (lp:#1798869) or always append, no matter where is the cursor.
118 // wxTextEntry::{Get/Set}InsertionPoint() do not work at all here.
119 // 3. There is wxTextEntry::ForceUpper(), but it is not yet available in common
120 // wxWidgets packages.
121 //
122 // So here we are, with a command event handler that converts
123 // the text to upper case upon every change.
124 wxTextCtrl* textCtrl = dynamic_cast<wxTextCtrl*>( GetTextEntry() );
125
126 if( textCtrl )
127 {
128 textCtrl->Connect( textCtrl->GetId(), wxEVT_COMMAND_TEXT_UPDATED,
129 wxCommandEventHandler( ENV_VAR_NAME_VALIDATOR::OnTextChanged ) );
130 }
131 }
132
133 aEvent.Skip();
134 }
135 else
136 {
137 wxBell();
138 }
139}
140
141
142void ENV_VAR_NAME_VALIDATOR::OnTextChanged( wxCommandEvent& event )
143{
144 wxTextCtrl* textCtrl = dynamic_cast<wxTextCtrl*>( event.GetEventObject() );
145
146 if( textCtrl )
147 {
148 if( !textCtrl->IsModified() )
149 return;
150
151 long insertionPoint = textCtrl->GetInsertionPoint();
152 textCtrl->ChangeValue( textCtrl->GetValue().Upper() );
153 textCtrl->SetInsertionPoint( insertionPoint );
154 textCtrl->Disconnect( textCtrl->GetId(), wxEVT_COMMAND_TEXT_UPDATED );
155 }
156
157 event.Skip();
158}
159
160
162 wxTextValidator(),
163 m_allowSpaces( true )
164{
165}
166
167
169 wxTextValidator( aValidator ),
170 m_allowSpaces( aValidator.m_allowSpaces )
171{
172}
173
174
176 wxTextValidator(),
177 m_allowSpaces( aAllowSpaces )
178{
179}
180
181
182bool NETNAME_VALIDATOR::Validate( wxWindow *aParent )
183{
184 // If window is disabled, simply return
185 if ( !m_validatorWindow->IsEnabled() )
186 return true;
187
188 wxTextEntry * const text = GetTextEntry();
189
190 if ( !text )
191 return false;
192
193 const wxString& errormsg = IsValid( text->GetValue() );
194
195 if( !errormsg.empty() )
196 {
197 m_validatorWindow->SetFocus();
198 wxMessageBox( errormsg, _( "Invalid signal name" ), wxOK | wxICON_EXCLAMATION, aParent );
199 return false;
200 }
201
202 return true;
203}
204
205
206wxString NETNAME_VALIDATOR::IsValid( const wxString& str ) const
207{
208 if( str.Contains( '\r' ) || str.Contains( '\n' ) )
209 return _( "Signal names cannot contain CR or LF characters" );
210
211 if( !m_allowSpaces && ( str.Contains( ' ' ) || str.Contains( '\t' ) ) )
212 return _( "Signal names cannot contain spaces" );
213
214 return wxString();
215}
216
217
218void KIUI::ValidatorTransferToWindowWithoutEvents( wxValidator& aValidator )
219{
220 wxWindow* ctrl = aValidator.GetWindow();
221
222 wxCHECK_RET( ctrl != nullptr, wxS( "Transferring validator data without a control" ) );
223
224 wxEventBlocker orient_update_blocker( ctrl, wxEVT_ANY );
225 aValidator.TransferToWindow();
226}
227
228
229FIELD_VALIDATOR::FIELD_VALIDATOR( FIELD_T aFieldId, wxString* aValue ) :
230 wxTextValidator( wxFILTER_EXCLUDE_CHAR_LIST, aValue ),
231 m_fieldId( aFieldId )
232{
233 // Fields cannot contain carriage returns, line feeds, or tabs.
234 wxString excludes( wxT( "\r\n\t" ) );
235
236 // The reference and sheet name fields cannot contain spaces.
237 if( aFieldId == FIELD_T::REFERENCE )
238 {
239 excludes += wxT( " " );
240 }
241 else if( m_fieldId == FIELD_T::SHEET_NAME )
242 {
243 excludes += wxT( "/" );
244 }
245 else if( aFieldId == FIELD_T::SHEET_REF_SUFFIX )
246 {
247 // '?' marks unannotated references and ',' is reserved for stacked sheet series
248 excludes += wxT( " ?," );
249 }
250
251 long style = GetStyle();
252
253 // The reference, sheetname and sheetfilename fields cannot be empty.
254 if( aFieldId == FIELD_T::REFERENCE
255 || aFieldId == FIELD_T::SHEET_NAME
256 || aFieldId == FIELD_T::SHEET_FILENAME )
257 {
258 style |= wxFILTER_EMPTY;
259 }
260
261 SetStyle( style );
262 SetCharExcludes( excludes );
263}
264
265
267 wxTextValidator( aValidator ),
268 m_fieldId( aValidator.m_fieldId )
269{
270}
271
272
273bool FIELD_VALIDATOR::Validate( wxWindow* aParent )
274{
275 // If window is disabled, simply return
276 if( !m_validatorWindow->IsEnabled() )
277 return true;
278
279 wxTextEntry* const text = GetTextEntry();
280
281 if( !text )
282 return false;
283
284 wxString val( text->GetValue() );
285
286 return DoValidate( val, aParent );
287}
288
289
290wxString GetFieldValidationErrorMessage( FIELD_T aFieldId, const wxString& aValue )
291{
292 FIELD_VALIDATOR validator( aFieldId );
293 wxString msg;
294 wxString value = aValue;
295
296 // Editors and rendering each unescape, so {brace}comma} must not survive as a hidden comma
297 if( aFieldId == FIELD_T::SHEET_REF_SUFFIX )
298 {
299 for( wxString next = UnescapeString( value ); next != value; next = UnescapeString( value ) )
300 value = next;
301 }
302
303 if( validator.HasFlag( wxFILTER_EMPTY ) && value.empty() )
304 {
305 switch( aFieldId )
306 {
307 case FIELD_T::SHEET_NAME: msg = _( "A sheet must have a name." ); break;
308 case FIELD_T::SHEET_FILENAME: msg = _( "A sheet must have a file specified." ); break;
309 default: msg = _( "The value of the field cannot be empty." ); break;
310 }
311 }
312
313 if( msg.empty() && validator.HasFlag( wxFILTER_EXCLUDE_CHAR_LIST ) )
314 {
315 wxArrayString badCharsFound;
316
317 for( const wxUniCharRef& excludeChar : validator.GetCharExcludes() )
318 {
319 if( value.Find( excludeChar ) != wxNOT_FOUND )
320 {
321 if( excludeChar == '\r' )
322 badCharsFound.Add( _( "carriage return" ) );
323 else if( excludeChar == '\n' )
324 badCharsFound.Add( _( "line feed" ) );
325 else if( excludeChar == '\t' )
326 badCharsFound.Add( _( "tab" ) );
327 else if( excludeChar == ' ' )
328 badCharsFound.Add( _( "space" ) );
329 else
330 badCharsFound.Add( wxString::Format( wxT( "'%c'" ), excludeChar ) );
331 }
332 }
333
334 if( !badCharsFound.IsEmpty() )
335 {
336 wxString badChars;
337
338 for( size_t i = 0; i < badCharsFound.GetCount(); i++ )
339 {
340 if( !badChars.IsEmpty() )
341 {
342 if( badCharsFound.GetCount() == 2 )
343 {
344 badChars += _( " or " );
345 }
346 else
347 {
348 if( i < badCharsFound.GetCount() - 2 )
349 badChars += _( ", or " );
350 else
351 badChars += wxT( ", " );
352 }
353 }
354
355 badChars += badCharsFound.Item( i );
356 }
357
358 switch( aFieldId )
359 {
361 msg.Printf( _( "The reference designator cannot contain %s character(s)." ), badChars );
362 break;
363
364 case FIELD_T::VALUE:
365 msg.Printf( _( "The value field cannot contain %s character(s)." ), badChars );
366 break;
367
369 msg.Printf( _( "The footprint field cannot contain %s character(s)." ), badChars );
370 break;
371
373 msg.Printf( _( "The datasheet field cannot contain %s character(s)." ), badChars );
374 break;
375
377 msg.Printf( _( "The sheet name cannot contain %s character(s)." ), badChars );
378 break;
379
381 msg.Printf( _( "The sheet filename cannot contain %s character(s)." ), badChars );
382 break;
383
385 msg.Printf( _( "The reference suffix cannot contain %s character(s)." ), badChars );
386 break;
387
388 default:
389 msg.Printf( _( "The field cannot contain %s character(s)." ), badChars );
390 break;
391 };
392 }
393 }
394
395 if( msg.empty() )
396 {
397 if( aFieldId == FIELD_T::REFERENCE && value.Contains( wxT( "${" ) ) )
398 {
399 msg.Printf( _( "The reference designator cannot contain text variable references" ) );
400 }
401 else if( aFieldId == FIELD_T::REFERENCE && UTIL::GetRefDesPrefix( value ).IsEmpty() )
402 {
403 msg.Printf( _( "References must start with a letter." ) );
404 }
405 else if( aFieldId == FIELD_T::SHEET_REF_SUFFIX && !value.empty() && wxIsdigit( value[0] ) )
406 {
407 // References end in their number, so R1 with suffix 2 would read as R12
408 msg = _( "The reference suffix cannot start with a digit." );
409 }
410 }
411
412 return msg;
413}
414
415
416bool FIELD_VALIDATOR::DoValidate( const wxString& aValue, wxWindow* aParent )
417{
418 wxString msg = GetFieldValidationErrorMessage( m_fieldId, aValue );
419
420 if( !msg.empty() )
421 {
422 if( m_validatorWindow )
423 m_validatorWindow->SetFocus();
424
425 wxMessageBox( msg, _( "Field Validation Error" ), wxOK | wxICON_EXCLAMATION, aParent );
426
427 return false;
428 }
429
430 return true;
431}
virtual ~ENV_VAR_NAME_VALIDATOR()
void OnChar(wxKeyEvent &event)
ENV_VAR_NAME_VALIDATOR(wxString *aValue=nullptr)
void OnTextChanged(wxCommandEvent &event)
A text control validator used for validating the text allowed in fields.
Definition validators.h:137
virtual bool Validate(wxWindow *aParent) override
Override the default Validate() function provided by wxTextValidator to provide better error messages...
bool DoValidate(const wxString &aValue, wxWindow *aParent)
FIELD_VALIDATOR(FIELD_T aFieldId, wxString *aValue=nullptr)
FIELD_T m_fieldId
Definition validators.h:157
FOOTPRINT_NAME_VALIDATOR(wxString *aValue=nullptr)
wxString IsValid(const wxString &aVal) const override
virtual bool Validate(wxWindow *aParent) override
NETNAME_VALIDATOR(wxString *aVal=nullptr)
This file is part of the common library.
#define _(s)
const wxString & GetLibFilenameForbiddenChars()
Characters illegal in a footprint library filename.
Definition lib_id.cpp:40
void ValidatorTransferToWindowWithoutEvents(wxValidator &aValidator)
Call a text validator's TransferDataToWindow method without firing a text change event.
wxString GetRefDesPrefix(const wxString &aRefDes)
Get the (non-numeric) prefix from a refdes - e.g.
CITER next(CITER it)
Definition ptree.cpp:120
Collection of utility functions for component reference designators (refdes)
wxString UnescapeString(const wxString &aSource)
FIELD_T
The set of all field indices assuming an array like sequence that a SCH_COMPONENT or LIB_PART can hol...
@ FOOTPRINT
Field Name Module PCB, i.e. "16DIP300".
@ SHEET_REF_SUFFIX
Optional sheet field appended to the references of the sheet's symbols.
@ DATASHEET
name of datasheet
@ REFERENCE
Field Reference of part, i.e. "IC21".
@ VALUE
Field Value of part, i.e. "3.3K".
JSON_SCHEMA_VALIDATOR validator(schema)
wxString GetFieldValidationErrorMessage(FIELD_T aFieldId, const wxString &aValue)
Return the error message if aValue is invalid for aFieldId.
Custom text control validator definitions.
wxString GetFieldValidationErrorMessage(FIELD_T aFieldId, const wxString &aValue)
Return the error message if aValue is invalid for aFieldId.