2016-04-02 12:25:44 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 Wayne Stambaugh, stambaughw@gmail.com
|
2021-05-25 18:10:02 +00:00
|
|
|
* Copyright (C) 2016-2021 KiCad Developers, see AUTHORS.txt for contributors.
|
2016-04-02 12:25:44 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file sch_validators.cpp
|
|
|
|
* @brief Implementation of control validators for schematic dialogs.
|
|
|
|
*/
|
|
|
|
|
2019-05-21 02:52:39 +00:00
|
|
|
#include <wx/combo.h>
|
2020-10-18 11:31:07 +00:00
|
|
|
#include <wx/msgdlg.h>
|
|
|
|
|
|
|
|
#include <sch_connection.h>
|
2016-04-02 12:25:44 +00:00
|
|
|
#include <sch_validators.h>
|
2020-07-06 10:51:04 +00:00
|
|
|
#include <project/net_settings.h>
|
2016-04-02 12:25:44 +00:00
|
|
|
#include <template_fieldnames.h>
|
|
|
|
|
2018-07-21 20:23:13 +00:00
|
|
|
|
|
|
|
SCH_FIELD_VALIDATOR::SCH_FIELD_VALIDATOR( bool aIsLibEditor, int aFieldId, wxString* aValue ) :
|
2016-04-02 12:25:44 +00:00
|
|
|
wxTextValidator( wxFILTER_EXCLUDE_CHAR_LIST, aValue )
|
|
|
|
{
|
|
|
|
m_fieldId = aFieldId;
|
2018-07-21 20:23:13 +00:00
|
|
|
m_isLibEditor = aIsLibEditor;
|
2016-04-02 12:25:44 +00:00
|
|
|
|
|
|
|
// Fields cannot contain carriage returns, line feeds, or tabs.
|
2021-05-25 18:10:02 +00:00
|
|
|
wxString excludes( wxT( "\r\n\t" ) );
|
2016-04-02 12:25:44 +00:00
|
|
|
|
2021-05-25 18:10:02 +00:00
|
|
|
// The reference and sheet name fields cannot contain spaces.
|
2021-05-26 19:26:48 +00:00
|
|
|
if( aFieldId == REFERENCE_FIELD )
|
2020-05-03 14:52:31 +00:00
|
|
|
{
|
2021-05-25 18:10:02 +00:00
|
|
|
excludes += wxT( " " );
|
2020-05-03 14:52:31 +00:00
|
|
|
}
|
2021-05-26 19:26:48 +00:00
|
|
|
else if( m_fieldId == SHEETNAME_V )
|
|
|
|
{
|
2021-06-30 10:53:04 +00:00
|
|
|
excludes += wxT( "/" );
|
2020-10-06 13:17:45 +00:00
|
|
|
}
|
2016-04-02 12:25:44 +00:00
|
|
|
|
|
|
|
long style = GetStyle();
|
|
|
|
|
2023-01-07 19:18:37 +00:00
|
|
|
// The reference, sheetname and sheetfilename fields cannot be empty.
|
2020-11-12 21:31:41 +00:00
|
|
|
if( aFieldId == REFERENCE_FIELD
|
2021-05-25 18:10:02 +00:00
|
|
|
|| aFieldId == SHEETNAME_V
|
|
|
|
|| aFieldId == SHEETFILENAME_V
|
|
|
|
|| aFieldId == FIELD_NAME )
|
2020-05-03 14:52:31 +00:00
|
|
|
{
|
2016-04-02 12:25:44 +00:00
|
|
|
style |= wxFILTER_EMPTY;
|
2020-05-03 14:52:31 +00:00
|
|
|
}
|
2016-04-02 12:25:44 +00:00
|
|
|
|
|
|
|
SetStyle( style );
|
|
|
|
SetCharExcludes( excludes );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SCH_FIELD_VALIDATOR::SCH_FIELD_VALIDATOR( const SCH_FIELD_VALIDATOR& aValidator ) :
|
|
|
|
wxTextValidator( aValidator )
|
|
|
|
{
|
|
|
|
m_fieldId = aValidator.m_fieldId;
|
2016-08-19 16:42:39 +00:00
|
|
|
m_isLibEditor = aValidator.m_isLibEditor;
|
2016-04-02 12:25:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-25 18:10:02 +00:00
|
|
|
bool SCH_FIELD_VALIDATOR::Validate( wxWindow* aParent )
|
2016-04-02 12:25:44 +00:00
|
|
|
{
|
|
|
|
// If window is disabled, simply return
|
2021-03-29 21:10:32 +00:00
|
|
|
if( !m_validatorWindow->IsEnabled() )
|
2016-04-02 12:25:44 +00:00
|
|
|
return true;
|
|
|
|
|
2021-05-25 18:10:02 +00:00
|
|
|
wxTextEntry* const text = GetTextEntry();
|
2016-04-02 12:25:44 +00:00
|
|
|
|
|
|
|
if( !text )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
wxString val( text->GetValue() );
|
2019-12-14 10:40:37 +00:00
|
|
|
|
|
|
|
// The format of the error message for not allowed chars
|
|
|
|
wxString fieldCharError;
|
2016-04-02 12:25:44 +00:00
|
|
|
|
|
|
|
switch( m_fieldId )
|
|
|
|
{
|
2020-11-12 21:31:41 +00:00
|
|
|
case REFERENCE_FIELD:
|
2020-03-17 16:08:29 +00:00
|
|
|
fieldCharError = _( "The reference designator cannot contain %s character(s)." );
|
2019-12-14 10:40:37 +00:00
|
|
|
break;
|
|
|
|
|
2020-11-12 21:31:41 +00:00
|
|
|
case VALUE_FIELD:
|
2019-12-14 10:40:37 +00:00
|
|
|
fieldCharError = _( "The value field cannot contain %s character(s)." );
|
|
|
|
break;
|
|
|
|
|
2020-11-12 21:31:41 +00:00
|
|
|
case FOOTPRINT_FIELD:
|
2019-12-14 10:40:37 +00:00
|
|
|
fieldCharError = _( "The footprint field cannot contain %s character(s)." );
|
|
|
|
break;
|
|
|
|
|
2020-11-12 21:31:41 +00:00
|
|
|
case DATASHEET_FIELD:
|
2019-12-14 10:40:37 +00:00
|
|
|
fieldCharError = _( "The datasheet field cannot contain %s character(s)." );
|
|
|
|
break;
|
|
|
|
|
2020-05-03 14:52:31 +00:00
|
|
|
case SHEETNAME_V:
|
|
|
|
fieldCharError = _( "The sheet name cannot contain %s character(s)." );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SHEETFILENAME_V:
|
|
|
|
fieldCharError = _( "The sheet filename cannot contain %s character(s)." );
|
|
|
|
break;
|
|
|
|
|
2019-12-14 10:40:37 +00:00
|
|
|
default:
|
|
|
|
fieldCharError = _( "The field cannot contain %s character(s)." );
|
|
|
|
break;
|
2016-04-02 12:25:44 +00:00
|
|
|
};
|
|
|
|
|
2018-07-21 20:23:13 +00:00
|
|
|
wxString msg;
|
2016-04-02 12:25:44 +00:00
|
|
|
|
|
|
|
// We can only do some kinds of validation once the input is complete, so
|
|
|
|
// check for them here:
|
|
|
|
if( HasFlag( wxFILTER_EMPTY ) && val.empty() )
|
2019-12-14 10:40:37 +00:00
|
|
|
{
|
|
|
|
// Some fields cannot have an empty value, and user fields require a name:
|
|
|
|
if( m_fieldId == FIELD_NAME )
|
|
|
|
msg.Printf( _( "The name of the field cannot be empty." ) );
|
2020-11-12 21:31:41 +00:00
|
|
|
else // the FIELD_VALUE id or REFERENCE_FIELD or VALUE_FIELD
|
2019-12-14 10:40:37 +00:00
|
|
|
msg.Printf( _( "The value of the field cannot be empty." ) );
|
|
|
|
}
|
2016-04-02 12:25:44 +00:00
|
|
|
else if( HasFlag( wxFILTER_EXCLUDE_CHAR_LIST ) && ContainsExcludedCharacters( val ) )
|
|
|
|
{
|
2021-05-25 18:10:02 +00:00
|
|
|
wxArrayString badCharsFound;
|
|
|
|
|
2023-03-06 14:13:35 +00:00
|
|
|
#if wxCHECK_VERSION( 3, 1, 3 )
|
2023-03-02 17:50:39 +00:00
|
|
|
for( const wxUniCharRef& excludeChar : GetCharExcludes() )
|
2021-05-25 18:10:02 +00:00
|
|
|
{
|
|
|
|
if( val.Find( excludeChar ) != wxNOT_FOUND )
|
|
|
|
{
|
2023-03-02 17:50:39 +00:00
|
|
|
if( excludeChar == '\r' )
|
2021-05-25 18:10:02 +00:00
|
|
|
badCharsFound.Add( _( "carriage return" ) );
|
2023-03-02 17:50:39 +00:00
|
|
|
else if( excludeChar == '\n' )
|
2021-05-25 18:10:02 +00:00
|
|
|
badCharsFound.Add( _( "line feed" ) );
|
2023-03-02 17:50:39 +00:00
|
|
|
else if( excludeChar == '\t' )
|
2021-05-25 18:10:02 +00:00
|
|
|
badCharsFound.Add( _( "tab" ) );
|
2023-03-02 17:50:39 +00:00
|
|
|
else if( excludeChar == ' ' )
|
2021-05-25 18:10:02 +00:00
|
|
|
badCharsFound.Add( _( "space" ) );
|
|
|
|
else
|
|
|
|
badCharsFound.Add( wxString::Format( wxT( "'%s'" ), excludeChar ) );
|
|
|
|
}
|
|
|
|
}
|
2023-03-06 14:13:35 +00:00
|
|
|
#else
|
|
|
|
for( const wxString& excludeChar : GetExcludes() )
|
|
|
|
{
|
|
|
|
if( val.Find( excludeChar ) != wxNOT_FOUND )
|
|
|
|
{
|
|
|
|
if( excludeChar == wxT( "\r" ) )
|
|
|
|
badCharsFound.Add( _( "carriage return" ) );
|
|
|
|
else if( excludeChar == wxT( "\n" ) )
|
|
|
|
badCharsFound.Add( _( "line feed" ) );
|
|
|
|
else if( excludeChar == wxT( "\t" ) )
|
|
|
|
badCharsFound.Add( _( "tab" ) );
|
|
|
|
else if( excludeChar == wxT( " " ) )
|
|
|
|
badCharsFound.Add( _( "space" ) );
|
|
|
|
else
|
|
|
|
badCharsFound.Add( wxString::Format( wxT( "'%s'" ), excludeChar ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2016-04-02 12:25:44 +00:00
|
|
|
|
|
|
|
wxString badChars;
|
|
|
|
|
2021-05-25 18:10:02 +00:00
|
|
|
for( size_t i = 0; i < badCharsFound.GetCount(); i++ )
|
|
|
|
{
|
|
|
|
if( !badChars.IsEmpty() )
|
|
|
|
{
|
|
|
|
if( badCharsFound.GetCount() == 2 )
|
|
|
|
{
|
|
|
|
badChars += _( " or " );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( i < badCharsFound.GetCount() - 2 )
|
|
|
|
badChars += _( ", or " );
|
|
|
|
else
|
|
|
|
badChars += wxT( ", " );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
badChars += badCharsFound.Item( i );
|
|
|
|
}
|
2016-04-02 12:25:44 +00:00
|
|
|
|
2019-12-14 10:40:37 +00:00
|
|
|
msg.Printf( fieldCharError, badChars );
|
2016-04-02 12:25:44 +00:00
|
|
|
}
|
2020-11-12 21:31:41 +00:00
|
|
|
else if( m_fieldId == REFERENCE_FIELD && val.Contains( wxT( "${" ) ) )
|
2020-08-21 22:07:23 +00:00
|
|
|
{
|
|
|
|
msg.Printf( _( "The reference designator cannot contain text variable references" ) );
|
|
|
|
}
|
2016-04-02 12:25:44 +00:00
|
|
|
|
2018-07-21 20:23:13 +00:00
|
|
|
if ( !msg.empty() )
|
2016-04-02 12:25:44 +00:00
|
|
|
{
|
|
|
|
m_validatorWindow->SetFocus();
|
|
|
|
|
2018-07-21 20:23:13 +00:00
|
|
|
wxMessageBox( msg, _( "Field Validation Error" ), wxOK | wxICON_EXCLAMATION, aParent );
|
2016-04-02 12:25:44 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2019-05-21 02:52:39 +00:00
|
|
|
|
|
|
|
|
2021-07-05 19:01:25 +00:00
|
|
|
// Match opening curly brace, preceeded by start-of-line or by a character not including $_^~
|
|
|
|
wxRegEx SCH_NETNAME_VALIDATOR::m_busGroupRegex( R"((^|[^$_\^~]){)", wxRE_ADVANCED );
|
2021-07-03 21:25:29 +00:00
|
|
|
|
|
|
|
|
2019-05-21 02:52:39 +00:00
|
|
|
wxString SCH_NETNAME_VALIDATOR::IsValid( const wxString& str ) const
|
|
|
|
{
|
2021-09-17 19:45:01 +00:00
|
|
|
wxString msg = NETNAME_VALIDATOR::IsValid( str );
|
|
|
|
|
|
|
|
if( !msg.IsEmpty() )
|
|
|
|
return msg;
|
|
|
|
|
2021-07-03 21:25:29 +00:00
|
|
|
// We don't do single-character validation here
|
|
|
|
if( str.Length() == 1 )
|
2019-05-21 02:52:39 +00:00
|
|
|
return wxString();
|
|
|
|
|
2021-09-17 19:45:01 +00:00
|
|
|
// Figuring out if the user "meant" to make a bus group is somewhat tricky because curly
|
2021-07-05 19:01:25 +00:00
|
|
|
// braces are also used for formatting and variable expansion
|
2021-07-03 21:25:29 +00:00
|
|
|
|
2021-09-17 19:45:01 +00:00
|
|
|
if( m_busGroupRegex.Matches( str ) && str.Contains( '}' ) )
|
|
|
|
{
|
|
|
|
if( !NET_SETTINGS::ParseBusGroup( str, nullptr, nullptr ) )
|
|
|
|
return _( "Signal name contains '{' and '}' but is not a valid bus name" );
|
|
|
|
}
|
|
|
|
else if( str.Contains( '[' ) || str.Contains( ']' ) )
|
2021-07-03 21:25:29 +00:00
|
|
|
{
|
2021-09-17 19:45:01 +00:00
|
|
|
if( !NET_SETTINGS::ParseBusVector( str, nullptr, nullptr ) )
|
|
|
|
return _( "Signal name contains '[' or ']' but is not a valid bus name." );
|
2021-07-03 21:25:29 +00:00
|
|
|
}
|
2019-05-21 02:52:39 +00:00
|
|
|
|
2021-09-17 19:45:01 +00:00
|
|
|
return wxString();
|
2019-05-21 02:52:39 +00:00
|
|
|
}
|