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
|
2023-03-19 19:15:02 +00:00
|
|
|
* Copyright (C) 2016-2023 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
|
2023-03-19 19:15:02 +00:00
|
|
|
|| aFieldId == SHEETFILENAME_V )
|
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() );
|
2018-07-21 20:23:13 +00:00
|
|
|
wxString msg;
|
2016-04-02 12:25:44 +00:00
|
|
|
|
|
|
|
if( HasFlag( wxFILTER_EMPTY ) && val.empty() )
|
2023-03-19 19:15:02 +00:00
|
|
|
msg.Printf( _( "The value of the field cannot be empty." ) );
|
|
|
|
|
|
|
|
if( HasFlag( wxFILTER_EXCLUDE_CHAR_LIST ) && ContainsExcludedCharacters( val ) )
|
2016-04-02 12:25:44 +00:00
|
|
|
{
|
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
|
|
|
|
2023-03-19 19:15:02 +00:00
|
|
|
switch( m_fieldId )
|
|
|
|
{
|
|
|
|
case REFERENCE_FIELD:
|
|
|
|
msg.Printf( _( "The reference designator cannot contain %s character(s)." ), badChars );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VALUE_FIELD:
|
|
|
|
msg.Printf( _( "The value field cannot contain %s character(s)." ), badChars );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FOOTPRINT_FIELD:
|
|
|
|
msg.Printf( _( "The footprint field cannot contain %s character(s)." ), badChars );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DATASHEET_FIELD:
|
|
|
|
msg.Printf( _( "The datasheet field cannot contain %s character(s)." ), badChars );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SHEETNAME_V:
|
|
|
|
msg.Printf( _( "The sheet name cannot contain %s character(s)." ), badChars );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SHEETFILENAME_V:
|
|
|
|
msg.Printf( _( "The sheet filename cannot contain %s character(s)." ), badChars );
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
msg.Printf( _( "The field cannot contain %s character(s)." ), badChars );
|
|
|
|
break;
|
|
|
|
};
|
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
|
|
|
}
|